Skip to content

Commit 25d17f5

Browse files
jheaff1copybara-github
authored andcommitted
Allow removal of default cc flags
This PR modifies the cc toolchain logic so that users can remove (most of) the default compile/linker flags if they so wish. This is required when building 3rd party projects, e.g. Qt, using Bazel. Closes #14735. PiperOrigin-RevId: 471502084 Change-Id: Idbc3e6685bc10c6b51fdff7ab7839dc9e00a5209
1 parent 63ddfc4 commit 25d17f5

File tree

2 files changed

+42
-83
lines changed

2 files changed

+42
-83
lines changed

src/main/java/com/google/devtools/build/lib/rules/cpp/CcCommon.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,22 +1050,6 @@ public static FeatureConfiguration configureFeaturesOrThrowEvalException(
10501050

10511051
ImmutableSet<String> allUnsupportedFeatures = unsupportedFeaturesBuilder.build();
10521052

1053-
// If STATIC_LINK_MSVCRT feature isn't specified by user, we add DYNAMIC_LINK_MSVCRT_* feature
1054-
// according to compilation mode.
1055-
// If STATIC_LINK_MSVCRT feature is specified, we add STATIC_LINK_MSVCRT_* feature
1056-
// according to compilation mode.
1057-
if (requestedFeatures.contains(CppRuleClasses.STATIC_LINK_MSVCRT)) {
1058-
allRequestedFeaturesBuilder.add(
1059-
cppConfiguration.getCompilationMode() == CompilationMode.DBG
1060-
? CppRuleClasses.STATIC_LINK_MSVCRT_DEBUG
1061-
: CppRuleClasses.STATIC_LINK_MSVCRT_NO_DEBUG);
1062-
} else {
1063-
allRequestedFeaturesBuilder.add(
1064-
cppConfiguration.getCompilationMode() == CompilationMode.DBG
1065-
? CppRuleClasses.DYNAMIC_LINK_MSVCRT_DEBUG
1066-
: CppRuleClasses.DYNAMIC_LINK_MSVCRT_NO_DEBUG);
1067-
}
1068-
10691053
ImmutableList.Builder<String> allFeatures =
10701054
new ImmutableList.Builder<String>()
10711055
.addAll(ImmutableSet.of(cppConfiguration.getCompilationMode().toString()))

tools/cpp/windows_cc_toolchain_config.bzl

Lines changed: 42 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ load(
2121
"env_entry",
2222
"env_set",
2323
"feature",
24-
"feature_set",
2524
"flag_group",
2625
"flag_set",
2726
"tool",
@@ -135,7 +134,6 @@ def _impl(ctx):
135134
"output_execpath_flags",
136135
"input_param_flags",
137136
"user_link_flags",
138-
"default_link_flags",
139137
"linker_subsystem_flag",
140138
"linker_param_file",
141139
"msvc_env",
@@ -187,13 +185,11 @@ def _impl(ctx):
187185
implies = [
188186
"compiler_input_flags",
189187
"compiler_output_flags",
190-
"default_compile_flags",
191188
"nologo",
192189
"msvc_env",
193190
"parse_showincludes",
194191
"user_compile_flags",
195192
"sysroot",
196-
"unfiltered_compile_flags",
197193
],
198194
tools = [tool(path = ctx.attr.msvc_cl_path)],
199195
)
@@ -219,13 +215,11 @@ def _impl(ctx):
219215
implies = [
220216
"compiler_input_flags",
221217
"compiler_output_flags",
222-
"default_compile_flags",
223218
"nologo",
224219
"msvc_env",
225220
"parse_showincludes",
226221
"user_compile_flags",
227222
"sysroot",
228-
"unfiltered_compile_flags",
229223
],
230224
tools = [tool(path = ctx.attr.msvc_cl_path)],
231225
)
@@ -238,7 +232,6 @@ def _impl(ctx):
238232
"output_execpath_flags",
239233
"input_param_flags",
240234
"user_link_flags",
241-
"default_link_flags",
242235
"linker_subsystem_flag",
243236
"linker_param_file",
244237
"msvc_env",
@@ -256,7 +249,6 @@ def _impl(ctx):
256249
"output_execpath_flags",
257250
"input_param_flags",
258251
"user_link_flags",
259-
"default_link_flags",
260252
"linker_subsystem_flag",
261253
"linker_param_file",
262254
"msvc_env",
@@ -356,6 +348,7 @@ def _impl(ctx):
356348

