blob: c4909609cbbb29e4387aead64a21c5caa5ce383f [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")
20
Ray Milkey32ea35c2018-06-06 15:28:07 -070021def _all_java_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070022 return native.glob(["src/main/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070023
Ray Milkey32ea35c2018-06-06 15:28:07 -070024def _all_java_test_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070025 return native.glob(["src/test/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070026
Ray Milkey32ea35c2018-06-06 15:28:07 -070027def _all_test_resources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070028 return native.glob(["src/test/resources/**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070029
Ray Milkey32ea35c2018-06-06 15:28:07 -070030def _all_resources(resources_root):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070031 if resources_root == None:
32 return native.glob(["src/main/resources/**"])
33 else:
34 return native.glob([resources_root + "**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070035
Ray Milkey25747d82018-06-13 14:12:51 -070036def _webapp():
37 return native.glob(["src/main/webapp/**"])
38
Ray Milkey7dac7da2017-08-01 16:56:05 -070039# Implementation of the rule to call bnd to make an OSGI jar file
40def _bnd_impl(ctx):
Ray Milkey3275ae82018-05-29 15:35:36 -070041 if (len(ctx.files.source) == 1):
42 input_file = ctx.files.source[0]
43 else:
44 # this is a list of inputs. The one we want is the last one
45 # in the list that isn't a source jar
46 for file in reversed(ctx.files.source):
47 if ("-src" in file.path):
48 continue
49 else:
50 input_file = file
51 break
52
53 jar = input_file.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070054 output = ctx.outputs.osgi_jar.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070055 name = ctx.attr.source.label.name
56 group = ctx.attr.package_name_root
57 version = ctx.attr.version
58 license = ""
Ray Milkey12ae6ca2018-06-11 15:34:30 -070059 import_packages = ctx.attr.import_packages
Ray Milkey0bcdfd12018-05-23 14:07:19 -070060 exportPackages = "*"
61 includeResources = ""
Ray Milkey25747d82018-06-13 14:12:51 -070062 web_context = ctx.attr.web_context
63 if web_context == None or web_context == "":
64 web_context = "NONE"
65 web_xml = ctx.attr.web_xml
Ray Milkey0bcdfd12018-05-23 14:07:19 -070066 dynamicimportPackages = ""
Ray Milkey3275ae82018-05-29 15:35:36 -070067 cp = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070068
Ray Milkey3275ae82018-05-29 15:35:36 -070069 inputDependencies = [input_file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070070
Ray Milkey0bcdfd12018-05-23 14:07:19 -070071 # determine the dependencies and build the class path
72 for dep in ctx.attr.deps:
Ray Milkey25b785a2018-06-12 09:59:14 -070073 if java_common.provider in dep:
74 file = dep.files.to_list()[0]
Ray Milkey472d8392018-05-23 17:06:51 -070075
Ray Milkey25b785a2018-06-12 09:59:14 -070076 if cp:
77 cp += ":"
78 cp += file.path
79 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070080
Ray Milkey0bcdfd12018-05-23 14:07:19 -070081 # extract the class files for use by bnd
Ray Milkey3275ae82018-05-29 15:35:36 -070082 classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-"))
Ray Milkey0bcdfd12018-05-23 14:07:19 -070083 classesPath = classes.path
84 jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
85 ctx.actions.run_shell(
86 inputs = inputDependencies,
87 outputs = [classes],
88 command = jarCommand,
89 progress_message = "Expanding jar file: %s" % jar,
90 )
91 inputDependencies += [classes]
Ray Milkey25747d82018-06-13 14:12:51 -070092 web_xml_root_path = ""
93 if len(web_xml) != 0:
94 web_xml_root = web_xml[0].files.to_list()[0]
95 inputDependencies += [web_xml_root]
96 web_xml_root_path = web_xml_root.path.replace("WEB-INF/web.xml", "")
Ray Milkey7dac7da2017-08-01 16:56:05 -070097
Ray Milkey0bcdfd12018-05-23 14:07:19 -070098 # call bnd to make the OSGI jar file
99 arguments = [
100 jar,
101 output,
102 cp,
103 name,
104 group,
105 version,
106 license,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700107 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700108 exportPackages,
109 includeResources,
Ray Milkey25747d82018-06-13 14:12:51 -0700110 web_context,
111 web_xml_root_path,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700112 dynamicimportPackages,
113 classesPath,
114 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700115
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700116 ctx.actions.run(
117 inputs = inputDependencies,
118 outputs = [ctx.outputs.osgi_jar],
119 arguments = arguments,
120 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
121 executable = ctx.executable._bnd_exe,
122 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700123
Ray Milkey25b785a2018-06-12 09:59:14 -0700124 deps = []
125 if java_common.provider in ctx.attr.source:
126 deps.append(ctx.attr.source[java_common.provider])
127 deps_provider = java_common.merge(deps)
128 return struct(
Ray Milkey15053f02018-06-13 10:00:45 -0700129 providers = [deps_provider],
Ray Milkey25b785a2018-06-12 09:59:14 -0700130 )
131
Ray Milkey32ea35c2018-06-06 15:28:07 -0700132_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700133 attrs = {
134 "deps": attr.label_list(),
135 "version": attr.string(),
136 "package_name_root": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700137 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700138 "import_packages": attr.string(),
Ray Milkey25747d82018-06-13 14:12:51 -0700139 "web_context": attr.string(),
140 "web_xml": attr.label_list(allow_files = True),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700141 "_bnd_exe": attr.label(
142 executable = True,
143 cfg = "host",
144 allow_files = True,
145 default = Label("//utils/osgiwrap:osgi-jar"),
146 ),
147 },
148 fragments = ["java"],
149 outputs = {
150 "osgi_jar": "lib%{name}.jar",
151 },
152 implementation = _bnd_impl,
153)
154
Ray Milkey25747d82018-06-13 14:12:51 -0700155def wrapped_osgi_jar(
156 name,
157 jar,
158 deps,
159 version = ONOS_VERSION,
160 package_name_root = "org.onosproject",
161 import_packages = "*",
162 web_context = None,
163 web_xml = None,
164 visibility = ["//visibility:private"]):
165 _bnd(
166 name = name,
167 source = jar,
168 deps = deps,
169 version = version,
170 package_name_root = package_name_root,
171 visibility = visibility,
172 import_packages = import_packages,
173 web_xml = web_xml,
174 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700175
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700176def osgi_jar_with_tests(
177 name = None,
178 deps = None,
179 test_deps = None,
180 package_name_root = "org.onosproject",
181 srcs = None,
182 resources_root = None,
183 resources = None,
184 test_srcs = None,
185 exclude_tests = None,
186 test_resources = None,
187 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700188 version = ONOS_VERSION,
Ray Milkey25747d82018-06-13 14:12:51 -0700189 web_context = None,
190 api_title = "",
191 api_version = "",
192 api_description = "",
193 api_package = "",
Ray Milkey15053f02018-06-13 10:00:45 -0700194 import_packages = None):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700195 if name == None:
196 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700197 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700198 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700199 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700200 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700201 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700202 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700203 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700204 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700205 if exclude_tests == None:
206 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700207 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700208 deps = COMPILE
209 if test_deps == None:
210 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700211 if import_packages == None:
212 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700213 tests_name = name + "-tests"
214 tests_jar_deps = list(depset(deps + test_deps)) + [name]
215 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey25747d82018-06-13 14:12:51 -0700216 web_xml = _webapp()
Ray Milkey7dac7da2017-08-01 16:56:05 -0700217
Ray Milkey25747d82018-06-13 14:12:51 -0700218 # compile the Java code
Ray Milkey25b785a2018-06-12 09:59:14 -0700219 native.java_library(name = name + "-native", srcs = srcs, resources = resources, deps = deps, visibility = visibility)
Ray Milkey25747d82018-06-13 14:12:51 -0700220
Ray Milkey32ea35c2018-06-06 15:28:07 -0700221 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700222 name = name,
223 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700224 deps = deps,
225 version = version,
226 package_name_root = package_name_root,
227 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700228 import_packages = import_packages,
Ray Milkey25747d82018-06-13 14:12:51 -0700229 web_context = web_context,
230 web_xml = web_xml,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700231 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700232 if test_srcs != []:
233 native.java_library(
234 name = tests_name,
235 srcs = test_srcs,
236 resources = test_resources,
237 deps = tests_jar_deps,
238 visibility = visibility,
239 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700240
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700241 generate_test_rules(
242 name = name + "-tests-gen",
243 test_files = test_srcs,
244 exclude_tests = exclude_tests,
245 deps = all_test_deps,
246 )
247
248def osgi_jar(
249 name = None,
250 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700251 import_packages = None,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700252 package_name_root = "org.onosproject",
253 srcs = None,
254 resources_root = None,
255 resources = None,
256 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700257 version = ONOS_VERSION,
258 # TODO - implement these for swagger and web.xml
Ray Milkey25747d82018-06-13 14:12:51 -0700259 web_context = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700260 api_title = "",
261 api_version = "",
262 api_description = "",
Ray Milkey15053f02018-06-13 10:00:45 -0700263 api_package = ""):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700264 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700265 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700266 if deps == None:
267 deps = COMPILE
268
269 osgi_jar_with_tests(
270 name = name,
271 deps = deps,
272 test_deps = [],
273 package_name_root = package_name_root,
274 srcs = srcs,
275 resources = resources,
276 resources_root = resources_root,
277 test_srcs = [],
278 exclude_tests = [],
279 test_resources = [],
280 visibility = visibility,
281 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700282 import_packages = import_packages,
Ray Milkey25747d82018-06-13 14:12:51 -0700283 web_context = web_context,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700284 )