Bootstrapping a java_runtime

14 views
Skip to first unread message

Jonathan Perry

unread,
Jun 24, 2024, 11:56:18 AM (5 days ago) Jun 24
to bazel-discuss
Hello all

I am trying to bootstrap a java_runtime with an extra jar included on the ext class path. So I think I need to:

- download and define a raw runtime
- define a toolchain based on that, with an exec_compatible_with (and/or target_compatible_with? Bit unclear to me) constraint to indicate it doesn’t have the jar (let’s call that no_jar_constraint)
- build the jar to include under ext
- define a new runtime with that jar (which is actually pretty tricky, I don’t really grok how it’s doing its run files etc, but let’s leave that problem for a second)
- define a toolchain based on that
- make the final toolchain have a constraint to indicate it does have the jar (with_jar_constraint again unclear whether I’m at exec or target constraints)
- in bazelrc make the host_platform point at a platform that includes the with_jar_constraint, and the —platforms similarly point at the with_jar platform
- put the jar to include under a transition that changes host_platform and platforms command line args

Is that correct? I’m in a bit of a maze here and want to check I’m anywhere near the right path. Obvs if someone happens to have a working example of something vaguely adjacent to this I’d be over the moon…

Thank you!

Jonathan Perry

unread,
Jun 25, 2024, 8:48:31 AM (4 days ago) Jun 25
to bazel-discuss
Ok so the answer was basically ‘yes’ with the caveat the I had to disentangle exec_compatible_with and target[compatible_with (I mostly wanted exec as I was trying to control the runtime of the bazel actions).  So that’s exciting!
Now I’m stuck trying to actually create a java_runtime from the output of the custom rule. My custom rule is copying the various parts of a jvm into the right output layout (I.e. my_root/bin, my_root/include, etc), plus its placing the output jars into the right place. However  can’t figure out what providers to return to make it so that consumers of the tool chain can actually find my files - nothing from defaultinfo’s files or runfiles turns up in the exec root of downstream consuming rules as far as I can tell…
Again if any kind would happens to have an example of this kind of thing I would be ever so grateful!

Jonathan Perry

unread,
Jun 26, 2024, 3:07:58 AM (4 days ago) Jun 26
to bazel-discuss
Anyone please? My current read of the java_runtime code seems to indicate that it only allows you to point at files in the source tree, nothing generated. Is that true?

Fabian Meumertzheim

unread,
Jun 26, 2024, 1:44:09 PM (3 days ago) Jun 26
to Jonathan Perry, bazel-discuss
You can pass a filegroup (or anything with DefaultInfo files) of generated files to java_runtime's srcs attribute. Did you try that?

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/CAPDgU35o2gbQwfLKkTd4N35QNvhR6U%3DLhc4zXnUOuiR5z7KwTQ%40mail.gmail.com.

Jonathan Perry

unread,
Jun 26, 2024, 2:49:42 PM (3 days ago) Jun 26
to Fabian Meumertzheim, bazel-discuss
Yes. A few problems with that -
 - I cannot for the life of me find the right value for java_home to make, say, bin/javac resolve. 
 - as experiments I’ve tried handcrafting ctx.actions.run_shell invocations of ‘find . > ‘ to a file, and inspecting the output, and I can’t figure out which thing to pass to the tools/inputs param to replicate what java_library/binary do
 - if I run with sandbox_debug and verbose_failures, I can’t see any of my custom rule outputs anywhere under the exec root (or even around it). By contrast in a repo rule-generated java_runtime the entire repo gets copied verbatim to external/<repo_name> directly under the exec root
 - since my java_runtime is generated under a transition, I think it will anyway land under some kind of ST-<hash> directory, so hardcoding that would be very sad

I also tried making my custom rule be executable, pointing at its bin/java, which inferred the right java_home (yay!) but only allowed a single_file, so couldn’t actually return all of the jdk (boo!)

I’m very lost, and I can’t find an example of anyone constructing a Java_runtime except in a repo rule, so I very much fear I’m off the edge of the possible.

Jonathan Perry

unread,
Jun 28, 2024, 4:40:10 AM (yesterday) Jun 28
to Fabian Meumertzheim, bazel-discuss
Well I've made a PR, https://github.com/bazelbuild/bazel/pull/22914, whose tests are failing for inscrutable reasons :(

Reply all
Reply to author
Forward
0 new messages