Skip to content

Commit

Permalink
Add Automatic-Module-Name (#14562)
Browse files Browse the repository at this point in the history
Adds Automatic-Module-Name after it was lost during the maven-bazel migration (and subsequent re-addition of osgi bundle support).

Updates OsgiWrapper to support adding the Automatic-Module-Name header to the list of properties supported by the `osgi_java_library` rule.

Fixes #12639

Closes #14562

COPYBARA_INTEGRATE_REVIEW=#14562 from Sineaggi:add-automatic-module-name a27b3e6
PiperOrigin-RevId: 579748655
  • Loading branch information
Clayton Walker authored and copybara-github committed Nov 6, 2023
1 parent 0a2fe57 commit d580fde
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions build_defs/java_opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def protobuf_java_library(**kwargs):
)

def protobuf_versioned_java_library(
automatic_module_name,
bundle_description,
bundle_name,
bundle_symbolic_name,
Expand All @@ -44,6 +45,9 @@ def protobuf_versioned_java_library(
Args:
bundle_description: (required) The Bundle-Description header defines a short
description of this bundle.
automatic_module_name: (required) The Automatic-Module-Name header that represents
the name of the module when this bundle is used as an automatic
module.
bundle_name: (required) The Bundle-Name header defines a readable name for this
bundle. This should be a short, human-readable name that can
contain spaces.
Expand All @@ -65,6 +69,7 @@ def protobuf_versioned_java_library(
"""
osgi_java_library(
javacopts = JAVA_OPTS,
automatic_module_name = automatic_module_name,
bundle_doc_url = BUNDLE_DOC_URL,
bundle_license = BUNDLE_LICENSE,
bundle_version = PROTOBUF_JAVA_VERSION,
Expand Down
2 changes: 2 additions & 0 deletions java/core/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ protobuf_versioned_java_library(
srcs = LITE_SRCS + [
":gen_well_known_protos_javalite",
],
automatic_module_name = "com.google.protobuf",
bundle_description = "Lite version of Protocol Buffers library. This " +
"version is optimized for code size, but does not " +
"guarantee API/ABI stability.",
Expand Down Expand Up @@ -217,6 +218,7 @@ protobuf_versioned_java_library(
) + [
":gen_well_known_protos_java",
],
automatic_module_name = "com.google.protobuf",
bundle_description = "Core Protocol Buffers library. Protocol Buffers " +
"are a way of encoding structured data in an " +
"efficient yet extensible format.",
Expand Down
6 changes: 6 additions & 0 deletions java/osgi/OsgiWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public final class OsgiWrapper implements Callable<Integer> {
description = "The classpath that contains dependencies of the input jar, separated with :")
private String classpath;

@Option(
names = {"--automatic_module_name"},
description = "The automatic module name of the bundle")
private String automaticModuleName;

@Option(
names = {"--bundle_copyright"},
description = "Copyright string for the bundle")
Expand Down Expand Up @@ -106,6 +111,7 @@ public Integer call() throws Exception {

Analyzer analyzer = new Analyzer();
analyzer.setJar(bin);
analyzer.setProperty(Analyzer.AUTOMATIC_MODULE_NAME, automaticModuleName);
analyzer.setProperty(Analyzer.BUNDLE_NAME, bundleName);
analyzer.setProperty(Analyzer.BUNDLE_SYMBOLICNAME, bundleSymbolicName);
analyzer.setProperty(Analyzer.BUNDLE_VERSION, bundleVersion);
Expand Down
4 changes: 4 additions & 0 deletions java/osgi/osgi.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ load("@rules_java//java:defs.bzl", "java_library")
# which is probably sub-optimal.
def osgi_java_library(
name,
automatic_module_name,
bundle_description,
bundle_doc_url,
bundle_license,
Expand Down Expand Up @@ -119,6 +120,7 @@ def osgi_java_library(
# Repackage the jar with an OSGI manifest
_osgi_jar(
name = name,
automatic_module_name = automatic_module_name,
bundle_description = bundle_description,
bundle_doc_url = bundle_doc_url,
bundle_license = bundle_license,
Expand All @@ -141,6 +143,7 @@ def _run_osgi_wrapper(ctx, input_jar, classpath_jars, output_jar):
args.add_joined("--classpath", classpath_jars, join_with = ":")
args.add("--input_jar", input_jar.path)
args.add("--output_jar", output_jar.path)
args.add("--automatic_module_name", ctx.attr.automatic_module_name)
args.add("--bundle_copyright", ctx.attr.bundle_copyright)
args.add("--bundle_description", ctx.attr.bundle_description)
args.add("--bundle_doc_url", ctx.attr.bundle_doc_url)
Expand Down Expand Up @@ -215,6 +218,7 @@ _osgi_jar = rule(
"output_jar": "lib%{name}.jar",
},
attrs = {
"automatic_module_name": attr.string(),
"bundle_copyright": attr.string(),
"bundle_description": attr.string(),
"bundle_doc_url": attr.string(),
Expand Down
1 change: 1 addition & 0 deletions java/util/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protobuf_versioned_java_library(
srcs = glob([
"src/main/java/com/google/protobuf/util/*.java",
]),
automatic_module_name = "com.google.protobuf.util",
bundle_description = "Utilities for Protocol Buffers",
bundle_name = "Protocol Buffers [Util]",
bundle_symbolic_name = "com.google.protobuf.util",
Expand Down

0 comments on commit d580fde

Please sign in to comment.