blob: e5065f7321801cce797daca437ad62ae38f5b695 [file] [log] [blame]
Ray Milkey3275ae82018-05-29 15:35:36 -07001"""
2 Copyright 2018-present Open Networking Foundation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15"""
16
Ray Milkey7dac7da2017-08-01 16:56:05 -070017load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST")
18load("//tools/build/bazel:variables.bzl", "ONOS_VERSION")
19load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules")
Ray Milkeyb7949e72018-06-19 18:31:02 -070020load("//tools/build/bazel:checkstyle.bzl", "checkstyle_test")
Thomas Vachuskaac9e5242018-07-19 16:15:39 -070021load("//tools/build/bazel:pom_file.bzl", "pom_file")
Thomas Vachuska50ac0982018-07-19 10:17:37 -070022load("//tools/build/bazel:java_sources.bzl", "java_sources")
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070023load("//tools/build/bazel:java_sources.bzl", "java_sources_alt")
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -070024load("//tools/build/bazel:javadoc.bzl", "javadoc")
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070025load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
26
27def _auto_name():
28 return "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -070029
Ray Milkey32ea35c2018-06-06 15:28:07 -070030def _all_java_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070031 return native.glob(["src/main/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070032
Ray Milkey32ea35c2018-06-06 15:28:07 -070033def _all_java_test_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070034 return native.glob(["src/test/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070035
Ray Milkey32ea35c2018-06-06 15:28:07 -070036def _all_test_resources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070037 return native.glob(["src/test/resources/**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070038
Ray Milkey32ea35c2018-06-06 15:28:07 -070039def _all_resources(resources_root):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070040 if resources_root == None:
41 return native.glob(["src/main/resources/**"])
42 else:
43 return native.glob([resources_root + "**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070044
Ray Milkey25747d82018-06-13 14:12:51 -070045def _webapp():
Ray Milkey6b3775a2018-06-28 11:18:44 -070046 return native.glob(["src/main/webapp/WEB-INF/web.xml"])
47
48def _include_resources_to_string(include_resources):
49 result = ""
50 for (path, filename) in include_resources.items():
51 result += (path + "=" + filename)
52 return result
Ray Milkey25747d82018-06-13 14:12:51 -070053
Ray Milkey275af2c2018-06-15 13:05:08 -070054"""
55 Implementation of the rule to call bnd to make an OSGI jar file
56"""
Ray Milkey6b3775a2018-06-28 11:18:44 -070057
Ray Milkey7dac7da2017-08-01 16:56:05 -070058def _bnd_impl(ctx):
Ray Milkey3275ae82018-05-29 15:35:36 -070059 if (len(ctx.files.source) == 1):
60 input_file = ctx.files.source[0]
61 else:
62 # this is a list of inputs. The one we want is the last one
63 # in the list that isn't a source jar
64 for file in reversed(ctx.files.source):
65 if ("-src" in file.path):
66 continue
67 else:
68 input_file = file
69 break
70
71 jar = input_file.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070072 output = ctx.outputs.osgi_jar.path
Thomas Vachuska8e022a92018-07-10 14:47:38 -070073 name = ctx.attr.name
Ray Milkey275af2c2018-06-15 13:05:08 -070074 group = ctx.attr.group
Ray Milkey0bcdfd12018-05-23 14:07:19 -070075 version = ctx.attr.version
76 license = ""
Ray Milkey12ae6ca2018-06-11 15:34:30 -070077 import_packages = ctx.attr.import_packages
Jian Li4ad86872018-08-05 03:34:35 +090078 bundle_classpath = ctx.attr.bundle_classpath
Ray Milkey0bcdfd12018-05-23 14:07:19 -070079 exportPackages = "*"
Ray Milkey6b3775a2018-06-28 11:18:44 -070080 include_resources = ctx.attr.include_resources
Ray Milkey25747d82018-06-13 14:12:51 -070081 web_context = ctx.attr.web_context
82 if web_context == None or web_context == "":
83 web_context = "NONE"
84 web_xml = ctx.attr.web_xml
Ray Milkey0bcdfd12018-05-23 14:07:19 -070085 dynamicimportPackages = ""
Ray Milkeyd84f89b2018-08-17 14:54:17 -070086 karaf_commands = ctx.attr.karaf_commands
Ray Milkey3275ae82018-05-29 15:35:36 -070087 cp = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070088
Ray Milkey3275ae82018-05-29 15:35:36 -070089 inputDependencies = [input_file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070090
Ray Milkey0bcdfd12018-05-23 14:07:19 -070091 # determine the dependencies and build the class path
92 for dep in ctx.attr.deps:
Ray Milkey25b785a2018-06-12 09:59:14 -070093 if java_common.provider in dep:
94 file = dep.files.to_list()[0]
Ray Milkey25b785a2018-06-12 09:59:14 -070095 if cp:
96 cp += ":"
97 cp += file.path
98 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070099
Ray Milkey25747d82018-06-13 14:12:51 -0700100 web_xml_root_path = ""
101 if len(web_xml) != 0:
102 web_xml_root = web_xml[0].files.to_list()[0]
103 inputDependencies += [web_xml_root]
104 web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700105
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700106 # call bnd to make the OSGI jar file
107 arguments = [
108 jar,
109 output,
110 cp,
111 name,
112 group,
113 version,
114 license,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700115 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700116 exportPackages,
Ray Milkey6b3775a2018-06-28 11:18:44 -0700117 include_resources,
Ray Milkey25747d82018-06-13 14:12:51 -0700118 web_context,
119 web_xml_root_path,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700120 dynamicimportPackages,
Thomas Vachuskae4436942018-08-07 19:27:10 -0700121 "classes",
Jian Li4ad86872018-08-05 03:34:35 +0900122 bundle_classpath,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700123 karaf_commands,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700124 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700125
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700126 ctx.actions.run(
127 inputs = inputDependencies,
128 outputs = [ctx.outputs.osgi_jar],
129 arguments = arguments,
130 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
131 executable = ctx.executable._bnd_exe,
132 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700133
Ray Milkey25b785a2018-06-12 09:59:14 -0700134 deps = []
135 if java_common.provider in ctx.attr.source:
136 deps.append(ctx.attr.source[java_common.provider])
137 deps_provider = java_common.merge(deps)
138 return struct(
Ray Milkey15053f02018-06-13 10:00:45 -0700139 providers = [deps_provider],
Ray Milkey25b785a2018-06-12 09:59:14 -0700140 )
141
Ray Milkey275af2c2018-06-15 13:05:08 -0700142"""
143 Rule definition for calling bnd to make an OSGi jar file.
144"""
Ray Milkey32ea35c2018-06-06 15:28:07 -0700145_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700146 attrs = {
147 "deps": attr.label_list(),
148 "version": attr.string(),
Ray Milkey275af2c2018-06-15 13:05:08 -0700149 "group": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700150 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700151 "import_packages": attr.string(),
Jian Li4ad86872018-08-05 03:34:35 +0900152 "bundle_classpath": attr.string(),
Ray Milkey25747d82018-06-13 14:12:51 -0700153 "web_context": attr.string(),
154 "web_xml": attr.label_list(allow_files = True),
Ray Milkey6b3775a2018-06-28 11:18:44 -0700155 "include_resources": attr.string(),
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700156 "karaf_commands": attr.string(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700157 "_bnd_exe": attr.label(
158 executable = True,
159 cfg = "host",
160 allow_files = True,
161 default = Label("//utils/osgiwrap:osgi-jar"),
162 ),
163 },
164 fragments = ["java"],
165 outputs = {
166 "osgi_jar": "lib%{name}.jar",
167 },
168 implementation = _bnd_impl,
169)
170
Ray Milkey275af2c2018-06-15 13:05:08 -0700171"""
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700172 Implementation of the rule to generate cfgdef files from java class source
173"""
174
175def _cfgdef_impl(ctx):
176 output_jar = ctx.outputs.cfgdef_jar.path
177
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700178 arguments = [
179 output_jar,
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700180 ]
181
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700182 for src in ctx.files.srcs:
183 arguments.append(src.path)
184
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700185 ctx.actions.run(
186 inputs = ctx.files.srcs,
187 outputs = [ctx.outputs.cfgdef_jar],
188 arguments = arguments,
189 progress_message = "Running cfgdef generator on: %s" % ctx.attr.name,
190 executable = ctx.executable._cfgdef_generator_exe,
191 )
192
193"""
194 Rule definition to call cfgdef generator to create the *.cfgdef files from java sources
195"""
196_cfgdef = rule(
197 attrs = {
198 "srcs": attr.label_list(allow_files = True),
199 "_cfgdef_generator_exe": attr.label(
200 executable = True,
201 cfg = "host",
202 allow_files = True,
203 default = Label("//tools/build/cfgdef:cfgdef_generator"),
204 ),
205 "cfgdef_jar": attr.output(),
206 },
207 fragments = ["java"],
208 implementation = _cfgdef_impl,
209)
210
211"""
Ray Milkey275af2c2018-06-15 13:05:08 -0700212 Implementation of the rule to call swagger generator to create the registrator java class source
213"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700214
Ray Milkey275af2c2018-06-15 13:05:08 -0700215def _swagger_java_impl(ctx):
216 api_title = ctx.attr.api_title
217 api_version = ctx.attr.api_version
218 api_description = ctx.attr.api_description
219 api_package = ctx.attr.api_package
220 web_context = ctx.attr.web_context
221
222 output_java = ctx.outputs.swagger_java.path
Ray Milkey6b3775a2018-06-28 11:18:44 -0700223 output_dir = output_java[:output_java.find("generated-sources")]
Ray Milkey275af2c2018-06-15 13:05:08 -0700224
225 package_name = ctx.attr.package_name
226
227 srcs_arg = ""
228 resources_arg = ""
Ray Milkey275af2c2018-06-15 13:05:08 -0700229
230 for file in ctx.files.srcs:
231 srcs_arg += file.path + ","
Ray Milkey275af2c2018-06-15 13:05:08 -0700232
233 for resource in resources_arg:
234 resources_arg += resource.path + ","
235
236 # call swagger generator to make the swagger JSON and java files
237 arguments = [
238 srcs_arg,
239 resources_arg,
240 "",
241 package_name + "/src/main/resources",
242 output_dir,
243 output_dir,
244 web_context,
245 api_title,
246 api_version,
247 api_package,
248 api_description,
249 ]
250
251 ctx.actions.run(
252 inputs = ctx.files.srcs,
253 outputs = [ctx.outputs.swagger_java],
254 arguments = arguments,
255 progress_message = "Running swagger generator on: %s" % ctx.attr.name,
256 executable = ctx.executable._swagger_generator_exe,
257 )
258
259"""
260Implementation of the rule to call swagger generator for swagger.json file
261"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700262
Ray Milkey275af2c2018-06-15 13:05:08 -0700263def _swagger_json_impl(ctx):
264 api_title = ctx.attr.api_title
265 api_version = ctx.attr.api_version
266 api_description = ctx.attr.api_description
267 api_package = ctx.attr.api_package
268 web_context = ctx.attr.web_context
269
270 output_json = ctx.outputs.swagger_json.path
271 output_dir = output_json[:output_json.find("swagger.json")]
272
273 package_name = ctx.attr.package_name
274
275 srcs_arg = ""
276 resources_arg = ""
Ray Milkey275af2c2018-06-15 13:05:08 -0700277
278 for file in ctx.files.srcs:
279 srcs_arg += file.path + ","
Ray Milkey275af2c2018-06-15 13:05:08 -0700280
281 for resource in resources_arg:
282 resources_arg += resource.path + ","
283
284 # call swagger generator to make the swagger JSON and java files
285 arguments = [
286 srcs_arg,
287 resources_arg,
288 "",
289 package_name + "/src/main/resources",
290 output_dir,
291 output_dir,
292 web_context,
293 api_title,
294 api_version,
295 api_package,
296 api_description,
297 ]
298
299 ctx.actions.run(
300 inputs = ctx.files.srcs,
301 outputs = [ctx.outputs.swagger_json],
302 arguments = arguments,
303 progress_message = "Running swagger generator on: %s" % ctx.attr.name,
304 executable = ctx.executable._swagger_generator_exe,
305 )
306
307"""
308 Rule definition to call swagger generator to create the registrator java class source
309"""
310_swagger_java = rule(
311 attrs = {
312 "srcs": attr.label_list(allow_files = True),
313 "package_name": attr.string(),
314 "api_title": attr.string(),
315 "api_version": attr.string(),
316 "api_description": attr.string(),
317 "api_package": attr.string(),
318 "web_context": attr.string(),
319 "_swagger_generator_exe": attr.label(
320 executable = True,
321 cfg = "host",
322 allow_files = True,
Thomas Vachuska22ea6122018-10-24 12:28:59 -0700323 default = Label("//tools/build/swagger:swagger_generator"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700324 ),
325 "swagger_java": attr.output(),
326 },
327 fragments = ["java"],
328 implementation = _swagger_java_impl,
329)
330
331"""
332 Rule definition to call swagger generator to create the swagger JSON
333"""
334_swagger_json = rule(
335 attrs = {
336 "srcs": attr.label_list(allow_files = True),
337 "package_name": attr.string(),
338 "api_title": attr.string(),
339 "api_version": attr.string(),
340 "api_description": attr.string(),
341 "api_package": attr.string(),
342 "web_context": attr.string(),
343 "_swagger_generator_exe": attr.label(
344 executable = True,
345 cfg = "host",
346 allow_files = True,
Thomas Vachuska22ea6122018-10-24 12:28:59 -0700347 default = Label("//tools/build/swagger:swagger_generator"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700348 ),
349 "swagger_json": attr.output(),
350 },
351 fragments = ["java"],
352 implementation = _swagger_json_impl,
353)
354
355"""
356 Converts a jar file to an OSGI compatible jar file.
357
358 Args:
359 name: name of the rule to create the OSGI jar file - required
360 jar: jar file to convert - required target
361 deps: dependencies needed by the jar file - required list of targets
362 version: Version of the generated jar file. Optional, defaults to the current ONOS version
363 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
364 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
365 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to private
366"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700367
Ray Milkey25747d82018-06-13 14:12:51 -0700368def wrapped_osgi_jar(
369 name,
370 jar,
371 deps,
372 version = ONOS_VERSION,
Ray Milkey275af2c2018-06-15 13:05:08 -0700373 group = "org.onosproject",
Ray Milkey25747d82018-06-13 14:12:51 -0700374 import_packages = "*",
Ray Milkey25747d82018-06-13 14:12:51 -0700375 visibility = ["//visibility:private"]):
376 _bnd(
377 name = name,
378 source = jar,
379 deps = deps,
380 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700381 group = group,
Ray Milkey25747d82018-06-13 14:12:51 -0700382 visibility = visibility,
383 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700384 web_xml = None,
Ray Milkey25747d82018-06-13 14:12:51 -0700385 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700386
Ray Milkey275af2c2018-06-15 13:05:08 -0700387"""
388 Creates an OSGI jar and test jar file from a set of source and test files.
389 See osgi_jar() for a description of shared parameters.
390 Args:
391 test_srcs: Test source file(s) to compile. Optional list of targets, defaults to src/test/java/**/*.java
392 test_deps: Dependencies for the test jar. Optional list of targets, defaults to a common set of dependencies
393 test_resources: Resources to include in the test jar. Optional list of targets, defaults to src/test/resources/**
394 exclude_tests: Tests that should not be run. Useful for excluding things like test files without any @Test methods.
395 Optional ist of targets, defaults to []
396"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700397
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700398def osgi_jar_with_tests(
399 name = None,
400 deps = None,
401 test_deps = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700402 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700403 srcs = None,
404 resources_root = None,
405 resources = None,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700406 resource_jars = [],
Ray Milkey6b3775a2018-06-28 11:18:44 -0700407 include_resources = {},
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700408 test_srcs = None,
409 exclude_tests = None,
Thomas Vachuskaa79bf6e2018-08-07 11:24:40 -0700410 medium_tests = [],
411 large_tests = [],
412 enormous_tests = [],
413 flaky_tests = [],
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700414 test_resources = None,
415 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700416 version = ONOS_VERSION,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700417 suppress_errorprone = False,
Ray Milkey134d2922018-07-15 15:24:01 -0700418 suppress_checkstyle = False,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700419 suppress_javadocs = False,
Ray Milkey25747d82018-06-13 14:12:51 -0700420 web_context = None,
421 api_title = "",
422 api_version = "",
423 api_description = "",
424 api_package = "",
Jian Li4ad86872018-08-05 03:34:35 +0900425 import_packages = None,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700426 bundle_classpath = "",
427 karaf_command_packages = []):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700428 if name == None:
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700429 name = _auto_name()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700430 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700431 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700432 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700433 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700434 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700435 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700436 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700437 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700438 if exclude_tests == None:
439 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700440 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700441 deps = COMPILE
442 if test_deps == None:
443 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700444 if import_packages == None:
445 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700446 tests_name = name + "-tests"
447 tests_jar_deps = list(depset(deps + test_deps)) + [name]
448 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey25747d82018-06-13 14:12:51 -0700449 web_xml = _webapp()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700450
Ray Milkey275af2c2018-06-15 13:05:08 -0700451 native_srcs = srcs
452 native_resources = resources
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700453
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700454 if web_context != None and api_title != "" and len(resources) != 0:
Ray Milkey275af2c2018-06-15 13:05:08 -0700455 # generate Swagger files if needed
456 _swagger_java(
457 name = name + "_swagger_java",
458 srcs = srcs + resources,
459 package_name = native.package_name(),
460 api_title = api_title,
461 api_version = api_version,
462 api_description = api_description,
463 web_context = web_context,
464 api_package = api_package,
465 swagger_java = ("src/main/resources/apidoc/generated-sources/" +
Ray Milkey6b3775a2018-06-28 11:18:44 -0700466 api_package.replace(".", "/") +
467 "/ApiDocRegistrator.java").replace("//", "/"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700468 )
469 _swagger_json(
470 name = name + "_swagger_json",
471 srcs = srcs + resources,
472 package_name = native.package_name(),
473 api_title = api_title,
474 api_version = api_version,
475 api_description = api_description,
476 web_context = web_context,
477 api_package = api_package,
478 swagger_json = "src/main/resources/apidoc/swagger.json",
479 )
480 native_resources = []
481 for r in resources:
Ray Milkey6b3775a2018-06-28 11:18:44 -0700482 if not "definitions" in r:
Ray Milkey275af2c2018-06-15 13:05:08 -0700483 native_resources.append(r)
Ray Milkey6b3775a2018-06-28 11:18:44 -0700484 native_srcs = srcs + [name + "_swagger_java"]
485 native_resources.append(name + "_swagger_json")
Ray Milkey275af2c2018-06-15 13:05:08 -0700486
Ray Milkey5063f5b2018-08-15 16:22:30 -0700487 javacopts = ["-XepDisableAllChecks"] if suppress_errorprone else []
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700488
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700489 _cfgdef(
490 name = name + "_cfgdef_jar",
491 srcs = native_srcs,
492 visibility = visibility,
493 cfgdef_jar = name + "_cfgdef.jar",
494 )
495
Ray Milkey25747d82018-06-13 14:12:51 -0700496 # compile the Java code
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700497 if len(resource_jars) > 0:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700498 native.java_library(
499 name = name + "-native",
500 srcs = native_srcs,
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700501 resource_jars = resource_jars + [name + "_cfgdef_jar"],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700502 deps = deps,
503 visibility = visibility,
504 javacopts = javacopts,
505 )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700506 else:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700507 native.java_library(
508 name = name + "-native",
509 srcs = native_srcs,
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700510 resource_jars = [name + "_cfgdef_jar"],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700511 resources = native_resources,
512 deps = deps,
513 visibility = visibility,
514 javacopts = javacopts,
515 )
Ray Milkey25747d82018-06-13 14:12:51 -0700516
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700517 karaf_command_packages_string = ",".join(karaf_command_packages)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700518 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700519 name = name,
520 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700521 deps = deps,
522 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700523 group = group,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700524 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700525 import_packages = import_packages,
Jian Li4ad86872018-08-05 03:34:35 +0900526 bundle_classpath = bundle_classpath,
Ray Milkey25747d82018-06-13 14:12:51 -0700527 web_context = web_context,
528 web_xml = web_xml,
Ray Milkey6b3775a2018-06-28 11:18:44 -0700529 include_resources = _include_resources_to_string(include_resources),
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700530 karaf_commands = karaf_command_packages_string,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700531 )
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700532
Thomas Vachuskaac9e5242018-07-19 16:15:39 -0700533 # rule for generating pom file for publishing
534 pom_file(name = name + "-pom", artifact = name, deps = deps, visibility = visibility)
535
536 # rule for building source jar
Thomas Vachuska50ac0982018-07-19 10:17:37 -0700537 if not suppress_javadocs:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700538 java_sources(name = name + "-sources", srcs = srcs, visibility = visibility)
Thomas Vachuska50ac0982018-07-19 10:17:37 -0700539
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700540 # rule for building javadocs
541 if not suppress_javadocs:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700542 javadoc(name = name + "-javadoc", deps = deps, srcs = srcs, visibility = visibility)
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700543
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700544 if test_srcs != []:
545 native.java_library(
546 name = tests_name,
547 srcs = test_srcs,
548 resources = test_resources,
549 deps = tests_jar_deps,
550 visibility = visibility,
551 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700552
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700553 generate_test_rules(
554 name = name + "-tests-gen",
555 test_files = test_srcs,
556 exclude_tests = exclude_tests,
Thomas Vachuskaa79bf6e2018-08-07 11:24:40 -0700557 medium_tests = medium_tests,
558 large_tests = large_tests,
559 enormous_tests = enormous_tests,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700560 deps = all_test_deps,
561 )
562
Ray Milkey134d2922018-07-15 15:24:01 -0700563 if not suppress_checkstyle:
564 checkstyle_test(
565 name = name + "_checkstyle_test",
566 srcs = srcs,
567 )
Ray Milkeyb64fe822018-08-30 09:18:16 -0700568 if test_srcs != []:
569 checkstyle_test(
570 name = name + "_checkstyle_tests_test",
571 srcs = test_srcs,
572 )
Ray Milkeyb7949e72018-06-19 18:31:02 -0700573
Ray Milkey275af2c2018-06-15 13:05:08 -0700574"""
575 Creates an OSGI jar file from a set of source files.
576
577 Args:
578 name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree.
579 For example apps/mcast/app becomes onos-apps-mcast-app
580 deps: Dependencies of the generated jar file. Expressed as a list of targets
581 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
Jian Li4ad86872018-08-05 03:34:35 +0900582 bundle_classpath: intended for including dependencies in our bundle, so that our bundle can be deployed standalone
Ray Milkey275af2c2018-06-15 13:05:08 -0700583 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
584 srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java
585 resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources
586 resources: Resources to include in the jar file. Optional list of targets, defaults to all files beneath resources_root
587 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public
588 version: Version of the generated jar file. Optional, defaults to the current ONOS version
Ray Milkey134d2922018-07-15 15:24:01 -0700589 suppress_errorprone: If true, don't run ErrorProne tests. Default is false
590 suppress_checkstyle: If true, don't run checkstyle tests. Default is false
Ray Milkey275af2c2018-06-15 13:05:08 -0700591 web_context: Web context for a WAB file if needed. Only needed if the jar file provides a REST API. Optional string
592 api_title: Swagger API title. Optional string, only used if the jar file provides a REST API and has swagger annotations
593 api_version: Swagger API version. Optional string, only used if the jar file provides a REST API and has swagger annotations
594 api_description: Swagger API description. Optional string, only used if the jar file provides a REST API and has swagger annotations
595 api_package: Swagger API package name. Optional string, only used if the jar file provides a REST API and has swagger annotations
596"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700597
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700598def osgi_jar(
599 name = None,
600 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700601 import_packages = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700602 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700603 srcs = None,
604 resources_root = None,
605 resources = None,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700606 resource_jars = [],
Ray Milkey6b3775a2018-06-28 11:18:44 -0700607 include_resources = {},
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700608 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700609 version = ONOS_VERSION,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700610 suppress_errorprone = False,
Ray Milkey134d2922018-07-15 15:24:01 -0700611 suppress_checkstyle = False,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700612 suppress_javadocs = False,
Ray Milkey25747d82018-06-13 14:12:51 -0700613 web_context = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700614 api_title = "",
615 api_version = "",
616 api_description = "",
Jian Li4ad86872018-08-05 03:34:35 +0900617 api_package = "",
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700618 bundle_classpath = "",
619 karaf_command_packages = []):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700620 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700621 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700622 if deps == None:
623 deps = COMPILE
624
625 osgi_jar_with_tests(
626 name = name,
627 deps = deps,
628 test_deps = [],
Ray Milkey275af2c2018-06-15 13:05:08 -0700629 group = group,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700630 srcs = srcs,
631 resources = resources,
632 resources_root = resources_root,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700633 resource_jars = resource_jars,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700634 test_srcs = [],
635 exclude_tests = [],
636 test_resources = [],
637 visibility = visibility,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700638 suppress_errorprone = suppress_errorprone,
Ray Milkey134d2922018-07-15 15:24:01 -0700639 suppress_checkstyle = suppress_checkstyle,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700640 suppress_javadocs = suppress_javadocs,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700641 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700642 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700643 api_title = api_title,
644 api_version = api_version,
645 api_description = api_description,
646 api_package = api_package,
Ray Milkey25747d82018-06-13 14:12:51 -0700647 web_context = web_context,
Jian Li4ad86872018-08-05 03:34:35 +0900648 bundle_classpath = bundle_classpath,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700649 karaf_command_packages = karaf_command_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700650 )
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700651
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700652"""
653 Creates an OSGI jar file from a set of protobuf and gRPC libraries.
654
655 Args:
656 name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree.
657 For example apps/mcast/app becomes onos-apps-mcast-app
658 proto_libs: (required) list of proto_library targets which generated Java classes will be included to this OSGi
659 jar. It is important that all the given targets reference to a single proto source files, for example
660 only the first 2 rules are good:
661
662 proto_library(
663 name = "foo_proto",
664 srcs = ["foo.proto"],
665 )
666
667 proto_library(
668 name = "bar_proto",
669 srcs = ["bar.proto"],
670 )
671
672 # THIS WILL NOT WORK
673 proto_library(
674 name = "foo_and_bar_proto",
675 srcs = ["foo.proto", "bar.proto"],
676 )
677
678 grpc_proto_lib: (optional) proto_library target that contains the schema of a gRPC service. If not passed,
679 the produced jar will NOT have any gRPC stub classes.
680 deps: Dependencies of the generated jar file. Expressed as a list of targets
681 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
682 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public
683 version: Version of the generated jar file. Optional, defaults to the current ONOS version
684"""
685
686def osgi_proto_jar(
687 proto_libs,
688 grpc_proto_lib = None,
689 name = None,
690 deps = [],
691 group = "org.onosproject",
692 visibility = ["//visibility:public"],
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700693 version = ONOS_VERSION,
694 karaf_command_packages = []):
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700695 if name == None:
696 name = _auto_name()
697 native.java_proto_library(
698 name = name + "-java-proto",
699 deps = proto_libs,
700 )
701 java_sources_alt(
702 name = name + "-proto-srcjar",
703 srcs = [":%s-java-proto" % name],
704 )
705 osgi_srcs = [
706 ":%s-proto-srcjar" % name,
707 ]
708 base_deps = [
709 "@com_google_protobuf//:protobuf_java",
710 ]
711 if grpc_proto_lib != None:
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700712 java_grpc_library(
713 name = name + "-java-grpc",
714 srcs = [grpc_proto_lib],
715 deps = [":%s-java-proto" % name],
716 )
717 osgi_srcs.append(
Ray Milkey5063f5b2018-08-15 16:22:30 -0700718 ":%s-java-grpc__do_not_reference__srcjar" % name,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700719 )
720 base_deps.extend([
721 "@com_google_guava_guava//jar",
722 "@io_grpc_grpc_java//core",
723 "@io_grpc_grpc_java//stub",
724 "@io_grpc_grpc_java//protobuf",
725 ])
726 osgi_jar(
727 name = name,
728 srcs = osgi_srcs,
729 deps = base_deps + deps,
730 group = group,
731 visibility = visibility,
732 version = version,
733 suppress_errorprone = True,
734 suppress_checkstyle = True,
735 suppress_javadocs = True,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700736 karaf_command_packages = karaf_command_packages,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700737 )