use of com.google.devtools.build.lib.packages.NoSuchThingException in project bazel by bazelbuild.
the class CompileOneDependencyTransformer method listContainsFile.
/**
* Returns true if a specific rule compiles a specific source. Looks through genrules and
* filegroups.
*/
private boolean listContainsFile(ExtendedEventHandler eventHandler, Collection<Label> srcLabels, Label source, Set<Label> visitedRuleLabels) throws TargetParsingException, InterruptedException {
if (srcLabels.contains(source)) {
return true;
}
for (Label label : srcLabels) {
if (!visitedRuleLabels.add(label)) {
continue;
}
Target target = null;
try {
target = targetProvider.getTarget(eventHandler, label);
} catch (NoSuchThingException e) {
// Just ignore failing sources/packages. We could report them here, but as long as we do
// early return, the presence of this error would then be determined by the order of items
// in the srcs attribute. A proper error will be created by the subsequent loading.
}
if (target == null || target instanceof FileTarget) {
continue;
}
Rule targetRule = target.getAssociatedRule();
if ("filegroup".equals(targetRule.getRuleClass())) {
RawAttributeMapper attributeMapper = RawAttributeMapper.of(targetRule);
Collection<Label> srcs = attributeMapper.getMergedValues("srcs", BuildType.LABEL_LIST);
if (listContainsFile(eventHandler, srcs, source, visitedRuleLabels)) {
return true;
}
} else if ("genrule".equals(targetRule.getRuleClass())) {
// TODO(djasper): Likely, it makes much more sense to look at the inputs of a genrule.
for (OutputFile file : targetRule.getOutputFiles()) {
if (file.getLabel().equals(source)) {
return true;
}
}
}
}
return false;
}
use of com.google.devtools.build.lib.packages.NoSuchThingException in project bazel by bazelbuild.
the class BlazeQueryEnvironment method getTargetsMatchingPatternImpl.
private void getTargetsMatchingPatternImpl(String pattern, Callback<Target> callback) throws QueryException, InterruptedException {
// We can safely ignore the boolean error flag. The evaluateQuery() method above wraps the
// entire query computation in an error sensor.
Set<Target> targets = new LinkedHashSet<>(resolvedTargetPatterns.get(pattern));
// Sets.filter would be more convenient here, but can't deal with exceptions.
Iterator<Target> targetIterator = targets.iterator();
while (targetIterator.hasNext()) {
Target target = targetIterator.next();
if (!validateScope(target.getLabel(), strictScope)) {
targetIterator.remove();
}
}
Set<PathFragment> packages = new HashSet<>();
for (Target target : targets) {
packages.add(target.getLabel().getPackageFragment());
}
Set<Target> result = new LinkedHashSet<>();
for (Target target : targets) {
result.add(getOrCreate(target));
// targets in this set.
if (target instanceof OutputFile) {
OutputFile outputFile = (OutputFile) target;
if (targets.contains(outputFile.getGeneratingRule())) {
makeEdge(outputFile, outputFile.getGeneratingRule());
}
} else if (target instanceof Rule) {
Rule rule = (Rule) target;
for (Label label : rule.getLabels(dependencyFilter)) {
if (!packages.contains(label.getPackageFragment())) {
// don't cause additional package loading
continue;
}
try {
if (!validateScope(label, strictScope)) {
// Don't create edges to targets which are out of scope.
continue;
}
Target to = getTargetOrThrow(label);
if (targets.contains(to)) {
makeEdge(rule, to);
}
} catch (NoSuchThingException e) {
/* ignore */
}
}
}
}
callback.process(result);
}
use of com.google.devtools.build.lib.packages.NoSuchThingException in project bazel by bazelbuild.
the class TransitiveTargetFunction method getAspectLabels.
@Override
protected Collection<Label> getAspectLabels(Rule fromRule, Attribute attr, Label toLabel, ValueOrException2<NoSuchPackageException, NoSuchTargetException> toVal, final Environment env) throws InterruptedException {
SkyKey packageKey = PackageValue.key(toLabel.getPackageIdentifier());
try {
PackageValue pkgValue = (PackageValue) env.getValueOrThrow(packageKey, NoSuchPackageException.class);
if (pkgValue == null) {
return ImmutableList.of();
}
Package pkg = pkgValue.getPackage();
if (pkg.containsErrors()) {
// TransitiveTargetValue.
return ImmutableList.of();
}
Target dependedTarget = pkgValue.getPackage().getTarget(toLabel.getName());
return AspectDefinition.visitAspectsIfRequired(fromRule, attr, dependedTarget, DependencyFilter.ALL_DEPS).values();
} catch (NoSuchThingException e) {
// TransitiveTargetValue.
return ImmutableList.of();
}
}
use of com.google.devtools.build.lib.packages.NoSuchThingException 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);
}
use of com.google.devtools.build.lib.packages.NoSuchThingException in project bazel by bazelbuild.
the class CrosstoolConfigurationLoader method getCrosstoolProtofromBuildFile.
private static CrosstoolProto getCrosstoolProtofromBuildFile(ConfigurationEnvironment env, Label crosstoolTop) throws InterruptedException {
Target target;
try {
target = env.getTarget(crosstoolTop);
} catch (NoSuchThingException e) {
// Should have beeen evaluated by RedirectChaser
throw new IllegalStateException(e);
}
if (!(target instanceof Rule)) {
return null;
}
Rule rule = (Rule) target;
if (!(rule.getRuleClass().equals("cc_toolchain_suite")) || !rule.isAttributeValueExplicitlySpecified("proto")) {
return null;
}
final String contents = NonconfigurableAttributeMapper.of(rule).get("proto", Type.STRING);
byte[] md5 = new Fingerprint().addBytes(contents.getBytes(UTF_8)).digestAndReset();
return new CrosstoolProto(md5, "cc_toolchain_suite rule " + crosstoolTop.toString()) {
@Override
public String getContents() throws IOException {
return contents;
}
};
}
Aggregations