Search in sources :

Example 6 with EnvironmentGroup

use of com.google.devtools.build.lib.packages.EnvironmentGroup in project bazel by bazelbuild.

the class ConstraintSemantics method checkRefinedEnvironmentConstraints.

/**
   * Helper method for checkConstraints: performs refined environment constraint checking.
   *
   * <p>Refined environment expectations: no environment group should be emptied out due to
   * refining. This reflects the idea that some of the static declared environments get pruned
   * out by the build configuration, but <i>all</i> environments shouldn't be pruned out.
   *
   * <p>Violations of this expectation trigger rule analysis errors.
   */
private static void checkRefinedEnvironmentConstraints(RuleContext ruleContext, Set<EnvironmentGroup> groupsWithEnvironmentsRemoved, Set<EnvironmentWithGroup> refinedEnvironmentsSoFar, EnvironmentCollection.Builder refinedEnvironments, Map<Label, Target> removedEnvironmentCulprits) {
    Set<EnvironmentGroup> refinedGroups = new LinkedHashSet<>();
    for (EnvironmentWithGroup envWithGroup : refinedEnvironmentsSoFar) {
        refinedEnvironments.put(envWithGroup.group(), envWithGroup.environment());
        refinedGroups.add(envWithGroup.group());
    }
    Set<EnvironmentGroup> newlyEmptyGroups = groupsWithEnvironmentsRemoved.isEmpty() ? ImmutableSet.<EnvironmentGroup>of() : Sets.difference(groupsWithEnvironmentsRemoved, refinedGroups);
    if (!newlyEmptyGroups.isEmpty()) {
        ruleContext.ruleError(getOverRefinementError(newlyEmptyGroups, removedEnvironmentCulprits));
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) EnvironmentWithGroup(com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection.EnvironmentWithGroup)

Example 7 with EnvironmentGroup

use of com.google.devtools.build.lib.packages.EnvironmentGroup in project bazel by bazelbuild.

the class Environment method create.

@Override
public ConfiguredTarget create(RuleContext ruleContext) throws RuleErrorException {
    // The main analysis work to do here is to simply fill in SupportedEnvironmentsProvider to
    // pass the environment itself to depending rules.
    Label label = ruleContext.getLabel();
    EnvironmentGroup group;
    try {
        group = ConstraintSemantics.getEnvironmentGroup(ruleContext.getRule());
    } catch (ConstraintSemantics.EnvironmentLookupException e) {
        ruleContext.ruleError(e.getMessage());
        return null;
    }
    EnvironmentCollection env = new EnvironmentCollection.Builder().put(group, label).build();
    return new RuleConfiguredTargetBuilder(ruleContext).addProvider(SupportedEnvironmentsProvider.class, new SupportedEnvironments(env, env, ImmutableMap.<Label, Target>of())).addProvider(RunfilesProvider.class, RunfilesProvider.EMPTY).add(FileProvider.class, FileProvider.EMPTY).add(FilesToRunProvider.class, FilesToRunProvider.EMPTY).build();
}
Also used : EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) FilesToRunProvider(com.google.devtools.build.lib.analysis.FilesToRunProvider) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) Label(com.google.devtools.build.lib.cmdline.Label) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider)

Example 8 with EnvironmentGroup

use of com.google.devtools.build.lib.packages.EnvironmentGroup in project bazel by bazelbuild.

the class ProtoOutputFormatter method toTargetProtoBuffer.

