Skip to content

Commit

Permalink
Make Dep Module Monomorphization Use Hermetic Java Toolchain
Browse files Browse the repository at this point in the history
Previously the subprocesses were getting triggered via a direct call to `java -jar ...`
which I've only now realized breaks Claro's efforts to make installing Bazel the only
requirement for getting running as you'd also need to have Java installed locally before
you'd be able to run any Claro programs that made use of generic procedures from dep modles.

This update is a bit of a hack to ensure that supbrocesses are started using the hermetic
Java toolchain provided by Bazel via the `common --java_runtime_version=remotejdk_11`
flag configured in the .bazelrc. This should ensure that anyone who simply runs the
`create_claro_project.sh` script will be setup to run Claro programs without any further
setup necessary.

I got lucky to notice this since obviously on my dev machine I already had Java installed.
  • Loading branch information
JasonSteving99 committed Apr 29, 2024
1 parent 97a97c7 commit 878b557
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
load("//src/java/com/claro:claro_build_rules_internal.bzl", "bootstrapped_claro_module_internal", "bootstrapped_claro_module", "CLARO_BUILTIN_JAVA_DEPS")

# We need to ensure that users don't need to have Java installed at all. So, this hack ensures hermeticity by ensuring that the
# Dep Module Monomorphization subprocesses are invoked using the hermetic Java toolchain, rather than just attempting to directly
# invoke `java -jar ...`. This is certainly a bit of a hack that relies on some very specific Bazel behavior that risks being
# changed in later Bazel versions. So, while this helps address a problem with Claro's monomorphization, I still can't wait for
# subprocesses to no longer be required.
genrule(
name = "get_java_path",
outs = ["java_path.txt"],
cmd = "realpath $(JAVA) > $(OUTS)",
toolchains = ["@bazel_tools//tools/jdk:current_java_runtime"],
)

genrule(
name = "monomorphization_coordinator_with_bazel_java",
srcs = ["java_path.txt", "MonomorphizationCoordinator.tmpl.java"],
outs = ["MonomorphizationCoordinator.java"],
cmd = """
sed "s#{{BAZEL_JAVA_PATH}}#$$(<$(location java_path.txt))#g" $(location MonomorphizationCoordinator.tmpl.java) > $(OUTS)
"""
)

java_library(
name = "monomorphization_coordinator",
srcs = ["MonomorphizationCoordinator.java"],
Expand Down Expand Up @@ -86,4 +107,4 @@ java_binary(
visibility = [
"//src/java/com/claro:__pkg__",
]
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private static ListenableFuture<DepModuleMonomorphizationService> getDepModuleMo
}
ProcessBuilder depModuleSubprocess =
new ProcessBuilder()
.command("java", "-jar", DEP_MODULE_MONOMORPHIZATION_SUBPROCESS_BINARY_PATH,
// .command("/private/var/tmp/_bazel_jasonsteving/895b6e2a5639ccc5f59fc3ca15e75bfa/external/rules_java~5.5.1~toolchains~remotejdk11_macos_aarch64/zulu-11.jdk/Contents/Home/bin/java", "-jar", DEP_MODULE_MONOMORPHIZATION_SUBPROCESS_BINARY_PATH,
.command("{{BAZEL_JAVA_PATH}}", "-jar", DEP_MODULE_MONOMORPHIZATION_SUBPROCESS_BINARY_PATH,
"--coordinator_port", String.valueOf(coordinatorPort),
"--dep_module_file_path", DEP_GRAPH_CLARO_MODULE_PATHS_BY_UNIQUE_MODULE_NAME.get(uniqueModuleName),
// TODO(steving) DELETE THIS, The uniqe name should be looked up in the .claro_module.
Expand All @@ -190,7 +191,7 @@ private static ListenableFuture<DepModuleMonomorphizationService> getDepModuleMo
.redirectOutput(ProcessBuilder.Redirect.INHERIT)
.redirectError(ProcessBuilder.Redirect.INHERIT);
depModuleSubprocess.environment().putAll(RUNFILES_ENV_VARS);
depModuleSubprocess.start();
depModuleSubprocess.start();
} catch (IOException e) {
throw new RuntimeException(
"Internal Compiler Error! Unable to start dep module monomorphization subprocess for module: " +
Expand Down

0 comments on commit 878b557

Please sign in to comment.