Skip to content

Commit

Permalink
Add feature detection
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jun 7, 2024
1 parent fd5054d commit 280480b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
1 change: 1 addition & 0 deletions tools/cpp/BUILD.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ cc_toolchain_config(
coverage_compile_flags = [%{coverage_compile_flags}],
coverage_link_flags = [%{coverage_link_flags}],
supports_start_end_lib = %{supports_start_end_lib},
extra_flags_per_feature = %{extra_flags_per_feature},
)

# Android tooling requires a default toolchain for the armeabi-v7a cpu.
Expand Down
17 changes: 17 additions & 0 deletions tools/cpp/unix_cc_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,22 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
builtin_include_directories,
paths["@bazel_tools//tools/cpp:generate_system_module_map.sh"],
))
extra_flags_per_feature = {}
if is_clang:
# Only supported by LLVM 14 and later, but required with C++20 and
# layering_check as C++ modules are the default.
# https://github.com/llvm/llvm-project/commit/0556138624edf48621dd49a463dbe12e7101f17d
result = repository_ctx.execute([
cc,
"-Xclang",
"-fno-cxx-modules",
"-o",
"/dev/null",
"-c",
str(repository_ctx.path("tools/cpp/empty.cc")),
])
if "-fno-cxx-modules" not in result.stderr:
extra_flags_per_feature["use_header_modules"] = ["-Xclang", "-fno-cxx-modules"]

write_builtin_include_directory_paths(repository_ctx, cc, builtin_include_directories)
repository_ctx.template(
Expand Down Expand Up @@ -696,5 +712,6 @@ def configure_unix_toolchain(repository_ctx, cpu_value, overriden_tools):
"%{coverage_compile_flags}": coverage_compile_flags,
"%{coverage_link_flags}": coverage_link_flags,
"%{supports_start_end_lib}": "True" if gold_or_lld_linker_path else "False",
"%{extra_flags_per_feature}": repr(extra_flags_per_feature),
},
)
12 changes: 5 additions & 7 deletions tools/cpp/unix_cc_toolchain_config.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _target_os_version(ctx):
xcode_config = ctx.attr._xcode_config[apple_common.XcodeVersionConfig]
return xcode_config.minimum_os_for_platform_type(platform_type)

def layering_check_features(compiler, is_macos):
def layering_check_features(compiler, extra_flags_per_feature, is_macos):
if compiler != "clang":
return []
return [
Expand All @@ -54,14 +54,11 @@ def layering_check_features(compiler, is_macos):
flag_groups = [
flag_group(
# macOS requires -Xclang because of a bug in Apple Clang
# -fno-cxx-modules is necessary to avoid clang defaulting
# to C++ modules with -std=c++20. The flag requires the
# -Xclang prefix even on Linux.
flags = (["-Xclang"] if is_macos else []) + [
"-fmodule-name=%{module_name}",
] + (["-Xclang"] if is_macos else []) + [
"-fmodule-map-file=%{module_map_file}",
] + ["-Xclang", "-fno-cxx-modules"],
] + extra_flags_per_feature.get("use_module_maps", []),
),
],
),
Expand Down Expand Up @@ -1492,7 +1489,7 @@ def _impl(ctx):
unfiltered_compile_flags_feature,
treat_warnings_as_errors_feature,
archive_param_file_feature,
] + layering_check_features(ctx.attr.compiler, is_macos = False)
] + layering_check_features(ctx.attr.compiler, ctx.attr.extra_flags_per_feature, is_macos = False)
else:
# macOS artifact name patterns differ from the defaults only for dynamic
# libraries.
Expand Down Expand Up @@ -1533,7 +1530,7 @@ def _impl(ctx):
treat_warnings_as_errors_feature,
archive_param_file_feature,
generate_linkmap_feature,
] + layering_check_features(ctx.attr.compiler, is_macos = True)
] + layering_check_features(ctx.attr.compiler, ctx.attr.extra_flags_per_feature, is_macos = True)

parse_headers_action_configs, parse_headers_features = parse_headers_support(
parse_headers_tool_path = ctx.attr.tool_paths.get("parse_headers"),
Expand Down Expand Up @@ -1586,6 +1583,7 @@ cc_toolchain_config = rule(
"coverage_link_flags": attr.string_list(),
"supports_start_end_lib": attr.bool(),
"builtin_sysroot": attr.string(),
"extra_flags_per_feature": attr.string_list_dict(),
"_xcode_config": attr.label(default = configuration_field(
fragment = "apple",
name = "xcode_config_label",
Expand Down

0 comments on commit 280480b

Please sign in to comment.