Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package com.google.devtools.build.lib.runtime; |
| 16 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 17 | import static com.google.devtools.build.lib.profiler.AutoProfiler.profiled; |
| 18 | |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 19 | import com.google.common.annotations.VisibleForTesting; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableList; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 22 | import com.google.common.eventbus.EventBus; |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 23 | import com.google.devtools.build.lib.actions.PackageRootResolver; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 24 | import com.google.devtools.build.lib.actions.cache.ActionCache; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.analysis.BuildView; |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.SkyframePackageRootResolver; |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.analysis.config.BuildConfiguration; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection; |
| 30 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
Damien Martin-Guillerez | ffbddeb | 2017-01-10 17:20:05 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.config.DefaultsPackage; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.config.InvalidConfigurationException; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.cmdline.Label; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.events.Reporter; |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.exec.OutputService; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 37 | import com.google.devtools.build.lib.packages.Target; |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.pkgcache.PackageCacheOptions; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 39 | import com.google.devtools.build.lib.pkgcache.PackageManager; |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.pkgcache.TargetPatternEvaluator; |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
| 42 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.skyframe.SkyframeBuildView; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.skyframe.SkyframeExecutor; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.util.AbruptExitException; |
| 46 | import com.google.devtools.build.lib.util.ExitCode; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.util.Preconditions; |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 48 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.vfs.FileSystemUtils; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.vfs.Path; |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.vfs.PathFragment; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 52 | import com.google.devtools.common.options.OptionPriority; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 53 | import com.google.devtools.common.options.OptionsClassProvider; |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 54 | import com.google.devtools.common.options.OptionsParser; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 55 | import com.google.devtools.common.options.OptionsParsingException; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 56 | import com.google.devtools.common.options.OptionsProvider; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 57 | import java.io.IOException; |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 58 | import java.util.Collection; |
| 59 | import java.util.Collections; |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 60 | import java.util.List; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 61 | import java.util.Map; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 62 | import java.util.Set; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 63 | import java.util.TreeMap; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 64 | import java.util.TreeSet; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 65 | import java.util.UUID; |
Ulf Adams | 88f643c | 2015-09-17 10:55:52 +0000 | [diff] [blame] | 66 | import java.util.concurrent.atomic.AtomicReference; |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 67 | import javax.annotation.Nullable; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Encapsulates the state needed for a single command. The environment is dropped when the current |
| 71 | * command is done and all corresponding objects are garbage collected. |
| 72 | */ |
| 73 | public final class CommandEnvironment { |
| 74 | private final BlazeRuntime runtime; |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 75 | private final BlazeWorkspace workspace; |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 76 | private final BlazeDirectories directories; |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 77 | |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 78 | private UUID commandId; // Unique identifier for the command being run |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 79 | private final Reporter reporter; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 80 | private final EventBus eventBus; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 81 | private final BlazeModule.ModuleEnvironment blazeModuleEnvironment; |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 82 | private final Map<String, String> clientEnv = new TreeMap<>(); |
| 83 | private final Set<String> visibleClientEnv = new TreeSet<>(); |
Damien Martin-Guillerez | ffbddeb | 2017-01-10 17:20:05 +0000 | [diff] [blame] | 84 | private final Map<String, String> actionClientEnv = new TreeMap<>(); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 85 | private final TimestampGranularityMonitor timestampGranularityMonitor; |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 86 | private final Thread commandThread; |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 87 | |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 88 | private String[] crashData; |
| 89 | |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 90 | private PathFragment relativeWorkingDirectory = PathFragment.EMPTY_FRAGMENT; |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 91 | private long commandStartTime; |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 92 | private OutputService outputService; |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 93 | private Path workingDirectory; |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 94 | |
Ulf Adams | 3877ebd | 2016-11-15 10:22:59 +0000 | [diff] [blame] | 95 | private String commandName; |
| 96 | private OptionsProvider options; |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 97 | |
Ulf Adams | 88f643c | 2015-09-17 10:55:52 +0000 | [diff] [blame] | 98 | private AtomicReference<AbruptExitException> pendingException = new AtomicReference<>(); |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 99 | |
| 100 | private class BlazeModuleEnvironment implements BlazeModule.ModuleEnvironment { |
| 101 | @Override |
Ulf Adams | ae90bc9 | 2015-09-18 09:18:58 +0000 | [diff] [blame] | 102 | public Path getFileFromWorkspace(Label label) |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 103 | throws NoSuchThingException, InterruptedException, IOException { |
| 104 | Target target = getPackageManager().getTarget(reporter, label); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 105 | return (outputService != null) |
| 106 | ? outputService.stageTool(target) |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 107 | : target.getPackage().getPackageDirectory().getRelative(target.getName()); |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void exit(AbruptExitException exception) { |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 112 | Preconditions.checkNotNull(exception); |
| 113 | Preconditions.checkNotNull(exception.getExitCode()); |
| 114 | if (pendingException.compareAndSet(null, exception)) { |
| 115 | // There was no exception, so we're the first one to ask for an exit. Interrupt the command. |
| 116 | commandThread.interrupt(); |
| 117 | } |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Creates a new command environment which can be used for executing commands for the given |
| 123 | * runtime in the given workspace, which will publish events on the given eventBus. The |
| 124 | * commandThread passed is interrupted when a module requests an early exit. |
| 125 | */ |
| 126 | CommandEnvironment( |
| 127 | BlazeRuntime runtime, BlazeWorkspace workspace, EventBus eventBus, Thread commandThread) { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 128 | this.runtime = runtime; |
Ulf Adams | 69cc003 | 2016-03-30 13:52:25 +0000 | [diff] [blame] | 129 | this.workspace = workspace; |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 130 | this.directories = workspace.getDirectories(); |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 131 | this.commandId = null; // Will be set once we get the client environment |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 132 | this.reporter = new Reporter(eventBus); |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 133 | this.eventBus = eventBus; |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 134 | this.commandThread = commandThread; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 135 | this.blazeModuleEnvironment = new BlazeModuleEnvironment(); |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 136 | this.timestampGranularityMonitor = new TimestampGranularityMonitor(runtime.getClock()); |
| 137 | // Record the command's starting time again, for use by |
| 138 | // TimestampGranularityMonitor.waitForTimestampGranularity(). |
| 139 | // This should be done as close as possible to the start of |
| 140 | // the command's execution. |
| 141 | timestampGranularityMonitor.setCommandStartTime(); |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 142 | |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 143 | // TODO(ulfjack): We don't call beforeCommand() in tests, but rely on workingDirectory being set |
| 144 | // in setupPackageCache(). This leads to NPE if we don't set it here. |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 145 | this.workingDirectory = directories.getWorkspace(); |
Ulf Adams | 69cc003 | 2016-03-30 13:52:25 +0000 | [diff] [blame] | 146 | |
| 147 | workspace.getSkyframeExecutor().setEventBus(eventBus); |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 148 | } |
| 149 | |
lpino | 1fde230 | 2017-04-07 10:22:28 +0000 | [diff] [blame] | 150 | /** |
| 151 | * Same as CommandEnvironment(BlazeRuntime, BlazeWorkspace, EventBus, Thread) but with an |
| 152 | * explicit commandName and options. |
| 153 | * |
| 154 | * ONLY for testing. |
| 155 | */ |
| 156 | @VisibleForTesting |
| 157 | CommandEnvironment( |
| 158 | BlazeRuntime runtime, BlazeWorkspace workspace, EventBus eventBus, Thread commandThread, |
| 159 | String commandNameForTesting, OptionsProvider optionsForTesting) { |
| 160 | this(runtime, workspace, eventBus, commandThread); |
| 161 | // Both commandName and options are normally set by beforeCommand(); however this method is not |
| 162 | // called in tests (i.e. tests use BlazeRuntimeWrapper). These fields should only be set for |
| 163 | // testing. |
| 164 | this.commandName = commandNameForTesting; |
| 165 | this.options = optionsForTesting; |
| 166 | } |
| 167 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 168 | public BlazeRuntime getRuntime() { |
| 169 | return runtime; |
| 170 | } |
| 171 | |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 172 | public BlazeWorkspace getBlazeWorkspace() { |
| 173 | return workspace; |
| 174 | } |
| 175 | |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 176 | public BlazeDirectories getDirectories() { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 177 | return directories; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the reporter for events. |
| 182 | */ |
| 183 | public Reporter getReporter() { |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 184 | return reporter; |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | public EventBus getEventBus() { |
| 188 | return eventBus; |
| 189 | } |
| 190 | |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 191 | public BlazeModule.ModuleEnvironment getBlazeModuleEnvironment() { |
| 192 | return blazeModuleEnvironment; |
| 193 | } |
| 194 | |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 195 | /** |
| 196 | * Return an unmodifiable view of the blaze client's environment when it invoked the current |
| 197 | * command. |
| 198 | */ |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 199 | public Map<String, String> getClientEnv() { |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 200 | return Collections.unmodifiableMap(clientEnv); |
| 201 | } |
| 202 | |
Ulf Adams | 3877ebd | 2016-11-15 10:22:59 +0000 | [diff] [blame] | 203 | public String getCommandName() { |
| 204 | return commandName; |
| 205 | } |
| 206 | |
| 207 | public OptionsProvider getOptions() { |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 208 | return options; |
| 209 | } |
| 210 | |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 211 | /** |
Klaus Aehlig | b8d0902 | 2016-09-20 13:39:08 +0000 | [diff] [blame] | 212 | * Return an ordered version of the client environment restricted to those variables whitelisted |
| 213 | * by the command-line options to be inheritable by actions. |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 214 | */ |
Klaus Aehlig | b8d0902 | 2016-09-20 13:39:08 +0000 | [diff] [blame] | 215 | public Map<String, String> getWhitelistedClientEnv() { |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 216 | Map<String, String> visibleEnv = new TreeMap<>(); |
| 217 | for (String var : visibleClientEnv) { |
| 218 | String value = clientEnv.get(var); |
| 219 | if (value != null) { |
| 220 | visibleEnv.put(var, value); |
| 221 | } |
| 222 | } |
| 223 | return Collections.unmodifiableMap(visibleEnv); |
| 224 | } |
| 225 | |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 226 | @VisibleForTesting |
Dmitry Lomov | 31fab29 | 2017-03-07 18:33:08 +0000 | [diff] [blame] | 227 | void updateClientEnv(List<Map.Entry<String, String>> clientEnvList) { |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 228 | Preconditions.checkState(clientEnv.isEmpty()); |
| 229 | |
Dmitry Lomov | 31fab29 | 2017-03-07 18:33:08 +0000 | [diff] [blame] | 230 | Collection<Map.Entry<String, String>> env = clientEnvList; |
Ulf Adams | 8d2e60d | 2015-09-17 08:11:24 +0000 | [diff] [blame] | 231 | for (Map.Entry<String, String> entry : env) { |
| 232 | clientEnv.put(entry.getKey(), entry.getValue()); |
| 233 | } |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 234 | // Try to set the clientId from the client environment. |
| 235 | if (commandId == null) { |
| 236 | String uuidString = clientEnv.get("BAZEL_INTERNAL_INVOCATION_ID"); |
| 237 | if (uuidString != null) { |
| 238 | try { |
| 239 | commandId = UUID.fromString(uuidString); |
| 240 | } catch (IllegalArgumentException e) { |
| 241 | // String was malformed, so we will resort to generating a random UUID |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | if (commandId == null) { |
| 246 | // We have been provided with the client environment, but it didn't contain |
| 247 | // the invocation id; hence generate our own. |
| 248 | commandId = UUID.randomUUID(); |
| 249 | } |
| 250 | setCommandIdInCrashData(); |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 251 | } |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 252 | |
Ulf Adams | c73051c6 | 2016-03-23 09:18:13 +0000 | [diff] [blame] | 253 | public TimestampGranularityMonitor getTimestampGranularityMonitor() { |
| 254 | return timestampGranularityMonitor; |
| 255 | } |
| 256 | |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 257 | public PackageManager getPackageManager() { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 258 | return getSkyframeExecutor().getPackageManager(); |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 261 | public PathFragment getRelativeWorkingDirectory() { |
| 262 | return relativeWorkingDirectory; |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | /** |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 266 | * Creates and returns a new target pattern parser. |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 267 | */ |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 268 | public TargetPatternEvaluator newTargetPatternEvaluator() { |
| 269 | TargetPatternEvaluator result = getPackageManager().newTargetPatternEvaluator(); |
| 270 | result.updateOffset(relativeWorkingDirectory); |
| 271 | return result; |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Ulf Adams | 5b9009b | 2015-09-24 09:52:53 +0000 | [diff] [blame] | 274 | public PackageRootResolver getPackageRootResolver() { |
| 275 | return new SkyframePackageRootResolver(getSkyframeExecutor(), reporter); |
| 276 | } |
| 277 | |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 278 | /** |
| 279 | * Returns the UUID that Blaze uses to identify everything logged from the current build command. |
| 280 | * It's also used to invalidate Skyframe nodes that are specific to a certain invocation, such as |
| 281 | * the build info. |
| 282 | */ |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 283 | public UUID getCommandId() { |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 284 | if (commandId == null) { |
| 285 | // The commandId should not be requested before the beforeCommand is executed, as the |
| 286 | // commandId might be set through the client environment. However, to simplify testing, |
| 287 | // we set the id value before we throw the exception. |
| 288 | commandId = UUID.randomUUID(); |
| 289 | throw new IllegalArgumentException("Build Id requested before client environment provided"); |
| 290 | } |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 291 | return commandId; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | public SkyframeExecutor getSkyframeExecutor() { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 295 | return workspace.getSkyframeExecutor(); |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 298 | public SkyframeBuildView getSkyframeBuildView() { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 299 | return getSkyframeExecutor().getSkyframeBuildView(); |
Ulf Adams | 3d67e00 | 2016-03-29 16:23:01 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 302 | /** |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 303 | * Returns the working directory of the server. |
| 304 | * |
| 305 | * <p>This is often the first entry on the {@code --package_path}, but not always. |
| 306 | * Callers should certainly not make this assumption. The Path returned may be null. |
| 307 | */ |
| 308 | public Path getWorkspace() { |
| 309 | return getDirectories().getWorkspace(); |
| 310 | } |
| 311 | |
| 312 | public String getWorkspaceName() { |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 313 | Path workspace = getDirectories().getWorkspace(); |
| 314 | if (workspace == null) { |
| 315 | return ""; |
| 316 | } |
| 317 | return workspace.getBaseName(); |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Returns if the client passed a valid workspace to be used for the build. |
| 322 | */ |
| 323 | public boolean inWorkspace() { |
| 324 | return getDirectories().inWorkspace(); |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * Returns the output base directory associated with this Blaze server |
| 329 | * process. This is the base directory for shared Blaze state as well as tool |
| 330 | * and strategy specific subdirectories. |
| 331 | */ |
| 332 | public Path getOutputBase() { |
| 333 | return getDirectories().getOutputBase(); |
| 334 | } |
| 335 | |
| 336 | /** |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 337 | * Returns the execution root directory associated with this Blaze server |
| 338 | * process. This is where all input and output files visible to the actual |
| 339 | * build reside. |
| 340 | */ |
| 341 | public Path getExecRoot() { |
hlopko | c413480 | 2017-04-04 13:53:14 +0000 | [diff] [blame] | 342 | return getDirectories().getExecRoot(); |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | /** |
janakr | 0049e96 | 2017-04-05 21:41:23 +0000 | [diff] [blame] | 346 | * Returns the directory where actions' outputs and errors will be written. Is below the directory |
| 347 | * returned by {@link #getExecRoot}. |
| 348 | */ |
| 349 | public Path getActionConsoleOutputDirectory() { |
| 350 | return getDirectories().getActionConsoleOutputDirectory(getExecRoot()); |
| 351 | } |
| 352 | |
| 353 | /** |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 354 | * Returns the working directory of the {@code blaze} client process. |
| 355 | * |
| 356 | * <p>This may be equal to {@code BlazeRuntime#getWorkspace()}, or beneath it. |
| 357 | * |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 358 | * @see #getWorkspace() |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 359 | */ |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 360 | public Path getWorkingDirectory() { |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 361 | return workingDirectory; |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 364 | /** |
| 365 | * @return the OutputService in use, or null if none. |
| 366 | */ |
| 367 | public OutputService getOutputService() { |
| 368 | return outputService; |
| 369 | } |
| 370 | |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 371 | public ActionCache getPersistentActionCache() throws IOException { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 372 | return workspace.getPersistentActionCache(reporter); |
| 373 | } |
| 374 | |
| 375 | /** |
Janak Ramakrishnan | 5e94673 | 2016-11-17 17:01:46 +0000 | [diff] [blame] | 376 | * An array of String values useful if Blaze crashes. For now, just returns the build id as soon |
| 377 | * as it is determined. |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 378 | */ |
Janak Ramakrishnan | 5e94673 | 2016-11-17 17:01:46 +0000 | [diff] [blame] | 379 | String[] getCrashData() { |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 380 | if (crashData == null) { |
| 381 | String buildId; |
| 382 | if (commandId == null) { |
| 383 | buildId = " (build id not set yet)"; |
| 384 | } else { |
| 385 | buildId = commandId + " (build id)"; |
| 386 | } |
Janak Ramakrishnan | 5e94673 | 2016-11-17 17:01:46 +0000 | [diff] [blame] | 387 | crashData = new String[] {buildId}; |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 388 | } |
| 389 | return crashData; |
| 390 | } |
| 391 | |
| 392 | private void setCommandIdInCrashData() { |
| 393 | // Update the command id in the crash data, if it is already generated |
| 394 | if (crashData != null && crashData.length >= 2) { |
| 395 | crashData[1] = getCommandId() + " (build id)"; |
| 396 | } |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 399 | /** |
| 400 | * This method only exists for the benefit of InfoCommand, which needs to construct a {@link |
| 401 | * BuildConfigurationCollection} without running a full loading phase. Don't add any more clients; |
| 402 | * instead, we should change info so that it doesn't need the configuration. |
| 403 | */ |
| 404 | public BuildConfigurationCollection getConfigurations(OptionsProvider optionsProvider) |
| 405 | throws InvalidConfigurationException, InterruptedException { |
| 406 | BuildOptions buildOptions = runtime.createBuildOptions(optionsProvider); |
| 407 | boolean keepGoing = optionsProvider.getOptions(BuildView.Options.class).keepGoing; |
Ulf Adams | e5aaacf | 2015-09-24 12:40:30 +0000 | [diff] [blame] | 408 | return getSkyframeExecutor().createConfigurations(reporter, runtime.getConfigurationFactory(), |
Ulf Adams | f069d45b | 2016-04-15 07:54:24 +0000 | [diff] [blame] | 409 | buildOptions, ImmutableSet.<String>of(), keepGoing); |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 413 | * Prevents any further interruption of this command by modules, and returns the final exit code |
| 414 | * from modules, or null if no modules requested an abrupt exit. |
| 415 | * |
| 416 | * <p>Always returns the same value on subsequent calls. |
| 417 | */ |
| 418 | @Nullable |
| 419 | private ExitCode finalizeExitCode() { |
| 420 | // Set the pending exception so that further calls to exit(AbruptExitException) don't lead to |
| 421 | // unwanted thread interrupts. |
| 422 | if (pendingException.compareAndSet(null, new AbruptExitException(null))) { |
| 423 | return null; |
| 424 | } |
| 425 | if (Thread.currentThread() == commandThread) { |
| 426 | // We may have interrupted the thread in the process, so clear the interrupted bit. |
| 427 | // Whether the command was interrupted or not, it's about to be over, so don't interrupt later |
| 428 | // things happening on this thread. |
| 429 | Thread.interrupted(); |
| 430 | } |
| 431 | // Extract the exit code (it can be null if someone has already called finalizeExitCode()). |
| 432 | return getPendingExitCode(); |
| 433 | } |
| 434 | |
| 435 | /** |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 436 | * Hook method called by the BlazeCommandDispatcher right before the dispatch |
| 437 | * of each command ends (while its outcome can still be modified). |
| 438 | */ |
| 439 | ExitCode precompleteCommand(ExitCode originalExit) { |
| 440 | eventBus.post(new CommandPrecompleteEvent(originalExit)); |
| 441 | // If Blaze did not suffer an infrastructure failure, check for errors in modules. |
| 442 | ExitCode exitCode = originalExit; |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 443 | ExitCode newExitCode = finalizeExitCode(); |
| 444 | if (!originalExit.isInfrastructureFailure() && newExitCode != null) { |
| 445 | exitCode = newExitCode; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 446 | } |
| 447 | return exitCode; |
| 448 | } |
| 449 | |
| 450 | /** |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 451 | * Returns the current exit code requested by modules, or null if no exit has been requested. |
| 452 | */ |
| 453 | @Nullable |
| 454 | public ExitCode getPendingExitCode() { |
| 455 | AbruptExitException exception = getPendingException(); |
| 456 | return exception == null ? null : exception.getExitCode(); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Retrieves the exception currently queued by a Blaze module. |
| 461 | * |
| 462 | * <p>Prefer getPendingExitCode or throwPendingException where appropriate. |
| 463 | */ |
| 464 | public AbruptExitException getPendingException() { |
| 465 | return pendingException.get(); |
| 466 | } |
| 467 | |
| 468 | /** |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 469 | * Throws the exception currently queued by a Blaze module. |
| 470 | * |
| 471 | * <p>This should be called as often as is practical so that errors are reported as soon as |
| 472 | * possible. Ideally, we'd not need this, but the event bus swallows exceptions so we raise |
| 473 | * the exception this way. |
| 474 | */ |
| 475 | public void throwPendingException() throws AbruptExitException { |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 476 | AbruptExitException exception = getPendingException(); |
Ulf Adams | 88f643c | 2015-09-17 10:55:52 +0000 | [diff] [blame] | 477 | if (exception != null) { |
Michael Staib | 6e5e8fb | 2016-10-04 21:26:37 +0000 | [diff] [blame] | 478 | if (Thread.currentThread() == commandThread) { |
| 479 | // Throwing this exception counts as the requested interruption. Clear the interrupted bit. |
| 480 | Thread.interrupted(); |
| 481 | } |
Ulf Adams | 88f643c | 2015-09-17 10:55:52 +0000 | [diff] [blame] | 482 | throw exception; |
Ulf Adams | ca2d8d2 | 2015-09-16 13:00:45 +0000 | [diff] [blame] | 483 | } |
Ulf Adams | 8061302 | 2015-09-16 09:11:33 +0000 | [diff] [blame] | 484 | } |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 485 | |
| 486 | /** |
| 487 | * Initializes the package cache using the given options, and syncs the package cache. Also |
| 488 | * injects a defaults package using the options for the {@link BuildConfiguration}. |
| 489 | * |
| 490 | * @see DefaultsPackage |
| 491 | */ |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 492 | public void setupPackageCache(OptionsClassProvider options, |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 493 | String defaultsPackageContents) throws InterruptedException, AbruptExitException { |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 494 | SkyframeExecutor skyframeExecutor = getSkyframeExecutor(); |
| 495 | if (!skyframeExecutor.hasIncrementalState()) { |
| 496 | skyframeExecutor.resetEvaluator(); |
| 497 | } |
kchodorow | dfcd5da8 | 2017-04-19 18:58:50 +0200 | [diff] [blame^] | 498 | |
| 499 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 500 | skyframeExecutor.injectExtraPrecomputedValues(module.getPrecomputedValues()); |
| 501 | } |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 502 | skyframeExecutor.sync( |
| 503 | reporter, |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 504 | options.getOptions(PackageCacheOptions.class), |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 505 | getOutputBase(), |
| 506 | getWorkingDirectory(), |
| 507 | defaultsPackageContents, |
| 508 | getCommandId(), |
Damien Martin-Guillerez | 777f3af | 2017-02-08 17:22:02 +0000 | [diff] [blame] | 509 | clientEnv, |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 510 | timestampGranularityMonitor, |
| 511 | options); |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 514 | public void recordLastExecutionTime() { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 515 | workspace.recordLastExecutionTime(getCommandStartTime()); |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | public void recordCommandStartTime(long commandStartTime) { |
| 519 | this.commandStartTime = commandStartTime; |
| 520 | } |
| 521 | |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 522 | public long getCommandStartTime() { |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 523 | return commandStartTime; |
Ulf Adams | 3815b4c | 2015-09-18 07:34:13 +0000 | [diff] [blame] | 524 | } |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 525 | |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 526 | void setWorkingDirectory(Path workingDirectory) { |
| 527 | this.workingDirectory = workingDirectory; |
| 528 | } |
| 529 | |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 530 | /** |
| 531 | * Hook method called by the BlazeCommandDispatcher prior to the dispatch of |
| 532 | * each command. |
| 533 | * |
| 534 | * @param options The CommonCommandOptions used by every command. |
| 535 | * @throws AbruptExitException if this command is unsuitable to be run as specified |
| 536 | */ |
| 537 | void beforeCommand(Command command, OptionsParser optionsParser, |
Lukacs Berki | 2896dc0 | 2016-07-07 07:55:04 +0000 | [diff] [blame] | 538 | CommonCommandOptions options, long execStartTimeNanos, long waitTimeInMs) |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 539 | throws AbruptExitException { |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 540 | commandStartTime -= options.startupTime; |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 541 | if (runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).watchFS) { |
| 542 | try { |
| 543 | // TODO(ulfjack): Get rid of the startup option and drop this code. |
| 544 | optionsParser.parse("--watchfs"); |
| 545 | } catch (OptionsParsingException e) { |
| 546 | // This should never happen. |
| 547 | throw new IllegalStateException(e); |
| 548 | } |
| 549 | } |
Ulf Adams | 3877ebd | 2016-11-15 10:22:59 +0000 | [diff] [blame] | 550 | this.commandName = command.name(); |
Ulf Adams | a0e3af4 | 2016-10-31 16:52:48 +0000 | [diff] [blame] | 551 | this.options = optionsParser; |
Ulf Adams | b514610 | 2015-10-20 08:57:26 +0000 | [diff] [blame] | 552 | |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 553 | eventBus.post(new GotOptionsEvent(runtime.getStartupOptionsProvider(), optionsParser)); |
| 554 | throwPendingException(); |
| 555 | |
| 556 | outputService = null; |
| 557 | BlazeModule outputModule = null; |
Janak Ramakrishnan | 6c3ac8a | 2016-11-17 18:30:46 +0000 | [diff] [blame] | 558 | if (command.builds()) { |
| 559 | for (BlazeModule module : runtime.getBlazeModules()) { |
| 560 | OutputService moduleService = module.getOutputService(); |
| 561 | if (moduleService != null) { |
| 562 | if (outputService != null) { |
| 563 | throw new IllegalStateException( |
| 564 | String.format( |
| 565 | "More than one module (%s and %s) returns an output service", |
| 566 | module.getClass(), outputModule.getClass())); |
| 567 | } |
| 568 | outputService = moduleService; |
| 569 | outputModule = module; |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 570 | } |
Philipp Wollermann | 49c20aa | 2016-08-25 12:59:52 +0000 | [diff] [blame] | 571 | } |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 574 | SkyframeExecutor skyframeExecutor = getSkyframeExecutor(); |
Eric Fellheimer | f3b43af | 2015-11-13 19:51:20 +0000 | [diff] [blame] | 575 | skyframeExecutor.setOutputService(outputService); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 576 | |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 577 | // Ensure that the working directory will be under the workspace directory. |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 578 | Path workspace = getWorkspace(); |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 579 | Path workingDirectory; |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 580 | if (inWorkspace()) { |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 581 | workingDirectory = workspace.getRelative(options.clientCwd); |
| 582 | } else { |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 583 | workspace = FileSystemUtils.getWorkingDirectory(getDirectories().getFileSystem()); |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 584 | workingDirectory = workspace; |
| 585 | } |
Ulf Adams | 08663e6 | 2016-02-12 09:59:22 +0000 | [diff] [blame] | 586 | this.relativeWorkingDirectory = workingDirectory.relativeTo(workspace); |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 587 | this.workingDirectory = workingDirectory; |
| 588 | |
Dmitry Lomov | 31fab29 | 2017-03-07 18:33:08 +0000 | [diff] [blame] | 589 | updateClientEnv(options.clientEnv); |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 590 | |
| 591 | // Fail fast in the case where a Blaze command forgets to install the package path correctly. |
| 592 | skyframeExecutor.setActive(false); |
| 593 | // Let skyframe figure out if it needs to store graph edges for this build. |
| 594 | skyframeExecutor.decideKeepIncrementalState( |
| 595 | runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class).batch, |
janakr | 95d4280 | 2017-04-06 00:33:39 +0000 | [diff] [blame] | 596 | optionsParser.getOptions(BuildView.Options.class)); |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 597 | |
| 598 | // Start the performance and memory profilers. |
| 599 | runtime.beforeCommand(this, options, execStartTimeNanos); |
| 600 | |
Damien Martin-Guillerez | ffbddeb | 2017-01-10 17:20:05 +0000 | [diff] [blame] | 601 | // actionClientEnv contains the environment where values from actionEnvironment are |
| 602 | // overridden. |
| 603 | actionClientEnv.clear(); |
| 604 | actionClientEnv.putAll(clientEnv); |
| 605 | |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 606 | if (command.builds()) { |
| 607 | Map<String, String> testEnv = new TreeMap<>(); |
| 608 | for (Map.Entry<String, String> entry : |
| 609 | optionsParser.getOptions(BuildConfiguration.Options.class).testEnvironment) { |
| 610 | testEnv.put(entry.getKey(), entry.getValue()); |
| 611 | } |
| 612 | |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 613 | // Compute the set of environment variables that are whitelisted on the commandline |
| 614 | // for inheritence. |
| 615 | for (Map.Entry<String, String> entry : |
| 616 | optionsParser.getOptions(BuildConfiguration.Options.class).actionEnvironment) { |
| 617 | if (entry.getValue() == null) { |
| 618 | visibleClientEnv.add(entry.getKey()); |
| 619 | } else { |
| 620 | visibleClientEnv.remove(entry.getKey()); |
Damien Martin-Guillerez | ffbddeb | 2017-01-10 17:20:05 +0000 | [diff] [blame] | 621 | actionClientEnv.put(entry.getKey(), entry.getValue()); |
Klaus Aehlig | 6f33a1c | 2016-09-13 16:46:10 +0000 | [diff] [blame] | 622 | } |
| 623 | } |
| 624 | |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 625 | try { |
| 626 | for (Map.Entry<String, String> entry : testEnv.entrySet()) { |
| 627 | if (entry.getValue() == null) { |
| 628 | String clientValue = clientEnv.get(entry.getKey()); |
| 629 | if (clientValue != null) { |
| 630 | optionsParser.parse(OptionPriority.SOFTWARE_REQUIREMENT, |
| 631 | "test environment variable from client environment", |
| 632 | ImmutableList.of( |
| 633 | "--test_env=" + entry.getKey() + "=" + clientEnv.get(entry.getKey()))); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | } catch (OptionsParsingException e) { |
| 638 | throw new IllegalStateException(e); |
| 639 | } |
| 640 | } |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 641 | |
Lukacs Berki | 2896dc0 | 2016-07-07 07:55:04 +0000 | [diff] [blame] | 642 | eventBus.post(new CommandStartEvent( |
Klaus Aehlig | 88a590a | 2016-08-12 08:56:30 +0000 | [diff] [blame] | 643 | command.name(), getCommandId(), getClientEnv(), workingDirectory, getDirectories(), |
Lukacs Berki | 2896dc0 | 2016-07-07 07:55:04 +0000 | [diff] [blame] | 644 | waitTimeInMs + options.waitTime)); |
Ulf Adams | ebf1b2e | 2015-09-29 11:06:53 +0000 | [diff] [blame] | 645 | } |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 646 | |
Nathan Harmata | dd61520 | 2016-04-29 22:17:00 +0000 | [diff] [blame] | 647 | /** Returns the name of the file system we are writing output to. */ |
| 648 | public String determineOutputFileSystem() { |
| 649 | // If we have a fancy OutputService, this may be different between consecutive Blaze commands |
| 650 | // and so we need to compute it freshly. Otherwise, we can used the immutable value that's |
| 651 | // precomputed by our BlazeWorkspace. |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 652 | if (getOutputService() != null) { |
Nathan Harmata | dd61520 | 2016-04-29 22:17:00 +0000 | [diff] [blame] | 653 | try (AutoProfiler p = profiled("Finding output file system", ProfilerTask.INFO)) { |
| 654 | return getOutputService().getFilesSystemName(); |
| 655 | } |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 656 | } |
Nathan Harmata | dd61520 | 2016-04-29 22:17:00 +0000 | [diff] [blame] | 657 | return workspace.getOutputBaseFilesystemTypeName(); |
Ulf Adams | 706b7f2 | 2015-10-20 09:06:45 +0000 | [diff] [blame] | 658 | } |
Damien Martin-Guillerez | ffbddeb | 2017-01-10 17:20:05 +0000 | [diff] [blame] | 659 | |
| 660 | /** |
| 661 | * Returns the client environment for which value specified in the command line with the flag |
| 662 | * --action_env have been enforced. |
| 663 | */ |
| 664 | public Map<String, String> getActionClientEnv() { |
| 665 | return Collections.unmodifiableMap(actionClientEnv); |
| 666 | } |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 667 | } |