Skip to content

Commit

Permalink
[javac/kt] Supply the correct system path for j2kt when compiling wit…
Browse files Browse the repository at this point in the history
…h javac.

PiperOrigin-RevId: 696683151
  • Loading branch information
Googler authored and copybara-github committed Nov 15, 2024
1 parent e847872 commit caa0ceb
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions build_defs/internal_do_not_use/j2cl_common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ def _compile(
java_toolchain = _get_java_toolchain(ctx)
jvm_srcs, js_srcs = split_srcs(srcs)

# Heuristic to determine if we need to specify the jre module. Our jre defines --system=none.
jdk_system_set = any([s for s in javac_opts if s.startswith("--system")])

# TODO(b/197211878): Switch to a public API when available.
[jdk_system] = java_toolchain._bootclasspath_info._system_inputs.to_list() if not jdk_system_set else [None]

has_srcs_to_transpile = (jvm_srcs or kt_common_srcs)
has_kotlin_srcs = any([src for src in jvm_srcs if src.extension == "kt"]) or kt_common_srcs

Expand Down Expand Up @@ -87,7 +81,7 @@ def _compile(
_j2cl_transpile(
ctx,
jvm_provider,
jdk_system,
get_jdk_system(java_toolchain, javac_opts),
js_srcs,
output_js,
output_library_info,
Expand Down Expand Up @@ -233,6 +227,18 @@ def _kt_compile(
def _get_java_toolchain(ctx):
return ctx.attr._j2cl_java_toolchain[java_common.JavaToolchainInfo]

def get_jdk_system(java_toolchain, javac_opts):
""" Returns the path to the system module directory.
The path is returned in a single-element list or empty list if compiling the JRE itself.
"""

# Heuristic to determine if we need to specify the jre module. Our jre defines --system=none.
jdk_system_already_set = any([s.startswith("--system") for s in javac_opts])

# TODO(b/197211878): Switch to a public API when available.
return java_toolchain._bootclasspath_info._system_inputs.to_list() if not jdk_system_already_set else []

def _strip_incompatible_annotation(ctx, name, java_srcs, mnemonic, strip_annotation):
# Paths are matched by Kythe to identify generated J2CL sources.
output_file = ctx.actions.declare_file(name + "_j2cl_stripped-src.jar")
Expand Down Expand Up @@ -298,9 +304,7 @@ def _j2cl_transpile(
args.use_param_file("@%s", use_always = True)
args.set_param_file_format("multiline")
args.add_joined("-classpath", classpath, join_with = ctx.configuration.host_path_separator)

if jdk_system:
args.add("-system", jdk_system.path)
args.add_all("-system", jdk_system, expand_directories = False)

# Explicitly format this as Bazel target labels can start with a @, which
# can be misinterpreted as a flag file to load.
Expand Down Expand Up @@ -338,7 +342,7 @@ def _j2cl_transpile(
# kt_common_srcs are not read by the transpiler as they are already
# included in the srcjars of srcs. However, params.add_all requires them
# to be inputs in order to be properly expanded out into params.
inputs = depset(srcs + kt_common_srcs + ([jdk_system] if jdk_system else []), transitive = [classpath]),
inputs = depset(srcs + kt_common_srcs + jdk_system, transitive = [classpath]),
outputs = [output_dir, library_info_output],
executable = j2cl_transpiler_override or ctx.executable._j2cl_transpiler,
arguments = [args],
Expand Down

0 comments on commit caa0ceb

Please sign in to comment.