Search in sources :

Example 1 with RepositoryName

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

Example 2 with RepositoryName

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();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) FileValue(com.google.devtools.build.lib.skyframe.FileValue) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) EvalException(com.google.devtools.build.lib.syntax.EvalException) IOException(java.io.IOException) TreeMap(java.util.TreeMap) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) Rule(com.google.devtools.build.lib.packages.Rule) RepositoryFunctionException(com.google.devtools.build.lib.rules.repository.RepositoryFunction.RepositoryFunctionException)

Example 3 with RepositoryName

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);
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) RootedPath(com.google.devtools.build.lib.vfs.RootedPath) Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) WorkspaceFileValue(com.google.devtools.build.lib.skyframe.WorkspaceFileValue) RepositoryValue(com.google.devtools.build.lib.skyframe.RepositoryValue) Nullable(javax.annotation.Nullable)

Example 4 with RepositoryName

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;
}
Also used : SkyKey(com.google.devtools.build.skyframe.SkyKey) RecursivePkgKey(com.google.devtools.build.lib.skyframe.RecursivePkgValue.RecursivePkgKey) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) PrepareDepsOfTargetsUnderDirectoryKey(com.google.devtools.build.lib.skyframe.PrepareDepsOfTargetsUnderDirectoryValue.PrepareDepsOfTargetsUnderDirectoryKey) FilteringPolicy(com.google.devtools.build.lib.pkgcache.FilteringPolicy) RootedPath(com.google.devtools.build.lib.vfs.RootedPath)

Example 5 with RepositoryName

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();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) LabelSyntaxException(com.google.devtools.build.lib.cmdline.LabelSyntaxException) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) RepositoryName(com.google.devtools.build.lib.cmdline.RepositoryName) ActionExecutionException(com.google.devtools.build.lib.actions.ActionExecutionException) Artifact(com.google.devtools.build.lib.actions.Artifact) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ThreadCompatible(com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible)

Aggregations

RepositoryName (com.google.devtools.build.lib.cmdline.RepositoryName)8 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)5 LabelSyntaxException (com.google.devtools.build.lib.cmdline.LabelSyntaxException)3 Path (com.google.devtools.build.lib.vfs.Path)3 Root (com.google.devtools.build.lib.actions.Root)2 Rule (com.google.devtools.build.lib.packages.Rule)2 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)2 SkyKey (com.google.devtools.build.skyframe.SkyKey)2 Nullable (javax.annotation.Nullable)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ActionExecutionException (com.google.devtools.build.lib.actions.ActionExecutionException)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ArtifactFactory (com.google.devtools.build.lib.actions.ArtifactFactory)1 ArtifactOwner (com.google.devtools.build.lib.actions.ArtifactOwner)1 BlazeDirectories (com.google.devtools.build.lib.analysis.BlazeDirectories)1 BuildInfoFactory (com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory)1 BuildInfoContext (com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory.BuildInfoContext)1 BuildInfoKey (com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory.BuildInfoKey)1 BuildInfoType (com.google.devtools.build.lib.analysis.buildinfo.BuildInfoFactory.BuildInfoType)1 BuildConfiguration (com.google.devtools.build.lib.analysis.config.BuildConfiguration)1