Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions toolchain/cc_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def cc_toolchain(name, tool_map, module_map = None, extra_args = []):
"@llvm//toolchain/features:dbg",
"@llvm//toolchain/features:archive_param_file",
"@llvm//toolchain/features:parse_headers",
"@llvm//toolchain/features/header_modules:all_header_module_features",
"@llvm//toolchain/features/legacy:all_legacy_builtin_features",
# Always last (contains user_compile_flags and user_link_flags who should apply last).
"@llvm//toolchain/features/legacy:experimental_replace_legacy_action_config_features",
Expand Down
142 changes: 142 additions & 0 deletions toolchain/features/header_modules/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
load("@rules_cc//cc/toolchains:args.bzl", "cc_args")
load("@rules_cc//cc/toolchains:feature.bzl", "cc_feature")
load("@rules_cc//cc/toolchains:feature_set.bzl", "cc_feature_set")

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

# Based on https://github.com/bazelbuild/bazel/issues/4005#issuecomment-1212952697.
#
# Features that control header modules.
#
# We have different features for module consumers and producers:
# 'header_modules' is enabled for targets that support being compiled as a
# header module.
# 'use_header_modules' is enabled for targets that want to use the provided
# header modules from their transitive closure. We enable this globally and
# disable it for targets that do not support builds with header modules.
#
# Allow switching off header modules completely by specifying
# features=["-use_header_modules"] in a rule.

cc_args(
name = "header_module_compile_args",
actions = ["@rules_cc//cc/toolchains/actions:cpp_module_compile"],
args = [
"-xc++",
"-Xclang",
"-emit-module",
"-Xclang",
"-fmodules-embed-all-files",
"-Xclang",
"-fmodules-local-submodule-visibility",
],
)

cc_feature(
name = "header_module_compile",
args = [":header_module_compile_args"],
feature_name = "header_module_compile",
)

cc_feature(
name = "header_modules",
feature_name = "header_modules",
implies = [":header_module_compile"],
requires_any_of = [":use_header_modules"],
)

cc_feature(
name = "header_module_codegen",
feature_name = "header_module_codegen",
requires_any_of = [":header_modules"],
)

cc_args(
name = "header_modules_codegen_functions_args",
actions = ["@rules_cc//cc/toolchains/actions:cpp_module_compile"],
args = [
"-Xclang",
"-fmodules-codegen",
],
)

cc_feature(
name = "header_modules_codegen_functions",
args = [":header_modules_codegen_functions_args"],
feature_name = "header_modules_codegen_functions",
implies = [":header_module_codegen"],
)

cc_args(
name = "header_modules_codegen_debuginfo_args",
actions = ["@rules_cc//cc/toolchains/actions:cpp_module_compile"],
args = [
"-Xclang",
"-fmodules-debuginfo",
],
)

cc_feature(
name = "header_modules_codegen_debuginfo",
args = [":header_modules_codegen_debuginfo_args"],
feature_name = "header_modules_codegen_debuginfo",
implies = [":header_module_codegen"],
)

cc_args(
name = "use_header_modules_args",
actions = [
"@rules_cc//cc/toolchains/actions:cpp_compile",
"@rules_cc//cc/toolchains/actions:cpp_header_parsing",
"@rules_cc//cc/toolchains/actions:cpp_module_compile",
],
args = [
"-fmodules",
"-fmodule-file-deps",
"-fno-implicit-modules",
"-fno-implicit-module-maps",
"-Wno-modules-ambiguous-internal-linkage",
"-Wno-module-import-in-extern-c",
"-Wno-modules-import-nested-redundant",
],
)

cc_args(
name = "use_header_modules_module_files_args",
actions = [
"@rules_cc//cc/toolchains/actions:cpp_compile",
"@rules_cc//cc/toolchains/actions:cpp_header_parsing",
"@rules_cc//cc/toolchains/actions:cpp_module_compile",
],
args = [
"-Xclang",
"-fmodule-file={module_files}",
],
format = {
"module_files": "@rules_cc//cc/toolchains/variables:module_files",
},
iterate_over = "@rules_cc//cc/toolchains/variables:module_files",
requires_not_none = "@rules_cc//cc/toolchains/variables:module_files",
)

cc_feature(
name = "use_header_modules",
args = [
":use_header_modules_args",
":use_header_modules_module_files_args",
],
feature_name = "use_header_modules",
implies = ["@rules_cc//cc/toolchains/args/layering_check:use_module_maps"],
)

cc_feature_set(
name = "all_header_module_features",
all_of = [
":header_module_compile",
":header_modules",
":header_module_codegen",
":header_modules_codegen_functions",
":header_modules_codegen_debuginfo",
":use_header_modules",
],
)
Loading