use of com.google.devtools.build.lib.cmdline.RepositoryName in project bazel by bazelbuild.
the class ArtifactFactory method findSourceRoot.
/**
* Probe the known packages to find the longest package prefix up until the base, or until the
* root directory if our execPath doesn't start with baseExecPath due to uplevel references.
*/
@Nullable
private Root findSourceRoot(PathFragment execPath, @Nullable PathFragment baseExecPath, @Nullable Root baseRoot, RepositoryName repositoryName) {
PathFragment dir = execPath.getParentDirectory();
if (dir == null) {
return null;
}
Pair<RepositoryName, PathFragment> repo = RepositoryName.fromPathFragment(dir);
if (repo != null) {
repositoryName = repo.getFirst();
dir = repo.getSecond();
}
while (dir != null && !dir.equals(baseExecPath)) {
Root sourceRoot = packageRoots.get(PackageIdentifier.create(repositoryName, dir));
if (sourceRoot != null) {
return sourceRoot;
}
dir = dir.getParentDirectory();
}
return dir != null && dir.equals(baseExecPath) ? baseRoot : null;
}
use of com.google.devtools.build.lib.cmdline.RepositoryName in project bazel by bazelbuild.
the class RepositoryDelegatorFunction method compute.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
RepositoryName repositoryName = (RepositoryName) skyKey.argument();
Rule rule = RepositoryFunction.getRule(repositoryName, null, env);
if (rule == null) {
return null;
}
BlazeDirectories directories = PrecomputedValue.BLAZE_DIRECTORIES.get(env);
if (directories == null) {
return null;
}
RepositoryFunction handler;
if (rule.getRuleClassObject().isSkylark()) {
handler = skylarkHandler;
} else {
handler = handlers.get(rule.getRuleClass());
}
if (handler == null) {
throw new RepositoryFunctionException(new EvalException(Location.fromFile(directories.getWorkspace().getRelative("WORKSPACE")), "Could not find handler for " + rule), Transience.PERSISTENT);
}
handler.setClientEnvironment(clientEnvironment);
Path repoRoot = RepositoryFunction.getExternalRepositoryDirectory(directories).getRelative(rule.getName());
byte[] ruleSpecificData = handler.getRuleSpecificMarkerData(rule, env);
if (ruleSpecificData == null) {
return null;
}
String ruleKey = computeRuleKey(rule, ruleSpecificData);
Map<String, String> markerData = new TreeMap<>();
Path markerPath = getMarkerPath(directories, rule);
if (handler.isLocal(rule)) {
// Local repositories are always fetched because the operation is generally fast and they do
// not depend on non-local data, so it does not make much sense to try to cache from across
// server instances.
setupRepositoryRoot(repoRoot);
RepositoryDirectoryValue.Builder localRepo = handler.fetch(rule, repoRoot, directories, env, markerData);
if (localRepo == null) {
return null;
} else {
// We write the marker file for local repository essentially for getting the digest and
// injecting it in the RepositoryDirectoryValue.
byte[] digest = writeMarkerFile(markerPath, markerData, ruleKey);
return localRepo.setDigest(digest).build();
}
}
// We check the repository root for existence here, but we can't depend on the FileValue,
// because it's possible that we eventually create that directory in which case the FileValue
// and the state of the file system would be inconsistent.
byte[] markerHash = isFilesystemUpToDate(markerPath, rule, ruleKey, handler, env);
if (env.valuesMissing()) {
return null;
}
if (markerHash != null && repoRoot.exists()) {
// Now that we know that it exists, we can declare a Skyframe dependency on the repository
// root.
RepositoryFunction.getRepositoryDirectory(repoRoot, env);
if (env.valuesMissing()) {
return null;
}
return RepositoryDirectoryValue.builder().setPath(repoRoot).setDigest(markerHash).build();
}
if (isFetch.get()) {
// Fetching enabled, go ahead.
setupRepositoryRoot(repoRoot);
RepositoryDirectoryValue.Builder result = handler.fetch(rule, repoRoot, directories, env, markerData);
if (env.valuesMissing()) {
return null;
}
// No new Skyframe dependencies must be added between calling the repository implementation
// and writing the marker file because if they aren't computed, it would cause a Skyframe
// restart thus calling the possibly very slow (networking, decompression...) fetch()
// operation again. So we write the marker file here immediately.
byte[] digest = writeMarkerFile(markerPath, markerData, ruleKey);
return result.setDigest(digest).build();
}
if (!repoRoot.exists()) {
// The repository isn't on the file system, there is nothing we can do.
throw new RepositoryFunctionException(new IOException("to fix, run\n\tbazel fetch //...\nExternal repository " + repositoryName + " not found and fetching repositories is disabled."), Transience.TRANSIENT);
}
// Declare a Skyframe dependency so that this is re-evaluated when something happens to the
// directory.
FileValue repoRootValue = RepositoryFunction.getRepositoryDirectory(repoRoot, env);
if (env.valuesMissing()) {
return null;
}
// Try to build with whatever is on the file system and emit a warning.
env.getListener().handle(Event.warn(rule.getLocation(), String.format("External repository '%s' is not up-to-date and fetching is disabled. To update, " + "run the build without the '--nofetch' command line option.", rule.getName())));
return RepositoryDirectoryValue.builder().setPath(repoRootValue.realRootedPath().asPath()).setFetchingDelayed().build();
}
use of com.google.devtools.build.lib.cmdline.RepositoryName in project bazel by bazelbuild.
the class RepositoryLoaderFunction method compute.
@Nullable
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws SkyFunctionException, InterruptedException {
// This cannot be combined with {@link RepositoryDelegatorFunction}. RDF fetches the
// repository and must not have a Skyframe restart after writing it (otherwise the repository
// would be re-downloaded).
RepositoryName nameFromRule = (RepositoryName) skyKey.argument();
SkyKey repositoryKey = RepositoryDirectoryValue.key(nameFromRule);
RepositoryDirectoryValue repository = (RepositoryDirectoryValue) env.getValue(repositoryKey);
if (repository == null) {
return null;
}
SkyKey workspaceKey = WorkspaceFileValue.key(RootedPath.toRootedPath(repository.getPath(), new PathFragment("WORKSPACE")));
WorkspaceFileValue workspacePackage = (WorkspaceFileValue) env.getValue(workspaceKey);
if (workspacePackage == null) {
return null;
}
RepositoryName workspaceName;
try {
String workspaceNameStr = workspacePackage.getPackage().getWorkspaceName();
workspaceName = workspaceNameStr.isEmpty() ? RepositoryName.create("") : RepositoryName.create("@" + workspaceNameStr);
} catch (LabelSyntaxException e) {
throw new IllegalStateException(e);
}
if (!workspaceName.isDefault() && !workspaceName.strippedName().equals(Label.DEFAULT_REPOSITORY_DIRECTORY) && !nameFromRule.equals(workspaceName)) {
Path workspacePath = repository.getPath().getRelative("WORKSPACE");
env.getListener().handle(Event.warn(Location.fromFile(workspacePath), "Workspace name in " + workspacePath + " (" + workspaceName + ") does not match the " + "name given in the repository's definition (" + nameFromRule + "); this will " + "cause a build error in future versions"));
}
return new RepositoryValue(nameFromRule, repository);
}
use of com.google.devtools.build.lib.cmdline.RepositoryName in project bazel by bazelbuild.
the class PrepareDepsOfTargetsUnderDirectoryFunction method compute.
@Override
public SkyValue compute(SkyKey skyKey, Environment env) throws InterruptedException {
PrepareDepsOfTargetsUnderDirectoryKey argument = (PrepareDepsOfTargetsUnderDirectoryKey) skyKey.argument();
final FilteringPolicy filteringPolicy = argument.getFilteringPolicy();
RecursivePkgKey recursivePkgKey = argument.getRecursivePkgKey();
ProcessPackageDirectory processPackageDirectory = new ProcessPackageDirectory(directories, new ProcessPackageDirectory.SkyKeyTransformer() {
@Override
public SkyKey makeSkyKey(RepositoryName repository, RootedPath subdirectory, ImmutableSet<PathFragment> excludedSubdirectoriesBeneathSubdirectory) {
return PrepareDepsOfTargetsUnderDirectoryValue.key(repository, subdirectory, excludedSubdirectoriesBeneathSubdirectory, filteringPolicy);
}
});
ProcessPackageDirectoryResult packageExistenceAndSubdirDeps = processPackageDirectory.getPackageExistenceAndSubdirDeps(recursivePkgKey.getRootedPath(), recursivePkgKey.getRepository(), env, recursivePkgKey.getExcludedPaths());
if (env.valuesMissing()) {
return null;
}
Iterable<SkyKey> keysToRequest = packageExistenceAndSubdirDeps.getChildDeps();
if (packageExistenceAndSubdirDeps.packageExists()) {
keysToRequest = Iterables.concat(ImmutableList.of(CollectTargetsInPackageValue.key(PackageIdentifier.create(recursivePkgKey.getRepository(), recursivePkgKey.getRootedPath().getRelativePath()), filteringPolicy)), keysToRequest);
}
env.getValuesOrThrow(keysToRequest, NoSuchPackageException.class);
if (env.valuesMissing()) {
return null;
}
return PrepareDepsOfTargetsUnderDirectoryValue.INSTANCE;
}
use of com.google.devtools.build.lib.cmdline.RepositoryName in project bazel by bazelbuild.
the class HeaderDiscovery method discoverInputsFromDotdFiles.
/**
* Returns a collection with additional input artifacts relevant to the action by reading the
* dynamically-discovered dependency information from the .d file after the action has run.
*
* <p>Artifacts are considered inputs but not "mandatory" inputs.
*
* @throws ActionExecutionException iff the .d is missing (when required), malformed, or has
* unresolvable included artifacts.
*/
@VisibleForTesting
@ThreadCompatible
public NestedSet<Artifact> discoverInputsFromDotdFiles(Path execRoot, ArtifactResolver artifactResolver) throws ActionExecutionException {
NestedSetBuilder<Artifact> inputs = NestedSetBuilder.stableOrder();
if (dotdFile == null) {
return inputs.build();
}
List<Path> systemIncludePrefixes = permittedSystemIncludePrefixes;
// Check inclusions.
IncludeProblems problems = new IncludeProblems();
for (Path execPath : depSet.getDependencies()) {
PathFragment execPathFragment = execPath.asFragment();
if (execPathFragment.isAbsolute()) {
// Absolute includes from system paths are ignored.
if (FileSystemUtils.startsWithAny(execPath, systemIncludePrefixes)) {
continue;
}
// the build with an error.
if (execPath.startsWith(execRoot)) {
// funky but tolerable path
execPathFragment = execPath.relativeTo(execRoot);
} else {
problems.add(execPathFragment.getPathString());
continue;
}
}
Artifact artifact = allowedDerivedInputsMap.get(execPathFragment);
if (artifact == null) {
try {
RepositoryName repository = PackageIdentifier.discoverFromExecPath(execPathFragment, false).getRepository();
artifact = artifactResolver.resolveSourceArtifact(execPathFragment, repository);
} catch (LabelSyntaxException e) {
throw new ActionExecutionException(String.format("Could not find the external repository for %s", execPathFragment), e, action, false);
}
}
if (artifact != null) {
inputs.add(artifact);
// to the set of actual inputs.
if (specialInputsHandler != null) {
inputs.addAll(specialInputsHandler.getInputsForIncludedFile(artifact, artifactResolver));
}
} else {
// Abort if we see files that we can't resolve, likely caused by
// undeclared includes or illegal include constructs.
problems.add(execPathFragment.getPathString());
}
}
if (shouldValidateInclusions) {
problems.assertProblemFree(action, sourceFile);
}
return inputs.build();
}
Aggregations