blob: de8c28c748b43a549b20c88512afec88cd07691f [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 = ""
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
Ray Milkey32ea35c2018-06-06 15:28:07 -0700113_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700114 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
Ray Milkey32ea35c2018-06-06 15:28:07 -0700133def wrapped_osgi_jar(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", visibility = ["//visibility:private"]):
134 _bnd(name = name, source = jar, 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 osgi_jar_with_tests(
137 name = None,
138 deps = None,
139 test_deps = None,
140 package_name_root = "org.onosproject",
141 srcs = None,
142 resources_root = None,
143 resources = None,
144 test_srcs = None,
145 exclude_tests = None,
146 test_resources = None,
147 visibility = ["//visibility:public"],
148 version = ONOS_VERSION):
149 if name == None:
150 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700151 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700152 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700153 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700154 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700155 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700156 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700157 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700158 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700159 if exclude_tests == None:
160 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700161 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700162 deps = COMPILE
163 if test_deps == None:
164 test_deps = TEST
165 tests_name = name + "-tests"
166 tests_jar_deps = list(depset(deps + test_deps)) + [name]
167 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700168
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700169 native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700170 _bnd(
171 name = name + "-osgi",
172 source = name,
173 deps = deps,
174 version = version,
175 package_name_root = package_name_root,
176 visibility = visibility,
177 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700178 if test_srcs != []:
179 native.java_library(
180 name = tests_name,
181 srcs = test_srcs,
182 resources = test_resources,
183 deps = tests_jar_deps,
184 visibility = visibility,
185 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700186
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700187 generate_test_rules(
188 name = name + "-tests-gen",
189 test_files = test_srcs,
190 exclude_tests = exclude_tests,
191 deps = all_test_deps,
192 )
193
194def osgi_jar(
195 name = None,
196 deps = None,
197 package_name_root = "org.onosproject",
198 srcs = None,
199 resources_root = None,
200 resources = None,
201 visibility = ["//visibility:public"],
202 version = ONOS_VERSION):
203 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700204 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700205 if deps == None:
206 deps = COMPILE
207
208 osgi_jar_with_tests(
209 name = name,
210 deps = deps,
211 test_deps = [],
212 package_name_root = package_name_root,
213 srcs = srcs,
214 resources = resources,
215 resources_root = resources_root,
216 test_srcs = [],
217 exclude_tests = [],
218 test_resources = [],
219 visibility = visibility,
220 version = version,
221 )