Skip to content

Commit 1855ab6

Browse files
committed
Support C++20 modules in the rules-based toolchain API
The rules-based cc/toolchains toolchain API was missing several pieces needed to drive Bazel's C++20 modules pipeline (clang-scan-deps -> .ddi -> aggregate-ddi -> generate-modmap -> module compile): - Add a cpp20_module_actions action type set grouping the module interface compile, codegen and dependency scanning actions. These are deliberately kept out of compile_actions/source_compile_actions so that flags incompatible with C++20 modules can keep targeting only the non-module compiles. - Register the cpp_module_output_file and cpp_module_modmap_file build variables and make dependency_file, output_file, source_file and user_compile_flags available to the C++20 module actions. - Include the C++20 module actions in the compiler_files legacy file group so their tools (e.g. the dependency scanner) are available to the compile actions. - Fix _create_scan_deps_action to pass needs_include_validation and to not request a .d dotd file (the scanner emits a P1689 .ddi file), and let _create_compile_action forward the C++20 module output/modmap arguments to the underlying action.
1 parent 6072dab commit 1855ab6

4 files changed

Lines changed: 78 additions & 18 deletions

File tree

cc/private/compile/compile.bzl

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -527,21 +527,9 @@ def _create_scan_deps_action(
527527
ddi_file,
528528
ddi_output_name,
529529
progress_message_prefix):
530+
# The dependency scanner (e.g. clang-scan-deps) emits the P1689 .ddi file,
531+
# not a Make-style .d dependency file, so no dotd file is requested here.
530532
dotd_file = None
531-
if (
532-
dotd_files_enabled(language, cpp_configuration, feature_configuration) and
533-
_use_dotd_file(feature_configuration, source_artifact)
534-
):
535-
dotd_file = _get_compile_output_file(
536-
action_construction_context,
537-
label,
538-
configuration = configuration,
539-
output_name = _cc_internal.get_artifact_name_for_category(
540-
cc_toolchain = cc_toolchain,
541-
category = artifact_category.INCLUDED_FILE_LIST,
542-
output_name = ddi_output_name,
543-
),
544-
)
545533
specific_compile_build_variables = get_specific_compile_build_variables(
546534
feature_configuration,
547535
use_pic = use_pic,
@@ -579,6 +567,7 @@ def _create_scan_deps_action(
579567
compile_build_variables = compile_variables,
580568
action_name = ACTION_NAMES.cpp_module_deps_scanning,
581569
toolchain_type = _starlark_cc_semantics.toolchain,
570+
needs_include_validation = _starlark_cc_semantics.needs_include_validation(language),
582571
progress_message_prefix = progress_message_prefix,
583572
)
584573

@@ -2319,11 +2308,24 @@ def _create_compile_action(
23192308
progress_message_prefix = None,
23202309
source = None,
23212310
toolchain_type = None,
2322-
use_pic = False):
2311+
use_pic = False,
2312+
additional_outputs = [],
2313+
module_files = None,
2314+
modmap_file = None,
2315+
modmap_input_file = None):
23232316
# TODO(bgorshenev): use bazel_features checks
23242317
version_dependent_kwargs = {}
23252318
if progress_message_prefix:
23262319
version_dependent_kwargs["progress_message_prefix"] = progress_message_prefix
2320+
2321+
# Provide the C++20 module args conditionally as they require a recent
2322+
# version of Bazel.
2323+
if modmap_file:
2324+
version_dependent_kwargs["additional_outputs"] = additional_outputs
2325+
version_dependent_kwargs["module_files"] = module_files
2326+
version_dependent_kwargs["modmap_file"] = modmap_file
2327+
version_dependent_kwargs["modmap_input_file"] = modmap_input_file
2328+
23272329
return _cc_internal.create_cc_compile_action(
23282330
action_construction_context = action_construction_context,
23292331
action_name = action_name,

cc/toolchains/actions/BUILD

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,31 @@ cc_action_type_set(
272272
],
273273
)
274274

