Skip to content
Closed
Show file tree
Hide file tree
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
15 changes: 12 additions & 3 deletions .bazelci/presubmit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ test_target_integration: &test_target_integration
flags_workspace_integration: &flags_workspace_integration
- "--noenable_bzlmod"
- "--enable_workspace"
- "--repositories_without_autoloads=bazel_features_version,bazel_features_globals"

buildifier: latest

Expand All @@ -58,25 +59,33 @@ tasks:
bazel: "7.6.1"
platform: ${{ all_platforms }}
build_targets: *build_targets
build_flags:
- "--build_tag_filters=-min_bazel_8,-min_bazel_9"
test_targets: *test_targets
test_flags:
- "--test_tag_filters=-min_bazel_8"
# Bazel 8+
- "--test_tag_filters=-min_bazel_8,-min_bazel_9"
# Bazel 8.x
build_and_test:
name: "Bazel {modern_bazel}"
bazel: ${{ modern_bazel }}
platform: ${{ all_platforms }}
build_targets: *build_targets
build_flags:
- "--build_tag_filters=-min_bazel_9"
test_targets: *test_targets
test_flags:
- "--test_tag_filters=-min_bazel_9"
# Bazel 6.x
build_and_test_bazel6:
name: "Bazel 6.5.0"
bazel: 6.5.0
platform: ${{ all_platforms }}
build_targets: *build_targets_bazel6
build_flags:
- "--build_tag_filters=-min_bazel_7,-min_bazel_8,-min_bazel_9"
test_targets: *test_targets_bazel6
test_flags:
- "--test_tag_filters=-min_bazel_7,-min_bazel_8"
- "--test_tag_filters=-min_bazel_7,-min_bazel_8,-min_bazel_9"
ubuntu2004_integration_bazel6:
name: "Integration w/ Bazel 6.5.0"
bazel: 6.5.0
Expand Down
9 changes: 1 addition & 8 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ module(

bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.0.15")
bazel_dep(name = "bazel_features", version = "1.28.0")
archive_override(
module_name = "bazel_features",
integrity = "sha256-SOPLvKDy+RN7GHKN8eFjQ+58Wx4Isj+vcXoUluBqxLo=",
strip_prefix = "bazel_features-59915eb2ca215c7b2266c83c49bb7522a5b6737f",
urls = ["https://github.com/bazel-contrib/bazel_features/archive/59915eb2ca215c7b2266c83c49bb7522a5b6737f.zip"],
)

bazel_dep(name = "bazel_features", version = "1.30.0")
bazel_dep(name = "bazel_skylib", version = "1.6.1")
bazel_dep(name = "protobuf", version = "27.0", repo_name = "com_google_protobuf")
bazel_dep(name = "zlib", version = "1.3.1.bcr.5")
Expand Down
15 changes: 4 additions & 11 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ load("//java:rules_java_deps.bzl", "rules_java_dependencies")

rules_java_dependencies()

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

bazel_features_deps()

load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility

proto_bazel_features(name = "proto_bazel_features")
Expand All @@ -63,14 +67,3 @@ http_archive(
load("//test:repositories.bzl", "test_repositories")

test_repositories()

http_archive(
name = "bazel_features",
sha256 = "48e3cbbca0f2f9137b18728df1e16343ee7c5b1e08b23faf717a1496e06ac4ba",
strip_prefix = "bazel_features-59915eb2ca215c7b2266c83c49bb7522a5b6737f",
url = "https://github.com/bazel-contrib/bazel_features/archive/59915eb2ca215c7b2266c83c49bb7522a5b6737f.zip",
)

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

bazel_features_deps()
13 changes: 13 additions & 0 deletions distro/relnotes.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ bazel_dep(name = "rules_java", version = "{VERSION}")
~~~

**WORKSPACE setup**

With Bazel 8.0.0 and before 8.3.0, add the following to your `.bazelrc` file:

~~~
# https://github.com/bazelbuild/bazel/pull/26119
common --repositories_without_autoloads=bazel_features_version,bazel_features_globals
~~~

In all cases, add the following to your `WORKSPACE` file:

~~~
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
Expand All @@ -31,6 +41,9 @@ http_archive(
load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")
rules_java_dependencies()

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

# note that the following line is what is minimally required from protobuf for the java rules
# consider using the protobuf_deps() public API from @com_google_protobuf//:protobuf_deps.bzl
load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility
Expand Down
20 changes: 17 additions & 3 deletions java/bazel/rules/bazel_java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.
"""Bazel java_binary rule"""

load("@bazel_features//:features.bzl", "bazel_features")
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@rules_cc//cc:find_cc_toolchain.bzl", "use_cc_toolchain")
load("//java/common:java_semantics.bzl", "semantics")
Expand Down Expand Up @@ -202,6 +203,14 @@ def _get_executable(ctx):

return ctx.actions.declare_file(executable_name)

_LAUNCHER_MAKER_TOOLCHAIN_TYPE = "@bazel_tools//tools/launcher:launcher_maker_toolchain_type"
_LAUNCHER_MAKER_TOOLCHAIN = config_common.toolchain_type(_LAUNCHER_MAKER_TOOLCHAIN_TYPE, mandatory = True)

def _find_launcher_maker(ctx):
if bazel_features.rules._has_launcher_maker_toolchain:
return ctx.toolchains[_LAUNCHER_MAKER_TOOLCHAIN_TYPE].binary
return ctx.executable._windows_launcher_maker

def _create_stub(ctx, java_attrs, launcher, executable, jvm_flags, main_class, coverage_main_class):
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
java_executable = helper.get_java_executable(ctx, java_runtime_toolchain, launcher)
Expand Down Expand Up @@ -282,11 +291,12 @@ def _create_windows_exe_launcher(ctx, java_executable, classpath, main_class, jv
# TODO(b/295221112): Change to use the "launcher" attribute (only windows use a fixed _launcher attribute)
launcher_artifact = ctx.executable._launcher
ctx.actions.run(
executable = ctx.executable._windows_launcher_maker,
executable = _find_launcher_maker(ctx),
inputs = [launcher_artifact],
outputs = [executable],
arguments = [launcher_artifact.path, launch_info, executable.path],
use_default_shell_env = True,
toolchain = _LAUNCHER_MAKER_TOOLCHAIN_TYPE if bazel_features.rules._has_launcher_maker_toolchain else None,
)
return executable

Expand All @@ -308,6 +318,8 @@ def make_binary_rule(implementation, *, doc, attrs, executable = False, test = F
provides = [JavaInfo],
toolchains = [semantics.JAVA_TOOLCHAIN] + use_cc_toolchain() + (
[semantics.JAVA_RUNTIME_TOOLCHAIN] if executable or test else []
) + (
[_LAUNCHER_MAKER_TOOLCHAIN] if bazel_features.rules._has_launcher_maker_toolchain else []
),
# TODO(hvd): replace with filegroups?
outputs = {
Expand Down Expand Up @@ -340,16 +352,18 @@ logic as the Java package of source files. For example, a source file at
),
"_test_support": attr.label(default = _compute_test_support),
"_launcher": attr.label(
cfg = "exec",
cfg = "target",
executable = True,
default = "@bazel_tools//tools/launcher:launcher",
),
},
{
"_windows_launcher_maker": attr.label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),
},
} if not bazel_features.rules._has_launcher_maker_toolchain else {},
)

def make_java_binary(executable):
Expand Down
10 changes: 10 additions & 0 deletions java/rules_java_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ def rules_license_repo():
],
)

