Search in sources :

Example 6 with Rule

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

the class SkyQueryEnvironment method processRawReverseDeps.

private Collection<Target> processRawReverseDeps(Map<SkyKey, Collection<Target>> rawReverseDeps) throws InterruptedException {
    Set<Target> result = CompactHashSet.create();
    CompactHashSet<Target> visited = CompactHashSet.createWithExpectedSize(totalSizeOfCollections(rawReverseDeps.values()));
    Set<Label> keys = CompactHashSet.create(Collections2.transform(rawReverseDeps.keySet(), SKYKEY_TO_LABEL));
    for (Collection<Target> parentCollection : rawReverseDeps.values()) {
        for (Target parent : parentCollection) {
            if (visited.add(parent)) {
                if (parent instanceof Rule && dependencyFilter != DependencyFilter.ALL_DEPS) {
                    for (Label label : getAllowedDeps((Rule) parent)) {
                        if (keys.contains(label)) {
                            result.add(parent);
                        }
                    }
                } else {
                    result.add(parent);
                }
            }
        }
    }
    return result;
}
Also used : Target(com.google.devtools.build.lib.packages.Target) Label(com.google.devtools.build.lib.cmdline.Label) Rule(com.google.devtools.build.lib.packages.Rule)

Example 7 with Rule

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

the class TransitiveBaseTraversalFunction method getStrictLabelAspectKeys.

/**
   * Return an Iterable of SkyKeys corresponding to the Aspect-related dependencies of target.
   *
   * <p>This method may return a precise set of aspect keys, but may need to request additional
   * dependencies from the env to do so.
   */
private Iterable<SkyKey> getStrictLabelAspectKeys(Target target, Map<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> depMap, Environment env) throws InterruptedException {
    List<SkyKey> depKeys = Lists.newArrayList();
    if (target instanceof Rule) {
        Map<Label, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> labelDepMap = new HashMap<>(depMap.size());
        for (Entry<SkyKey, ValueOrException2<NoSuchPackageException, NoSuchTargetException>> entry : depMap.entrySet()) {
            labelDepMap.put((Label) entry.getKey().argument(), entry.getValue());
        }
        Multimap<Attribute, Label> transitions = ((Rule) target).getTransitions(DependencyFilter.NO_NODEP_ATTRIBUTES);
        for (Entry<Attribute, Label> entry : transitions.entries()) {
            ValueOrException2<NoSuchPackageException, NoSuchTargetException> value = labelDepMap.get(entry.getValue());
            for (Label label : getAspectLabels((Rule) target, entry.getKey(), entry.getValue(), value, env)) {
                depKeys.add(getKey(label));
            }
        }
    }
    return depKeys;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) HashMap(java.util.HashMap) Attribute(com.google.devtools.build.lib.packages.Attribute) Label(com.google.devtools.build.lib.cmdline.Label) ValueOrException2(com.google.devtools.build.skyframe.ValueOrException2) NoSuchPackageException(com.google.devtools.build.lib.packages.NoSuchPackageException) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Rule(com.google.devtools.build.lib.packages.Rule)

Example 8 with Rule

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

the class TransitiveTargetFunction method computeSkyValue.

