blob: f59786bc46a168f67e169a317affefcc8c437eea [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()
Brian O'Connor0ef31962017-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',
Ray Milkey2d572dd2017-04-14 10:01:24 -070039 deps = [ jar_target,
40 '//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'Connor0ef31962017-11-27 18:40:08 -080049
50 #TODO test_rule_timeout_ms seems to be ignored on Linux
51 test_rule_timeout_ms = 45000,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070052 labels = [ 'checkstyle' ],
53 )
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070054
Ray Milkeyc340e282016-06-30 14:45:42 -070055def java_doc(
56 name,
57 title,
58 pkgs,
59 paths,
60 srcs = [],
61 deps = [],
62 visibility = [],
63 do_it_wrong = False,
64 ):
65 if do_it_wrong:
66 sourcepath = paths
67 else:
68 sourcepath = ['$SRCDIR/' + n for n in paths]
Ray Milkey2a749832016-08-02 15:22:20 -070069
70 if len(srcs) != 0:
Ray Milkeyc340e282016-06-30 14:45:42 -070071 cmd = ' '.join([
Ray Milkey2a749832016-08-02 15:22:20 -070072 'while ! test -f .buckconfig; do cd ..; done;',
73 'javadoc',
74 '-tag onos.rsModel:a:"onos model"',
75 '-quiet',
76 '-protected',
77 '-encoding UTF-8',
78 '-charset UTF-8',
79 '-notimestamp',
80 '-windowtitle "' + title + '"',
81 '-link http://docs.oracle.com/javase/8/docs/api',
82 '-subpackages ',
83 ':'.join(pkgs),
84 '-sourcepath ',
85 ':'.join(sourcepath),
86 ' -classpath ',
87 ':'.join(['$(classpath %s)' % n for n in deps]),
88 '-d $TMP',
89 ]) + ';jar cf $OUT -C $TMP .'
90
91 genrule(
92 name = name,
93 cmd = cmd,
94 srcs = srcs,
95 out = name + '.jar',
96 visibility = visibility,
Ray Milkeyc340e282016-06-30 14:45:42 -070097)
98
Brian O'Connor9c2c8232016-11-08 17:13:14 -080099def sonar(
100 name,
101 test = False
102 ):
103
104 cmd = '; '.join([ 'rm -f $OUT',
105 'printf "%(src_base)s = " >> $OUT',
106 '%(srcs)s >> $OUT',
107 'echo "%(binary_base)s = %(classes)s" >> $OUT',
108 'printf "%(lib_base)s = " >> $OUT',
109 '%(libraries)s >> $OUT'
110 ]) % {
111 'srcs' : "echo $(srcs :%s) | sed 's/ /,/g'" % name,
112 'classes' : ("$(bin_dir :%s#non-osgi)" if not test else "$(bin_dir :%s)") % name,
113 'libraries' : "echo $(classpath :%s) | sed 's/:/,/g'" % name,
114 'src_base' : 'sonar.sources' if not test else 'sonar.tests',
115 'binary_base' : 'sonar.java.binaries' if not test else 'sonar.java.test.binaries',
116 'lib_base' : 'sonar.java.libraries' if not test else 'sonar.java.test.libraries'
117 }
118 # FIXME do we need to specify dep here or with the expander cover it?
119 genrule(
120 name = name + "-sonar",
121 cmd = cmd,
122 out = 'sonar-project.properties'
123 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700124
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700125def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -0700126 name = None,
127 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -0700128 group_id = ONOS_GROUP_ID,
129 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700130 deps = [],
131 visibility = ['PUBLIC'],
132 license = 'NONE',
133 description = '',
134 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700135 import_packages = '*',
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700136 dynamicimport_packages = '',
Yuta HIGUCHI29d640c2017-04-19 19:37:18 -0700137 export_packages = '!.,!*.impl.*,!*.internal.*,*',
Ray Milkey2a749832016-08-02 15:22:20 -0700138 package_name_root = 'org.onosproject',
Brian O'Connor79b70672016-10-20 13:44:52 -0700139 include_resources = {},
Brian O'Connore5817c92016-04-06 15:41:48 -0700140 web_context = NONE,
Brian O'Connor79b70672016-10-20 13:44:52 -0700141 api_title = None,
Brian O'Connore8468b52016-07-25 13:42:36 -0700142 api_version = NONE,
143 api_package = NONE,
144 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700145 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700146 resources_root = None,
Brian O'Connoree674952016-09-13 16:31:45 -0700147 tests = None,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700148 do_javadocs = True,
149 do_checkstyle = True,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700150 **kwargs
151 ):
152
Brian O'Connore4da59d2016-04-08 00:32:18 -0700153 # if name and _get_name() != name:
154 # print _get_name(), '!=', name
155 if name is None:
156 name = _get_name()
157
158 if srcs is None:
159 srcs = glob([SRC + '/*.java'])
160
161 if resources == NONE and resources_root is not None:
162 resources = glob([resources_root + '**'])
163 elif resources == NONE:
164 resources = glob([RESOURCES_ROOT + '**'])
165
166 if resources and not resources_root:
167 resources_root = RESOURCES_ROOT
168
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700169 mvn_coords = group_id + ':' + name + ':' + version
170
Brian O'Connore8468b52016-07-25 13:42:36 -0700171 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700172 name = name,
173 srcs = srcs + glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700174 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700175 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700176 resources = resources,
177 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700178 bundle_name = name,
179 group_id = group_id,
180 bundle_version = version,
181 bundle_license = license,
182 bundle_description = description,
183 import_packages = import_packages,
184 export_packages = export_packages,
185 include_resources = include_resources,
186 dynamicimport_packages = dynamicimport_packages,
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(
200 name = name + '-checkstyle-files',
201 srcs = srcs,
202 jar_target = ':'+ name,
203 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700204
Thomas Vachuska73436b52017-03-22 19:50:47 -0700205 if do_javadocs:
206 java_doc(
207 name = name + '-javadoc',
208 title = 'Java Docs',
209 pkgs = [ package_name_root ],
210 paths = [ 'src/main/java' ],
211 srcs = srcs,
212 deps = deps,
213 visibility = visibility,
214 do_it_wrong = False,
215 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700216
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700217 # TODO add project config for intellij
218 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700219 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700220 # src_roots = [ 'src/main/java' ],
221 # test_target = ':' + name + '-tests',
222 # test_roots = [ 'src/test/java' ],
223 # )
224
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700225 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700226 mvn_cmd = ' '.join(( 'mvn install:install-file',
227 '-Dfile=$(location :%s)' % name,
228 '-DgroupId=%s' % group_id,
229 '-DartifactId=%s' % name,
230 '-Dversion=%s' % version,
231 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700232 cmd = mvn_cmd + ' > $OUT'
233 if FORCE_INSTALL:
234 # Add a random number to the command to force this rule to run.
235 # TODO We should make this configurable from CLI, perhaps with a flag.
236 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700237 genrule(
238 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700239 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700240 out = 'install.log',
241 visibility = visibility,
242 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800243 sonar(
244 name = name,
245 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700246
Brian O'Connore4da59d2016-04-08 00:32:18 -0700247def osgi_jar_with_tests(
248 name = None,
249 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700250 group_id = ONOS_GROUP_ID,
251 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700252 test_srcs = None,
253 test_deps = [ '//lib:TEST' ],
254 test_resources = None,
255 test_resources_root = None,
256 visibility = [ 'PUBLIC' ],
257 **kwargs
258 ):
259
260 if name is None:
261 name = _get_name()
262
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700263 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700264 test_resources_root = TEST_RESOURCES_ROOT
265 if test_resources_root and not test_resources:
266 test_resources = glob([test_resources_root + '**'])
267 if not test_resources and not test_resources_root:
268 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
269 if test_resources:
270 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700271
Brian O'Connore4da59d2016-04-08 00:32:18 -0700272 if test_srcs is None:
273 test_srcs = glob([TEST + '/*.java'])
274
Brian O'Connoree674952016-09-13 16:31:45 -0700275 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
276
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800277 if test_srcs:
278 java_test(
279 name = name + '-tests',
280 srcs = test_srcs,
281 deps = deps +
282 test_deps +
283 [':' + name + '#non-osgi'],
284 resources = test_resources,
285 resources_root = test_resources_root,
286 maven_coords = mvn_coords,
287 visibility = visibility,
288 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700289
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800290 checkstyle(
291 name = name + '-tests',
292 srcs = test_srcs,
293 jar_target = ':' + name + '-tests',
294 )
295
296 sonar(
297 name = name + '-tests',
298 test = True
299 )
300
301 osgi_jar(name = name,
302 deps = deps,
303 group_id = group_id,
304 version = version,
305 visibility = visibility,
306 tests = [':' + name + '-tests'],
307 **kwargs)
308 else:
309 osgi_jar(name = name,
310 deps = deps,
311 group_id = group_id,
312 version = version,
313 visibility = visibility,
314 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700315
316def tar_file(
317 name,
318 srcs,
319 root = None,
320 out = None,
321 visibility = [ 'PUBLIC' ],
322 ):
323
324 cmd = ( 'mkdir -p $TMP/%(root_dir)s && '
Ray Milkey442c8f22017-05-02 11:23:55 -0700325 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s && '
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700326 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % {
327 'root_dir': root if root is not None else name
328 }
329
330 genrule(
331 name = name,
332 srcs = srcs,
333 bash = cmd,
334 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
335 visibility = visibility,
336 )
Ray Milkey5c5454b2017-01-25 13:26:30 -0800337
338def only_lib_dep_pom(
339 name,
340 src,
341 out,
342 version = ONOS_VERSION,
343 onosGroupId = ONOS_GROUP_ID,
344 visibility = [ 'PUBLIC' ],
345 ):
346
Ray Milkeye46d31a2017-02-01 12:16:18 -0800347 cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT'
Ray Milkey5c5454b2017-01-25 13:26:30 -0800348
349 genrule(
350 name = name,
351 srcs = [ src ],
352 bash = cmd,
353 out = out,
354 visibility = visibility,
355 maven_coords = onosGroupId + ':onos:pom:' + version,
356 )
357
358def pass_thru_pom(
359 name,
360 src,
361 out,
362 artifactId,
363 version = ONOS_VERSION,
364 onosGroupId = ONOS_GROUP_ID,
365 visibility = [ 'PUBLIC' ],
366 ):
367
368 cmd = 'cp ' + src + ' $OUT'
369
370 genrule(
371 name = name,
372 srcs = [ src ],
373 bash = cmd,
374 out = out,
375 visibility = visibility,
376 maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version,
377 )