Skip to content

Commit

Permalink
Obtain bootstrap class path from dedicated runtime toolchain type
Browse files Browse the repository at this point in the history
This decouples the concepts of a Java runtime used to run on the target
platform from the Java runtime that provides the bootstrap class path
used during compilation.

The former needs constraints on the target platform, whereas the latter
should not have any constraints on the target platform to allow for
cross-compilation to target platforms for which a JDK runtime is not
available (e.g. Android).

Work towards #17085
Work towards #64
Split off from #18262
  • Loading branch information
fmeum committed Jun 9, 2023
1 parent b9a576a commit cb6dfa4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
14 changes: 12 additions & 2 deletions toolchains/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ filegroup(
# TODO: migrate away from using @bazel_tools//tools/jdk:runtime_toolchain_type ?
# toolchain_type(name = "runtime_toolchain_type")

# The Java runtime to extract the bootclasspath from that is then used to compile Java sources.
#
# As the bootclasspath is platform independent, toolchains of this type may have no constraints.
# Purely as an optimization to prevent unnecessary fetches of remote runtimes for other
# architectures, toolchains of this type may have constraints on the execution platform that match
# those on the corresponding compilation toolchain.
#
# Toolchains of this type are only consumed internally by the bootclasspath rule and should not be
# accessed from Starlark.
toolchain_type(name = "bootstrap_runtime_toolchain_type")

# Points to toolchain[":runtime_toolchain_type"] (was :legacy_current_java_runtime)
java_runtime_alias(name = "current_java_runtime")

Expand Down Expand Up @@ -201,8 +212,7 @@ alias(
bootclasspath(
name = "platformclasspath",
src = "DumpPlatformClassPath.java",
host_javabase = ":current_java_runtime",
target_javabase = ":current_java_runtime",
java_runtime_alias = ":current_java_runtime",
)

default_java_toolchain(
Expand Down
27 changes: 13 additions & 14 deletions toolchains/default_java_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ def java_runtime_files(name, srcs):
tags = ["manual"],
)

_JAVA_BOOTSTRAP_RUNTIME_TOOLCHAIN_TYPE = Label("@bazel_tools//tools/jdk:bootstrap_runtime_toolchain_type")

def _bootclasspath_impl(ctx):
host_javabase = ctx.attr.host_javabase[java_common.JavaRuntimeInfo]
exec_javabase = ctx.attr.java_runtime_alias[java_common.JavaRuntimeInfo]

class_dir = ctx.actions.declare_directory("%s_classes" % ctx.label.name)

Expand All @@ -219,17 +221,15 @@ def _bootclasspath_impl(ctx):
args.add(ctx.file.src)

ctx.actions.run(
executable = "%s/bin/javac" % host_javabase.java_home,
executable = "%s/bin/javac" % exec_javabase.java_home,
mnemonic = "JavaToolchainCompileClasses",
inputs = [ctx.file.src] + ctx.files.host_javabase,
inputs = [ctx.file.src] + ctx.files.java_runtime_alias,
outputs = [class_dir],
arguments = [args],
)

bootclasspath = ctx.outputs.output_jar

inputs = [class_dir] + ctx.files.host_javabase

args = ctx.actions.args()
args.add("-XX:+IgnoreUnrecognizedVMOptions")
args.add("--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED")
Expand All @@ -239,16 +239,17 @@ def _bootclasspath_impl(ctx):
args.add("DumpPlatformClassPath")
args.add(bootclasspath)

any_javabase = ctx.toolchains[_JAVA_BOOTSTRAP_RUNTIME_TOOLCHAIN_TYPE].java_runtime
args.add(any_javabase.java_home)

system_files = ("release", "modules", "jrt-fs.jar")
system = [f for f in ctx.files.target_javabase if f.basename in system_files]
system = [f for f in any_javabase.files.to_list() if f.basename in system_files]
if len(system) != len(system_files):
system = None
if ctx.attr.target_javabase:
inputs.extend(ctx.files.target_javabase)
args.add(ctx.attr.target_javabase[java_common.JavaRuntimeInfo].java_home)

inputs = depset([class_dir] + ctx.files.java_runtime_alias, transitive = [any_javabase.files])
ctx.actions.run(
executable = str(host_javabase.java_executable_exec_path),
executable = str(exec_javabase.java_executable_exec_path),
mnemonic = "JavaToolchainCompileBootClasspath",
inputs = inputs,
outputs = [bootclasspath],
Expand All @@ -266,7 +267,7 @@ def _bootclasspath_impl(ctx):
_bootclasspath = rule(
implementation = _bootclasspath_impl,
attrs = {
"host_javabase": attr.label(
"java_runtime_alias": attr.label(
cfg = "exec",
providers = [java_common.JavaRuntimeInfo],
),
Expand All @@ -275,10 +276,8 @@ _bootclasspath = rule(
cfg = "exec",
allow_single_file = True,
),
"target_javabase": attr.label(
providers = [java_common.JavaRuntimeInfo],
),
},
toolchains = [_JAVA_BOOTSTRAP_RUNTIME_TOOLCHAIN_TYPE],
)

def bootclasspath(name, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions toolchains/local_java_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ def local_java_runtime(name, java_home, version, runtime_name = None, visibility
toolchain_type = Label("@bazel_tools//tools/jdk:runtime_toolchain_type"),
toolchain = runtime_name,
)
native.toolchain(
name = "bootstrap_runtime_toolchain_definition",
target_settings = [":%s_settings_alias" % name],
toolchain_type = Label("//toolchains:bootstrap_runtime_toolchain_type"),
toolchain = runtime_name,
)

if type(version) == type("") and version.isdigit() and int(version) > 8:
for version in range(8, int(version) + 1):
Expand Down Expand Up @@ -245,6 +251,12 @@ toolchain(
toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
toolchain = ":jdk",
)
toolchain(
name = "bootstrap_runtime_toolchain_definition",
target_settings = [":localjdk_setting"],
toolchain_type = "@rules_java//toolchains:bootstrap_runtime_toolchain_type",
toolchain = ":jdk",
)
'''

_local_java_repository_rule = repository_rule(
Expand Down
10 changes: 10 additions & 0 deletions toolchains/remote_java_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ toolchain(
toolchain_type = "@bazel_tools//tools/jdk:runtime_toolchain_type",
toolchain = "{toolchain}",
)
toolchain(
name = "bootstrap_runtime_toolchain",
# These constraints are not required for correctness, but prevent fetches of remote JDK for
# different architectures. As every Java compilation toolchain depends on a bootstrap runtime in
# the same configuration, this constraint will not result in toolchain resolution failures.
exec_compatible_with = {target_compatible_with},
target_settings = [":version_or_prefix_version_setting"],
toolchain_type = "@rules_java//toolchains:bootstrap_runtime_toolchain_type",
toolchain = "{toolchain}",
)
""".format(
prefix = prefix,
version = version,
Expand Down

0 comments on commit cb6dfa4

Please sign in to comment.