/**
  * 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) {
     add(SupportedEnvironmentsProvider.class, new SupportedEnvironments(supportedEnvironments));
     ConstraintSemantics.checkConstraints(ruleContext, supportedEnvironments);
   }
 }
Ejemplo n.º 2
0
  /**
   * 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 void checkTargetEnvironmentRestrictions(
      Iterable<ConfiguredTarget> topLevelTargets, PackageManager packageManager)
      throws ViewCreationFailedException {
    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.getEnvironments(), 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)));
      }
    }
  }