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.buildtool; |
| 15 | |
Eric Fellheimer | e9b41c6 | 2016-06-14 15:39:51 +0000 | [diff] [blame] | 16 | import static java.util.concurrent.TimeUnit.MILLISECONDS; |
| 17 | import static java.util.concurrent.TimeUnit.NANOSECONDS; |
| 18 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.base.Joiner; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.base.Predicate; |
| 21 | import com.google.common.base.Stopwatch; |
Chloe Calvarin | 5dc44f0 | 2017-03-21 14:40:28 +0000 | [diff] [blame] | 22 | import com.google.common.base.Strings; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | import com.google.common.collect.HashBasedTable; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.ImmutableMap; |
Philipp Wollermann | 1ecdc51 | 2016-02-26 21:55:31 +0000 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableMultimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.common.collect.ImmutableSet; |
| 28 | import com.google.common.collect.Iterables; |
Philipp Wollermann | 1ecdc51 | 2016-02-26 21:55:31 +0000 | [diff] [blame] | 29 | import com.google.common.collect.Multimap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.common.collect.Ordering; |
| 31 | import com.google.common.collect.Table; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.Action; |
| 33 | import com.google.devtools.build.lib.actions.ActionCacheChecker; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ActionContextMarker; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.ActionGraph; |
| 36 | import com.google.devtools.build.lib.actions.ActionInputFileCache; |
| 37 | import com.google.devtools.build.lib.actions.Artifact; |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.ArtifactFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.BuildFailedException; |
| 40 | import com.google.devtools.build.lib.actions.ExecException; |
| 41 | import com.google.devtools.build.lib.actions.ExecutionStrategy; |
| 42 | import com.google.devtools.build.lib.actions.Executor; |
| 43 | import com.google.devtools.build.lib.actions.Executor.ActionContext; |
| 44 | import com.google.devtools.build.lib.actions.ExecutorInitException; |
| 45 | import com.google.devtools.build.lib.actions.LocalHostCapacity; |
| 46 | import com.google.devtools.build.lib.actions.ResourceManager; |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.actions.ResourceSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 48 | import com.google.devtools.build.lib.actions.SpawnActionContext; |
| 49 | import com.google.devtools.build.lib.actions.TestExecException; |
| 50 | import com.google.devtools.build.lib.actions.cache.ActionCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 52 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.analysis.TopLevelArtifactContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 55 | import com.google.devtools.build.lib.analysis.WorkspaceStatusAction; |
Lukacs Berki | 7b2f2e8 | 2016-11-23 14:16:43 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.analysis.actions.SymlinkTreeActionContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 57 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
| 58 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
| 59 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionPhaseCompleteEvent; |
| 60 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionStartingEvent; |
Kristina Chodorow | 33aada2 | 2016-06-22 14:23:47 +0000 | [diff] [blame] | 61 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | import com.google.devtools.build.lib.events.Event; |
| 63 | import com.google.devtools.build.lib.events.EventHandler; |
| 64 | import com.google.devtools.build.lib.events.EventKind; |
| 65 | import com.google.devtools.build.lib.events.Reporter; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.exec.ActionContextConsumer; |
| 67 | import com.google.devtools.build.lib.exec.ActionContextProvider; |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.exec.ActionInputPrefetcher; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.exec.BlazeExecutor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 70 | import com.google.devtools.build.lib.exec.CheckUpToDateFilter; |
| 71 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.exec.ExecutorBuilder; |
| 73 | import com.google.devtools.build.lib.exec.FilesetActionContextImpl; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | import com.google.devtools.build.lib.exec.OutputService; |
| 75 | import com.google.devtools.build.lib.exec.SingleBuildFileCache; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | import com.google.devtools.build.lib.exec.SymlinkTreeStrategy; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | import com.google.devtools.build.lib.profiler.ProfilePhase; |
| 79 | import com.google.devtools.build.lib.profiler.Profiler; |
| 80 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
| 81 | import com.google.devtools.build.lib.rules.fileset.FilesetActionContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 82 | import com.google.devtools.build.lib.rules.test.TestActionContext; |
| 83 | import com.google.devtools.build.lib.runtime.BlazeModule; |
| 84 | import com.google.devtools.build.lib.runtime.BlazeRuntime; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 85 | import com.google.devtools.build.lib.runtime.CommandEnvironment; |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 86 | import com.google.devtools.build.lib.skyframe.AspectValue; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | import com.google.devtools.build.lib.skyframe.Builder; |
| 88 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 89 | import com.google.devtools.build.lib.util.AbruptExitException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 90 | import com.google.devtools.build.lib.util.ExitCode; |
| 91 | import com.google.devtools.build.lib.util.LoggingUtil; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 92 | import com.google.devtools.build.lib.util.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 93 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 94 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | import com.google.devtools.build.lib.vfs.Path; |
| 96 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | import java.io.IOException; |
| 98 | import java.io.OutputStream; |
| 99 | import java.io.PrintWriter; |
| 100 | import java.util.ArrayList; |
| 101 | import java.util.Collection; |
| 102 | import java.util.HashMap; |
| 103 | import java.util.HashSet; |
| 104 | import java.util.LinkedHashSet; |
| 105 | import java.util.List; |
| 106 | import java.util.Map; |
| 107 | import java.util.Set; |
Philipp Wollermann | a068dee | 2015-06-08 11:15:02 +0000 | [diff] [blame] | 108 | import java.util.TreeMap; |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 109 | import java.util.UUID; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 110 | import java.util.logging.Level; |
| 111 | import java.util.logging.Logger; |
| 112 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | /** |
| 114 | * This class manages the execution phase. The entry point is {@link #executeBuild}. |
| 115 | * |
| 116 | * <p>This is only intended for use by {@link BuildTool}. |
| 117 | * |
| 118 | * <p>This class contains an ActionCache, and refers to the BlazeRuntime's BuildView and |
| 119 | * PackageCache. |
| 120 | * |
| 121 | * @see BuildTool |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 122 | * @see com.google.devtools.build.lib.analysis.BuildView |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 123 | */ |
| 124 | public class ExecutionTool { |
| 125 | private static class StrategyConverter { |
| 126 | private Table<Class<? extends ActionContext>, String, ActionContext> classMap = |
| 127 | HashBasedTable.create(); |
| 128 | private Map<Class<? extends ActionContext>, ActionContext> defaultClassMap = |
| 129 | new HashMap<>(); |
| 130 | |
| 131 | /** |
| 132 | * Aggregates all {@link ActionContext}s that are in {@code contextProviders}. |
| 133 | */ |
| 134 | @SuppressWarnings("unchecked") |
| 135 | private StrategyConverter(Iterable<ActionContextProvider> contextProviders) { |
| 136 | for (ActionContextProvider provider : contextProviders) { |
| 137 | for (ActionContext strategy : provider.getActionContexts()) { |
| 138 | ExecutionStrategy annotation = |
| 139 | strategy.getClass().getAnnotation(ExecutionStrategy.class); |
| 140 | if (annotation != null) { |
| 141 | defaultClassMap.put(annotation.contextType(), strategy); |
| 142 | |
| 143 | for (String name : annotation.name()) { |
| 144 | classMap.put(annotation.contextType(), name, strategy); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | @SuppressWarnings("unchecked") |
| 152 | private <T extends ActionContext> T getStrategy(Class<T> clazz, String name) { |
| 153 | return (T) (name.isEmpty() ? defaultClassMap.get(clazz) : classMap.get(clazz, name)); |
| 154 | } |
| 155 | |
| 156 | private String getValidValues(Class<? extends ActionContext> context) { |
| 157 | return Joiner.on(", ").join(Ordering.natural().sortedCopy(classMap.row(context).keySet())); |
| 158 | } |
| 159 | |
| 160 | private String getUserFriendlyName(Class<? extends ActionContext> context) { |
| 161 | ActionContextMarker marker = context.getAnnotation(ActionContextMarker.class); |
| 162 | return marker != null |
| 163 | ? marker.name() |
| 164 | : context.getSimpleName(); |
| 165 | } |
| 166 | } |
| 167 | |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 168 | static final Logger log = Logger.getLogger(ExecutionTool.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 169 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 170 | private final CommandEnvironment env; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 171 | private final BlazeRuntime runtime; |
| 172 | private final BuildRequest request; |
| 173 | private BlazeExecutor executor; |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 174 | private final ActionInputFileCache fileCache; |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 175 | private final ActionInputPrefetcher prefetcher; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 176 | private final ImmutableList<ActionContextProvider> actionContextProviders; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 177 | |
Philipp Wollermann | a068dee | 2015-06-08 11:15:02 +0000 | [diff] [blame] | 178 | private Map<String, SpawnActionContext> spawnStrategyMap = |
| 179 | new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 180 | private List<ActionContext> strategies = new ArrayList<>(); |
| 181 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 182 | ExecutionTool(CommandEnvironment env, BuildRequest request) throws ExecutorInitException { |
| 183 | this.env = env; |
| 184 | this.runtime = env.getRuntime(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 185 | this.request = request; |
| 186 | |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 187 | |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 188 | // Create tools before getting the strategies from the modules as some of them need tools to |
| 189 | // determine whether the host actually supports certain strategies (e.g. sandboxing). |
| 190 | createToolsSymlinks(); |
| 191 | |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 192 | ExecutorBuilder builder = new ExecutorBuilder(); |
| 193 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 194 | module.executorInit(env, request, builder); |
| 195 | } |
| 196 | builder.addActionContextProvider( |
| 197 | new FilesetActionContextImpl.Provider(env.getReporter(), env.getWorkspaceName())); |
| 198 | builder.addActionContext(new SymlinkTreeStrategy( |
| 199 | env.getOutputService(), env.getBlazeWorkspace().getBinTools())); |
| 200 | // TODO(philwo) - the ExecutionTool should not add arbitrary dependencies on its own, instead |
| 201 | // these dependencies should be added to the ActionContextConsumer of the module that actually |
| 202 | // depends on them. |
| 203 | builder.addActionContextConsumer( |
| 204 | new ActionContextConsumer() { |
| 205 | @Override |
| 206 | public ImmutableMap<String, String> getSpawnActionContexts() { |
| 207 | return ImmutableMap.of(); |
| 208 | } |
| 209 | |
| 210 | @Override |
| 211 | public Multimap<Class<? extends ActionContext>, String> getActionContexts() { |
| 212 | return ImmutableMultimap.<Class<? extends ActionContext>, String>builder() |
| 213 | .put(FilesetActionContext.class, "") |
| 214 | .put(WorkspaceStatusAction.Context.class, "") |
| 215 | .put(SymlinkTreeActionContext.class, "") |
| 216 | .build(); |
| 217 | } |
| 218 | }); |
| 219 | |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 220 | ActionInputFileCache cache = builder.getActionInputFileCache(); |
| 221 | if (cache == null) { |
| 222 | // Unfortunately, the exec root cache is not shared with caches in the remote execution |
| 223 | // client. |
| 224 | cache = |
| 225 | new SingleBuildFileCache( |
| 226 | env.getExecRoot().getPathString(), env.getDirectories().getFileSystem()); |
| 227 | } |
| 228 | this.fileCache = cache; |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 229 | this.prefetcher = builder.getActionInputPrefetcher(); |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 230 | |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 231 | this.actionContextProviders = builder.getActionContextProviders(); |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 232 | for (ActionContextProvider provider : actionContextProviders) { |
| 233 | provider.init(fileCache, prefetcher); |
| 234 | } |
| 235 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 236 | StrategyConverter strategyConverter = new StrategyConverter(actionContextProviders); |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 237 | |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 238 | for (ActionContextConsumer consumer : builder.getActionContextConsumers()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | // There are many different SpawnActions, and we want to control the action context they use |
| 240 | // independently from each other, for example, to run genrules locally and Java compile action |
| 241 | // in prod. Thus, for SpawnActions, we decide the action context to use not only based on the |
| 242 | // context class, but also the mnemonic of the action. |
| 243 | for (Map.Entry<String, String> entry : consumer.getSpawnActionContexts().entrySet()) { |
| 244 | SpawnActionContext context = |
| 245 | strategyConverter.getStrategy(SpawnActionContext.class, entry.getValue()); |
| 246 | if (context == null) { |
Chloe Calvarin | 5dc44f0 | 2017-03-21 14:40:28 +0000 | [diff] [blame] | 247 | String strategy = Strings.emptyToNull(entry.getKey()); |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 248 | throw makeExceptionForInvalidStrategyValue( |
| 249 | entry.getValue(), |
Chloe Calvarin | 5dc44f0 | 2017-03-21 14:40:28 +0000 | [diff] [blame] | 250 | Joiner.on(' ').skipNulls().join(strategy, "spawn"), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 251 | strategyConverter.getValidValues(SpawnActionContext.class)); |
| 252 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 253 | spawnStrategyMap.put(entry.getKey(), context); |
| 254 | } |
| 255 | |
| 256 | for (Map.Entry<Class<? extends ActionContext>, String> entry : |
Philipp Wollermann | 1ecdc51 | 2016-02-26 21:55:31 +0000 | [diff] [blame] | 257 | consumer.getActionContexts().entries()) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 258 | ActionContext context = strategyConverter.getStrategy(entry.getKey(), entry.getValue()); |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 259 | if (context == null) { |
| 260 | throw makeExceptionForInvalidStrategyValue( |
| 261 | entry.getValue(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 262 | strategyConverter.getUserFriendlyName(entry.getKey()), |
| 263 | strategyConverter.getValidValues(entry.getKey())); |
| 264 | } |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 265 | strategies.add(context); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Luis Fernando Pino Duque | 9b1fb68 | 2016-06-16 10:04:25 +0000 | [diff] [blame] | 269 | String testStrategyValue = request.getOptions(ExecutionOptions.class).testStrategy; |
| 270 | ActionContext context = strategyConverter.getStrategy(TestActionContext.class, |
| 271 | testStrategyValue); |
| 272 | if (context == null) { |
| 273 | throw makeExceptionForInvalidStrategyValue(testStrategyValue, "test", |
| 274 | strategyConverter.getValidValues(TestActionContext.class)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | } |
Luis Fernando Pino Duque | 9b1fb68 | 2016-06-16 10:04:25 +0000 | [diff] [blame] | 276 | strategies.add(context); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | private static ExecutorInitException makeExceptionForInvalidStrategyValue(String value, |
| 280 | String strategy, String validValues) { |
| 281 | return new ExecutorInitException(String.format( |
| 282 | "'%s' is an invalid value for %s strategy. Valid values are: %s", value, strategy, |
| 283 | validValues), ExitCode.COMMAND_LINE_ERROR); |
| 284 | } |
| 285 | |
| 286 | Executor getExecutor() throws ExecutorInitException { |
| 287 | if (executor == null) { |
| 288 | executor = createExecutor(); |
| 289 | } |
| 290 | return executor; |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * Creates an executor for the current set of blaze runtime, execution options, and request. |
| 295 | */ |
| 296 | private BlazeExecutor createExecutor() |
| 297 | throws ExecutorInitException { |
| 298 | return new BlazeExecutor( |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 299 | env.getExecRoot(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 300 | getReporter(), |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 301 | env.getEventBus(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 302 | runtime.getClock(), |
| 303 | request, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 304 | strategies, |
| 305 | spawnStrategyMap, |
| 306 | actionContextProviders); |
| 307 | } |
| 308 | |
| 309 | void init() throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 310 | getExecutor(); |
| 311 | } |
| 312 | |
| 313 | void shutdown() { |
| 314 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
| 315 | actionContextProvider.executionPhaseEnding(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Performs the execution phase (phase 3) of the build, in which the Builder |
| 321 | * is applied to the action graph to bring the targets up to date. (This |
| 322 | * function will return prior to execution-proper if --nobuild was specified.) |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 323 | * @param buildId UUID of the build id |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 324 | * @param analysisResult the analysis phase output |
| 325 | * @param buildResult the mutable build result |
Lukacs Berki | 3ea4d44 | 2016-01-21 15:15:30 +0000 | [diff] [blame] | 326 | * @param packageRoots package roots collected from loading phase and BuildConfigurationCollection |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 327 | * creation |
| 328 | */ |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 329 | void executeBuild(UUID buildId, AnalysisResult analysisResult, |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 330 | BuildResult buildResult, |
| 331 | BuildConfigurationCollection configurations, |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 332 | ImmutableMap<PackageIdentifier, Path> packageRoots, |
| 333 | TopLevelArtifactContext topLevelArtifactContext) |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 334 | throws BuildFailedException, InterruptedException, TestExecException, AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 335 | Stopwatch timer = Stopwatch.createStarted(); |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 336 | prepare(packageRoots, analysisResult.getWorkspaceName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 337 | |
| 338 | ActionGraph actionGraph = analysisResult.getActionGraph(); |
| 339 | |
| 340 | // Get top-level artifacts. |
| 341 | ImmutableSet<Artifact> additionalArtifacts = analysisResult.getAdditionalArtifactsToBuild(); |
| 342 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 343 | OutputService outputService = env.getOutputService(); |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 344 | ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 345 | if (outputService != null) { |
Eric Fellheimer | 1246eb3 | 2015-11-18 15:41:38 +0000 | [diff] [blame] | 346 | modifiedOutputFiles = outputService.startBuild(buildId, |
| 347 | request.getBuildOptions().finalizeActions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 348 | } else { |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 349 | // TODO(bazel-team): this could be just another OutputService |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 350 | startLocalOutputBuild(analysisResult.getWorkspaceName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 351 | } |
| 352 | |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame^] | 353 | // Must be created after the output path is created above. |
| 354 | createActionLogDirectory(); |
| 355 | |
Lukacs Berki | ad359c6 | 2016-01-26 12:59:36 +0000 | [diff] [blame] | 356 | List<BuildConfiguration> targetConfigurations = configurations.getTargetConfigurations(); |
| 357 | BuildConfiguration targetConfiguration = targetConfigurations.size() == 1 |
| 358 | ? targetConfigurations.get(0) : null; |
| 359 | if (targetConfigurations.size() == 1) { |
Luis Fernando Pino Duque | 5a5bd8a | 2017-02-10 13:50:01 +0000 | [diff] [blame] | 360 | String productName = runtime.getProductName(); |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 361 | String dirName = env.getWorkspaceName(); |
| 362 | String workspaceName = analysisResult.getWorkspaceName(); |
Lukacs Berki | ad359c6 | 2016-01-26 12:59:36 +0000 | [diff] [blame] | 363 | OutputDirectoryLinksUtils.createOutputDirectoryLinks( |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 364 | dirName, env.getWorkspace(), env.getDirectories().getExecRoot(workspaceName), |
Luis Fernando Pino Duque | 5a5bd8a | 2017-02-10 13:50:01 +0000 | [diff] [blame] | 365 | env.getDirectories().getOutputPath(workspaceName), getReporter(), targetConfiguration, |
| 366 | request.getBuildOptions().getSymlinkPrefix(productName), productName); |
Lukacs Berki | 85c63c4 | 2016-01-22 09:25:20 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 369 | ActionCache actionCache = getActionCache(); |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 370 | SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor(); |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 371 | Builder builder = createBuilder( |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 372 | request, actionCache, skyframeExecutor, modifiedOutputFiles); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 373 | |
| 374 | // |
| 375 | // Execution proper. All statements below are logically nested in |
| 376 | // begin/end pairs. No early returns or exceptions please! |
| 377 | // |
| 378 | |
| 379 | Collection<ConfiguredTarget> configuredTargets = buildResult.getActualTargets(); |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 380 | env.getEventBus().post(new ExecutionStartingEvent(configuredTargets)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 381 | |
| 382 | getReporter().handle(Event.progress("Building...")); |
| 383 | |
| 384 | // Conditionally record dependency-checker log: |
| 385 | ExplanationHandler explanationHandler = |
| 386 | installExplanationHandler(request.getBuildOptions().explanationPath, |
| 387 | request.getOptionsDescription()); |
| 388 | |
| 389 | Set<ConfiguredTarget> builtTargets = new HashSet<>(); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 390 | Collection<AspectValue> aspects = analysisResult.getAspects(); |
| 391 | |
| 392 | Iterable<Artifact> allArtifactsForProviders = |
| 393 | Iterables.concat( |
| 394 | additionalArtifacts, |
| 395 | TopLevelArtifactHelper.getAllArtifactsToBuild( |
| 396 | analysisResult.getTargetsToBuild(), analysisResult.getTopLevelContext()) |
| 397 | .getAllArtifacts(), |
| 398 | TopLevelArtifactHelper.getAllArtifactsToBuildFromAspects( |
| 399 | aspects, analysisResult.getTopLevelContext()) |
| 400 | .getAllArtifacts(), |
| 401 | //TODO(dslomov): Artifacts to test from aspects? |
| 402 | TopLevelArtifactHelper.getAllArtifactsToTest(analysisResult.getTargetsToTest())); |
| 403 | |
| 404 | if (request.isRunningInEmacs()) { |
| 405 | // The syntax of this message is tightly constrained by lisp/progmodes/compile.el in emacs |
| 406 | request.getOutErr().printErrLn("blaze: Entering directory `" + getExecRoot() + "/'"); |
| 407 | } |
| 408 | boolean buildCompleted = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 409 | try { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 410 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 411 | actionContextProvider.executionPhaseStarting(actionGraph, allArtifactsForProviders); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 412 | } |
| 413 | executor.executionPhaseStarting(); |
| 414 | skyframeExecutor.drainChangedFiles(); |
| 415 | |
| 416 | if (request.getViewOptions().discardAnalysisCache) { |
| 417 | // Free memory by removing cache entries that aren't going to be needed. Note that in |
| 418 | // skyframe full, this destroys the action graph as well, so we can only do it after the |
| 419 | // action graph is no longer needed. |
janakr | db4dec2 | 2017-03-28 22:39:35 +0000 | [diff] [blame] | 420 | env.getSkyframeBuildView() |
| 421 | .clearAnalysisCache(analysisResult.getTargetsToBuild(), analysisResult.getAspects()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | configureResourceManager(request); |
| 425 | |
| 426 | Profiler.instance().markPhase(ProfilePhase.EXECUTE); |
| 427 | |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 428 | builder.buildArtifacts( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 429 | env.getReporter(), |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 430 | additionalArtifacts, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 431 | analysisResult.getParallelTests(), |
| 432 | analysisResult.getExclusiveTests(), |
| 433 | analysisResult.getTargetsToBuild(), |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 434 | analysisResult.getAspects(), |
| 435 | executor, |
| 436 | builtTargets, |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 437 | request.getBuildOptions().explanationPath != null, |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 438 | env.getBlazeWorkspace().getLastExecutionTimeRange(), |
| 439 | topLevelArtifactContext); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 440 | buildCompleted = true; |
| 441 | } catch (BuildFailedException | TestExecException e) { |
| 442 | buildCompleted = true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 443 | throw e; |
| 444 | } finally { |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 445 | env.recordLastExecutionTime(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 446 | if (request.isRunningInEmacs()) { |
| 447 | request.getOutErr().printErrLn("blaze: Leaving directory `" + getExecRoot() + "/'"); |
| 448 | } |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 449 | if (buildCompleted) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 450 | getReporter().handle(Event.progress("Building complete.")); |
| 451 | } |
| 452 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 453 | env.getEventBus().post(new ExecutionFinishedEvent(ImmutableMap.<String, Long> of(), 0L, |
Miguel Alcon Pinto | 9186f68 | 2015-04-09 21:49:34 +0000 | [diff] [blame] | 454 | skyframeExecutor.getOutputDirtyFilesAndClear(), |
| 455 | skyframeExecutor.getModifiedFilesDuringPreviousBuildAndClear())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 456 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 457 | executor.executionPhaseEnding(); |
| 458 | for (ActionContextProvider actionContextProvider : actionContextProviders) { |
| 459 | actionContextProvider.executionPhaseEnding(); |
| 460 | } |
| 461 | |
| 462 | Profiler.instance().markPhase(ProfilePhase.FINISH); |
| 463 | |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 464 | if (buildCompleted) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 465 | saveCaches(actionCache); |
| 466 | } |
| 467 | |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 468 | try (AutoProfiler p = AutoProfiler.profiled("Show results", ProfilerTask.INFO)) { |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 469 | buildResult.setSuccessfulTargets( |
| 470 | determineSuccessfulTargets(configuredTargets, builtTargets, timer)); |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 471 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
| 472 | buildResultPrinter.showBuildResult( |
| 473 | request, buildResult, configuredTargets, analysisResult.getAspects()); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 474 | } |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 475 | |
| 476 | try (AutoProfiler p = AutoProfiler.profiled("Show artifacts", ProfilerTask.INFO)) { |
| 477 | if (request.getBuildOptions().showArtifacts) { |
| 478 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
| 479 | buildResultPrinter.showArtifacts( |
| 480 | request, configuredTargets, analysisResult.getAspects()); |
| 481 | } |
| 482 | } |
| 483 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 484 | if (explanationHandler != null) { |
| 485 | uninstallExplanationHandler(explanationHandler); |
| 486 | } |
| 487 | // Finalize output service last, so that if we do throw an exception, we know all the other |
| 488 | // code has already run. |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 489 | if (env.getOutputService() != null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 490 | boolean isBuildSuccessful = |
| 491 | buildResult.getSuccessfulTargets().size() == configuredTargets.size(); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 492 | env.getOutputService().finalizeBuild(isBuildSuccessful); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | } |
| 496 | |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 497 | private void prepare(ImmutableMap<PackageIdentifier, Path> packageRoots, String workspaceName) |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 498 | throws ExecutorInitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 499 | // Prepare for build. |
| 500 | Profiler.instance().markPhase(ProfilePhase.PREPARE); |
| 501 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 502 | // Plant the symlink forest. |
Kristina Chodorow | 52bc57c | 2016-06-20 14:27:56 +0000 | [diff] [blame] | 503 | try { |
Dmitry Lomov | e36a66c | 2017-02-17 14:48:48 +0000 | [diff] [blame] | 504 | new SymlinkForest( |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 505 | packageRoots, getExecRoot(), runtime.getProductName(), workspaceName) |
Kristina Chodorow | 36a6c17 | 2016-07-22 15:18:07 +0000 | [diff] [blame] | 506 | .plantSymlinkForest(); |
Kristina Chodorow | 52bc57c | 2016-06-20 14:27:56 +0000 | [diff] [blame] | 507 | } catch (IOException e) { |
| 508 | throw new ExecutorInitException("Source forest creation failed", e); |
| 509 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | private void createToolsSymlinks() throws ExecutorInitException { |
| 513 | try { |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 514 | env.getBlazeWorkspace().getBinTools().setupBuildTools(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 515 | } catch (ExecException e) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 516 | throw new ExecutorInitException("Tools symlink creation failed", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 520 | private void createActionLogDirectory() throws ExecutorInitException { |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame^] | 521 | Path directory = env.getActionConsoleOutputDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 522 | try { |
| 523 | if (directory.exists()) { |
| 524 | FileSystemUtils.deleteTree(directory); |
| 525 | } |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame^] | 526 | FileSystemUtils.createDirectoryAndParents(directory); |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 527 | } catch (IOException e) { |
| 528 | throw new ExecutorInitException("Couldn't delete action output directory", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 529 | } |
| 530 | } |
| 531 | |
| 532 | /** |
| 533 | * Prepare for a local output build. |
| 534 | */ |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 535 | private void startLocalOutputBuild(String workspaceName) throws ExecutorInitException { |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 536 | try (AutoProfiler p = AutoProfiler.profiled("Starting local output build", ProfilerTask.INFO)) { |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 537 | Path outputPath = env.getDirectories().getOutputPath(workspaceName); |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 538 | Path localOutputPath = env.getDirectories().getLocalOutputPath(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 539 | |
| 540 | if (outputPath.isSymbolicLink()) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 541 | try { |
| 542 | // Remove the existing symlink first. |
| 543 | outputPath.delete(); |
| 544 | if (localOutputPath.exists()) { |
| 545 | // Pre-existing local output directory. Move to outputPath. |
| 546 | localOutputPath.renameTo(outputPath); |
| 547 | } |
| 548 | } catch (IOException e) { |
| 549 | throw new ExecutorInitException("Couldn't handle local output directory symlinks", e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * If a path is supplied, creates and installs an ExplanationHandler. Returns |
| 557 | * an instance on success. Reports an error and returns null otherwise. |
| 558 | */ |
| 559 | private ExplanationHandler installExplanationHandler(PathFragment explanationPath, |
| 560 | String allOptions) { |
| 561 | if (explanationPath == null) { |
| 562 | return null; |
| 563 | } |
| 564 | ExplanationHandler handler; |
| 565 | try { |
| 566 | handler = new ExplanationHandler( |
| 567 | getWorkspace().getRelative(explanationPath).getOutputStream(), |
| 568 | allOptions); |
| 569 | } catch (IOException e) { |
| 570 | getReporter().handle(Event.warn(String.format( |
| 571 | "Cannot write explanation of rebuilds to file '%s': %s", |
| 572 | explanationPath, e.getMessage()))); |
| 573 | return null; |
| 574 | } |
| 575 | getReporter().handle( |
| 576 | Event.info("Writing explanation of rebuilds to '" + explanationPath + "'")); |
| 577 | getReporter().addHandler(handler); |
| 578 | return handler; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Uninstalls the specified ExplanationHandler (if any) and closes the log |
| 583 | * file. |
| 584 | */ |
| 585 | private void uninstallExplanationHandler(ExplanationHandler handler) { |
| 586 | if (handler != null) { |
| 587 | getReporter().removeHandler(handler); |
| 588 | handler.log.close(); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * An ErrorEventListener implementation that records DEPCHECKER events into a log |
| 594 | * file, iff the --explain flag is specified during a build. |
| 595 | */ |
| 596 | private static class ExplanationHandler implements EventHandler { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 597 | private final PrintWriter log; |
| 598 | |
| 599 | private ExplanationHandler(OutputStream log, String optionsDescription) { |
| 600 | this.log = new PrintWriter(log); |
| 601 | this.log.println("Build options: " + optionsDescription); |
| 602 | } |
| 603 | |
| 604 | |
| 605 | @Override |
| 606 | public void handle(Event event) { |
| 607 | if (event.getKind() == EventKind.DEPCHECKER) { |
| 608 | log.println(event.getMessage()); |
| 609 | } |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Computes the result of the build. Sets the list of successful (up-to-date) |
| 615 | * targets in the request object. |
| 616 | * |
| 617 | * @param configuredTargets The configured targets whose artifacts are to be |
| 618 | * built. |
| 619 | * @param timer A timer that was started when the execution phase started. |
| 620 | */ |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 621 | private Collection<ConfiguredTarget> determineSuccessfulTargets( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 622 | Collection<ConfiguredTarget> configuredTargets, Set<ConfiguredTarget> builtTargets, |
| 623 | Stopwatch timer) { |
| 624 | // Maintain the ordering by copying builtTargets into a LinkedHashSet in the same iteration |
| 625 | // order as configuredTargets. |
| 626 | Collection<ConfiguredTarget> successfulTargets = new LinkedHashSet<>(); |
| 627 | for (ConfiguredTarget target : configuredTargets) { |
| 628 | if (builtTargets.contains(target)) { |
| 629 | successfulTargets.add(target); |
| 630 | } |
| 631 | } |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 632 | env.getEventBus().post( |
Eric Fellheimer | e9b41c6 | 2016-06-14 15:39:51 +0000 | [diff] [blame] | 633 | new ExecutionPhaseCompleteEvent(timer.stop().elapsed(MILLISECONDS))); |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 634 | return successfulTargets; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Dmitry Lomov | 56c7d55 | 2015-09-07 14:57:47 +0000 | [diff] [blame] | 637 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 638 | private ActionCache getActionCache() throws LocalEnvironmentException { |
| 639 | try { |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 640 | return env.getPersistentActionCache(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 641 | } catch (IOException e) { |
| 642 | // TODO(bazel-team): (2010) Ideally we should just remove all cache data and reinitialize |
| 643 | // caches. |
| 644 | LoggingUtil.logToRemote(Level.WARNING, "Failed to initialize action cache: " |
| 645 | + e.getMessage(), e); |
| 646 | throw new LocalEnvironmentException("couldn't create action cache: " + e.getMessage() |
| 647 | + ". If error persists, use 'blaze clean'"); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | private Builder createBuilder(BuildRequest request, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 652 | ActionCache actionCache, |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 653 | SkyframeExecutor skyframeExecutor, |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 654 | ModifiedFileSet modifiedOutputFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 655 | BuildRequest.BuildRequestOptions options = request.getBuildOptions(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 656 | boolean keepGoing = request.getViewOptions().keepGoing; |
| 657 | |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame^] | 658 | Path actionOutputRoot = env.getActionConsoleOutputDirectory(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 659 | Predicate<Action> executionFilter = CheckUpToDateFilter.fromOptions( |
| 660 | request.getOptions(ExecutionOptions.class)); |
| 661 | |
| 662 | // jobs should have been verified in BuildRequest#validateOptions(). |
| 663 | Preconditions.checkState(options.jobs >= -1); |
| 664 | int actualJobs = options.jobs == 0 ? 1 : options.jobs; // Treat 0 jobs as a single task. |
| 665 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 666 | skyframeExecutor.setActionOutputRoot(actionOutputRoot); |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 667 | ArtifactFactory artifactFactory = env.getSkyframeBuildView().getArtifactFactory(); |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 668 | return new SkyframeBuilder( |
| 669 | skyframeExecutor, |
| 670 | new ActionCacheChecker( |
| 671 | actionCache, |
| 672 | artifactFactory, |
| 673 | executionFilter, |
| 674 | ActionCacheChecker.CacheConfig.builder() |
| 675 | .setEnabled(options.useActionCache) |
| 676 | .setVerboseExplanations(options.verboseExplanations) |
| 677 | .build()), |
| 678 | keepGoing, |
| 679 | actualJobs, |
Nathan Harmata | 6010883 | 2016-03-25 08:02:42 +0000 | [diff] [blame] | 680 | request.getPackageCacheOptions().checkOutputFiles |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 681 | ? modifiedOutputFiles |
| 682 | : ModifiedFileSet.NOTHING_MODIFIED, |
| 683 | options.finalizeActions, |
| 684 | fileCache, |
| 685 | request.getBuildOptions().progressReportInterval); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 686 | } |
| 687 | |
| 688 | private void configureResourceManager(BuildRequest request) { |
| 689 | ResourceManager resourceMgr = ResourceManager.instance(); |
| 690 | ExecutionOptions options = request.getOptions(ExecutionOptions.class); |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 691 | ResourceSet resources; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 692 | if (options.availableResources != null) { |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 693 | resources = options.availableResources; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 694 | resourceMgr.setRamUtilizationPercentage(100); |
| 695 | } else { |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 696 | resources = LocalHostCapacity.getLocalHostCapacity(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 697 | resourceMgr.setRamUtilizationPercentage(options.ramUtilizationPercentage); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 698 | } |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 699 | |
| 700 | resourceMgr.setAvailableResources(ResourceSet.create( |
| 701 | resources.getMemoryMb(), |
| 702 | resources.getCpuUsage(), |
| 703 | resources.getIoUsage(), |
| 704 | request.getExecutionOptions().usingLocalTestJobs() |
| 705 | ? request.getExecutionOptions().localTestJobs : Integer.MAX_VALUE |
| 706 | )); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 707 | } |
| 708 | |
| 709 | /** |
| 710 | * Writes the cache files to disk, reporting any errors that occurred during |
| 711 | * writing. |
| 712 | */ |
| 713 | private void saveCaches(ActionCache actionCache) { |
| 714 | long actionCacheSizeInBytes = 0; |
Eric Fellheimer | e9b41c6 | 2016-06-14 15:39:51 +0000 | [diff] [blame] | 715 | long actionCacheSaveTimeInMs; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 716 | |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 717 | AutoProfiler p = AutoProfiler.profiledAndLogged("Saving action cache", ProfilerTask.INFO, log); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 718 | try { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 719 | actionCacheSizeInBytes = actionCache.save(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 720 | } catch (IOException e) { |
| 721 | getReporter().handle(Event.error("I/O error while writing action log: " + e.getMessage())); |
| 722 | } finally { |
Eric Fellheimer | e9b41c6 | 2016-06-14 15:39:51 +0000 | [diff] [blame] | 723 | actionCacheSaveTimeInMs = |
| 724 | MILLISECONDS.convert(p.completeAndGetElapsedTimeNanos(), NANOSECONDS); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 725 | } |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 726 | env.getEventBus().post(new CachesSavedEvent( |
Eric Fellheimer | e9b41c6 | 2016-06-14 15:39:51 +0000 | [diff] [blame] | 727 | actionCacheSaveTimeInMs, actionCacheSizeInBytes)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 728 | } |
| 729 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 730 | private Reporter getReporter() { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 731 | return env.getReporter(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 732 | } |
| 733 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 734 | private Path getWorkspace() { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 735 | return env.getWorkspace(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | private Path getExecRoot() { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 739 | return env.getExecRoot(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 740 | } |
| 741 | } |