Add docs and test for rendering stardoc without a C++ toolchain#313
Add docs and test for rendering stardoc without a C++ toolchain#313jjmaestro wants to merge 1 commit into
Conversation
Building Stardoc's renderer from source compiles protoc, which forces a C/C++ toolchain onto doc builds. Add: - docs/rendering_without_cc_toolchain.md: why it happens and the per-consumer setup that avoids it, linked from getting_started. - test/no_cc_toolchain: a standalone module (wired into CI like test/bzlmod) that renders Stardoc's own rule doc with no CC toolchain and diff-tests the checked-in Markdown. Fixes bazelbuild#305
| (`@bazel_tools//tools/cpp:toolchain_type`) for its *optional* native | ||
| launcher: rules_java adds | ||
| [`use_cc_toolchain(mandatory = False)`][rules-java-cc] to the rule's | ||
| `toolchains`, so toolchain *resolution* must still succeed even though |
There was a problem hiding this comment.
I'm confused - doesn't the fact that the toolchain requirement is optional mean that resolution doesn't have to succeed?
| exactly why `toolchains_protoc` (now archived) is unnecessary here. | ||
| 2. **`java_binary` needs CC toolchain *resolution* to succeed.** Even with | ||
| nothing C++ left to compile, `java_binary` pulls in the C++ toolchain type | ||
| (`@bazel_tools//tools/cpp:toolchain_type`) for its *optional* native |
There was a problem hiding this comment.
Is this really true? The launcher should come from a separate toolchain, so unless you are cross-compiling for Windows on a non-Windows host, the launcher shouldn't introduce a C++ toolchain requirement.
@fmeum you are right that it's confusing, and also what I wrote is probably wrong and/or have the wrong wording. I'd like to dig deeper into this to really understand what's happening and then fix the wording and the example, because I'm not cross-compiling or anything, without the empty CC toolchain it just fails doing a regular build in Linux for Bazel 7, 8 and 9. Here's some debugging I've done, running the Debugging 7.1.0$ USE_BAZEL_VERSION=7.1.0 \
bazel cquery 'kind("cc_binary rule", deps(//:stardoc_doc))'
Starting local Bazel server and connecting to it...
INFO: Analyzed target //:stardoc_doc (176 packages loaded, 3440 targets configured).
INFO: Found 1 target...
@bazel_tools//src/tools/launcher:launcher_maker (8f31475)
@bazel_tools//src/tools/launcher:launcher (8f31475)
INFO: Elapsed time: 2.943s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions
$ USE_BAZEL_VERSION=7.1.0 \
bazel query "somepath(//:stardoc_doc, @bazel_tools//tools/cpp:current_cc_toolchain)"
//:stardoc_doc
@stardoc//stardoc:renderer
@stardoc//src/main/java/com/google/devtools/build/stardoc/renderer:renderer
@bazel_tools//tools/launcher:launcher
@bazel_tools//src/tools/launcher:launcher
@bazel_tools//tools/cpp:current_cc_toolchainFrom https://github.com/bazelbuild/rules_java/blob/02f488de/java/bazel/rules/bazel_java_binary.bzl#L320 when I saw def make_binary_rule(..., attrs, ...):
return rule(
...,
attrs,
toolchains = \
[semantics.JAVA_TOOLCHAIN] + \
use_cc_toolchain(mandatory = False) + \
... + \
[_LAUNCHER_MAKER_TOOLCHAIN] if bazel_features.rules._has_launcher_maker_toolchain else []
),
exec_groups = {
"cpp_link": exec_group(toolchains = use_cc_toolchain(mandatory = False)),
},I thought the toolchain was optional because of A few lines below in L339-L338 we have BASE_BINARY_ATTRS = merge_attrs(
BASIC_JAVA_BINARY_ATTRIBUTES,
{
"resource_strip_prefix": attr.string(...),
"_test_support": attr.label(default = _compute_test_support),
"_launcher": attr.label(
cfg = "target",
executable = True,
default = "@bazel_tools//tools/launcher:launcher",
),
},
{
"_windows_launcher_maker": attr.label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),
} if not bazel_features.rules._has_launcher_maker_toolchain else {},
)Debugging 8.3+I've also tried with Bazel 8.2 and I get the same trace as with 7.1.0. But with >= 8.3 the query for the `current_cc_toolchain` is empty:$ USE_BAZEL_VERSION=8.3.0 \
bazel query "somepath(//:stardoc_doc, @bazel_tools//tools/cpp:current_cc_toolchain)"
INFO: Empty resultsHowever, I still get an error if I don't have a CC toolchain: # MODULES.bazel: commenting register_toolchains("//toolchains:empty_cc")
$ USE_BAZEL_VERSION=8.3.0 bazel build //:stardoc_doc
ERROR: /home/jjmaestro/.cache/bazel/_bazel_jjmaestro/a868db51c74c9c26abae9ac39c36c609/external/rules_cc+/cc/BUILD:103:19: in cc_toolchain_alias rule @@rules_cc+//cc:current_cc_toolchain:
Traceback (most recent call last):
File "/virtual_builtins_bzl/common/cc/cc_toolchain_alias.bzl", line 26, column 48, in _impl
File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 163, column 13, in _find_cpp_toolchain
Error in fail: Unable to find a CC toolchain using toolchain resolution. Target: @@rules_cc+//cc:current_cc_toolchain, Platform: @@platforms//host:host, Exec platform: @@bazel_tools//tools:host_platform
ERROR: /home/jjmaestro/.cache/bazel/_bazel_jjmaestro/a868db51c74c9c26abae9ac39c36c609/external/rules_cc+/cc/BUILD:103:19: Analysis of target '@@rules_cc+//cc:current_cc_toolchain' failed
ERROR: Analysis of target '//:stardoc_doc' failed; build aborted: Analysis failed
INFO: Elapsed time: 1.200s, Critical Path: 0.02s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
FAILED:
Fetching repository @@rules_java++toolchains+remotejdk21_linux; starting
Fetching repository @@rules_java++toolchains+remote_java_tools; starting
Fetching repository @@rules_java++toolchains+remote_java_tools_linux; starting
Fetching repository @@protobuf++protoc+prebuilt_protoc.linux_x86_64; starting
Fetching repository @@rules_python++python+python_3_11_x86_64-unknown-linux-gnu; starting
Fetching /home/jjmaestro/.cache/bazel/_bazel_jjmaestro/a868db51c74c9c26abae9ac39c36c609/external/protobuf++protoc+prebuilt_protoc.linux_x86_64; Extracting protoc-33.4-linux-x86_64.zip
And, adding back the So, it seems for 8.3+ the CC toolchain is also pulled in by the And, in https://github.com/bazel-contrib/rules_python/blob/1.6.0/python/private/py_executable.bzl#L231-L235 "_windows_launcher_maker": lambda: attrb.Label(
default = "@bazel_tools//tools/launcher:launcher_maker",
cfg = "exec",
executable = True,
),It seems that < 8.3 lead to With Bazel 9.1.0 and So I guess it has to be that, right? The launcher marker forcing the CC toolchain resolution, and empty works because it resolves even if it's not used? I'm going to see if I can play a bit more with the internals in |
|
I've also tried this patch porting your So, I think the issue is pre-8.3.0 there's no launcher_maker toolchain, and the Then, at 8.3.0, |
|
Perhaps all it takes would be a bazel_features version bump? bazel-contrib/bazel_features@1a86fd6 |
I was hopeful! But, I've just tried with: diff --git a/test/no_cc_toolchain/MODULE.bazel b/test/no_cc_toolchain/MODULE.bazel
index f1d7c25..3f8be66 100644
--- a/test/no_cc_toolchain/MODULE.bazel
+++ b/test/no_cc_toolchain/MODULE.bazel
@@ -16,6 +16,9 @@ local_path_override(
bazel_dep(name = "aspect_bazel_lib", version = "2.16.0")
bazel_dep(name = "rules_java", version = "8.14.0")
+bazel_dep(name = "rules_python", version = "1.8.0")
+bazel_dep(name = "bazel_features", version = "1.33.0")
+
# Only for the *empty* CC toolchain stub in toolchains/BUILD.bazel (which
# loads cc_toolchain from @rules_cc); nothing here compiles C/C++.
bazel_dep(name = "rules_cc", version = "0.1.1")and I still get the same "chain" to the CC toolchain: $ USE_BAZEL_VERSION=8.3.0 bazel cquery "somepath(//:stardoc_doc, @@rules_cc+//cc:current_cc_toolchain)"
WARNING: For repository 'rules_cc', the root module requires module version rules_cc@0.1.1, but got rules_cc@0.1.5 in the resolved dependency graph. Please update the version in your MODULE.bazel or set --check_direct_dependencies=off
INFO: Analyzed 2 targets (153 packages loaded, 3727 targets configured).
INFO: Found 2 targets...
//:stardoc_doc (0e16bf0)
@stardoc//stardoc:renderer (2284148)
@stardoc//src/main/java/com/google/devtools/build/stardoc/renderer:renderer (2284148)
@rules_java//toolchains:toolchain_java11 (2284148)
@bazel_tools//tools/jdk:proguard_whitelister (2284148)
@bazel_tools//tools/launcher:launcher_maker (2284148)
@bazel_tools//src/tools/launcher:launcher_maker (2284148)
@bazel_tools//tools/cpp:current_cc_toolchain (2284148)
@rules_cc//cc:current_cc_toolchain (2284148)
INFO: Elapsed time: 0.615s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actionsAnd when I comment out the empty CC toolchain and try Is there something I'm missing / not doing properly? |
Building Stardoc's renderer from source compiles protoc, which forces a C/C++ toolchain onto doc builds. Add:
Fixes #305