/** Converts a logical {@link Target} object into a {@link Build.Target} protobuffer. */
@VisibleForTesting
public Build.Target toTargetProtoBuffer(Target target) throws InterruptedException {
    Build.Target.Builder targetPb = Build.Target.newBuilder();
    String location = getLocation(target, relativeLocations);
    if (target instanceof Rule) {
        Rule rule = (Rule) target;
        Build.Rule.Builder rulePb = Build.Rule.newBuilder().setName(rule.getLabel().toString()).setRuleClass(rule.getRuleClass());
        if (includeLocation()) {
            rulePb.setLocation(location);
        }
        Map<Attribute, Build.Attribute> serializedAttributes = Maps.newHashMap();
        for (Attribute attr : rule.getAttributes()) {
            if ((!includeDefaultValues && !rule.isAttributeValueExplicitlySpecified(attr)) || !includeAttribute(rule, attr)) {
                continue;
            }
            Object flattenedAttributeValue = flattenAttributeValues(attr.getType(), getPossibleAttributeValues(rule, attr));
            Build.Attribute serializedAttribute = AttributeFormatter.getAttributeProto(attr, flattenedAttributeValue, rule.isAttributeValueExplicitlySpecified(attr), /*encodeBooleanAndTriStateAsIntegerAndString=*/
            true);
            rulePb.addAttribute(serializedAttribute);
            serializedAttributes.put(attr, serializedAttribute);
        }
        postProcess(rule, rulePb, serializedAttributes);
        Environment env = rule.getRuleClassObject().getRuleDefinitionEnvironment();
        if (env != null && includeRuleDefinitionEnvironment()) {
            // The RuleDefinitionEnvironment is always defined for Skylark rules and
            // always null for non Skylark rules.
            rulePb.addAttribute(Build.Attribute.newBuilder().setName(RULE_IMPLEMENTATION_HASH_ATTR_NAME).setType(ProtoUtils.getDiscriminatorFromType(Type.STRING)).setStringValue(env.getTransitiveContentHashCode()));
        }
        ImmutableMultimap<Attribute, Label> aspectsDependencies = aspectResolver.computeAspectDependencies(target, dependencyFilter);
        // Add information about additional attributes from aspects.
        for (Entry<Attribute, Collection<Label>> entry : aspectsDependencies.asMap().entrySet()) {
            Attribute attribute = entry.getKey();
            Collection<Label> labels = entry.getValue();
            if (!includeAspectAttribute(attribute, labels)) {
                continue;
            }
            Object attributeValue = getAspectAttributeValue(attribute, labels);
            Build.Attribute serializedAttribute = AttributeFormatter.getAttributeProto(attribute, attributeValue, /*explicitlySpecified=*/
            false, /*encodeBooleanAndTriStateAsIntegerAndString=*/
            true);
            rulePb.addAttribute(serializedAttribute);
        }
        if (includeRuleInputsAndOutputs()) {
            // Add all deps from aspects as rule inputs of current target.
            for (Label label : aspectsDependencies.values()) {
                rulePb.addRuleInput(label.toString());
            }
            // host-configuration outputs, and default values.
            for (Label label : rule.getLabels(dependencyFilter)) {
                rulePb.addRuleInput(label.toString());
            }
            for (OutputFile outputFile : rule.getOutputFiles()) {
                Label fileLabel = outputFile.getLabel();
                rulePb.addRuleOutput(fileLabel.toString());
            }
        }
        for (String feature : rule.getFeatures()) {
            rulePb.addDefaultSetting(feature);
        }
        targetPb.setType(RULE);
        targetPb.setRule(rulePb);
    } else if (target instanceof OutputFile) {
        OutputFile outputFile = (OutputFile) target;
        Label label = outputFile.getLabel();
        Rule generatingRule = outputFile.getGeneratingRule();
        GeneratedFile.Builder output = GeneratedFile.newBuilder().setGeneratingRule(generatingRule.getLabel().toString()).setName(label.toString());
        if (includeLocation()) {
            output.setLocation(location);
        }
        targetPb.setType(GENERATED_FILE);
        targetPb.setGeneratedFile(output.build());
    } else if (target instanceof InputFile) {
        InputFile inputFile = (InputFile) target;
        Label label = inputFile.getLabel();
        Build.SourceFile.Builder input = Build.SourceFile.newBuilder().setName(label.toString());
        if (includeLocation()) {
            input.setLocation(location);
        }
        if (inputFile.getName().equals("BUILD")) {
            Set<Label> subincludeLabels = new LinkedHashSet<>();
            subincludeLabels.addAll(aspectResolver == null ? inputFile.getPackage().getSubincludeLabels() : aspectResolver.computeBuildFileDependencies(inputFile.getPackage(), BuildFileDependencyMode.SUBINCLUDE));
            subincludeLabels.addAll(aspectResolver == null ? inputFile.getPackage().getSkylarkFileDependencies() : aspectResolver.computeBuildFileDependencies(inputFile.getPackage(), BuildFileDependencyMode.SKYLARK));
            for (Label skylarkFileDep : subincludeLabels) {
                input.addSubinclude(skylarkFileDep.toString());
            }
            for (String feature : inputFile.getPackage().getFeatures()) {
                input.addFeature(feature);
            }
            input.setPackageContainsErrors(inputFile.getPackage().containsErrors());
        }
        for (Label visibilityDependency : target.getVisibility().getDependencyLabels()) {
            input.addPackageGroup(visibilityDependency.toString());
        }
        for (Label visibilityDeclaration : target.getVisibility().getDeclaredLabels()) {
            input.addVisibilityLabel(visibilityDeclaration.toString());
        }
        targetPb.setType(SOURCE_FILE);
        targetPb.setSourceFile(input);
    } else if (target instanceof FakeSubincludeTarget) {
        Label label = target.getLabel();
        SourceFile.Builder input = SourceFile.newBuilder().setName(label.toString());
        if (includeLocation()) {
            input.setLocation(location);
        }
        targetPb.setType(SOURCE_FILE);
        targetPb.setSourceFile(input.build());
    } else if (target instanceof PackageGroup) {
        PackageGroup packageGroup = (PackageGroup) target;
        Build.PackageGroup.Builder packageGroupPb = Build.PackageGroup.newBuilder().setName(packageGroup.getLabel().toString());
        for (String containedPackage : packageGroup.getContainedPackages()) {
            packageGroupPb.addContainedPackage(containedPackage);
        }
        for (Label include : packageGroup.getIncludes()) {
            packageGroupPb.addIncludedPackageGroup(include.toString());
        }
        targetPb.setType(PACKAGE_GROUP);
        targetPb.setPackageGroup(packageGroupPb);
    } else if (target instanceof EnvironmentGroup) {
        EnvironmentGroup envGroup = (EnvironmentGroup) target;
        Build.EnvironmentGroup.Builder envGroupPb = Build.EnvironmentGroup.newBuilder().setName(envGroup.getLabel().toString());
        for (Label env : envGroup.getEnvironments()) {
            envGroupPb.addEnvironment(env.toString());
        }
        for (Label defaultEnv : envGroup.getDefaults()) {
            envGroupPb.addDefault(defaultEnv.toString());
        }
        targetPb.setType(ENVIRONMENT_GROUP);
        targetPb.setEnvironmentGroup(envGroupPb);
    } else {
        throw new IllegalArgumentException(target.toString());
    }
    return targetPb.build();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Attribute(com.google.devtools.build.lib.packages.Attribute) Builder(com.google.devtools.build.lib.query2.proto.proto2api.Build.QueryResult.Builder) Label(com.google.devtools.build.lib.cmdline.Label) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Target(com.google.devtools.build.lib.packages.Target) FakeSubincludeTarget(com.google.devtools.build.lib.query2.FakeSubincludeTarget) Build(com.google.devtools.build.lib.query2.proto.proto2api.Build) SourceFile(com.google.devtools.build.lib.query2.proto.proto2api.Build.SourceFile) OutputFile(com.google.devtools.build.lib.packages.OutputFile) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) Environment(com.google.devtools.build.lib.syntax.Environment) QueryEnvironment(com.google.devtools.build.lib.query2.engine.QueryEnvironment) Collection(java.util.Collection) Rule(com.google.devtools.build.lib.packages.Rule) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 9 with EnvironmentGroup

