blob: 18caee78da9d508899d5a399f8c2bd2c72d7547b [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Philipp Wollermann1572344e2015-06-29 13:59:45 +00002//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
Philipp Wollermann5a50b4f2016-08-31 12:07:40 +000014
Philipp Wollermann1572344e2015-06-29 13:59:45 +000015package com.google.devtools.build.lib.sandbox;
16
17import com.google.common.collect.ImmutableList;
philwo754325e2017-04-07 12:35:07 +000018import com.google.devtools.build.lib.analysis.BlazeDirectories;
Philipp Wollermann1572344e2015-06-29 13:59:45 +000019import com.google.devtools.build.lib.buildtool.BuildRequest;
Ulf Adamsdba3c832016-12-21 16:50:02 +000020import com.google.devtools.build.lib.exec.ExecutorBuilder;
Philipp Wollermann1572344e2015-06-29 13:59:45 +000021import com.google.devtools.build.lib.runtime.BlazeModule;
Philipp Wollermann1572344e2015-06-29 13:59:45 +000022import com.google.devtools.build.lib.runtime.Command;
Ulf Adams633f5392015-09-15 11:13:08 +000023import com.google.devtools.build.lib.runtime.CommandEnvironment;
philwo754325e2017-04-07 12:35:07 +000024import com.google.devtools.build.lib.util.Fingerprint;
25import com.google.devtools.build.lib.vfs.FileSystem;
26import com.google.devtools.build.lib.vfs.FileSystemUtils;
27import com.google.devtools.build.lib.vfs.Path;
Philipp Wollermann590ea392015-08-25 13:57:33 +000028import com.google.devtools.common.options.OptionsBase;
Yue Gan4257ff92016-08-08 12:39:02 +000029import java.io.IOException;
Philipp Wollermann43c4a1a2015-08-25 12:52:57 +000030
Philipp Wollermann1572344e2015-06-29 13:59:45 +000031/**
32 * This module provides the Sandbox spawn strategy.
33 */
Philipp Wollermann5a50b4f2016-08-31 12:07:40 +000034public final class SandboxModule extends BlazeModule {
philwo754325e2017-04-07 12:35:07 +000035 private Path sandboxBase;
36 private boolean shouldCleanupSandboxBase;
37
Philipp Wollermann590ea392015-08-25 13:57:33 +000038 @Override
39 public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) {
Ulf Adams755dd842016-06-22 13:43:38 +000040 return "build".equals(command.name())
Philipp Wollermann590ea392015-08-25 13:57:33 +000041 ? ImmutableList.<Class<? extends OptionsBase>>of(SandboxOptions.class)
42 : ImmutableList.<Class<? extends OptionsBase>>of();
Philipp Wollermann1572344e2015-06-29 13:59:45 +000043 }
44
45 @Override
Philipp Wollermann182e0ff2017-05-08 11:27:51 -040046 public void executorInit(
47 CommandEnvironment cmdEnv, BuildRequest request, ExecutorBuilder builder) {
48 BlazeDirectories blazeDirs = cmdEnv.getDirectories();
49 String productName = cmdEnv.getRuntime().getProductName();
philwo754325e2017-04-07 12:35:07 +000050 SandboxOptions sandboxOptions = request.getOptions(SandboxOptions.class);
51 FileSystem fs = blazeDirs.getFileSystem();
52
53 if (sandboxOptions.sandboxBase.isEmpty()) {
54 sandboxBase = blazeDirs.getOutputBase().getRelative(productName + "-sandbox");
55 } else {
56 String dirName =
57 productName + "-sandbox." + Fingerprint.md5Digest(blazeDirs.getOutputBase().toString());
58 sandboxBase = fs.getPath(sandboxOptions.sandboxBase).getRelative(dirName);
59 }
60
61 // Do not remove the sandbox base when --sandbox_debug was specified so that people can check
62 // out the contents of the generated sandbox directories.
63 shouldCleanupSandboxBase = !sandboxOptions.sandboxDebug;
64
Ulf Adamsa0e3af42016-10-31 16:52:48 +000065 try {
philwo754325e2017-04-07 12:35:07 +000066 FileSystemUtils.createDirectoryAndParents(sandboxBase);
67 builder.addActionContextProvider(
Philipp Wollermann182e0ff2017-05-08 11:27:51 -040068 SandboxActionContextProvider.create(cmdEnv, request, sandboxBase));
Ulf Adamsa0e3af42016-10-31 16:52:48 +000069 } catch (IOException e) {
70 throw new IllegalStateException(e);
71 }
Philipp Wollermann182e0ff2017-05-08 11:27:51 -040072 builder.addActionContextConsumer(new SandboxActionContextConsumer(cmdEnv));
Philipp Wollermann1572344e2015-06-29 13:59:45 +000073 }
philwo754325e2017-04-07 12:35:07 +000074
75 @Override
76 public void afterCommand() {
77 super.afterCommand();
78
79 if (sandboxBase != null) {
80 if (shouldCleanupSandboxBase) {
81 try {
82 FileSystemUtils.deleteTree(sandboxBase);
83 } catch (IOException e) {
84 // Nothing we can do at this point.
85 }
86 }
87 sandboxBase = null;
88 }
89 }
Philipp Wollermann1572344e2015-06-29 13:59:45 +000090}