blob: 8e4fb562fb7528a05a4a25d5327b9c487b8bfd51 [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 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,
Ray Milkey12ae6ca2018-06-11 15:34:30 -070098 import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -070099 exportPackages,
100 includeResources,
101 webContext,
102 dynamicimportPackages,
103 classesPath,
104 ]
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700105
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700106 ctx.actions.run(
107 inputs = inputDependencies,
108 outputs = [ctx.outputs.osgi_jar],
109 arguments = arguments,
110 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
111 executable = ctx.executable._bnd_exe,
112 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700113
Ray Milkey32ea35c2018-06-06 15:28:07 -0700114_bnd = rule(
Ray Milkey7dac7da2017-08-01 16:56:05 -0700115 attrs = {
116 "deps": attr.label_list(),
117 "version": attr.string(),
118 "package_name_root": attr.string(),
Ray Milkey3275ae82018-05-29 15:35:36 -0700119 "source": attr.label(),
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700120 "import_packages": attr.string(),
Ray Milkey7dac7da2017-08-01 16:56:05 -0700121 "_bnd_exe": attr.label(
122 executable = True,
123 cfg = "host",
124 allow_files = True,
125 default = Label("//utils/osgiwrap:osgi-jar"),
126 ),
127 },
128 fragments = ["java"],
129 outputs = {
130 "osgi_jar": "lib%{name}.jar",
131 },
132 implementation = _bnd_impl,
133)
134
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700135def wrapped_osgi_jar(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", import_packages = "*", visibility = ["//visibility:private"]):
136 _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 -0700137
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700138def osgi_jar_with_tests(
139 name = None,
140 deps = None,
141 test_deps = None,
142 package_name_root = "org.onosproject",
143 srcs = None,
144 resources_root = None,
145 resources = None,
146 test_srcs = None,
147 exclude_tests = None,
148 test_resources = None,
149 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700150 version = ONOS_VERSION,
151 import_packages = None,
152 ):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700153 if name == None:
154 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700155 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700156 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700157 if resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700158 resources = _all_resources(resources_root)
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700159 if test_srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700160 test_srcs = _all_java_test_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700161 if test_resources == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700162 test_resources = _all_test_resources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700163 if exclude_tests == None:
164 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700165 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700166 deps = COMPILE
167 if test_deps == None:
168 test_deps = TEST
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700169 if import_packages == None:
170 import_packages = "*"
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700171 tests_name = name + "-tests"
172 tests_jar_deps = list(depset(deps + test_deps)) + [name]
173 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700174
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700175 native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility)
Ray Milkey32ea35c2018-06-06 15:28:07 -0700176 _bnd(
177 name = name + "-osgi",
178 source = name,
179 deps = deps,
180 version = version,
181 package_name_root = package_name_root,
182 visibility = visibility,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700183 import_packages = import_packages,
Ray Milkey32ea35c2018-06-06 15:28:07 -0700184 )
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700185 if test_srcs != []:
186 native.java_library(
187 name = tests_name,
188 srcs = test_srcs,
189 resources = test_resources,
190 deps = tests_jar_deps,
191 visibility = visibility,
192 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700193
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700194 generate_test_rules(
195 name = name + "-tests-gen",
196 test_files = test_srcs,
197 exclude_tests = exclude_tests,
198 deps = all_test_deps,
199 )
200
201def osgi_jar(
202 name = None,
203 deps = None,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700204 import_packages = None,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700205 package_name_root = "org.onosproject",
206 srcs = None,
207 resources_root = None,
208 resources = None,
209 visibility = ["//visibility:public"],
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700210 version = ONOS_VERSION,
211 # TODO - implement these for swagger and web.xml
212 web_context = "",
213 api_title = "",
214 api_version = "",
215 api_description = "",
216 api_package = "",
217 ):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700218 if srcs == None:
Ray Milkey32ea35c2018-06-06 15:28:07 -0700219 srcs = _all_java_sources()
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700220 if deps == None:
221 deps = COMPILE
222
223 osgi_jar_with_tests(
224 name = name,
225 deps = deps,
226 test_deps = [],
227 package_name_root = package_name_root,
228 srcs = srcs,
229 resources = resources,
230 resources_root = resources_root,
231 test_srcs = [],
232 exclude_tests = [],
233 test_resources = [],
234 visibility = visibility,
235 version = version,
Ray Milkey12ae6ca2018-06-11 15:34:30 -0700236 import_packages = import_packages,
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700237 )