Skip to content

Add support for add_exports/add_opens #1551

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -47,6 +47,16 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")
bazel_features_deps()

# See https://github.com/bazelbuild/rules_scala/releases for up to date version information.
http_archive(
name = "io_bazel_rules_scala",
11 changes: 11 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -15,6 +15,17 @@ load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")

bazel_skylib_workspace()

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

_build_tools_release = "5.1.0"

http_archive(
11 changes: 11 additions & 0 deletions examples/scala3/WORKSPACE
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

local_repository(
name = "io_bazel_rules_scala",
path = "../..",
11 changes: 11 additions & 0 deletions examples/semanticdb/WORKSPACE
Original file line number Diff line number Diff line change
@@ -14,6 +14,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

local_repository(
name = "io_bazel_rules_scala",
path = "../..",
11 changes: 11 additions & 0 deletions examples/testing/multi_frameworks_toolchain/WORKSPACE
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

local_repository(
name = "io_bazel_rules_scala",
path = "../../..",
11 changes: 11 additions & 0 deletions examples/testing/scalatest_repositories/WORKSPACE
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

local_repository(
name = "io_bazel_rules_scala",
path = "../../..",
11 changes: 11 additions & 0 deletions examples/testing/specs2_junit_repositories/WORKSPACE
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

local_repository(
name = "io_bazel_rules_scala",
path = "../../..",
6 changes: 5 additions & 1 deletion scala/private/common_attributes.bzl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Shared attributes for rules"""

load("@bazel_features//:features.bzl", "bazel_features")
load(
"@io_bazel_rules_scala//scala/private:coverage_replacements_provider.bzl",
_coverage_replacements_provider = "coverage_replacements_provider",
@@ -48,7 +49,10 @@ common_attrs_for_plugin_bootstrapping = {
default = False,
mandatory = False,
),
}
} | ({
"add_exports": attr.string_list(),
"add_opens": attr.string_list(),
} if bazel_features.java.java_info_constructor_module_flags else {})

common_attrs = {}

8 changes: 8 additions & 0 deletions scala/private/macros/scala_repositories.bzl
Original file line number Diff line number Diff line change
@@ -92,6 +92,14 @@ def rules_scala_setup(scala_compiler_srcjar = None):
],
)

if not native.existing_rule("bazel_features"):
http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

if not native.existing_rule("rules_cc"):
http_archive(
name = "rules_cc",
10 changes: 9 additions & 1 deletion scala/private/phases/phase_compile.bzl
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#
# DOCUMENT THIS
#
load("@bazel_features//:features.bzl", "bazel_features")
load(
"@io_bazel_rules_scala//scala/private:paths.bzl",
_get_files_with_extension = "get_files_with_extension",
@@ -298,7 +299,7 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
runtime_deps = []
if hasattr(ctx.attr, "runtime_deps"):
runtime_deps = [dep[JavaInfo] for dep in ctx.attr.runtime_deps]
return JavaInfo(
kwargs = dict(
output_jar = ctx.outputs.jar,
compile_jar = ijar,
source_jar = source_jar,
@@ -308,6 +309,13 @@ def _create_scala_compilation_provider(ctx, ijar, source_jar, deps_providers):
neverlink = ctx.attr.neverlink,
)

# The JavaInfo constructor's add_exports and add_opens flags were added in Bazel 7:
# https://github.com/bazelbuild/bazel/issues/20033
if bazel_features.java.java_info_constructor_module_flags:
kwargs["add_exports"] = getattr(ctx.attr, "add_exports", [])
kwargs["add_opens"] = getattr(ctx.attr, "add_opens", [])
return JavaInfo(**kwargs)

def _pack_source_jar(ctx, scala_srcs, input_srcjars):
# https://github.com/bazelbuild/bazel/blob/ff6c0333e4f957bb9f7ab5401b01dbf3e9b515b1/src/main/java/com/google/devtools/build/lib/rules/java/JavaInfoBuildHelper.java#L180-L183
# java_common.pack_sources checks for no srcs and only a single input jar
9 changes: 9 additions & 0 deletions scala/private/phases/phase_write_executable.bzl
Original file line number Diff line number Diff line change
@@ -73,6 +73,15 @@ def _phase_write_executable(
executable = p.declare_executable.executable
wrapper = p.java_wrapper

# Add --add-exports / --add-opens flags
merged_java_info = p.compile.merged_provider
add_exports = merged_java_info.module_flags_info.add_exports
add_opens = merged_java_info.module_flags_info.add_opens
if add_exports or add_opens:
jvm_flags = list(jvm_flags)
jvm_flags += ["--add-exports=%s=ALL-UNNAMED" % x for x in add_exports.to_list()]
jvm_flags += ["--add-opens=%s=ALL-UNNAMED" % x for x in add_opens.to_list()]

if (is_windows(ctx)):
return _write_executable_windows(ctx, executable, rjars, main_class, jvm_flags, wrapper, use_jacoco)
else:
15 changes: 13 additions & 2 deletions scala/scala_import.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
load("@bazel_features//:features.bzl", "bazel_features")
load("@io_bazel_rules_scala//scala:jars_to_labels.bzl", "JarsToLabelsInfo")
load("//scala/settings:stamp_settings.bzl", "StampScalaImport")
load(
@@ -68,7 +69,7 @@ def _scala_import_impl(ctx):
]

def _new_java_info(ctx, jar, stamped_jar):
return JavaInfo(
kwargs = dict(
output_jar = jar,
compile_jar = stamped_jar,
exports = [target[JavaInfo] for target in ctx.attr.exports],
@@ -78,6 +79,13 @@ def _new_java_info(ctx, jar, stamped_jar):
neverlink = ctx.attr.neverlink,
)

# The JavaInfo constructor's add_exports and add_opens flags were added in Bazel 7:
# https://github.com/bazelbuild/bazel/issues/20033
if bazel_features.java.java_info_constructor_module_flags:
kwargs["add_exports"] = ctx.attr.add_exports
kwargs["add_opens"] = ctx.attr.add_opens
return JavaInfo(**kwargs)

def _add_labels_of_current_code_jars(code_jars, label, jars2labels):
for jar in code_jars.to_list():
jars2labels[jar.path] = label
@@ -148,6 +156,9 @@ scala_import = rule(
"java_compile_toolchain": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_toolchain"),
),
},
} | ({
"add_exports": attr.string_list(),
"add_opens": attr.string_list(),
} if bazel_features.java.java_info_constructor_module_flags else {}),
toolchains = ["@bazel_tools//tools/jdk:toolchain_type"],
)
11 changes: 11 additions & 0 deletions test_version/WORKSPACE.template
Original file line number Diff line number Diff line change
@@ -11,6 +11,17 @@ http_archive(
],
)

http_archive(
name = "bazel_features",
sha256 = "d7787da289a7fb497352211ad200ec9f698822a9e0757a4976fd9f713ff372b3",
strip_prefix = "bazel_features-1.9.1",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.9.1/bazel_features-v1.9.1.tar.gz",
)

load("@bazel_features//:deps.bzl", "bazel_features_deps")

bazel_features_deps()

http_archive(
name = "rules_proto",
sha256 = "8e7d59a5b12b233be5652e3d29f42fba01c7cbab09f6b3a8d0a57ed6d1e9a0da",