Skip to content

Commit

Permalink
Add --host_features
Browse files Browse the repository at this point in the history
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.

Now `--features` only applies to targets built in the target
configuration, and `--host_features` is used for the host / exec
configuration.

Fixes bazelbuild#13839
  • Loading branch information
keith committed Nov 4, 2022
1 parent a67c823 commit cba5cc2
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,19 @@ public OutputDirectoryNamingSchemeConverter() {
+ "Bazel release.")
public List<String> defaultFeatures;

@Option(
name = "host_features",
allowMultiple = true,
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.OUTPUT_PARAMETERS,
effectTags = {OptionEffectTag.CHANGES_INPUTS, OptionEffectTag.AFFECTS_OUTPUTS},
help =
"The given features will be enabled or disabled by default for packages "
+ "built in the host or exec configurations. "
+ "Specifying -<feature> will disable the feature globally. "
+ "Negative features always override positive ones.")
public List<String> hostFeatures;

@Option(
name = "target_environment",
converter = LabelListConverter.class,
Expand Down Expand Up @@ -948,7 +961,7 @@ public FragmentOptions getHost() {
host.checkLicenses = checkLicenses;

// === Pass on C++ compiler features.
host.defaultFeatures = ImmutableList.copyOf(defaultFeatures);
host.defaultFeatures = ImmutableList.copyOf(hostFeatures);

// Save host options in case of a further exec->host transition.
host.hostCpu = hostCpu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,27 @@ public void testFeatureEnabledOnCommandLine() throws Exception {
assertThat(features).doesNotContain("other");
}

@Test
public void testTargetIgnoresHostFeatures() throws Exception {
useConfiguration("--features=feature", "--host_features=other");
scratch.file("a/BUILD",
"cc_library(name = 'a')");
ImmutableSet<String> features = getRuleContext(configure("//a")).getFeatures();
assertThat(features).contains("feature");
assertThat(features).doesNotContain("other");
}

@Test
public void testHostFeatures() throws Exception {
useConfiguration("--features=feature", "--host_features=host_feature");
scratch.file("a/BUILD",
"cc_library(name = 'a')");
ImmutableSet<String> features = getRuleContext(
getConfiguredTarget("//a", getHostConfiguration())).getFeatures();
assertThat(features).contains("host_feature");
assertThat(features).doesNotContain("feature");
}

@Test
public void testFeatureDisabledOnCommandLine() throws Exception {
useConfiguration("--features=-feature");
Expand Down

0 comments on commit cba5cc2

Please sign in to comment.