Search in sources :

Example 41 with Path

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

the class DarwinSandboxedStrategy method create.

public static DarwinSandboxedStrategy create(BuildRequest buildRequest, Map<String, String> clientEnv, BlazeDirectories blazeDirs, boolean verboseFailures, String productName) throws IOException {
    // On OS X, in addition to what is specified in $TMPDIR, two other temporary directories may be
    // written to by processes. We have to get their location by calling "getconf".
    List<String> confVars = ImmutableList.of("DARWIN_USER_TEMP_DIR", "DARWIN_USER_CACHE_DIR");
    ImmutableList.Builder<Path> writablePaths = ImmutableList.builder();
    for (String confVar : confVars) {
        Path path = blazeDirs.getFileSystem().getPath(getConfStr(confVar));
        if (path.exists()) {
            writablePaths.add(path);
        }
    }
    return new DarwinSandboxedStrategy(buildRequest, clientEnv, blazeDirs, verboseFailures, productName, writablePaths.build(), new SpawnHelpers(blazeDirs.getExecRoot()));
}
Also used : SearchPath(com.google.devtools.build.lib.vfs.SearchPath) Path(com.google.devtools.build.lib.vfs.Path) ImmutableList(com.google.common.collect.ImmutableList)

Example 42 with Path

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

the class DarwinSandboxedStrategy method finalizeLinks.

private Map<PathFragment, Path> finalizeLinks(Map<PathFragment, Path> unfinalized) throws IOException {
    HashMap<PathFragment, Path> finalizedLinks = new HashMap<>();
    for (Map.Entry<PathFragment, Path> mount : unfinalized.entrySet()) {
        PathFragment target = mount.getKey();
        Path source = mount.getValue();
        // have to deal with finalizing the link.
        if (source == null) {
            finalizedLinks.put(target, source);
            continue;
        }
        FileStatus stat = source.statNullable(Symlinks.NOFOLLOW);
        if (stat != null && stat.isDirectory()) {
            for (Path subSource : FileSystemUtils.traverseTree(source, Predicates.alwaysTrue())) {
                PathFragment subTarget = target.getRelative(subSource.relativeTo(source));
                finalizeLinksPath(finalizedLinks, subTarget, subSource, subSource.statNullable(Symlinks.NOFOLLOW));
            }
        } else {
            finalizeLinksPath(finalizedLinks, target, source, stat);
        }
    }
    return finalizedLinks;
}
Also used : SearchPath(com.google.devtools.build.lib.vfs.SearchPath) Path(com.google.devtools.build.lib.vfs.Path) FileStatus(com.google.devtools.build.lib.vfs.FileStatus) HashMap(java.util.HashMap) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 43 with Path

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

the class DarwinSandboxedStrategy method getRunUnderPath.

/**
   * If a --run_under= option is set and refers to a command via its path (as opposed to via its
   * label), we have to mount this. Note that this is best effort and works fine for shell scripts
   * and small binaries, but we can't track any further dependencies of this command.
   *
   * <p>If --run_under= refers to a label, it is automatically provided in the spawn's input files,
   * so mountInputs() will catch that case.
   */
private Path getRunUnderPath(Spawn spawn) {
    if (spawn.getResourceOwner() instanceof TestRunnerAction) {
        TestRunnerAction testRunnerAction = ((TestRunnerAction) spawn.getResourceOwner());
        RunUnder runUnder = testRunnerAction.getExecutionSettings().getRunUnder();
        if (runUnder != null && runUnder.getCommand() != null) {
            PathFragment sourceFragment = new PathFragment(runUnder.getCommand());
            Path mount;
            if (sourceFragment.isAbsolute()) {
                mount = blazeDirs.getFileSystem().getPath(sourceFragment);
            } else if (blazeDirs.getExecRoot().getRelative(sourceFragment).exists()) {
                mount = blazeDirs.getExecRoot().getRelative(sourceFragment);
            } else {
                List<Path> searchPath = SearchPath.parse(blazeDirs.getFileSystem(), clientEnv.get("PATH"));
                mount = SearchPath.which(searchPath, runUnder.getCommand());
            }
            // only need to hardlink when under workspace
            Path workspace = blazeDirs.getWorkspace();
            if (mount != null && mount.startsWith(workspace)) {
                return mount;
            }
        }
    }
    return null;
}
Also used : SearchPath(com.google.devtools.build.lib.vfs.SearchPath) Path(com.google.devtools.build.lib.vfs.Path) RunUnder(com.google.devtools.build.lib.analysis.config.RunUnder) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) TestRunnerAction(com.google.devtools.build.lib.rules.test.TestRunnerAction) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List)

Example 44 with Path

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

the class DarwinSandboxedStrategy method actuallyExec.

