blob: 491444f44136e2b7d3487d53be86f51ac74243d7 [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")
Ray Milkey24ba24a2019-04-26 11:53:02 -070025load("//tools/build/bazel:minimal_jar.bzl", "minimal_jar")
Carmelo Cascone6a1ae712018-08-10 12:19:47 -070026load("@io_grpc_grpc_java//:java_grpc_library.bzl", "java_grpc_library")
27
28def _auto_name():
29 return "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -070030
Ray Milkey32ea35c2018-06-06 15:28:07 -070031def _all_java_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070032 return native.glob(["src/main/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070033
Ray Milkey32ea35c2018-06-06 15:28:07 -070034def _all_java_test_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070035 return native.glob(["src/test/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070036
Ray Milkey32ea35c2018-06-06 15:28:07 -070037def _all_test_resources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070038 return native.glob(["src/test/resources/**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070039
Ray Milkey32ea35c2018-06-06 15:28:07 -070040def _all_resources(resources_root):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070041 if resources_root == None:
42 return native.glob(["src/main/resources/**"])
43 else:
44 return native.glob([resources_root + "**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070045
Ray Milkey25747d82018-06-13 14:12:51 -070046def _webapp():
Ray Milkey6b3775a2018-06-28 11:18:44 -070047 return native.glob(["src/main/webapp/WEB-INF/web.xml"])
48
49def _include_resources_to_string(include_resources):
50 result = ""
51 for (path, filename) in include_resources.items():
52 result += (path + "=" + filename)
53 return result
Ray Milkey25747d82018-06-13 14:12:51 -070054
Ray Milkey275af2c2018-06-15 13:05:08 -070055"""
56 Implementation of the rule to call bnd to make an OSGI jar file
57"""
Ray Milkey6b3775a2018-06-28 11:18:44 -070058
Ray Milkey7dac7da2017-08-01 16:56:05 -070059def _bnd_impl(ctx):
Ray Milkey3275ae82018-05-29 15:35:36 -070060 if (len(ctx.files.source) == 1):
61 input_file = ctx.files.source[0]
62 else:
63 # this is a list of inputs. The one we want is the last one
64 # in the list that isn't a source jar
65 for file in reversed(ctx.files.source):
66 if ("-src" in file.path):
67 continue
68 else:
69 input_file = file
70 break
71
72 jar = input_file.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070073 output = ctx.outputs.osgi_jar.path
Thomas Vachuska8e022a92018-07-10 14:47:38 -070074 name = ctx.attr.name
Ray Milkey275af2c2018-06-15 13:05:08 -070075 group = ctx.attr.group
Ray Milkey0bcdfd12018-05-23 14:07:19 -070076 version = ctx.attr.version
77 license = ""
Ray Milkey12ae6ca2018-06-11 15:34:30 -070078 import_packages = ctx.attr.import_packages
Jian Li4ad86872018-08-05 03:34:35 +090079 bundle_classpath = ctx.attr.bundle_classpath
Ray Milkey0bcdfd12018-05-23 14:07:19 -070080 exportPackages = "*"
Ray Milkey6b3775a2018-06-28 11:18:44 -070081 include_resources = ctx.attr.include_resources
Ray Milkey25747d82018-06-13 14:12:51 -070082 web_context = ctx.attr.web_context
83 if web_context == None or web_context == "":
84 web_context = "NONE"
85 web_xml = ctx.attr.web_xml
Ray Milkey0bcdfd12018-05-23 14:07:19 -070086 dynamicimportPackages = ""
Ray Milkeyd84f89b2018-08-17 14:54:17 -070087 karaf_commands = ctx.attr.karaf_commands
Daniele Moro27527592020-02-03 23:33:55 -080088 fragment_host = ctx.attr.fragment_host
Ray Milkey3275ae82018-05-29 15:35:36 -070089 cp = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070090
Ray Milkey3275ae82018-05-29 15:35:36 -070091 inputDependencies = [input_file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070092
Ray Milkey0bcdfd12018-05-23 14:07:19 -070093 # determine the dependencies and build the class path
94 for dep in ctx.attr.deps:
Ray Milkey25b785a2018-06-12 09:59:14 -070095 if java_common.provider in dep:
96 file = dep.files.to_list()[0]
Ray Milkey25b785a2018-06-12 09:59:14 -070097 if cp:
98 cp += ":"
99 cp += file.path
100 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700101
Ray Milkey25747d82018-06-13 14:12:51 -0700102 web_xml_root_path = ""
103 if len(web_xml) != 0:
104 web_xml_root = web_xml[0].files.to_list()[0]
105 inputDependencies += [web_xml_root]
106 web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700107
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700108 # call bnd to make the OSGI jar file
109 arguments = [
110 jar,
111 output,
112 cp,
113 name,
114 group,
115 version,
116 license,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700117 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700118 exportPackages,
Ray Milkey6b3775a2018-06-28 11:18:44 -0700119 include_resources,
Ray Milkey25747d82018-06-13 14:12:51 -0700120 web_context,
121 web_xml_root_path,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700122 dynamicimportPackages,
Thomas Vachuskae4436942018-08-07 19:27:10 -0700123 "classes",
Jian Li4ad86872018-08-05 03:34:35 +0900124 bundle_classpath,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700125 karaf_commands,
Daniele Moro27527592020-02-03 23:33:55 -0800126 fragment_host,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700127 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700128
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700129 ctx.actions.run(
130 inputs = inputDependencies,
131 outputs = [ctx.outputs.osgi_jar],
132 arguments = arguments,
133 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
134 executable = ctx.executable._bnd_exe,
135 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700136
Ray Milkey25b785a2018-06-12 09:59:14 -0700137 deps = []
138 if java_common.provider in ctx.attr.source:
139 deps.append(ctx.attr.source[java_common.provider])
140 deps_provider = java_common.merge(deps)
141 return struct(
Ray Milkey15053f02018-06-13 10:00:45 -0700142 providers = [deps_provider],
Ray Milkey25b785a2018-06-12 09:59:14 -0700143 )
144
Ray Milkey275af2c2018-06-15 13:05:08 -0700145"""
146 Rule definition for calling bnd to make an OSGi jar file.
147"""
Ray Milkey32ea35c2018-06-06 15:28:07 -0700148_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700149 attrs = {
150 "deps": attr.label_list(),
151 "version": attr.string(),
Ray Milkey275af2c2018-06-15 13:05:08 -0700152 "group": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700153 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700154 "import_packages": attr.string(),
Jian Li4ad86872018-08-05 03:34:35 +0900155 "bundle_classpath": attr.string(),
Ray Milkey25747d82018-06-13 14:12:51 -0700156 "web_context": attr.string(),
157 "web_xml": attr.label_list(allow_files = True),
Ray Milkey6b3775a2018-06-28 11:18:44 -0700158 "include_resources": attr.string(),
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700159 "karaf_commands": attr.string(),
Daniele Moro27527592020-02-03 23:33:55 -0800160 "fragment_host": attr.string(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700161 "_bnd_exe": attr.label(
162 executable = True,
163 cfg = "host",
164 allow_files = True,
165 default = Label("//utils/osgiwrap:osgi-jar"),
166 ),
167 },
168 fragments = ["java"],
169 outputs = {
170 "osgi_jar": "lib%{name}.jar",
171 },
172 implementation = _bnd_impl,
173)
174
Ray Milkey275af2c2018-06-15 13:05:08 -0700175"""
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700176 Implementation of the rule to generate cfgdef files from java class source
177"""
178
179def _cfgdef_impl(ctx):
180 output_jar = ctx.outputs.cfgdef_jar.path
181
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700182 arguments = [
183 output_jar,
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700184 ]
185
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700186 for src in ctx.files.srcs:
187 arguments.append(src.path)
188
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700189 ctx.actions.run(
190 inputs = ctx.files.srcs,
191 outputs = [ctx.outputs.cfgdef_jar],
192 arguments = arguments,
193 progress_message = "Running cfgdef generator on: %s" % ctx.attr.name,
194 executable = ctx.executable._cfgdef_generator_exe,
195 )
196
197"""
198 Rule definition to call cfgdef generator to create the *.cfgdef files from java sources
199"""
200_cfgdef = rule(
201 attrs = {
202 "srcs": attr.label_list(allow_files = True),
203 "_cfgdef_generator_exe": attr.label(
204 executable = True,
205 cfg = "host",
206 allow_files = True,
207 default = Label("//tools/build/cfgdef:cfgdef_generator"),
208 ),
209 "cfgdef_jar": attr.output(),
210 },
211 fragments = ["java"],
212 implementation = _cfgdef_impl,
213)
214
215"""
Ray Milkey275af2c2018-06-15 13:05:08 -0700216 Implementation of the rule to call swagger generator to create the registrator java class source
217"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700218
Ray Milkey275af2c2018-06-15 13:05:08 -0700219def _swagger_java_impl(ctx):
220 api_title = ctx.attr.api_title
221 api_version = ctx.attr.api_version
222 api_description = ctx.attr.api_description
223 api_package = ctx.attr.api_package
224 web_context = ctx.attr.web_context
225
226 output_java = ctx.outputs.swagger_java.path
Ray Milkey6b3775a2018-06-28 11:18:44 -0700227 output_dir = output_java[:output_java.find("generated-sources")]
Ray Milkey275af2c2018-06-15 13:05:08 -0700228
229 package_name = ctx.attr.package_name
230
231 srcs_arg = ""
Ray Milkey275af2c2018-06-15 13:05:08 -0700232
233 for file in ctx.files.srcs:
234 srcs_arg += file.path + ","
Ray Milkey275af2c2018-06-15 13:05:08 -0700235
Ray Milkey275af2c2018-06-15 13:05:08 -0700236 # call swagger generator to make the swagger JSON and java files
237 arguments = [
238 srcs_arg,
Ray Milkey8c6fe172019-01-29 17:34:04 -0800239 "",
Ray Milkey275af2c2018-06-15 13:05:08 -0700240 "",
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 = ""
Ray Milkey275af2c2018-06-15 13:05:08 -0700276
277 for file in ctx.files.srcs:
278 srcs_arg += file.path + ","
Ray Milkey275af2c2018-06-15 13:05:08 -0700279
Ray Milkey275af2c2018-06-15 13:05:08 -0700280 # call swagger generator to make the swagger JSON and java files
281 arguments = [
282 srcs_arg,
Ray Milkey8c6fe172019-01-29 17:34:04 -0800283 "",
Ray Milkey275af2c2018-06-15 13:05:08 -0700284 "",
285 package_name + "/src/main/resources",
286 output_dir,
287 output_dir,
288 web_context,
289 api_title,
290 api_version,
291 api_package,
292 api_description,
293 ]
294
295 ctx.actions.run(
296 inputs = ctx.files.srcs,
297 outputs = [ctx.outputs.swagger_json],
298 arguments = arguments,
299 progress_message = "Running swagger generator on: %s" % ctx.attr.name,
300 executable = ctx.executable._swagger_generator_exe,
301 )
302
303"""
304 Rule definition to call swagger generator to create the registrator java class source
305"""
306_swagger_java = rule(
307 attrs = {
308 "srcs": attr.label_list(allow_files = True),
309 "package_name": attr.string(),
310 "api_title": attr.string(),
311 "api_version": attr.string(),
312 "api_description": attr.string(),
313 "api_package": attr.string(),
314 "web_context": attr.string(),
315 "_swagger_generator_exe": attr.label(
316 executable = True,
317 cfg = "host",
318 allow_files = True,
Thomas Vachuska22ea6122018-10-24 12:28:59 -0700319 default = Label("//tools/build/swagger:swagger_generator"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700320 ),
321 "swagger_java": attr.output(),
322 },
323 fragments = ["java"],
324 implementation = _swagger_java_impl,
325)
326
327"""
328 Rule definition to call swagger generator to create the swagger JSON
329"""
330_swagger_json = rule(
331 attrs = {
332 "srcs": attr.label_list(allow_files = True),
333 "package_name": attr.string(),
334 "api_title": attr.string(),
335 "api_version": attr.string(),
336 "api_description": attr.string(),
337 "api_package": attr.string(),
338 "web_context": attr.string(),
339 "_swagger_generator_exe": attr.label(
340 executable = True,
341 cfg = "host",
342 allow_files = True,
Thomas Vachuska22ea6122018-10-24 12:28:59 -0700343 default = Label("//tools/build/swagger:swagger_generator"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700344 ),
345 "swagger_json": attr.output(),
346 },
347 fragments = ["java"],
348 implementation = _swagger_json_impl,
349)
350
351"""
352 Converts a jar file to an OSGI compatible jar file.
353
354 Args:
355 name: name of the rule to create the OSGI jar file - required
356 jar: jar file to convert - required target
357 deps: dependencies needed by the jar file - required list of targets
358 version: Version of the generated jar file. Optional, defaults to the current ONOS version
359 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
360 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
361 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to private
362"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700363
Ray Milkey25747d82018-06-13 14:12:51 -0700364def wrapped_osgi_jar(
365 name,
366 jar,
367 deps,
368 version = ONOS_VERSION,
Ray Milkey275af2c2018-06-15 13:05:08 -0700369 group = "org.onosproject",
Ray Milkey25747d82018-06-13 14:12:51 -0700370 import_packages = "*",
Carmelo Casconec0b23a42019-04-25 13:50:54 -0700371 visibility = ["//visibility:private"],
Daniele Moro27527592020-02-03 23:33:55 -0800372 generate_pom = False,
373 fragment_host = ""):
Ray Milkey25747d82018-06-13 14:12:51 -0700374 _bnd(
375 name = name,
376 source = jar,
377 deps = deps,
378 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700379 group = group,
Ray Milkey25747d82018-06-13 14:12:51 -0700380 visibility = visibility,
381 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700382 web_xml = None,
Daniele Moro27527592020-02-03 23:33:55 -0800383 fragment_host = fragment_host,
Ray Milkey25747d82018-06-13 14:12:51 -0700384 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700385
Carmelo Casconec0b23a42019-04-25 13:50:54 -0700386 if generate_pom:
387 pom_file(
388 name = name + "-pom",
389 artifact = name,
390 deps = deps,
391 visibility = visibility,
392 )
Ray Milkey24ba24a2019-04-26 11:53:02 -0700393 minimal_jar(name = name + "-sources", visibility = visibility)
394 minimal_jar(name = name + "-javadoc", visibility = visibility)
Carmelo Casconec0b23a42019-04-25 13:50:54 -0700395
Ray Milkey275af2c2018-06-15 13:05:08 -0700396"""
397 Creates an OSGI jar and test jar file from a set of source and test files.
398 See osgi_jar() for a description of shared parameters.
399 Args:
400 test_srcs: Test source file(s) to compile. Optional list of targets, defaults to src/test/java/**/*.java
401 test_deps: Dependencies for the test jar. Optional list of targets, defaults to a common set of dependencies
402 test_resources: Resources to include in the test jar. Optional list of targets, defaults to src/test/resources/**
403 exclude_tests: Tests that should not be run. Useful for excluding things like test files without any @Test methods.
404 Optional ist of targets, defaults to []
405"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700406
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700407def osgi_jar_with_tests(
408 name = None,
409 deps = None,
410 test_deps = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700411 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700412 srcs = None,
413 resources_root = None,
414 resources = None,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700415 resource_jars = [],
Ray Milkey6b3775a2018-06-28 11:18:44 -0700416 include_resources = {},
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700417 test_srcs = None,
418 exclude_tests = None,
Thomas Vachuskaa79bf6e2018-08-07 11:24:40 -0700419 medium_tests = [],
420 large_tests = [],
421 enormous_tests = [],
422 flaky_tests = [],
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700423 test_resources = None,
424 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700425 version = ONOS_VERSION,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700426 suppress_errorprone = False,
Ray Milkey134d2922018-07-15 15:24:01 -0700427 suppress_checkstyle = False,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700428 suppress_javadocs = False,
Ray Milkey25747d82018-06-13 14:12:51 -0700429 web_context = None,
430 api_title = "",
431 api_version = "",
432 api_description = "",
433 api_package = "",
Jian Li4ad86872018-08-05 03:34:35 +0900434 import_packages = None,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700435 bundle_classpath = "",
436 karaf_command_packages = []):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700437 if name == None:
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700438 name = _auto_name()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700439 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700440 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700441 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700442 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700443 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700444 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700445 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700446 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700447 if exclude_tests == None:
448 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700449 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700450 deps = COMPILE
451 if test_deps == None:
452 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700453 if import_packages == None:
454 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700455 tests_name = name + "-tests"
Carmelo Cascone21eb0422019-06-17 12:00:33 -0700456 tests_jar_deps = depset(deps + test_deps).to_list() + [name]
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700457 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey25747d82018-06-13 14:12:51 -0700458 web_xml = _webapp()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700459
Ray Milkey275af2c2018-06-15 13:05:08 -0700460 native_srcs = srcs
461 native_resources = resources
Thomas Vachuska24d4f6d2018-10-19 11:25:04 -0700462
Andrea Campanella4005c062019-01-17 16:05:03 +0100463 if web_context != None and api_title != "":
Ray Milkey275af2c2018-06-15 13:05:08 -0700464 # generate Swagger files if needed
465 _swagger_java(
466 name = name + "_swagger_java",
467 srcs = srcs + resources,
468 package_name = native.package_name(),
469 api_title = api_title,
470 api_version = api_version,
471 api_description = api_description,
472 web_context = web_context,
473 api_package = api_package,
474 swagger_java = ("src/main/resources/apidoc/generated-sources/" +
Ray Milkey6b3775a2018-06-28 11:18:44 -0700475 api_package.replace(".", "/") +
476 "/ApiDocRegistrator.java").replace("//", "/"),
Ray Milkey275af2c2018-06-15 13:05:08 -0700477 )
478 _swagger_json(
479 name = name + "_swagger_json",
480 srcs = srcs + resources,
481 package_name = native.package_name(),
482 api_title = api_title,
483 api_version = api_version,
484 api_description = api_description,
485 web_context = web_context,
486 api_package = api_package,
487 swagger_json = "src/main/resources/apidoc/swagger.json",
488 )
489 native_resources = []
490 for r in resources:
Ray Milkey6b3775a2018-06-28 11:18:44 -0700491 if not "definitions" in r:
Ray Milkey275af2c2018-06-15 13:05:08 -0700492 native_resources.append(r)
Ray Milkey6b3775a2018-06-28 11:18:44 -0700493 native_srcs = srcs + [name + "_swagger_java"]
494 native_resources.append(name + "_swagger_json")
Ray Milkey275af2c2018-06-15 13:05:08 -0700495
Ray Milkey5063f5b2018-08-15 16:22:30 -0700496 javacopts = ["-XepDisableAllChecks"] if suppress_errorprone else []
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700497
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700498 _cfgdef(
499 name = name + "_cfgdef_jar",
500 srcs = native_srcs,
501 visibility = visibility,
502 cfgdef_jar = name + "_cfgdef.jar",
503 )
504
Ray Milkey25747d82018-06-13 14:12:51 -0700505 # compile the Java code
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700506 if len(resource_jars) > 0:
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 = resource_jars + [name + "_cfgdef_jar"],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700511 deps = deps,
512 visibility = visibility,
513 javacopts = javacopts,
514 )
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700515 else:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700516 native.java_library(
517 name = name + "-native",
518 srcs = native_srcs,
Thomas Vachuskabef430b2018-10-22 10:55:47 -0700519 resource_jars = [name + "_cfgdef_jar"],
Ray Milkey5063f5b2018-08-15 16:22:30 -0700520 resources = native_resources,
521 deps = deps,
522 visibility = visibility,
523 javacopts = javacopts,
524 )
Ray Milkey25747d82018-06-13 14:12:51 -0700525
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700526 karaf_command_packages_string = ",".join(karaf_command_packages)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700527 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700528 name = name,
529 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700530 deps = deps,
531 version = version,
Ray Milkey275af2c2018-06-15 13:05:08 -0700532 group = group,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700533 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700534 import_packages = import_packages,
Jian Li4ad86872018-08-05 03:34:35 +0900535 bundle_classpath = bundle_classpath,
Ray Milkey25747d82018-06-13 14:12:51 -0700536 web_context = web_context,
537 web_xml = web_xml,
Ray Milkey6b3775a2018-06-28 11:18:44 -0700538 include_resources = _include_resources_to_string(include_resources),
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700539 karaf_commands = karaf_command_packages_string,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700540 )
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700541
Thomas Vachuskaac9e5242018-07-19 16:15:39 -0700542 # rule for generating pom file for publishing
543 pom_file(name = name + "-pom", artifact = name, deps = deps, visibility = visibility)
544
545 # rule for building source jar
Thomas Vachuska50ac0982018-07-19 10:17:37 -0700546 if not suppress_javadocs:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700547 java_sources(name = name + "-sources", srcs = srcs, visibility = visibility)
Thomas Vachuska50ac0982018-07-19 10:17:37 -0700548
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700549 # rule for building javadocs
550 if not suppress_javadocs:
Ray Milkey5063f5b2018-08-15 16:22:30 -0700551 javadoc(name = name + "-javadoc", deps = deps, srcs = srcs, visibility = visibility)
Ray Milkey522a3bd2019-04-25 17:32:57 -0700552 else:
Ray Milkey24ba24a2019-04-26 11:53:02 -0700553 minimal_jar(name = name + "-javadoc", visibility = visibility)
554 minimal_jar(name = name + "-sources", visibility = visibility)
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700555
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700556 if test_srcs != []:
557 native.java_library(
558 name = tests_name,
559 srcs = test_srcs,
560 resources = test_resources,
561 deps = tests_jar_deps,
562 visibility = visibility,
563 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700564
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700565 generate_test_rules(
566 name = name + "-tests-gen",
567 test_files = test_srcs,
568 exclude_tests = exclude_tests,
Thomas Vachuskaa79bf6e2018-08-07 11:24:40 -0700569 medium_tests = medium_tests,
570 large_tests = large_tests,
571 enormous_tests = enormous_tests,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700572 deps = all_test_deps,
573 )
574
Ray Milkey134d2922018-07-15 15:24:01 -0700575 if not suppress_checkstyle:
576 checkstyle_test(
577 name = name + "_checkstyle_test",
578 srcs = srcs,
579 )
Ray Milkeyb64fe822018-08-30 09:18:16 -0700580 if test_srcs != []:
581 checkstyle_test(
582 name = name + "_checkstyle_tests_test",
583 srcs = test_srcs,
584 )
Ray Milkeyb7949e72018-06-19 18:31:02 -0700585
Ray Milkey275af2c2018-06-15 13:05:08 -0700586"""
587 Creates an OSGI jar file from a set of source files.
588
589 Args:
590 name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree.
591 For example apps/mcast/app becomes onos-apps-mcast-app
592 deps: Dependencies of the generated jar file. Expressed as a list of targets
593 import_packages: OSGI import list. Optional, comma separated list, defaults to "*"
Jian Li4ad86872018-08-05 03:34:35 +0900594 bundle_classpath: intended for including dependencies in our bundle, so that our bundle can be deployed standalone
Ray Milkey275af2c2018-06-15 13:05:08 -0700595 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
596 srcs: Source file(s) to compile. Optional list of targets, defaults to src/main/java/**/*.java
597 resources_root: Relative path to the root of the tree of resources for this jar. Optional, defaults to src/main/resources
598 resources: Resources to include in the jar file. Optional list of targets, defaults to all files beneath resources_root
599 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public
600 version: Version of the generated jar file. Optional, defaults to the current ONOS version
Ray Milkey134d2922018-07-15 15:24:01 -0700601 suppress_errorprone: If true, don't run ErrorProne tests. Default is false
602 suppress_checkstyle: If true, don't run checkstyle tests. Default is false
Ray Milkey275af2c2018-06-15 13:05:08 -0700603 web_context: Web context for a WAB file if needed. Only needed if the jar file provides a REST API. Optional string
604 api_title: Swagger API title. Optional string, only used if the jar file provides a REST API and has swagger annotations
605 api_version: Swagger API version. Optional string, only used if the jar file provides a REST API and has swagger annotations
606 api_description: Swagger API description. Optional string, only used if the jar file provides a REST API and has swagger annotations
607 api_package: Swagger API package name. Optional string, only used if the jar file provides a REST API and has swagger annotations
608"""
Ray Milkey6b3775a2018-06-28 11:18:44 -0700609
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700610def osgi_jar(
611 name = None,
612 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700613 import_packages = None,
Ray Milkey275af2c2018-06-15 13:05:08 -0700614 group = "org.onosproject",
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700615 srcs = None,
616 resources_root = None,
617 resources = None,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700618 resource_jars = [],
Ray Milkey6b3775a2018-06-28 11:18:44 -0700619 include_resources = {},
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700620 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700621 version = ONOS_VERSION,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700622 suppress_errorprone = False,
Ray Milkey134d2922018-07-15 15:24:01 -0700623 suppress_checkstyle = False,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700624 suppress_javadocs = False,
Ray Milkey25747d82018-06-13 14:12:51 -0700625 web_context = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700626 api_title = "",
627 api_version = "",
628 api_description = "",
Jian Li4ad86872018-08-05 03:34:35 +0900629 api_package = "",
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700630 bundle_classpath = "",
631 karaf_command_packages = []):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700632 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700633 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700634 if deps == None:
635 deps = COMPILE
636
637 osgi_jar_with_tests(
638 name = name,
639 deps = deps,
640 test_deps = [],
Ray Milkey275af2c2018-06-15 13:05:08 -0700641 group = group,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700642 srcs = srcs,
643 resources = resources,
644 resources_root = resources_root,
Thomas Vachuskaf8c8cb92018-07-11 17:12:43 -0700645 resource_jars = resource_jars,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700646 test_srcs = [],
647 exclude_tests = [],
648 test_resources = [],
649 visibility = visibility,
Thomas Vachuska5b9ff6a2018-07-13 11:00:50 -0700650 suppress_errorprone = suppress_errorprone,
Ray Milkey134d2922018-07-15 15:24:01 -0700651 suppress_checkstyle = suppress_checkstyle,
Thomas Vachuska0f7d7a42018-07-18 15:23:40 -0700652 suppress_javadocs = suppress_javadocs,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700653 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700654 import_packages = import_packages,
Ray Milkey275af2c2018-06-15 13:05:08 -0700655 api_title = api_title,
656 api_version = api_version,
657 api_description = api_description,
658 api_package = api_package,
Ray Milkey25747d82018-06-13 14:12:51 -0700659 web_context = web_context,
Jian Li4ad86872018-08-05 03:34:35 +0900660 bundle_classpath = bundle_classpath,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700661 karaf_command_packages = karaf_command_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700662 )
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700663
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700664"""
665 Creates an OSGI jar file from a set of protobuf and gRPC libraries.
666
667 Args:
668 name: Name of the rule to generate. Optional, defaults to a name based on the location in the source tree.
669 For example apps/mcast/app becomes onos-apps-mcast-app
670 proto_libs: (required) list of proto_library targets which generated Java classes will be included to this OSGi
671 jar. It is important that all the given targets reference to a single proto source files, for example
672 only the first 2 rules are good:
673
674 proto_library(
675 name = "foo_proto",
676 srcs = ["foo.proto"],
677 )
678
679 proto_library(
680 name = "bar_proto",
681 srcs = ["bar.proto"],
682 )
683
684 # THIS WILL NOT WORK
685 proto_library(
686 name = "foo_and_bar_proto",
687 srcs = ["foo.proto", "bar.proto"],
688 )
689
690 grpc_proto_lib: (optional) proto_library target that contains the schema of a gRPC service. If not passed,
691 the produced jar will NOT have any gRPC stub classes.
692 deps: Dependencies of the generated jar file. Expressed as a list of targets
693 group: Maven group ID for the resulting jar file. Optional, defaults to 'org.onosproject'
694 visibility: Visibility of the produced jar file to other BUILDs. Optional, defaults to public
695 version: Version of the generated jar file. Optional, defaults to the current ONOS version
696"""
697
698def osgi_proto_jar(
699 proto_libs,
700 grpc_proto_lib = None,
701 name = None,
702 deps = [],
703 group = "org.onosproject",
704 visibility = ["//visibility:public"],
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700705 version = ONOS_VERSION,
706 karaf_command_packages = []):
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700707 if name == None:
708 name = _auto_name()
Carmelo Casconeb9536692019-05-28 18:15:23 -0700709 proto_name = name + "-java-proto"
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700710 native.java_proto_library(
Carmelo Casconeb9536692019-05-28 18:15:23 -0700711 name = proto_name,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700712 deps = proto_libs,
713 )
714 java_sources_alt(
Carmelo Casconeb9536692019-05-28 18:15:23 -0700715 name = proto_name + "-srcjar",
716 srcs = [":" + proto_name],
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700717 )
718 osgi_srcs = [
Carmelo Casconeb9536692019-05-28 18:15:23 -0700719 proto_name + "-srcjar",
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700720 ]
721 base_deps = [
Carmelo Casconed33d3b42019-06-18 12:12:36 -0700722 "//deps:com_google_protobuf_protobuf_java",
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700723 ]
724 if grpc_proto_lib != None:
Carmelo Casconeb9536692019-05-28 18:15:23 -0700725 grpc_name = name + "-java-grpc"
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700726 java_grpc_library(
Carmelo Casconeb9536692019-05-28 18:15:23 -0700727 name = grpc_name,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700728 srcs = [grpc_proto_lib],
Carmelo Casconeb9536692019-05-28 18:15:23 -0700729 deps = [":" + proto_name],
730 )
731 java_sources_alt(
732 name = grpc_name + "-srcjar",
733 srcs = [":lib%s-src.jar" % grpc_name],
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700734 )
735 osgi_srcs.append(
Carmelo Casconeb9536692019-05-28 18:15:23 -0700736 ":" + grpc_name + "-srcjar",
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700737 )
738 base_deps.extend([
739 "@com_google_guava_guava//jar",
Carmelo Casconed33d3b42019-06-18 12:12:36 -0700740 "//deps:io_grpc_grpc_api_context",
741 "//deps:io_grpc_grpc_stub",
742 "//deps:io_grpc_grpc_protobuf",
Carmelo Casconeb9536692019-05-28 18:15:23 -0700743 "@javax_annotation_javax_annotation_api//jar",
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700744 ])
745 osgi_jar(
746 name = name,
747 srcs = osgi_srcs,
748 deps = base_deps + deps,
749 group = group,
750 visibility = visibility,
751 version = version,
752 suppress_errorprone = True,
753 suppress_checkstyle = True,
754 suppress_javadocs = True,
Ray Milkeyd84f89b2018-08-17 14:54:17 -0700755 karaf_command_packages = karaf_command_packages,
Carmelo Cascone6a1ae712018-08-10 12:19:47 -0700756 )