Skip to content

Commit 5932b3b

Browse files
keithShreeM01
andauthored
[6.1.0] Add --host_features (bazelbuild#17528)
* 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 bazelbuild#13839 Closes bazelbuild#16626. PiperOrigin-RevId: 509809427 Change-Id: I3fadb1e28267c096a25d8841648175306ee73f2e (cherry picked from commit 0ef9c7c) * Fix tests --------- Co-authored-by: kshyanashree <[email protected]>
1 parent ec36595 commit 5932b3b

File tree

2 files changed

+66
-5
lines changed

2 files changed

+66
-5
lines changed

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

+34-5
Original file line numberDiff line numberDiff line change
@@ -629,13 +629,37 @@ public OutputDirectoryNamingSchemeConverter() {
629629
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
630630
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
631631
help =
632-
"The given features will be enabled or disabled by default for all packages. "
633-
+ "Specifying -<feature> will disable the feature globally. "
632+
"The given features will be enabled or disabled by default for targets "
633+
+ "built in the target configuration. "
634+
+ "Specifying -<feature> will disable the feature. "
634635
+ "Negative features always override positive ones. "
635-
+ "This flag is used to enable rolling out default feature changes without a "
636-
+ "Bazel release.")
636+
+ "See also --host_features")
637637
public List<String> defaultFeatures;
638638

639+
@Option(
640+
name = "host_features",
641+
allowMultiple = true,
642+
defaultValue = "null",
643+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
644+
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
645+
help =
646+
"The given features will be enabled or disabled by default for targets "
647+
+ "built in the exec configuration. "
648+
+ "Specifying -<feature> will disable the feature. "
649+
+ "Negative features always override positive ones.")
650+
public List<String> hostFeatures;
651+
652+
@Option(
653+
name = "incompatible_use_host_features",
654+
defaultValue = "false",
655+
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
656+
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
657+
metadataTags = {OptionMetadataTag.INCOMPATIBLE_CHANGE},
658+
help =
659+
"If true, use --features only for the target configuration and --host_features for the"
660+
+ " exec configuration.")
661+
public boolean incompatibleUseHostFeatures;
662+
639663
@Option(
640664
name = "target_environment",
641665
converter = LabelListConverter.class,
@@ -960,7 +984,12 @@ public FragmentOptions getHost() {
960984
host.checkLicenses = checkLicenses;
961985

962986
// === Pass on C++ compiler features.
963-
host.defaultFeatures = ImmutableList.copyOf(defaultFeatures);
987+
host.incompatibleUseHostFeatures = incompatibleUseHostFeatures;
988+
if (incompatibleUseHostFeatures) {
989+
host.defaultFeatures = ImmutableList.copyOf(hostFeatures);
990+
} else {
991+
host.defaultFeatures = ImmutableList.copyOf(defaultFeatures);
992+
}
964993

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

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

+32
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,38 @@ 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(
66+
"--features=feature",
67+
"--host_features=host_feature",
68+
"--incompatible_use_host_features=true");
69+
scratch.file("a/BUILD", "cc_library(name = 'a')");
70+
ImmutableSet<String> features =
71+
getRuleContext(getConfiguredTarget("//a", getHostConfiguration())).getFeatures();
72+
assertThat(features).contains("host_feature");
73+
assertThat(features).doesNotContain("feature");
74+
}
75+
76+
@Test
77+
public void testHostFeaturesIncompatibleDisabled() throws Exception {
78+
useConfiguration("--features=feature", "--host_features=host_feature");
79+
scratch.file("a/BUILD", "cc_library(name = 'a')");
80+
ImmutableSet<String> features =
81+
getRuleContext(getConfiguredTarget("//a", getHostConfiguration())).getFeatures();
82+
assertThat(features).contains("feature");
83+
assertThat(features).doesNotContain("host_feature");
84+
}
85+
5486
@Test
5587
public void testFeatureDisabledOnCommandLine() throws Exception {
5688
useConfiguration("--features=-feature");

0 commit comments

Comments
 (0)