aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar jcater <jcater@google.com>2017-06-30 23:11:34 +0200
committerGravatar Marcel Hlopko <hlopko@google.com>2017-07-03 09:06:48 +0200
commit57f5396fe0d86c840e24f5d72b09cb624c7acfee (patch)
tree341fc9452f03369e14fd33bc329e05eb77ab1f98 /src
parent1d33858eac03c4474e75085c849c554b57c491a0 (diff)
Add new flag defining an environment group for automatic cpu-based environment
detection. PiperOrigin-RevId: 160686932
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java28
-rw-r--r--src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java49
2 files changed, 73 insertions, 4 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
index c9a933b8ac..8c217afa17 100644
--- a/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
+++ b/src/main/java/com/google/devtools/build/lib/analysis/config/BuildConfiguration.java
@@ -84,6 +84,7 @@ import com.google.devtools.common.options.OptionsParser.OptionUsageRestrictions;
import com.google.devtools.common.options.OptionsParsingException;
import com.google.devtools.common.options.TriState;
import com.google.devtools.common.options.proto.OptionFilters.OptionEffectTag;
+import com.google.devtools.common.options.proto.OptionFilters.OptionMetadataTag;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
@@ -1045,8 +1046,8 @@ public final class BuildConfiguration implements BuildEvent {
allowMultiple = true,
defaultValue = "",
category = "flags",
- documentationCategory = OptionDocumentationCategory.UNCATEGORIZED,
- effectTags = {OptionEffectTag.UNKNOWN},
+ documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
+ effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
help =
"Declares this build's target environment. Must be a label reference to an "
+ "\"environment\" rule. If specified, all top-level targets must be "
@@ -1054,6 +1055,21 @@ public final class BuildConfiguration implements BuildEvent {
)
public List<Label> targetEnvironments;
+ @Option(
+ name = "experimental_auto_cpu_environment_group",
+ converter = EmptyToNullLabelConverter.class,
+ defaultValue = "",
+ category = "flags",
+ documentationCategory = OptionDocumentationCategory.EXECUTION_STRATEGY,
+ effectTags = {OptionEffectTag.LOADING_AND_ANALYSIS},
+ metadataTags = {OptionMetadataTag.EXPERIMENTAL},
+ optionUsageRestrictions = OptionUsageRestrictions.UNDOCUMENTED,
+ help =
+ "Declare the environment_group to use for automatically mapping cpu values to "
+ + "target_environment values."
+ )
+ public Label autoCpuEnvironmentGroup;
+
/**
* Values for --experimental_dynamic_configs.
*/
@@ -2764,6 +2780,14 @@ public final class BuildConfiguration implements BuildEvent {
return options.targetEnvironments;
}
+ /**
+ * Returns the {@link Label} of the {@code environment_group} target that will be used to find the
+ * target environment during auto-population.
+ */
+ public Label getAutoCpuEnvironmentGroup() {
+ return options.autoCpuEnvironmentGroup;
+ }
+
public Class<? extends Fragment> getSkylarkFragmentByName(String name) {
return skylarkVisibleFragments.get(name);
}
diff --git a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
index e6e5e72302..32490cda8e 100644
--- a/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
+++ b/src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java
@@ -18,6 +18,7 @@ import com.google.common.base.Joiner;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.base.Verify;
+import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.actions.BuildFailedException;
import com.google.devtools.build.lib.actions.TestExecException;
import com.google.devtools.build.lib.analysis.AnalysisPhaseCompleteEvent;
@@ -38,6 +39,7 @@ import com.google.devtools.build.lib.analysis.config.BuildOptions;
import com.google.devtools.build.lib.analysis.config.DefaultsPackage;
import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException;
import com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics;
+import com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics.EnvironmentLookupException;
import com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection;
import com.google.devtools.build.lib.analysis.constraints.SupportedEnvironmentsProvider;
import com.google.devtools.build.lib.buildtool.BuildRequest.BuildRequestOptions;
@@ -51,6 +53,7 @@ import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.OutputFilter;
import com.google.devtools.build.lib.events.Reporter;
+import com.google.devtools.build.lib.packages.EnvironmentGroup;
import com.google.devtools.build.lib.packages.InputFile;
import com.google.devtools.build.lib.packages.License;
import com.google.devtools.build.lib.packages.License.DistributionType;
@@ -73,10 +76,12 @@ import com.google.devtools.build.lib.util.Preconditions;
import com.google.devtools.build.lib.util.RegexFilter;
import com.google.devtools.common.options.OptionsParsingException;
import java.util.Collection;
+import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import java.util.regex.Pattern;
+import javax.annotation.Nullable;
/**
* Provides the bulk of the implementation of the 'blaze build' command.
@@ -300,13 +305,30 @@ public final class BuildTool {
if (config == null) {
// TODO(bazel-team): support file targets (they should apply package-default constraints).
continue;
- } else if (!config.enforceConstraints() || config.getTargetEnvironments().isEmpty()) {
+ } else if (!config.enforceConstraints()) {
+ continue;
+ }
+
+ List<Label> targetEnvironments = config.getTargetEnvironments();
+ if (targetEnvironments.isEmpty()) {
+ try {
+ targetEnvironments =
+ autoConfigureTargetEnvironments(
+ packageManager, config, config.getAutoCpuEnvironmentGroup());
+ } catch (NoSuchPackageException
+ | NoSuchTargetException
+ | ConstraintSemantics.EnvironmentLookupException e) {
+ throw new ViewCreationFailedException("invalid target environment", e);
+ }
+ }
+
+ if (targetEnvironments.isEmpty()) {
continue;
}
// Parse and collect this configuration's environments.
EnvironmentCollection.Builder builder = new EnvironmentCollection.Builder();
- for (Label envLabel : config.getTargetEnvironments()) {
+ for (Label envLabel : targetEnvironments) {
try {
Target env = packageManager.getLoadedTarget(envLabel);
builder.put(ConstraintSemantics.getEnvironmentGroup(env), envLabel);
@@ -341,6 +363,29 @@ public final class BuildTool {
}
}
+ private static List<Label> autoConfigureTargetEnvironments(
+ LoadedPackageProvider packageManager,
+ BuildConfiguration config,
+ @Nullable Label environmentGroupLabel)
+ throws InterruptedException, NoSuchTargetException, NoSuchPackageException,
+ EnvironmentLookupException {
+ if (environmentGroupLabel == null) {
+ return ImmutableList.of();
+ }
+
+ EnvironmentGroup environmentGroup =
+ (EnvironmentGroup) packageManager.getLoadedTarget(environmentGroupLabel);
+
+ ImmutableList.Builder<Label> targetEnvironments = new ImmutableList.Builder<>();
+ for (Label environmentLabel : environmentGroup.getEnvironments()) {
+ if (environmentLabel.getName().equals(config.getCpu())) {
+ targetEnvironments.add(environmentLabel);
+ }
+ }
+
+ return targetEnvironments.build();
+ }
+
private void reportExceptionError(Exception e) {
if (e.getMessage() != null) {
getReporter().handle(Event.error(e.getMessage()));