Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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. |
| 14 | package com.google.devtools.build.lib.exec; |
| 15 | |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 16 | import com.google.common.annotations.VisibleForTesting; |
Damien Martin-Guillerez | e5b7c59 | 2016-01-18 11:03:59 +0000 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableMap; |
cparsons | 8358148 | 2018-04-16 11:49:24 -0700 | [diff] [blame] | 20 | import com.google.common.collect.Lists; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.devtools.build.lib.actions.ActionExecutionContext; |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.actions.ActionExecutionMetadata; |
| 23 | import com.google.devtools.build.lib.actions.ActionInput; |
| 24 | import com.google.devtools.build.lib.actions.Artifact; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.devtools.build.lib.actions.ExecException; |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ExecutionRequirements; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.actions.ResourceSet; |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.SimpleSpawn; |
| 29 | import com.google.devtools.build.lib.actions.Spawn; |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 30 | import com.google.devtools.build.lib.actions.SpawnResult; |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.actions.UserExecException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 33 | import com.google.devtools.build.lib.shell.CommandException; |
| 34 | import com.google.devtools.build.lib.util.CommandBuilder; |
| 35 | import com.google.devtools.build.lib.util.OsUtils; |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.vfs.Path; |
| 38 | import com.google.devtools.build.lib.vfs.PathFragment; |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 39 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | import java.util.List; |
| 41 | |
| 42 | /** |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 43 | * Helper class responsible for the symlink tree creation. Used to generate runfiles and fileset |
| 44 | * symlink farms. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | */ |
| 46 | public final class SymlinkTreeHelper { |
Ulf Adams | 015aad9 | 2016-07-13 16:49:40 +0000 | [diff] [blame] | 47 | @VisibleForTesting |
| 48 | public static final String BUILD_RUNFILES = "build-runfiles" + OsUtils.executableExtension(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 51 | * These actions run faster overall when serialized, because most of their cost is in the ext2 |
| 52 | * block allocator, and there's less seeking required if their directory creations get |
| 53 | * non-interleaved allocations. So we give them a huge resource cost. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | */ |
Mark Schaller | 5d81dd5 | 2015-02-20 23:14:47 +0000 | [diff] [blame] | 55 | public static final ResourceSet RESOURCE_SET = ResourceSet.createWithRamCpuIo(1000, 0.5, 0.75); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 57 | private final Path inputManifest; |
| 58 | private final Path symlinkTreeRoot; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | private final boolean filesetTree; |
| 60 | |
| 61 | /** |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 62 | * Creates SymlinkTreeHelper instance. Can be used independently of SymlinkTreeAction. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 63 | * |
| 64 | * @param inputManifest exec path to the input runfiles manifest |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 65 | * @param symlinkTreeRoot the root of the symlink tree to be created |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | * @param filesetTree true if this is fileset symlink tree, |
| 67 | * false if this is a runfiles symlink tree. |
| 68 | */ |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 69 | public SymlinkTreeHelper(Path inputManifest, Path symlinkTreeRoot, boolean filesetTree) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | this.inputManifest = inputManifest; |
| 71 | this.symlinkTreeRoot = symlinkTreeRoot; |
| 72 | this.filesetTree = filesetTree; |
| 73 | } |
| 74 | |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 75 | public Path getOutputManifest() { |
| 76 | return symlinkTreeRoot; |
| 77 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | |
| 79 | /** |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 80 | * Creates a symlink tree using a CommandBuilder. This means that the symlink tree will always be |
| 81 | * present on the developer's workstation. Useful when running commands locally. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | * |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 83 | * Warning: this method REALLY executes the command on the box Bazel is running on, without any |
| 84 | * kind of synchronization, locking, or anything else. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | * |
| 86 | * @param config the configuration that is used for creating the symlink tree. |
| 87 | * @throws CommandException |
| 88 | */ |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 89 | public void createSymlinksUsingCommand( |
| 90 | Path execRoot, BuildConfiguration config, BinTools binTools) |
| 91 | throws CommandException { |
ulfjack | d3dd6a1 | 2018-03-06 00:49:37 -0800 | [diff] [blame] | 92 | List<String> argv = getSpawnArgumentList(execRoot, binTools.getExecPath(BUILD_RUNFILES)); |
cparsons | 8358148 | 2018-04-16 11:49:24 -0700 | [diff] [blame] | 93 | CommandBuilder builder = new CommandBuilder(); |
| 94 | builder.addArgs(argv); |
| 95 | builder.setWorkingDir(execRoot); |
| 96 | builder.build().execute(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 100 | * Creates symlink tree using appropriate method. At this time tree always created using |
| 101 | * build-runfiles helper application. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | * |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 103 | * @param owner action instance that requested symlink tree creation |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | * @param actionExecutionContext Services that are in the scope of the action. |
Dmitry Lomov | a148b4c | 2016-06-21 12:04:34 +0000 | [diff] [blame] | 105 | * @param enableRunfiles |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 106 | * @return a list of SpawnResults created during symlink creation, if any |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | */ |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 108 | public List<SpawnResult> createSymlinks( |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 109 | ActionExecutionMetadata owner, |
Dmitry Lomov | dfe2f10 | 2016-02-12 14:41:05 +0000 | [diff] [blame] | 110 | ActionExecutionContext actionExecutionContext, |
| 111 | BinTools binTools, |
Dmitry Lomov | a148b4c | 2016-06-21 12:04:34 +0000 | [diff] [blame] | 112 | ImmutableMap<String, String> shellEnvironment, |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 113 | Artifact inputManifestArtifact, |
Dmitry Lomov | a148b4c | 2016-06-21 12:04:34 +0000 | [diff] [blame] | 114 | boolean enableRunfiles) |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 115 | throws ExecException, InterruptedException { |
shahan | 18726b7 | 2018-03-15 14:18:46 -0700 | [diff] [blame] | 116 | Preconditions.checkState( |
| 117 | actionExecutionContext.getInputPath(inputManifestArtifact).equals(inputManifest)); |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 118 | if (enableRunfiles) { |
Googler | 3b9e152 | 2018-03-26 11:03:30 -0700 | [diff] [blame] | 119 | Spawn spawn = |
| 120 | createSpawn( |
| 121 | owner, |
| 122 | actionExecutionContext.getExecRoot(), |
| 123 | binTools, |
| 124 | shellEnvironment, |
| 125 | inputManifestArtifact); |
ruperts | 4050a89 | 2017-10-07 00:46:20 +0200 | [diff] [blame] | 126 | return actionExecutionContext |
Googler | 2c3990c | 2018-03-26 16:33:56 -0700 | [diff] [blame] | 127 | .getSpawnActionContext(spawn) |
Googler | 3b9e152 | 2018-03-26 11:03:30 -0700 | [diff] [blame] | 128 | .exec(spawn, actionExecutionContext); |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 129 | } else { |
| 130 | // Pretend we created the runfiles tree by copying the manifest |
| 131 | try { |
ulfjack | d3dd6a1 | 2018-03-06 00:49:37 -0800 | [diff] [blame] | 132 | symlinkTreeRoot.createDirectoryAndParents(); |
Lukacs Berki | 31b059f | 2016-08-04 11:55:20 +0000 | [diff] [blame] | 133 | FileSystemUtils.copyFile(inputManifest, symlinkTreeRoot.getChild("MANIFEST")); |
| 134 | } catch (IOException e) { |
| 135 | throw new UserExecException(e.getMessage(), e); |
| 136 | } |
ruperts | 7967f33 | 2017-11-21 16:37:13 -0800 | [diff] [blame] | 137 | return ImmutableList.of(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 141 | @VisibleForTesting |
| 142 | Spawn createSpawn( |
| 143 | ActionExecutionMetadata owner, |
| 144 | Path execRoot, |
| 145 | BinTools binTools, |
| 146 | ImmutableMap<String, String> environment, |
| 147 | ActionInput inputManifestArtifact) { |
ulfjack | d3dd6a1 | 2018-03-06 00:49:37 -0800 | [diff] [blame] | 148 | ActionInput buildRunfiles = binTools.getActionInput(BUILD_RUNFILES); |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 149 | return new SimpleSpawn( |
| 150 | owner, |
cparsons | 8358148 | 2018-04-16 11:49:24 -0700 | [diff] [blame] | 151 | getSpawnArgumentList(execRoot, buildRunfiles.getExecPath()), |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 152 | environment, |
| 153 | ImmutableMap.of( |
| 154 | ExecutionRequirements.LOCAL, "", |
| 155 | ExecutionRequirements.NO_CACHE, "", |
| 156 | ExecutionRequirements.NO_SANDBOX, ""), |
ulfjack | d3dd6a1 | 2018-03-06 00:49:37 -0800 | [diff] [blame] | 157 | ImmutableList.of(inputManifestArtifact, buildRunfiles), |
ulfjack | 4d7f8f7 | 2017-11-29 03:37:04 -0800 | [diff] [blame] | 158 | /*outputs=*/ ImmutableList.of(), |
| 159 | RESOURCE_SET); |
| 160 | } |
| 161 | |
cparsons | 8358148 | 2018-04-16 11:49:24 -0700 | [diff] [blame] | 162 | /** |
| 163 | * Returns the complete argument list build-runfiles has to be called with. |
| 164 | */ |
| 165 | private ImmutableList<String> getSpawnArgumentList(Path execRoot, PathFragment buildRunfiles) { |
| 166 | List<String> args = Lists.newArrayList(); |
ulfjack | d3dd6a1 | 2018-03-06 00:49:37 -0800 | [diff] [blame] | 167 | args.add(buildRunfiles.getPathString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 168 | |
| 169 | if (filesetTree) { |
| 170 | args.add("--allow_relative"); |
| 171 | args.add("--use_metadata"); |
| 172 | } |
| 173 | |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 174 | args.add(inputManifest.relativeTo(execRoot).getPathString()); |
| 175 | args.add(symlinkTreeRoot.relativeTo(execRoot).getPathString()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 176 | |
cparsons | 8358148 | 2018-04-16 11:49:24 -0700 | [diff] [blame] | 177 | return ImmutableList.copyOf(args); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | } |
| 179 | } |