use of com.google.devtools.build.lib.packages.EnvironmentGroup in project bazel by bazelbuild.

the class ConfiguredTargetFactory method createConfiguredTarget.

/**
   * Invokes the appropriate constructor to create a {@link ConfiguredTarget} instance.
   * <p>For use in {@code ConfiguredTargetFunction}.
   *
   * <p>Returns null if Skyframe deps are missing or upon certain errors.
   */
@Nullable
public final ConfiguredTarget createConfiguredTarget(AnalysisEnvironment analysisEnvironment, ArtifactFactory artifactFactory, Target target, BuildConfiguration config, BuildConfiguration hostConfig, OrderedSetMultimap<Attribute, ConfiguredTarget> prerequisiteMap, ImmutableMap<Label, ConfigMatchingProvider> configConditions) throws InterruptedException {
    if (target instanceof Rule) {
        return createRule(analysisEnvironment, (Rule) target, config, hostConfig, prerequisiteMap, configConditions);
    }
    // Visibility, like all package groups, doesn't have a configuration
    NestedSet<PackageSpecification> visibility = convertVisibility(prerequisiteMap, analysisEnvironment.getEventHandler(), target, null);
    TargetContext targetContext = new TargetContext(analysisEnvironment, target, config, prerequisiteMap.get(null), visibility);
    if (target instanceof OutputFile) {
        OutputFile outputFile = (OutputFile) target;
        boolean isFileset = outputFile.getGeneratingRule().getRuleClass().equals("Fileset");
        Artifact artifact = getOutputArtifact(outputFile, config, isFileset, artifactFactory);
        TransitiveInfoCollection rule = targetContext.findDirectPrerequisite(outputFile.getGeneratingRule().getLabel(), config);
        if (isFileset) {
            return new FilesetOutputConfiguredTarget(targetContext, outputFile, rule, artifact, rule.getProvider(FilesetProvider.class).getTraversals());
        } else {
            return new OutputFileConfiguredTarget(targetContext, outputFile, rule, artifact);
        }
    } else if (target instanceof InputFile) {
        InputFile inputFile = (InputFile) target;
        Artifact artifact = artifactFactory.getSourceArtifact(inputFile.getExecPath(), Root.asSourceRoot(inputFile.getPackage().getSourceRoot(), inputFile.getPackage().getPackageIdentifier().getRepository().isMain()), new ConfiguredTargetKey(target.getLabel(), config));
        return new InputFileConfiguredTarget(targetContext, inputFile, artifact);
    } else if (target instanceof PackageGroup) {
        PackageGroup packageGroup = (PackageGroup) target;
        return new PackageGroupConfiguredTarget(targetContext, packageGroup);
    } else if (target instanceof EnvironmentGroup) {
        return new EnvironmentGroupConfiguredTarget(targetContext, (EnvironmentGroup) target);
    } else {
        throw new AssertionError("Unexpected target class: " + target.getClass().getName());
    }
}
Also used : OutputFile(com.google.devtools.build.lib.packages.OutputFile) Artifact(com.google.devtools.build.lib.actions.Artifact) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) Rule(com.google.devtools.build.lib.packages.Rule) ConfiguredTargetKey(com.google.devtools.build.lib.skyframe.ConfiguredTargetKey) PackageSpecification(com.google.devtools.build.lib.packages.PackageSpecification) Nullable(javax.annotation.Nullable)

