blob: e5e4b0ad764223cc8d97ddbf1fb11657f7a53dc3 [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',
Brian O'Connor40a3fbd2016-06-08 13:54:46 -070038 deps = [ jar_target ],
39 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
94
Brian O'Connor42c38cf2016-04-05 17:05:57 -070095def osgi_jar(
Brian O'Connore4da59d2016-04-08 00:32:18 -070096 name = None,
97 srcs = None,
Brian O'Connor1f165982016-04-06 21:36:09 -070098 group_id = ONOS_GROUP_ID,
99 version = ONOS_VERSION,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700100 deps = [],
101 visibility = ['PUBLIC'],
102 license = 'NONE',
103 description = '',
104 debug = False,
Brian O'Connore5817c92016-04-06 15:41:48 -0700105 import_packages = '*',
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700106 dynamicimport_packages = '',
Brian O'Connore5817c92016-04-06 15:41:48 -0700107 export_packages = '*',
Ray Milkey2a749832016-08-02 15:22:20 -0700108 package_name_root = 'org.onosproject',
Brian O'Connore5817c92016-04-06 15:41:48 -0700109 include_resources = NONE,
110 web_context = NONE,
Brian O'Connore8468b52016-07-25 13:42:36 -0700111 api_title = NONE,
112 api_version = NONE,
113 api_package = NONE,
114 api_description = NONE,
Brian O'Connore4da59d2016-04-08 00:32:18 -0700115 resources = NONE,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700116 resources_root = None,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700117 **kwargs
118 ):
119
Brian O'Connore4da59d2016-04-08 00:32:18 -0700120 # if name and _get_name() != name:
121 # print _get_name(), '!=', name
122 if name is None:
123 name = _get_name()
124
125 if srcs is None:
126 srcs = glob([SRC + '/*.java'])
127
128 if resources == NONE and resources_root is not None:
129 resources = glob([resources_root + '**'])
130 elif resources == NONE:
131 resources = glob([RESOURCES_ROOT + '**'])
132
133 if resources and not resources_root:
134 resources_root = RESOURCES_ROOT
135
Brian O'Connore8468b52016-07-25 13:42:36 -0700136 if api_title != NONE:
137 r = 'WEB-INF/classes/apidoc/swagger.json=bin/swagger.json'
138 include_resources = include_resources + ',' + r if include_resources != NONE else r
139
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700140 bare_jar_name = name + '-jar'
141 osgi_jar_name = name + '-osgi'
142 mvn_coords = group_id + ':' + name + ':' + version
143
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700144
Brian O'Connore8468b52016-07-25 13:42:36 -0700145 onos_jar(
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700146 name = bare_jar_name,
147 srcs = srcs,
148 deps = deps,
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700149 visibility = [], #intentially, not visible
150 resources = resources,
151 resources_root = resources_root,
Brian O'Connore8468b52016-07-25 13:42:36 -0700152 web_context = web_context,
153 api_title = api_title,
154 api_version = api_version,
155 api_package = api_package,
156 api_description = api_description,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700157 **kwargs
158 )
159
Brian O'Connor4847ea32016-04-29 16:33:06 -0700160 cp = ':'.join(['$(classpath %s)' % c for c in deps]) if deps else '""'
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700161
162 args = ( '$(location :%s)' % bare_jar_name, #input jar
163 '$OUT', #output jar
164 cp, #classpath
165 name, #bundle name
166 group_id, #group id
167 version, #version
168 license, #license url
Brian O'Connore5817c92016-04-06 15:41:48 -0700169 "'%s'" % import_packages, #packages to import
170 "'%s'" % export_packages, #packages to export
171 include_resources, #custom includes to classpath
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700172 web_context, #web context (REST API only)
Yuta HIGUCHIf05db402016-08-12 18:36:33 -0700173 "'%s'" % dynamicimport_packages, #DynamicImport-Package
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700174 description, #description
175 )
176
177 #TODO stage_jar is a horrendous hack
178 stage_jar = 'pushd $SRCDIR; mkdir bin; cd bin; jar xf $(location :%s); ls; popd; ' % bare_jar_name
179 wrap_jar = '$(exe //utils/osgiwrap:osgi-jar) ' + ' '.join(args)
180 bash = stage_jar + wrap_jar
181 if debug:
182 bash = stage_jar + DEBUG_ARG + ' ' + wrap_jar
183 print bash
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700184
Brian O'Connore8468b52016-07-25 13:42:36 -0700185 # FIXME: make sure that /swagger.json gets filtered
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700186 genrule(
187 name = osgi_jar_name,
188 bash = bash,
Brian O'Connor1f165982016-04-06 21:36:09 -0700189 out = '%s-%s.jar' % (name, version), #FIXME add version to jar file
Brian O'Connore5817c92016-04-06 15:41:48 -0700190 srcs = glob(['src/main/webapp/**']),
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700191 visibility = [], #intentially, not visible
192 )
193
194 # TODO we really should shade the jar with maven flavor
195 prebuilt_jar(
196 name = name,
197 maven_coords = mvn_coords,
198 binary_jar = ':' + osgi_jar_name,
199 visibility = visibility,
200 )
201
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700202 ### Checkstyle
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700203 checkstyle(
Brian O'Connorbe95f682016-05-18 15:40:19 -0700204 name = name + '-checkstyle-files',
Brian O'Connor4847ea32016-04-29 16:33:06 -0700205 srcs = srcs,
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700206 jar_target = ':'+ bare_jar_name,
207 )
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700208
Ray Milkeyc340e282016-06-30 14:45:42 -0700209 java_doc(
210 name = name + '-javadoc',
211 title = 'Java Docs',
Ray Milkey2a749832016-08-02 15:22:20 -0700212 pkgs = [ package_name_root ],
Ray Milkeyc340e282016-06-30 14:45:42 -0700213 paths = [ 'src/main/java' ],
214 srcs = srcs,
215 deps = deps,
216 visibility = visibility,
217 do_it_wrong = False,
218 )
219
Brian O'Connorb3cc6042016-04-25 11:55:51 -0700220 # TODO add project config for intellij
221 # project_config(
222 # src_target = ':' + bare_jar_name,
223 # src_roots = [ 'src/main/java' ],
224 # test_target = ':' + name + '-tests',
225 # test_roots = [ 'src/test/java' ],
226 # )
227
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700228 ### .m2 Install
Brian O'Connorb86c9202016-04-05 20:15:04 -0700229 mvn_cmd = ' '.join(( 'mvn install:install-file',
230 '-Dfile=$(location :%s)' % name,
231 '-DgroupId=%s' % group_id,
232 '-DartifactId=%s' % name,
233 '-Dversion=%s' % version,
234 '-Dpackaging=jar' ))
Brian O'Connore5817c92016-04-06 15:41:48 -0700235 cmd = mvn_cmd + ' > $OUT'
236 if FORCE_INSTALL:
237 # Add a random number to the command to force this rule to run.
238 # TODO We should make this configurable from CLI, perhaps with a flag.
239 cmd = 'FOO=%s ' % random.random() + cmd
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700240 genrule(
241 name = name + '-install',
Brian O'Connorb86c9202016-04-05 20:15:04 -0700242 bash = cmd,
Brian O'Connor42c38cf2016-04-05 17:05:57 -0700243 out = 'install.log',
244 visibility = visibility,
245 )
246
Brian O'Connore4da59d2016-04-08 00:32:18 -0700247def osgi_jar_with_tests(
248 name = None,
249 deps = [],
250 test_srcs = None,
251 test_deps = [ '//lib:TEST' ],
252 test_resources = None,
253 test_resources_root = None,
254 visibility = [ 'PUBLIC' ],
255 **kwargs
256 ):
257
258 if name is None:
259 name = _get_name()
260
261 osgi_jar(name = name,
262 deps = deps,
263 visibility = visibility,
264 **kwargs)
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700265
266 if test_resources and not test_resources_root:
Brian O'Connore4da59d2016-04-08 00:32:18 -0700267 test_resources_root = TEST_RESOURCES_ROOT
268 if test_resources_root and not test_resources:
269 test_resources = glob([test_resources_root + '**'])
270 if not test_resources and not test_resources_root:
271 test_resources = glob([TEST_RESOURCES_ROOT + '**'])
272 if test_resources:
273 test_resources_root = TEST_RESOURCES_ROOT
Brian O'Connor0f6677d2016-04-06 23:26:51 -0700274
Brian O'Connore4da59d2016-04-08 00:32:18 -0700275 if test_srcs is None:
276 test_srcs = glob([TEST + '/*.java'])
277
Brian O'Connore4da59d2016-04-08 00:32:18 -0700278 java_test(
279 name = name + '-tests',
280 srcs = test_srcs,
281 deps = deps +
282 test_deps +
283 [':' + name + '-jar'],
284 source_under_test = [':' + name + '-jar'],
285 resources = test_resources,
286 resources_root = test_resources_root,
287 visibility = visibility
Brian O'Connorbe95f682016-05-18 15:40:19 -0700288 )
289
Brian O'Connor40a3fbd2016-06-08 13:54:46 -0700290 checkstyle(
291 name = name + '-tests',
292 srcs = test_srcs,
293 jar_target = ':' + name + '-tests',
294 )
295
Brian O'Connore642f7c2016-05-23 18:33:04 -0700296 #FIXME need to run checkstyle on test sources