275+
# The C++20 module actions: module interface compilation, module codegen and
276+
# module dependency scanning. These are deliberately kept out of
277+
# `compile_actions`/`source_compile_actions` so that flags incompatible with
278+
# C++20 modules can keep targeting only the non-module compiles. Toolchains that
279+
# support C++20 modules typically combine this set with `compile_actions` or
280+
# `source_compile_actions` on the relevant `cc_args`, e.g.
281+
#
282+
# actions = [
283+
# "//cc/toolchains/actions:compile_actions",
284+
# "//cc/toolchains/actions:cpp20_module_actions",
285+
# ]
286+
#
287+
# The module interface compilation and codegen actions are ordinary C++ compiles
288+
# and the dependency scanning action reuses the regular compiler command line,
289+
# so toolchain flags that apply to compiles (target, sysroot, include search
290+
# paths, ...) generally need to apply to these actions as well.
291+
cc_action_type_set(
292+
name = "cpp20_module_actions",
293+
actions = [
294+
":cpp20_module_compile",
295+
":cpp20_module_codegen",
296+
":cpp_module_deps_scanning",
297+
],
298+
)
299+
275300
cc_action_type_set(
276301
name = "compile_actions_without_header_parsing",
277302
actions = [

cc/toolchains/legacy_file_group.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ LEGACY_FILE_GROUPS = {
2828
Label("//cc/toolchains/actions:c_compile"),
2929
Label("//cc/toolchains/actions:cpp_compile"),
3030
Label("//cc/toolchains/actions:cpp_header_parsing"),
31+
# C++20 module actions: their tools (e.g. the dependency scanner) must
32+
# be part of compiler_files so they are available to compile actions.
33+
Label("//cc/toolchains/actions:cpp20_module_compile"),
34+
Label("//cc/toolchains/actions:cpp20_module_codegen"),
35+
Label("//cc/toolchains/actions:cpp_module_deps_scanning"),
3136
],
3237
# There are no actions listed for coverage and objcopy in action_names.bzl.
3338
"coverage_files": [],

cc/toolchains/variables/BUILD

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,22 @@ load("//cc/toolchains/impl:variables.bzl", "cc_builtin_variables", "cc_variable"
22

33
package(default_visibility = ["//visibility:public"])
44

5+
cc_variable(
6+
name = "cpp_module_modmap_file",
7+
actions = [
8+
"//cc/toolchains/actions:cpp_compile",
9+
"//cc/toolchains/actions:cpp20_module_compile",
10+
"//cc/toolchains/actions:cpp20_module_codegen",
11+
],
12+
type = types.option(types.file),
13+
)
14+
15+
cc_variable(
16+
name = "cpp_module_output_file",
17+
actions = ["//cc/toolchains/actions:cpp20_module_compile"],
18+
type = types.option(types.file),
19+
)
20+
521
cc_variable(
622
name = "cs_fdo_instrument_path",
723
actions = [
@@ -19,7 +35,10 @@ cc_variable(
1935

2036
cc_variable(
2137
name = "dependency_file",
22-
actions = ["//cc/toolchains/actions:compile_actions"],
38+
actions = [
39+
"//cc/toolchains/actions:compile_actions",
40+
"//cc/toolchains/actions:cpp20_module_actions",
41+
],
2342
type = types.option(types.file),
2443
)
2544

@@ -412,6 +431,7 @@ cc_variable(
412431
name = "output_file",
413432
actions = [
414433
"//cc/toolchains/actions:compile_actions",
434+
"//cc/toolchains/actions:cpp20_module_actions",
415435
"//cc/toolchains/actions:strip",
416436
],
417437
type = types.option(types.file),
@@ -473,7 +493,10 @@ cc_variable(
473493

474494
cc_variable(
475495
name = "source_file",
476-
actions = ["//cc/toolchains/actions:compile_actions"],
496+
actions = [
497+
"//cc/toolchains/actions:compile_actions",
498+
"//cc/toolchains/actions:cpp20_module_actions",
499+
],
477500
type = types.option(types.file),
478501
)
479502

@@ -554,7 +577,10 @@ cc_variable(
554577

555578
cc_variable(
556579
name = "user_compile_flags",
557-
actions = ["//cc/toolchains/actions:compile_actions"],
580+
actions = [
581+
"//cc/toolchains/actions:compile_actions",
582+
"//cc/toolchains/actions:cpp20_module_actions",
583+
],
558584
type = types.option(types.list(types.string)),
559585
)
560586

@@ -569,6 +595,8 @@ cc_builtin_variables(
569595
srcs = [
570596
":attr_linkopts",
571597
":cc_library_exec_paths",
598+
":cpp_module_modmap_file",
599+
":cpp_module_output_file",
572600
":cs_fdo_instrument_path",
573601
":def_file_path",
574602
":dep_linkopts",

0 commit comments

Comments
 (0)