Example 10 with EnvironmentGroup

use of com.google.devtools.build.lib.packages.EnvironmentGroup in project bazel by bazelbuild.

the class DependencyResolver method dependentNodeMap.

/**
   * Returns ids for dependent nodes of a given node, sorted by attribute. Note that some
   * dependencies do not have a corresponding attribute here, and we use the null attribute to
   * represent those edges.
   *
   * <p>If {@code aspects} is empty, returns the dependent nodes of the configured target node
   * representing the given target and configuration.
   *
   * Otherwise {@code aspects} represents an aspect path. The function returns dependent nodes
   * of the entire path applied to given target and configuration. These are the depenent nodes
   * of the last aspect in the path.
   *
   * <p>This also implements the first step of applying
   * configuration transitions, namely, split transitions. This needs to be done before the labels
   * are resolved because late bound attributes depend on the configuration. A good example for this
   * is @{code :cc_toolchain}.
   *
   * <p>The long-term goal is that most configuration transitions be applied here. However, in order
   * to do that, we first have to eliminate transitions that depend on the rule class of the
   * dependency.
   *
   * @param node the target/configuration being evaluated
   * @param hostConfig the configuration this target would use if it was evaluated as a host tool.
   *     This is needed to support {@link LateBoundDefault#useHostConfiguration()}.
   * @param aspects the aspects applied to this target (if any)
   * @param configConditions resolver for config_setting labels
   * @param rootCauses collector for dep labels that can't be (loading phase) loaded
   * @return a mapping of each attribute in this rule or aspects to its dependent nodes
   */
