Search in sources :

Example 51 with PathFragment

use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.

the class ActionCacheChecker method getCachedInputs.

@Nullable
public Iterable<Artifact> getCachedInputs(Action action, PackageRootResolver resolver) throws PackageRootResolutionException, InterruptedException {
    ActionCache.Entry entry = getCacheEntry(action);
    if (entry == null || entry.isCorrupted()) {
        return ImmutableList.of();
    }
    List<PathFragment> outputs = new ArrayList<>();
    for (Artifact output : action.getOutputs()) {
        outputs.add(output.getExecPath());
    }
    List<PathFragment> inputExecPaths = new ArrayList<>();
    for (String path : entry.getPaths()) {
        PathFragment execPath = new PathFragment(path);
        // most efficient.
        if (!outputs.contains(execPath)) {
            inputExecPaths.add(execPath);
        }
    }
    // Note that this method may trigger a violation of the desirable invariant that getInputs()
    // is a superset of getMandatoryInputs(). See bug about an "action not in canonical form"
    // error message and the integration test test_crosstool_change_and_failure().
    Map<PathFragment, Artifact> allowedDerivedInputsMap = new HashMap<>();
    for (Artifact derivedInput : action.getAllowedDerivedInputs()) {
        if (!derivedInput.isSourceArtifact()) {
            allowedDerivedInputsMap.put(derivedInput.getExecPath(), derivedInput);
        }
    }
    List<Artifact> inputArtifacts = new ArrayList<>();
    List<PathFragment> unresolvedPaths = new ArrayList<>();
    for (PathFragment execPath : inputExecPaths) {
        Artifact artifact = allowedDerivedInputsMap.get(execPath);
        if (artifact != null) {
            inputArtifacts.add(artifact);
        } else {
            // Remember this execPath, we will try to resolve it as a source artifact.
            unresolvedPaths.add(execPath);
        }
    }
    Map<PathFragment, Artifact> resolvedArtifacts = artifactResolver.resolveSourceArtifacts(unresolvedPaths, resolver);
    if (resolvedArtifacts == null) {
        // We are missing some dependencies. We need to rerun this update later.
        return null;
    }
    for (PathFragment execPath : unresolvedPaths) {
        Artifact artifact = resolvedArtifacts.get(execPath);
        // was used before) and will force action execution.
        if (artifact != null) {
            inputArtifacts.add(artifact);
        }
    }
    return inputArtifacts;
}
Also used : ActionCache(com.google.devtools.build.lib.actions.cache.ActionCache) HashMap(java.util.HashMap) Entry(com.google.devtools.build.lib.actions.cache.ActionCache.Entry) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) Nullable(javax.annotation.Nullable)

Example 52 with PathFragment

use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.

the class ActionInputHelper method asTreeFileArtifacts.

/** Returns a Set of TreeFileArtifacts with the given parent and parent-relative paths. */
public static Set<TreeFileArtifact> asTreeFileArtifacts(final Artifact parent, Set<? extends PathFragment> parentRelativePaths) {
    Preconditions.checkState(parent.isTreeArtifact(), "Given parent %s must be a TreeArtifact", parent);
    ImmutableSet.Builder<TreeFileArtifact> builder = ImmutableSet.builder();
    for (PathFragment path : parentRelativePaths) {
        builder.add(treeFileArtifact(parent, path));
    }
    return builder.build();
}
Also used : TreeFileArtifact(com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact) ImmutableSet(com.google.common.collect.ImmutableSet) PathFragment(com.google.devtools.build.lib.vfs.PathFragment)

Example 53 with PathFragment

use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.

the class Artifact method asRootPrefixedExecPath.

@VisibleForTesting
static String asRootPrefixedExecPath(Artifact artifact) {
    PathFragment execPath = artifact.getExecPath();
    PathFragment rootRel = artifact.getRootRelativePath();
    if (execPath.equals(rootRel)) {
        return ":" + rootRel.getPathString();
    } else {
        //if (execPath.endsWith(rootRel)) {
        PathFragment rootPrefix = trimTail(execPath, rootRel);
        return rootPrefix.getPathString() + ":" + rootRel.getPathString();
    }
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 54 with PathFragment

use of com.google.devtools.build.lib.vfs.PathFragment in project bazel by bazelbuild.

the class ArtifactFactory method resolveSourceArtifactWithAncestor.

/**
   * Returns an {@link Artifact} with exec path formed by composing {@code baseExecPath} and
   * {@code relativePath} (via {@code baseExecPath.getRelative(relativePath)} if baseExecPath is
   * not null). That Artifact will have root determined by the package roots of this factory if it
   * lives in a subpackage distinct from that of baseExecPath, and {@code baseRoot} otherwise.
   */
public synchronized Artifact resolveSourceArtifactWithAncestor(PathFragment relativePath, PathFragment baseExecPath, Root baseRoot, RepositoryName repositoryName) {
    Preconditions.checkState((baseExecPath == null) == (baseRoot == null), "%s %s %s", relativePath, baseExecPath, baseRoot);
    Preconditions.checkState(relativePath.segmentCount() > 0, "%s %s %s", relativePath, baseExecPath, baseRoot);
    PathFragment execPath = baseExecPath == null ? relativePath : baseExecPath.getRelative(relativePath);
    execPath = execPath.normalize();
    if (execPath.containsUplevelReferences()) {
        // Source exec paths cannot escape the source root.
        return null;
    }
    // Don't create an artifact if it's derived.
    if (isDerivedArtifact(execPath)) {
        return null;
    }
    Root sourceRoot = findSourceRoot(execPath, baseExecPath, baseRoot, repositoryName);
    Artifact artifact = sourceArtifactCache.getArtifactIfValid(execPath);
    if (artifact != null) {
        Preconditions.checkState(sourceRoot == null || sourceRoot.equals(artifact.getRoot()));
        return artifact;
    }
    return createArtifactIfNotValid(sourceRoot, execPath);
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment)

Example 55 with PathFragment

use of com.google.devtools.build.lib.vfs.PathFragment 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;
}
Also used : PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) Nullable(javax.annotation.Nullable)

Aggregations

PathFragment (com.google.devtools.build.lib.vfs.PathFragment)512 Test (org.junit.Test)208 Artifact (com.google.devtools.build.lib.actions.Artifact)184 Path (com.google.devtools.build.lib.vfs.Path)111 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)65 SkyKey (com.google.devtools.build.skyframe.SkyKey)56 IOException (java.io.IOException)38 ArrayList (java.util.ArrayList)35 ImmutableList (com.google.common.collect.ImmutableList)32 Root (com.google.devtools.build.lib.actions.Root)32 HashMap (java.util.HashMap)27 Label (com.google.devtools.build.lib.cmdline.Label)26 LinkedHashMap (java.util.LinkedHashMap)26 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)23 ImmutableMap (com.google.common.collect.ImmutableMap)22 Map (java.util.Map)21 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)20 FilesetTraversalParams (com.google.devtools.build.lib.actions.FilesetTraversalParams)16 PackageIdentifier (com.google.devtools.build.lib.cmdline.PackageIdentifier)16 NestedSetBuilder (com.google.devtools.build.lib.collect.nestedset.NestedSetBuilder)16