Skip to content

Commit 4b5652b

Browse files
Re-attach OSGI headers to lite,core, and util. This information was dropped in the move from maven to bazel.
PiperOrigin-RevId: 518267412
1 parent bf1ae22 commit 4b5652b

File tree

8 files changed

+688
-8
lines changed

8 files changed

+688
-8
lines changed

build_defs/java_opts.bzl

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22

33
load("@rules_java//java:defs.bzl", "java_library")
44
load("@rules_jvm_external//:defs.bzl", "java_export")
5+
load("//java/osgi:osgi.bzl", "osgi_java_library")
6+
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
57

68
JAVA_OPTS = [
79
"-source 8",
810
"-target 8",
911
"-Xep:Java8ApiChecker:ERROR",
1012
]
1113

14+
BUNDLE_DOC_URL = "https://developers.google.com/protocol-buffers/"
15+
BUNDLE_LICENSE = "https://opensource.org/licenses/BSD-3-Clause"
16+
1217
def protobuf_java_export(**kwargs):
1318
java_export(
1419
javacopts = JAVA_OPTS,
@@ -20,3 +25,53 @@ def protobuf_java_library(**kwargs):
2025
javacopts = JAVA_OPTS,
2126
**kwargs
2227
)
28+
29+
def protobuf_versioned_java_library(
30+
bundle_description,
31+
bundle_name,
32+
bundle_symbolic_name,
33+
bundle_additional_imports = [],
34+
bundle_additional_exports = [],
35+
**kwargs):
36+
"""Extends `java_library` to add OSGi headers to the MANIFEST.MF using bndlib
37+
38+
This macro should be usable as a drop-in replacement for java_library.
39+
40+
The additional arguments are given the bndlib tool to generate an OSGi-compliant manifest file.
41+
See [bnd documentation](https://bnd.bndtools.org/chapters/110-introduction.html)
42+
43+
Takes all the args that are standard for a java_library target plus the following.
44+
Args:
45+
bundle_description: (required) The Bundle-Description header defines a short
46+
description of this bundle.
47+
bundle_name: (required) The Bundle-Name header defines a readable name for this
48+
bundle. This should be a short, human-readable name that can
49+
contain spaces.
50+
bundle_symbolic_name: (required) The Bundle-SymbolicName header specifies a
51+
non-localizable name for this bundle. The bundle symbolic name
52+
together with a version must identify a unique bundle though it can
53+
be installed multiple times in a framework. The bundle symbolic
54+
name should be based on the reverse domain name convention.
55+
bundle_additional_exports: The Export-Package header contains a
56+
declaration of exported packages. These are additional export
57+
package statements to be added before the default wildcard export
58+
"*;version={$Bundle-Version}".
59+
bundle_additional_imports: The Import-Package header declares the
60+
imported packages for this bundle. These are additional import
61+
package statements to be added before the default wildcard import
62+
"*".
63+
**kwargs: Additional key-word arguments that are passed to the internal
64+
java_library target.
65+
"""
66+
osgi_java_library(
67+
javacopts = JAVA_OPTS,
68+
bundle_doc_url = BUNDLE_DOC_URL,
69+
bundle_license = BUNDLE_LICENSE,
70+
bundle_version = PROTOBUF_JAVA_VERSION,
71+
bundle_description = bundle_description,
72+
bundle_name = bundle_name,
73+
bundle_symbolic_name = bundle_symbolic_name,
74+
bundle_additional_exports = bundle_additional_exports,
75+
bundle_additional_imports = bundle_additional_imports + ["sun.misc;resolution:=optional"],
76+
**kwargs
77+
)