private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
    Executor executor = actionExecutionContext.getExecutor();
    SandboxHelpers.reportSubcommand(executor, spawn);
    PrintWriter errWriter = sandboxDebug ? new PrintWriter(actionExecutionContext.getFileOutErr().getErrorStream()) : null;
    // Each invocation of "exec" gets its own sandbox.
    Path sandboxPath = SandboxHelpers.getSandboxRoot(blazeDirs, productName, uuid, execCounter);
    Path sandboxExecRoot = sandboxPath.getRelative("execroot").getRelative(execRoot.getBaseName());
    if (errWriter != null) {
        errWriter.printf("sandbox root is %s\n", sandboxPath.toString());
        errWriter.printf("working dir is %s\n", sandboxExecRoot.toString());
    }
    ImmutableMap<String, String> spawnEnvironment = StandaloneSpawnStrategy.locallyDeterminedEnv(execRoot, productName, spawn.getEnvironment());
    Set<Path> writableDirs = getWritableDirs(sandboxExecRoot, spawn.getEnvironment());
    Path runUnderPath = getRunUnderPath(spawn);
    HardlinkedExecRoot hardlinkedExecRoot = new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot, errWriter);
    ImmutableSet<PathFragment> outputs = SandboxHelpers.getOutputFiles(spawn);
    try {
        hardlinkedExecRoot.createFileSystem(getMounts(spawn, actionExecutionContext), outputs, writableDirs);
    } catch (IOException e) {
        throw new UserExecException("Could not prepare sandbox directory", e);
    }
    // Flush our logs before executing the spawn, otherwise they might get overwritten.
    if (errWriter != null) {
        errWriter.flush();
    }
    DarwinSandboxRunner runner = new DarwinSandboxRunner(sandboxPath, sandboxExecRoot, getWritableDirs(sandboxExecRoot, spawnEnvironment), getInaccessiblePaths(), runUnderPath, verboseFailures);
    try {
        runSpawn(spawn, actionExecutionContext, spawnEnvironment, hardlinkedExecRoot, outputs, runner, writeOutputFiles);
    } finally {
        if (!sandboxDebug) {
            try {
                FileSystemUtils.deleteTree(sandboxPath);
            } catch (IOException e) {
                executor.getEventHandler().handle(Event.warn(String.format("Cannot delete sandbox directory after action execution: %s (%s)", sandboxPath.getPathString(), e)));
            }
        }
    }
}
Also used : SearchPath(com.google.devtools.build.lib.vfs.SearchPath) Path(com.google.devtools.build.lib.vfs.Path) Executor(com.google.devtools.build.lib.actions.Executor) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) UserExecException(com.google.devtools.build.lib.actions.UserExecException) IOException(java.io.IOException) PrintWriter(java.io.PrintWriter)

Example 45 with Path

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

the class DarwinSandboxedStrategy method getMounts.

@Override
public Map<PathFragment, Path> getMounts(Spawn spawn, ActionExecutionContext executionContext) throws ExecException {
    try {
        Map<PathFragment, Path> mounts = new HashMap<>();
        spawnHelpers.mountInputs(mounts, spawn, executionContext);
        Map<PathFragment, Path> unfinalized = new HashMap<>();
        spawnHelpers.mountRunfilesFromSuppliers(unfinalized, spawn);
        spawnHelpers.mountFilesFromFilesetManifests(unfinalized, spawn, executionContext);
        mounts.putAll(finalizeLinks(unfinalized));
        return mounts;
    } catch (IllegalArgumentException | IOException e) {
        throw new EnvironmentalExecException("Could not prepare mounts for sandbox execution", e);
    }
}
Also used : SearchPath(com.google.devtools.build.lib.vfs.SearchPath) Path(com.google.devtools.build.lib.vfs.Path) HashMap(java.util.HashMap) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) EnvironmentalExecException(com.google.devtools.build.lib.actions.EnvironmentalExecException)

Aggregations

Path (com.google.devtools.build.lib.vfs.Path)492 Test (org.junit.Test)250 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)111 RootedPath (com.google.devtools.build.lib.vfs.RootedPath)105 IOException (java.io.IOException)102 Artifact (com.google.devtools.build.lib.actions.Artifact)37 SkyKey (com.google.devtools.build.skyframe.SkyKey)37 ArrayList (java.util.ArrayList)29 SpecialArtifact (com.google.devtools.build.lib.actions.Artifact.SpecialArtifact)17 FileSystem (com.google.devtools.build.lib.vfs.FileSystem)17 InMemoryFileSystem (com.google.devtools.build.lib.vfs.inmemoryfs.InMemoryFileSystem)17 HashMap (java.util.HashMap)17 WindowsPath (com.google.devtools.build.lib.windows.WindowsFileSystem.WindowsPath)16 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)14 Before (org.junit.Before)14 Package (com.google.devtools.build.lib.packages.Package)13 FileStatus (com.google.devtools.build.lib.vfs.FileStatus)13 Map (java.util.Map)12 Nullable (javax.annotation.Nullable)12 Executor (com.google.devtools.build.lib.actions.Executor)10