blob: f34e6574ca45b0db1251d328261c40b6b1b3a814 [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',
Ray Milkey85267002016-11-16 11:06:35 -080038 deps = [ jar_target, '//tools/build/conf:onos-java-header' ],
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070039 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 )
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070049
Ray Milkeyc340e282016-06-30 14:45:42 -070050def java_doc(
51 name,
52 title,
53 pkgs,
54 paths,
55 srcs = [],
56 deps = [],
57 visibility = [],
58 do_it_wrong = False,
59 ):
60 if do_it_wrong:
61 sourcepath = paths
62 else:
63 sourcepath = ['$SRCDIR/' + n for n in paths]
Ray Milkey2a749832016-08-02 15:22:20 -070064
65 if len(srcs) != 0:
Ray Milkeyc340e282016-06-30 14:45:42 -070066 cmd = ' '.join([
Ray Milkey2a749832016-08-02 15:22:20 -070067 'while ! test -f .buckconfig; do cd ..; done;',
68 'javadoc',
69 '-tag onos.rsModel:a:"onos model"',
70 '-quiet',
71 '-protected',
72 '-encoding UTF-8',
73 '-charset UTF-8',
74 '-notimestamp',
75 '-windowtitle "' + title + '"',
76 '-link http://docs.oracle.com/javase/8/docs/api',
77 '-subpackages ',
78 ':'.join(pkgs),
79 '-sourcepath ',
80 ':'.join(sourcepath),
81 ' -classpath ',
82 ':'.join(['$(classpath %s)' % n for n in deps]),
83 '-d $TMP',
84 ]) + ';jar cf $OUT -C $TMP .'
85
86 genrule(
87 name = name,
88 cmd = cmd,
89 srcs = srcs,
90 out = name + '.jar',
91 visibility = visibility,
Ray Milkeyc340e282016-06-30 14:45:42 -070092)
93
Brian O'Connor9c2c8232016-11-08 17:13:14 -080094def sonar(
95 name,
96 test = False
97 ):
98
99 cmd = '; '.join([ 'rm -f $OUT',
100 'printf "%(src_base)s = " >> $OUT',
101 '%(srcs)s >> $OUT',
102 'echo "%(binary_base)s = %(classes)s" >> $OUT',
103 'printf "%(lib_base)s = " >> $OUT',
104 '%(libraries)s >> $OUT'
105 ]) % {
106 'srcs' : "echo $(srcs :%s) | sed 's/ /,/g'" % name,
107 'classes' : ("$(bin_dir :%s#non-osgi)" if not test else "$(bin_dir :%s)") % name,
108 'libraries' : "echo $(classpath :%s) | sed 's/:/,/g'" % name,
109 'src_base' : 'sonar.sources' if not test else 'sonar.tests',
110 'binary_base' : 'sonar.java.binaries' if not test else 'sonar.java.test.binaries',
111 'lib_base' : 'sonar.java.libraries' if not test else 'sonar.java.test.libraries'
112 }
113 # FIXME do we need to specify dep here or with the expander cover it?
114 genrule(
115 name = name + "-sonar",
116 cmd = cmd,
117 out = 'sonar-project.properties'
118 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700119
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700120def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -0700121 name = None,
122 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -0700123 group_id = ONOS_GROUP_ID,
124 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700125 deps = [],
126 visibility = ['PUBLIC'],
127 license = 'NONE',
128 description = '',
129 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700130 import_packages = '*',
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700131 dynamicimport_packages = '',
Yuta HIGUCHI29d640c2017-04-19 19:37:18 -0700132 export_packages = '!.,!*.impl.*,!*.internal.*,*',
Ray Milkey2a749832016-08-02 15:22:20 -0700133 package_name_root = 'org.onosproject',
Brian O'Connor79b70672016-10-20 13:44:52 -0700134 include_resources = {},
Brian O'Connore5817c92016-04-06 15:41:48 -0700135 web_context = NONE,
Brian O'Connor79b70672016-10-20 13:44:52 -0700136 api_title = None,
Brian O'Connore8468b52016-07-25 13:42:36 -0700137 api_version = NONE,
138 api_package = NONE,
139 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700140 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700141 resources_root = None,
Brian O'Connoree674952016-09-13 16:31:45 -0700142 tests = None,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700143 do_javadocs = True,
144 do_checkstyle = True,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700145 **kwargs
146 ):
147
Brian O'Connore4da59d2016-04-08 00:32:18 -0700148 # if name and _get_name() != name:
149 # print _get_name(), '!=', name
150 if name is None:
151 name = _get_name()
152
153 if srcs is None:
154 srcs = glob([SRC + '/*.java'])
155
156 if resources == NONE and resources_root is not None:
157 resources = glob([resources_root + '**'])
158 elif resources == NONE:
159 resources = glob([RESOURCES_ROOT + '**'])
160
161 if resources and not resources_root:
162 resources_root = RESOURCES_ROOT
163
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700164 mvn_coords = group_id + ':' + name + ':' + version
165
Brian O'Connore8468b52016-07-25 13:42:36 -0700166 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700167 name = name,
168 srcs = srcs + glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700169 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700170 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700171 resources = resources,
172 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700173 bundle_name = name,
174 group_id = group_id,
175 bundle_version = version,
176 bundle_license = license,
177 bundle_description = description,
178 import_packages = import_packages,
179 export_packages = export_packages,
180 include_resources = include_resources,
181 dynamicimport_packages = dynamicimport_packages,
Brian O'Connore8468b52016-07-25 13:42:36 -0700182 web_context = web_context,
183 api_title = api_title,
184 api_version = api_version,
185 api_package = api_package,
186 api_description = api_description,
Brian O'Connoree674952016-09-13 16:31:45 -0700187 tests = tests,
188 maven_coords = mvn_coords,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700189 **kwargs
190 )
191
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700192 ### Checkstyle
Thomas Vachuska73436b52017-03-22 19:50:47 -0700193 if do_checkstyle:
194 checkstyle(
195 name = name + '-checkstyle-files',
196 srcs = srcs,
197 jar_target = ':'+ name,
198 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700199
Thomas Vachuska73436b52017-03-22 19:50:47 -0700200 if do_javadocs:
201 java_doc(
202 name = name + '-javadoc',
203 title = 'Java Docs',
204 pkgs = [ package_name_root ],
205 paths = [ 'src/main/java' ],
206 srcs = srcs,
207 deps = deps,
208 visibility = visibility,
209 do_it_wrong = False,
210 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700211
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700212 # TODO add project config for intellij
213 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700214 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700215 # src_roots = [ 'src/main/java' ],
216 # test_target = ':' + name + '-tests',
217 # test_roots = [ 'src/test/java' ],
218 # )
219
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700220 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700221 mvn_cmd = ' '.join(( 'mvn install:install-file',
222 '-Dfile=$(location :%s)' % name,
223 '-DgroupId=%s' % group_id,
224 '-DartifactId=%s' % name,
225 '-Dversion=%s' % version,
226 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700227 cmd = mvn_cmd + ' > $OUT'
228 if FORCE_INSTALL:
229 # Add a random number to the command to force this rule to run.
230 # TODO We should make this configurable from CLI, perhaps with a flag.
231 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700232 genrule(
233 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700234 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700235 out = 'install.log',
236 visibility = visibility,
237 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800238 sonar(
239 name = name,
240 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700241
Brian O'Connore4da59d2016-04-08 00:32:18 -0700242def osgi_jar_with_tests(
243 name = None,
244 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700245 group_id = ONOS_GROUP_ID,
246 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700247 test_srcs = None,
248 test_deps = [ '//lib:TEST' ],
249 test_resources = None,
250 test_resources_root = None,
251 visibility = [ 'PUBLIC' ],
252 **kwargs
253 ):
254
255 if name is None:
256 name = _get_name()
257
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700258 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700259 test_resources_root = TEST_RESOURCES_ROOT
260 if test_resources_root and not test_resources:
261 test_resources = glob([test_resources_root + '**'])
262 if not test_resources and not test_resources_root:
263 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
264 if test_resources:
265 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700266
Brian O'Connore4da59d2016-04-08 00:32:18 -0700267 if test_srcs is None:
268 test_srcs = glob([TEST + '/*.java'])
269
Brian O'Connoree674952016-09-13 16:31:45 -0700270 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
271
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800272 if test_srcs:
273 java_test(
274 name = name + '-tests',
275 srcs = test_srcs,
276 deps = deps +
277 test_deps +
278 [':' + name + '#non-osgi'],
279 resources = test_resources,
280 resources_root = test_resources_root,
281 maven_coords = mvn_coords,
282 visibility = visibility,
283 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700284
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800285 checkstyle(
286 name = name + '-tests',
287 srcs = test_srcs,
288 jar_target = ':' + name + '-tests',
289 )
290
291 sonar(
292 name = name + '-tests',
293 test = True
294 )
295
296 osgi_jar(name = name,
297 deps = deps,
298 group_id = group_id,
299 version = version,
300 visibility = visibility,
301 tests = [':' + name + '-tests'],
302 **kwargs)
303 else:
304 osgi_jar(name = name,
305 deps = deps,
306 group_id = group_id,
307 version = version,
308 visibility = visibility,
309 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700310
311def tar_file(
312 name,
313 srcs,
314 root = None,
315 out = None,
316 visibility = [ 'PUBLIC' ],
317 ):
318
319 cmd = ( 'mkdir -p $TMP/%(root_dir)s && '
Ray Milkey442c8f22017-05-02 11:23:55 -0700320 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s && '
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700321 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % {
322 'root_dir': root if root is not None else name
323 }
324
325 genrule(
326 name = name,
327 srcs = srcs,
328 bash = cmd,
329 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
330 visibility = visibility,
331 )
Ray Milkey5c5454b2017-01-25 13:26:30 -0800332
333def only_lib_dep_pom(
334 name,
335 src,
336 out,
337 version = ONOS_VERSION,
338 onosGroupId = ONOS_GROUP_ID,
339 visibility = [ 'PUBLIC' ],
340 ):
341
Ray Milkeye46d31a2017-02-01 12:16:18 -0800342 cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT'
Ray Milkey5c5454b2017-01-25 13:26:30 -0800343
344 genrule(
345 name = name,
346 srcs = [ src ],
347 bash = cmd,
348 out = out,
349 visibility = visibility,
350 maven_coords = onosGroupId + ':onos:pom:' + version,
351 )
352
353def pass_thru_pom(
354 name,
355 src,
356 out,
357 artifactId,
358 version = ONOS_VERSION,
359 onosGroupId = ONOS_GROUP_ID,
360 visibility = [ 'PUBLIC' ],
361 ):
362
363 cmd = 'cp ' + src + ' $OUT'
364
365 genrule(
366 name = name,
367 srcs = [ src ],
368 bash = cmd,
369 out = out,
370 visibility = visibility,
371 maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version,
372 )