Skip to content

Commit

Permalink
Fix bazelbuild#1709: Print debug messages on OS X when --sandbox_debu…
Browse files Browse the repository at this point in the history
…g is specified.

--
MOS_MIGRATED_REVID=131941585
  • Loading branch information
philwo authored and aehlig committed Sep 1, 2016
1 parent 45fd841 commit af4bf30
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import com.google.devtools.build.lib.vfs.SearchPath;
import com.google.devtools.build.lib.vfs.Symlinks;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -153,10 +154,20 @@ public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext)
SandboxHelpers.reportSubcommand(executor, spawn);
SandboxHelpers.postActionStatusMessage(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");

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());

Expand All @@ -166,7 +177,11 @@ public void exec(Spawn spawn, ActionExecutionContext actionExecutionContext)

try {
HardlinkedExecRoot hardlinkedExecRoot =
new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot);
new HardlinkedExecRoot(execRoot, sandboxPath, sandboxExecRoot, errWriter);
if (errWriter != null) {
errWriter.flush();
}

ImmutableSet<PathFragment> outputs = SandboxHelpers.getOutputFiles(spawn);
hardlinkedExecRoot.createFileSystem(
getMounts(spawn, actionExecutionContext), outputs, writableDirs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -34,11 +35,14 @@ public class HardlinkedExecRoot implements SandboxExecRoot {
private final Path execRoot;
private final Path sandboxPath;
private final Path sandboxExecRoot;
private final PrintWriter errWriter;

public HardlinkedExecRoot(Path execRoot, Path sandboxPath, Path sandboxExecRoot) {
public HardlinkedExecRoot(
Path execRoot, Path sandboxPath, Path sandboxExecRoot, PrintWriter errWriter) {
this.execRoot = execRoot;
this.sandboxPath = sandboxPath;
this.sandboxExecRoot = sandboxExecRoot;
this.errWriter = errWriter;
}

@Override
Expand All @@ -51,6 +55,9 @@ public void createFileSystem(

// Create all needed directories.
for (Path createDir : writableDirs) {
if (errWriter != null) {
errWriter.printf("createdir: %s\n", createDir.getPathString());
}
FileSystemUtils.createDirectoryAndParents(createDir);
}

Expand Down Expand Up @@ -91,23 +98,29 @@ private void linkInputs(Map<PathFragment, Path> inputs) throws IOException {

for (ImmutableMap.Entry<PathFragment, Path> entry : inputs.entrySet()) {
// Hardlink, resolve symlink here instead in finalizeLinks.
Path source = entry.getValue().resolveSymbolicLinks();
Path target =
source.startsWith(execRoot)
? inputsDir.getRelative(source.relativeTo(execRoot))
Path target = entry.getValue().resolveSymbolicLinks();
Path hardlinkName =
target.startsWith(execRoot)
? inputsDir.getRelative(target.relativeTo(execRoot))
: inputsDir.getRelative(entry.getKey());
if (errWriter != null) {
errWriter.printf("hardlink: %s -> %s\n", hardlinkName, target);
}
try {
createHardLink(target, source);
createHardLink(hardlinkName, target);
} catch (IOException e) {
// Creating a hardlink might fail when the input file and the sandbox directory are not on
// the same filesystem / device. Then we use symlink instead.
target.createSymbolicLink(source);
hardlinkName.createSymbolicLink(target);
}

// symlink
Path symlinkNewPath = sandboxExecRoot.getRelative(entry.getKey());
FileSystemUtils.createDirectoryAndParents(symlinkNewPath.getParentDirectory());
symlinkNewPath.createSymbolicLink(target);
Path symlinkName = sandboxExecRoot.getRelative(entry.getKey());
if (errWriter != null) {
errWriter.printf("symlink: %s -> %s\n", symlinkName, hardlinkName);
}
FileSystemUtils.createDirectoryAndParents(symlinkName.getParentDirectory());
symlinkName.createSymbolicLink(hardlinkName);
}
}

Expand Down

0 comments on commit af4bf30

Please sign in to comment.