public final OrderedSetMultimap<Attribute, Dependency> dependentNodeMap(TargetAndConfiguration node, BuildConfiguration hostConfig, Iterable<Aspect> aspects, ImmutableMap<Label, ConfigMatchingProvider> configConditions, NestedSetBuilder<Label> rootCauses) throws EvalException, InvalidConfigurationException, InterruptedException, InconsistentAspectOrderException {
    Target target = node.getTarget();
    BuildConfiguration config = node.getConfiguration();
    OrderedSetMultimap<Attribute, Dependency> outgoingEdges = OrderedSetMultimap.create();
    if (target instanceof OutputFile) {
        Preconditions.checkNotNull(config);
        visitTargetVisibility(node, rootCauses, outgoingEdges.get(null));
        Rule rule = ((OutputFile) target).getGeneratingRule();
        outgoingEdges.put(null, Dependency.withConfiguration(rule.getLabel(), config));
    } else if (target instanceof InputFile) {
        visitTargetVisibility(node, rootCauses, outgoingEdges.get(null));
    } else if (target instanceof EnvironmentGroup) {
        visitTargetVisibility(node, rootCauses, outgoingEdges.get(null));
    } else if (target instanceof Rule) {
        visitRule(node, hostConfig, aspects, configConditions, rootCauses, outgoingEdges);
    } else if (target instanceof PackageGroup) {
        visitPackageGroup(node, (PackageGroup) target, rootCauses, outgoingEdges.get(null));
    } else {
        throw new IllegalStateException(target.getLabel().toString());
    }
    return outgoingEdges;
}
Also used : BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) OutputFile(com.google.devtools.build.lib.packages.OutputFile) EnvironmentGroup(com.google.devtools.build.lib.packages.EnvironmentGroup) Target(com.google.devtools.build.lib.packages.Target) Attribute(com.google.devtools.build.lib.packages.Attribute) Rule(com.google.devtools.build.lib.packages.Rule) PackageGroup(com.google.devtools.build.lib.packages.PackageGroup) InputFile(com.google.devtools.build.lib.packages.InputFile)

Aggregations

EnvironmentGroup (com.google.devtools.build.lib.packages.EnvironmentGroup)10 Label (com.google.devtools.build.lib.cmdline.Label)6 EnvironmentWithGroup (com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection.EnvironmentWithGroup)4 InputFile (com.google.devtools.build.lib.packages.InputFile)4 OutputFile (com.google.devtools.build.lib.packages.OutputFile)4 PackageGroup (com.google.devtools.build.lib.packages.PackageGroup)4 Rule (com.google.devtools.build.lib.packages.Rule)4 LinkedHashSet (java.util.LinkedHashSet)4 Attribute (com.google.devtools.build.lib.packages.Attribute)3 Target (com.google.devtools.build.lib.packages.Target)3 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 FilesToRunProvider (com.google.devtools.build.lib.analysis.FilesToRunProvider)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1 TransitiveInfoCollection (com.google.devtools.build.lib.analysis.TransitiveInfoCollection)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1