blob: 351212c4fb1999a757ca0e51832084d8ff4ba662 [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
Yuta HIGUCHI295ee072017-06-21 15:48:21 -070024 if srcs:
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070025 base = get_base_path()
Brian O'Connorbd0e7202017-11-27 18:40:08 -080026 # module name; base filepath; files (line-separated); empty line (to signify end of stream)
27 files = '%s\n%s\n' % (name, base) + '\n'.join(['%s/%s' % (base, s) for s in srcs]) + '\n\n'
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070028
29 genrule(
30 name = name + '-checkstyle-files',
31 bash = "echo '%s' > $OUT" % files,
32 srcs = srcs,
33 out = 'checkstyle-files.txt',
34 )
35
36 sh_test(
37 name = name + '-checkstyle',
Thomas Vachuska275d2e82016-07-14 17:41:34 -070038 test = '//tools/build/conf:start-buck-daemon',
Yuta HIGUCHI295ee072017-06-21 15:48:21 -070039 deps = [ ':' + name + '-checkstyle-files',
Ray Milkey2d572dd2017-04-14 10:01:24 -070040 '//tools/build/conf:onos-java-header',
41 '//tools/build/conf:onos-build-conf', ],
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070042 args = [
Thomas Vachuska275d2e82016-07-14 17:41:34 -070043 '$(location //tools/build/conf:buck-daemon-jar)',
44 'checkstyle',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070045 '$(location :' + name + '-checkstyle-files)',
46 '$(location //tools/build/conf:checkstyle-xml)',
47 '$(location //tools/build/conf:suppressions-xml)',
48 ],
Brian O'Connorbd0e7202017-11-27 18:40:08 -080049 #TODO test_rule_timeout_ms seems to be ignored on Linux
Ray Milkey84dafd92017-10-25 16:20:23 -070050 test_rule_timeout_ms = 45000,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070051 labels = [ 'checkstyle' ],
52 )
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070053
Ray Milkeyc340e282016-06-30 14:45:42 -070054def java_doc(
55 name,
56 title,
57 pkgs,
58 paths,
59 srcs = [],
60 deps = [],
61 visibility = [],
62 do_it_wrong = False,
63 ):
64 if do_it_wrong:
65 sourcepath = paths
66 else:
67 sourcepath = ['$SRCDIR/' + n for n in paths]
Ray Milkey2a749832016-08-02 15:22:20 -070068
69 if len(srcs) != 0:
Ray Milkeyc340e282016-06-30 14:45:42 -070070 cmd = ' '.join([
Ray Milkey2a749832016-08-02 15:22:20 -070071 'while ! test -f .buckconfig; do cd ..; done;',
72 'javadoc',
73 '-tag onos.rsModel:a:"onos model"',
74 '-quiet',
75 '-protected',
76 '-encoding UTF-8',
77 '-charset UTF-8',
78 '-notimestamp',
79 '-windowtitle "' + title + '"',
80 '-link http://docs.oracle.com/javase/8/docs/api',
81 '-subpackages ',
82 ':'.join(pkgs),
83 '-sourcepath ',
84 ':'.join(sourcepath),
85 ' -classpath ',
86 ':'.join(['$(classpath %s)' % n for n in deps]),
87 '-d $TMP',
88 ]) + ';jar cf $OUT -C $TMP .'
89
90 genrule(
91 name = name,
92 cmd = cmd,
93 srcs = srcs,
94 out = name + '.jar',
95 visibility = visibility,
Yuta HIGUCHI514ef1a2017-11-20 17:37:47 -080096 )
Ray Milkeyc340e282016-06-30 14:45:42 -070097
Brian O'Connor9c2c8232016-11-08 17:13:14 -080098def sonar(
99 name,
100 test = False
101 ):
102
103 cmd = '; '.join([ 'rm -f $OUT',
104 'printf "%(src_base)s = " >> $OUT',
105 '%(srcs)s >> $OUT',
106 'echo "%(binary_base)s = %(classes)s" >> $OUT',
107 'printf "%(lib_base)s = " >> $OUT',
108 '%(libraries)s >> $OUT'
109 ]) % {
110 'srcs' : "echo $(srcs :%s) | sed 's/ /,/g'" % name,
111 'classes' : ("$(bin_dir :%s#non-osgi)" if not test else "$(bin_dir :%s)") % name,
112 'libraries' : "echo $(classpath :%s) | sed 's/:/,/g'" % name,
113 'src_base' : 'sonar.sources' if not test else 'sonar.tests',
114 'binary_base' : 'sonar.java.binaries' if not test else 'sonar.java.test.binaries',
115 'lib_base' : 'sonar.java.libraries' if not test else 'sonar.java.test.libraries'
116 }
117 # FIXME do we need to specify dep here or with the expander cover it?
118 genrule(
119 name = name + "-sonar",
120 cmd = cmd,
121 out = 'sonar-project.properties'
122 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700123
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700124def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -0700125 name = None,
126 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -0700127 group_id = ONOS_GROUP_ID,
128 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700129 deps = [],
130 visibility = ['PUBLIC'],
131 license = 'NONE',
132 description = '',
133 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700134 import_packages = '*',
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700135 dynamicimport_packages = '',
Viswanath KSPd4923cf2017-05-24 13:03:51 +0530136 embedded_dependencies = '',
Jian Li62494862018-02-20 02:33:48 +0900137 bundle_classpath = '',
Yuta HIGUCHI29d640c2017-04-19 19:37:18 -0700138 export_packages = '!.,!*.impl.*,!*.internal.*,*',
Ray Milkey2a749832016-08-02 15:22:20 -0700139 package_name_root = 'org.onosproject',
Brian O'Connor79b70672016-10-20 13:44:52 -0700140 include_resources = {},
Brian O'Connore5817c92016-04-06 15:41:48 -0700141 web_context = NONE,
Brian O'Connor79b70672016-10-20 13:44:52 -0700142 api_title = None,
Brian O'Connore8468b52016-07-25 13:42:36 -0700143 api_version = NONE,
144 api_package = NONE,
145 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700146 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700147 resources_root = None,
Brian O'Connoree674952016-09-13 16:31:45 -0700148 tests = None,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700149 do_javadocs = True,
150 do_checkstyle = True,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700151 **kwargs
152 ):
153
Brian O'Connore4da59d2016-04-08 00:32:18 -0700154 # if name and _get_name() != name:
155 # print _get_name(), '!=', name
156 if name is None:
157 name = _get_name()
158
159 if srcs is None:
160 srcs = glob([SRC + '/*.java'])
161
162 if resources == NONE and resources_root is not None:
163 resources = glob([resources_root + '**'])
164 elif resources == NONE:
165 resources = glob([RESOURCES_ROOT + '**'])
166
167 if resources and not resources_root:
168 resources_root = RESOURCES_ROOT
169
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700170 mvn_coords = group_id + ':' + name + ':' + version
171
Brian O'Connore8468b52016-07-25 13:42:36 -0700172 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700173 name = name,
Yuta HIGUCHI514ef1a2017-11-20 17:37:47 -0800174 # FIXME webapp path hard coded here probably is not right
175 srcs = srcs + glob(['src/main/webapp/**'], excludes = ['src/main/webapp/tests/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700176 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700177 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700178 resources = resources,
179 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700180 bundle_name = name,
181 group_id = group_id,
182 bundle_version = version,
183 bundle_license = license,
184 bundle_description = description,
185 import_packages = import_packages,
186 export_packages = export_packages,
187 include_resources = include_resources,
188 dynamicimport_packages = dynamicimport_packages,
Viswanath KSPd4923cf2017-05-24 13:03:51 +0530189 embedded_dependencies = embedded_dependencies,
Jian Li62494862018-02-20 02:33:48 +0900190 bundle_classpath = bundle_classpath,
Brian O'Connore8468b52016-07-25 13:42:36 -0700191 web_context = web_context,
192 api_title = api_title,
193 api_version = api_version,
194 api_package = api_package,
195 api_description = api_description,
Brian O'Connoree674952016-09-13 16:31:45 -0700196 tests = tests,
197 maven_coords = mvn_coords,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700198 **kwargs
199 )
200
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700201 ### Checkstyle
Thomas Vachuska73436b52017-03-22 19:50:47 -0700202 if do_checkstyle:
203 checkstyle(
Yuta HIGUCHI9312a802017-06-12 20:01:27 -0700204 name = name,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700205 srcs = srcs,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700206 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700207
Thomas Vachuska73436b52017-03-22 19:50:47 -0700208 if do_javadocs:
209 java_doc(
210 name = name + '-javadoc',
211 title = 'Java Docs',
212 pkgs = [ package_name_root ],
213 paths = [ 'src/main/java' ],
214 srcs = srcs,
215 deps = deps,
216 visibility = visibility,
217 do_it_wrong = False,
218 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700219
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700220 # TODO add project config for intellij
221 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700222 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700223 # src_roots = [ 'src/main/java' ],
224 # test_target = ':' + name + '-tests',
225 # test_roots = [ 'src/test/java' ],
226 # )
227
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700228 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700229 mvn_cmd = ' '.join(( 'mvn install:install-file',
230 '-Dfile=$(location :%s)' % name,
231 '-DgroupId=%s' % group_id,
232 '-DartifactId=%s' % name,
233 '-Dversion=%s' % version,
234 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700235 cmd = mvn_cmd + ' > $OUT'
236 if FORCE_INSTALL:
237 # Add a random number to the command to force this rule to run.
238 # TODO We should make this configurable from CLI, perhaps with a flag.
239 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700240 genrule(
241 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700242 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700243 out = 'install.log',
244 visibility = visibility,
245 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800246 sonar(
247 name = name,
248 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700249
Brian O'Connore4da59d2016-04-08 00:32:18 -0700250def osgi_jar_with_tests(
251 name = None,
252 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700253 group_id = ONOS_GROUP_ID,
254 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700255 test_srcs = None,
256 test_deps = [ '//lib:TEST' ],
257 test_resources = None,
258 test_resources_root = None,
259 visibility = [ 'PUBLIC' ],
260 **kwargs
261 ):
262
263 if name is None:
264 name = _get_name()
265
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700266 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700267 test_resources_root = TEST_RESOURCES_ROOT
268 if test_resources_root and not test_resources:
269 test_resources = glob([test_resources_root + '**'])
270 if not test_resources and not test_resources_root:
271 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
272 if test_resources:
273 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700274
Brian O'Connore4da59d2016-04-08 00:32:18 -0700275 if test_srcs is None:
276 test_srcs = glob([TEST + '/*.java'])
277
Brian O'Connoree674952016-09-13 16:31:45 -0700278 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
279
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800280 if test_srcs:
281 java_test(
282 name = name + '-tests',
283 srcs = test_srcs,
284 deps = deps +
285 test_deps +
286 [':' + name + '#non-osgi'],
287 resources = test_resources,
288 resources_root = test_resources_root,
289 maven_coords = mvn_coords,
290 visibility = visibility,
291 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700292
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800293 checkstyle(
Yuta HIGUCHI295ee072017-06-21 15:48:21 -0700294 name = name + '-tests',
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800295 srcs = test_srcs,
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800296 )
297
298 sonar(
299 name = name + '-tests',
300 test = True
301 )
302
303 osgi_jar(name = name,
304 deps = deps,
305 group_id = group_id,
306 version = version,
307 visibility = visibility,
308 tests = [':' + name + '-tests'],
309 **kwargs)
310 else:
311 osgi_jar(name = name,
312 deps = deps,
313 group_id = group_id,
314 version = version,
315 visibility = visibility,
316 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700317
318def tar_file(
319 name,
320 srcs,
Brian O'Connor30322dd2017-08-14 19:11:29 -0700321 other_tars = [],
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700322 root = None,
323 out = None,
324 visibility = [ 'PUBLIC' ],
325 ):
326
Brian O'Connor30322dd2017-08-14 19:11:29 -0700327 cmds = [ 'mkdir -p $TMP/%(root_dir)s',
328 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s' ]
329 for t in other_tars:
330 cmds += [ 'tar -zxf $(location %s)' % t + ' -C $TMP/%(root_dir)s' ]
331 cmds += [ 'tar -C $TMP -zcf $OUT %(root_dir)s' ]
332
333 cmd = ' && '.join(cmds) % {
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700334 'root_dir': root if root is not None else name
335 }
336
337 genrule(
338 name = name,
339 srcs = srcs,
340 bash = cmd,
341 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
342 visibility = visibility,
343 )
Ray Milkey5c5454b2017-01-25 13:26:30 -0800344
345def only_lib_dep_pom(
346 name,
347 src,
348 out,
349 version = ONOS_VERSION,
350 onosGroupId = ONOS_GROUP_ID,
351 visibility = [ 'PUBLIC' ],
352 ):
353
Ray Milkeye46d31a2017-02-01 12:16:18 -0800354 cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT'
Ray Milkey5c5454b2017-01-25 13:26:30 -0800355
356 genrule(
357 name = name,
358 srcs = [ src ],
359 bash = cmd,
360 out = out,
361 visibility = visibility,
362 maven_coords = onosGroupId + ':onos:pom:' + version,
363 )
364
365def pass_thru_pom(
366 name,
367 src,
368 out,
369 artifactId,
370 version = ONOS_VERSION,
371 onosGroupId = ONOS_GROUP_ID,
372 visibility = [ 'PUBLIC' ],
373 ):
374
375 cmd = 'cp ' + src + ' $OUT'
376
377 genrule(
378 name = name,
379 srcs = [ src ],
380 bash = cmd,
381 out = out,
382 visibility = visibility,
383 maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version,
384 )