blob: 8f5a11b0a422ca6b0747abfd1bdcffaf8be317b2 [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()
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',
Yuta HIGUCHI295ee072017-06-21 15:48:21 -070038 deps = [ ':' + name + '-checkstyle-files',
Ray Milkey2d572dd2017-04-14 10:01:24 -070039 '//tools/build/conf:onos-java-header',
40 '//tools/build/conf:onos-build-conf', ],
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070041 args = [
Thomas Vachuska275d2e82016-07-14 17:41:34 -070042 '$(location //tools/build/conf:buck-daemon-jar)',
43 'checkstyle',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070044 '$(location :' + name + '-checkstyle-files)',
45 '$(location //tools/build/conf:checkstyle-xml)',
46 '$(location //tools/build/conf:suppressions-xml)',
47 ],
Ray Milkey84dafd92017-10-25 16:20:23 -070048 test_rule_timeout_ms = 45000,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070049 labels = [ 'checkstyle' ],
50 )
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,
Yuta HIGUCHI514ef1a2017-11-20 17:37:47 -080094 )
Ray Milkeyc340e282016-06-30 14:45:42 -070095
Brian O'Connor9c2c8232016-11-08 17:13:14 -080096def sonar(
97 name,
98 test = False
99 ):
100
101 cmd = '; '.join([ 'rm -f $OUT',
102 'printf "%(src_base)s = " >> $OUT',
103 '%(srcs)s >> $OUT',
104 'echo "%(binary_base)s = %(classes)s" >> $OUT',
105 'printf "%(lib_base)s = " >> $OUT',
106 '%(libraries)s >> $OUT'
107 ]) % {
108 'srcs' : "echo $(srcs :%s) | sed 's/ /,/g'" % name,
109 'classes' : ("$(bin_dir :%s#non-osgi)" if not test else "$(bin_dir :%s)") % name,
110 'libraries' : "echo $(classpath :%s) | sed 's/:/,/g'" % name,
111 'src_base' : 'sonar.sources' if not test else 'sonar.tests',
112 'binary_base' : 'sonar.java.binaries' if not test else 'sonar.java.test.binaries',
113 'lib_base' : 'sonar.java.libraries' if not test else 'sonar.java.test.libraries'
114 }
115 # FIXME do we need to specify dep here or with the expander cover it?
116 genrule(
117 name = name + "-sonar",
118 cmd = cmd,
119 out = 'sonar-project.properties'
120 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700121
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700122def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -0700123 name = None,
124 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -0700125 group_id = ONOS_GROUP_ID,
126 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700127 deps = [],
128 visibility = ['PUBLIC'],
129 license = 'NONE',
130 description = '',
131 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700132 import_packages = '*',
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700133 dynamicimport_packages = '',
Viswanath KSPd4923cf2017-05-24 13:03:51 +0530134 embedded_dependencies = '',
Yuta HIGUCHI29d640c2017-04-19 19:37:18 -0700135 export_packages = '!.,!*.impl.*,!*.internal.*,*',
Ray Milkey2a749832016-08-02 15:22:20 -0700136 package_name_root = 'org.onosproject',
Brian O'Connor79b70672016-10-20 13:44:52 -0700137 include_resources = {},
Brian O'Connore5817c92016-04-06 15:41:48 -0700138 web_context = NONE,
Brian O'Connor79b70672016-10-20 13:44:52 -0700139 api_title = None,
Brian O'Connore8468b52016-07-25 13:42:36 -0700140 api_version = NONE,
141 api_package = NONE,
142 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700143 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700144 resources_root = None,
Brian O'Connoree674952016-09-13 16:31:45 -0700145 tests = None,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700146 do_javadocs = True,
147 do_checkstyle = True,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700148 **kwargs
149 ):
150
Brian O'Connore4da59d2016-04-08 00:32:18 -0700151 # if name and _get_name() != name:
152 # print _get_name(), '!=', name
153 if name is None:
154 name = _get_name()
155
156 if srcs is None:
157 srcs = glob([SRC + '/*.java'])
158
159 if resources == NONE and resources_root is not None:
160 resources = glob([resources_root + '**'])
161 elif resources == NONE:
162 resources = glob([RESOURCES_ROOT + '**'])
163
164 if resources and not resources_root:
165 resources_root = RESOURCES_ROOT
166
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700167 mvn_coords = group_id + ':' + name + ':' + version
168
Brian O'Connore8468b52016-07-25 13:42:36 -0700169 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700170 name = name,
Yuta HIGUCHI514ef1a2017-11-20 17:37:47 -0800171 # FIXME webapp path hard coded here probably is not right
172 srcs = srcs + glob(['src/main/webapp/**'], excludes = ['src/main/webapp/tests/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700173 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700174 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700175 resources = resources,
176 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700177 bundle_name = name,
178 group_id = group_id,
179 bundle_version = version,
180 bundle_license = license,
181 bundle_description = description,
182 import_packages = import_packages,
183 export_packages = export_packages,
184 include_resources = include_resources,
185 dynamicimport_packages = dynamicimport_packages,
Viswanath KSPd4923cf2017-05-24 13:03:51 +0530186 embedded_dependencies = embedded_dependencies,
Brian O'Connore8468b52016-07-25 13:42:36 -0700187 web_context = web_context,
188 api_title = api_title,
189 api_version = api_version,
190 api_package = api_package,
191 api_description = api_description,
Brian O'Connoree674952016-09-13 16:31:45 -0700192 tests = tests,
193 maven_coords = mvn_coords,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700194 **kwargs
195 )
196
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700197 ### Checkstyle
Thomas Vachuska73436b52017-03-22 19:50:47 -0700198 if do_checkstyle:
199 checkstyle(
Yuta HIGUCHI9312a802017-06-12 20:01:27 -0700200 name = name,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700201 srcs = srcs,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700202 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700203
Thomas Vachuska73436b52017-03-22 19:50:47 -0700204 if do_javadocs:
205 java_doc(
206 name = name + '-javadoc',
207 title = 'Java Docs',
208 pkgs = [ package_name_root ],
209 paths = [ 'src/main/java' ],
210 srcs = srcs,
211 deps = deps,
212 visibility = visibility,
213 do_it_wrong = False,
214 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700215
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700216 # TODO add project config for intellij
217 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700218 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700219 # src_roots = [ 'src/main/java' ],
220 # test_target = ':' + name + '-tests',
221 # test_roots = [ 'src/test/java' ],
222 # )
223
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700224 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700225 mvn_cmd = ' '.join(( 'mvn install:install-file',
226 '-Dfile=$(location :%s)' % name,
227 '-DgroupId=%s' % group_id,
228 '-DartifactId=%s' % name,
229 '-Dversion=%s' % version,
230 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700231 cmd = mvn_cmd + ' > $OUT'
232 if FORCE_INSTALL:
233 # Add a random number to the command to force this rule to run.
234 # TODO We should make this configurable from CLI, perhaps with a flag.
235 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700236 genrule(
237 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700238 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700239 out = 'install.log',
240 visibility = visibility,
241 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800242 sonar(
243 name = name,
244 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700245
Brian O'Connore4da59d2016-04-08 00:32:18 -0700246def osgi_jar_with_tests(
247 name = None,
248 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700249 group_id = ONOS_GROUP_ID,
250 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700251 test_srcs = None,
252 test_deps = [ '//lib:TEST' ],
253 test_resources = None,
254 test_resources_root = None,
255 visibility = [ 'PUBLIC' ],
256 **kwargs
257 ):
258
259 if name is None:
260 name = _get_name()
261
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700262 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700263 test_resources_root = TEST_RESOURCES_ROOT
264 if test_resources_root and not test_resources:
265 test_resources = glob([test_resources_root + '**'])
266 if not test_resources and not test_resources_root:
267 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
268 if test_resources:
269 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700270
Brian O'Connore4da59d2016-04-08 00:32:18 -0700271 if test_srcs is None:
272 test_srcs = glob([TEST + '/*.java'])
273
Brian O'Connoree674952016-09-13 16:31:45 -0700274 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
275
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800276 if test_srcs:
277 java_test(
278 name = name + '-tests',
279 srcs = test_srcs,
280 deps = deps +
281 test_deps +
282 [':' + name + '#non-osgi'],
283 resources = test_resources,
284 resources_root = test_resources_root,
285 maven_coords = mvn_coords,
286 visibility = visibility,
287 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700288
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800289 checkstyle(
Yuta HIGUCHI295ee072017-06-21 15:48:21 -0700290 name = name + '-tests',
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800291 srcs = test_srcs,
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800292 )
293
294 sonar(
295 name = name + '-tests',
296 test = True
297 )
298
299 osgi_jar(name = name,
300 deps = deps,
301 group_id = group_id,
302 version = version,
303 visibility = visibility,
304 tests = [':' + name + '-tests'],
305 **kwargs)
306 else:
307 osgi_jar(name = name,
308 deps = deps,
309 group_id = group_id,
310 version = version,
311 visibility = visibility,
312 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700313
314def tar_file(
315 name,
316 srcs,
Brian O'Connor30322dd2017-08-14 19:11:29 -0700317 other_tars = [],
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700318 root = None,
319 out = None,
320 visibility = [ 'PUBLIC' ],
321 ):
322
Brian O'Connor30322dd2017-08-14 19:11:29 -0700323 cmds = [ 'mkdir -p $TMP/%(root_dir)s',
324 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s' ]
325 for t in other_tars:
326 cmds += [ 'tar -zxf $(location %s)' % t + ' -C $TMP/%(root_dir)s' ]
327 cmds += [ 'tar -C $TMP -zcf $OUT %(root_dir)s' ]
328
329 cmd = ' && '.join(cmds) % {
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700330 'root_dir': root if root is not None else name
331 }
332
333 genrule(
334 name = name,
335 srcs = srcs,
336 bash = cmd,
337 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
338 visibility = visibility,
339 )
Ray Milkey5c5454b2017-01-25 13:26:30 -0800340
341def only_lib_dep_pom(
342 name,
343 src,
344 out,
345 version = ONOS_VERSION,
346 onosGroupId = ONOS_GROUP_ID,
347 visibility = [ 'PUBLIC' ],
348 ):
349
Ray Milkeye46d31a2017-02-01 12:16:18 -0800350 cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT'
Ray Milkey5c5454b2017-01-25 13:26:30 -0800351
352 genrule(
353 name = name,
354 srcs = [ src ],
355 bash = cmd,
356 out = out,
357 visibility = visibility,
358 maven_coords = onosGroupId + ':onos:pom:' + version,
359 )
360
361def pass_thru_pom(
362 name,
363 src,
364 out,
365 artifactId,
366 version = ONOS_VERSION,
367 onosGroupId = ONOS_GROUP_ID,
368 visibility = [ 'PUBLIC' ],
369 ):
370
371 cmd = 'cp ' + src + ' $OUT'
372
373 genrule(
374 name = name,
375 srcs = [ src ],
376 bash = cmd,
377 out = out,
378 visibility = visibility,
379 maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version,
380 )