blob: d6e8b7f955acfceb826ec4b6ec849d1b2fbd2d5a [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 Milkey2d572dd2017-04-14 10:01:24 -070038 deps = [ jar_target,
39 '//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 ],
48 test_rule_timeout_ms = 20000,
49 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,
Ray Milkeyc340e282016-06-30 14:45:42 -070094)
95
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 = '',
Yuta HIGUCHI29d640c2017-04-19 19:37:18 -0700134 export_packages = '!.,!*.impl.*,!*.internal.*,*',
Ray Milkey2a749832016-08-02 15:22:20 -0700135 package_name_root = 'org.onosproject',
Brian O'Connor79b70672016-10-20 13:44:52 -0700136 include_resources = {},
Brian O'Connore5817c92016-04-06 15:41:48 -0700137 web_context = NONE,
Brian O'Connor79b70672016-10-20 13:44:52 -0700138 api_title = None,
Brian O'Connore8468b52016-07-25 13:42:36 -0700139 api_version = NONE,
140 api_package = NONE,
141 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700142 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700143 resources_root = None,
Brian O'Connoree674952016-09-13 16:31:45 -0700144 tests = None,
Thomas Vachuska73436b52017-03-22 19:50:47 -0700145 do_javadocs = True,
146 do_checkstyle = True,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700147 **kwargs
148 ):
149
Brian O'Connore4da59d2016-04-08 00:32:18 -0700150 # if name and _get_name() != name:
151 # print _get_name(), '!=', name
152 if name is None:
153 name = _get_name()
154
155 if srcs is None:
156 srcs = glob([SRC + '/*.java'])
157
158 if resources == NONE and resources_root is not None:
159 resources = glob([resources_root + '**'])
160 elif resources == NONE:
161 resources = glob([RESOURCES_ROOT + '**'])
162
163 if resources and not resources_root:
164 resources_root = RESOURCES_ROOT
165
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700166 mvn_coords = group_id + ':' + name + ':' + version
167
Brian O'Connore8468b52016-07-25 13:42:36 -0700168 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700169 name = name,
170 srcs = srcs + glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700171 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700172 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700173 resources = resources,
174 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700175 bundle_name = name,
176 group_id = group_id,
177 bundle_version = version,
178 bundle_license = license,
179 bundle_description = description,
180 import_packages = import_packages,
181 export_packages = export_packages,
182 include_resources = include_resources,
183 dynamicimport_packages = dynamicimport_packages,
Brian O'Connore8468b52016-07-25 13:42:36 -0700184 web_context = web_context,
185 api_title = api_title,
186 api_version = api_version,
187 api_package = api_package,
188 api_description = api_description,
Brian O'Connoree674952016-09-13 16:31:45 -0700189 tests = tests,
190 maven_coords = mvn_coords,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700191 **kwargs
192 )
193
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700194 ### Checkstyle
Thomas Vachuska73436b52017-03-22 19:50:47 -0700195 if do_checkstyle:
196 checkstyle(
197 name = name + '-checkstyle-files',
198 srcs = srcs,
199 jar_target = ':'+ name,
200 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700201
Thomas Vachuska73436b52017-03-22 19:50:47 -0700202 if do_javadocs:
203 java_doc(
204 name = name + '-javadoc',
205 title = 'Java Docs',
206 pkgs = [ package_name_root ],
207 paths = [ 'src/main/java' ],
208 srcs = srcs,
209 deps = deps,
210 visibility = visibility,
211 do_it_wrong = False,
212 )
Ray Milkeyc340e282016-06-30 14:45:42 -0700213
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700214 # TODO add project config for intellij
215 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700216 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700217 # src_roots = [ 'src/main/java' ],
218 # test_target = ':' + name + '-tests',
219 # test_roots = [ 'src/test/java' ],
220 # )
221
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700222 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700223 mvn_cmd = ' '.join(( 'mvn install:install-file',
224 '-Dfile=$(location :%s)' % name,
225 '-DgroupId=%s' % group_id,
226 '-DartifactId=%s' % name,
227 '-Dversion=%s' % version,
228 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700229 cmd = mvn_cmd + ' > $OUT'
230 if FORCE_INSTALL:
231 # Add a random number to the command to force this rule to run.
232 # TODO We should make this configurable from CLI, perhaps with a flag.
233 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700234 genrule(
235 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700236 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700237 out = 'install.log',
238 visibility = visibility,
239 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800240 sonar(
241 name = name,
242 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700243
Brian O'Connore4da59d2016-04-08 00:32:18 -0700244def osgi_jar_with_tests(
245 name = None,
246 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700247 group_id = ONOS_GROUP_ID,
248 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700249 test_srcs = None,
250 test_deps = [ '//lib:TEST' ],
251 test_resources = None,
252 test_resources_root = None,
253 visibility = [ 'PUBLIC' ],
254 **kwargs
255 ):
256
257 if name is None:
258 name = _get_name()
259
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700260 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700261 test_resources_root = TEST_RESOURCES_ROOT
262 if test_resources_root and not test_resources:
263 test_resources = glob([test_resources_root + '**'])
264 if not test_resources and not test_resources_root:
265 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
266 if test_resources:
267 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700268
Brian O'Connore4da59d2016-04-08 00:32:18 -0700269 if test_srcs is None:
270 test_srcs = glob([TEST + '/*.java'])
271
Brian O'Connoree674952016-09-13 16:31:45 -0700272 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
273
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800274 if test_srcs:
275 java_test(
276 name = name + '-tests',
277 srcs = test_srcs,
278 deps = deps +
279 test_deps +
280 [':' + name + '#non-osgi'],
281 resources = test_resources,
282 resources_root = test_resources_root,
283 maven_coords = mvn_coords,
284 visibility = visibility,
285 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700286
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800287 checkstyle(
288 name = name + '-tests',
289 srcs = test_srcs,
290 jar_target = ':' + name + '-tests',
291 )
292
293 sonar(
294 name = name + '-tests',
295 test = True
296 )
297
298 osgi_jar(name = name,
299 deps = deps,
300 group_id = group_id,
301 version = version,
302 visibility = visibility,
303 tests = [':' + name + '-tests'],
304 **kwargs)
305 else:
306 osgi_jar(name = name,
307 deps = deps,
308 group_id = group_id,
309 version = version,
310 visibility = visibility,
311 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700312
313def tar_file(
314 name,
315 srcs,
316 root = None,
317 out = None,
318 visibility = [ 'PUBLIC' ],
319 ):
320
321 cmd = ( 'mkdir -p $TMP/%(root_dir)s && '
Ray Milkey442c8f22017-05-02 11:23:55 -0700322 'cp -R -L $SRCDIR/* $TMP/%(root_dir)s && '
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700323 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % {
324 'root_dir': root if root is not None else name
325 }
326
327 genrule(
328 name = name,
329 srcs = srcs,
330 bash = cmd,
331 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
332 visibility = visibility,
333 )
Ray Milkey5c5454b2017-01-25 13:26:30 -0800334
335def only_lib_dep_pom(
336 name,
337 src,
338 out,
339 version = ONOS_VERSION,
340 onosGroupId = ONOS_GROUP_ID,
341 visibility = [ 'PUBLIC' ],
342 ):
343
Ray Milkeye46d31a2017-02-01 12:16:18 -0800344 cmd = 'grep -v \<module\> ' + src + ' | sed "s#<modules>#<modules><module>lib</module>#" >$OUT'
Ray Milkey5c5454b2017-01-25 13:26:30 -0800345
346 genrule(
347 name = name,
348 srcs = [ src ],
349 bash = cmd,
350 out = out,
351 visibility = visibility,
352 maven_coords = onosGroupId + ':onos:pom:' + version,
353 )
354
355def pass_thru_pom(
356 name,
357 src,
358 out,
359 artifactId,
360 version = ONOS_VERSION,
361 onosGroupId = ONOS_GROUP_ID,
362 visibility = [ 'PUBLIC' ],
363 ):
364
365 cmd = 'cp ' + src + ' $OUT'
366
367 genrule(
368 name = name,
369 srcs = [ src ],
370 bash = cmd,
371 out = out,
372 visibility = visibility,
373 maven_coords = onosGroupId + ':' + artifactId + ':pom:' + version,
374 )