From c3855309ecfe62daafaa9e5b68e14df034094626 Mon Sep 17 00:00:00 2001 From: PikachuHy Date: Tue, 23 Jul 2024 11:08:36 +0800 Subject: [PATCH 1/2] support C++20 Modules, update toolchain config --- .../build/lib/rules/cpp/ArtifactCategory.java | 6 + .../build/lib/rules/cpp/CcCommon.java | 3 + .../lib/rules/cpp/CompileBuildVariables.java | 4 + .../build/lib/rules/cpp/CppActionNames.java | 5 + .../build/lib/rules/cpp/CppFileTypes.java | 2 +- .../builtins_bzl/common/cc/action_names.bzl | 10 ++ .../common/cc/cc_helper_internal.bzl | 6 + tools/build_defs/cc/action_names.bzl | 10 ++ tools/cpp/BUILD.tpl | 4 + tools/cpp/BUILD.windows.tpl | 21 ++- tools/cpp/clang_deps_scanner_wrapper.sh.tpl | 11 ++ tools/cpp/gcc_deps_scanner_wrapper.sh.tpl | 12 ++ tools/cpp/msvc_deps_scanner_wrapper.bat.tpl | 16 ++ tools/cpp/unix_cc_configure.bzl | 23 +++ tools/cpp/unix_cc_toolchain_config.bzl | 134 ++++++++++++++ tools/cpp/windows_cc_configure.bzl | 18 ++ tools/cpp/windows_cc_toolchain_config.bzl | 170 +++++++++++++++++- 17 files changed, 452 insertions(+), 3 deletions(-) create mode 100644 tools/cpp/clang_deps_scanner_wrapper.sh.tpl create mode 100644 tools/cpp/gcc_deps_scanner_wrapper.sh.tpl create mode 100644 tools/cpp/msvc_deps_scanner_wrapper.bat.tpl diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java index ca2a2b1552eca5..c19908de177515 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/ArtifactCategory.java @@ -32,6 +32,12 @@ public enum ArtifactCategory { OBJECT_FILE("", ".o", ".obj"), PIC_OBJECT_FILE("", ".pic.o"), CPP_MODULE("", ".pcm"), + CPP_MODULE_GCM("", ".gcm"), + CPP_MODULE_IFC("", ".ifc"), + CPP_MODULES_INFO("", ".CXXModules.json"), + CPP_MODULES_DDI("", ".ddi"), + CPP_MODULES_MODMAP("", ".modmap"), + CPP_MODULES_MODMAP_INPUT("", ".modmap.input"), GENERATED_ASSEMBLY("", ".s", ".asm"), PROCESSED_HEADER("", ".processed"), GENERATED_HEADER("", ".h"), diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java index fd7959c4793cb4..c6571b60bb86a3 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java @@ -76,6 +76,9 @@ public final class CcCommon implements StarlarkValue { CppActionNames.CPP_HEADER_PARSING, CppActionNames.CPP_MODULE_COMPILE, CppActionNames.CPP_MODULE_CODEGEN, + CppActionNames.CPP_MODULE_DEPS_SCANNING, + CppActionNames.CPP20_MODULE_COMPILE, + CppActionNames.CPP20_MODULE_CODEGEN, CppActionNames.ASSEMBLE, CppActionNames.PREPROCESS_ASSEMBLE, CppActionNames.CLIF_MATCH, diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java index 03c2689c7f677e..4f6204086d678e 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CompileBuildVariables.java @@ -79,6 +79,10 @@ public enum CompileBuildVariables { * @see CcCompilationContext#getFrameworkIncludeDirs(). */ FRAMEWORK_PATHS("framework_include_paths"), + /** Variable for the c++20 module modmap file name. */ + CPP_MODULE_MODMAP_FILE("cpp_module_modmap_file"), + /** Variable for the c++20 module output file name. */ + CPP_MODULE_OUTPUT_FILE("cpp_module_output_file"), /** Variable for the module map file name. */ MODULE_MAP_FILE("module_map_file"), /** Variable for the dependent module map file name. */ diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionNames.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionNames.java index ab4881340abd16..27c61d56cfad90 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionNames.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppActionNames.java @@ -36,6 +36,11 @@ public class CppActionNames { public static final String OBJCPP_COMPILE = "objc++-compile"; /** A string constant for the c++ header parsing. */ public static final String CPP_HEADER_PARSING = "c++-header-parsing"; + /** A string constant for the c++20 modules deps scanning */ + public static final String CPP_MODULE_DEPS_SCANNING = "c++-module-deps-scanning"; + /** A string constant for the c++20 module compile action. */ + public static final String CPP20_MODULE_COMPILE = "c++20-module-compile"; + public static final String CPP20_MODULE_CODEGEN = "c++20-module-codegen"; /** * A string constant for the c++ module compilation action. Note: currently we don't support C * module compilation. diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java index ac63afa3d21acc..a2899072c164ea 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppFileTypes.java @@ -285,7 +285,7 @@ public boolean apply(String path) { public static final FileType LLVM_PROFILE_ZIP = FileType.of(".zip"); public static final FileType CPP_MODULE_MAP = FileType.of(".cppmap"); - public static final FileType CPP_MODULE = FileType.of(".pcm"); + public static final FileType CPP_MODULE = FileType.of(".pcm", ".gcm", ".ifc"); public static final FileType OBJC_MODULE_MAP = FileType.of("module.modulemap"); /** Predicate that matches all artifacts that can be used in an objc Clang module map. */ diff --git a/src/main/starlark/builtins_bzl/common/cc/action_names.bzl b/src/main/starlark/builtins_bzl/common/cc/action_names.bzl index 479413fd4766c6..d5b73d5d246bb8 100644 --- a/src/main/starlark/builtins_bzl/common/cc/action_names.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/action_names.bzl @@ -35,6 +35,13 @@ CPP_MODULE_CODEGEN_ACTION_NAME = "c++-module-codegen" # Name of the C++ header parsing action. CPP_HEADER_PARSING_ACTION_NAME = "c++-header-parsing" +# Name of the C++ deps scanning action. +CPP_MODULE_DEPS_SCANNING_ACTION_NAME = "c++-module-deps-scanning" + +# Name of the C++20 module compile action. +CPP20_MODULE_COMPILE_ACTION_NAME = "c++20-module-compile" +CPP20_MODULE_CODEGEN_ACTION_NAME = "c++20-module-codegen" + # Name of the C++ module compile action. CPP_MODULE_COMPILE_ACTION_NAME = "c++-module-compile" @@ -104,6 +111,9 @@ ACTION_NAMES = struct( cc_flags_make_variable = CC_FLAGS_MAKE_VARIABLE_ACTION_NAME, cpp_module_codegen = CPP_MODULE_CODEGEN_ACTION_NAME, cpp_header_parsing = CPP_HEADER_PARSING_ACTION_NAME, + cpp_module_deps_scanning = CPP_MODULE_DEPS_SCANNING_ACTION_NAME, + cpp20_module_compile = CPP20_MODULE_COMPILE_ACTION_NAME, + cpp20_module_codegen = CPP20_MODULE_CODEGEN_ACTION_NAME, cpp_module_compile = CPP_MODULE_COMPILE_ACTION_NAME, assemble = ASSEMBLE_ACTION_NAME, preprocess_assemble = PREPROCESS_ASSEMBLE_ACTION_NAME, diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl index 5c901149af2cbe..d1939d3c4b867f 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_helper_internal.bzl @@ -46,6 +46,12 @@ artifact_category = struct( OBJECT_FILE = "OBJECT_FILE", PIC_OBJECT_FILE = "PIC_OBJECT_FILE", CPP_MODULE = "CPP_MODULE", + CPP_MODULE_GCM = "CPP_MODULE_GCM", + CPP_MODULE_IFC = "CPP_MODULE_IFC", + CPP_MODULES_INFO = "CPP_MODULES_INFO", + CPP_MODULES_DDI = "CPP_MODULES_DDI", + CPP_MODULES_MODMAP = "CPP_MODULES_MODMAP", + CPP_MODULES_MODMAP_INPUT = "CPP_MODULES_MODMAP_INPUT", GENERATED_ASSEMBLY = "GENERATED_ASSEMBLY", PROCESSED_HEADER = "PROCESSED_HEADER", GENERATED_HEADER = "GENERATED_HEADER", diff --git a/tools/build_defs/cc/action_names.bzl b/tools/build_defs/cc/action_names.bzl index c57bfb5aa591c3..e93e08ec72202b 100644 --- a/tools/build_defs/cc/action_names.bzl +++ b/tools/build_defs/cc/action_names.bzl @@ -31,6 +31,13 @@ CPP_MODULE_CODEGEN_ACTION_NAME = "c++-module-codegen" # Name of the C++ header parsing action. CPP_HEADER_PARSING_ACTION_NAME = "c++-header-parsing" +# Name of the C++ deps scanning action. +CPP_MODULE_DEPS_SCANNING_ACTION_NAME = "c++-module-deps-scanning" + +# Name of the C++ module compile action. +CPP20_MODULE_COMPILE_ACTION_NAME = "c++20-module-compile" +CPP20_MODULE_CODEGEN_ACTION_NAME = "c++20-module-codegen" + # Name of the C++ module compile action. CPP_MODULE_COMPILE_ACTION_NAME = "c++-module-compile" @@ -101,6 +108,9 @@ ACTION_NAMES = struct( cc_flags_make_variable = CC_FLAGS_MAKE_VARIABLE_ACTION_NAME, cpp_module_codegen = CPP_MODULE_CODEGEN_ACTION_NAME, cpp_header_parsing = CPP_HEADER_PARSING_ACTION_NAME, + cpp_module_deps_scanning = CPP_MODULE_DEPS_SCANNING_ACTION_NAME, + cpp20_module_compile = CPP20_MODULE_COMPILE_ACTION_NAME, + cpp20_module_codegen = CPP20_MODULE_CODEGEN_ACTION_NAME, cpp_module_compile = CPP_MODULE_COMPILE_ACTION_NAME, assemble = ASSEMBLE_ACTION_NAME, preprocess_assemble = PREPROCESS_ASSEMBLE_ACTION_NAME, diff --git a/tools/cpp/BUILD.tpl b/tools/cpp/BUILD.tpl index b4b5c8baf46a7e..99896dc93f3054 100644 --- a/tools/cpp/BUILD.tpl +++ b/tools/cpp/BUILD.tpl @@ -59,6 +59,10 @@ filegroup( srcs = ["validate_static_library.sh"], ) +filegroup( + name = "deps_scanner_wrapper", + srcs = ["deps_scanner_wrapper.sh"], +) filegroup( name = "compiler_deps", srcs = glob(["extra_tools/**"], allow_empty = True) + [%{cc_compiler_deps}], diff --git a/tools/cpp/BUILD.windows.tpl b/tools/cpp/BUILD.windows.tpl index 641d574882a9e4..1a1b42b4ea1184 100644 --- a/tools/cpp/BUILD.windows.tpl +++ b/tools/cpp/BUILD.windows.tpl @@ -59,7 +59,22 @@ filegroup( filegroup( name = "msvc_compiler_files", - srcs = [":builtin_include_directory_paths_msvc"] + srcs = [":builtin_include_directory_paths_msvc" + , "%{msvc_deps_scanner_wrapper_path_x86}" + , "%{msvc_deps_scanner_wrapper_path_x64}" + , "%{msvc_deps_scanner_wrapper_path_arm}" + , "%{msvc_deps_scanner_wrapper_path_arm64}" + ] +) + +filegroup( + name = "agg-ddi", + srcs = ["//tools/cpp/cpp20modules_tools:agg-ddi-bin"], +) + +filegroup( + name = "gen-modmap", + srcs = ["//tools/cpp/cpp20modules_tools:gen-modmap-bin"], ) # Hardcoded toolchain, legacy behaviour. @@ -315,6 +330,7 @@ cc_toolchain_config( "objdump": "wrapper/bin/msvc_nop.bat", "strip": "wrapper/bin/msvc_nop.bat", "dumpbin": "%{msvc_dumpbin_path_x64}", + "cpp-module-deps-scanner": "%{msvc_deps_scanner_wrapper_path_x64}", }, archiver_flags = ["/MACHINE:X64"], default_link_flags = ["/MACHINE:X64"], @@ -383,6 +399,7 @@ cc_toolchain_config( "objdump": "wrapper/bin/msvc_nop.bat", "strip": "wrapper/bin/msvc_nop.bat", "dumpbin": "%{msvc_dumpbin_path_x86}", + "cpp-module-deps-scanner": "%{msvc_deps_scanner_wrapper_path_x86}", }, archiver_flags = ["/MACHINE:X86"], default_link_flags = ["/MACHINE:X86"], @@ -451,6 +468,7 @@ cc_toolchain_config( "objdump": "wrapper/bin/msvc_nop.bat", "strip": "wrapper/bin/msvc_nop.bat", "dumpbin": "%{msvc_dumpbin_path_arm}", + "cpp-module-deps-scanner": "%{msvc_deps_scanner_wrapper_path_arm}", }, archiver_flags = ["/MACHINE:ARM"], default_link_flags = ["/MACHINE:ARM"], @@ -519,6 +537,7 @@ cc_toolchain_config( "objdump": "wrapper/bin/msvc_nop.bat", "strip": "wrapper/bin/msvc_nop.bat", "dumpbin": "%{msvc_dumpbin_path_arm64}", + "cpp-module-deps-scanner": "%{msvc_deps_scanner_wrapper_path_arm64}", }, archiver_flags = ["/MACHINE:ARM64"], default_link_flags = ["/MACHINE:ARM64"], diff --git a/tools/cpp/clang_deps_scanner_wrapper.sh.tpl b/tools/cpp/clang_deps_scanner_wrapper.sh.tpl new file mode 100644 index 00000000000000..323bca54ae4938 --- /dev/null +++ b/tools/cpp/clang_deps_scanner_wrapper.sh.tpl @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +# +# Ship the environment to the C++ action +# +set -eu + +# Set-up the environment +%{env} + +# Call the C++ compiler +%{deps_scanner} -format=p1689 -- %{cc} "$@" >out.tmp && mv out.tmp $DEPS_SCANNER_OUTPUT_FILE diff --git a/tools/cpp/gcc_deps_scanner_wrapper.sh.tpl b/tools/cpp/gcc_deps_scanner_wrapper.sh.tpl new file mode 100644 index 00000000000000..6434211fd9a815 --- /dev/null +++ b/tools/cpp/gcc_deps_scanner_wrapper.sh.tpl @@ -0,0 +1,12 @@ +#!/bin/bash +# +# Ship the environment to the C++ action +# +set -eu + +# Set-up the environment +%{env} + +# Call the C++ compiler + +%{cc} -E -x c++ -fmodules-ts -fdeps-file=out.tmp -fdeps-format=p1689r5 "$@" >out.tmp && mv out.tmp $DEPS_SCANNER_OUTPUT_FILE diff --git a/tools/cpp/msvc_deps_scanner_wrapper.bat.tpl b/tools/cpp/msvc_deps_scanner_wrapper.bat.tpl new file mode 100644 index 00000000000000..c2447f038af46d --- /dev/null +++ b/tools/cpp/msvc_deps_scanner_wrapper.bat.tpl @@ -0,0 +1,16 @@ +:: Copyright 2024 The Bazel Authors. All rights reserved. +:: +:: Licensed under the Apache License, Version 2.0 (the "License"); +:: you may not use this file except in compliance with the License. +:: You may obtain a copy of the License at +:: +:: http://www.apache.org/licenses/LICENSE-2.0 +:: +:: Unless required by applicable law or agreed to in writing, software +:: distributed under the License is distributed on an "AS IS" BASIS, +:: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +:: See the License for the specific language governing permissions and +:: limitations under the License. + +@echo OFF +"%{cc}" /scanDependencies- /TP %* >out.tmp && move out.tmp %DEPS_SCANNER_OUTPUT_FILE% diff --git a/tools/cpp/unix_cc_configure.bzl b/tools/cpp/unix_cc_configure.bzl index 4661f607cc22f5..0e796a1504783e 100644 --- a/tools/cpp/unix_cc_configure.bzl +++ b/tools/cpp/unix_cc_configure.bzl @@ -332,6 +332,8 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "@bazel_tools//tools/cpp:linux_cc_wrapper.sh.tpl", "@bazel_tools//tools/cpp:validate_static_library.sh.tpl", "@bazel_tools//tools/cpp:osx_cc_wrapper.sh.tpl", + "@bazel_tools//tools/cpp:clang_deps_scanner_wrapper.sh.tpl", + "@bazel_tools//tools/cpp:gcc_deps_scanner_wrapper.sh.tpl", ]) repository_ctx.symlink( @@ -387,8 +389,10 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): if darwin: overriden_tools["gcc"] = "cc_wrapper.sh" overriden_tools["ar"] = _find_generic(repository_ctx, "libtool", "LIBTOOL", overriden_tools) + auto_configure_warning_maybe(repository_ctx, "CC used: " + str(cc)) tool_paths = _get_tool_paths(repository_ctx, overriden_tools) + tool_paths["cpp-module-deps-scanner"] = "deps_scanner_wrapper.sh" # The parse_header tool needs to be a wrapper around the compiler as it has # to touch the output file. @@ -424,6 +428,24 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "%{env}": escape_string(get_env(repository_ctx)), }, ) + deps_scanner_wrapper_src = ( + "@bazel_tools//tools/cpp:clang_deps_scanner_wrapper.sh.tpl" if is_clang else "@bazel_tools//tools/cpp:gcc_deps_scanner_wrapper.sh.tpl" + ) + deps_scanner = "cpp-module-deps-scanner_not_found" + if is_clang: + cc_str = str(cc) + path_arr = cc_str.split("/")[:-1] + path_arr.append("clang-scan-deps") + deps_scanner = "/".join(path_arr) + repository_ctx.template( + "deps_scanner_wrapper.sh", + paths[deps_scanner_wrapper_src], + { + "%{cc}": escape_string(str(cc)), + "%{deps_scanner}": escape_string(deps_scanner), + "%{env}": escape_string(get_env(repository_ctx)), + }, + ) conly_opts = split_escaped(get_env_var( repository_ctx, @@ -584,6 +606,7 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools): "%{cc_compiler_deps}": get_starlark_list([ ":builtin_include_directory_paths", ":cc_wrapper", + ":deps_scanner_wrapper", ] + ( [":validate_static_library"] if "validate_static_library" in tool_paths else [] )), diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl index 5a75893772b395..c5a184a80111b4 100644 --- a/tools/cpp/unix_cc_toolchain_config.bzl +++ b/tools/cpp/unix_cc_toolchain_config.bzl @@ -148,6 +148,9 @@ all_compile_actions = [ ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ACTION_NAMES.lto_backend, ] @@ -158,6 +161,9 @@ all_cpp_compile_actions = [ ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ] @@ -168,6 +174,8 @@ preprocessor_compile_actions = [ ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ] @@ -178,6 +186,7 @@ codegen_compile_actions = [ ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ] @@ -271,6 +280,64 @@ def _impl(ctx): else: symbol_check = None + deps_scanner = "cpp-module-deps-scanner_not_found" + if "cpp-module-deps-scanner" in ctx.attr.tool_paths: + deps_scanner = ctx.attr.tool_paths["cpp-module-deps-scanner"] + cc = ctx.attr.tool_paths.get("gcc") + compile_implies = [ + # keep same with c++-compile + "legacy_compile_flags", + "user_compile_flags", + "sysroot", + "unfiltered_compile_flags", + "compiler_input_flags", + "compiler_output_flags", + ] + cpp_module_scan_deps = action_config( + action_name = ACTION_NAMES.cpp_module_deps_scanning, + tools = [ + tool( + path = deps_scanner, + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp_module_scan_deps) + + cpp20_module_compile = action_config( + action_name = ACTION_NAMES.cpp20_module_compile, + tools = [ + tool( + path = cc, + ), + ], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group( + flags = [ + "-x", + "c++-module" if ctx.attr.compiler == "clang" else "c++", + ], + ), + ], + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp20_module_compile) + + cpp20_module_codegen = action_config( + action_name = ACTION_NAMES.cpp20_module_codegen, + tools = [ + tool( + path = cc, + ), + ], + implies = compile_implies, + ) + action_configs.append(cpp20_module_codegen) + supports_pic_feature = feature( name = "supports_pic", enabled = True, @@ -407,6 +474,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ] + all_link_actions + lto_index_actions, @@ -585,6 +655,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group(flags = ["-fPIC"], expand_if_available = "pic"), @@ -604,6 +676,7 @@ def _impl(ctx): ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -627,6 +700,9 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ], flag_groups = [ @@ -783,6 +859,9 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -806,6 +885,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, @@ -877,6 +958,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, @@ -910,6 +993,9 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, @@ -1243,6 +1329,8 @@ def _impl(ctx): ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ], flag_groups = [ @@ -1268,6 +1356,7 @@ def _impl(ctx): ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, ACTION_NAMES.clif_match, ], flag_groups = [ @@ -1522,12 +1611,55 @@ def _impl(ctx): enabled = False, ) + cpp_module_modmap_file_feature = feature( + name = "cpp_module_modmap_file", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group( + flags = ["@%{cpp_module_modmap_file}" if ctx.attr.compiler == "clang" else "-fmodule-mapper=%{cpp_module_modmap_file}"], + expand_if_available = "cpp_module_modmap_file", + ), + ], + ), + ], + enabled = True, + ) + if ctx.attr.compiler == "clang": + flag_groups = [ + flag_group( + flags = ["-fmodule-output=%{cpp_module_output_file}"], + expand_if_available = "cpp_module_output_file", + ), + ] + else: + flag_groups = [] + cpp20_module_compile_flags_feature = feature( + name = "cpp20_module_compile_flags", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp20_module_compile, + ], + flag_groups = flag_groups, + ), + ], + enabled = True, + ) + # TODO(#8303): Mac crosstool should also declare every feature. if is_linux: # Linux artifact name patterns are the default. artifact_name_patterns = [] features = [ cpp_modules_feature, + cpp_module_modmap_file_feature, + cpp20_module_compile_flags_feature, dependency_file_feature, serialized_diagnostics_file_feature, random_seed_feature, @@ -1598,6 +1730,8 @@ def _impl(ctx): ] features = [ cpp_modules_feature, + cpp_module_modmap_file_feature, + cpp20_module_compile_flags_feature, macos_minimum_os_feature, macos_default_link_flags_feature, dependency_file_feature, diff --git a/tools/cpp/windows_cc_configure.bzl b/tools/cpp/windows_cc_configure.bzl index ea1ffb3872c3a8..8f9905e92d8713 100644 --- a/tools/cpp/windows_cc_configure.bzl +++ b/tools/cpp/windows_cc_configure.bzl @@ -803,6 +803,19 @@ def _get_clang_cl_vars(repository_ctx, paths, msvc_vars, target_arch): } return clang_cl_vars +def _get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, target_arch = "x64"): + repository_ctx.template( + "msvc_deps_scanner_wrapper_" + target_arch + ".bat", + paths["@bazel_tools//tools/cpp:msvc_deps_scanner_wrapper.bat.tpl"], + { + "%{cc}": template_vars["%{msvc_cl_path_" + target_arch + "}"], + }, + ) + + return { + "%{msvc_deps_scanner_wrapper_path_" + target_arch + "}": "msvc_deps_scanner_wrapper_" + target_arch + ".bat", + } + def configure_windows_toolchain(repository_ctx): """Configure C++ toolchain on Windows.""" paths = resolve_labels(repository_ctx, [ @@ -812,6 +825,7 @@ def configure_windows_toolchain(repository_ctx): "@bazel_tools//tools/cpp:vc_installation_error.bat.tpl", "@bazel_tools//tools/cpp:msys_gcc_installation_error.bat", "@bazel_tools//tools/cpp:clang_installation_error.bat.tpl", + "@bazel_tools//tools/cpp:msvc_deps_scanner_wrapper.bat.tpl", ]) repository_ctx.symlink( @@ -838,6 +852,10 @@ def configure_windows_toolchain(repository_ctx): template_vars.update(msvc_vars_arm64) template_vars.update(_get_clang_cl_vars(repository_ctx, paths, msvc_vars_arm64, "arm64")) + template_vars.update(_get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, "x64")) + template_vars.update(_get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, "x86")) + template_vars.update(_get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, "arm")) + template_vars.update(_get_msvc_deps_scanner_vars(repository_ctx, paths, template_vars, "arm64")) repository_ctx.template( "BUILD", paths["@bazel_tools//tools/cpp:BUILD.windows.tpl"], diff --git a/tools/cpp/windows_cc_toolchain_config.bzl b/tools/cpp/windows_cc_toolchain_config.bzl index fccfcb765f7525..72d641c0936336 100644 --- a/tools/cpp/windows_cc_toolchain_config.bzl +++ b/tools/cpp/windows_cc_toolchain_config.bzl @@ -40,6 +40,9 @@ all_compile_actions = [ ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ACTION_NAMES.lto_backend, ] @@ -50,6 +53,9 @@ all_cpp_compile_actions = [ ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.clif_match, ] @@ -60,6 +66,8 @@ preprocessor_compile_actions = [ ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ] @@ -70,6 +78,7 @@ codegen_compile_actions = [ ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ] @@ -257,6 +266,71 @@ def _impl(ctx): tools = [tool(path = ctx.attr.msvc_link_path)], ) + deps_scanner = "cpp-module-deps-scanner_not_found" + if "cpp-module-deps-scanner" in ctx.attr.tool_paths: + deps_scanner = ctx.attr.tool_paths["cpp-module-deps-scanner"] + cpp_module_scan_deps = action_config( + action_name = ACTION_NAMES.cpp_module_deps_scanning, + tools = [ + tool( + path = deps_scanner, + ), + ], + implies = [ + "compiler_input_flags", + "compiler_output_flags", + "nologo", + "msvc_env", + "user_compile_flags", + "sysroot", + ], + ) + + cpp20_module_compile = action_config( + action_name = ACTION_NAMES.cpp20_module_compile, + tools = [ + tool( + path = ctx.attr.msvc_cl_path, + ), + ], + flag_sets = [ + flag_set( + flag_groups = [ + flag_group( + flags = [ + "/TP", + "/interface", + ], + ), + ], + ), + ], + implies = [ + "compiler_input_flags", + "compiler_output_flags", + "nologo", + "msvc_env", + "user_compile_flags", + "sysroot", + ], + ) + + cpp20_module_codegen = action_config( + action_name = ACTION_NAMES.cpp20_module_codegen, + tools = [ + tool( + path = ctx.attr.msvc_cl_path, + ), + ], + implies = [ + "compiler_input_flags", + "compiler_output_flags", + "nologo", + "msvc_env", + "user_compile_flags", + "sysroot", + ], + ) action_configs = [ assemble_action, preprocess_assemble_action, @@ -267,6 +341,9 @@ def _impl(ctx): cpp_link_dynamic_library_action, cpp_link_nodeps_dynamic_library_action, cpp_link_static_library_action, + cpp_module_scan_deps, + cpp20_module_compile, + cpp20_module_codegen, ] else: action_configs = [] @@ -329,6 +406,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.cpp_link_executable, ACTION_NAMES.cpp_link_dynamic_library, ACTION_NAMES.cpp_link_nodeps_dynamic_library, @@ -357,6 +437,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -492,6 +575,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -651,6 +737,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ], @@ -687,6 +776,9 @@ def _impl(ctx): ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ], @@ -708,6 +800,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ], flag_groups = [ flag_group( @@ -816,6 +910,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ], flag_groups = [flag_group(flags = ["/showIncludes"])], ), @@ -900,6 +996,8 @@ def _impl(ctx): ACTION_NAMES.cpp_compile, ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, ACTION_NAMES.clif_match, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, @@ -984,6 +1082,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -1030,6 +1131,9 @@ def _impl(ctx): ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_link_executable, @@ -1083,6 +1187,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ], flag_groups = [ flag_group( @@ -1120,6 +1227,9 @@ def _impl(ctx): ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_link_executable, @@ -1221,6 +1331,9 @@ def _impl(ctx): ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, ACTION_NAMES.cpp_header_parsing, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.assemble, ACTION_NAMES.preprocess_assemble, ACTION_NAMES.cpp_link_executable, @@ -1246,6 +1359,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ], @@ -1341,6 +1457,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ACTION_NAMES.cpp_link_executable, @@ -1404,6 +1523,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.lto_backend, ACTION_NAMES.clif_match, ], @@ -1445,9 +1567,55 @@ def _impl(ctx): if "dumpbin" in ctx.attr.tool_paths: make_variables.append(make_variable(name = "DUMPBIN", value = ctx.attr.tool_paths["dumpbin"])) + # Tell bazel we support C++ modules now + cpp_modules_feature = feature( + name = "cpp_modules", + # set default value to False + # to enable the feature + # use --features=cpp_modules + # or add cpp_modules to features attr + enabled = False, + ) + + cpp_module_modmap_file_feature = feature( + name = "cpp_module_modmap_file", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp_compile, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, + ], + flag_groups = [ + flag_group( + flags = ["@%{cpp_module_modmap_file}"], + expand_if_available = "cpp_module_modmap_file", + ), + ], + ), + ], + enabled = True, + ) + cpp20_module_compile_flags_feature = feature( + name = "cpp20_module_compile_flags", + flag_sets = [ + flag_set( + actions = [ + ACTION_NAMES.cpp20_module_compile, + ], + flag_groups = [ + flag_group( + flags = ["/ifcOutput%{cpp_module_output_file}"], + expand_if_available = "cpp_module_output_file", + ), + ], + ), + ], + enabled = True, + ) return cc_common.create_cc_toolchain_config_info( ctx = ctx, - features = features, + features = features + [cpp_modules_feature, cpp_module_modmap_file_feature, cpp20_module_compile_flags_feature], action_configs = action_configs, artifact_name_patterns = artifact_name_patterns, cxx_builtin_include_directories = ctx.attr.cxx_builtin_include_directories, From ae05dee658ad5788ba38d1309a84adb00fd8c53e Mon Sep 17 00:00:00 2001 From: PikachuHy Date: Tue, 23 Jul 2024 11:13:13 +0800 Subject: [PATCH 2/2] update compiler_input_flags_feature and compiler_output_flags_feature --- tools/cpp/unix_cc_toolchain_config.bzl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/cpp/unix_cc_toolchain_config.bzl b/tools/cpp/unix_cc_toolchain_config.bzl index c5a184a80111b4..9b5d95efbfe56e 100644 --- a/tools/cpp/unix_cc_toolchain_config.bzl +++ b/tools/cpp/unix_cc_toolchain_config.bzl @@ -504,6 +504,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, ACTION_NAMES.lto_backend, @@ -532,6 +535,9 @@ def _impl(ctx): ACTION_NAMES.cpp_header_parsing, ACTION_NAMES.cpp_module_compile, ACTION_NAMES.cpp_module_codegen, + ACTION_NAMES.cpp_module_deps_scanning, + ACTION_NAMES.cpp20_module_compile, + ACTION_NAMES.cpp20_module_codegen, ACTION_NAMES.objc_compile, ACTION_NAMES.objcpp_compile, ACTION_NAMES.lto_backend,