Skip to content

Commit 4a29f08

Browse files
fmeumcopybara-github
authored andcommitted
Pass ct.sym to direct version of Turbine
This ensures that Turbine has access to `ct.sym` and thus supports the `--release` flag even if it is compiled into a Graal native image, which doesn't have access to a `JAVA_HOME`. This requires exposing the `ct.sym` file on a new `lib_ct_sym` attribute of `java_runtime` and passing it into Turbine via the `turbine.ctSymPath` system property introduced in v0.3.1. Along the way this commit fixes the setup code for testing unreleased versions of the remote Java and coverage tools, which silently broke with the flip of `--enable_bzlmod`. Work towards bazelbuild/stardoc#195 Closes bazelbuild#20294. PiperOrigin-RevId: 585866870 Change-Id: I416b787e324bd3a01e223edbda4f9ba137c21241
1 parent b2cca31 commit 4a29f08

15 files changed

Lines changed: 272 additions & 201 deletions

File tree

MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ maven.install(
9595
"com.google.http-client:google-http-client-gson:1.42.0",
9696
"com.google.http-client:google-http-client:1.42.0",
9797
"com.google.j2objc:j2objc-annotations:1.3",
98-
"com.google.turbine:turbine:0.3.0",
98+
"com.google.turbine:turbine:0.3.1",
9999
"com.ryanharter.auto.value:auto-value-gson-extension:1.3.1",
100100
"com.ryanharter.auto.value:auto-value-gson-runtime:1.3.1",
101101
"com.ryanharter.auto.value:auto-value-gson-factory:1.3.1",

MODULE.bazel.lock

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

maven_install.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"__AUTOGENERATED_FILE_DO_NOT_MODIFY_THIS_FILE_MANUALLY": "THERE_IS_NO_DATA_ONLY_ZUUL",
3-
"__INPUT_ARTIFACTS_HASH": -922870577,
4-
"__RESOLVED_ARTIFACTS_HASH": 1291237234,
3+
"__INPUT_ARTIFACTS_HASH": 143984014,
4+
"__RESOLVED_ARTIFACTS_HASH": -866086153,
55
"conflict_resolution": {
66
"com.google.code.gson:gson:2.8.9": "com.google.code.gson:gson:2.9.0",
77
"com.google.errorprone:error_prone_annotations:2.3.2": "com.google.errorprone:error_prone_annotations:2.23.0",
@@ -278,9 +278,9 @@
278278
},
279279
"com.google.turbine:turbine": {
280280
"shasums": {
281-
"jar": "3fee92239ec300ef4142e2e32ac81af3123cce9cdc552ca78429086ed8c5a445"
281+
"jar": "c0778917005294a59c805a60a5ace2ec847dcece41ced70680f675d822fc03a7"
282282
},
283-
"version": "0.3.0"
283+
"version": "0.3.1"
284284
},
285285
"com.ryanharter.auto.value:auto-value-gson-extension": {
286286
"shasums": {

src/main/java/com/google/devtools/build/lib/rules/java/JavaRuntimeRule.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
5151
Files in the runtime needed for hermetic deployments.
5252
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
5353
.add(attr("hermetic_srcs", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE))
54+
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(lib_ct_sym) -->
55+
The lib/ct.sym file needed for compilation with <code>--release</code>. If not specified and
56+
there is exactly one file in <code>srcs</code> whose path ends with
57+
<code>/lib/ct.sym</code>, that file is used.
58+
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */
59+
.add(
60+
attr("lib_ct_sym", LABEL)
61+
.singleArtifact()
62+
.allowedFileTypes(FileTypeSet.ANY_FILE)
63+
.exec())
5464
/* <!-- #BLAZE_RULE(java_runtime).ATTRIBUTE(lib_modules) -->
5565
The lib/modules file needed for hermetic deployments.
5666
<!-- #END_BLAZE_RULE.ATTRIBUTE --> */

src/main/java/com/google/devtools/build/lib/rules/java/JavaToolchainTool.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ static JavaToolchainTool fromStarlark(@Nullable StructImpl struct) throws RuleEr
6464
public abstract NestedSet<Artifact> data();
6565

6666
/**
67-
* JVM flags to invoke the tool with, or empty if it is not a {@code _deploy.jar}. Location
68-
* expansion is performed on these flags using the inputs in {@link #data}.
67+
* JVM flags to invoke the tool with. Location expansion is performed on these flags using the
68+
* inputs in {@link #data}.
6969
*/
7070
public abstract NestedSet<String> jvmOpts();
7171

@@ -105,7 +105,7 @@ private CustomCommandLine extractCommandLine(JavaToolchainProvider toolchain)
105105

106106
Artifact executable = tool().getExecutable();
107107
if (!executable.getExtension().equals("jar")) {
108-
command.addExecPath(executable);
108+
command = command.addExecPath(executable).addAll(jvmOpts());
109109
} else {
110110
command
111111
.addPath(toolchain.getJavaRuntime().javaBinaryExecPathFragment())

src/main/java/com/google/devtools/build/lib/starlarkbuildapi/java/JavaRuntimeInfoApi.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ public interface JavaRuntimeInfoApi extends StructApi {
8080
structField = true)
8181
Depset starlarkHermeticInputs();
8282

83+
/** The lib/ct.sym file. */
84+
@StarlarkMethod(
85+
name = "lib_ct_sym",
86+
doc = "Returns the lib/ct.sym file.",
87+
structField = true,
88+
allowReturnNones = true)
89+
@Nullable
90+
FileApi libCtSym();
91+
8392
/** The lib/modules file. */
8493
@StarlarkMethod(
8594
name = "lib_modules",

src/main/starlark/builtins_bzl/common/java/java_runtime.bzl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ JavaRuntimeInfo, _new_javaruntimeinfo = provider(
4545
This should only be used when one needs to access the JDK during the execution
4646
of a binary or a test built by Bazel. In particular, when one needs the JDK
4747
during an action, java_home should be used instead.""",
48+
"lib_ct_sym": "Returns the lib/ct.sym file.",
4849
"lib_modules": "Returns the lib/modules file.",
4950
"version": "The Java feature version of the runtime. This is 0 if the version is unknown.",
5051
},
@@ -75,6 +76,15 @@ def _get_runfiles_java_executable(java_home, label):
7576
def _is_java_binary(path):
7677
return path.endswith("bin/java") or path.endswith("bin/java.exe")
7778

79+
def _get_lib_ct_sym(srcs, explicit_lib_ct_sym):
80+
if explicit_lib_ct_sym:
81+
return explicit_lib_ct_sym
82+
candidates = [src for src in srcs if src.path.endswith("/lib/ct.sym")]
83+
if len(candidates) == 1:
84+
return candidates[0]
85+
else:
86+
return None
87+
7888
def _java_runtime_rule_impl(ctx):
7989
all_files = [] # [depset[File]]
8090
all_files.append(depset(ctx.files.srcs))
@@ -105,6 +115,7 @@ def _java_runtime_rule_impl(ctx):
105115
hermetic_inputs = depset(ctx.files.hermetic_srcs)
106116
all_files.append(hermetic_inputs)
107117

118+
lib_ct_sym = _get_lib_ct_sym(ctx.files.srcs, ctx.file.lib_ct_sym)
108119
lib_modules = ctx.file.lib_modules
109120
hermetic_static_libs = [dep[CcInfo] for dep in ctx.attr.hermetic_static_libs]
110121

@@ -128,6 +139,7 @@ def _java_runtime_rule_impl(ctx):
128139
java_executable_runfiles_path = java_binary_runfiles_path,
129140
java_home = java_home,
130141
java_home_runfiles_path = java_home_runfiles_path,
142+
lib_ct_sym = lib_ct_sym,
131143
lib_modules = lib_modules,
132144
version = ctx.attr.version,
133145
)
@@ -152,6 +164,7 @@ java_runtime = rule(
152164
"hermetic_static_libs": attr.label_list(providers = [CcInfo]),
153165
"java": attr.label(allow_single_file = True, executable = True, cfg = "target"),
154166
"java_home": attr.string(),
167+
"lib_ct_sym": attr.label(allow_single_file = True),
155168
"lib_modules": attr.label(allow_single_file = True, executable = True, cfg = "target"),
156169
"output_licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
157170
"srcs": attr.label_list(allow_files = True),

src/main/starlark/builtins_bzl/common/java/java_toolchain.bzl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,18 @@ JavaToolchainInfo, _new_javatoolchaininfo = provider(
7878
def _java_toolchain_impl(ctx):
7979
javac_opts_list = _get_javac_opts(ctx)
8080
bootclasspath_info = _get_bootclasspath_info(ctx)
81+
java_runtime = _get_java_runtime(ctx)
82+
if java_runtime and java_runtime.lib_ct_sym:
83+
header_compiler_direct_data = [java_runtime.lib_ct_sym]
84+
header_compiler_direct_jvm_opts = ["-Dturbine.ctSymPath=" + java_runtime.lib_ct_sym.path]
85+
else:
86+
header_compiler_direct_data = []
87+
header_compiler_direct_jvm_opts = []
8188
java_toolchain_info = _new_javatoolchaininfo(
8289
bootclasspath = bootclasspath_info.bootclasspath,
8390
ijar = ctx.attr.ijar.files_to_run if ctx.attr.ijar else None,
8491
jacocorunner = ctx.attr.jacocorunner.files_to_run if ctx.attr.jacocorunner else None,
85-
java_runtime = _get_java_runtime(ctx),
92+
java_runtime = java_runtime,
8693
jvm_opt = depset(_java_common_internal.expand_java_opts(ctx, "jvm_opts", tokenize = False, exec_paths = True)),
8794
label = ctx.label,
8895
proguard_allowlister = ctx.attr.proguard_allowlister.files_to_run if ctx.attr.proguard_allowlister else None,
@@ -100,7 +107,12 @@ def _java_toolchain_impl(ctx):
100107
_gen_class = ctx.file.genclass,
101108
_header_compiler = _get_tool_from_ctx(ctx, "header_compiler", "turbine_data", "turbine_jvm_opts"),
102109
_header_compiler_builtin_processors = depset(ctx.attr.header_compiler_builtin_processors),
103-
_header_compiler_direct = _get_tool_from_executable(ctx, "header_compiler_direct"),
110+
_header_compiler_direct = _get_tool_from_executable(
111+
ctx,
112+
"header_compiler_direct",
113+
data = header_compiler_direct_data,
114+
jvm_opts = header_compiler_direct_jvm_opts,
115+
),
104116
_javabuilder = _get_tool_from_ctx(ctx, "javabuilder", "javabuilder_data", "javabuilder_jvm_opts"),
105117
_javacopts = helper.detokenize_javacopts(javac_opts_list),
106118
_javacopts_list = javac_opts_list,
@@ -174,14 +186,14 @@ def _get_tool_from_ctx(ctx, tool_attr, data_attr, opts_attr):
174186
jvm_opts = depset([ctx.expand_location(opt, data) for opt in getattr(ctx.attr, opts_attr)]),
175187
)
176188

177-
def _get_tool_from_executable(ctx, attr_name):
189+
def _get_tool_from_executable(ctx, attr_name, data = [], jvm_opts = []):
178190
dep = getattr(ctx.attr, attr_name)
179191
if not dep:
180192
return None
181193
files_to_run = dep.files_to_run
182194
if not files_to_run or not files_to_run.executable:
183195
fail(dep.label, "does not refer to a valid executable target")
184-
return struct(tool = files_to_run, data = depset(), jvm_opts = depset())
196+
return struct(tool = files_to_run, data = depset(data), jvm_opts = depset(jvm_opts))
185197

186198
def _get_compatible_javacopts(ctx):
187199
result = {}

src/test/java/com/google/devtools/build/lib/rules/java/JavaStarlarkApiTest.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,6 +3566,97 @@ public void hermeticStaticLibs() throws Exception {
35663566
.containsExactly("a/libStatic");
35673567
}
35683568

3569+
@Test
3570+
public void implicitLibCtSym() throws Exception {
3571+
scratch.file("a/libStatic.a");
3572+
scratch.file(
3573+
"a/BUILD",
3574+
"load(':rule.bzl', 'jrule')",
3575+
"load('"
3576+
+ TestConstants.TOOLS_REPOSITORY
3577+
+ "//tools/jdk:java_toolchain_alias.bzl', 'java_runtime_alias')",
3578+
"java_runtime(",
3579+
" name='jvm',",
3580+
" srcs=[",
3581+
" 'foo/bar/bin/java',",
3582+
" 'foo/bar/lib/ct.sym',",
3583+
" ],",
3584+
" java='foo/bar/bin/java',",
3585+
")",
3586+
"java_runtime_alias(name='alias')",
3587+
"jrule(name='r')",
3588+
"toolchain(",
3589+
" name = 'java_runtime_toolchain',",
3590+
" toolchain = ':jvm',",
3591+
" toolchain_type = '"
3592+
+ TestConstants.TOOLS_REPOSITORY
3593+
+ "//tools/jdk:runtime_toolchain_type',",
3594+
")");
3595+
scratch.file(
3596+
"a/rule.bzl",
3597+
"load('//myinfo:myinfo.bzl', 'MyInfo')",
3598+
"def _impl(ctx):",
3599+
" provider = ctx.attr._java_runtime[java_common.JavaRuntimeInfo]",
3600+
" return MyInfo(",
3601+
" lib_ct_sym = provider.lib_ct_sym,",
3602+
" )",
3603+
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
3604+
3605+
useConfiguration("--extra_toolchains=//a:all");
3606+
ConfiguredTarget ct = getConfiguredTarget("//a:r");
3607+
StructImpl myInfo = getMyInfoFromTarget(ct);
3608+
@SuppressWarnings("unchecked")
3609+
Artifact libCtSym = (Artifact) myInfo.getValue("lib_ct_sym");
3610+
assertThat(libCtSym).isNotNull();
3611+
assertThat(libCtSym.getExecPathString()).isEqualTo("a/foo/bar/lib/ct.sym");
3612+
}
3613+
3614+
@Test
3615+
public void explicitLibCtSym() throws Exception {
3616+
scratch.file("a/libStatic.a");
3617+
scratch.file(
3618+
"a/BUILD",
3619+
"load(':rule.bzl', 'jrule')",
3620+
"load('"
3621+
+ TestConstants.TOOLS_REPOSITORY
3622+
+ "//tools/jdk:java_toolchain_alias.bzl', 'java_runtime_alias')",
3623+
"java_runtime(",
3624+
" name='jvm',",
3625+
" srcs=[",
3626+
" 'foo/bar/bin/java',",
3627+
" 'foo/bar/lib/ct.sym',",
3628+
" ],",
3629+
" java='foo/bar/bin/java',",
3630+
" lib_ct_sym='lib/ct.sym',",
3631+
")",
3632+
"java_runtime_alias(name='alias')",
3633+
"jrule(name='r')",
3634+
"toolchain(",
3635+
" name = 'java_runtime_toolchain',",
3636+
" toolchain = ':jvm',",
3637+
" toolchain_type = '"
3638+
+ TestConstants.TOOLS_REPOSITORY
3639+
+ "//tools/jdk:runtime_toolchain_type',",
3640+
")");
3641+
scratch.file(
3642+
"a/rule.bzl",
3643+
"load('//myinfo:myinfo.bzl', 'MyInfo')",
3644+
"def _impl(ctx):",
3645+
" provider = ctx.attr._java_runtime[java_common.JavaRuntimeInfo]",
3646+
" return MyInfo(",
3647+
" lib_ct_sym = provider.lib_ct_sym,",
3648+
" )",
3649+
"jrule = rule(_impl, attrs = { '_java_runtime': attr.label(default=Label('//a:alias'))})");
3650+
3651+
useConfiguration("--extra_toolchains=//a:all");
3652+
ConfiguredTarget ct = getConfiguredTarget("//a:r");
3653+
StructImpl myInfo = getMyInfoFromTarget(ct);
3654+
@SuppressWarnings("unchecked")
3655+
Artifact libCtSym = (Artifact) myInfo.getValue("lib_ct_sym");
3656+
assertThat(libCtSym).isNotNull();
3657+
assertThat(libCtSym.getExecPathString()).isEqualTo("a/lib/ct.sym");
3658+
}
3659+
35693660
@Test
35703661
@TestParameters({
35713662
"{module: java_config, api: use_ijars}",

0 commit comments

Comments
 (0)