Skip to content

Commit 3163cb8

Browse files
committed
Add --host_features
Previously there was no way to have a feature only apply to the entire transitive target closure without `--features` which also applied to the host / exec configuration. This is undesirable for many features such as C++ sanitizers where you often only need them to affect targets in the target configuration, and you don't want to invalidate all host tools when switching between these build configurations. RELNOTES[INC]: `--features` only applies to targets built in the target configuration, and `--host_features` is used for the host / exec configuration (gated behind `--incompatible_use_host_features`) Fixes #13839
1 parent 7227fd4 commit 3163cb8

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,35 @@ public OutputDirectoryNamingSchemeConverter() {
646646
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
647647
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
648648
help =
649-
"The given features will be enabled or disabled by default for all packages. "
650-
+ "Specifying -<feature> will disable the feature globally. "
649+
"The given features will be enabled or disabled by default for targets "
650+
+ "built in the target configuration. "
651+
+ "Specifying -<feature> will disable the feature. "
651652
+ "Negative features always override positive ones. "
652-
+ "This flag is used to enable rolling out default feature changes without a "
653-
+ "Bazel release.")
653+
+ "See also --host_features")
654654
public List<String> defaultFeatures;
655655

656+
@Option(
657+
name = "host_features",
658+
allowMultiple = true,
659+
defaultValue = "null",
660+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
661+
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
662+
help =
663+
"The given features will be enabled or disabled by default for targets "
664+
+ "built in the exec configuration. "
665+
+ "Specifying -<feature> will disable the feature. "
666+
+ "Negative features always override positive ones.")
667+
public List<String> hostFeatures;
668+
669+
@Option(
670+
name = "incompatible_use_host_features",
671+
defaultValue = "false",
672+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
673+
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
674+
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
675+
help = "If true, use --features only for the target configuration and --host_features for the exec configuration.")
676+
public boolean incompatibleUseHostFeatures;
677+
656678
@Option(
657679
name = "target_environment",
658680
converter = LabelListConverter.class,
@@ -989,7 +1011,12 @@ public FragmentOptions getExec() {
9891011
exec.checkLicenses = checkLicenses;
9901012

9911013
// === Pass on C++ compiler features.
992-
exec.defaultFeatures = ImmutableList.copyOf(defaultFeatures);
1014+
exec.incompatibleUseHostFeatures = incompatibleUseHostFeatures;
1015+
if (incompatibleUseHostFeatures) {
1016+
exec.defaultFeatures = ImmutableList.copyOf(hostFeatures);
1017+
} else {
1018+
exec.defaultFeatures = ImmutableList.copyOf(defaultFeatures);
1019+
}
9931020

9941021
// Save host options in case of a further exec->host transition.
9951022
exec.hostCpu = hostCpu;

src/test/java/com/google/devtools/build/lib/analysis/RuleConfiguredTargetTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,36 @@ public void testFeatureEnabledOnCommandLine() throws Exception {
5151
assertThat(features).doesNotContain("other");
5252
}
5353

54+
@Test
55+
public void testTargetIgnoresHostFeatures() throws Exception {
56+
useConfiguration("--features=feature", "--host_features=host_feature");
57+
scratch.file("a/BUILD", "cc_library(name = 'a')");
58+
ImmutableSet<String> features = getRuleContext(configure("//a")).getFeatures();
59+
assertThat(features).contains("feature");
60+
assertThat(features).doesNotContain("host_feature");
61+
}
62+
63+
@Test
64+
public void testHostFeatures() throws Exception {
65+
useConfiguration("--features=feature", "--host_features=host_feature",
66+
"--incompatible_use_host_features=true");
67+
scratch.file("a/BUILD", "cc_library(name = 'a')");
68+
ImmutableSet<String> features = getRuleContext(
69+
getConfiguredTarget("//a", getExecConfiguration())).getFeatures();
70+
assertThat(features).contains("host_feature");
71+
assertThat(features).doesNotContain("feature");
72+
}
73+
74+
@Test
75+
public void testHostFeaturesIncompatibleDisabled() throws Exception {
76+
useConfiguration("--features=feature", "--host_features=host_feature");
77+
scratch.file("a/BUILD", "cc_library(name = 'a')");
78+
ImmutableSet<String> features = getRuleContext(
79+
getConfiguredTarget("//a", getExecConfiguration())).getFeatures();
80+
assertThat(features).contains("feature");
81+
assertThat(features).doesNotContain("host_feature");
82+
}
83+
5484
@Test
5585
public void testFeatureDisabledOnCommandLine() throws Exception {
5686
useConfiguration("--features=-feature");

0 commit comments

Comments
 (0)