diff options
author | Googler <noreply@google.com> | 2017-11-21 08:21:49 -0800 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2017-11-21 08:24:09 -0800 |
commit | edc3901ecaafdb778a8e9fac463af1b2812f8f48 (patch) | |
tree | 8934ad64d577d532ca96fec06fc0c52bddee874b /src/main/java/com/google/devtools | |
parent | 03d52e23ef2e247ebd17bc10ed122795e7113472 (diff) |
Allow conditioning flags on the _absence_ of a feature.
This allows a flag_set to emit one flag when a feature is enabled, and a
different flag when that feature is disabled.
And while I was in there, I noticed and fixed a couple other issues:
1. env_set didn't actually implement with_feature, despite having the field in
its proto.
2. action_config implemented with_feature as an optional field, instead of
repeated field.
RELNOTES: None
PiperOrigin-RevId: 176510960
Diffstat (limited to 'src/main/java/com/google/devtools')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java | 43 |
1 files changed, 29 insertions, 14 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java index 679d2c2b38..87a04c9f7f 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CcToolchainFeatures.java @@ -479,12 +479,19 @@ public class CcToolchainFeatures implements Serializable { } private static boolean isWithFeaturesSatisfied( - Set<CToolchain.FeatureSet> withFeatureSets, Set<String> enabledFeatureNames) { + Collection<CToolchain.WithFeatureSet> withFeatureSets, Set<String> enabledFeatureNames) { if (withFeatureSets.isEmpty()) { return true; } - for (CToolchain.FeatureSet featureSet : withFeatureSets) { - if (enabledFeatureNames.containsAll(featureSet.getFeatureList())) { + for (CToolchain.WithFeatureSet featureSet : withFeatureSets) { + boolean negativeMatch = + featureSet + .getNotFeatureList() + .stream() + .anyMatch(notFeature -> enabledFeatureNames.contains(notFeature)); + boolean positiveMatch = enabledFeatureNames.containsAll(featureSet.getFeatureList()); + + if (!negativeMatch && positiveMatch) { return true; } } @@ -498,7 +505,7 @@ public class CcToolchainFeatures implements Serializable { private static class FlagSet implements Serializable { private final ImmutableSet<String> actions; private final ImmutableSet<String> expandIfAllAvailable; - private final ImmutableSet<CToolchain.FeatureSet> withFeatureSets; + private final ImmutableSet<CToolchain.WithFeatureSet> withFeatureSets; private final ImmutableList<FlagGroup> flagGroups; private FlagSet(CToolchain.FlagSet flagSet) throws InvalidConfigurationException { @@ -550,7 +557,8 @@ public class CcToolchainFeatures implements Serializable { private static class EnvSet implements Serializable { private final ImmutableSet<String> actions; private final ImmutableList<EnvEntry> envEntries; - + private final ImmutableSet<CToolchain.WithFeatureSet> withFeatureSets; + private EnvSet(CToolchain.EnvSet envSet) throws InvalidConfigurationException { this.actions = ImmutableSet.copyOf(envSet.getActionList()); ImmutableList.Builder<EnvEntry> builder = ImmutableList.builder(); @@ -558,17 +566,24 @@ public class CcToolchainFeatures implements Serializable { builder.add(new EnvEntry(envEntry)); } this.envEntries = builder.build(); + this.withFeatureSets = ImmutableSet.copyOf(envSet.getWithFeatureList()); } /** * Adds the environment key/value pairs that apply to the given {@code action} to * {@code envBuilder}. */ - private void expandEnvironment(String action, Variables variables, + private void expandEnvironment( + String action, + Variables variables, + Set<String> enabledFeatureNames, ImmutableMap.Builder<String, String> envBuilder) { if (!actions.contains(action)) { return; } + if (!isWithFeaturesSatisfied(withFeatureSets, enabledFeatureNames)) { + return; + } for (EnvEntry envEntry : envEntries) { envEntry.addEnvEntry(variables, envBuilder); } @@ -618,13 +633,14 @@ public class CcToolchainFeatures implements Serializable { return name; } - /** - * Adds environment variables for the given action to the provided builder. - */ + /** Adds environment variables for the given action to the provided builder. */ private void expandEnvironment( - String action, Variables variables, ImmutableMap.Builder<String, String> envBuilder) { + String action, + Variables variables, + Set<String> enabledFeatureNames, + ImmutableMap.Builder<String, String> envBuilder) { for (EnvSet envSet : envSets) { - envSet.expandEnvironment(action, variables, envBuilder); + envSet.expandEnvironment(action, variables, enabledFeatureNames, envBuilder); } } @@ -747,8 +763,7 @@ public class CcToolchainFeatures implements Serializable { Iterables.tryFind( tools, input -> { - Collection<String> featureNamesForTool = input.getWithFeature().getFeatureList(); - return enabledFeatureNames.containsAll(featureNamesForTool); + return isWithFeaturesSatisfied(input.getWithFeatureList(), enabledFeatureNames); }); if (tool.isPresent()) { return new Tool(tool.get()); @@ -1756,7 +1771,7 @@ public class CcToolchainFeatures implements Serializable { ImmutableMap<String, String> getEnvironmentVariables(String action, Variables variables) { ImmutableMap.Builder<String, String> envBuilder = ImmutableMap.builder(); for (Feature feature : enabledFeatures) { - feature.expandEnvironment(action, variables, envBuilder); + feature.expandEnvironment(action, variables, enabledFeatureNames, envBuilder); } return envBuilder.build(); } |