Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 2 | // |
| 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 Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 14 | |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 15 | package com.google.devtools.build.lib.sandbox; |
| 16 | |
| 17 | import com.google.common.collect.ImmutableList; |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.buildtool.BuildRequest; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.exec.ExecutorBuilder; |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.runtime.BlazeModule; |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 22 | import com.google.devtools.build.lib.runtime.Command; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.util.Fingerprint; |
| 25 | import com.google.devtools.build.lib.vfs.FileSystem; |
| 26 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
| 27 | import com.google.devtools.build.lib.vfs.Path; |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 28 | import com.google.devtools.common.options.OptionsBase; |
Yue Gan | 4257ff9 | 2016-08-08 12:39:02 +0000 | [diff] [blame] | 29 | import java.io.IOException; |
Philipp Wollermann | 43c4a1a | 2015-08-25 12:52:57 +0000 | [diff] [blame] | 30 | |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 31 | /** |
| 32 | * This module provides the Sandbox spawn strategy. |
| 33 | */ |
Philipp Wollermann | 5a50b4f | 2016-08-31 12:07:40 +0000 | [diff] [blame] | 34 | public final class SandboxModule extends BlazeModule { |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 35 | private Path sandboxBase; |
| 36 | private boolean shouldCleanupSandboxBase; |
| 37 | |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 38 | @Override |
| 39 | public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) { |
Ulf Adams | 755dd84 | 2016-06-22 13:43:38 +0000 | [diff] [blame] | 40 | return "build".equals(command.name()) |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 41 | ? ImmutableList.<Class<? extends OptionsBase>>of(SandboxOptions.class) |
| 42 | : ImmutableList.<Class<? extends OptionsBase>>of(); |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | @Override |
Philipp Wollermann | 182e0ff | 2017-05-08 11:27:51 -0400 | [diff] [blame] | 46 | public void executorInit( |
| 47 | CommandEnvironment cmdEnv, BuildRequest request, ExecutorBuilder builder) { |
| 48 | BlazeDirectories blazeDirs = cmdEnv.getDirectories(); |
| 49 | String productName = cmdEnv.getRuntime().getProductName(); |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 50 | 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 Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 65 | try { |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 66 | FileSystemUtils.createDirectoryAndParents(sandboxBase); |
| 67 | builder.addActionContextProvider( |
Philipp Wollermann | 182e0ff | 2017-05-08 11:27:51 -0400 | [diff] [blame] | 68 | SandboxActionContextProvider.create(cmdEnv, request, sandboxBase)); |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 69 | } catch (IOException e) { |
| 70 | throw new IllegalStateException(e); |
| 71 | } |
Philipp Wollermann | 182e0ff | 2017-05-08 11:27:51 -0400 | [diff] [blame] | 72 | builder.addActionContextConsumer(new SandboxActionContextConsumer(cmdEnv)); |
Philipp Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 73 | } |
philwo | 754325e | 2017-04-07 12:35:07 +0000 | [diff] [blame] | 74 | |
| 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 Wollermann | 1572344e | 2015-06-29 13:59:45 +0000 | [diff] [blame] | 90 | } |