def bazel_features_repo():
maybe(
http_archive,
name = "bazel_features",
sha256 = "a660027f5a87f13224ab54b8dc6e191693c554f2692fcca46e8e29ee7dabc43b",
strip_prefix = "bazel_features-1.30.0",
url = "https://github.com/bazel-contrib/bazel_features/releases/download/v1.30.0/bazel_features-v1.30.0.tar.gz",
)

def rules_java_dependencies():
"""An utility method to load non-toolchain dependencies of rules_java.

Expand All @@ -223,3 +232,4 @@ def rules_java_dependencies():
zlib_repo()
absl_repo()
rules_license_repo()
bazel_features_repo()
40 changes: 40 additions & 0 deletions test/java/common/rules/java_binary_tests.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,45 @@ def _test_java_binary_propagates_direct_native_libraries_impl(env, target):
matching.str_matches("-Djava.library.path=${JAVA_RUNFILES}/*/test_java_binary_propagates_direct_native_libraries"),
)

def _test_java_binary_cross_compilation_to_unix(name):
# A Unix platform that:
# - has a JDK
# - does not require a launcher
# - is not supported by the default C++ toolchain
util.helper_target(
native.platform,
name = name + "/platform",
constraint_values = [
"@platforms//os:linux",
"@platforms//cpu:s390x",
],
)

util.helper_target(
java_binary,
name = name + "/bin",
srcs = ["java/C.java"],
main_class = "C",
)

analysis_test(
name = name,
impl = _test_java_binary_cross_compilation_to_unix_impl,
target = name + "/bin",
config_settings = {
"//command_line_option:platforms": [Label(name + "/platform")],
},
# Requires the launcher_maker toolchain.
attr_values = {"tags": ["min_bazel_9"]},
)

def _test_java_binary_cross_compilation_to_unix_impl(env, target):
# The main assertion is that analysis succeeds, but verify the absence of a
# binary launcher for good measure.
executable = target[DefaultInfo].files_to_run.executable.short_path
assert_action = env.expect.that_target(target).action_generating(executable)
assert_action.substitutions().keys().contains("%jvm_flags%")

def java_binary_tests(name):
test_suite(
name = name,
Expand All @@ -179,5 +218,6 @@ def java_binary_tests(name):
_test_stamp_conversion_does_not_override_int,
_test_java_binary_attributes,
_test_java_binary_propagates_direct_native_libraries,
_test_java_binary_cross_compilation_to_unix,
],
)
4 changes: 4 additions & 0 deletions test/repo/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ load("@rules_java//java:rules_java_deps.bzl", "rules_java_dependencies")

rules_java_dependencies()

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

bazel_features_deps()

load("@com_google_protobuf//bazel/private:proto_bazel_features.bzl", "proto_bazel_features") # buildifier: disable=bzl-visibility

proto_bazel_features(name = "proto_bazel_features")
Expand Down