Search in sources :

Example 1 with EnvironmentCollection

use of com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection in project bazel by bazelbuild.

the class BuildTool method checkTargetEnvironmentRestrictions.

/**
   * Checks that if this is an environment-restricted build, all top-level targets support the
   * expected environments.
   *
   * @param topLevelTargets the build's top-level targets
   * @throws ViewCreationFailedException if constraint enforcement is on, the build declares
   *     environment-restricted top level configurations, and any top-level target doesn't support
   *     the expected environments
   */
private static void checkTargetEnvironmentRestrictions(Iterable<ConfiguredTarget> topLevelTargets, LoadedPackageProvider packageManager) throws ViewCreationFailedException, InterruptedException {
    for (ConfiguredTarget topLevelTarget : topLevelTargets) {
        BuildConfiguration config = topLevelTarget.getConfiguration();
        if (config == null) {
            // TODO(bazel-team): support file targets (they should apply package-default constraints).
            continue;
        } else if (!config.enforceConstraints() || config.getTargetEnvironments().isEmpty()) {
            continue;
        }
        // Parse and collect this configuration's environments.
        EnvironmentCollection.Builder builder = new EnvironmentCollection.Builder();
        for (Label envLabel : config.getTargetEnvironments()) {
            try {
                Target env = packageManager.getLoadedTarget(envLabel);
                builder.put(ConstraintSemantics.getEnvironmentGroup(env), envLabel);
            } catch (NoSuchPackageException | NoSuchTargetException | ConstraintSemantics.EnvironmentLookupException e) {
                throw new ViewCreationFailedException("invalid target environment", e);
            }
        }
        EnvironmentCollection expectedEnvironments = builder.build();
        // Now check the target against those environments.
        SupportedEnvironmentsProvider provider = Verify.verifyNotNull(topLevelTarget.getProvider(SupportedEnvironmentsProvider.class));
        Collection<Label> missingEnvironments = ConstraintSemantics.getUnsupportedEnvironments(provider.getRefinedEnvironments(), expectedEnvironments);
        if (!missingEnvironments.isEmpty()) {
            throw new ViewCreationFailedException(String.format("This is a restricted-environment build. %s does not support" + " required environment%s %s", topLevelTarget.getLabel(), missingEnvironments.size() == 1 ? "" : "s", Joiner.on(", ").join(missingEnvironments)));
        }
    }
}
Also used : Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) ViewCreationFailedException(com.google.devtools.build.lib.analysis.ViewCreationFailedException) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) SupportedEnvironmentsProvider(com.google.devtools.build.lib.analysis.constraints.SupportedEnvironmentsProvider) EnvironmentCollection(com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection)

Example 2 with EnvironmentCollection

use of com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection in project bazel by bazelbuild.

the class RuleConfiguredTargetBuilder method checkConstraints.

/**
   * Invokes Blaze's constraint enforcement system: checks that this rule's dependencies
   * support its environments and reports appropriate errors if violations are found. Also
   * publishes this rule's supported environments for the rules that depend on it.
   */
private void checkConstraints() {
    if (!ruleContext.getRule().getRuleClassObject().supportsConstraintChecking()) {
        return;
    }
    EnvironmentCollection supportedEnvironments = ConstraintSemantics.getSupportedEnvironments(ruleContext);
    if (supportedEnvironments != null) {
        EnvironmentCollection.Builder refinedEnvironments = new EnvironmentCollection.Builder();
        Map<Label, Target> removedEnvironmentCulprits = new LinkedHashMap<>();
        ConstraintSemantics.checkConstraints(ruleContext, supportedEnvironments, refinedEnvironments, removedEnvironmentCulprits);
        add(SupportedEnvironmentsProvider.class, new SupportedEnvironments(supportedEnvironments, refinedEnvironments.build(), removedEnvironmentCulprits));
    }
}
Also used : Target(com.google.devtools.build.lib.packages.Target) TestActionBuilder(com.google.devtools.build.lib.rules.test.TestActionBuilder) NestedSetBuilder(com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder) Label(com.google.devtools.build.lib.cmdline.Label) EnvironmentCollection(com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection) SupportedEnvironments(com.google.devtools.build.lib.analysis.constraints.SupportedEnvironments) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

EnvironmentCollection (com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection)2 Label (com.google.devtools.build.lib.cmdline.Label)2 Target (com.google.devtools.build.lib.packages.Target)2 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 ViewCreationFailedException (com.google.devtools.build.lib.analysis.ViewCreationFailedException)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1 SupportedEnvironments (com.google.devtools.build.lib.analysis.constraints.SupportedEnvironments)1 SupportedEnvironmentsProvider (com.google.devtools.build.lib.analysis.constraints.SupportedEnvironmentsProvider)1 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)1 NoSuchPackageException (com.google.devtools.build.lib.packages.NoSuchPackageException)1 NoSuchTargetException (com.google.devtools.build.lib.packages.NoSuchTargetException)1 TestActionBuilder (com.google.devtools.build.lib.rules.test.TestActionBuilder)1 LinkedHashMap (java.util.LinkedHashMap)1