blob: 274ed253d4f0166a511f7040f956ab263c2cb5bc [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
21def 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
24def 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
27def 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
30def 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 = ""
56 importPackages = "*"
57 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 Milkey472d8392018-05-23 17:06:51 -070067 if len(dep.java.outputs.jars) == 0:
68 continue
69
Ray Milkey0bcdfd12018-05-23 14:07:19 -070070 file = dep.java.outputs.jars[0].class_jar
Ray Milkey7dac7da2017-08-01 16:56:05 -070071
Ray Milkey0bcdfd12018-05-23 14:07:19 -070072 if cp:
73 cp += ":"
74 cp += file.path
75 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070076
Ray Milkey0bcdfd12018-05-23 14:07:19 -070077 # extract the class files for use by bnd
Ray Milkey3275ae82018-05-29 15:35:36 -070078 classes = ctx.actions.declare_file("classes" + ctx.label.name.replace("/", "-"))
Ray Milkey0bcdfd12018-05-23 14:07:19 -070079 classesPath = classes.path
80 jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
81 ctx.actions.run_shell(
82 inputs = inputDependencies,
83 outputs = [classes],
84 command = jarCommand,
85 progress_message = "Expanding jar file: %s" % jar,
86 )
87 inputDependencies += [classes]
Ray Milkey7dac7da2017-08-01 16:56:05 -070088
Ray Milkey0bcdfd12018-05-23 14:07:19 -070089 # call bnd to make the OSGI jar file
90 arguments = [
91 jar,
92 output,
93 cp,
94 name,
95 group,
96 version,
97 license,
98 importPackages,
99 exportPackages,
100 includeResources,
101 webContext,
102 dynamicimportPackages,
103 classesPath,
104 ]
105 ctx.actions.run(
106 inputs = inputDependencies,
107 outputs = [ctx.outputs.osgi_jar],
108 arguments = arguments,
109 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
110 executable = ctx.executable._bnd_exe,
111 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700112
113bnd = rule(
114 attrs = {
115 "deps": attr.label_list(),
116 "version": attr.string(),
117 "package_name_root": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700118 "source": attr.label(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700119 "_bnd_exe": attr.label(
120 executable = True,
121 cfg = "host",
122 allow_files = True,
123 default = Label("//utils/osgiwrap:osgi-jar"),
124 ),
125 },
126 fragments = ["java"],
127 outputs = {
128 "osgi_jar": "lib%{name}.jar",
129 },
130 implementation = _bnd_impl,
131)
132
133def _fwd_bnd(name, source, deps, version, package_name_root, visibility):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700134 bnd(name = name, source = source, deps = deps, version = version, package_name_root = package_name_root, visibility = visibility)
Ray Milkey7dac7da2017-08-01 16:56:05 -0700135
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700136def wrapped_osgi_library(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", visibility = ["//visibility:private"]):
137 _fwd_bnd(name, jar, deps, version, package_name_root, visibility)
Ray Milkey7dac7da2017-08-01 16:56:05 -0700138
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700139def osgi_jar_with_tests(
140 name = None,
141 deps = None,
142 test_deps = None,
143 package_name_root = "org.onosproject",
144 srcs = None,
145 resources_root = None,
146 resources = None,
147 test_srcs = None,
148 exclude_tests = None,
149 test_resources = None,
150 visibility = ["//visibility:public"],
151 version = ONOS_VERSION):
152 if name == None:
153 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700154 if srcs == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700155 srcs = all_java_sources()
156 if resources == None:
157 resources = all_resources(resources_root)
158 if test_srcs == None:
159 test_srcs = all_java_test_sources()
160 if test_resources == None:
161 test_resources = all_test_resources()
162 if exclude_tests == None:
163 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700164 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700165 deps = COMPILE
166 if test_deps == None:
167 test_deps = TEST
168 tests_name = name + "-tests"
169 tests_jar_deps = list(depset(deps + test_deps)) + [name]
170 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700171
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700172 native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility)
173 _fwd_bnd(name + "-osgi", name, deps, version, package_name_root, visibility)
174 if test_srcs != []:
175 native.java_library(
176 name = tests_name,
177 srcs = test_srcs,
178 resources = test_resources,
179 deps = tests_jar_deps,
180 visibility = visibility,
181 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700182
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700183 generate_test_rules(
184 name = name + "-tests-gen",
185 test_files = test_srcs,
186 exclude_tests = exclude_tests,
187 deps = all_test_deps,
188 )
189
190def osgi_jar(
191 name = None,
192 deps = None,
193 package_name_root = "org.onosproject",
194 srcs = None,
195 resources_root = None,
196 resources = None,
197 visibility = ["//visibility:public"],
198 version = ONOS_VERSION):
199 if srcs == None:
200 srcs = all_java_sources()
201 if deps == None:
202 deps = COMPILE
203
204 osgi_jar_with_tests(
205 name = name,
206 deps = deps,
207 test_deps = [],
208 package_name_root = package_name_root,
209 srcs = srcs,
210 resources = resources,
211 resources_root = resources_root,
212 test_srcs = [],
213 exclude_tests = [],
214 test_resources = [],
215 visibility = visibility,
216 version = version,
217 )