use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.
the class CrosstoolConfigurationLoader method getCrosstoolProtoFromCrosstoolFile.
private static CrosstoolProto getCrosstoolProtoFromCrosstoolFile(ConfigurationEnvironment env, Label crosstoolTop) throws IOException, InvalidConfigurationException, InterruptedException {
final Path path;
try {
Package containingPackage = env.getTarget(crosstoolTop.getLocalTargetLabel("BUILD")).getPackage();
if (containingPackage == null) {
return null;
}
path = env.getPath(containingPackage, CROSSTOOL_CONFIGURATION_FILENAME);
} catch (LabelSyntaxException e) {
throw new InvalidConfigurationException(e);
} catch (NoSuchThingException e) {
// Handled later
return null;
}
if (path == null || !path.exists()) {
return null;
}
return new CrosstoolProto(path.getDigest(), "CROSSTOOL file " + path.getPathString()) {
@Override
public String getContents() throws IOException {
try (InputStream inputStream = path.getInputStream()) {
return new String(FileSystemUtils.readContentAsLatin1(inputStream));
}
}
};
}
use of com.google.devtools.build.lib.packages.Package 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);
}
use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.
the class AspectFunction method createAliasAspect.
private static SkyValue createAliasAspect(Environment env, Target originalTarget, Aspect aspect, AspectKey originalKey, ConfiguredTarget configuredTarget) throws InterruptedException {
ImmutableList<Label> aliasChain = configuredTarget.getProvider(AliasProvider.class).getAliasChain();
// Find the next alias in the chain: either the next alias (if there are two) or the name of
// the real configured target.
Label aliasLabel = aliasChain.size() > 1 ? aliasChain.get(1) : configuredTarget.getLabel();
SkyKey depKey = ActionLookupValue.key(originalKey.withLabel(aliasLabel));
// Compute the AspectValue of the target the alias refers to (which can itself be either an
// alias or a real target)
AspectValue real = (AspectValue) env.getValue(depKey);
if (env.valuesMissing()) {
return null;
}
NestedSet<Package> transitivePackages = NestedSetBuilder.<Package>stableOrder().addTransitive(real.getTransitivePackages()).add(originalTarget.getPackage()).build();
return new AspectValue(originalKey, aspect, originalTarget.getLabel(), originalTarget.getLocation(), ConfiguredAspect.forAlias(real.getConfiguredAspect()), ImmutableList.<ActionAnalysisMetadata>of(), transitivePackages);
}
use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.
the class LocalRepositoryLookupFunction method maybeCheckWorkspaceForRepository.
/**
* Checks whether the directory exists and is a workspace root. Returns {@link Optional#absent()}
* if Skyframe needs to re-run, {@link Optional#of(LocalRepositoryLookupValue)} otherwise.
*/
private Optional<LocalRepositoryLookupValue> maybeCheckWorkspaceForRepository(Environment env, final RootedPath directory) throws InterruptedException, LocalRepositoryLookupFunctionException {
// Look up the main WORKSPACE file by the external package, to find all repositories.
PackageLookupValue externalPackageLookupValue;
try {
externalPackageLookupValue = (PackageLookupValue) env.getValueOrThrow(PackageLookupValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER), BuildFileNotFoundException.class, InconsistentFilesystemException.class);
if (externalPackageLookupValue == null) {
return Optional.absent();
}
} catch (BuildFileNotFoundException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("BuildFileNotFoundException while loading the //external package", e), Transience.PERSISTENT);
} catch (InconsistentFilesystemException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("InconsistentFilesystemException while loading the //external package", e), Transience.PERSISTENT);
}
RootedPath workspacePath = externalPackageLookupValue.getRootedPath(Label.EXTERNAL_PACKAGE_IDENTIFIER);
SkyKey workspaceKey = WorkspaceFileValue.key(workspacePath);
do {
WorkspaceFileValue value;
try {
value = (WorkspaceFileValue) env.getValueOrThrow(workspaceKey, PackageFunctionException.class, NameConflictException.class);
if (value == null) {
return Optional.absent();
}
} catch (PackageFunctionException e) {
// TODO(jcater): When WFF is rewritten to not throw a PFE, update this.
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("PackageFunctionException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
} catch (NameConflictException e) {
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("NameConflictException while loading the root WORKSPACE file", e), Transience.PERSISTENT);
}
Package externalPackage = value.getPackage();
if (externalPackage.containsErrors()) {
Event.replayEventsOn(env.getListener(), externalPackage.getEvents());
}
// Find all local_repository rules in the WORKSPACE, and check if any have a "path" attribute
// the same as the requested directory.
Iterable<Rule> localRepositories = externalPackage.getRulesMatchingRuleClass(LocalRepositoryRule.NAME);
Rule rule = Iterables.find(localRepositories, new Predicate<Rule>() {
@Override
public boolean apply(@Nullable Rule rule) {
AggregatingAttributeMapper mapper = AggregatingAttributeMapper.of(rule);
PathFragment pathAttr = new PathFragment(mapper.get("path", Type.STRING));
return directory.getRelativePath().equals(pathAttr);
}
}, null);
if (rule != null) {
try {
return Optional.of(LocalRepositoryLookupValue.success(RepositoryName.create("@" + rule.getName())));
} catch (LabelSyntaxException e) {
// validated.
throw new LocalRepositoryLookupFunctionException(new ErrorDeterminingRepositoryException("LabelSyntaxException while creating the repository name from the rule " + rule.getName(), e), Transience.PERSISTENT);
}
}
workspaceKey = value.next();
// TODO(bazel-team): This loop can be quadratic in the number of load() statements, consider
// rewriting or unrolling.
} while (workspaceKey != null);
return Optional.of(LocalRepositoryLookupValue.notFound());
}
use of com.google.devtools.build.lib.packages.Package in project bazel by bazelbuild.
the class PackageFunction method getExternalPackage.
/**
* Adds a dependency on the WORKSPACE file, representing it as a special type of package.
*
* @throws PackageFunctionException if there is an error computing the workspace file or adding
* its rules to the //external package.
*/
private SkyValue getExternalPackage(Environment env, Path packageLookupPath) throws PackageFunctionException, InterruptedException {
RootedPath workspacePath = RootedPath.toRootedPath(packageLookupPath, Label.EXTERNAL_PACKAGE_FILE_NAME);
SkyKey workspaceKey = ExternalPackageFunction.key(workspacePath);
PackageValue workspace = null;
try {
// This may throw a NoSuchPackageException if the WORKSPACE file was malformed or had other
// problems. Since this function can't add much context, we silently bubble it up.
workspace = (PackageValue) env.getValueOrThrow(workspaceKey, IOException.class, FileSymlinkException.class, InconsistentFilesystemException.class, EvalException.class, SkylarkImportFailedException.class);
} catch (IOException | FileSymlinkException | InconsistentFilesystemException | EvalException | SkylarkImportFailedException e) {
throw new PackageFunctionException(new NoSuchPackageException(Label.EXTERNAL_PACKAGE_IDENTIFIER, "Error encountered while dealing with the WORKSPACE file: " + e.getMessage()), Transience.PERSISTENT);
}
if (workspace == null) {
return null;
}
Package pkg = workspace.getPackage();
Event.replayEventsOn(env.getListener(), pkg.getEvents());
packageFactory.afterDoneLoadingPackage(pkg);
return new PackageValue(pkg);
}
Aggregations