blob: 6c8a381e9f4311ffd82f5b42edd490de98ac9d1e [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(
Ray Milkey15053f02018-06-13 10:00:45 -0700117 providers = [deps_provider],
Ray Milkey25b785a2018-06-12 09:59:14 -0700118 )
119
Ray Milkey32ea35c2018-06-06 15:28:07 -0700120_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700121 attrs = {
122 "deps": attr.label_list(),
123 "version": attr.string(),
124 "package_name_root": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700125 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700126 "import_packages": attr.string(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700127 "_bnd_exe": attr.label(
128 executable = True,
129 cfg = "host",
130 allow_files = True,
131 default = Label("//utils/osgiwrap:osgi-jar"),
132 ),
133 },
134 fragments = ["java"],
135 outputs = {
136 "osgi_jar": "lib%{name}.jar",
137 },
138 implementation = _bnd_impl,
139)
140
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700141def wrapped_osgi_jar(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", import_packages = "*", visibility = ["//visibility:private"]):
142 _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 -0700143
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700144def osgi_jar_with_tests(
145 name = None,
146 deps = None,
147 test_deps = None,
148 package_name_root = "org.onosproject",
149 srcs = None,
150 resources_root = None,
151 resources = None,
152 test_srcs = None,
153 exclude_tests = None,
154 test_resources = None,
155 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700156 version = ONOS_VERSION,
Ray Milkey15053f02018-06-13 10:00:45 -0700157 import_packages = None):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700158 if name == None:
159 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700160 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700161 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700162 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700163 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700164 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700165 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700166 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700167 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700168 if exclude_tests == None:
169 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700170 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700171 deps = COMPILE
172 if test_deps == None:
173 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700174 if import_packages == None:
175 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700176 tests_name = name + "-tests"
177 tests_jar_deps = list(depset(deps + test_deps)) + [name]
178 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700179
Ray Milkey25b785a2018-06-12 09:59:14 -0700180 native.java_library(name = name + "-native", srcs = srcs, resources = resources, deps = deps, visibility = visibility)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700181 _bnd(
Ray Milkey25b785a2018-06-12 09:59:14 -0700182 name = name,
183 source = name + "-native",
Ray Milkey32ea35c2018-06-06 15:28:07 -0700184 deps = deps,
185 version = version,
186 package_name_root = package_name_root,
187 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700188 import_packages = import_packages,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700189 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700190 if test_srcs != []:
191 native.java_library(
192 name = tests_name,
193 srcs = test_srcs,
194 resources = test_resources,
195 deps = tests_jar_deps,
196 visibility = visibility,
197 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700198
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700199 generate_test_rules(
200 name = name + "-tests-gen",
201 test_files = test_srcs,
202 exclude_tests = exclude_tests,
203 deps = all_test_deps,
204 )
205
206def osgi_jar(
207 name = None,
208 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700209 import_packages = None,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700210 package_name_root = "org.onosproject",
211 srcs = None,
212 resources_root = None,
213 resources = None,
214 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700215 version = ONOS_VERSION,
216 # TODO - implement these for swagger and web.xml
217 web_context = "",
218 api_title = "",
219 api_version = "",
220 api_description = "",
Ray Milkey15053f02018-06-13 10:00:45 -0700221 api_package = ""):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700222 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700223 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700224 if deps == None:
225 deps = COMPILE
226
227 osgi_jar_with_tests(
228 name = name,
229 deps = deps,
230 test_deps = [],
231 package_name_root = package_name_root,
232 srcs = srcs,
233 resources = resources,
234 resources_root = resources_root,
235 test_srcs = [],
236 exclude_tests = [],
237 test_resources = [],
238 visibility = visibility,
239 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700240 import_packages = import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700241 )