@Override
public SkyValue computeSkyValue(TargetAndErrorIfAny targetAndErrorIfAny, TransitiveTargetValueBuilder builder) {
    Target target = targetAndErrorIfAny.getTarget();
    NoSuchTargetException errorLoadingTarget = targetAndErrorIfAny.getErrorLoadingTarget();
    // Get configuration fragments directly required by this rule.
    if (target instanceof Rule) {
        Rule rule = (Rule) target;
        // Declared by the rule class:
        ConfigurationFragmentPolicy configurationFragmentPolicy = rule.getRuleClassObject().getConfigurationFragmentPolicy();
        for (ConfigurationFragmentFactory factory : ruleClassProvider.getConfigurationFragments()) {
            Class<? extends Fragment> fragment = factory.creates();
            // (named) fragments.
            if (configurationFragmentPolicy.isLegalConfigurationFragment(fragment)) {
                addFragmentIfNew(builder, fragment.asSubclass(BuildConfiguration.Fragment.class));
            }
        }
        // Declared by late-bound attributes:
        for (Attribute attr : rule.getAttributes()) {
            if (attr.isLateBound()) {
                addFragmentsIfNew(builder, attr.getLateBoundDefault().getRequiredConfigurationFragments());
            }
        }
        // corresponding fragments in their configurations to properly resolve:
        if (rule.getRuleClass().equals(ConfigSettingRule.RULE_NAME)) {
            addFragmentsIfNew(builder, ConfigSettingRule.requiresConfigurationFragments(rule, optionsToFragmentMap));
        }
        // Fragments to unconditionally include:
        addFragmentIfNew(builder, ruleClassProvider.getUniversalFragment().asSubclass(BuildConfiguration.Fragment.class));
    }
    return builder.build(errorLoadingTarget);
}
Also used : Target(com.google.devtools.build.lib.packages.Target) ConfigurationFragmentPolicy(com.google.devtools.build.lib.packages.ConfigurationFragmentPolicy) NoSuchTargetException(com.google.devtools.build.lib.packages.NoSuchTargetException) Attribute(com.google.devtools.build.lib.packages.Attribute) ConfigSettingRule(com.google.devtools.build.lib.analysis.config.ConfigRuleClasses.ConfigSettingRule) Rule(com.google.devtools.build.lib.packages.Rule) ConfigurationFragmentFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory) Fragment(com.google.devtools.build.lib.analysis.config.BuildConfiguration.Fragment)

Example 9 with Rule

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

the class TransitiveTraversalValue method forTarget.

static TransitiveTraversalValue forTarget(Target target, @Nullable String firstErrorMessage) {
    if (target instanceof Rule) {
        Rule rule = (Rule) target;
        RuleClass ruleClass = rule.getRuleClassObject();
        if (firstErrorMessage == null && !ruleClass.isSkylark()) {
            TransitiveTraversalValue value = VALUES_BY_RULE_CLASS.get(ruleClass);
            if (value != null) {
                return value;
            }
            AdvertisedProviderSet providers = ruleClass.getAdvertisedProviders();
            value = new TransitiveTraversalValue(providers, null);
            // May already be there from another RuleClass or a concurrent put.
            value = VALUE_INTERNER.intern(value);
            // May already be there from a concurrent put.
            VALUES_BY_RULE_CLASS.putIfAbsent(ruleClass, value);
            return value;
        } else {
            // same providers.
            return TransitiveTraversalValue.create(rule.getRuleClassObject().getAdvertisedProviders(), firstErrorMessage);
        }
    }
    if (firstErrorMessage == null) {
        return EMPTY_VALUE;
    } else {
        return new TransitiveTraversalValue(AdvertisedProviderSet.EMPTY, firstErrorMessage);
    }
}
Also used : AdvertisedProviderSet(com.google.devtools.build.lib.packages.AdvertisedProviderSet) Rule(com.google.devtools.build.lib.packages.Rule) RuleClass(com.google.devtools.build.lib.packages.RuleClass)

Example 10 with Rule

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

the class CppConfigurationLoader method createParameters.

