blob: 16d0b5272216b4a576fdd141133d9b1c4bb8517b [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 = '',
Brian O'Connore5817c92016-04-06 15:41:48 -0700132 export_packages = '*',
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,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700143 **kwargs
144 ):
145
Brian O'Connore4da59d2016-04-08 00:32:18 -0700146 # if name and _get_name() != name:
147 # print _get_name(), '!=', name
148 if name is None:
149 name = _get_name()
150
151 if srcs is None:
152 srcs = glob([SRC + '/*.java'])
153
154 if resources == NONE and resources_root is not None:
155 resources = glob([resources_root + '**'])
156 elif resources == NONE:
157 resources = glob([RESOURCES_ROOT + '**'])
158
159 if resources and not resources_root:
160 resources_root = RESOURCES_ROOT
161
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700162 mvn_coords = group_id + ':' + name + ':' + version
163
Brian O'Connore8468b52016-07-25 13:42:36 -0700164 onos_jar(
Brian O'Connoree674952016-09-13 16:31:45 -0700165 name = name,
166 srcs = srcs + glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700167 deps = deps,
Brian O'Connoree674952016-09-13 16:31:45 -0700168 visibility = visibility,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700169 resources = resources,
170 resources_root = resources_root,
Brian O'Connoree674952016-09-13 16:31:45 -0700171 bundle_name = name,
172 group_id = group_id,
173 bundle_version = version,
174 bundle_license = license,
175 bundle_description = description,
176 import_packages = import_packages,
177 export_packages = export_packages,
178 include_resources = include_resources,
179 dynamicimport_packages = dynamicimport_packages,
Brian O'Connore8468b52016-07-25 13:42:36 -0700180 web_context = web_context,
181 api_title = api_title,
182 api_version = api_version,
183 api_package = api_package,
184 api_description = api_description,
Brian O'Connoree674952016-09-13 16:31:45 -0700185 tests = tests,
186 maven_coords = mvn_coords,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700187 **kwargs
188 )
189
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700190 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700191 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700192 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700193 srcs = srcs,
Brian O'Connoree674952016-09-13 16:31:45 -0700194 jar_target = ':'+ name,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700195 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700196
Ray Milkeyc340e282016-06-30 14:45:42 -0700197 java_doc(
198 name = name + '-javadoc',
199 title = 'Java Docs',
Ray Milkey2a749832016-08-02 15:22:20 -0700200 pkgs = [ package_name_root ],
Ray Milkeyc340e282016-06-30 14:45:42 -0700201 paths = [ 'src/main/java' ],
202 srcs = srcs,
203 deps = deps,
204 visibility = visibility,
205 do_it_wrong = False,
206 )
207
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700208 # TODO add project config for intellij
209 # project_config(
Brian O'Connoree674952016-09-13 16:31:45 -0700210 # src_target = ':' + name,
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700211 # src_roots = [ 'src/main/java' ],
212 # test_target = ':' + name + '-tests',
213 # test_roots = [ 'src/test/java' ],
214 # )
215
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700216 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700217 mvn_cmd = ' '.join(( 'mvn install:install-file',
218 '-Dfile=$(location :%s)' % name,
219 '-DgroupId=%s' % group_id,
220 '-DartifactId=%s' % name,
221 '-Dversion=%s' % version,
222 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700223 cmd = mvn_cmd + ' > $OUT'
224 if FORCE_INSTALL:
225 # Add a random number to the command to force this rule to run.
226 # TODO We should make this configurable from CLI, perhaps with a flag.
227 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700228 genrule(
229 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700230 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700231 out = 'install.log',
232 visibility = visibility,
233 )
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800234 sonar(
235 name = name,
236 )
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700237
Brian O'Connore4da59d2016-04-08 00:32:18 -0700238def osgi_jar_with_tests(
239 name = None,
240 deps = [],
Brian O'Connoree674952016-09-13 16:31:45 -0700241 group_id = ONOS_GROUP_ID,
242 version = ONOS_VERSION,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700243 test_srcs = None,
244 test_deps = [ '//lib:TEST' ],
245 test_resources = None,
246 test_resources_root = None,
247 visibility = [ 'PUBLIC' ],
248 **kwargs
249 ):
250
251 if name is None:
252 name = _get_name()
253
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700254 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700255 test_resources_root = TEST_RESOURCES_ROOT
256 if test_resources_root and not test_resources:
257 test_resources = glob([test_resources_root + '**'])
258 if not test_resources and not test_resources_root:
259 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
260 if test_resources:
261 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700262
Brian O'Connore4da59d2016-04-08 00:32:18 -0700263 if test_srcs is None:
264 test_srcs = glob([TEST + '/*.java'])
265
Brian O'Connoree674952016-09-13 16:31:45 -0700266 mvn_coords = group_id + ':' + name + ':jar:tests:' + version
267
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800268 if test_srcs:
269 java_test(
270 name = name + '-tests',
271 srcs = test_srcs,
272 deps = deps +
273 test_deps +
274 [':' + name + '#non-osgi'],
275 resources = test_resources,
276 resources_root = test_resources_root,
277 maven_coords = mvn_coords,
278 visibility = visibility,
279 )
Brian O'Connorbe95f682016-05-18 15:40:19 -0700280
Brian O'Connor9c2c8232016-11-08 17:13:14 -0800281 checkstyle(
282 name = name + '-tests',
283 srcs = test_srcs,
284 jar_target = ':' + name + '-tests',
285 )
286
287 sonar(
288 name = name + '-tests',
289 test = True
290 )
291
292 osgi_jar(name = name,
293 deps = deps,
294 group_id = group_id,
295 version = version,
296 visibility = visibility,
297 tests = [':' + name + '-tests'],
298 **kwargs)
299 else:
300 osgi_jar(name = name,
301 deps = deps,
302 group_id = group_id,
303 version = version,
304 visibility = visibility,
305 **kwargs)
Thomas Vachuskabe1a1962016-10-25 16:59:29 -0700306
307def tar_file(
308 name,
309 srcs,
310 root = None,
311 out = None,
312 visibility = [ 'PUBLIC' ],
313 ):
314
315 cmd = ( 'mkdir -p $TMP/%(root_dir)s && '
316 'cp -r $SRCDIR/ $TMP/%(root_dir)s && '
317 'tar -C $TMP -zcf $OUT %(root_dir)s' ) % {
318 'root_dir': root if root is not None else name
319 }
320
321 genrule(
322 name = name,
323 srcs = srcs,
324 bash = cmd,
325 out = out if out is not None else ( root if root is not None else name ) + '.tar.gz',
326 visibility = visibility,
327 )