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 | |
steinman | 13a628a | 2019-01-09 08:39:32 -0800 | [diff] [blame] | 18 | import com.google.common.annotations.VisibleForTesting; |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
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; |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 22 | import com.google.common.base.Throwables; |
Philipp Wollermann | 45bf15b | 2015-06-18 13:06:16 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableList; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | import com.google.common.collect.ImmutableMap; |
| 25 | import com.google.common.collect.ImmutableSet; |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 26 | import com.google.common.collect.ImmutableSortedSet; |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 27 | import com.google.common.collect.Iterables; |
janakr | 0dfb466 | 2020-04-20 08:37:53 -0700 | [diff] [blame] | 28 | import com.google.common.flogger.GoogleLogger; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import com.google.devtools.build.lib.actions.Action; |
| 30 | import com.google.devtools.build.lib.actions.ActionCacheChecker; |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 31 | import com.google.devtools.build.lib.actions.ActionExecutionStatusReporter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | import com.google.devtools.build.lib.actions.ActionGraph; |
ulfjack | 5b99e50 | 2017-07-20 22:22:36 +0200 | [diff] [blame] | 33 | import com.google.devtools.build.lib.actions.ActionInputPrefetcher; |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.actions.ArtifactFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 35 | import com.google.devtools.build.lib.actions.BuildFailedException; |
jcater | 85d666f | 2020-04-15 07:22:21 -0700 | [diff] [blame] | 36 | import com.google.devtools.build.lib.actions.DynamicStrategyRegistry; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.actions.Executor; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 38 | import com.google.devtools.build.lib.actions.PackageRoots; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import com.google.devtools.build.lib.actions.ResourceManager; |
Mark Schaller | dffb6ee | 2015-02-25 20:01:01 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.actions.ResourceSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | import com.google.devtools.build.lib.actions.TestExecException; |
| 42 | import com.google.devtools.build.lib.actions.cache.ActionCache; |
jmmv | ccb43ee | 2017-08-30 22:07:10 +0200 | [diff] [blame] | 43 | import com.google.devtools.build.lib.actions.cache.Protos.ActionCacheStatistics; |
ulfjack | c23bdac | 2018-06-13 03:06:16 -0700 | [diff] [blame] | 44 | import com.google.devtools.build.lib.analysis.AnalysisResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.analysis.ConfiguredTarget; |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.analysis.TopLevelArtifactContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | import com.google.devtools.build.lib.analysis.TopLevelArtifactHelper; |
jcater | c864f87 | 2020-01-28 09:01:45 -0800 | [diff] [blame] | 48 | import com.google.devtools.build.lib.analysis.WorkspaceStatusAction; |
Lukacs Berki | 7b2f2e8 | 2016-11-23 14:16:43 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.analysis.actions.SymlinkTreeActionContext; |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationValue; |
brandjon | 7b5043f | 2019-11-25 12:30:22 -0800 | [diff] [blame] | 51 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
| 52 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
schmitt | 4d6aff9 | 2020-01-13 10:49:30 -0800 | [diff] [blame] | 53 | import com.google.devtools.build.lib.analysis.test.TestActionContext; |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 54 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.ConvenienceSymlink; |
| 55 | import com.google.devtools.build.lib.buildtool.BuildRequestOptions.ConvenienceSymlinksMode; |
| 56 | import com.google.devtools.build.lib.buildtool.buildevent.ConvenienceSymlinksIdentifiedEvent; |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 57 | import com.google.devtools.build.lib.buildtool.buildevent.ExecRootPreparedEvent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionPhaseCompleteEvent; |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 59 | import com.google.devtools.build.lib.buildtool.buildevent.ExecutionProgressReceiverAvailableEvent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 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.BlazeExecutor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 67 | import com.google.devtools.build.lib.exec.CheckUpToDateFilter; |
| 68 | import com.google.devtools.build.lib.exec.ExecutionOptions; |
Ulf Adams | dba3c83 | 2016-12-21 16:50:02 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.exec.ExecutorBuilder; |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 70 | import com.google.devtools.build.lib.exec.ExecutorLifecycleListener; |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 71 | import com.google.devtools.build.lib.exec.ModuleActionContextRegistry; |
jcater | 85d666f | 2020-04-15 07:22:21 -0700 | [diff] [blame] | 72 | import com.google.devtools.build.lib.exec.RemoteLocalFallbackRegistry; |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 73 | import com.google.devtools.build.lib.exec.SpawnStrategyRegistry; |
jcater | ac4c06c | 2020-04-14 13:20:32 -0700 | [diff] [blame] | 74 | import com.google.devtools.build.lib.exec.SpawnStrategyResolver; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | import com.google.devtools.build.lib.exec.SymlinkTreeStrategy; |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 76 | import com.google.devtools.build.lib.packages.semantics.BuildLanguageOptions; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
janakr | b182a3f | 2020-04-04 08:05:48 -0700 | [diff] [blame] | 78 | import com.google.devtools.build.lib.profiler.GoogleAutoProfilerUtils; |
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; |
ulfjack | 4cf2ebd | 2018-06-11 06:00:36 -0700 | [diff] [blame] | 82 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
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; |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 86 | import com.google.devtools.build.lib.server.FailureDetails; |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 87 | import com.google.devtools.build.lib.server.FailureDetails.Execution; |
| 88 | import com.google.devtools.build.lib.server.FailureDetails.Execution.Code; |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 89 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
leba | b52a190 | 2021-09-23 01:35:13 -0700 | [diff] [blame] | 90 | import com.google.devtools.build.lib.skyframe.AspectKeyCreator.AspectKey; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 91 | import com.google.devtools.build.lib.skyframe.Builder; |
cpeyser | 8e9b28f | 2018-06-04 08:05:13 -0700 | [diff] [blame] | 92 | import com.google.devtools.build.lib.skyframe.ConfiguredTargetKey; |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 93 | import com.google.devtools.build.lib.skyframe.PackageRootsNoSymlinkCreation; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 95 | import com.google.devtools.build.lib.util.AbruptExitException; |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 96 | import com.google.devtools.build.lib.util.DetailedExitCode; |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 97 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
shahan | e35e8cf | 2018-06-18 08:14:01 -0700 | [diff] [blame] | 98 | import com.google.devtools.build.lib.vfs.OutputService; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | import com.google.devtools.build.lib.vfs.Path; |
| 100 | import com.google.devtools.build.lib.vfs.PathFragment; |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 101 | import com.google.devtools.build.lib.vfs.Root; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | import java.io.IOException; |
| 103 | import java.io.OutputStream; |
lberki | 0d8d4cf | 2017-09-05 16:01:44 +0200 | [diff] [blame] | 104 | import java.io.OutputStreamWriter; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 105 | import java.io.PrintWriter; |
lberki | 0d8d4cf | 2017-09-05 16:01:44 +0200 | [diff] [blame] | 106 | import java.nio.charset.StandardCharsets; |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 107 | import java.time.Duration; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | import java.util.Collection; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 109 | import java.util.HashSet; |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 110 | import java.util.List; |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 111 | import java.util.Objects; |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 112 | import java.util.Optional; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 113 | import java.util.Set; |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 114 | import java.util.UUID; |
jmmv | ccb43ee | 2017-08-30 22:07:10 +0200 | [diff] [blame] | 115 | import java.util.concurrent.TimeUnit; |
brandjon | 7b5043f | 2019-11-25 12:30:22 -0800 | [diff] [blame] | 116 | import javax.annotation.Nullable; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 118 | /** |
| 119 | * This class manages the execution phase. The entry point is {@link #executeBuild}. |
| 120 | * |
| 121 | * <p>This is only intended for use by {@link BuildTool}. |
| 122 | * |
mstaib | d9b141d | 2017-09-14 20:47:31 +0200 | [diff] [blame] | 123 | * <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] | 124 | * PackageCache. |
| 125 | * |
| 126 | * @see BuildTool |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 127 | * @see com.google.devtools.build.lib.analysis.BuildView |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 128 | */ |
| 129 | public class ExecutionTool { |
janakr | 0dfb466 | 2020-04-20 08:37:53 -0700 | [diff] [blame] | 130 | private static final GoogleLogger logger = GoogleLogger.forEnclosingClass(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 132 | private final CommandEnvironment env; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 133 | private final BlazeRuntime runtime; |
| 134 | private final BuildRequest request; |
| 135 | private BlazeExecutor executor; |
Ulf Adams | ccd5c0a | 2017-01-13 15:46:51 +0000 | [diff] [blame] | 136 | private final ActionInputPrefetcher prefetcher; |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 137 | private final ImmutableSet<ExecutorLifecycleListener> executorLifecycleListeners; |
jcater | 40b6074 | 2020-05-15 07:04:00 -0700 | [diff] [blame] | 138 | private final SpawnStrategyRegistry spawnStrategyRegistry; |
| 139 | private final ModuleActionContextRegistry actionContextRegistry; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | |
janakr | ca6d7ac | 2020-08-18 07:42:05 -0700 | [diff] [blame] | 141 | ExecutionTool(CommandEnvironment env, BuildRequest request) |
| 142 | throws AbruptExitException, InterruptedException { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 143 | this.env = env; |
| 144 | this.runtime = env.getRuntime(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 145 | this.request = request; |
| 146 | |
felly | 472320c | 2018-10-29 14:27:00 -0700 | [diff] [blame] | 147 | try { |
| 148 | env.getExecRoot().createDirectoryAndParents(); |
| 149 | } catch (IOException e) { |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 150 | throw createExitException("Execroot creation failed", Code.EXECROOT_CREATION_FAILURE, e); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 151 | } |
Philipp Wollermann | 590ea39 | 2015-08-25 13:57:33 +0000 | [diff] [blame] | 152 | |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 153 | ExecutorBuilder executorBuilder = new ExecutorBuilder(); |
| 154 | ModuleActionContextRegistry.Builder actionContextRegistryBuilder = |
jcater | 283f340 | 2020-06-09 10:59:52 -0700 | [diff] [blame] | 155 | ModuleActionContextRegistry.builder(); |
| 156 | SpawnStrategyRegistry.Builder spawnStrategyRegistryBuilder = SpawnStrategyRegistry.builder(); |
jcater | 85d666f | 2020-04-15 07:22:21 -0700 | [diff] [blame] | 157 | actionContextRegistryBuilder.register(SpawnStrategyResolver.class, new SpawnStrategyResolver()); |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 158 | |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 159 | for (BlazeModule module : runtime.getBlazeModules()) { |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 160 | try (SilentCloseable ignored = Profiler.instance().profile(module + ".executorInit")) { |
| 161 | module.executorInit(env, request, executorBuilder); |
| 162 | } |
| 163 | |
| 164 | try (SilentCloseable ignored = |
| 165 | Profiler.instance().profile(module + ".registerActionContexts")) { |
| 166 | module.registerActionContexts(actionContextRegistryBuilder, env, request); |
| 167 | } |
| 168 | |
| 169 | try (SilentCloseable ignored = |
| 170 | Profiler.instance().profile(module + ".registerSpawnStrategies")) { |
| 171 | module.registerSpawnStrategies(spawnStrategyRegistryBuilder, env); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 172 | } |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 173 | } |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 174 | actionContextRegistryBuilder.register( |
jcater | c864f87 | 2020-01-28 09:01:45 -0800 | [diff] [blame] | 175 | SymlinkTreeActionContext.class, |
| 176 | new SymlinkTreeStrategy(env.getOutputService(), env.getBlazeWorkspace().getBinTools())); |
| 177 | // TODO(philwo) - the ExecutionTool should not add arbitrary dependencies on its own, instead |
| 178 | // these dependencies should be added to the ActionContextConsumer of the module that actually |
| 179 | // depends on them. |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 180 | actionContextRegistryBuilder |
| 181 | .restrictTo(WorkspaceStatusAction.Context.class, "") |
| 182 | .restrictTo(SymlinkTreeActionContext.class, ""); |
schmitt | 4d6aff9 | 2020-01-13 10:49:30 -0800 | [diff] [blame] | 183 | |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 184 | this.prefetcher = executorBuilder.getActionInputPrefetcher(); |
| 185 | this.executorLifecycleListeners = executorBuilder.getExecutorLifecycleListeners(); |
jcater | c864f87 | 2020-01-28 09:01:45 -0800 | [diff] [blame] | 186 | |
| 187 | // There are many different SpawnActions, and we want to control the action context they use |
| 188 | // independently from each other, for example, to run genrules locally and Java compile action |
| 189 | // in prod. Thus, for SpawnActions, we decide the action context to use not only based on the |
| 190 | // context class, but also the mnemonic of the action. |
| 191 | ExecutionOptions options = request.getOptions(ExecutionOptions.class); |
| 192 | // TODO(jmmv): This should live in some testing-related Blaze module, not here. |
jcater | bf144b9 | 2020-03-27 12:04:28 -0700 | [diff] [blame] | 193 | actionContextRegistryBuilder.restrictTo(TestActionContext.class, options.testStrategy); |
| 194 | |
jcater | 85d666f | 2020-04-15 07:22:21 -0700 | [diff] [blame] | 195 | SpawnStrategyRegistry spawnStrategyRegistry = spawnStrategyRegistryBuilder.build(); |
| 196 | actionContextRegistryBuilder.register(SpawnStrategyRegistry.class, spawnStrategyRegistry); |
| 197 | actionContextRegistryBuilder.register(DynamicStrategyRegistry.class, spawnStrategyRegistry); |
| 198 | actionContextRegistryBuilder.register(RemoteLocalFallbackRegistry.class, spawnStrategyRegistry); |
| 199 | |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 200 | this.actionContextRegistry = actionContextRegistryBuilder.build(); |
jcater | 40b6074 | 2020-05-15 07:04:00 -0700 | [diff] [blame] | 201 | this.spawnStrategyRegistry = spawnStrategyRegistry; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Googler | 7319c8a | 2020-05-11 08:52:56 -0700 | [diff] [blame] | 204 | Executor getExecutor() throws AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 205 | if (executor == null) { |
| 206 | executor = createExecutor(); |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 207 | for (ExecutorLifecycleListener executorLifecycleListener : executorLifecycleListeners) { |
| 208 | executorLifecycleListener.executorCreated(); |
| 209 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 210 | } |
| 211 | return executor; |
| 212 | } |
| 213 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 214 | /** Creates an executor for the current set of blaze runtime, execution options, and request. */ |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 215 | private BlazeExecutor createExecutor() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 216 | return new BlazeExecutor( |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 217 | runtime.getFileSystem(), |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 218 | env.getExecRoot(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 219 | getReporter(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 220 | runtime.getClock(), |
ajurkowski | c040946 | 2020-12-22 19:22:11 -0800 | [diff] [blame] | 221 | runtime.getBugReporter(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 222 | request, |
jcater | 40b6074 | 2020-05-15 07:04:00 -0700 | [diff] [blame] | 223 | actionContextRegistry, |
| 224 | spawnStrategyRegistry); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 225 | } |
| 226 | |
Googler | 7319c8a | 2020-05-11 08:52:56 -0700 | [diff] [blame] | 227 | void init() throws AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 228 | getExecutor(); |
| 229 | } |
| 230 | |
| 231 | void shutdown() { |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 232 | for (ExecutorLifecycleListener executorLifecycleListener : executorLifecycleListeners) { |
| 233 | executorLifecycleListener.executionPhaseEnding(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | |
Googler | 2af2aa3 | 2020-01-30 06:45:55 -0800 | [diff] [blame] | 237 | TestActionContext getTestActionContext() { |
jcater | 40b6074 | 2020-05-15 07:04:00 -0700 | [diff] [blame] | 238 | return actionContextRegistry.getContext(TestActionContext.class); |
Googler | 2af2aa3 | 2020-01-30 06:45:55 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 241 | /** |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 242 | * Sets up for execution. |
| 243 | * |
| 244 | * <p>b/199053098: This method concentrates the setup steps for execution, which were previously |
| 245 | * scattered over several classes. We need this in order to merge analysis & execution phases. |
| 246 | * TODO(b/199053098): Minimize code duplication with the main code path. |
| 247 | */ |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 248 | public void prepareForExecution( |
| 249 | UUID buildId, Set<ConfiguredTargetKey> builtTargets, Set<AspectKey> builtAspects) |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 250 | throws AbruptExitException, BuildFailedException, InterruptedException { |
| 251 | init(); |
| 252 | |
| 253 | SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor(); |
| 254 | // TODO(b/199053098): Support symlink forest. |
| 255 | List<Root> pkgPathEntries = env.getPackageLocator().getPathEntries(); |
| 256 | Preconditions.checkState( |
| 257 | pkgPathEntries.size() == 1, |
| 258 | "--experimental_merged_skyframe_analysis_execution requires a single package path entry." |
| 259 | + " Found a list of size: %s", |
| 260 | pkgPathEntries.size()); |
| 261 | Root singleSourceRoot = Iterables.getOnlyElement(pkgPathEntries); |
| 262 | PackageRoots noSymlinkPackageRoots = new PackageRootsNoSymlinkCreation(singleSourceRoot); |
| 263 | env.getEventBus().post(new ExecRootPreparedEvent(noSymlinkPackageRoots.getPackageRootsMap())); |
| 264 | env.getSkyframeBuildView() |
| 265 | .getArtifactFactory() |
| 266 | .setPackageRoots(noSymlinkPackageRoots.getPackageRootLookup()); |
| 267 | |
| 268 | OutputService outputService = env.getOutputService(); |
| 269 | ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED; |
| 270 | if (outputService != null) { |
| 271 | try (SilentCloseable c = Profiler.instance().profile("outputService.startBuild")) { |
| 272 | modifiedOutputFiles = |
| 273 | outputService.startBuild( |
| 274 | env.getReporter(), buildId, request.getBuildOptions().finalizeActions); |
| 275 | } |
| 276 | } else { |
| 277 | // TODO(bazel-team): this could be just another OutputService |
| 278 | try (SilentCloseable c = Profiler.instance().profile("startLocalOutputBuild")) { |
| 279 | startLocalOutputBuild(); |
| 280 | } |
| 281 | } |
| 282 | if (outputService == null || !outputService.actionFileSystemType().inMemoryFileSystem()) { |
| 283 | // Must be created after the output path is created above. |
| 284 | createActionLogDirectory(); |
| 285 | } |
| 286 | |
| 287 | ActionCache actionCache = getActionCache(); |
| 288 | actionCache.resetStatistics(); |
| 289 | SkyframeBuilder skyframeBuilder; |
| 290 | try (SilentCloseable c = Profiler.instance().profile("createBuilder")) { |
| 291 | skyframeBuilder = |
| 292 | (SkyframeBuilder) |
| 293 | createBuilder(request, actionCache, skyframeExecutor, modifiedOutputFiles); |
| 294 | } |
| 295 | try (SilentCloseable c = Profiler.instance().profile("configureActionExecutor")) { |
| 296 | skyframeExecutor.configureActionExecutor( |
| 297 | skyframeBuilder.getFileCache(), skyframeBuilder.getActionInputPrefetcher()); |
| 298 | } |
| 299 | // TODO(b/199053098): Setup progress reporting objects in SkyframeActionExecutor. |
| 300 | try (SilentCloseable c = |
| 301 | Profiler.instance().profile("prepareSkyframeActionExecutorForExecution")) { |
| 302 | skyframeExecutor.prepareSkyframeActionExecutorForExecution( |
| 303 | env.getReporter(), |
| 304 | executor, |
| 305 | request, |
| 306 | skyframeBuilder.getActionCacheChecker(), |
| 307 | skyframeBuilder.getTopDownActionCache()); |
| 308 | } |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 309 | |
| 310 | // Note that executionProgressReceiver accesses builtTargets concurrently (after wrapping in a |
| 311 | // synchronized collection), so unsynchronized access to this variable is unsafe while it runs. |
| 312 | // TODO(leba): count test actions |
| 313 | ExecutionProgressReceiver executionProgressReceiver = |
| 314 | new ExecutionProgressReceiver( |
| 315 | Preconditions.checkNotNull(builtTargets), Preconditions.checkNotNull(builtAspects), 0); |
| 316 | skyframeExecutor |
| 317 | .getEventBus() |
| 318 | .post(new ExecutionProgressReceiverAvailableEvent(executionProgressReceiver)); |
| 319 | |
| 320 | ActionExecutionStatusReporter statusReporter = |
| 321 | ActionExecutionStatusReporter.create(env.getReporter(), skyframeExecutor.getEventBus()); |
| 322 | // TODO(leba): Add watchdog support. |
| 323 | skyframeExecutor.setActionExecutionProgressReportingObjects( |
| 324 | executionProgressReceiver, executionProgressReceiver, statusReporter); |
| 325 | skyframeExecutor.setExecutionProgressReceiver(executionProgressReceiver); |
| 326 | |
leba | cac2246 | 2021-09-22 01:45:34 -0700 | [diff] [blame] | 327 | for (ExecutorLifecycleListener executorLifecycleListener : executorLifecycleListeners) { |
| 328 | try (SilentCloseable c = |
| 329 | Profiler.instance().profile(executorLifecycleListener + ".executionPhaseStarting")) { |
| 330 | executorLifecycleListener.executionPhaseStarting(null, () -> null); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | try (SilentCloseable c = Profiler.instance().profile("configureResourceManager")) { |
| 335 | configureResourceManager(env.getLocalResourceManager(), request); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 340 | * Performs the execution phase (phase 3) of the build, in which the Builder is applied to the |
| 341 | * action graph to bring the targets up to date. (This function will return prior to |
| 342 | * execution-proper if --nobuild was specified.) |
| 343 | * |
Eric Fellheimer | 2db6a74 | 2015-04-28 21:38:43 +0000 | [diff] [blame] | 344 | * @param buildId UUID of the build id |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 345 | * @param analysisResult the analysis phase output |
| 346 | * @param buildResult the mutable build result |
Lukacs Berki | 3ea4d44 | 2016-01-21 15:15:30 +0000 | [diff] [blame] | 347 | * @param packageRoots package roots collected from loading phase and BuildConfigurationCollection |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 348 | * creation. May be empty if {@link |
| 349 | * SkyframeExecutor#getForcedSingleSourceRootIfNoExecrootSymlinkCreation} is false. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 350 | */ |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 351 | void executeBuild( |
| 352 | UUID buildId, |
| 353 | AnalysisResult analysisResult, |
Laurent Le Brun | f3cf98f | 2016-06-17 13:36:24 +0000 | [diff] [blame] | 354 | BuildResult buildResult, |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 355 | PackageRoots packageRoots, |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 356 | TopLevelArtifactContext topLevelArtifactContext) |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 357 | throws BuildFailedException, InterruptedException, TestExecException, AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 358 | Stopwatch timer = Stopwatch.createStarted(); |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 359 | prepare(packageRoots, analysisResult.getNonSymlinkedDirectoriesUnderExecRoot()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 360 | |
| 361 | ActionGraph actionGraph = analysisResult.getActionGraph(); |
| 362 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 363 | OutputService outputService = env.getOutputService(); |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 364 | ModifiedFileSet modifiedOutputFiles = ModifiedFileSet.EVERYTHING_MODIFIED; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 365 | if (outputService != null) { |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 366 | try (SilentCloseable c = Profiler.instance().profile("outputService.startBuild")) { |
| 367 | modifiedOutputFiles = |
| 368 | outputService.startBuild( |
| 369 | env.getReporter(), buildId, request.getBuildOptions().finalizeActions); |
| 370 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 371 | } else { |
Kristina Chodorow | e423fdb | 2016-09-15 14:08:08 +0000 | [diff] [blame] | 372 | // TODO(bazel-team): this could be just another OutputService |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 373 | try (SilentCloseable c = Profiler.instance().profile("startLocalOutputBuild")) { |
| 374 | startLocalOutputBuild(); |
| 375 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 376 | } |
| 377 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 378 | if (outputService == null || !outputService.actionFileSystemType().inMemoryFileSystem()) { |
| 379 | // Must be created after the output path is created above. |
| 380 | createActionLogDirectory(); |
| 381 | } |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame] | 382 | |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 383 | handleConvenienceSymlinks(analysisResult); |
Lukacs Berki | 85c63c4 | 2016-01-22 09:25:20 +0000 | [diff] [blame] | 384 | |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 385 | BuildRequestOptions options = request.getBuildOptions(); |
| 386 | ActionCache actionCache = null; |
| 387 | if (options.useActionCache) { |
| 388 | actionCache = getActionCache(); |
| 389 | actionCache.resetStatistics(); |
| 390 | } |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 391 | SkyframeExecutor skyframeExecutor = env.getSkyframeExecutor(); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 392 | Builder builder; |
| 393 | try (SilentCloseable c = Profiler.instance().profile("createBuilder")) { |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 394 | builder = createBuilder(request, actionCache, skyframeExecutor, modifiedOutputFiles); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 395 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 396 | |
| 397 | // |
| 398 | // Execution proper. All statements below are logically nested in |
| 399 | // begin/end pairs. No early returns or exceptions please! |
| 400 | // |
| 401 | |
| 402 | Collection<ConfiguredTarget> configuredTargets = buildResult.getActualTargets(); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 403 | try (SilentCloseable c = Profiler.instance().profile("ExecutionStartingEvent")) { |
| 404 | env.getEventBus().post(new ExecutionStartingEvent(configuredTargets)); |
| 405 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 406 | |
| 407 | getReporter().handle(Event.progress("Building...")); |
| 408 | |
| 409 | // Conditionally record dependency-checker log: |
| 410 | ExplanationHandler explanationHandler = |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 411 | installExplanationHandler( |
| 412 | request.getBuildOptions().explanationPath, request.getOptionsDescription()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 413 | |
cpeyser | 8e9b28f | 2018-06-04 08:05:13 -0700 | [diff] [blame] | 414 | Set<ConfiguredTargetKey> builtTargets = new HashSet<>(); |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 415 | Set<AspectKey> builtAspects = new HashSet<>(); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 416 | |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 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() |
brandjon | 79712cb | 2019-12-04 10:01:17 -0800 | [diff] [blame] | 421 | .printErrLn(runtime.getProductName() + ": Entering directory `" + getExecRoot() + "/'"); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 422 | } |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 423 | |
| 424 | Throwable catastrophe = null; |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 425 | boolean buildCompleted = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 426 | try { |
Googler | 594c32d | 2019-07-17 17:55:18 -0700 | [diff] [blame] | 427 | if (request.getViewOptions().discardAnalysisCache |
| 428 | || !skyframeExecutor.tracksStateForIncrementality()) { |
| 429 | // Free memory by removing cache entries that aren't going to be needed. |
| 430 | try (SilentCloseable c = Profiler.instance().profile("clearAnalysisCache")) { |
| 431 | env.getSkyframeBuildView() |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 432 | .clearAnalysisCache( |
| 433 | analysisResult.getTargetsToBuild(), analysisResult.getAspectsMap().keySet()); |
Googler | 594c32d | 2019-07-17 17:55:18 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 437 | for (ExecutorLifecycleListener executorLifecycleListener : executorLifecycleListeners) { |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 438 | try (SilentCloseable c = |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 439 | Profiler.instance().profile(executorLifecycleListener + ".executionPhaseStarting")) { |
| 440 | executorLifecycleListener.executionPhaseStarting( |
janakr | ccb7456 | 2018-07-30 10:56:57 -0700 | [diff] [blame] | 441 | actionGraph, |
Googler | 184b454 | 2019-07-19 19:34:19 -0700 | [diff] [blame] | 442 | // If this supplier is ever consumed by more than one ActionContextProvider, it can be |
| 443 | // pulled out of the loop and made a memoizing supplier. |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 444 | () -> TopLevelArtifactHelper.findAllTopLevelArtifacts(analysisResult)); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 445 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 446 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 447 | skyframeExecutor.drainChangedFiles(); |
| 448 | |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 449 | try (SilentCloseable c = Profiler.instance().profile("configureResourceManager")) { |
ulfjack | 985a34ad | 2019-08-09 01:51:15 -0700 | [diff] [blame] | 450 | configureResourceManager(env.getLocalResourceManager(), request); |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 451 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 452 | |
| 453 | Profiler.instance().markPhase(ProfilePhase.EXECUTE); |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 454 | boolean shouldTrustRemoteArtifacts = |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 455 | env.getOutputService() != null && env.getOutputService().shouldTrustRemoteArtifacts(); |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 456 | builder.buildArtifacts( |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 457 | env.getReporter(), |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 458 | analysisResult.getArtifactsToBuild(), |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 459 | analysisResult.getParallelTests(), |
| 460 | analysisResult.getExclusiveTests(), |
| 461 | analysisResult.getTargetsToBuild(), |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 462 | analysisResult.getTargetsToSkip(), |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 463 | analysisResult.getAspectsMap().keySet(), |
Dmitry Lomov | e2033b1 | 2015-08-19 16:57:49 +0000 | [diff] [blame] | 464 | executor, |
| 465 | builtTargets, |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 466 | builtAspects, |
tomlu | 2661be8 | 2018-08-30 08:30:01 -0700 | [diff] [blame] | 467 | request, |
Googler | bfd4e24 | 2016-07-15 22:23:37 +0000 | [diff] [blame] | 468 | env.getBlazeWorkspace().getLastExecutionTimeRange(), |
steinman | 39c00d2 | 2020-03-20 15:23:10 -0700 | [diff] [blame] | 469 | topLevelArtifactContext, |
| 470 | shouldTrustRemoteArtifacts); |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 471 | buildCompleted = true; |
| 472 | } catch (BuildFailedException | TestExecException e) { |
| 473 | buildCompleted = true; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 474 | throw e; |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 475 | } catch (Error | RuntimeException e) { |
| 476 | catastrophe = e; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 477 | } finally { |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 478 | // These may flush logs, which may help if there is a catastrophic failure. |
schmitt | be241a1 | 2020-01-16 13:30:55 -0800 | [diff] [blame] | 479 | for (ExecutorLifecycleListener executorLifecycleListener : executorLifecycleListeners) { |
| 480 | executorLifecycleListener.executionPhaseEnding(); |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | // Handlers process these events and others (e.g. CommandCompleteEvent), even in the event of |
| 484 | // a catastrophic failure. Posting these is consistent with other behavior. |
janakr | ac58ca8 | 2019-02-14 09:34:20 -0800 | [diff] [blame] | 485 | env.getEventBus().post(skyframeExecutor.createExecutionFinishedEvent()); |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 486 | |
| 487 | env.getEventBus() |
| 488 | .post(new ExecutionPhaseCompleteEvent(timer.stop().elapsed(TimeUnit.MILLISECONDS))); |
| 489 | |
| 490 | if (catastrophe != null) { |
| 491 | Throwables.throwIfUnchecked(catastrophe); |
| 492 | } |
| 493 | // NOTE: No finalization activities below will run in the event of a catastrophic error! |
| 494 | |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 495 | env.recordLastExecutionTime(); |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 496 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 497 | if (request.isRunningInEmacs()) { |
Klaus Aehlig | ad87c13 | 2017-08-14 12:20:39 +0200 | [diff] [blame] | 498 | request |
| 499 | .getOutErr() |
brandjon | 79712cb | 2019-12-04 10:01:17 -0800 | [diff] [blame] | 500 | .printErrLn(runtime.getProductName() + ": Leaving directory `" + getExecRoot() + "/'"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 501 | } |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 502 | if (buildCompleted) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 503 | getReporter().handle(Event.progress("Building complete.")); |
| 504 | } |
| 505 | |
Janak Ramakrishnan | a5d8d09 | 2015-12-08 18:32:47 +0000 | [diff] [blame] | 506 | if (buildCompleted) { |
jmmv | 59787f1 | 2017-08-28 21:41:21 +0200 | [diff] [blame] | 507 | saveActionCache(actionCache); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 508 | } |
| 509 | |
ulfjack | 1fbe72e | 2018-06-14 04:46:27 -0700 | [diff] [blame] | 510 | try (SilentCloseable c = Profiler.instance().profile("Show results")) { |
Ulf Adams | 7a3fe52 | 2015-12-04 10:48:56 +0000 | [diff] [blame] | 511 | buildResult.setSuccessfulTargets( |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 512 | determineSuccessfulTargets(configuredTargets, builtTargets)); |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 513 | buildResult.setSuccessfulAspects( |
| 514 | determineSuccessfulAspects(analysisResult.getAspectsMap().keySet(), builtAspects)); |
gregce | 45ae096 | 2017-07-22 00:11:13 +0200 | [diff] [blame] | 515 | buildResult.setSkippedTargets(analysisResult.getTargetsToSkip()); |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 516 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
mschaller | 4037908 | 2019-02-01 14:14:47 -0800 | [diff] [blame] | 517 | buildResultPrinter.showBuildResult( |
| 518 | request, |
| 519 | buildResult, |
| 520 | configuredTargets, |
| 521 | analysisResult.getTargetsToSkip(), |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 522 | analysisResult.getAspectsMap()); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 523 | } |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 524 | |
ulfjack | 1fbe72e | 2018-06-14 04:46:27 -0700 | [diff] [blame] | 525 | try (SilentCloseable c = Profiler.instance().profile("Show artifacts")) { |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 526 | if (request.getBuildOptions().showArtifacts) { |
| 527 | BuildResultPrinter buildResultPrinter = new BuildResultPrinter(env); |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 528 | buildResultPrinter.showArtifacts( |
| 529 | request, configuredTargets, analysisResult.getAspectsMap().values()); |
Googler | 3ca7b78 | 2015-10-12 15:44:09 +0000 | [diff] [blame] | 530 | } |
| 531 | } |
| 532 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 533 | if (explanationHandler != null) { |
| 534 | uninstallExplanationHandler(explanationHandler); |
laszlocsomor | 482235b | 2018-11-22 08:15:15 -0800 | [diff] [blame] | 535 | try { |
| 536 | explanationHandler.close(); |
| 537 | } catch (IOException _ignored) { |
| 538 | // Ignored |
| 539 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 540 | } |
| 541 | // Finalize output service last, so that if we do throw an exception, we know all the other |
| 542 | // code has already run. |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 543 | if (env.getOutputService() != null) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 544 | boolean isBuildSuccessful = |
| 545 | buildResult.getSuccessfulTargets().size() == configuredTargets.size(); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 546 | env.getOutputService().finalizeBuild(isBuildSuccessful); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 551 | private void prepare( |
| 552 | PackageRoots packageRoots, ImmutableSortedSet<String> nonSymlinkedDirectoriesUnderExecRoot) |
| 553 | throws AbruptExitException, InterruptedException { |
tomlu | ee6a686 | 2018-01-17 14:36:26 -0800 | [diff] [blame] | 554 | Optional<ImmutableMap<PackageIdentifier, Root>> packageRootMap = |
janakr | ae32398 | 2017-09-29 21:11:53 +0200 | [diff] [blame] | 555 | packageRoots.getPackageRootsMap(); |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 556 | if (packageRootMap.isPresent()) { |
| 557 | // Prepare for build. |
| 558 | Profiler.instance().markPhase(ProfilePhase.PREPARE); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 559 | |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 560 | // Plant the symlink forest. |
| 561 | try (SilentCloseable c = Profiler.instance().profile("plantSymlinkForest")) { |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 562 | SymlinkForest symlinkForest = |
| 563 | new SymlinkForest( |
| 564 | packageRootMap.get(), |
| 565 | getExecRoot(), |
| 566 | runtime.getProductName(), |
Jingwen Chen | 6360557 | 2020-02-13 13:47:00 -0800 | [diff] [blame] | 567 | nonSymlinkedDirectoriesUnderExecRoot, |
adonovan | 240bdea | 2020-09-03 15:24:12 -0700 | [diff] [blame] | 568 | request.getOptions(BuildLanguageOptions.class).experimentalSiblingRepositoryLayout); |
ichern | 121933e | 2020-01-27 02:08:19 -0800 | [diff] [blame] | 569 | symlinkForest.plantSymlinkForest(); |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 570 | } catch (IOException e) { |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 571 | throw new AbruptExitException( |
| 572 | DetailedExitCode.of( |
| 573 | FailureDetail.newBuilder() |
| 574 | .setMessage("Source forest creation failed") |
| 575 | .setSymlinkForest( |
| 576 | FailureDetails.SymlinkForest.newBuilder() |
| 577 | .setCode(FailureDetails.SymlinkForest.Code.CREATION_FAILED)) |
| 578 | .build()), |
| 579 | e); |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 580 | } |
Kristina Chodorow | 52bc57c | 2016-06-20 14:27:56 +0000 | [diff] [blame] | 581 | } |
arostovtsev | 20b056a | 2019-08-30 15:00:31 -0700 | [diff] [blame] | 582 | env.getEventBus().post(new ExecRootPreparedEvent(packageRootMap)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 583 | } |
| 584 | |
arostovtsev | 5b8c8c3 | 2020-06-23 11:31:06 -0700 | [diff] [blame] | 585 | private static void logDeleteTreeFailure( |
| 586 | Path directory, String description, IOException deleteTreeFailure) { |
| 587 | logger.atWarning().withCause(deleteTreeFailure).log( |
| 588 | "Failed to delete %s '%s'", description, directory); |
| 589 | if (directory.exists()) { |
| 590 | try { |
| 591 | Collection<Path> entries = directory.getDirectoryEntries(); |
| 592 | StringBuilder directoryDetails = |
| 593 | new StringBuilder("'") |
| 594 | .append(directory) |
| 595 | .append("' contains ") |
| 596 | .append(entries.size()) |
| 597 | .append(" entries:"); |
| 598 | for (Path entry : entries) { |
| 599 | directoryDetails.append(" '").append(entry.getBaseName()).append("'"); |
| 600 | } |
| 601 | logger.atWarning().log(directoryDetails.toString()); |
| 602 | } catch (IOException e) { |
| 603 | logger.atWarning().withCause(e).log("'%s' exists but could not be read", directory); |
| 604 | } |
| 605 | } else { |
| 606 | logger.atWarning().log("'%s' does not exist", directory); |
| 607 | } |
| 608 | } |
| 609 | |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 610 | private void createActionLogDirectory() throws AbruptExitException { |
jmmv | 88e22cd | 2019-09-15 09:59:49 -0700 | [diff] [blame] | 611 | Path directory = env.getActionTempsDirectory(); |
arostovtsev | 702a1c6 | 2020-01-15 11:59:50 -0800 | [diff] [blame] | 612 | if (directory.exists()) { |
| 613 | try { |
jmmv | 5cc1f65 | 2019-03-20 09:34:08 -0700 | [diff] [blame] | 614 | directory.deleteTree(); |
arostovtsev | 702a1c6 | 2020-01-15 11:59:50 -0800 | [diff] [blame] | 615 | } catch (IOException e) { |
arostovtsev | 5b8c8c3 | 2020-06-23 11:31:06 -0700 | [diff] [blame] | 616 | // TODO(b/140567980): Remove when we determine the cause of occasional deleteTree() failure. |
| 617 | logDeleteTreeFailure(directory, "action output directory", e); |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 618 | throw createExitException( |
| 619 | "Couldn't delete action output directory", |
mschaller | 05efa28 | 2020-06-24 12:54:16 -0700 | [diff] [blame] | 620 | Code.TEMP_ACTION_OUTPUT_DIRECTORY_DELETION_FAILURE, |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 621 | e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 622 | } |
arostovtsev | 702a1c6 | 2020-01-15 11:59:50 -0800 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | try { |
ulfjack | f20b33c | 2020-01-30 07:36:30 -0800 | [diff] [blame] | 626 | directory.createDirectoryAndParents(); |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 627 | } catch (IOException e) { |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 628 | throw createExitException( |
| 629 | "Couldn't create action output directory", |
mschaller | 05efa28 | 2020-06-24 12:54:16 -0700 | [diff] [blame] | 630 | Code.TEMP_ACTION_OUTPUT_DIRECTORY_CREATION_FAILURE, |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 631 | e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 632 | } |
ulfjack | f20b33c | 2020-01-30 07:36:30 -0800 | [diff] [blame] | 633 | |
| 634 | try { |
| 635 | env.getPersistentActionOutsDirectory().createDirectoryAndParents(); |
| 636 | } catch (IOException e) { |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 637 | throw createExitException( |
| 638 | "Couldn't create persistent action output directory", |
| 639 | Code.PERSISTENT_ACTION_OUTPUT_DIRECTORY_CREATION_FAILURE, |
| 640 | e); |
ulfjack | f20b33c | 2020-01-30 07:36:30 -0800 | [diff] [blame] | 641 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 642 | } |
| 643 | |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 644 | /** |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 645 | * Obtains the {@link BuildConfigurationValue} for a given {@link BuildOptions} for the purpose of |
brandjon | 7b5043f | 2019-11-25 12:30:22 -0800 | [diff] [blame] | 646 | * symlink creation. |
| 647 | * |
| 648 | * <p>In the event of a {@link InvalidConfigurationException}, a warning is emitted and null is |
| 649 | * returned. |
| 650 | */ |
| 651 | @Nullable |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 652 | private static BuildConfigurationValue getConfiguration( |
brandjon | 7b5043f | 2019-11-25 12:30:22 -0800 | [diff] [blame] | 653 | SkyframeExecutor executor, Reporter reporter, BuildOptions options) { |
| 654 | try { |
| 655 | return executor.getConfiguration(reporter, options, /*keepGoing=*/ false); |
| 656 | } catch (InvalidConfigurationException e) { |
| 657 | reporter.handle( |
| 658 | Event.warn( |
| 659 | "Couldn't get configuration for convenience symlink creation: " + e.getMessage())); |
| 660 | return null; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | /** |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 665 | * Handles what action to perform on the convenience symlinks. If the the mode is {@link |
jcater | 40b6074 | 2020-05-15 07:04:00 -0700 | [diff] [blame] | 666 | * ConvenienceSymlinksMode#IGNORE}, then skip any creating or cleaning of convenience symlinks. |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 667 | * Otherwise, manage the convenience symlinks and then post a {@link |
| 668 | * ConvenienceSymlinksIdentifiedEvent} build event. |
| 669 | */ |
| 670 | private void handleConvenienceSymlinks(AnalysisResult analysisResult) { |
| 671 | ImmutableList<ConvenienceSymlink> convenienceSymlinks = ImmutableList.of(); |
| 672 | if (request.getBuildOptions().experimentalConvenienceSymlinks |
| 673 | != ConvenienceSymlinksMode.IGNORE) { |
| 674 | convenienceSymlinks = createConvenienceSymlinks(request.getBuildOptions(), analysisResult); |
| 675 | } |
| 676 | if (request.getBuildOptions().experimentalConvenienceSymlinksBepEvent) { |
| 677 | env.getEventBus().post(new ConvenienceSymlinksIdentifiedEvent(convenienceSymlinks)); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | /** |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 682 | * Creates convenience symlinks based on the target configurations. |
| 683 | * |
| 684 | * <p>Exactly what target configurations we consider depends on the value of {@code |
| 685 | * --use_top_level_targets_for_symlinks}. If this flag is false, we use the top-level target |
| 686 | * configuration as represented by the command line prior to processing any target. If the flag is |
| 687 | * true, we instead use the configurations OF the top-level targets -- meaning that we account for |
| 688 | * the effects of any rule transitions these targets may have. |
| 689 | * |
| 690 | * <p>For each type of convenience symlink, if all the considered configurations agree on what |
| 691 | * path the symlink should point to, it gets created; otherwise, the symlink is not created, and |
| 692 | * in fact gets removed if it was already present from a previous invocation. |
| 693 | */ |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 694 | private ImmutableList<ConvenienceSymlink> createConvenienceSymlinks( |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 695 | BuildRequestOptions buildRequestOptions, AnalysisResult analysisResult) { |
| 696 | SkyframeExecutor executor = env.getSkyframeExecutor(); |
| 697 | Reporter reporter = env.getReporter(); |
| 698 | |
| 699 | // Gather configurations to consider. |
jhorvitz | 33f7648 | 2021-10-28 10:13:26 -0700 | [diff] [blame] | 700 | Set<BuildConfigurationValue> targetConfigurations = |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 701 | buildRequestOptions.useTopLevelTargetsForSymlinks() |
| 702 | ? analysisResult.getTargetsToBuild().stream() |
| 703 | .map(ConfiguredTarget::getConfigurationKey) |
jhorvitz | cd477f2 | 2021-05-05 10:18:22 -0700 | [diff] [blame] | 704 | .filter(Objects::nonNull) |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 705 | .distinct() |
| 706 | .map((key) -> executor.getConfiguration(reporter, key)) |
| 707 | .collect(toImmutableSet()) |
| 708 | : ImmutableSet.copyOf( |
| 709 | analysisResult.getConfigurationCollection().getTargetConfigurations()); |
| 710 | |
| 711 | String productName = runtime.getProductName(); |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 712 | try (SilentCloseable c = |
| 713 | Profiler.instance().profile("OutputDirectoryLinksUtils.createOutputDirectoryLinks")) { |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 714 | return OutputDirectoryLinksUtils.createOutputDirectoryLinks( |
brandjon | 8a60360 | 2019-12-04 10:40:38 -0800 | [diff] [blame] | 715 | runtime.getRuleClassProvider().getSymlinkDefinitions(), |
brandjon | 282958b | 2019-12-04 08:14:26 -0800 | [diff] [blame] | 716 | buildRequestOptions, |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 717 | env.getWorkspaceName(), |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 718 | env.getWorkspace(), |
Googler | 8763639 | 2019-12-30 12:57:56 -0800 | [diff] [blame] | 719 | env.getDirectories(), |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 720 | getReporter(), |
| 721 | targetConfigurations, |
brandjon | 7b5043f | 2019-11-25 12:30:22 -0800 | [diff] [blame] | 722 | options -> getConfiguration(executor, reporter, options), |
brandjon | 282958b | 2019-12-04 08:14:26 -0800 | [diff] [blame] | 723 | productName); |
brandjon | 07cc56d | 2019-11-25 10:00:12 -0800 | [diff] [blame] | 724 | } |
| 725 | } |
| 726 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 727 | /** Prepare for a local output build. */ |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 728 | private void startLocalOutputBuild() throws AbruptExitException { |
ulfjack | 1fbe72e | 2018-06-14 04:46:27 -0700 | [diff] [blame] | 729 | try (SilentCloseable c = Profiler.instance().profile("Starting local output build")) { |
kchodorow | 85ae190 | 2017-04-22 15:07:22 -0400 | [diff] [blame] | 730 | Path outputPath = env.getDirectories().getOutputPath(env.getWorkspaceName()); |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 731 | Path localOutputPath = env.getDirectories().getLocalOutputPath(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 732 | |
| 733 | if (outputPath.isSymbolicLink()) { |
Nathan Harmata | ca06fa2 | 2015-07-27 19:54:14 +0000 | [diff] [blame] | 734 | try { |
| 735 | // Remove the existing symlink first. |
| 736 | outputPath.delete(); |
| 737 | if (localOutputPath.exists()) { |
| 738 | // Pre-existing local output directory. Move to outputPath. |
| 739 | localOutputPath.renameTo(outputPath); |
| 740 | } |
| 741 | } catch (IOException e) { |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 742 | throw createExitException( |
| 743 | "Couldn't handle local output directory symlinks", |
| 744 | Code.LOCAL_OUTPUT_DIRECTORY_SYMLINK_FAILURE, |
| 745 | e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 746 | } |
| 747 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | /** |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 752 | * If a path is supplied, creates and installs an ExplanationHandler. Returns an instance on |
| 753 | * success. Reports an error and returns null otherwise. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 754 | */ |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 755 | private ExplanationHandler installExplanationHandler( |
| 756 | PathFragment explanationPath, String allOptions) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 757 | if (explanationPath == null) { |
| 758 | return null; |
| 759 | } |
| 760 | ExplanationHandler handler; |
| 761 | try { |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 762 | handler = |
| 763 | new ExplanationHandler( |
| 764 | getWorkspace().getRelative(explanationPath).getOutputStream(), allOptions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 765 | } catch (IOException e) { |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 766 | getReporter() |
| 767 | .handle( |
| 768 | Event.warn( |
| 769 | String.format( |
| 770 | "Cannot write explanation of rebuilds to file '%s': %s", |
| 771 | explanationPath, e.getMessage()))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 772 | return null; |
| 773 | } |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 774 | getReporter() |
| 775 | .handle(Event.info("Writing explanation of rebuilds to '" + explanationPath + "'")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 776 | getReporter().addHandler(handler); |
| 777 | return handler; |
| 778 | } |
| 779 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 780 | /** Uninstalls the specified ExplanationHandler (if any) and closes the log file. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 781 | private void uninstallExplanationHandler(ExplanationHandler handler) { |
| 782 | if (handler != null) { |
| 783 | getReporter().removeHandler(handler); |
| 784 | handler.log.close(); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
laszlocsomor | 482235b | 2018-11-22 08:15:15 -0800 | [diff] [blame] | 789 | * An ErrorEventListener implementation that records DEPCHECKER events into a log file, iff the |
| 790 | * --explain flag is specified during a build. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 791 | */ |
laszlocsomor | 482235b | 2018-11-22 08:15:15 -0800 | [diff] [blame] | 792 | private static class ExplanationHandler implements EventHandler, AutoCloseable { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 793 | private final PrintWriter log; |
| 794 | |
| 795 | private ExplanationHandler(OutputStream log, String optionsDescription) { |
lberki | 0d8d4cf | 2017-09-05 16:01:44 +0200 | [diff] [blame] | 796 | this.log = new PrintWriter(new OutputStreamWriter(log, StandardCharsets.UTF_8)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 797 | this.log.println("Build options: " + optionsDescription); |
| 798 | } |
| 799 | |
laszlocsomor | 482235b | 2018-11-22 08:15:15 -0800 | [diff] [blame] | 800 | @Override |
| 801 | public void close() throws IOException { |
| 802 | this.log.close(); |
| 803 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 804 | |
| 805 | @Override |
| 806 | public void handle(Event event) { |
| 807 | if (event.getKind() == EventKind.DEPCHECKER) { |
| 808 | log.println(event.getMessage()); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | /** |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 814 | * Computes the result of the build. Sets the list of successful (up-to-date) targets in the |
| 815 | * request object. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 816 | * |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 817 | * @param configuredTargets The configured targets whose artifacts are to be built. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 818 | */ |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 819 | static ImmutableSet<ConfiguredTarget> determineSuccessfulTargets( |
cpeyser | 8e9b28f | 2018-06-04 08:05:13 -0700 | [diff] [blame] | 820 | Collection<ConfiguredTarget> configuredTargets, Set<ConfiguredTargetKey> builtTargets) { |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 821 | // Maintain the ordering by copying builtTargets into an ImmutableSet.Builder in the same |
| 822 | // iteration order as configuredTargets. |
| 823 | ImmutableSet.Builder<ConfiguredTarget> successfulTargets = ImmutableSet.builder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 824 | for (ConfiguredTarget target : configuredTargets) { |
jcater | 83221e3 | 2020-05-28 11:37:39 -0700 | [diff] [blame] | 825 | if (builtTargets.contains( |
| 826 | ConfiguredTargetKey.builder() |
| 827 | .setConfiguredTarget(target) |
| 828 | .setConfigurationKey(target.getConfigurationKey()) |
| 829 | .build())) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 830 | successfulTargets.add(target); |
| 831 | } |
| 832 | } |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 833 | return successfulTargets.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 834 | } |
| 835 | |
leba | f7b8b72 | 2021-11-16 06:09:08 -0800 | [diff] [blame^] | 836 | static ImmutableSet<AspectKey> determineSuccessfulAspects( |
janakr | f15d08d | 2020-04-22 12:53:03 -0700 | [diff] [blame] | 837 | ImmutableSet<AspectKey> aspects, Set<AspectKey> builtAspects) { |
| 838 | // Maintain the ordering. |
| 839 | return aspects.stream().filter(builtAspects::contains).collect(ImmutableSet.toImmutableSet()); |
tomlu | 6ed4fd5 | 2018-04-03 10:01:10 -0700 | [diff] [blame] | 840 | } |
| 841 | |
ccalvarin | b5fd761 | 2017-12-14 12:21:48 -0800 | [diff] [blame] | 842 | /** Get action cache if present or reload it from the on-disk cache. */ |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 843 | private ActionCache getActionCache() throws AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 844 | try { |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 845 | return env.getBlazeWorkspace().getOrLoadPersistentActionCache(getReporter()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 846 | } catch (IOException e) { |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 847 | String message = |
| 848 | String.format( |
| 849 | "Couldn't create action cache: %s. If error persists, use 'bazel clean'.", |
| 850 | e.getMessage()); |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 851 | throw new AbruptExitException( |
| 852 | DetailedExitCode.of( |
| 853 | FailureDetail.newBuilder() |
| 854 | .setMessage(message) |
| 855 | .setActionCache( |
| 856 | FailureDetails.ActionCache.newBuilder() |
| 857 | .setCode(FailureDetails.ActionCache.Code.INITIALIZATION_FAILURE)) |
| 858 | .build()), |
Googler | 2e1104e | 2020-05-08 06:51:08 -0700 | [diff] [blame] | 859 | e); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 863 | private Builder createBuilder( |
| 864 | BuildRequest request, |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 865 | @Nullable ActionCache actionCache, |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 866 | SkyframeExecutor skyframeExecutor, |
Ulf Adams | 1ac1b99 | 2016-11-02 12:50:26 +0000 | [diff] [blame] | 867 | ModifiedFileSet modifiedOutputFiles) { |
janakr | 6b1b8a9 | 2017-11-10 23:20:22 +0100 | [diff] [blame] | 868 | BuildRequestOptions options = request.getBuildOptions(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 869 | |
ulfjack | f20b33c | 2020-01-30 07:36:30 -0800 | [diff] [blame] | 870 | skyframeExecutor.setActionOutputRoot( |
| 871 | env.getActionTempsDirectory(), env.getPersistentActionOutsDirectory()); |
| 872 | |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 873 | Predicate<Action> executionFilter = |
| 874 | CheckUpToDateFilter.fromOptions(request.getOptions(ExecutionOptions.class)); |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 875 | ArtifactFactory artifactFactory = env.getSkyframeBuildView().getArtifactFactory(); |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 876 | return new SkyframeBuilder( |
| 877 | skyframeExecutor, |
ulfjack | 985a34ad | 2019-08-09 01:51:15 -0700 | [diff] [blame] | 878 | env.getLocalResourceManager(), |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 879 | new ActionCacheChecker( |
| 880 | actionCache, |
| 881 | artifactFactory, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 882 | skyframeExecutor.getActionKeyContext(), |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 883 | executionFilter, |
| 884 | ActionCacheChecker.CacheConfig.builder() |
| 885 | .setEnabled(options.useActionCache) |
| 886 | .setVerboseExplanations(options.verboseExplanations) |
Chi Wang | 4e29042 | 2021-08-03 17:56:19 -0700 | [diff] [blame] | 887 | .setStoreOutputMetadata(options.actionCacheStoreOutputMetadata) |
jhorvitz | 53f1899 | 2021-10-29 10:00:35 -0700 | [diff] [blame] | 888 | .build()), |
felly | c5c078c | 2019-07-31 12:05:24 -0700 | [diff] [blame] | 889 | env.getTopDownActionCache(), |
ajurkowski | d74b0ec | 2020-04-13 10:58:21 -0700 | [diff] [blame] | 890 | request.getPackageOptions().checkOutputFiles |
Googler | 2f11192 | 2017-02-28 20:58:45 +0000 | [diff] [blame] | 891 | ? modifiedOutputFiles |
| 892 | : ModifiedFileSet.NOTHING_MODIFIED, |
schmitt | e643340 | 2020-01-07 12:57:46 -0800 | [diff] [blame] | 893 | env.getFileCache(), |
janakr | 46e5efd | 2021-04-05 13:03:46 -0700 | [diff] [blame] | 894 | prefetcher, |
| 895 | env.getRuntime().getBugReporter()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 896 | } |
| 897 | |
steinman | 13a628a | 2019-01-09 08:39:32 -0800 | [diff] [blame] | 898 | @VisibleForTesting |
ulfjack | 985a34ad | 2019-08-09 01:51:15 -0700 | [diff] [blame] | 899 | public static void configureResourceManager(ResourceManager resourceMgr, BuildRequest request) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 900 | ExecutionOptions options = request.getOptions(ExecutionOptions.class); |
larsrc | 8fa3ccb | 2021-10-26 08:27:34 -0700 | [diff] [blame] | 901 | resourceMgr.setPrioritizeLocalActions(options.prioritizeLocalActions); |
Josh Pieper | 7f3bddd | 2018-05-04 00:39:37 -0700 | [diff] [blame] | 902 | resourceMgr.setUseLocalMemoryEstimate(options.localMemoryEstimate); |
buchgr | 7b51552 | 2019-03-29 04:42:17 -0700 | [diff] [blame] | 903 | resourceMgr.setAvailableResources( |
| 904 | ResourceSet.create( |
larsrc | 8fa3ccb | 2021-10-26 08:27:34 -0700 | [diff] [blame] | 905 | options.localRamResources, |
| 906 | options.localCpuResources, |
| 907 | options.usingLocalTestJobs() ? options.localTestJobs : Integer.MAX_VALUE)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /** |
jmmv | 59787f1 | 2017-08-28 21:41:21 +0200 | [diff] [blame] | 911 | * Writes the action cache files to disk, reporting any errors that occurred during writing and |
| 912 | * capturing statistics. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 913 | */ |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 914 | private void saveActionCache(@Nullable ActionCache actionCache) { |
jmmv | ccb43ee | 2017-08-30 22:07:10 +0200 | [diff] [blame] | 915 | ActionCacheStatistics.Builder builder = ActionCacheStatistics.newBuilder(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 916 | |
jhorvitz | 4d5f354 | 2021-07-19 10:12:29 -0700 | [diff] [blame] | 917 | if (actionCache != null) { |
| 918 | actionCache.mergeIntoActionCacheStatistics(builder); |
| 919 | |
| 920 | AutoProfiler p = |
| 921 | GoogleAutoProfilerUtils.profiledAndLogged("Saving action cache", ProfilerTask.INFO); |
| 922 | try { |
| 923 | builder.setSizeInBytes(actionCache.save()); |
| 924 | } catch (IOException e) { |
| 925 | builder.setSizeInBytes(0); |
| 926 | getReporter().handle(Event.error("I/O error while writing action log: " + e.getMessage())); |
| 927 | } finally { |
| 928 | builder.setSaveTimeInMs(Duration.ofNanos(p.completeAndGetElapsedTimeNanos()).toMillis()); |
| 929 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 930 | } |
jmmv | 59787f1 | 2017-08-28 21:41:21 +0200 | [diff] [blame] | 931 | |
| 932 | env.getEventBus().post(builder.build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 935 | private Reporter getReporter() { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 936 | return env.getReporter(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 937 | } |
| 938 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 939 | private Path getWorkspace() { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 940 | return env.getWorkspace(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | private Path getExecRoot() { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 944 | return env.getExecRoot(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 945 | } |
mschaller | b976a8b | 2020-05-11 11:00:14 -0700 | [diff] [blame] | 946 | |
| 947 | private static AbruptExitException createExitException( |
| 948 | String message, Code detailedCode, IOException e) { |
| 949 | return new AbruptExitException( |
| 950 | DetailedExitCode.of( |
| 951 | FailureDetail.newBuilder() |
| 952 | .setMessage(message) |
| 953 | .setExecution(Execution.newBuilder().setCode(detailedCode)) |
| 954 | .build()), |
| 955 | e); |
| 956 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 957 | } |