Search in sources :

Example 66 with Rule

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

the class PyCommon method determineMainExecutableSource.

/** @return A String that is the full path to the main python entry point. */
public String determineMainExecutableSource(boolean withWorkspaceName) {
    String mainSourceName;
    Rule target = ruleContext.getRule();
    boolean explicitMain = target.isAttributeValueExplicitlySpecified("main");
    if (explicitMain) {
        mainSourceName = ruleContext.attributes().get("main", BuildType.LABEL).getName();
        if (!mainSourceName.endsWith(".py")) {
            ruleContext.attributeError("main", "main must end in '.py'");
        }
    } else {
        String ruleName = target.getName();
        if (ruleName.endsWith(".py")) {
            ruleContext.attributeError("name", "name must not end in '.py'");
        }
        mainSourceName = ruleName + ".py";
    }
    PathFragment mainSourcePath = new PathFragment(mainSourceName);
    Artifact mainArtifact = null;
    for (Artifact outItem : ruleContext.getPrerequisiteArtifacts("srcs", Mode.TARGET).list()) {
        if (outItem.getRootRelativePath().endsWith(mainSourcePath)) {
            if (mainArtifact == null) {
                mainArtifact = outItem;
            } else {
                ruleContext.attributeError("srcs", buildMultipleMainMatchesErrorText(explicitMain, mainSourceName, mainArtifact.getRunfilesPath().toString(), outItem.getRunfilesPath().toString()));
            }
        }
    }
    if (mainArtifact == null) {
        ruleContext.attributeError("srcs", buildNoMainMatchesErrorText(explicitMain, mainSourceName));
        return null;
    }
    if (!withWorkspaceName) {
        return mainArtifact.getRunfilesPath().getPathString();
    }
    PathFragment workspaceName = new PathFragment(ruleContext.getRule().getPackage().getWorkspaceName());
    return workspaceName.getRelative(mainArtifact.getRunfilesPath()).getPathString();
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Rule(com.google.devtools.build.lib.packages.Rule) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 67 with Rule

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

the class RepositoryFunction method getRule.

/**
   * Uses a remote repository name to fetch the corresponding Rule describing how to get it. This
   * should be called from {@link SkyFunction#compute} functions, which should return null if this
   * returns null. If {@code ruleClassName} is set, the rule found must have a matching rule class
   * name.
   */
@Nullable
public static Rule getRule(RepositoryName repositoryName, @Nullable String ruleClassName, Environment env) throws RepositoryFunctionException, InterruptedException {
    Rule rule = getRule(repositoryName.strippedName(), env);
    Preconditions.checkState(rule == null || ruleClassName == null || rule.getRuleClass().equals(ruleClassName), "Got %s, was expecting a %s", rule, ruleClassName);
    return rule;
}
Also used : Rule(com.google.devtools.build.lib.packages.Rule) Nullable(javax.annotation.Nullable)

Example 68 with Rule

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

the class RepositoryFunction method getRule.

/**
   * Uses a remote repository name to fetch the corresponding Rule describing how to get it.
   *
   * <p>This should be the unique entry point for resolving a remote repository function.
   */
@Nullable
public static Rule getRule(String repository, Environment env) throws RepositoryFunctionException, InterruptedException {
    SkyKey packageLookupKey = PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    PackageLookupValue packageLookupValue = (PackageLookupValue) env.getValue(packageLookupKey);
    if (packageLookupValue == null) {
        return null;
    }
    RootedPath workspacePath = packageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
    SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
    do {
        WorkspaceFileValue value = (WorkspaceFileValue) env.getValue(workspaceKey);
        if (value == null) {
            return null;
        }
        Package externalPackage = value.getPackage();
        if (externalPackage.containsErrors()) {
            Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
            throw new RepositoryFunctionException(new BuildFileContainsErrorsException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Could not load //external package"), Transience.PERSISTENT);
        }
        Rule rule = externalPackage.getRule(repository);
        if (rule != null) {
            return rule;
        }
        workspaceKey = value.next();
    } while (workspaceKey != null);
    throw new RepositoryNotFoundException(repository);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) PackageLookupValue(com.google.devtools.build.lib.skyframe.PackageLookupValue) BuildFileContainsErrorsException(com.google.devtools.build.lib.packages.BuildFileContainsErrorsException) WorkspaceFileValue(com.google.devtools.build.lib.skyframe.WorkspaceFileValue) Package(com.google.devtools.build.lib.packages.Package) Rule(com.google.devtools.build.lib.packages.Rule) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Nullable(javax.annotation.Nullable)

Example 69 with Rule

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

the class ConfiguredTargetFunction method getConfigConditions.

/**
   * Returns the set of {@link ConfigMatchingProvider}s that key the configurable attributes used by
   * this rule.
   *
   * <p>>If the configured targets supplying those providers aren't yet resolved by the dependency
   * resolver, returns null.
   */
@Nullable
static ImmutableMap<Label, ConfigMatchingProvider> getConfigConditions(Target target, Environment env, SkyframeDependencyResolver resolver, TargetAndConfiguration ctgValue, NestedSetBuilder<Package> transitivePackages, NestedSetBuilder<Label> transitiveLoadingRootCauses) throws DependencyEvaluationException, InterruptedException {
    if (!(target instanceof Rule)) {
        return NO_CONFIG_CONDITIONS;
    }
    Map<Label, ConfigMatchingProvider> configConditions = new LinkedHashMap<>();
    // Collect the labels of the configured targets we need to resolve.
    OrderedSetMultimap<Attribute, Label> configLabelMap = OrderedSetMultimap.create();
    RawAttributeMapper attributeMap = RawAttributeMapper.of(((Rule) target));
    for (Attribute a : ((Rule) target).getAttributes()) {
        for (Label configLabel : attributeMap.getConfigurabilityKeys(a.getName(), a.getType())) {
            if (!BuildType.Selector.isReservedLabel(configLabel)) {
                configLabelMap.put(a, target.getLabel().resolveRepositoryRelative(configLabel));
            }
        }
    }
    if (configLabelMap.isEmpty()) {
        return NO_CONFIG_CONDITIONS;
    }
    // Collect the corresponding Skyframe configured target values. Abort early if they haven't
    // been computed yet.
    Collection<Dependency> configValueNames = null;
    try {
        configValueNames = resolver.resolveRuleLabels(ctgValue, configLabelMap, transitiveLoadingRootCauses);
    } catch (InconsistentAspectOrderException e) {
        throw new DependencyEvaluationException(e);
    }
    if (env.valuesMissing()) {
        return null;
    }
    // No need to get new configs from Skyframe - config_setting rules always use the current
    // target's config.
    // TODO(bazel-team): remove the need for this special transformation. We can probably do this by
    // simply passing this through trimConfigurations.
    BuildConfiguration targetConfig = ctgValue.getConfiguration();
    if (useDynamicConfigurations(targetConfig)) {
        ImmutableList.Builder<Dependency> staticConfigs = ImmutableList.builder();
        for (Dependency dep : configValueNames) {
            staticConfigs.add(Dependency.withConfigurationAndAspects(dep.getLabel(), targetConfig, dep.getAspects()));
        }
        configValueNames = staticConfigs.build();
    }
    Map<SkyKey, ConfiguredTarget> configValues = resolveConfiguredTargetDependencies(env, configValueNames, transitivePackages, transitiveLoadingRootCauses);
    if (configValues == null) {
        return null;
    }
    // Get the configured targets as ConfigMatchingProvider interfaces.
    for (Dependency entry : configValueNames) {
        ConfiguredTarget value = configValues.get(TO_KEYS.apply(entry));
        // The code above guarantees that value is non-null here.
        ConfigMatchingProvider provider = value.getProvider(ConfigMatchingProvider.class);
        if (provider != null) {
            configConditions.put(entry.getLabel(), provider);
        } else {
            // Not a valid provider for configuration conditions.
            String message = entry.getLabel() + " is not a valid configuration key for " + target.getLabel();
            env.getListener().handle(Event.error(TargetUtils.getLocationMaybe(target), message));
            throw new DependencyEvaluationException(new ConfiguredValueCreationException(message, target.getLabel()));
        }
    }
    return ImmutableMap.copyOf(configConditions);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) SkyKey(com.google.devtools.build.skyframe.SkyKey) Attribute(com.google.devtools.build.lib.packages.Attribute) ImmutableList(com.google.common.collect.ImmutableList) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) MergedConfiguredTarget(com.google.devtools.build.lib.analysis.MergedConfiguredTarget) Dependency(com.google.devtools.build.lib.analysis.Dependency) InconsistentAspectOrderException(com.google.devtools.build.lib.analysis.DependencyResolver.InconsistentAspectOrderException) LinkedHashMap(java.util.LinkedHashMap) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Rule(com.google.devtools.build.lib.packages.Rule) Nullable(javax.annotation.Nullable)

Example 70 with Rule

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

the class PostConfiguredTargetFunction method getConfigurableAttributeConditions.

/**
   * Returns the configurable attribute conditions necessary to evaluate the given configured
   * target, or null if not all dependencies have yet been SkyFrame-evaluated.
   */
@Nullable
private static ImmutableMap<Label, ConfigMatchingProvider> getConfigurableAttributeConditions(TargetAndConfiguration ctg, Environment env) throws InterruptedException {
    if (!(ctg.getTarget() instanceof Rule)) {
        return ImmutableMap.of();
    }
    Rule rule = (Rule) ctg.getTarget();
    RawAttributeMapper mapper = RawAttributeMapper.of(rule);
    Set<SkyKey> depKeys = new LinkedHashSet<>();
    for (Attribute attribute : rule.getAttributes()) {
        for (Label label : mapper.getConfigurabilityKeys(attribute.getName(), attribute.getType())) {
            if (!BuildType.Selector.isReservedLabel(label)) {
                depKeys.add(ConfiguredTargetValue.key(label, ctg.getConfiguration()));
            }
        }
    }
    Map<SkyKey, SkyValue> cts = env.getValues(depKeys);
    if (env.valuesMissing()) {
        return null;
    }
    Map<Label, ConfigMatchingProvider> conditions = new LinkedHashMap<>();
    for (Map.Entry<SkyKey, SkyValue> entry : cts.entrySet()) {
        Label label = ((ConfiguredTargetKey) entry.getKey().argument()).getLabel();
        ConfiguredTarget ct = ((ConfiguredTargetValue) entry.getValue()).getConfiguredTarget();
        conditions.put(label, Preconditions.checkNotNull(ct.getProvider(ConfigMatchingProvider.class)));
    }
    return ImmutableMap.copyOf(conditions);
}
Also used : RawAttributeMapper(com.google.devtools.build.lib.packages.RawAttributeMapper) LinkedHashSet(java.util.LinkedHashSet) SkyKey(com.google.devtools.build.skyframe.SkyKey) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) LinkedHashMap(java.util.LinkedHashMap) SkyValue(com.google.devtools.build.skyframe.SkyValue) ConfigMatchingProvider(com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider) Rule(com.google.devtools.build.lib.packages.Rule) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Nullable(javax.annotation.Nullable)

Aggregations

Rule (com.google.devtools.build.lib.packages.Rule)79 Test (org.junit.Test)27 Label (com.google.devtools.build.lib.cmdline.Label)26 Attribute (com.google.devtools.build.lib.packages.Attribute)20 Target (com.google.devtools.build.lib.packages.Target)19 Nullable (javax.annotation.Nullable)10 RawAttributeMapper (com.google.devtools.build.lib.packages.RawAttributeMapper)9 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)9 OutputFile (com.google.devtools.build.lib.packages.OutputFile)8 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)7 NoSuchThingException (com.google.devtools.build.lib.packages.NoSuchThingException)7 SkyKey (com.google.devtools.build.skyframe.SkyKey)7 ImmutableList (com.google.common.collect.ImmutableList)6 InputFile (com.google.devtools.build.lib.packages.InputFile)6 IOException (java.io.IOException)6 LinkedHashSet (java.util.LinkedHashSet)6 AggregatingAttributeMapper (com.google.devtools.build.lib.packages.AggregatingAttributeMapper)5 Package (com.google.devtools.build.lib.packages.Package)5 Artifact (com.google.devtools.build.lib.actions.Artifact)4 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)4