Skip to content

Commit 81bab06

Browse files
committed
Split up kotlin and java osgi bazel files
1 parent a46007d commit 81bab06

File tree

3 files changed

+216
-131
lines changed

3 files changed

+216
-131
lines changed

build_defs/kotlin_opts.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Protobuf-specific kotlin build rules."""
22

33
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
4-
load("//java/osgi:osgi.bzl", "osgi_kt_jvm_library")
4+
load("//java/osgi:kotlin_osgi.bzl", "osgi_kt_jvm_library")
55

66

77
BUNDLE_DOC_URL = "https://developers.google.com/protocol-buffers/"

java/osgi/kotlin_osgi.bzl

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
""" Custom rule to generate OSGi Manifest for Kotlin """
2+
3+
load("@rules_java//java:defs.bzl", "JavaInfo")
4+
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
5+
6+
def osgi_kt_jvm_library(
7+
name,
8+
automatic_module_name,
9+
bundle_description,
10+
bundle_doc_url,
11+
bundle_license,
12+
bundle_name,
13+
bundle_symbolic_name,
14+
bundle_version,
15+
bundle_additional_imports = [],
16+
bundle_additional_exports = [],
17+
deps = [],
18+
exports = [],
19+
exported_plugins = [],
20+
neverlink = False,
21+
runtime_deps = [],
22+
visibility = [],
23+
**kwargs):
24+
"""Extends `kt_jvm_library` to add OSGi headers to the MANIFEST.MF using bndlib
25+
26+
This macro should be usable as a drop-in replacement for kt_jvm_library.
27+
28+
The additional arguments are given the bndlib tool to generate an OSGi-compliant manifest file.
29+
See [bnd documentation](https://bnd.bndtools.org/chapters/110-introduction.html)
30+
31+
Args:
32+
name: (required) A unique name for this target.
33+
bundle_description: (required) The Bundle-Description header defines a short
34+
description of this bundle.
35+
bundle_doc_url: (required) The Bundle-DocURL headers must contain a URL pointing
36+
to documentation about this bundle.
37+
bundle_license: (required) The Bundle-License header provides an optional machine
38+
readable form of license information.
39+
bundle_name: (required) The Bundle-Name header defines a readable name for this
40+
bundle. This should be a short, human-readable name that can
41+
contain spaces.
42+
bundle_symbolic_name: (required) The Bundle-SymbolicName header specifies a
43+
non-localizable name for this bundle. The bundle symbolic name
44+
together with a version must identify a unique bundle though it can
45+
be installed multiple times in a framework. The bundle symbolic
46+
name should be based on the reverse domain name convention.
47+
bundle_version: (required) The Bundle-Version header specifies the version string
48+
for this bundle. The version string is expected to follow semantic
49+
versioning conventions MAJOR.MINOR.PATCH[.BUILD]
50+
bundle_additional_exports: The Export-Package header contains a
51+
declaration of exported packages. These are additional export
52+
package statements to be added before the default wildcard export
53+
"*;version={$Bundle-Version}".
54+
bundle_additional_imports: The Import-Package header declares the
55+
imported packages for this bundle. These are additional import
56+
package statements to be added before the default wildcard import
57+
"*".
58+
deps: The list of libraries to link into this library. See general
59+
comments about deps at Typical attributes defined by most build
60+
rules. The jars built by java_library rules listed in deps will be
61+
on the compile-time classpath of this rule. Furthermore the
62+
transitive closure of their deps, runtime_deps and exports will be
63+
on the runtime classpath. By contrast, targets in the data
64+
attribute are included in the runfiles but on neither the
65+
compile-time nor runtime classpath.
66+
exports: Exported libraries.
67+
exported_plugins: The list of java_plugins (e.g. annotation processors)
68+
to export to libraries that directly depend on this library. The
69+
specified list of java_plugins will be applied to any library which
70+
directly depends on this library, just as if that library had
71+
explicitly declared these labels in plugins.
72+
neverlink: Whether this library should only be used for compilation and
73+
not at runtime. Useful if the library will be provided by the runtime
74+
environment during execution. Examples of such libraries are the IDE
75+
APIs for IDE plug-ins or tools.jar for anything running on a standard
76+
JDK.
77+
runtime_deps: Libraries to make available to the final binary or test
78+
at runtime only. Like ordinary deps, these will appear on the runtime
79+
classpath, but unlike them, not on the compile-time classpath.
80+
Dependencies needed only at runtime should be listed here.
81+
Dependency-analysis tools should ignore targets that appear in both
82+
runtime_deps and deps
83+
visibility: The visibility attribute on a target controls whether the
84+
target can be used in other packages. See the documentation for
85+
visibility.
86+
**kwargs: Additional key-word arguments that are passed to the internal
87+
java_library target.
88+
89+
"""
90+
91+
# Build the private jar without the OSGI manifest
92+
private_library_name = "%s-no-manifest-do-not-use" % name
93+
kt_jvm_library(
94+
name = private_library_name,
95+
deps = deps,
96+
runtime_deps = runtime_deps,
97+
neverlink = True,
98+
visibility = ["//visibility:private"],
99+
**kwargs
100+
)
101+
102+
# Repackage the jar with an OSGI manifest
103+
_osgi_kt_jvm_jar(
104+
name = name,
105+
automatic_module_name = automatic_module_name,
106+
bundle_description = bundle_description,
107+
bundle_doc_url = bundle_doc_url,
108+
bundle_license = bundle_license,
109+
bundle_name = bundle_name,
110+
bundle_symbolic_name = bundle_symbolic_name,
111+
bundle_version = bundle_version,
112+
export_package = bundle_additional_exports + ["*;version=${Bundle-Version}"],
113+
import_package = bundle_additional_imports + ["*"],
114+
target = private_library_name,
115+
deps = deps,
116+
runtime_deps = runtime_deps,
117+
exported_plugins = exported_plugins,
118+
neverlink = neverlink,
119+
exports = exports,
120+
visibility = visibility,
121+
)
122+
123+
124+
def _run_osgi_wrapper(ctx, input_jar, output_jar):
125+
args = ctx.actions.args()
126+
args.add("--input_jar", input_jar.path)
127+
args.add("--output_jar", output_jar.path)
128+
args.add("--automatic_module_name", ctx.attr.automatic_module_name)
129+
args.add("--bundle_copyright", ctx.attr.bundle_copyright)
130+
args.add("--bundle_description", ctx.attr.bundle_description)
131+
args.add("--bundle_doc_url", ctx.attr.bundle_doc_url)
132+
args.add("--bundle_license", ctx.attr.bundle_license)
133+
args.add("--bundle_name", ctx.attr.bundle_name)
134+
args.add("--bundle_version", ctx.attr.bundle_version)
135+
args.add("--bundle_symbolic_name", ctx.attr.bundle_symbolic_name)
136+
args.add_joined("--export_package", ctx.attr.export_package, join_with = ",")
137+
args.add_joined("--import_package", ctx.attr.import_package, join_with = ",")
138+
139+
ctx.actions.run(
140+
inputs = [input_jar],
141+
executable = ctx.executable._osgi_wrapper_exe,
142+
arguments = [args],
143+
outputs = [output_jar],
144+
progress_message = "Generating OSGi bundle Manifest for %s" % input_jar.path,
145+
)
146+
147+
148+
# Kotlin implementation of osgi jar, removes classpath and source_jar
149+
def _osgi_kt_jvm_jar_impl(ctx):
150+
if len(ctx.attr.target[JavaInfo].java_outputs) != 1:
151+
fail("osgi_jar rule can only be used on a single java target.")
152+
target_java_output = ctx.attr.target[JavaInfo].java_outputs[0]
153+
154+
output_jar = ctx.outputs.output_jar
155+
156+
input_jar = target_java_output.class_jar
157+
158+
_run_osgi_wrapper(ctx, input_jar, output_jar)
159+
160+
return [
161+
DefaultInfo(
162+
files = depset([output_jar]),
163+
# Workaround for https://github.com/bazelbuild/bazel/issues/15043
164+
# Bazel's native rule such as sh_test do not pick up 'files' in
165+
# DefaultInfo for a target in 'data'.
166+
data_runfiles = ctx.runfiles([output_jar]),
167+
),
168+
JavaInfo(
169+
output_jar = output_jar,
170+
171+
# compile_jar should be an ijar, but using an ijar results in
172+
# missing protobuf import version.
173+
compile_jar = output_jar,
174+
generated_class_jar = target_java_output.generated_class_jar,
175+
native_headers_jar = target_java_output.native_headers_jar,
176+
manifest_proto = target_java_output.manifest_proto,
177+
neverlink = ctx.attr.neverlink,
178+
deps = [dep[JavaInfo] for dep in ctx.attr.deps],
179+
runtime_deps = [dep[JavaInfo] for dep in ctx.attr.runtime_deps],
180+
exports = [exp[JavaInfo] for exp in ctx.attr.exports],
181+
exported_plugins = ctx.attr.exported_plugins,
182+
),
183+
]
184+
185+
186+
_osgi_kt_jvm_jar = rule(
187+
implementation = _osgi_kt_jvm_jar_impl,
188+
outputs = {
189+
"output_jar": "lib%{name}.jar",
190+
},
191+
attrs = {
192+
"automatic_module_name": attr.string(),
193+
"bundle_copyright": attr.string(),
194+
"bundle_description": attr.string(),
195+
"bundle_doc_url": attr.string(),
196+
"bundle_license": attr.string(),
197+
"bundle_name": attr.string(),
198+
"bundle_version": attr.string(),
199+
"bundle_symbolic_name": attr.string(),
200+
"export_package": attr.string_list(),
201+
"import_package": attr.string_list(),
202+
"target": attr.label(),
203+
"deps": attr.label_list(),
204+
"runtime_deps": attr.label_list(),
205+
"exports": attr.label_list(),
206+
"neverlink": attr.bool(),
207+
"exported_plugins": attr.label_list(),
208+
"_osgi_wrapper_exe": attr.label(
209+
executable = True,
210+
cfg = "exec",
211+
allow_files = True,
212+
default = Label("//java/osgi:osgi_wrapper"),
213+
),
214+
},
215+
)

java/osgi/osgi.bzl

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
""" Custom rule to generate OSGi Manifest """
22

33
load("@rules_java//java:defs.bzl", "JavaInfo", "java_library")
4-
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library")
54

65
# Note that this rule is currently agnostic of protobuf concerns and could be
76
# pulled out as a general purpose helper to allow migrations from maven to bazel
@@ -139,64 +138,6 @@ def osgi_java_library(
139138
visibility = visibility,
140139
)
141140

142-
def osgi_kt_jvm_library(
143-
name,
144-
automatic_module_name,
145-
bundle_description,
146-
bundle_doc_url,
147-
bundle_license,
148-
bundle_name,
149-
bundle_symbolic_name,
150-
bundle_version,
151-
bundle_additional_imports = [],
152-
bundle_additional_exports = [],
153-
deps = [],
154-
exports = [],
155-
exported_plugins = [],
156-
neverlink = False,
157-
runtime_deps = [],
158-
visibility = [],
159-
**kwargs):
160-
"""Extends `kt_jvm_library` to add OSGi headers to the MANIFEST.MF using bndlib
161-
162-
This macro should be usable as a drop-in replacement for kt_jvm_library.
163-
164-
The additional arguments are the same as osgi_java_library above.
165-
"""
166-
167-
# Build the private jar without the OSGI manifest
168-
private_library_name = "%s-no-manifest-do-not-use" % name
169-
kt_jvm_library(
170-
name = private_library_name,
171-
deps = deps,
172-
runtime_deps = runtime_deps,
173-
neverlink = True,
174-
visibility = ["//visibility:private"],
175-
**kwargs
176-
)
177-
178-
# Repackage the jar with an OSGI manifest
179-
_osgi_kt_jvm_jar(
180-
name = name,
181-
automatic_module_name = automatic_module_name,
182-
bundle_description = bundle_description,
183-
bundle_doc_url = bundle_doc_url,
184-
bundle_license = bundle_license,
185-
bundle_name = bundle_name,
186-
bundle_symbolic_name = bundle_symbolic_name,
187-
bundle_version = bundle_version,
188-
export_package = bundle_additional_exports + ["*;version=${Bundle-Version}"],
189-
import_package = bundle_additional_imports + ["*"],
190-
target = private_library_name,
191-
deps = deps,
192-
runtime_deps = runtime_deps,
193-
exported_plugins = exported_plugins,
194-
neverlink = neverlink,
195-
exports = exports,
196-
visibility = visibility,
197-
)
198-
199-
200141
def _run_osgi_wrapper(ctx, input_jar, classpath_jars, output_jar):
201142
args = ctx.actions.args()
202143
args.add_joined("--classpath", classpath_jars, join_with = ":")
@@ -221,7 +162,6 @@ def _run_osgi_wrapper(ctx, input_jar, classpath_jars, output_jar):
221162
progress_message = "Generating OSGi bundle Manifest for %s" % input_jar.path,
222163
)
223164

224-
225165
def _osgi_jar_impl(ctx):
226166
if len(ctx.attr.target[JavaInfo].java_outputs) != 1:
227167
fail("osgi_jar rule can only be used on a single java target.")
@@ -302,73 +242,3 @@ _osgi_jar = rule(
302242
),
303243
},
304244
)
305-
306-
307-
# Kotlin implementation of osgi jar, removes classpath and source_jar
308-
def _osgi_kt_jvm_jar_impl(ctx):
309-
if len(ctx.attr.target[JavaInfo].java_outputs) != 1:
310-
fail("osgi_jar rule can only be used on a single java target.")
311-
target_java_output = ctx.attr.target[JavaInfo].java_outputs[0]
312-
313-
output_jar = ctx.outputs.output_jar
314-
315-
input_jar = target_java_output.class_jar
316-
317-
_run_osgi_wrapper(ctx, input_jar, [], output_jar)
318-
319-
return [
320-
DefaultInfo(
321-
files = depset([output_jar]),
322-
# Workaround for https://github.com/bazelbuild/bazel/issues/15043
323-
# Bazel's native rule such as sh_test do not pick up 'files' in
324-
# DefaultInfo for a target in 'data'.
325-
data_runfiles = ctx.runfiles([output_jar]),
326-
),
327-
JavaInfo(
328-
output_jar = output_jar,
329-
330-
# compile_jar should be an ijar, but using an ijar results in
331-
# missing protobuf import version.
332-
compile_jar = output_jar,
333-
generated_class_jar = target_java_output.generated_class_jar,
334-
native_headers_jar = target_java_output.native_headers_jar,
335-
manifest_proto = target_java_output.manifest_proto,
336-
neverlink = ctx.attr.neverlink,
337-
deps = [dep[JavaInfo] for dep in ctx.attr.deps],
338-
runtime_deps = [dep[JavaInfo] for dep in ctx.attr.runtime_deps],
339-
exports = [exp[JavaInfo] for exp in ctx.attr.exports],
340-
exported_plugins = ctx.attr.exported_plugins,
341-
),
342-
]
343-
344-
345-
_osgi_kt_jvm_jar = rule(
346-
implementation = _osgi_kt_jvm_jar_impl,
347-
outputs = {
348-
"output_jar": "lib%{name}.jar",
349-
},
350-
attrs = {
351-
"automatic_module_name": attr.string(),
352-
"bundle_copyright": attr.string(),
353-
"bundle_description": attr.string(),
354-
"bundle_doc_url": attr.string(),
355-
"bundle_license": attr.string(),
356-
"bundle_name": attr.string(),
357-
"bundle_version": attr.string(),
358-
"bundle_symbolic_name": attr.string(),
359-
"export_package": attr.string_list(),
360-
"import_package": attr.string_list(),
361-
"target": attr.label(),
362-
"deps": attr.label_list(),
363-
"runtime_deps": attr.label_list(),
364-
"exports": attr.label_list(),
365-
"neverlink": attr.bool(),
366-
"exported_plugins": attr.label_list(),
367-
"_osgi_wrapper_exe": attr.label(
368-
executable = True,
369-
cfg = "exec",
370-
allow_files = True,
371-
default = Label("//java/osgi:osgi_wrapper"),
372-
),
373-
},
374-
)

0 commit comments

Comments
 (0)