@Nullable
protected CppConfigurationParameters createParameters(ConfigurationEnvironment env, BuildOptions options) throws InvalidConfigurationException, InterruptedException {
    BlazeDirectories directories = env.getBlazeDirectories();
    if (directories == null) {
        return null;
    }
    Label crosstoolTopLabel = RedirectChaser.followRedirects(env, options.get(CppOptions.class).crosstoolTop, "crosstool_top");
    if (crosstoolTopLabel == null) {
        return null;
    }
    CrosstoolConfigurationLoader.CrosstoolFile file = CrosstoolConfigurationLoader.readCrosstool(env, crosstoolTopLabel);
    if (file == null) {
        return null;
    }
    CrosstoolConfig.CToolchain toolchain = CrosstoolConfigurationLoader.selectToolchain(file.getProto(), options, cpuTransformer);
    // FDO
    // TODO(bazel-team): move this to CppConfiguration.prepareHook
    CppOptions cppOptions = options.get(CppOptions.class);
    Path fdoZip;
    if (cppOptions.fdoOptimize == null) {
        fdoZip = null;
    } else if (cppOptions.fdoOptimize.startsWith("//")) {
        try {
            Target target = env.getTarget(Label.parseAbsolute(cppOptions.fdoOptimize));
            if (target == null) {
                return null;
            }
            if (!(target instanceof InputFile)) {
                throw new InvalidConfigurationException("--fdo_optimize cannot accept targets that do not refer to input files");
            }
            fdoZip = env.getPath(target.getPackage(), target.getName());
            if (fdoZip == null) {
                throw new InvalidConfigurationException("The --fdo_optimize parameter you specified resolves to a file that does not exist");
            }
        } catch (NoSuchPackageException | NoSuchTargetException | LabelSyntaxException e) {
            env.getEventHandler().handle(Event.error(e.getMessage()));
            throw new InvalidConfigurationException(e);
        }
    } else {
        fdoZip = directories.getWorkspace().getRelative(cppOptions.fdoOptimize);
        try {
            // We don't check for file existence, but at least the filename should be well-formed.
            FileSystemUtils.checkBaseName(fdoZip.getBaseName());
        } catch (IllegalArgumentException e) {
            throw new InvalidConfigurationException(e);
        }
    }
    Label ccToolchainLabel;
    Target crosstoolTop;
    try {
        crosstoolTop = env.getTarget(crosstoolTopLabel);
    } catch (NoSuchThingException e) {
        // Should have been found out during redirect chasing
        throw new IllegalStateException(e);
    }
    if (crosstoolTop instanceof Rule && ((Rule) crosstoolTop).getRuleClass().equals("cc_toolchain_suite")) {
        Rule ccToolchainSuite = (Rule) crosstoolTop;
        ccToolchainLabel = NonconfigurableAttributeMapper.of(ccToolchainSuite).get("toolchains", BuildType.LABEL_DICT_UNARY).get(toolchain.getTargetCpu() + "|" + toolchain.getCompiler());
        if (ccToolchainLabel == null) {
            throw new InvalidConfigurationException(String.format("cc_toolchain_suite '%s' does not contain a toolchain for CPU '%s' and compiler '%s'", crosstoolTopLabel, toolchain.getTargetCpu(), toolchain.getCompiler()));
        }
    } else {
        throw new InvalidConfigurationException(String.format("The specified --crosstool_top '%s' is not a valid cc_toolchain_suite rule", crosstoolTopLabel));
    }
    Target ccToolchain;
    try {
        ccToolchain = env.getTarget(ccToolchainLabel);
        if (ccToolchain == null) {
            return null;
        }
    } catch (NoSuchThingException e) {
        throw new InvalidConfigurationException(String.format("The toolchain rule '%s' does not exist", ccToolchainLabel));
    }
    if (!(ccToolchain instanceof Rule) || !CcToolchainRule.isCcToolchain(ccToolchain)) {
        throw new InvalidConfigurationException(String.format("The label '%s' is not a cc_toolchain rule", ccToolchainLabel));
    }
    return new CppConfigurationParameters(toolchain, file.getMd5(), options, fdoZip, crosstoolTopLabel, ccToolchainLabel);
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Label(com.google.devtools.build.lib.cmdline.Label) InputFile(com.google.devtools.build.lib.packages.InputFile) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) Target(com.google.devtools.build.lib.packages.Target) NoSuchThingException(com.google.devtools.build.lib.packages.NoSuchThingException) Rule(com.google.devtools.build.lib.packages.Rule) CrosstoolConfig(com.google.devtools.build.lib.view.config.crosstool.CrosstoolConfig) 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