bazel
bazel copied to clipboard
[1/5] support C++20 Modules, add module_interfaces attr
I split the XXL PR https://github.com/bazelbuild/bazel/pull/19940 into several small patches.
This is the first patch of Support C++20 Modules, I add module_interfaces attr only
example
- foo.cppm
// foo.cppm
export module foo;
// ...
- BUILD.bazel
cc_library(
name="foo",
copts=["-std=c++20"],
module_interfaces=["foo.cppm"],
# features=["cpp20_module"]
)
build failed with the following message
➜ bazel build :foo
ERROR: bazel_demo/BUILD.bazel:1:11: in cc_library rule //:foo:
Traceback (most recent call last):
File "/virtual_builtins_bzl/common/cc/cc_library.bzl", line 40, column 42, in _cc_library_impl
File "/virtual_builtins_bzl/common/cc/semantics.bzl", line 123, column 13, in _check_can_module_interfaces
Error in fail: attribute module_interfaces: requires --experimental_cpp20_modules
ERROR: bazel_demo/BUILD.bazel:1:11: Analysis of target '//:foo' failed
ERROR: Analysis of target '//:foo' failed; build aborted
INFO: Elapsed time: 0.106s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
To build with C++20 Modules, the flag --experimental_cpp20_modules must be added.
➜ bazel build :foo --experimental_cpp20_modules
ERROR: bazel_demo/BUILD.bazel:1:11: in cc_library rule //:foo:
Traceback (most recent call last):
File "/virtual_builtins_bzl/common/cc/cc_library.bzl", line 41, column 34, in _cc_library_impl
File "/virtual_builtins_bzl/common/cc/cc_helper.bzl", line 1225, column 13, in _check_cpp20_modules
Error in fail: to use C++20 Modules, the feature cpp20_modules must be enabled
ERROR: bazel_demo/BUILD.bazel:1:11: Analysis of target '//:foo' failed
ERROR: Analysis of target '//:foo' failed; build aborted
INFO: Elapsed time: 0.091s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
To build with C++20 Modules, the feature cpp20_modules must be enabled.
bazel build :foo --experimental_cpp20_modules --features cpp20_modules
the flag --experimental_cpp20_modules works on global and
the feature cpp20_modules work on each target
but in this patch, do nothing with C++20 Module Interfaces.
You might want to consider a way to tell Bazel that some sources do not use modules and can therefore completely skip scanning (and, if nothing in the target needs scanned, the target's collation step as well).
hi @mathstuf, In current implementation, I'm using the cpp20_modules feature to control if a target should be scanned. This only works for the whole target, not for individual files.
With this feature, we can update our codebase bit by bit, turning on C++20 Modules for some code without having to scan every file.
hi @comius, I have updated the code. please review again. for each comment, I give a small commit. Squash commits can be performed if needed.
I test the failed case //src/test/java/com/google/devtools/build/lib/dynamic:DynamicSpawnStrategyTest on GitHub Action (job link) with branch cxx20-modules-support-patch-1-ci. and the test passed.
the new branch only add ci config. There could be differing settings that prevent the replication of the CI error. about the window CI test broken, could you give me some suggestions?
In current implementation, I'm using the cpp20_modules feature to control if a target should be scanned. This only works for the whole target, not for individual files.
That's probably fine given what I know of Bazel (that per-source behaviors are discouraged).
hi @comius , thanks for your comments. I have updated the code. please review again.
Changes Summary
- update doc about
--experimental_cpp20_modulesoption andmodule_interfacesattribute - refactor
_get_cpp20module_interfaces - refactor Cpp20ModulesConfiguredTargetTest
- add
testSameModuleInterfacesFileTwiceinspired by testSameCcFileTwice
The code was approved by @trybka internally, suggesting to change the name from cpp20_modules to cpp_modules in the code, except in the documentation/some comments. I applied the changes, to move things along a bit faster. The PR will be merged now.
change the name from cpp20_modules to cpp_modules in the code
Do I need to make this change in this patch?
@PikachuHyA thank you so much for this work, I am looking forward to using C++20 modules in our bazel builds :-)