Skip to content

Commit

Permalink
FlagSet.with_feature can be used to condition the application of the …
Browse files Browse the repository at this point in the history
…FlagSet on the presence of features.

--
PiperOrigin-RevId: 151038811
MOS_MIGRATED_REVID=151038811
  • Loading branch information
calpeyser authored and hermione521 committed Mar 24, 2017
1 parent 99523a9 commit 18203b8
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,14 +480,28 @@ private void expandCommandLine(Variables variables, final List<String> commandLi
expand(variables, commandLine);
}
}


private static boolean isWithFeaturesSatisfied(
Set<CToolchain.FeatureSet> withFeatureSets, Set<String> enabledFeatureNames) {
if (withFeatureSets.isEmpty()) {
return true;
}
for (CToolchain.FeatureSet featureSet : withFeatureSets) {
if (enabledFeatureNames.containsAll(featureSet.getFeatureList())) {
return true;
}
}
return false;
}

/**
* Groups a set of flags to apply for certain actions.
*/
@Immutable
private static class FlagSet implements Serializable {
private final ImmutableSet<String> actions;
private final ImmutableSet<String> expandIfAllAvailable;
private final ImmutableSet<CToolchain.FeatureSet> withFeatureSets;
private final ImmutableList<FlagGroup> flagGroups;

private FlagSet(CToolchain.FlagSet flagSet) throws InvalidConfigurationException {
Expand All @@ -501,22 +515,28 @@ private FlagSet(CToolchain.FlagSet flagSet, ImmutableSet<String> actions)
throws InvalidConfigurationException {
this.actions = actions;
this.expandIfAllAvailable = ImmutableSet.copyOf(flagSet.getExpandIfAllAvailableList());
this.withFeatureSets = ImmutableSet.copyOf(flagSet.getWithFeatureList());
ImmutableList.Builder<FlagGroup> builder = ImmutableList.builder();
for (CToolchain.FlagGroup flagGroup : flagSet.getFlagGroupList()) {
builder.add(new FlagGroup(flagGroup));
}
this.flagGroups = builder.build();
}

/**
* Adds the flags that apply to the given {@code action} to {@code commandLine}.
*/
private void expandCommandLine(String action, Variables variables, List<String> commandLine) {
/** Adds the flags that apply to the given {@code action} to {@code commandLine}. */
private void expandCommandLine(
String action,
Variables variables,
Set<String> enabledFeatureNames,
List<String> commandLine) {
for (String variable : expandIfAllAvailable) {
if (!variables.isAvailable(variable)) {
return;
}
}
if (!isWithFeaturesSatisfied(withFeatureSets, enabledFeatureNames)) {
return;
}
if (!actions.contains(action)) {
return;
}
Expand Down Expand Up @@ -611,13 +631,14 @@ private void expandEnvironment(
}
}

/**
* Adds the flags that apply to the given {@code action} to {@code commandLine}.
*/
private void expandCommandLine(String action, Variables variables,
/** Adds the flags that apply to the given {@code action} to {@code commandLine}. */
private void expandCommandLine(
String action,
Variables variables,
Set<String> enabledFeatureNames,
List<String> commandLine) {
for (FlagSet flagSet : flagSets) {
flagSet.expandCommandLine(action, variables, commandLine);
flagSet.expandCommandLine(action, variables, enabledFeatureNames, commandLine);
}
}
}
Expand Down Expand Up @@ -748,12 +769,11 @@ public boolean apply(CToolchain.Tool input) {
}
}

/**
* Adds the flags that apply to this action to {@code commandLine}.
*/
private void expandCommandLine(Variables variables, List<String> commandLine) {
/** Adds the flags that apply to this action to {@code commandLine}. */
private void expandCommandLine(
Variables variables, Set<String> enabledFeatureNames, List<String> commandLine) {
for (FlagSet flagSet : flagSets) {
flagSet.expandCommandLine(actionName, variables, commandLine);
flagSet.expandCommandLine(actionName, variables, enabledFeatureNames, commandLine);
}
}
}
Expand Down Expand Up @@ -1740,11 +1760,13 @@ boolean actionIsConfigured(String actionName) {
List<String> getCommandLine(String action, Variables variables) {
List<String> commandLine = new ArrayList<>();
for (Feature feature : enabledFeatures) {
feature.expandCommandLine(action, variables, commandLine);
feature.expandCommandLine(action, variables, enabledFeatureNames, commandLine);
}

if (actionIsConfigured(action)) {
actionConfigByActionName.get(action).expandCommandLine(variables, commandLine);
actionConfigByActionName
.get(action)
.expandCommandLine(variables, enabledFeatureNames, commandLine);
}

return commandLine;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,102 @@ public void testDefaultFeatures() throws Exception {
assertThat(features.getDefaultFeatures()).containsExactly("b");
}

@Test
public void testWithFeature_OneSetOneFeature() throws Exception {
CcToolchainFeatures features =
buildFeatures(
"feature {",
" name: 'a'",
" flag_set {",
" with_feature {feature: 'b'}",
" action: 'c++-compile'",
" flag_group {",
" flag: 'dummy_flag'",
" }",
" }",
"}",
"feature {name: 'b'}");
assertThat(
features
.getFeatureConfiguration("a", "b")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.containsExactly("dummy_flag");
assertThat(
features
.getFeatureConfiguration("a")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.doesNotContain("dummy_flag");
}

@Test
public void testWithFeature_OneSetMultipleFeatures() throws Exception {
CcToolchainFeatures features =
buildFeatures(
"feature {",
" name: 'a'",
" flag_set {",
" with_feature {feature: 'b', feature: 'c'}",
" action: 'c++-compile'",
" flag_group {",
" flag: 'dummy_flag'",
" }",
" }",
"}",
"feature {name: 'b'}",
"feature {name: 'c'}");
assertThat(
features
.getFeatureConfiguration("a", "b", "c")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.containsExactly("dummy_flag");
assertThat(
features
.getFeatureConfiguration("a", "b")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.doesNotContain("dummy_flag");
assertThat(
features
.getFeatureConfiguration("a")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.doesNotContain("dummy_flag");
}

@Test
public void testWithFeature_MulipleSetsMultipleFeatures() throws Exception {
CcToolchainFeatures features =
buildFeatures(
"feature {",
" name: 'a'",
" flag_set {",
" with_feature {feature: 'b1', feature: 'c1'}",
" with_feature {feature: 'b2', feature: 'c2'}",
" action: 'c++-compile'",
" flag_group {",
" flag: 'dummy_flag'",
" }",
" }",
"}",
"feature {name: 'b1'}",
"feature {name: 'c1'}",
"feature {name: 'b2'}",
"feature {name: 'c2'}");
assertThat(
features
.getFeatureConfiguration("a", "b1", "c1", "b2", "c2")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.containsExactly("dummy_flag");
assertThat(
features
.getFeatureConfiguration("a", "b1", "c1")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.containsExactly("dummy_flag");
assertThat(
features
.getFeatureConfiguration("a", "b1", "b2")
.getCommandLine(CppCompileAction.CPP_COMPILE, createVariables()))
.doesNotContain("dummy_flag");
}

@Test
public void testActivateActionConfigFromFeature() throws Exception {
CcToolchainFeatures toolchainFeatures =
Expand Down

0 comments on commit 18203b8

Please sign in to comment.