Skip to content

Commit

Permalink
Remove runtime dependency of non-executable java_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed May 2, 2023
1 parent 8f1f41c commit c8aa335
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,9 @@ def _make_binary_rule(implementation, attrs, executable = False, test = False):
test = test,
fragments = ["cpp", "java"],
provides = [JavaInfo],
toolchains = [semantics.JAVA_TOOLCHAIN, semantics.JAVA_RUNTIME_TOOLCHAIN] + cc_helper.use_cpp_toolchain(),
toolchains = [semantics.JAVA_TOOLCHAIN] + cc_helper.use_cpp_toolchain() + (
[semantics.JAVA_RUNTIME_TOOLCHAIN] if executable or test else []
),
# TODO(hvd): replace with filegroups?
outputs = {
"classjar": "%{name}.jar",
Expand Down Expand Up @@ -307,6 +309,7 @@ def make_java_binary(executable, resolve_launcher_flag, has_launcher = False):
"args": attr.string_list(),
"output_licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
}),
remove_attrs = [] if executable else ["_java_runtime_toolchain_type"],
),
executable = executable,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TODO: Remove on import. This file only exists to indicate the changes that have to be made to the
# internal version of java_semantics.bzl.

def _get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
*,
executable,
feature_config,
is_absolute_path):
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)

# Add symlinks to the C++ runtime libraries under a path that can be built
# into the Java binary without having to embed the crosstool, gcc, and grte
# version information contained within the libraries' package paths.
runfiles_symlinks = {}

if not is_absolute_path(ctx, java_runtime_toolchain.java_home):
runfiles_symlinks = {
("_cpp_runtimes/%s" % lib.basename): lib
for lib in cc_helper.find_cpp_toolchain(ctx).dynamic_runtime_lib(
feature_configuration = feature_config,
).to_list()
}

return [java_runtime_toolchain.files], runfiles_symlinks

semantics = struct(
get_java_runtime_dependent_runfiles_and_symlinks = _get_java_runtime_dependent_runfiles_and_symlinks,
)
28 changes: 9 additions & 19 deletions src/main/starlark/builtins_bzl/common/java/java_binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -211,32 +211,22 @@ def basic_java_binary(

files = depset(files_to_build + common_info.files_to_build)

java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
runtime_runfiles, runtime_symlinks = semantics.get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
executable = executable,
feature_config = feature_config,
is_absolute_path = helper.is_absolute_path,
)
transitive_runfiles_artifacts = depset(transitive = [
files,
java_attrs.runtime_classpath,
depset(transitive = launcher_info.runfiles),
java_runtime_toolchain.files,
])

# Add symlinks to the C++ runtime libraries under a path that can be built
# into the Java binary without having to embed the crosstool, gcc, and grte
# version information contained within the libraries' package paths.
runfiles_symlinks = {}

# TODO(hvd): do we need this in bazel? if yes, fix abs path check on windows
if not helper.is_absolute_path(ctx, java_runtime_toolchain.java_home):
runfiles_symlinks = {
("_cpp_runtimes/%s" % lib.basename): lib
for lib in cc_helper.find_cpp_toolchain(ctx).dynamic_runtime_lib(
feature_configuration = feature_config,
).to_list()
}
] + runtime_runfiles)

runfiles = ctx.runfiles(
transitive_files = transitive_runfiles_artifacts,
collect_default = True,
symlinks = runfiles_symlinks,
symlinks = runtime_symlinks,
skip_conflict_checking = True,
)

Expand Down Expand Up @@ -348,10 +338,10 @@ def _generate_coverage_manifest(ctx, output, runtime_classpath):

#TODO(hvd): not needed in bazel
def _create_shared_archive(ctx, java_attrs):
runtime = semantics.find_java_runtime_toolchain(ctx)
classlist = ctx.file.classlist if hasattr(ctx.file, "classlist") else None
if not classlist:
return None
runtime = semantics.find_java_runtime_toolchain(ctx)
jsa = ctx.actions.declare_file("%s.jsa" % ctx.label.name)
merged = ctx.actions.declare_file(jsa.dirname + "/" + helper.strip_extension(jsa) + "-merged.jar")
create_single_jar(
Expand Down
12 changes: 12 additions & 0 deletions src/main/starlark/builtins_bzl/common/java/java_semantics.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ def _stamping_enabled(ctx, stamp):
def _get_build_info(ctx, stamp):
return java_common.get_build_info(ctx, _stamping_enabled(ctx, stamp))

def _get_java_runtime_dependent_runfiles_and_symlinks(
ctx,
*,
executable,
feature_config,
is_absolute_path):
if not executable:
return [], {}
java_runtime_toolchain = semantics.find_java_runtime_toolchain(ctx)
return [java_runtime_toolchain.files], {}

semantics = struct(
JAVA_TOOLCHAIN_LABEL = "@bazel_tools//tools/jdk:current_java_toolchain",
JAVA_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:toolchain_type",
Expand All @@ -62,6 +73,7 @@ semantics = struct(
JAVA_RUNTIME_TOOLCHAIN_TYPE = "@bazel_tools//tools/jdk:runtime_toolchain_type",
JAVA_RUNTIME_TOOLCHAIN = _builtins.toplevel.config_common.toolchain_type("@bazel_tools//tools/jdk:runtime_toolchain_type", mandatory = True),
find_java_runtime_toolchain = _find_java_runtime_toolchain,
get_java_runtime_dependent_runfiles_and_symlinks = _get_java_runtime_dependent_runfiles_and_symlinks,
JAVA_PLUGINS_FLAG_ALIAS_LABEL = "@bazel_tools//tools/jdk:java_plugins_flag_alias",
EXTRA_SRCS_TYPES = [],
ALLOWED_RULES_IN_DEPS = [
Expand Down
48 changes: 48 additions & 0 deletions src/test/shell/bazel/bazel_java_test_defaults.sh
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,54 @@ EOF
//pkg:foo &>"$TEST_log" || fail "Build should succeed"
}

function test_non_executable_java_binary_compiles_for_any_platform_with_local_jdk() {
mkdir -p pkg
cat > pkg/BUILD.bazel <<'EOF'
platform(name = "exotic_platform")
java_binary(
name = "foo",
srcs = ["Foo.java"],
create_executable = False,
)
EOF

cat > pkg/Foo.java <<'EOF'
package com.example;
public class Foo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF

bazel build --platforms=//pkg:exotic_platform --java_runtime_version=local_jdk \
//pkg:foo &>"$TEST_log" || fail "Build should succeed"
}

function test_non_executable_java_binary_compiles_for_any_platform_with_remote_jdk() {
mkdir -p pkg
cat > pkg/BUILD.bazel <<'EOF'
platform(name = "exotic_platform")
java_binary(
name = "foo",
srcs = ["Foo.java"],
create_executable = False,
)
EOF

cat > pkg/Foo.java <<'EOF'
package com.example;
public class Foo {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
EOF

bazel build --platforms=//pkg:exotic_platform --java_runtime_version=remotejdk_11 \
//pkg:foo &>"$TEST_log" || fail "Build should succeed"
}

function test_executable_java_binary_fails_without_runtime_with_local_jdk() {
mkdir -p pkg
cat > pkg/BUILD.bazel <<'EOF'
Expand Down

0 comments on commit c8aa335

Please sign in to comment.