[3/5] support C++20 Modules, add deps-scanner and update toolchains

## Summary
I have splited the XXL PR [#19940](https://github.com/bazelbuild/bazel/pull/19940) into several smaller patches. This is the third patch to support C++20 Modules, which adds the `deps-scanner` tool and updates toolchains.

This patch includes:
1. New action names
2. File extensions
3. Build variables
4. Updated toolchains for compiling C++20 Modules

## Action Names
Three action names have been added:
- `c++-module-deps-scanning`
- `c++20-module-compile`
- `c++20-module-codegen`

When two-phase compilation is employed:
- `c++-module-deps-scanning`: Scans source files and retrieves C++20 Modules dependencies, storing them in `<filename>.ddi`.
- `c++20-module-compile`: Compiles the C++20 Modules Interfaces to a Built Module Interface (BMI), converting `<filename>.cppm` to `<filename>.pcm`.
- `c++20-module-codegen`: Compiles the BMI to an object file, converting `<filename>.pcm` to `<filename>.o`.

When one-phase compilation is employed:
- `c++-module-deps-scanning`: Operates similarly to two-phase compilation.
- `c++20-module-compile`: Compiles the C++20 Modules Interfaces directly to an object file `<filename>.o` and produces a BMI `<filename>.pcm` as a byproduct.

## File Extensions
We follow the file extensions preferred by different compilers, adding two new `ArtifactCategory`s: `CPP_MODULE_GCM` and `CPP_MODULE_IFC`.

- Clang uses `.pcm` (CPP_MODULE, already exists).
- GCC uses `.gcm` (CPP_MODULE_GCM, new).
- MSVC uses `.ifc` (CPP_MODULE_IFC, new).

Following the CMake implementation, we added three extra `ArtifactCategory`s: `CPP_MODULES_INFO`, `CPP_MODULES_DDI`, and `CPP_MODULES_MODMAP`.

- The `.ddi` file (CPP_MODULES_DDI) stores the dependencies information of one source file.
- The `.CXXModules.json` file (CPP_MODULES_INFO) stores dependencies information for an entire target.
- The `.modmap` file (CPP_MODULES_MODMAP) maps module names to BMIs, with different formats for each compiler.

Additionally, a special `ArtifactCategory`, `CPP_MODULES_MODMAP_INPUT`, is an auxiliary file used to easily obtain the requested BMI paths.

## Build Variables
Two build variables, `CPP_MODULE_MODMAP_FILE` and `CPP_MODULE_OUTPUT_FILE`, have been added.

- `CPP_MODULE_MODMAP_FILE` specifies the path to the `.modmap` file and is used by the `cpp20_modmap_file_feature`.
- `CPP_MODULE_OUTPUT_FILE` specifies the output name of the BMI when one-phase compilation is employed and is used by the `cpp20_module_compile_flags_feature`.

## Toolchains
Three action configs (`cpp_module_scan_deps`, `cpp20_module_compile`, and `cpp20_module_codegen`) have been added, corresponding to the action names section.

Two features (`cpp_module_modmap_file_feature` and `cpp20_module_compile_flags_feature`) have been added, corresponding to the build variables section.

Using C++20 Modules necessitates topological ordering for the compilation units. For more details, see the [Discovering Dependencies](https://clang.llvm.org/docs/StandardCPlusPlusModules.html#discovering-dependencies) section.

Considering the various compilers, I have added the `deps-scanner` tool. The default implementation is a script wrapper that uses different scanning methods depending on the compiler. The wrapper `deps_scanner_wrapper` is generated by a template file `<compiler>_deps_scanner_wrapper.sh.tpl`. Three template files have been added:

- `clang_deps_scanner_wrapper.sh.tpl`
- `gcc_deps_scanner_wrapper.sh.tpl`
- `mvsc_deps_scanner_wrapper.bat.tpl`

For a demonstration of how to scan C++20 dependencies, please refer to this [demo](https://github.com/PikachuHyA/cpp20_modules_scan_dependency_demo).

Closes #22429.

PiperOrigin-RevId: 669241384
Change-Id: Id9ee2f66cb075446d0c38e6a6c70786ad9b28022
17 files changed
tree: 42ffd35f33b5dd22329d0f762c17651819be07ff
  1. .bazelci/
  2. .github/
  3. examples/
  4. scripts/
  5. site/
  6. src/
  7. third_party/
  8. tools/
  9. .bazelrc
  10. .bazelversion
  11. .gitattributes
  12. .gitignore
  13. AUTHORS
  14. bazel_downloader.cfg
  15. BUILD
  16. CHANGELOG.md
  17. CODE_OF_CONDUCT.md
  18. CODEOWNERS
  19. combine_distfiles.py
  20. combine_distfiles_to_tar.sh
  21. compile.sh
  22. CONTRIBUTING.md
  23. CONTRIBUTORS
  24. distdir.bzl
  25. extensions.bzl
  26. LICENSE
  27. maven_install.json
  28. MODULE.bazel
  29. MODULE.bazel.lock
  30. rbe_extension.bzl
  31. README.md
  32. repositories.bzl
  33. requirements.txt
  34. SECURITY.md
  35. WORKSPACE.bzlmod
  36. workspace_deps.bzl
README.md

Bazel

{Fast, Correct} - Choose two

Build and test software of any size, quickly and reliably.

  • Speed up your builds and tests: Bazel rebuilds only what is necessary. With advanced local and distributed caching, optimized dependency analysis and parallel execution, you get fast and incremental builds.

  • One tool, multiple languages: Build and test Java, C++, Android, iOS, Go, and a wide variety of other language platforms. Bazel runs on Windows, macOS, and Linux.

  • Scalable: Bazel helps you scale your organization, codebase, and continuous integration solution. It handles codebases of any size, in multiple repositories or a huge monorepo.

  • Extensible to your needs: Easily add support for new languages and platforms with Bazel's familiar extension language. Share and re-use language rules written by the growing Bazel community.

Getting Started

Documentation

Reporting a Vulnerability

To report a security issue, please email security@bazel.build with a description of the issue, the steps you took to create the issue, affected versions, and, if known, mitigations for the issue. Our vulnerability management team will respond within 3 working days of your email. If the issue is confirmed as a vulnerability, we will open a Security Advisory. This project follows a 90 day disclosure timeline.

Contributing to Bazel

See CONTRIBUTING.md

Build status