java/core/BUILD.bazel

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@bazel_skylib//rules:build_test.bzl", "build_test")
22
load("@rules_java//java:defs.bzl", "java_lite_proto_library", "java_proto_library")
33
load("@rules_pkg//:mappings.bzl", "pkg_files", "strip_prefix")
44
load("@rules_proto//proto:defs.bzl", "proto_lang_toolchain", "proto_library")
5-
load("//build_defs:java_opts.bzl", "protobuf_java_export", "protobuf_java_library")
5+
load("//build_defs:java_opts.bzl", "protobuf_java_export", "protobuf_java_library", "protobuf_versioned_java_library")
66
load("//conformance:defs.bzl", "conformance_test")
77
load("//:protobuf.bzl", "internal_gen_well_known_protos_java")
88
load("//:protobuf_version.bzl", "PROTOBUF_JAVA_VERSION")
@@ -121,11 +121,16 @@ internal_gen_well_known_protos_java(
121121
)
122122

123123
# Should be used as `//java/lite`.
124-
protobuf_java_library(
124+
protobuf_versioned_java_library(
125125
name = "lite",
126126
srcs = LITE_SRCS + [
127127
":gen_well_known_protos_javalite",
128128
],
129+
bundle_description = "Lite version of Protocol Buffers library. This " +
130+
"version is optimized for code size, but does not " +
131+
"guarantee API/ABI stability.",
132+
bundle_name = "Protocol Buffers [Lite]",
133+
bundle_symbolic_name = "com.google.protobuf",
129134
visibility = [
130135
"//java/lite:__pkg__",
131136
],
@@ -166,7 +171,7 @@ internal_gen_well_known_protos_java(
166171
],
167172
)
168173

169-
protobuf_java_library(
174+
protobuf_versioned_java_library(
170175
name = "core",
171176
srcs = glob(
172177
[
@@ -176,6 +181,11 @@ protobuf_java_library(
176181
) + [
177182
":gen_well_known_protos_java",
178183
],
184+
bundle_description = "Core Protocol Buffers library. Protocol Buffers " +
185+
"are a way of encoding structured data in an " +
186+
"efficient yet extensible format.",
187+
bundle_name = "Protocol Buffers [Core]",
188+
bundle_symbolic_name = "com.google.protobuf",
179189
visibility = ["//visibility:public"],
180190
exports = [
181191
":lite_runtime_only",

java/osgi/BUILD.bazel

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
load("@rules_java//java:defs.bzl", "java_binary")
2+
3+
package(
4+
default_visibility = ["//java:__subpackages__"],
5+
)
6+
7+
java_binary(
8+
name = "osgi_wrapper",
9+
srcs = ["OsgiWrapper.java"],
10+
main_class = "com.google.protobuf.osgi.OsgiWrapper",
11+
deps = [
12+
"@maven//:biz_aQute_bnd_biz_aQute_bndlib",
13+
"@maven//:com_google_guava_guava",
14+
"@maven//:info_picocli_picocli",
15+
],
16+
)

java/osgi/OsgiWrapper.java

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Protocol Buffers - Google's data interchange format
2+
// Copyright 2008 Google Inc. All rights reserved.
3+
// https://developers.google.com/protocol-buffers/
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, are permitted provided that the following conditions are
7+
// met:
8+
//
9+
// * Redistributions of source code must retain the above copyright
10+
// notice, this list of conditions and the following disclaimer.
11+
// * Redistributions in binary form must reproduce the above
12+
// copyright notice, this list of conditions and the following disclaimer
13+
// in the documentation and/or other materials provided with the
14+
// distribution.
15+
// * Neither the name of Google Inc. nor the names of its
16+
// contributors may be used to endorse or promote products derived from
17+
// this software without specific prior written permission.
18+
//
19+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
31+
package com.google.protobuf.osgi;
32+
33+
import aQute.bnd.osgi.Analyzer;
34+
import aQute.bnd.osgi.Jar;
35+
import java.io.File;
36+
import java.util.Arrays;
37+
import java.util.concurrent.Callable;
38+
import java.util.jar.Manifest;
39+
import java.util.stream.Collectors;
40+
import picocli.CommandLine;
41+
import picocli.CommandLine.Command;
42+
import picocli.CommandLine.Option;
43+
44+
/** Java binary that runs bndlib to analyze a jar file to generate OSGi bundle manifest. */
45+
@Command(name = "osgi_wrapper")
46+
public final class OsgiWrapper implements Callable<Integer> {
47+
private static final String REMOVEHEADERS =
48+
Arrays.stream(
49+
new String[] {
50+
"Embed-Dependency",
51+
"Embed-Transitive",
52+
"Built-By",
53+
// "Tool",
54+
"Created-By",
55+
// "Build-Jdk",
56+
"Originally-Created-By",
57+
"Archiver-Version",
58+
"Include-Resource",
59+
"Private-Package",
60+
"Ignore-Package",
61+
// "Bnd-LastModified",
62+
"Target-Label"
63+
})
64+
.collect(Collectors.joining(","));
65+
66+
@Option(
67+
names = {"--input_jar"},
68+
description = "The jar file to wrap with OSGi metadata")
69+
private File inputJar;
70+
71+
@Option(
72+
names = {"--output_jar"},
73+
description = "Output path to the wrapped jar")
74+
private File outputJar;
75+
76+
@Option(
77+
names = {"--classpath"},
78+
description = "The classpath that contains dependencies of the input jar, separated with :")
79+
private String classpath;
80+
81+
@Option(
82+
names = {"--bundle_copyright"},
83+
description = "Copyright string for the bundle")
84+
private String bundleCopyright;
85+
86+
@Option(
87+
names = {"--bundle_description"},
88+
description = "Description of the bundle")
89+
private String bundleDescription;
90+
91+
@Option(
92+
names = {"--bundle_doc_url"},
93+
description = "Documentation URL for the bundle")
94+
private String bundleDocUrl;
95+
96+
@Option(
97+
names = {"--bundle_license"},
98+
description = "URL for the license of the bundle")
99+
private String bundleLicense;
100+
101+
@Option(
102+
names = {"--bundle_name"},
103+
description = "The name of the bundle")
104+
private String bundleName;
105+
106+
@Option(
107+
names = {"--bundle_symbolic_name"},
108+
description = "The symbolic name of the bundle")
109+
private String bundleSymbolicName;
110+
111+
@Option(
112+
names = {"--bundle_version"},
113+
description = "The version of the bundle")
114+
private String bundleVersion;
115+
116+
@Option(
117+
names = {"--export_package"},
118+
description = "The exported packages from this bundle")
119+
private String exportPackage;
120+
121+
@Option(
122+
names = {"--import_package"},
123+
description = "The imported packages from this bundle")
124+
private String importPackage;
125+
126+
@Override
127+
public Integer call() throws Exception {
128+
Jar bin = new Jar(inputJar);
129+
130+
Analyzer analyzer = new Analyzer();
131+
analyzer.setJar(bin);
132+
analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName);
133+
analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName);
134+
analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion);
135+
analyzer.setProperty(Analyzer.IMPORT_PACKAGE, importPackage);
136+
analyzer.setProperty(Analyzer.EXPORT_PACKAGE, exportPackage);
137+
analyzer.setProperty(Analyzer.BUNDLE_DESCRIPTION, bundleDescription);
138+
analyzer.setProperty(Analyzer.BUNDLE_COPYRIGHT, bundleCopyright);
139+
analyzer.setProperty(Analyzer.BUNDLE_DOCURL, bundleDocUrl);
140+
analyzer.setProperty(Analyzer.BUNDLE_LICENSE, bundleLicense);
141+
analyzer.setProperty(Analyzer.REMOVEHEADERS, REMOVEHEADERS);
142+
143+
if (classpath != null) {
144+
for (String dep : Arrays.asList(classpath.split(":"))) {
145+
analyzer.addClasspath(new File(dep));
146+
}
147+
}
148+
149+
analyzer.analyze();
150+
151+
Manifest manifest = analyzer.calcManifest();
152+
153+
if (analyzer.isOk()) {
154+
analyzer.getJar().setManifest(manifest);
155+
if (analyzer.save(outputJar, true)) {
156+
return 0;
157+
}
158+
}
159+
return 1;
160+
}
161+
162+
public static void main(String[] args) {
163+
int exitCode = new CommandLine(new OsgiWrapper()).execute(args);
164+
System.exit(exitCode);
165+
}
166+
}

0 commit comments

Comments
 (0)