Skip to content

Support C++20 standard modules in the rules-based toolchain#654

Draft
fmeum wants to merge 1 commit into
mainfrom
fmeum/cpp20-modules
Draft

Support C++20 standard modules in the rules-based toolchain#654
fmeum wants to merge 1 commit into
mainfrom
fmeum/cpp20-modules

Conversation

@fmeum

@fmeum fmeum commented Jun 26, 2026

Copy link
Copy Markdown
Collaborator

Drives Bazel's C++20 modules pipeline (clang-scan-deps.ddi → aggregate-ddi → generate-modmap → module compile) through the rules-based cc/toolchains API.

Toolchain

  • Add tools/internal/deps_scanner.c, a compiled wrapper that runs clang-scan-deps -format=p1689 -- clang++ <args> and writes the P1689 JSON to DEPS_SCANNER_OUTPUT_FILE, mirroring the other internal tool wrappers. Wire it as the cpp_module_deps_scanning tool in both the prebuilt (toolchain/llvm/llvm.bzl) and bootstrap (toolchain/bootstrap/declare_toolchains.bzl) toolchains.
  • Add cc_args for the C++20 module actions: module output (-fmodule-output), modmap (@modmap), scanner output env, source/output flags, dependency file and user compile flags. Gate -fno-cxx-modules on the new cpp_modules feature being disabled.
  • Add the cpp_modules feature and extend the sysroot / header search-path / resource-dir args to cover cpp20_module_actions.

rules_cc dependency

This currently requires two unreleased rules_cc PRs, applied to rules_cc 0.2.20 as checked-in patches (3rd_party/rules_cc) via single_version_override:

The e2e/rules_cc module is a separate root module, so it carries its own single_version_override and symlinks the patch files from the parent (single_version_override only accepts main-repo patch labels).

Draft because of the above: it should land once those PRs are merged and a rules_cc release includes them, at which point the patches and overrides are dropped and the rules_cc version is bumped.

e2e

Adds //:cpp20_modules_test in e2e/rules_cc, exercising an exported math module (math.cppm) imported by modules_main.cc, built with --features=cpp_modules and --experimental_cpp_modules. Passes on macOS arm64.

🤖 Generated with Claude Code

@fmeum fmeum force-pushed the fmeum/cpp20-modules branch from 4891671 to 8da110f Compare June 26, 2026 20:55
Drive Bazel's C++20 modules pipeline (clang-scan-deps -> .ddi ->
aggregate-ddi -> generate-modmap -> module compile) through the
rules-based cc/toolchains API.

Toolchain:
- Add tools/internal/deps_scanner.c, a wrapper that runs
  `clang-scan-deps -format=p1689 -- clang++ <args>` and writes the P1689
  JSON to DEPS_SCANNER_OUTPUT_FILE, mirroring the other internal tool
  wrappers. Wire it as the cpp_module_deps_scanning tool in both the
  prebuilt (toolchain/llvm/llvm.bzl) and bootstrap
  (toolchain/bootstrap/declare_toolchains.bzl) toolchains.
- Add cc_args for the compiler-specific C++20 module flags only: module
  output (-fmodule-output), the modmap (@ModMap) and the dependency
  scanner output env. The generic -c / -o / -MD -MF / copt flags are
  emitted by rules_cc's legacy compile features, which #750 extends to the
  C++20 module actions, so every rules_cc toolchain benefits. Gate
  -fno-cxx-modules on the new cpp_modules feature being disabled.
- Add the cpp_modules feature and extend sysroot / header search-path /
  resource-dir args to cover cpp20_module_actions.

rules_cc currently requires bazelbuild/rules_cc PRs #750 (rules-based
module build variables/actions, plus extending the legacy compile features
-- compiler_input_flags, compiler_output_flags, dependency_file,
user_compile_flags -- to the C++20 module actions) and #775 (compile.bzl
analysis fix). Until a release ships them, they are applied to rules_cc
0.2.20 as checked-in patches (3rd_party/rules_cc) via
single_version_override. The e2e/rules_cc module is a separate root
module, so it carries its own single_version_override and symlinks the
patch files from the parent.

e2e: add //:cpp20_modules_test exercising an exported `math` module
(math.cppm) imported by modules_main.cc, built with --features=cpp_modules
and --experimental_cpp_modules.
@fmeum fmeum force-pushed the fmeum/cpp20-modules branch from f606dfd to 25ed393 Compare June 27, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant