// Copyright 2014 Google Inc. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package com.google.devtools.build.lib.analysis; import com.google.common.base.Preconditions; import com.google.common.collect.HashMultimap; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Multimap; import com.google.devtools.build.lib.actions.Action; import com.google.devtools.build.lib.actions.Artifact; import com.google.devtools.build.lib.analysis.ExtraActionArtifactsProvider.ExtraArtifactSet; import com.google.devtools.build.lib.analysis.LicensesProvider.TargetLicense; import com.google.devtools.build.lib.analysis.RuleConfiguredTarget.Mode; import com.google.devtools.build.lib.analysis.config.BuildConfiguration; import com.google.devtools.build.lib.analysis.constraints.ConstraintSemantics; import com.google.devtools.build.lib.analysis.constraints.EnvironmentCollection; import com.google.devtools.build.lib.analysis.constraints.SupportedEnvironments; import com.google.devtools.build.lib.analysis.constraints.SupportedEnvironmentsProvider; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder; import com.google.devtools.build.lib.collect.nestedset.Order; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.packages.License; import com.google.devtools.build.lib.packages.Rule; import com.google.devtools.build.lib.packages.TargetUtils; import com.google.devtools.build.lib.packages.Type; import com.google.devtools.build.lib.rules.SkylarkApiProvider; import com.google.devtools.build.lib.rules.extra.ExtraActionMapProvider; import com.google.devtools.build.lib.rules.extra.ExtraActionSpec; import com.google.devtools.build.lib.rules.test.ExecutionInfoProvider; import com.google.devtools.build.lib.rules.test.InstrumentedFilesProvider; import com.google.devtools.build.lib.rules.test.TestActionBuilder; import com.google.devtools.build.lib.rules.test.TestProvider; import com.google.devtools.build.lib.rules.test.TestProvider.TestParams; import com.google.devtools.build.lib.syntax.ClassObject; import com.google.devtools.build.lib.syntax.Environment; import com.google.devtools.build.lib.syntax.EvalException; import com.google.devtools.build.lib.syntax.EvalUtils; import com.google.devtools.build.lib.syntax.Label; import com.google.devtools.build.lib.syntax.SkylarkList; import com.google.devtools.build.lib.syntax.SkylarkNestedSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; /** * Builder class for analyzed rule instances (i.e., instances of {@link ConfiguredTarget}). */ public final class RuleConfiguredTargetBuilder { private final RuleContext ruleContext; private final Map, TransitiveInfoProvider> providers = new LinkedHashMap<>(); private final ImmutableMap.Builder skylarkProviders = ImmutableMap.builder(); private final Map> outputGroupBuilders = new TreeMap<>(); /** These are supported by all configured targets and need to be specially handled. */ private NestedSet filesToBuild = NestedSetBuilder.emptySet(Order.STABLE_ORDER); private RunfilesSupport runfilesSupport; private Artifact executable; private ImmutableList mandatoryStampFiles; private ImmutableSet actionsWithoutExtraAction = ImmutableSet.of(); public RuleConfiguredTargetBuilder(RuleContext ruleContext) { this.ruleContext = ruleContext; add(LicensesProvider.class, initializeLicensesProvider()); add(VisibilityProvider.class, new VisibilityProviderImpl(ruleContext.getVisibility())); } /** * Constructs the RuleConfiguredTarget instance based on the values set for this Builder. */ public ConfiguredTarget build() { if (ruleContext.getConfiguration().enforceConstraints()) { checkConstraints(); } if (ruleContext.hasErrors()) { return null; } FilesToRunProvider filesToRunProvider = new FilesToRunProvider(ruleContext.getLabel(), RuleContext.getFilesToRun(runfilesSupport, filesToBuild), runfilesSupport, executable); add(FileProvider.class, new FileProvider(ruleContext.getLabel(), filesToBuild)); add(FilesToRunProvider.class, filesToRunProvider); if (runfilesSupport != null) { // If a binary is built, build its runfiles, too addOutputGroup( OutputGroupProvider.HIDDEN_TOP_LEVEL, runfilesSupport.getRunfilesMiddleman()); } else if (providers.get(RunfilesProvider.class) != null) { // If we don't have a RunfilesSupport (probably because this is not a binary rule), we still // want to build the files this rule contributes to runfiles of dependent rules so that we // report an error if one of these is broken. // // Note that this is a best-effort thing: there is .getDataRunfiles() and all the language- // specific *RunfilesProvider classes, which we don't add here for reasons that are lost in // the mists of time. addOutputGroup(OutputGroupProvider.HIDDEN_TOP_LEVEL, ((RunfilesProvider) providers.get(RunfilesProvider.class)) .getDefaultRunfiles().getAllArtifacts()); } // Create test action and artifacts if target was successfully initialized // and is a test. if (TargetUtils.isTestRule(ruleContext.getTarget())) { Preconditions.checkState(runfilesSupport != null); add(TestProvider.class, initializeTestProvider(filesToRunProvider)); } add(ExtraActionArtifactsProvider.class, initializeExtraActions()); if (!outputGroupBuilders.isEmpty()) { ImmutableMap.Builder> outputGroups = ImmutableMap.builder(); for (Map.Entry> entry : outputGroupBuilders.entrySet()) { outputGroups.put(entry.getKey(), entry.getValue().build()); } add(OutputGroupProvider.class, new OutputGroupProvider(outputGroups.build())); } return new RuleConfiguredTarget( ruleContext, mandatoryStampFiles, skylarkProviders.build(), providers); } /** * 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); } } private TestProvider initializeTestProvider(FilesToRunProvider filesToRunProvider) { int explicitShardCount = ruleContext.attributes().get("shard_count", Type.INTEGER); if (explicitShardCount < 0 && ruleContext.getRule().isAttributeValueExplicitlySpecified("shard_count")) { ruleContext.attributeError("shard_count", "Must not be negative."); } if (explicitShardCount > 50) { ruleContext.attributeError("shard_count", "Having more than 50 shards is indicative of poor test organization. " + "Please reduce the number of shards."); } TestActionBuilder testActionBuilder = new TestActionBuilder(ruleContext); InstrumentedFilesProvider instrumentedFilesProvider = findProvider(InstrumentedFilesProvider.class); if (instrumentedFilesProvider != null) { testActionBuilder .setInstrumentedFiles(instrumentedFilesProvider) .setExtraEnv(instrumentedFilesProvider.getExtraEnv()); } final TestParams testParams = testActionBuilder .setFilesToRunProvider(filesToRunProvider) .setExecutionRequirements(findProvider(ExecutionInfoProvider.class)) .setShardCount(explicitShardCount) .build(); final ImmutableList testTags = ImmutableList.copyOf(ruleContext.getRule().getRuleTags()); return new TestProvider(testParams, testTags); } private LicensesProvider initializeLicensesProvider() { if (!ruleContext.getConfiguration().checkLicenses()) { return LicensesProviderImpl.EMPTY; } NestedSetBuilder builder = NestedSetBuilder.linkOrder(); BuildConfiguration configuration = ruleContext.getConfiguration(); Rule rule = ruleContext.getRule(); License toolOutputLicense = rule.getToolOutputLicense(ruleContext.attributes()); if (configuration.isHostConfiguration() && toolOutputLicense != null) { if (toolOutputLicense != License.NO_LICENSE) { builder.add(new TargetLicense(rule.getLabel(), toolOutputLicense)); } } else { if (rule.getLicense() != License.NO_LICENSE) { builder.add(new TargetLicense(rule.getLabel(), rule.getLicense())); } for (TransitiveInfoCollection dep : ruleContext.getConfiguredTargetMap().values()) { LicensesProvider provider = dep.getProvider(LicensesProvider.class); if (provider != null) { builder.addTransitive(provider.getTransitiveLicenses()); } } } return new LicensesProviderImpl(builder.build()); } /** * Scans {@code action_listeners} associated with this build to see if any * {@code extra_actions} should be added to this configured target. If any * action_listeners are present, a partial visit of the artifact/action graph * is performed (for as long as actions found are owned by this {@link * ConfiguredTarget}). Any actions that match the {@code action_listener} * get an {@code extra_action} associated. The output artifacts of the * extra_action are reported to the {@link AnalysisEnvironment} for * bookkeeping. */ private ExtraActionArtifactsProvider initializeExtraActions() { BuildConfiguration configuration = ruleContext.getConfiguration(); if (configuration.isHostConfiguration()) { return ExtraActionArtifactsProvider.EMPTY; } ImmutableList extraActionArtifacts = ImmutableList.of(); NestedSetBuilder builder = NestedSetBuilder.stableOrder(); List