From edc3901ecaafdb778a8e9fac463af1b2812f8f48 Mon Sep 17 00:00:00 2001 From: Googler Date: Tue, 21 Nov 2017 08:21:49 -0800 Subject: 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 --- .../build/lib/rules/cpp/CcToolchainFeatures.java | 43 +++++++++++++++------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'src/main/java/com/google') 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 withFeatureSets, Set enabledFeatureNames) { + Collection withFeatureSets, Set 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 actions; private final ImmutableSet expandIfAllAvailable; - private final ImmutableSet withFeatureSets; + private final ImmutableSet withFeatureSets; private final ImmutableList 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 actions; private final ImmutableList envEntries; - + private final ImmutableSet withFeatureSets; + private EnvSet(CToolchain.EnvSet envSet) throws InvalidConfigurationException { this.actions = ImmutableSet.copyOf(envSet.getActionList()); ImmutableList.Builder 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 enabledFeatureNames, ImmutableMap.Builder 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 envBuilder) { + String action, + Variables variables, + Set enabledFeatureNames, + ImmutableMap.Builder 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 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 getEnvironmentVariables(String action, Variables variables) { ImmutableMap.Builder envBuilder = ImmutableMap.builder(); for (Feature feature : enabledFeatures) { - feature.expandEnvironment(action, variables, envBuilder); + feature.expandEnvironment(action, variables, enabledFeatureNames, envBuilder); } return envBuilder.build(); } -- cgit v1.2.3