blob: 77d791b1cb89df71506ed54c19ef70132afa9814 [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
36# Implementation of the rule to call bnd to make an OSGI jar file
37def _bnd_impl(ctx):
Ray Milkey3275ae82018-05-29 15:35:36 -070038 if (len(ctx.files.source) == 1):
39 input_file = ctx.files.source[0]
40 else:
41 # this is a list of inputs. The one we want is the last one
42 # in the list that isn't a source jar
43 for file in reversed(ctx.files.source):
44 if ("-src" in file.path):
45 continue
46 else:
47 input_file = file
48 break
49
50 jar = input_file.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070051 output = ctx.outputs.osgi_jar.path
Ray Milkey0bcdfd12018-05-23 14:07:19 -070052 name = ctx.attr.source.label.name
53 group = ctx.attr.package_name_root
54 version = ctx.attr.version
55 license = ""
Ray Milkey12ae6ca2018-06-11 15:34:30 -070056 import_packages = ctx.attr.import_packages
Ray Milkey0bcdfd12018-05-23 14:07:19 -070057 exportPackages = "*"
58 includeResources = ""
59 webContext = "NONE"
60 dynamicimportPackages = ""
Ray Milkey3275ae82018-05-29 15:35:36 -070061 cp = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070062
Ray Milkey3275ae82018-05-29 15:35:36 -070063 inputDependencies = [input_file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070064
Ray Milkey0bcdfd12018-05-23 14:07:19 -070065 # determine the dependencies and build the class path
66 for dep in ctx.attr.deps:
Ray Milkey25b785a2018-06-12 09:59:14 -070067 if java_common.provider in dep:
68 file = dep.files.to_list()[0]
Ray Milkey472d8392018-05-23 17:06:51 -070069
Ray Milkey25b785a2018-06-12 09:59:14 -070070 if cp:
71 cp += ":"
72 cp += file.path
73 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070074
Ray Milkey0bcdfd12018-05-23 14:07:19 -070075 # extract the class files for use by bnd
Ray Milkey3275ae82018-05-29 15:35:36 -070076 classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-"))
Ray Milkey0bcdfd12018-05-23 14:07:19 -070077 classesPath = classes.path
78 jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
79 ctx.actions.run_shell(
80 inputs = inputDependencies,
81 outputs = [classes],
82 command = jarCommand,
83 progress_message = "Expanding jar file: %s" % jar,
84 )
85 inputDependencies += [classes]
Ray Milkey7dac7da2017-08-01 16:56:05 -070086
Ray Milkey0bcdfd12018-05-23 14:07:19 -070087 # call bnd to make the OSGI jar file
88 arguments = [
89 jar,
90 output,
91 cp,
92 name,
93 group,
94 version,
95 license,
Ray Milkey12ae6ca2018-06-11 15:34:30 -070096 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -070097 exportPackages,
98 includeResources,
99 webContext,
100 dynamicimportPackages,
101 classesPath,
102 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700103
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700104 ctx.actions.run(
105 inputs = inputDependencies,
106 outputs = [ctx.outputs.osgi_jar],
107 arguments = arguments,
108 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
109 executable = ctx.executable._bnd_exe,
110 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700111
Ray Milkey25b785a2018-06-12 09:59:14 -0700112 deps = []
113 if java_common.provider in ctx.attr.source:
114 deps.append(ctx.attr.source[java_common.provider])
115 deps_provider = java_common.merge(deps)
116 return struct(
117 providers = [deps_provider]
118 )
119
120
Ray Milkey32ea35c2018-06-06 15:28:07 -0700121_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700122 attrs = {
123 "deps": attr.label_list(),
124 "version": attr.string(),
125 "package_name_root": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700126 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700127 "import_packages": attr.string(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700128 "_bnd_exe": attr.label(
129 executable = True,
130 cfg = "host",
131 allow_files = True,
132 default = Label("//utils/osgiwrap:osgi-jar"),
133 ),
134 },
135 fragments = ["java"],
136 outputs = {
137 "osgi_jar": "lib%{name}.jar",
138 },
139 implementation = _bnd_impl,
140)
141
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700142def wrapped_osgi_jar(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", import_packages = "*", visibility = ["//visibility:private"]):
143 _bnd(name = name, source = jar, deps = deps, version = version, package_name_root = package_name_root, visibility = visibility, import_packages = import_packages)
Ray Milkey7dac7da2017-08-01 16:56:05 -0700144
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700145def osgi_jar_with_tests(
146 name = None,
147 deps = None,
148 test_deps = None,
149 package_name_root = "org.onosproject",
150 srcs = None,
151 resources_root = None,
152 resources = None,
153 test_srcs = None,
154 exclude_tests = None,
155 test_resources = None,
156 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700157 version = ONOS_VERSION,
158 import_packages = None,
159 ):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700160 if name == None:
161 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700162 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700163 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700164 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700165 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700166 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700167 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700168 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700169 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700170 if exclude_tests == None:
171 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700172 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700173 deps = COMPILE
174 if test_deps == None:
175 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700176 if import_packages == None:
177 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700178 tests_name = name + "-tests"
179 tests_jar_deps = list(depset(deps + test_deps)) + [name]
180 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700181
Ray Milkey25b785a2018-06-12 09:59:14 -0700182 native.java_library(name = name + "-native", srcs = srcs, resources = resources, deps = deps, visibility = visibility)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700183 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700184 name = name,
185 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700186 deps = deps,
187 version = version,
188 package_name_root = package_name_root,
189 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700190 import_packages = import_packages,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700191 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700192 if test_srcs != []:
193 native.java_library(
194 name = tests_name,
195 srcs = test_srcs,
196 resources = test_resources,
197 deps = tests_jar_deps,
198 visibility = visibility,
199 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700200
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700201 generate_test_rules(
202 name = name + "-tests-gen",
203 test_files = test_srcs,
204 exclude_tests = exclude_tests,
205 deps = all_test_deps,
206 )
207
208def osgi_jar(
209 name = None,
210 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700211 import_packages = None,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700212 package_name_root = "org.onosproject",
213 srcs = None,
214 resources_root = None,
215 resources = None,
216 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700217 version = ONOS_VERSION,
218 # TODO - implement these for swagger and web.xml
219 web_context = "",
220 api_title = "",
221 api_version = "",
222 api_description = "",
223 api_package = "",
224 ):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700225 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700226 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700227 if deps == None:
228 deps = COMPILE
229
230 osgi_jar_with_tests(
231 name = name,
232 deps = deps,
233 test_deps = [],
234 package_name_root = package_name_root,
235 srcs = srcs,
236 resources = resources,
237 resources_root = resources_root,
238 test_srcs = [],
239 exclude_tests = [],
240 test_resources = [],
241 visibility = visibility,
242 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700243 import_packages = import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700244 )