blob: 9fa219064eac4fa3fd34665a7f63f61e30e4e9df [file] [log] [blame]
Brian O'Connorb86c9202016-04-05 20:15:04 -07001import random
Brian O'Connor42c38cf2016-04-05 17:05:57 -07002
3DEBUG_ARG='JAVA_TOOL_OPTIONS="-Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=5005,suspend=y"'
Brian O'Connore5817c92016-04-06 15:41:48 -07004FORCE_INSTALL=True
5NONE='NONE'
Brian O'Connor42c38cf2016-04-05 17:05:57 -07006
Brian O'Connore4da59d2016-04-08 00:32:18 -07007SRC = 'src/main/java/**/'
8TEST = 'src/test/java/**/'
Brian O'Connor0f6677d2016-04-06 23:26:51 -07009RESOURCES_ROOT = 'src/main/resources/'
Brian O'Connore4da59d2016-04-08 00:32:18 -070010TEST_RESOURCES_ROOT = 'src/test/resources/'
11
Ray Milkey171b89a2016-07-28 15:22:26 -070012include_defs('//onos.defs')
Brian O'Connore4da59d2016-04-08 00:32:18 -070013
14def _get_name():
15 base_path = get_base_path()
16 return ONOS_ARTIFACT_BASE + base_path.replace('/', '-') #TODO Unix-separator
Brian O'Connor1f165982016-04-06 21:36:09 -070017
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070018def checkstyle(
19 name,
20 srcs = None,
21 jar_target = None,
22 ):
23
24 if srcs and jar_target:
25 base = get_base_path()
26 files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs])
27
28 genrule(
29 name = name + '-checkstyle-files',
30 bash = "echo '%s' > $OUT" % files,
31 srcs = srcs,
32 out = 'checkstyle-files.txt',
33 )
34
35 sh_test(
36 name = name + '-checkstyle',
Thomas Vachuska275d2e82016-07-14 17:41:34 -070037 test = '//tools/build/conf:start-buck-daemon',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070038 deps = [ jar_target ],
39 args = [
Thomas Vachuska275d2e82016-07-14 17:41:34 -070040 '$(location //tools/build/conf:buck-daemon-jar)',
41 'checkstyle',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070042 '$(location :' + name + '-checkstyle-files)',
43 '$(location //tools/build/conf:checkstyle-xml)',
44 '$(location //tools/build/conf:suppressions-xml)',
45 ],
46 test_rule_timeout_ms = 20000,
47 labels = [ 'checkstyle' ],
48 )
Ray Milkey2a749832016-08-02 15:22:20 -070049 else:
50 print 'Not generating checkstyle rule for %s because there are no sources.' % name
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070051
Ray Milkeyc340e282016-06-30 14:45:42 -070052def java_doc(
53 name,
54 title,
55 pkgs,
56 paths,
57 srcs = [],
58 deps = [],
59 visibility = [],
60 do_it_wrong = False,
61 ):
62 if do_it_wrong:
63 sourcepath = paths
64 else:
65 sourcepath = ['$SRCDIR/' + n for n in paths]
Ray Milkey2a749832016-08-02 15:22:20 -070066
67 if len(srcs) != 0:
Ray Milkeyc340e282016-06-30 14:45:42 -070068 cmd = ' '.join([
Ray Milkey2a749832016-08-02 15:22:20 -070069 'while ! test -f .buckconfig; do cd ..; done;',
70 'javadoc',
71 '-tag onos.rsModel:a:"onos model"',
72 '-quiet',
73 '-protected',
74 '-encoding UTF-8',
75 '-charset UTF-8',
76 '-notimestamp',
77 '-windowtitle "' + title + '"',
78 '-link http://docs.oracle.com/javase/8/docs/api',
79 '-subpackages ',
80 ':'.join(pkgs),
81 '-sourcepath ',
82 ':'.join(sourcepath),
83 ' -classpath ',
84 ':'.join(['$(classpath %s)' % n for n in deps]),
85 '-d $TMP',
86 ]) + ';jar cf $OUT -C $TMP .'
87
88 genrule(
89 name = name,
90 cmd = cmd,
91 srcs = srcs,
92 out = name + '.jar',
93 visibility = visibility,
Ray Milkeyc340e282016-06-30 14:45:42 -070094)
95
96
Brian O'Connor42c38cf2016-04-05 17:05:57 -070097def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070098 name = None,
99 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -0700100 group_id = ONOS_GROUP_ID,
101 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700102 deps = [],
103 visibility = ['PUBLIC'],
104 license = 'NONE',
105 description = '',
106 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700107 import_packages = '*',
108 export_packages = '*',
Ray Milkey2a749832016-08-02 15:22:20 -0700109 package_name_root = 'org.onosproject',
Brian O'Connore5817c92016-04-06 15:41:48 -0700110 include_resources = NONE,
111 web_context = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700112 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700113 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700114 **kwargs
115 ):
116
Brian O'Connore4da59d2016-04-08 00:32:18 -0700117 # if name and _get_name() != name:
118 # print _get_name(), '!=', name
119 if name is None:
120 name = _get_name()
121
122 if srcs is None:
123 srcs = glob([SRC + '/*.java'])
124
125 if resources == NONE and resources_root is not None:
126 resources = glob([resources_root + '**'])
127 elif resources == NONE:
128 resources = glob([RESOURCES_ROOT + '**'])
129
130 if resources and not resources_root:
131 resources_root = RESOURCES_ROOT
132
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700133 bare_jar_name = name + '-jar'
134 osgi_jar_name = name + '-osgi'
135 mvn_coords = group_id + ':' + name + ':' + version
136
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700137
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700138 java_library(
139 name = bare_jar_name,
140 srcs = srcs,
141 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700142 visibility = [], #intentially, not visible
143 resources = resources,
144 resources_root = resources_root,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700145 **kwargs
146 )
147
Brian O'Connor4847ea32016-04-29 16:33:06 -0700148 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700149
150 args = ( '$(location :%s)' % bare_jar_name, #input jar
151 '$OUT', #output jar
152 cp, #classpath
153 name, #bundle name
154 group_id, #group id
155 version, #version
156 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700157 "'%s'" % import_packages, #packages to import
158 "'%s'" % export_packages, #packages to export
159 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700160 web_context, #web context (REST API only)
161 description, #description
162 )
163
164 #TODO stage_jar is a horrendous hack
165 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
166 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
167 bash = stage_jar + wrap_jar
168 if debug:
169 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
170 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700171
172 genrule(
173 name = osgi_jar_name,
174 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700175 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700176 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700177 visibility = [], #intentially, not visible
178 )
179
180 # TODO we really should shade the jar with maven flavor
181 prebuilt_jar(
182 name = name,
183 maven_coords = mvn_coords,
184 binary_jar = ':' + osgi_jar_name,
185 visibility = visibility,
186 )
187
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700188 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700189 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700190 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700191 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700192 jar_target = ':'+ bare_jar_name,
193 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700194
Ray Milkeyc340e282016-06-30 14:45:42 -0700195 java_doc(
196 name = name + '-javadoc',
197 title = 'Java Docs',
Ray Milkey2a749832016-08-02 15:22:20 -0700198 pkgs = [ package_name_root ],
Ray Milkeyc340e282016-06-30 14:45:42 -0700199 paths = [ 'src/main/java' ],
200 srcs = srcs,
201 deps = deps,
202 visibility = visibility,
203 do_it_wrong = False,
204 )
205
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700206 # TODO add project config for intellij
207 # project_config(
208 # src_target = ':' + bare_jar_name,
209 # src_roots = [ 'src/main/java' ],
210 # test_target = ':' + name + '-tests',
211 # test_roots = [ 'src/test/java' ],
212 # )
213
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700214 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700215 mvn_cmd = ' '.join(( 'mvn install:install-file',
216 '-Dfile=$(location :%s)' % name,
217 '-DgroupId=%s' % group_id,
218 '-DartifactId=%s' % name,
219 '-Dversion=%s' % version,
220 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700221 cmd = mvn_cmd + ' > $OUT'
222 if FORCE_INSTALL:
223 # Add a random number to the command to force this rule to run.
224 # TODO We should make this configurable from CLI, perhaps with a flag.
225 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700226 genrule(
227 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700228 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700229 out = 'install.log',
230 visibility = visibility,
231 )
232
Brian O'Connore4da59d2016-04-08 00:32:18 -0700233def osgi_jar_with_tests(
234 name = None,
235 deps = [],
236 test_srcs = None,
237 test_deps = [ '//lib:TEST' ],
238 test_resources = None,
239 test_resources_root = None,
240 visibility = [ 'PUBLIC' ],
241 **kwargs
242 ):
243
244 if name is None:
245 name = _get_name()
246
247 osgi_jar(name = name,
248 deps = deps,
249 visibility = visibility,
250 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700251
252 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700253 test_resources_root = TEST_RESOURCES_ROOT
254 if test_resources_root and not test_resources:
255 test_resources = glob([test_resources_root + '**'])
256 if not test_resources and not test_resources_root:
257 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
258 if test_resources:
259 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700260
Brian O'Connore4da59d2016-04-08 00:32:18 -0700261 if test_srcs is None:
262 test_srcs = glob([TEST + '/*.java'])
263
Ray Milkey2a749832016-08-02 15:22:20 -0700264 if not test_srcs:
265 print "Generating test rule for %s, but there are no tests." % name
266
Brian O'Connore4da59d2016-04-08 00:32:18 -0700267 java_test(
268 name = name + '-tests',
269 srcs = test_srcs,
270 deps = deps +
271 test_deps +
272 [':' + name + '-jar'],
273 source_under_test = [':' + name + '-jar'],
274 resources = test_resources,
275 resources_root = test_resources_root,
276 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700277 )
278
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700279 checkstyle(
280 name = name + '-tests',
281 srcs = test_srcs,
282 jar_target = ':' + name + '-tests',
283 )
284
Brian O'Connore642f7c2016-05-23 18:33:04 -0700285 #FIXME need to run checkstyle on test sources