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;
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations