Skip to content

Commit 541eda5

Browse files
dzbarskycopybara-github
authored andcommitted
Only pass profile-correction flag on GCC
Copybara Import from #580 cloned from unknown commit to add BtrGuardian exclusion BEGIN_PUBLIC Only pass profile-correction flag on GCC (#580) This is a GCC flag, Clang emits a warning and ignores it. Closes #580 END_PUBLIC COPYBARA_INTEGRATE_REVIEW=#580 from dzbarsky:zbarsky/profile-correction 7970c98 PiperOrigin-RevId: 871744266 Change-Id: I7b3664d7f6f0673327cf4be05b11d28ec1b9a9dc
1 parent ae1df74 commit 541eda5

5 files changed

Lines changed: 28 additions & 16 deletions

File tree

cc/cc_toolchain_config_lib.bzl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,3 +614,9 @@ def artifact_name_pattern(category_name, prefix, extension):
614614
extension = extension,
615615
type_name = "artifact_name_pattern",
616616
)
617+
618+
def _is_gcc_compiler(compiler):
619+
return "gcc" in compiler
620+
621+
def get_profile_correction_flags(ctx):
622+
return ["-fprofile-correction"] if hasattr(ctx.attr, "compiler") and _is_gcc_compiler(ctx.attr.compiler) else []

cc/private/toolchain/unix_cc_toolchain_config.bzl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ load(
2424
"feature_set",
2525
"flag_group",
2626
"flag_set",
27+
"get_profile_correction_flags",
2728
"tool",
2829
"tool_path",
2930
"variable_with_value",
@@ -237,6 +238,7 @@ def _sanitizer_feature(name = "", specific_compile_flags = [], specific_link_fla
237238

238239
def _impl(ctx):
239240
is_linux = ctx.attr.target_libc != "macosx"
241+
profile_correction_flags = get_profile_correction_flags(ctx)
240242

241243
tool_paths = [
242244
tool_path(name = name, path = path)
@@ -623,8 +625,7 @@ def _impl(ctx):
623625
flag_group(
624626
flags = [
625627
"-fprofile-use=%{fdo_profile_path}",
626-
"-fprofile-correction",
627-
],
628+
] + profile_correction_flags,
628629
expand_if_available = "fdo_profile_path",
629630
),
630631
],
@@ -786,8 +787,7 @@ def _impl(ctx):
786787
"-fprofile-use=%{fdo_profile_path}",
787788
"-Wno-profile-instr-unprofiled",
788789
"-Wno-profile-instr-out-of-date",
789-
"-fprofile-correction",
790-
],
790+
] + profile_correction_flags,
791791
expand_if_available = "fdo_profile_path",
792792
),
793793
],
@@ -805,8 +805,7 @@ def _impl(ctx):
805805
flag_group(
806806
flags = [
807807
"-fauto-profile=%{fdo_profile_path}",
808-
"-fprofile-correction",
809-
],
808+
] + profile_correction_flags,
810809
expand_if_available = "fdo_profile_path",
811810
),
812811
],

cc/private/toolchain/windows_cc_toolchain_config.bzl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ load(
2323
"feature",
2424
"flag_group",
2525
"flag_set",
26+
"get_profile_correction_flags",
2627
"make_variable",
2728
"tool",
2829
"tool_path",
@@ -100,6 +101,8 @@ def _use_msvc_toolchain(ctx):
100101
return ctx.attr.cpu in ["x64_windows", "arm64_windows"] and (ctx.attr.compiler == "msvc-cl" or ctx.attr.compiler == "clang-cl")
101102

102103
def _impl(ctx):
104+
profile_correction_flags = get_profile_correction_flags(ctx)
105+
103106
if _use_msvc_toolchain(ctx):
104107
artifact_name_patterns = [
105108
artifact_name_pattern(
@@ -1551,8 +1554,7 @@ def _impl(ctx):
15511554
flag_group(
15521555
flags = [
15531556
"-fprofile-use=%{fdo_profile_path}",
1554-
"-fprofile-correction",
1555-
],
1557+
] + profile_correction_flags,
15561558
expand_if_available = "fdo_profile_path",
15571559
),
15581560
],

cc/private/toolchain_config/cc_toolchain_config_info.bzl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ def create_cc_toolchain_config_info(
102102
default_compile_flags = ([f for f in features if f.name == "default_compile_flags"])[0]
103103
legacy_features.append(default_compile_flags)
104104
platform = "mac" if target_libc == "macosx" else "linux"
105-
legacy_features.extend(get_legacy_features(platform, feature_names, linker_tool_path))
105+
legacy_features.extend(get_legacy_features(
106+
ctx,
107+
platform,
108+
feature_names,
109+
linker_tool_path,
110+
))
106111
legacy_features.extend([f for f in features if f.name not in ["legacy_compile_flags", "default_compile_flags"]])
107112
legacy_features.extend(get_features_to_appear_last(feature_names))
108113

cc/private/toolchain_config/legacy_features.bzl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,29 @@ load(
2121
"feature_set",
2222
"flag_group",
2323
"flag_set",
24+
"get_profile_correction_flags",
2425
"tool",
2526
"variable_with_value",
2627
"with_feature_set",
2728
)
2829

29-
def get_legacy_features(platform, existing_feature_names, linker_tool_path):
30+
def get_legacy_features(ctx, platform, existing_feature_names, linker_tool_path):
3031
"""The features added to all legacy toolchains
3132
3233
Note: these features won't be added to the crosstools that defines
3334
no_legacy_features feature (e.g. ndk, apple, enclave crosstools). Those need
3435
to be modified separately.
3536
3637
Args:
38+
ctx: bazel rule context
3739
platform: (str) One of 'linux' or 'mac'
3840
existing_feature_names: ([str])
3941
linker_tool_path: (str)
4042
4143
Returns:
4244
([FeatureInfo])
4345
"""
46+
profile_correction_flags = get_profile_correction_flags(ctx)
4447
result = []
4548
if "legacy_compile_flags" not in existing_feature_names:
4649
result.append(feature(
@@ -285,8 +288,7 @@ def get_legacy_features(platform, existing_feature_names, linker_tool_path):
285288
"-fprofile-use=%{fdo_profile_path}",
286289
"-Wno-profile-instr-unprofiled",
287290
"-Wno-profile-instr-out-of-date",
288-
"-fprofile-correction",
289-
],
291+
] + profile_correction_flags,
290292
)],
291293
)],
292294
))
@@ -326,8 +328,7 @@ def get_legacy_features(platform, existing_feature_names, linker_tool_path):
326328
"-fprofile-use=%{fdo_profile_path}",
327329
"-Wno-profile-instr-unprofiled",
328330
"-Wno-profile-instr-out-of-date",
329-
"-fprofile-correction",
330-
],
331+
] + profile_correction_flags,
331332
)],
332333
)],
333334
))
@@ -364,8 +365,7 @@ def get_legacy_features(platform, existing_feature_names, linker_tool_path):
364365
expand_if_available = "fdo_profile_path",
365366
flags = [
366367
"-fauto-profile=%{fdo_profile_path}",
367-
"-fprofile-correction",
368-
],
368+
] + profile_correction_flags,
369369
)],
370370
)],
371371
))

0 commit comments

Comments
 (0)