357349
unfiltered_compile_flags_feature = feature(
358350
name = "unfiltered_compile_flags",
351+
enabled = True,
359352
flag_sets = [
360353
flag_set(
361354
actions = [
@@ -540,21 +533,57 @@ def _impl(ctx):
540533
],
541534
)
542535

543-
static_link_msvcrt_feature = feature(name = "static_link_msvcrt")
536+
static_link_msvcrt_feature = feature(
537+
name = "static_link_msvcrt",
538+
flag_sets = [
539+
flag_set(
540+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
541+
flag_groups = [flag_group(flags = ["/MT"])],
542+
with_features = [with_feature_set(not_features = ["dbg"])],
543+
),
544+
flag_set(
545+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
546+
flag_groups = [flag_group(flags = ["/MTd"])],
547+
with_features = [with_feature_set(features = ["dbg"])],
548+
),
549+
flag_set(
550+
actions = all_link_actions,
551+
flag_groups = [flag_group(flags = ["/DEFAULTLIB:libcmt.lib"])],
552+
with_features = [with_feature_set(not_features = ["dbg"])],
553+
),
554+
flag_set(
555+
actions = all_link_actions,
556+
flag_groups = [flag_group(flags = ["/DEFAULTLIB:libcmtd.lib"])],
557+
with_features = [with_feature_set(features = ["dbg"])],
558+
),
559+
],
560+
)
544561

545-
dynamic_link_msvcrt_debug_feature = feature(
546-
name = "dynamic_link_msvcrt_debug",
562+
dynamic_link_msvcrt_feature = feature(
563+
name = "dynamic_link_msvcrt",
564+
enabled = True,
547565
flag_sets = [
566+
flag_set(
567+
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
568+
flag_groups = [flag_group(flags = ["/MD"])],
569+
with_features = [with_feature_set(not_features = ["dbg", "static_link_msvcrt"])],
570+
),
548571
flag_set(
549572
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
550573
flag_groups = [flag_group(flags = ["/MDd"])],
574+
with_features = [with_feature_set(features = ["dbg"], not_features = ["static_link_msvcrt"])],
575+
),
576+
flag_set(
577+
actions = all_link_actions,
578+
flag_groups = [flag_group(flags = ["/DEFAULTLIB:msvcrt.lib"])],
579+
with_features = [with_feature_set(not_features = ["dbg", "static_link_msvcrt"])],
551580
),
552581
flag_set(
553582
actions = all_link_actions,
554583
flag_groups = [flag_group(flags = ["/DEFAULTLIB:msvcrtd.lib"])],
584+
with_features = [with_feature_set(features = ["dbg"], not_features = ["static_link_msvcrt"])],
555585
),
556586
],
557-
requires = [feature_set(features = ["dbg"])],
558587
)
559588

560589
dbg_feature = feature(
@@ -709,24 +738,6 @@ def _impl(ctx):
709738
],
710739
)
711740

712-
dynamic_link_msvcrt_no_debug_feature = feature(
713-
name = "dynamic_link_msvcrt_no_debug",
714-
flag_sets = [
715-
flag_set(
716-
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
717-
flag_groups = [flag_group(flags = ["/MD"])],
718-
),
719-
flag_set(
720-
actions = all_link_actions,
721-
flag_groups = [flag_group(flags = ["/DEFAULTLIB:msvcrt.lib"])],
722-
),
723-
],
724-
requires = [
725-
feature_set(features = ["fastbuild"]),
726-
feature_set(features = ["opt"]),
727-
],
728-
)
729-
730741
disable_assertions_feature = feature(
731742
name = "disable_assertions",
732743
enabled = True,
@@ -791,24 +802,6 @@ def _impl(ctx):
791802
],
792803
)
793804

794-
static_link_msvcrt_no_debug_feature = feature(
795-
name = "static_link_msvcrt_no_debug",
796-
flag_sets = [
797-
flag_set(
798-
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
799-
flag_groups = [flag_group(flags = ["/MT"])],
800-
),
801-
flag_set(
802-
actions = all_link_actions,
803-
flag_groups = [flag_group(flags = ["/DEFAULTLIB:libcmt.lib"])],
804-
),
805-
],
806-
requires = [
807-
feature_set(features = ["fastbuild"]),
808-
feature_set(features = ["opt"]),
809-
],
810-
)
811-
812805
treat_warnings_as_errors_feature = feature(
813806
name = "treat_warnings_as_errors",
814807
flag_sets = [
@@ -887,21 +880,6 @@ def _impl(ctx):
887880
],
888881
)
889882

890-
static_link_msvcrt_debug_feature = feature(
891-
name = "static_link_msvcrt_debug",
892-
flag_sets = [
893-
flag_set(
894-
actions = [ACTION_NAMES.c_compile, ACTION_NAMES.cpp_compile],
895-
flag_groups = [flag_group(flags = ["/MTd"])],
896-
),
897-
flag_set(
898-
actions = all_link_actions,
899-
flag_groups = [flag_group(flags = ["/DEFAULTLIB:libcmtd.lib"])],
900-
),
901-
],
902-
requires = [feature_set(features = ["dbg"])],
903-
)
904-
905883
frame_pointer_feature = feature(
906884
name = "frame_pointer",
907885
flag_sets = [
@@ -1105,10 +1083,7 @@ def _impl(ctx):
11051083
default_link_flags_feature,
11061084
linker_param_file_feature,
11071085
static_link_msvcrt_feature,
1108-
static_link_msvcrt_no_debug_feature,
1109-
dynamic_link_msvcrt_no_debug_feature,
1110-
static_link_msvcrt_debug_feature,
1111-
dynamic_link_msvcrt_debug_feature,
1086+
dynamic_link_msvcrt_feature,
11121087
dbg_feature,
11131088
fastbuild_feature,
11141089
opt_feature,

0 commit comments

Comments
 (0)