blob: c3ed9b358ff9b5925d28248059b4b5a8d8815b4a [file] [log] [blame]
Ray Milkey7dac7da2017-08-01 16:56:05 -07001load("//tools/build/bazel:generate_workspace.bzl", "COMPILE", "TEST")
2load("//tools/build/bazel:variables.bzl", "ONOS_VERSION")
3load("//tools/build/bazel:generate_test_rules.bzl", "generate_test_rules")
4
5def all_java_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -07006 return native.glob(["src/main/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -07007
8def all_java_test_sources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -07009 return native.glob(["src/test/java/**/*.java"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070010
11def all_test_resources():
Ray Milkey0bcdfd12018-05-23 14:07:19 -070012 return native.glob(["src/test/resources/**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070013
14def all_resources(resources_root):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070015 if resources_root == None:
16 return native.glob(["src/main/resources/**"])
17 else:
18 return native.glob([resources_root + "**"])
Ray Milkey7dac7da2017-08-01 16:56:05 -070019
20# Implementation of the rule to call bnd to make an OSGI jar file
21def _bnd_impl(ctx):
Ray Milkey0bcdfd12018-05-23 14:07:19 -070022 jar = ctx.file.source.path
23 output = ctx.outputs.osgi_jar.path
24 cp = ""
25 name = ctx.attr.source.label.name
26 group = ctx.attr.package_name_root
27 version = ctx.attr.version
28 license = ""
29 importPackages = "*"
30 exportPackages = "*"
31 includeResources = ""
32 webContext = "NONE"
33 dynamicimportPackages = ""
Ray Milkey7dac7da2017-08-01 16:56:05 -070034
Ray Milkey0bcdfd12018-05-23 14:07:19 -070035 inputDependencies = [ctx.file.source]
Ray Milkey7dac7da2017-08-01 16:56:05 -070036
Ray Milkey0bcdfd12018-05-23 14:07:19 -070037 # determine the dependencies and build the class path
38 for dep in ctx.attr.deps:
Ray Milkey472d8392018-05-23 17:06:51 -070039 if len(dep.java.outputs.jars) == 0:
40 continue
41
Ray Milkey0bcdfd12018-05-23 14:07:19 -070042 file = dep.java.outputs.jars[0].class_jar
Ray Milkey7dac7da2017-08-01 16:56:05 -070043
Ray Milkey0bcdfd12018-05-23 14:07:19 -070044 if cp:
45 cp += ":"
46 cp += file.path
47 inputDependencies = inputDependencies + [file]
Ray Milkey7dac7da2017-08-01 16:56:05 -070048
Ray Milkey0bcdfd12018-05-23 14:07:19 -070049 # extract the class files for use by bnd
50 classes = ctx.actions.declare_file("classes")
51 classesPath = classes.path
52 jarCommand = "mkdir -p %s && cp %s %s && cd %s && jar xf *.jar" % (classesPath, jar, classesPath, classesPath)
53 ctx.actions.run_shell(
54 inputs = inputDependencies,
55 outputs = [classes],
56 command = jarCommand,
57 progress_message = "Expanding jar file: %s" % jar,
58 )
59 inputDependencies += [classes]
Ray Milkey7dac7da2017-08-01 16:56:05 -070060
Ray Milkey0bcdfd12018-05-23 14:07:19 -070061 # call bnd to make the OSGI jar file
62 arguments = [
63 jar,
64 output,
65 cp,
66 name,
67 group,
68 version,
69 license,
70 importPackages,
71 exportPackages,
72 includeResources,
73 webContext,
74 dynamicimportPackages,
75 classesPath,
76 ]
77 ctx.actions.run(
78 inputs = inputDependencies,
79 outputs = [ctx.outputs.osgi_jar],
80 arguments = arguments,
81 progress_message = "Running bnd wrapper on: %s" % ctx.attr.name,
82 executable = ctx.executable._bnd_exe,
83 )
Ray Milkey7dac7da2017-08-01 16:56:05 -070084
85bnd = rule(
86 attrs = {
87 "deps": attr.label_list(),
88 "version": attr.string(),
89 "package_name_root": attr.string(),
90 "source": attr.label(allow_single_file = True),
91 "_bnd_exe": attr.label(
92 executable = True,
93 cfg = "host",
94 allow_files = True,
95 default = Label("//utils/osgiwrap:osgi-jar"),
96 ),
97 },
98 fragments = ["java"],
99 outputs = {
100 "osgi_jar": "lib%{name}.jar",
101 },
102 implementation = _bnd_impl,
103)
104
105def _fwd_bnd(name, source, deps, version, package_name_root, visibility):
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700106 bnd(name = name, source = source, deps = deps, version = version, package_name_root = package_name_root, visibility = visibility)
Ray Milkey7dac7da2017-08-01 16:56:05 -0700107
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700108def wrapped_osgi_library(name, jar, deps, version = ONOS_VERSION, package_name_root = "org.onosproject", visibility = ["//visibility:private"]):
109 _fwd_bnd(name, jar, deps, version, package_name_root, visibility)
Ray Milkey7dac7da2017-08-01 16:56:05 -0700110
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700111def osgi_jar_with_tests(
112 name = None,
113 deps = None,
114 test_deps = None,
115 package_name_root = "org.onosproject",
116 srcs = None,
117 resources_root = None,
118 resources = None,
119 test_srcs = None,
120 exclude_tests = None,
121 test_resources = None,
122 visibility = ["//visibility:public"],
123 version = ONOS_VERSION):
124 if name == None:
125 name = "onos-" + native.package_name().replace("/", "-")
Ray Milkey7dac7da2017-08-01 16:56:05 -0700126 if srcs == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700127 srcs = all_java_sources()
128 if resources == None:
129 resources = all_resources(resources_root)
130 if test_srcs == None:
131 test_srcs = all_java_test_sources()
132 if test_resources == None:
133 test_resources = all_test_resources()
134 if exclude_tests == None:
135 exclude_tests = []
Ray Milkey7dac7da2017-08-01 16:56:05 -0700136 if deps == None:
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700137 deps = COMPILE
138 if test_deps == None:
139 test_deps = TEST
140 tests_name = name + "-tests"
141 tests_jar_deps = list(depset(deps + test_deps)) + [name]
142 all_test_deps = tests_jar_deps + [tests_name]
Ray Milkey7dac7da2017-08-01 16:56:05 -0700143
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700144 native.java_library(name = name, srcs = srcs, resources = resources, deps = deps, visibility = visibility)
145 _fwd_bnd(name + "-osgi", name, deps, version, package_name_root, visibility)
146 if test_srcs != []:
147 native.java_library(
148 name = tests_name,
149 srcs = test_srcs,
150 resources = test_resources,
151 deps = tests_jar_deps,
152 visibility = visibility,
153 )
Ray Milkey7dac7da2017-08-01 16:56:05 -0700154
Ray Milkey0bcdfd12018-05-23 14:07:19 -0700155 generate_test_rules(
156 name = name + "-tests-gen",
157 test_files = test_srcs,
158 exclude_tests = exclude_tests,
159 deps = all_test_deps,
160 )
161
162def osgi_jar(
163 name = None,
164 deps = None,
165 package_name_root = "org.onosproject",
166 srcs = None,
167 resources_root = None,
168 resources = None,
169 visibility = ["//visibility:public"],
170 version = ONOS_VERSION):
171 if srcs == None:
172 srcs = all_java_sources()
173 if deps == None:
174 deps = COMPILE
175
176 osgi_jar_with_tests(
177 name = name,
178 deps = deps,
179 test_deps = [],
180 package_name_root = package_name_root,
181 srcs = srcs,
182 resources = resources,
183 resources_root = resources_root,
184 test_srcs = [],
185 exclude_tests = [],
186 test_resources = [],
187 visibility = visibility,
188 version = version,
189 )