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