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 | |
| 15 | package com.google.devtools.build.lib.runtime; |
| 16 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.annotations.VisibleForTesting; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.collect.ImmutableList; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ImmutableMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import com.google.common.eventbus.SubscriberExceptionContext; |
| 23 | import com.google.common.eventbus.SubscriberExceptionHandler; |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 24 | import com.google.common.util.concurrent.Futures; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.util.concurrent.Uninterruptibles; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 26 | import com.google.devtools.build.lib.actions.ActionKeyContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 27 | import com.google.devtools.build.lib.analysis.BlazeDirectories; |
| 28 | import com.google.devtools.build.lib.analysis.BlazeVersionInfo; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 29 | import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; |
Ulf Adams | 15a23b9 | 2016-08-09 11:46:00 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.analysis.ServerDirectories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 31 | import com.google.devtools.build.lib.analysis.config.BuildOptions; |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 32 | import com.google.devtools.build.lib.analysis.config.ConfigurationFragmentFactory; |
ulfjack | ab21d18 | 2017-08-10 15:36:14 +0200 | [diff] [blame] | 33 | import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory; |
buchgr | 35ff63a | 2018-07-09 05:55:40 -0700 | [diff] [blame^] | 34 | import com.google.devtools.build.lib.buildeventstream.BuildEventArtifactUploaderFactoryMap; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 35 | import com.google.devtools.build.lib.clock.BlazeClock; |
| 36 | import com.google.devtools.build.lib.clock.Clock; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.events.Event; |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 38 | import com.google.devtools.build.lib.events.EventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 39 | import com.google.devtools.build.lib.events.OutputFilter; |
ulfjack | 9d8ff35 | 2018-02-15 01:50:36 -0800 | [diff] [blame] | 40 | import com.google.devtools.build.lib.exec.BinTools; |
Nathan Harmata | 42fb560 | 2016-05-25 20:32:08 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.packages.Package; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 42 | import com.google.devtools.build.lib.packages.PackageFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | import com.google.devtools.build.lib.packages.RuleClassProvider; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.lib.profiler.MemoryProfiler; |
| 46 | import com.google.devtools.build.lib.profiler.ProfilePhase; |
| 47 | import com.google.devtools.build.lib.profiler.Profiler; |
| 48 | import com.google.devtools.build.lib.profiler.Profiler.ProfiledTaskKinds; |
| 49 | import com.google.devtools.build.lib.profiler.ProfilerTask; |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 50 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment; |
| 52 | import com.google.devtools.build.lib.query2.QueryEnvironmentFactory; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | import com.google.devtools.build.lib.query2.output.OutputFormatter; |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.LockingMode; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.runtime.commands.InfoItem; |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 58 | import com.google.devtools.build.lib.server.CommandProtos.EnvironmentVariable; |
| 59 | import com.google.devtools.build.lib.server.CommandProtos.ExecRequest; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | import com.google.devtools.build.lib.server.RPCServer; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 61 | import com.google.devtools.build.lib.server.signal.InterruptSignalHandler; |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 62 | import com.google.devtools.build.lib.shell.JavaSubprocessFactory; |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 63 | import com.google.devtools.build.lib.shell.SubprocessBuilder; |
ulfjack | 27758e4 | 2017-09-07 13:41:33 +0200 | [diff] [blame] | 64 | import com.google.devtools.build.lib.shell.SubprocessFactory; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.unix.UnixFileSystem; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 66 | import com.google.devtools.build.lib.util.AbruptExitException; |
Chloe Calvarin | eaa3be7 | 2016-12-13 19:48:34 +0000 | [diff] [blame] | 67 | import com.google.devtools.build.lib.util.CustomExitCodePublisher; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | import com.google.devtools.build.lib.util.ExitCode; |
| 69 | import com.google.devtools.build.lib.util.LoggingUtil; |
| 70 | import com.google.devtools.build.lib.util.OS; |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 71 | import com.google.devtools.build.lib.util.Pair; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.util.ProcessUtils; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | import com.google.devtools.build.lib.util.ThreadUtils; |
| 74 | import com.google.devtools.build.lib.util.io.OutErr; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | import com.google.devtools.build.lib.vfs.FileSystem; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 76 | import com.google.devtools.build.lib.vfs.JavaIoFileSystem; |
| 77 | import com.google.devtools.build.lib.vfs.Path; |
| 78 | import com.google.devtools.build.lib.vfs.PathFragment; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 79 | import com.google.devtools.build.lib.windows.WindowsFileSystem; |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 80 | import com.google.devtools.build.lib.windows.WindowsSubprocessFactory; |
ccalvarin | f39dc6f | 2017-06-09 11:51:45 -0400 | [diff] [blame] | 81 | import com.google.devtools.common.options.CommandNameCache; |
| 82 | import com.google.devtools.common.options.InvocationPolicyParser; |
ccalvarin | e8aae03 | 2017-08-22 07:17:44 +0200 | [diff] [blame] | 83 | import com.google.devtools.common.options.OptionDefinition; |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 84 | import com.google.devtools.common.options.OptionPriority.PriorityCategory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | import com.google.devtools.common.options.OptionsBase; |
| 86 | import com.google.devtools.common.options.OptionsClassProvider; |
| 87 | import com.google.devtools.common.options.OptionsParser; |
| 88 | import com.google.devtools.common.options.OptionsParsingException; |
| 89 | import com.google.devtools.common.options.OptionsProvider; |
| 90 | import com.google.devtools.common.options.TriState; |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 91 | import java.io.File; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 92 | import java.io.IOException; |
| 93 | import java.io.OutputStream; |
ccalvarin | 987f09f | 2017-08-31 19:50:39 +0200 | [diff] [blame] | 94 | import java.lang.reflect.Type; |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 95 | import java.nio.charset.StandardCharsets; |
ccalvarin | af79eb4 | 2018-02-22 12:41:44 -0800 | [diff] [blame] | 96 | import java.time.Duration; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | import java.util.ArrayList; |
| 98 | import java.util.Arrays; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 99 | import java.util.Date; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 100 | import java.util.Iterator; |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 101 | import java.util.LinkedHashMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 102 | import java.util.List; |
Ulf Adams | b5a06f33 | 2016-06-21 14:55:49 +0000 | [diff] [blame] | 103 | import java.util.Locale; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 104 | import java.util.Map; |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 105 | import java.util.Optional; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 106 | import java.util.UUID; |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 107 | import java.util.concurrent.Future; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 108 | import java.util.concurrent.TimeUnit; |
| 109 | import java.util.concurrent.atomic.AtomicInteger; |
ccalvarin | e8aae03 | 2017-08-22 07:17:44 +0200 | [diff] [blame] | 110 | import java.util.function.Function; |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 111 | import java.util.logging.Handler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 112 | import java.util.logging.Level; |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 113 | import java.util.logging.LogRecord; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 114 | import java.util.logging.Logger; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 115 | import java.util.regex.Matcher; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 116 | import java.util.regex.Pattern; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 117 | import javax.annotation.Nullable; |
| 118 | |
| 119 | /** |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 120 | * The BlazeRuntime class encapsulates the immutable configuration of the current instance. These |
| 121 | * runtime settings and services are available to most parts of any Blaze application for the |
| 122 | * duration of the batch run or server lifetime. |
| 123 | * |
| 124 | * <p>The parts specific to the current command are stored in {@link CommandEnvironment}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 125 | */ |
| 126 | public final class BlazeRuntime { |
Michajlo Matijkiw | a132f0e | 2016-11-21 17:30:43 +0000 | [diff] [blame] | 127 | private static final Pattern suppressFromLog = |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 128 | Pattern.compile("--client_env=([^=]*(?:auth|pass|cookie)[^=]*)=", Pattern.CASE_INSENSITIVE); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 129 | |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 130 | private static final Logger logger = Logger.getLogger(BlazeRuntime.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 131 | |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 132 | private final FileSystem fileSystem; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 133 | private final Iterable<BlazeModule> blazeModules; |
| 134 | private final Map<String, BlazeCommand> commandMap = new LinkedHashMap<>(); |
| 135 | private final Clock clock; |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 136 | private final Runnable abruptShutdownHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 137 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 138 | private final PackageFactory packageFactory; |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 139 | private final ImmutableList<ConfigurationFragmentFactory> configurationFragmentFactories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 140 | private final ConfiguredRuleClassProvider ruleClassProvider; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 141 | // For bazel info. |
| 142 | private final ImmutableMap<String, InfoItem> infoItems; |
| 143 | // For bazel query. |
| 144 | private final QueryEnvironmentFactory queryEnvironmentFactory; |
| 145 | private final ImmutableList<QueryFunction> queryFunctions; |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 146 | private final ImmutableList<OutputFormatter> queryOutputFormatters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 147 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 148 | private final AtomicInteger storedExitCode = new AtomicInteger(); |
| 149 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 150 | // We pass this through here to make it available to the MasterLogWriter. |
| 151 | private final OptionsProvider startupOptionsProvider; |
| 152 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 153 | private final ProjectFile.Provider projectFileProvider; |
Chloe Calvarin | 6b60718 | 2017-03-27 15:37:50 +0000 | [diff] [blame] | 154 | @Nullable private final InvocationPolicy moduleInvocationPolicy; |
Janak Ramakrishnan | 1d89960 | 2016-04-28 03:48:55 +0000 | [diff] [blame] | 155 | private final String defaultsPackageContent; |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 156 | private final SubscriberExceptionHandler eventBusExceptionHandler; |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 157 | private final String productName; |
buchgr | 35ff63a | 2018-07-09 05:55:40 -0700 | [diff] [blame^] | 158 | private final BuildEventArtifactUploaderFactoryMap buildEventArtifactUploaderFactoryMap; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 159 | private final ActionKeyContext actionKeyContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 160 | |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 161 | // Workspace state (currently exactly one workspace per server) |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 162 | private BlazeWorkspace workspace; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 163 | |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 164 | private BlazeRuntime( |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 165 | FileSystem fileSystem, |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 166 | QueryEnvironmentFactory queryEnvironmentFactory, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 167 | ImmutableList<QueryFunction> queryFunctions, |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 168 | ImmutableList<OutputFormatter> queryOutputFormatters, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 169 | PackageFactory pkgFactory, |
| 170 | ConfiguredRuleClassProvider ruleClassProvider, |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 171 | ImmutableList<ConfigurationFragmentFactory> configurationFragmentFactories, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 172 | ImmutableMap<String, InfoItem> infoItems, |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 173 | ActionKeyContext actionKeyContext, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 174 | Clock clock, |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 175 | Runnable abruptShutdownHandler, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 176 | OptionsProvider startupOptionsProvider, |
| 177 | Iterable<BlazeModule> blazeModules, |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 178 | SubscriberExceptionHandler eventBusExceptionHandler, |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 179 | ProjectFile.Provider projectFileProvider, |
Chloe Calvarin | 6b60718 | 2017-03-27 15:37:50 +0000 | [diff] [blame] | 180 | InvocationPolicy moduleInvocationPolicy, |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 181 | Iterable<BlazeCommand> commands, |
Klaus Aehlig | dadde6f | 2017-01-18 17:32:07 +0000 | [diff] [blame] | 182 | String productName, |
buchgr | 35ff63a | 2018-07-09 05:55:40 -0700 | [diff] [blame^] | 183 | BuildEventArtifactUploaderFactoryMap buildEventArtifactUploaderFactoryMap) { |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 184 | // Server state |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 185 | this.fileSystem = fileSystem; |
Ulf Adams | 50e7db6 | 2015-10-20 09:14:16 +0000 | [diff] [blame] | 186 | this.blazeModules = blazeModules; |
| 187 | overrideCommands(commands); |
| 188 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | this.packageFactory = pkgFactory; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 190 | this.projectFileProvider = projectFileProvider; |
Chloe Calvarin | 6b60718 | 2017-03-27 15:37:50 +0000 | [diff] [blame] | 191 | this.moduleInvocationPolicy = moduleInvocationPolicy; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 192 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 193 | this.ruleClassProvider = ruleClassProvider; |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 194 | this.configurationFragmentFactories = configurationFragmentFactories; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 195 | this.infoItems = infoItems; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 196 | this.actionKeyContext = actionKeyContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 197 | this.clock = clock; |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 198 | this.abruptShutdownHandler = abruptShutdownHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 199 | this.startupOptionsProvider = startupOptionsProvider; |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 200 | this.queryEnvironmentFactory = queryEnvironmentFactory; |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 201 | this.queryFunctions = queryFunctions; |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 202 | this.queryOutputFormatters = queryOutputFormatters; |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 203 | this.eventBusExceptionHandler = eventBusExceptionHandler; |
Janak Ramakrishnan | 1d89960 | 2016-04-28 03:48:55 +0000 | [diff] [blame] | 204 | |
| 205 | this.defaultsPackageContent = |
Chloe Calvarin | 6b60718 | 2017-03-27 15:37:50 +0000 | [diff] [blame] | 206 | ruleClassProvider.getDefaultsPackageContent(getModuleInvocationPolicy()); |
Janak Ramakrishnan | e9cd0f3 | 2016-04-29 22:46:16 +0000 | [diff] [blame] | 207 | CommandNameCache.CommandNameCacheInstance.INSTANCE.setCommandNameCache( |
| 208 | new CommandNameCacheImpl(getCommandMap())); |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 209 | this.productName = productName; |
buchgr | 35ff63a | 2018-07-09 05:55:40 -0700 | [diff] [blame^] | 210 | this.buildEventArtifactUploaderFactoryMap = buildEventArtifactUploaderFactoryMap; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 211 | } |
| 212 | |
ulfjack | 5a3b24c | 2017-06-08 17:10:06 +0200 | [diff] [blame] | 213 | public BlazeWorkspace initWorkspace(BlazeDirectories directories, BinTools binTools) |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 214 | throws AbruptExitException { |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 215 | Preconditions.checkState(this.workspace == null); |
Ulf Adams | de14ade | 2016-10-14 14:20:31 +0000 | [diff] [blame] | 216 | WorkspaceBuilder builder = new WorkspaceBuilder(directories, binTools); |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 217 | for (BlazeModule module : blazeModules) { |
ulfjack | 94bee75 | 2017-06-13 19:13:35 +0200 | [diff] [blame] | 218 | module.workspaceInit(this, directories, builder); |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 219 | } |
janakr | eea486c | 2017-09-22 15:38:50 -0400 | [diff] [blame] | 220 | this.workspace = |
| 221 | builder.build(this, packageFactory, ruleClassProvider, eventBusExceptionHandler); |
ulfjack | 5a3b24c | 2017-06-08 17:10:06 +0200 | [diff] [blame] | 222 | return workspace; |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Ulf Adams | 33644a4 | 2016-08-10 11:32:41 +0000 | [diff] [blame] | 225 | @Nullable public CoverageReportActionFactory getCoverageReportActionFactory( |
| 226 | OptionsClassProvider commandOptions) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 227 | CoverageReportActionFactory firstFactory = null; |
| 228 | for (BlazeModule module : blazeModules) { |
Ulf Adams | 33644a4 | 2016-08-10 11:32:41 +0000 | [diff] [blame] | 229 | CoverageReportActionFactory factory = module.getCoverageReportFactory(commandOptions); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 230 | if (factory != null) { |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 231 | Preconditions.checkState( |
| 232 | firstFactory == null, "only one Bazel Module can have a Coverage Report Factory"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 233 | firstFactory = factory; |
| 234 | } |
| 235 | } |
| 236 | return firstFactory; |
| 237 | } |
| 238 | |
| 239 | /** |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 240 | * Adds the given command under the given name to the map of commands. |
| 241 | * |
| 242 | * @throws AssertionError if the name is already used by another command. |
| 243 | */ |
| 244 | private void addCommand(BlazeCommand command) { |
| 245 | String name = command.getClass().getAnnotation(Command.class).name(); |
| 246 | if (commandMap.containsKey(name)) { |
| 247 | throw new IllegalStateException("Command name or alias " + name + " is already used."); |
| 248 | } |
| 249 | commandMap.put(name, command); |
| 250 | } |
| 251 | |
| 252 | final void overrideCommands(Iterable<BlazeCommand> commands) { |
| 253 | commandMap.clear(); |
| 254 | for (BlazeCommand command : commands) { |
| 255 | addCommand(command); |
| 256 | } |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 259 | @Nullable |
Chloe Calvarin | 6b60718 | 2017-03-27 15:37:50 +0000 | [diff] [blame] | 260 | public InvocationPolicy getModuleInvocationPolicy() { |
| 261 | return moduleInvocationPolicy; |
Luis Fernando Pino Duque | b1b28b6 | 2016-02-25 14:25:19 +0000 | [diff] [blame] | 262 | } |
| 263 | |
ulfjack | cca111f | 2018-06-15 06:15:57 -0700 | [diff] [blame] | 264 | /** Configure profiling based on the provided options. */ |
| 265 | void initProfiler( |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 266 | EventHandler eventHandler, |
| 267 | BlazeWorkspace workspace, |
ulfjack | f92107b | 2018-06-08 14:14:05 -0700 | [diff] [blame] | 268 | CommonCommandOptions options, |
| 269 | UUID buildID, |
| 270 | long execStartTimeNanos) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 271 | OutputStream out = null; |
| 272 | boolean recordFullProfilerData = false; |
| 273 | ProfiledTaskKinds profiledTasks = ProfiledTaskKinds.NONE; |
ulfjack | 4a12e07 | 2018-06-14 01:15:13 -0700 | [diff] [blame] | 274 | Profiler.Format format = Profiler.Format.BINARY_BAZEL_FORMAT; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | try { |
ulfjack | 400fffe | 2018-06-12 12:21:25 -0700 | [diff] [blame] | 276 | if (options.enableTracer) { |
| 277 | Path profilePath = options.profilePath != null |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 278 | ? workspace.getWorkspace().getRelative(options.profilePath) |
| 279 | : workspace.getOutputBase().getRelative("command.profile"); |
ulfjack | 400fffe | 2018-06-12 12:21:25 -0700 | [diff] [blame] | 280 | recordFullProfilerData = false; |
| 281 | out = profilePath.getOutputStream(); |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 282 | eventHandler.handle(Event.info("Writing tracer profile to '" + profilePath + "'")); |
ulfjack | 400fffe | 2018-06-12 12:21:25 -0700 | [diff] [blame] | 283 | profiledTasks = ProfiledTaskKinds.ALL_FOR_TRACE; |
ulfjack | 4a12e07 | 2018-06-14 01:15:13 -0700 | [diff] [blame] | 284 | format = Profiler.Format.JSON_TRACE_FILE_FORMAT; |
ulfjack | 400fffe | 2018-06-12 12:21:25 -0700 | [diff] [blame] | 285 | } else if (options.profilePath != null) { |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 286 | Path profilePath = workspace.getWorkspace().getRelative(options.profilePath); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 287 | |
| 288 | recordFullProfilerData = options.recordFullProfilerData; |
ulfjack | 21d60de | 2018-06-12 07:38:51 -0700 | [diff] [blame] | 289 | out = profilePath.getOutputStream(); |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 290 | eventHandler.handle(Event.info("Writing profile data to '" + profilePath + "'")); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 291 | profiledTasks = ProfiledTaskKinds.ALL; |
| 292 | } else if (options.alwaysProfileSlowOperations) { |
| 293 | recordFullProfilerData = false; |
| 294 | out = null; |
| 295 | profiledTasks = ProfiledTaskKinds.SLOWEST; |
| 296 | } |
| 297 | if (profiledTasks != ProfiledTaskKinds.NONE) { |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 298 | Profiler profiler = Profiler.instance(); |
| 299 | profiler.start( |
ulfjack | f92107b | 2018-06-08 14:14:05 -0700 | [diff] [blame] | 300 | profiledTasks, |
| 301 | out, |
ulfjack | 4a12e07 | 2018-06-14 01:15:13 -0700 | [diff] [blame] | 302 | format, |
ulfjack | 21d60de | 2018-06-12 07:38:51 -0700 | [diff] [blame] | 303 | String.format( |
| 304 | "%s profile for %s at %s, build ID: %s", |
| 305 | getProductName(), |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 306 | workspace.getOutputBase(), |
ulfjack | 21d60de | 2018-06-12 07:38:51 -0700 | [diff] [blame] | 307 | new Date(), |
| 308 | buildID), |
ulfjack | f92107b | 2018-06-08 14:14:05 -0700 | [diff] [blame] | 309 | recordFullProfilerData, |
| 310 | clock, |
| 311 | execStartTimeNanos); |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 312 | // Instead of logEvent() we're calling the low level function to pass the timings we took in |
| 313 | // the launcher. We're setting the INIT phase marker so that it follows immediately the |
| 314 | // LAUNCH phase. |
| 315 | long startupTimeNanos = options.startupTime * 1000000L; |
| 316 | profiler.logSimpleTaskDuration( |
| 317 | execStartTimeNanos - startupTimeNanos, |
| 318 | Duration.ZERO, |
| 319 | ProfilerTask.PHASE, |
| 320 | ProfilePhase.LAUNCH.description); |
| 321 | profiler.logSimpleTaskDuration( |
| 322 | execStartTimeNanos, Duration.ZERO, ProfilerTask.PHASE, ProfilePhase.INIT.description); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 323 | } |
| 324 | } catch (IOException e) { |
ulfjack | 8e09941 | 2018-06-15 02:53:42 -0700 | [diff] [blame] | 325 | eventHandler.handle(Event.error("Error while creating profile file: " + e.getMessage())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 326 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 327 | } |
| 328 | |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 329 | public FileSystem getFileSystem() { |
| 330 | return fileSystem; |
| 331 | } |
| 332 | |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 333 | public BlazeWorkspace getWorkspace() { |
| 334 | return workspace; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 335 | } |
| 336 | |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 337 | public ActionKeyContext getActionKeyContext() { |
| 338 | return actionKeyContext; |
| 339 | } |
| 340 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 341 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 342 | * The directory in which blaze stores the server state - that is, the socket |
| 343 | * file and a log. |
| 344 | */ |
Ulf Adams | 94b72db | 2016-03-30 11:58:37 +0000 | [diff] [blame] | 345 | private Path getServerDirectory() { |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 346 | return getWorkspace().getDirectories().getOutputBase().getChild("server"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 349 | /** |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 350 | * Returns the {@link QueryEnvironmentFactory} that should be used to create a |
| 351 | * {@link AbstractBlazeQueryEnvironment}, whenever one is needed. |
| 352 | */ |
| 353 | public QueryEnvironmentFactory getQueryEnvironmentFactory() { |
| 354 | return queryEnvironmentFactory; |
| 355 | } |
| 356 | |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 357 | public ImmutableList<QueryFunction> getQueryFunctions() { |
| 358 | return queryFunctions; |
| 359 | } |
| 360 | |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 361 | public ImmutableList<OutputFormatter> getQueryOutputFormatters() { |
| 362 | return queryOutputFormatters; |
| 363 | } |
| 364 | |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 365 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 366 | * Returns the package factory. |
| 367 | */ |
| 368 | public PackageFactory getPackageFactory() { |
| 369 | return packageFactory; |
| 370 | } |
| 371 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 372 | /** |
| 373 | * Returns the rule class provider. |
| 374 | */ |
| 375 | public ConfiguredRuleClassProvider getRuleClassProvider() { |
| 376 | return ruleClassProvider; |
| 377 | } |
| 378 | |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 379 | public ImmutableMap<String, InfoItem> getInfoItems() { |
| 380 | return infoItems; |
| 381 | } |
| 382 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 383 | public Iterable<BlazeModule> getBlazeModules() { |
| 384 | return blazeModules; |
| 385 | } |
| 386 | |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 387 | public BuildOptions getDefaultBuildOptions() { |
| 388 | BuildOptions options = null; |
| 389 | for (BlazeModule module : blazeModules) { |
| 390 | BuildOptions optionsFromModule = module.getDefaultBuildOptions(this); |
| 391 | if (optionsFromModule != null) { |
| 392 | if (options == null) { |
| 393 | options = optionsFromModule; |
| 394 | } else { |
| 395 | throw new IllegalArgumentException( |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 396 | "Two or more bazel modules contained default build options."); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | } |
| 400 | if (options == null) { |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 401 | throw new IllegalArgumentException("No default build options specified in any Bazel module"); |
mjhalupka | 5d7fa7b | 2018-03-22 13:37:38 -0700 | [diff] [blame] | 402 | } |
| 403 | return options; |
| 404 | } |
| 405 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 406 | @SuppressWarnings("unchecked") |
| 407 | public <T extends BlazeModule> T getBlazeModule(Class<T> moduleClass) { |
| 408 | for (BlazeModule module : blazeModules) { |
| 409 | if (module.getClass() == moduleClass) { |
| 410 | return (T) module; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | return null; |
| 415 | } |
| 416 | |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 417 | public ImmutableList<ConfigurationFragmentFactory> getConfigurationFragmentFactories() { |
| 418 | return configurationFragmentFactories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 422 | * Returns a provider for project file objects. Can be null if no such provider was set by any of |
| 423 | * the modules. |
| 424 | */ |
| 425 | @Nullable |
| 426 | public ProjectFile.Provider getProjectFileProvider() { |
| 427 | return projectFileProvider; |
| 428 | } |
| 429 | |
shahan | 2cbfb89 | 2018-03-28 09:00:11 -0700 | [diff] [blame] | 430 | public Path getOutputBase() { |
| 431 | return getWorkspace().getDirectories().getOutputBase(); |
| 432 | } |
| 433 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 434 | /** |
| 435 | * Hook method called by the BlazeCommandDispatcher prior to the dispatch of |
| 436 | * each command. |
| 437 | * |
| 438 | * @param options The CommonCommandOptions used by every command. |
| 439 | * @throws AbruptExitException if this command is unsuitable to be run as specified |
| 440 | */ |
ulfjack | cca111f | 2018-06-15 06:15:57 -0700 | [diff] [blame] | 441 | void beforeCommand(CommandEnvironment env, CommonCommandOptions options) |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 442 | throws AbruptExitException { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 443 | if (options.memoryProfilePath != null) { |
Ulf Adams | c585530 | 2015-10-20 08:46:38 +0000 | [diff] [blame] | 444 | Path memoryProfilePath = env.getWorkingDirectory().getRelative(options.memoryProfilePath); |
janakr | dfa0b12 | 2018-02-28 09:46:06 -0800 | [diff] [blame] | 445 | MemoryProfiler.instance() |
| 446 | .setStableMemoryParameters(options.memoryProfileStableHeapParameters); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 447 | try { |
| 448 | MemoryProfiler.instance().start(memoryProfilePath.getOutputStream()); |
| 449 | } catch (IOException e) { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 450 | env.getReporter().handle( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 451 | Event.error("Error while creating memory profile file: " + e.getMessage())); |
| 452 | } |
| 453 | } |
| 454 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 455 | // Initialize exit code to dummy value for afterCommand. |
| 456 | storedExitCode.set(ExitCode.RESERVED.getNumericExitCode()); |
| 457 | } |
| 458 | |
| 459 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 460 | * Posts the {@link CommandCompleteEvent}, so that listeners can tidy up. Called by {@link |
| 461 | * #afterCommand}, and by BugReport when crashing from an exception in an async thread. |
| 462 | */ |
Janak Ramakrishnan | 39a5513 | 2016-05-23 21:55:20 +0000 | [diff] [blame] | 463 | void notifyCommandComplete(int exitCode) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 464 | if (!storedExitCode.compareAndSet(ExitCode.RESERVED.getNumericExitCode(), exitCode)) { |
| 465 | // This command has already been called, presumably because there is a race between the main |
| 466 | // thread and a worker thread that crashed. Don't try to arbitrate the dispute. If the main |
| 467 | // thread won the race (unlikely, but possible), this may be incorrectly logged as a success. |
| 468 | return; |
| 469 | } |
Janak Ramakrishnan | a50b7b7 | 2016-06-02 21:46:40 +0000 | [diff] [blame] | 470 | workspace.getSkyframeExecutor().getEventBus().post(new CommandCompleteEvent(exitCode)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 471 | } |
| 472 | |
janakr | d1af430 | 2018-04-24 09:32:36 -0700 | [diff] [blame] | 473 | /** |
| 474 | * Hook method called by the BlazeCommandDispatcher after the dispatch of each command. Returns a |
| 475 | * new exit code in case exceptions were encountered during cleanup. |
| 476 | */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 477 | @VisibleForTesting |
janakr | d1af430 | 2018-04-24 09:32:36 -0700 | [diff] [blame] | 478 | public int afterCommand(CommandEnvironment env, int exitCode) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 479 | // Remove any filters that the command might have added to the reporter. |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 480 | env.getReporter().setOutputFilter(OutputFilter.OUTPUT_EVERYTHING); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 481 | |
| 482 | notifyCommandComplete(exitCode); |
| 483 | |
| 484 | for (BlazeModule module : blazeModules) { |
ulfjack | ef5b63d | 2018-06-15 07:19:11 -0700 | [diff] [blame] | 485 | try (SilentCloseable c = Profiler.instance().profile(module + ".afterCommand")) { |
| 486 | module.afterCommand(); |
| 487 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 488 | } |
| 489 | |
felly | ac7d5f6 | 2018-05-15 10:57:33 -0700 | [diff] [blame] | 490 | // Wipe the dependency graph if requested. Note that this method always runs at the end of |
| 491 | // a commands unless the server crashes, in which case no inmemory state will linger for the |
| 492 | // next build anyway. |
| 493 | CommonCommandOptions commonOptions = |
| 494 | Preconditions.checkNotNull(env.getOptions().getOptions(CommonCommandOptions.class)); |
| 495 | if (!commonOptions.keepStateAfterBuild) { |
ccalvarin | 28c20f7 | 2018-01-22 07:53:56 -0800 | [diff] [blame] | 496 | workspace.getSkyframeExecutor().resetEvaluator(); |
| 497 | } |
| 498 | |
janakr | d1af430 | 2018-04-24 09:32:36 -0700 | [diff] [blame] | 499 | // Build-related commands already call this hook in BuildTool#stopRequest, but non-build |
| 500 | // commands might also need to notify the SkyframeExecutor. It's called in #stopRequest so that |
| 501 | // timing metrics for builds can be more accurate (since this call can be slow). |
| 502 | try { |
| 503 | workspace.getSkyframeExecutor().notifyCommandComplete(); |
| 504 | } catch (InterruptedException e) { |
| 505 | exitCode = ExitCode.INTERRUPTED.getNumericExitCode(); |
| 506 | Thread.currentThread().interrupt(); |
| 507 | } |
| 508 | |
Ulf Adams | ab43b97 | 2016-03-30 12:23:50 +0000 | [diff] [blame] | 509 | env.getBlazeWorkspace().clearEventBus(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 510 | |
| 511 | try { |
| 512 | Profiler.instance().stop(); |
| 513 | MemoryProfiler.instance().stop(); |
| 514 | } catch (IOException e) { |
Ulf Adams | 633f539 | 2015-09-15 11:13:08 +0000 | [diff] [blame] | 515 | env.getReporter().handle(Event.error("Error while writing profile file: " + e.getMessage())); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 516 | } |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 517 | env.getReporter().clearEventBus(); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 518 | |
| 519 | actionKeyContext.clear(); |
janakr | d1af430 | 2018-04-24 09:32:36 -0700 | [diff] [blame] | 520 | return exitCode; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | // Make sure we keep a strong reference to this logger, so that the |
| 524 | // configuration isn't lost when the gc kicks in. |
| 525 | private static Logger templateLogger = Logger.getLogger("com.google.devtools.build"); |
| 526 | |
| 527 | /** |
| 528 | * Configures "com.google.devtools.build.*" loggers to the given |
| 529 | * {@code level}. Note: This code relies on static state. |
| 530 | */ |
| 531 | public static void setupLogging(Level level) { |
| 532 | templateLogger.setLevel(level); |
| 533 | templateLogger.info("Log level: " + templateLogger.getLevel()); |
| 534 | } |
| 535 | |
| 536 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 537 | * Returns the Clock-instance used for the entire build. Before, |
| 538 | * individual classes (such as Profiler) used to specify the type |
| 539 | * of clock (e.g. EpochClock) they wanted to use. This made it |
| 540 | * difficult to get Blaze working on Windows as some of the clocks |
| 541 | * available for Linux aren't (directly) available on Windows. |
| 542 | * Setting the Blaze-wide clock upon construction of BlazeRuntime |
| 543 | * allows injecting whatever Clock instance should be used from |
| 544 | * BlazeMain. |
| 545 | * |
| 546 | * @return The Blaze-wide clock |
| 547 | */ |
| 548 | public Clock getClock() { |
| 549 | return clock; |
| 550 | } |
| 551 | |
| 552 | public OptionsProvider getStartupOptionsProvider() { |
| 553 | return startupOptionsProvider; |
| 554 | } |
| 555 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 556 | public Map<String, BlazeCommand> getCommandMap() { |
| 557 | return commandMap; |
| 558 | } |
| 559 | |
Julio Merino | d1619b75 | 2016-10-17 09:50:11 +0000 | [diff] [blame] | 560 | /** Invokes {@link BlazeModule#blazeShutdown()} on all registered modules. */ |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 561 | public void shutdown() { |
| 562 | for (BlazeModule module : blazeModules) { |
| 563 | module.blazeShutdown(); |
| 564 | } |
| 565 | } |
| 566 | |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 567 | public void prepareForAbruptShutdown() { |
| 568 | if (abruptShutdownHandler != null) { |
| 569 | abruptShutdownHandler.run(); |
| 570 | } |
| 571 | } |
| 572 | |
Julio Merino | d1619b75 | 2016-10-17 09:50:11 +0000 | [diff] [blame] | 573 | /** Invokes {@link BlazeModule#blazeShutdownOnCrash()} on all registered modules. */ |
| 574 | public void shutdownOnCrash() { |
| 575 | for (BlazeModule module : blazeModules) { |
| 576 | module.blazeShutdownOnCrash(); |
| 577 | } |
| 578 | } |
| 579 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 580 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 581 | * Returns the defaults package for the default settings. Should only be called by commands that |
| 582 | * do <i>not</i> process {@link BuildOptions}, since build options can alter the contents of the |
| 583 | * defaults package, which will not be reflected here. |
| 584 | */ |
| 585 | public String getDefaultsPackageContent() { |
Janak Ramakrishnan | 1d89960 | 2016-04-28 03:48:55 +0000 | [diff] [blame] | 586 | return defaultsPackageContent; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /** |
| 590 | * Returns the defaults package for the given options taken from an optionsProvider. |
| 591 | */ |
| 592 | public String getDefaultsPackageContent(OptionsClassProvider optionsProvider) { |
| 593 | return ruleClassProvider.getDefaultsPackageContent(optionsProvider); |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * Creates a BuildOptions class for the given options taken from an optionsProvider. |
| 598 | */ |
| 599 | public BuildOptions createBuildOptions(OptionsClassProvider optionsProvider) { |
| 600 | return ruleClassProvider.createBuildOptions(optionsProvider); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * An EventBus exception handler that will report the exception to a remote server, if a |
| 605 | * handler is registered. |
| 606 | */ |
| 607 | public static final class RemoteExceptionHandler implements SubscriberExceptionHandler { |
| 608 | @Override |
| 609 | public void handleException(Throwable exception, SubscriberExceptionContext context) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 610 | logger.log(Level.SEVERE, "Failure in EventBus subscriber", exception); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 611 | LoggingUtil.logToRemote(Level.SEVERE, "Failure in EventBus subscriber.", exception); |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * An EventBus exception handler that will call BugReport.handleCrash exiting |
| 617 | * the current thread. |
| 618 | */ |
| 619 | public static final class BugReportingExceptionHandler implements SubscriberExceptionHandler { |
| 620 | @Override |
| 621 | public void handleException(Throwable exception, SubscriberExceptionContext context) { |
| 622 | BugReport.handleCrash(exception); |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Main method for the Blaze server startup. Note: This method logs |
| 628 | * exceptions to remote servers. Do not add this to a unittest. |
| 629 | */ |
| 630 | public static void main(Iterable<Class<? extends BlazeModule>> moduleClasses, String[] args) { |
| 631 | setupUncaughtHandler(args); |
| 632 | List<BlazeModule> modules = createModules(moduleClasses); |
Googler | c8c64e7 | 2015-03-23 23:22:18 +0000 | [diff] [blame] | 633 | // blaze.cc will put --batch first if the user set it. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 634 | if (args.length >= 1 && args[0].equals("--batch")) { |
| 635 | // Run Blaze in batch mode. |
| 636 | System.exit(batchMain(modules, args)); |
| 637 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 638 | logger.info( |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 639 | "Starting Bazel server with " + maybeGetPidString() + "args " + Arrays.toString(args)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 640 | try { |
| 641 | // Run Blaze in server mode. |
| 642 | System.exit(serverMain(modules, OutErr.SYSTEM_OUT_ERR, args)); |
| 643 | } catch (RuntimeException | Error e) { // A definite bug... |
| 644 | BugReport.printBug(OutErr.SYSTEM_OUT_ERR, e); |
| 645 | BugReport.sendBugReport(e, Arrays.asList(args)); |
| 646 | System.exit(ExitCode.BLAZE_INTERNAL_ERROR.getNumericExitCode()); |
| 647 | throw e; // Shouldn't get here. |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | @VisibleForTesting |
| 652 | public static List<BlazeModule> createModules( |
| 653 | Iterable<Class<? extends BlazeModule>> moduleClasses) { |
| 654 | ImmutableList.Builder<BlazeModule> result = ImmutableList.builder(); |
| 655 | for (Class<? extends BlazeModule> moduleClass : moduleClasses) { |
| 656 | try { |
Ulf Adams | 653a16a | 2016-08-11 12:39:46 +0000 | [diff] [blame] | 657 | BlazeModule module = moduleClass.getConstructor().newInstance(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 658 | result.add(module); |
| 659 | } catch (Throwable e) { |
| 660 | throw new IllegalStateException("Cannot instantiate module " + moduleClass.getName(), e); |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | return result.build(); |
| 665 | } |
| 666 | |
| 667 | /** |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 668 | * Generates a string form of a request to be written to the logs, filtering the user environment |
| 669 | * to remove anything that looks private. The current filter criteria removes any variable whose |
| 670 | * name includes "auth", "pass", or "cookie". |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 671 | * |
| 672 | * @param requestStrings |
| 673 | * @return the filtered request to write to the log. |
| 674 | */ |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 675 | public static String getRequestLogString(List<String> requestStrings) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 676 | StringBuilder buf = new StringBuilder(); |
| 677 | buf.append('['); |
| 678 | String sep = ""; |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 679 | Matcher m = suppressFromLog.matcher(""); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 680 | for (String s : requestStrings) { |
| 681 | buf.append(sep); |
Ulf Adams | fd37004 | 2017-06-16 15:52:06 +0200 | [diff] [blame] | 682 | m.reset(s); |
| 683 | if (m.lookingAt()) { |
| 684 | buf.append(m.group()); |
| 685 | buf.append("__private_value_removed__"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 686 | } else { |
| 687 | buf.append(s); |
| 688 | } |
| 689 | sep = ", "; |
| 690 | } |
| 691 | buf.append(']'); |
| 692 | return buf.toString(); |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * Command line options split in to two parts: startup options and everything else. |
| 697 | */ |
| 698 | @VisibleForTesting |
| 699 | static class CommandLineOptions { |
| 700 | private final List<String> startupArgs; |
| 701 | private final List<String> otherArgs; |
| 702 | |
| 703 | CommandLineOptions(List<String> startupArgs, List<String> otherArgs) { |
| 704 | this.startupArgs = ImmutableList.copyOf(startupArgs); |
| 705 | this.otherArgs = ImmutableList.copyOf(otherArgs); |
| 706 | } |
| 707 | |
| 708 | public List<String> getStartupArgs() { |
| 709 | return startupArgs; |
| 710 | } |
| 711 | |
| 712 | public List<String> getOtherArgs() { |
| 713 | return otherArgs; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | /** |
Lukacs Berki | 3d48683 | 2016-10-26 12:51:38 +0000 | [diff] [blame] | 718 | * Splits given options into two lists - arguments matching options defined in this class and |
| 719 | * everything else, while preserving order in each list. |
| 720 | * |
| 721 | * <p>Note that this method relies on the startup options always being in the |
| 722 | * <code>--flag=ARG</code> form (instead of <code>--flag ARG</code>). This is enforced by |
| 723 | * <code>GetArgumentArray()</code> in <code>blaze.cc</code> by reconstructing the startup |
| 724 | * options from their parsed versions instead of using <code>argv</code> verbatim. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 725 | */ |
| 726 | static CommandLineOptions splitStartupOptions( |
| 727 | Iterable<BlazeModule> modules, String... args) { |
| 728 | List<String> prefixes = new ArrayList<>(); |
ccalvarin | 987f09f | 2017-08-31 19:50:39 +0200 | [diff] [blame] | 729 | List<OptionDefinition> startupOptions = Lists.newArrayList(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 730 | for (Class<? extends OptionsBase> defaultOptions |
| 731 | : BlazeCommandUtils.getStartupOptions(modules)) { |
ccalvarin | 987f09f | 2017-08-31 19:50:39 +0200 | [diff] [blame] | 732 | startupOptions.addAll(OptionsParser.getOptionDefinitions(defaultOptions)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 733 | } |
| 734 | |
ccalvarin | 987f09f | 2017-08-31 19:50:39 +0200 | [diff] [blame] | 735 | for (OptionDefinition optionDefinition : startupOptions) { |
| 736 | Type optionType = optionDefinition.getField().getType(); |
| 737 | prefixes.add("--" + optionDefinition.getOptionName()); |
| 738 | if (optionType == boolean.class || optionType == TriState.class) { |
| 739 | prefixes.add("--no" + optionDefinition.getOptionName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 740 | } |
| 741 | } |
| 742 | |
| 743 | List<String> startupArgs = new ArrayList<>(); |
| 744 | List<String> otherArgs = Lists.newArrayList(args); |
| 745 | |
| 746 | for (Iterator<String> argi = otherArgs.iterator(); argi.hasNext(); ) { |
| 747 | String arg = argi.next(); |
| 748 | if (!arg.startsWith("--")) { |
| 749 | break; // stop at command - all startup options would be specified before it. |
| 750 | } |
| 751 | for (String prefix : prefixes) { |
| 752 | if (arg.startsWith(prefix)) { |
| 753 | startupArgs.add(arg); |
| 754 | argi.remove(); |
| 755 | break; |
| 756 | } |
| 757 | } |
| 758 | } |
| 759 | return new CommandLineOptions(startupArgs, otherArgs); |
| 760 | } |
| 761 | |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 762 | private static InterruptSignalHandler captureSigint() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 763 | final Thread mainThread = Thread.currentThread(); |
| 764 | final AtomicInteger numInterrupts = new AtomicInteger(); |
| 765 | |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 766 | final Runnable interruptWatcher = |
| 767 | () -> { |
| 768 | int count = 0; |
| 769 | // Not an actual infinite loop because it's run in a daemon thread. |
| 770 | while (true) { |
| 771 | count++; |
| 772 | Uninterruptibles.sleepUninterruptibly(10, TimeUnit.SECONDS); |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 773 | logger.warning("Slow interrupt number " + count + " in batch mode"); |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 774 | ThreadUtils.warnAboutSlowInterrupt(); |
| 775 | } |
| 776 | }; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 777 | |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 778 | return new InterruptSignalHandler() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 779 | @Override |
Janak Ramakrishnan | 7be337f | 2017-01-05 20:21:35 +0000 | [diff] [blame] | 780 | public void run() { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 781 | logger.info("User interrupt"); |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 782 | OutErr.SYSTEM_OUT_ERR.printErrLn("Bazel received an interrupt"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 783 | mainThread.interrupt(); |
| 784 | |
| 785 | int curNumInterrupts = numInterrupts.incrementAndGet(); |
| 786 | if (curNumInterrupts == 1) { |
| 787 | Thread interruptWatcherThread = new Thread(interruptWatcher, "interrupt-watcher"); |
| 788 | interruptWatcherThread.setDaemon(true); |
| 789 | interruptWatcherThread.start(); |
| 790 | } else if (curNumInterrupts == 2) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 791 | logger.warning("Second --batch interrupt: Reverting to JVM SIGINT handler"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 792 | uninstall(); |
| 793 | } |
| 794 | } |
| 795 | }; |
| 796 | } |
| 797 | |
| 798 | /** |
| 799 | * A main method that runs blaze commands in batch mode. The return value indicates the desired |
| 800 | * exit status of the program. |
| 801 | */ |
| 802 | private static int batchMain(Iterable<BlazeModule> modules, String[] args) { |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 803 | InterruptSignalHandler signalHandler = captureSigint(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 804 | CommandLineOptions commandLineOptions = splitStartupOptions(modules, args); |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 805 | logger.info( |
Dan Fabulich | 1d35ca0 | 2018-07-05 16:08:06 -0700 | [diff] [blame] | 806 | "Running Bazel in batch mode with " |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 807 | + maybeGetPidString() |
| 808 | + "startup args " |
| 809 | + commandLineOptions.getStartupArgs()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 810 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 811 | BlazeRuntime runtime; |
ccalvarin | 0ddda78 | 2017-04-06 21:12:11 +0000 | [diff] [blame] | 812 | InvocationPolicy policy; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 813 | try { |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 814 | runtime = newRuntime(modules, commandLineOptions.getStartupArgs(), null); |
ccalvarin | 0ddda78 | 2017-04-06 21:12:11 +0000 | [diff] [blame] | 815 | policy = InvocationPolicyParser.parsePolicy( |
| 816 | runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class) |
| 817 | .invocationPolicy); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 818 | } catch (OptionsParsingException e) { |
ccalvarin | 0ddda78 | 2017-04-06 21:12:11 +0000 | [diff] [blame] | 819 | OutErr.SYSTEM_OUT_ERR.printErrLn(e.getMessage()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 820 | return ExitCode.COMMAND_LINE_ERROR.getNumericExitCode(); |
| 821 | } catch (AbruptExitException e) { |
ccalvarin | 0ddda78 | 2017-04-06 21:12:11 +0000 | [diff] [blame] | 822 | OutErr.SYSTEM_OUT_ERR.printErrLn(e.getMessage()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 823 | return e.getExitCode().getNumericExitCode(); |
| 824 | } |
| 825 | |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 826 | ImmutableList.Builder<Pair<String, String>> startupOptionsFromCommandLine = |
| 827 | ImmutableList.builder(); |
| 828 | for (String option : commandLineOptions.getStartupArgs()) { |
| 829 | startupOptionsFromCommandLine.add(new Pair<>("", option)); |
| 830 | } |
| 831 | |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 832 | BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime); |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 833 | boolean shutdownDone = false; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 834 | |
| 835 | try { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 836 | logger.info(getRequestLogString(commandLineOptions.getOtherArgs())); |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 837 | BlazeCommandResult result = dispatcher.exec( |
ccalvarin | 1cbe62a | 2017-08-14 21:09:07 +0200 | [diff] [blame] | 838 | policy, |
| 839 | commandLineOptions.getOtherArgs(), |
| 840 | OutErr.SYSTEM_OUT_ERR, |
| 841 | LockingMode.ERROR_OUT, |
| 842 | "batch client", |
| 843 | runtime.getClock().currentTimeMillis(), |
| 844 | Optional.of(startupOptionsFromCommandLine.build())); |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 845 | if (result.getExecRequest() == null) { |
| 846 | // Simple case: we are given an exit code |
| 847 | return result.getExitCode().getNumericExitCode(); |
| 848 | } |
| 849 | |
| 850 | // Not so simple case: we need to execute a binary on shutdown. exec() is not accessible from |
| 851 | // Java and is impossible on Windows in any case, so we just execute the binary after getting |
| 852 | // out of the way as completely as possible and forward its exit code. |
| 853 | // When this code is executed, no locks are held: the client lock is released by the client |
| 854 | // before it executes any command and the server lock is handled by BlazeCommandDispatcher, |
| 855 | // whose job is done by the time we get here. |
| 856 | runtime.shutdown(); |
| 857 | dispatcher.shutdown(); |
| 858 | shutdownDone = true; |
| 859 | signalHandler.uninstall(); |
| 860 | ExecRequest request = result.getExecRequest(); |
| 861 | String[] argv = new String[request.getArgvCount()]; |
| 862 | for (int i = 0; i < argv.length; i++) { |
| 863 | argv[i] = request.getArgv(i).toString(StandardCharsets.ISO_8859_1); |
| 864 | } |
| 865 | |
| 866 | String workingDirectory = request.getWorkingDirectory().toString(StandardCharsets.ISO_8859_1); |
| 867 | try { |
| 868 | ProcessBuilder process = new ProcessBuilder() |
| 869 | .command(argv) |
| 870 | .directory(new File(workingDirectory)) |
| 871 | .inheritIO(); |
| 872 | |
| 873 | for (int i = 0; i < request.getEnvironmentVariableCount(); i++) { |
| 874 | EnvironmentVariable variable = request.getEnvironmentVariable(i); |
| 875 | process.environment().put(variable.getName().toString(StandardCharsets.ISO_8859_1), |
| 876 | variable.getValue().toString(StandardCharsets.ISO_8859_1)); |
| 877 | } |
| 878 | |
| 879 | return process.start().waitFor(); |
| 880 | } catch (IOException e) { |
| 881 | // We are in batch mode, thus, stdout/stderr are the same as that of the client. |
| 882 | System.err.println("Cannot execute process for 'run' command: " + e.getMessage()); |
| 883 | logger.log(Level.SEVERE, "Exception while executing binary from 'run' command", e); |
| 884 | return ExitCode.LOCAL_ENVIRONMENTAL_ERROR.getNumericExitCode(); |
| 885 | } |
Lukacs Berki | ce1445f | 2016-04-19 15:52:55 +0000 | [diff] [blame] | 886 | } catch (InterruptedException e) { |
| 887 | // This is almost main(), so it's okay to just swallow it. We are exiting soon. |
| 888 | return ExitCode.INTERRUPTED.getNumericExitCode(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 889 | } finally { |
lberki | 4741aa8 | 2018-02-05 07:59:13 -0800 | [diff] [blame] | 890 | if (!shutdownDone) { |
| 891 | runtime.shutdown(); |
| 892 | dispatcher.shutdown(); |
| 893 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 894 | } |
| 895 | } |
| 896 | |
| 897 | /** |
| 898 | * A main method that does not send email. The return value indicates the desired exit status of |
| 899 | * the program. |
| 900 | */ |
| 901 | private static int serverMain(Iterable<BlazeModule> modules, OutErr outErr, String[] args) { |
Lukacs Berki | f763379 | 2016-04-21 12:38:29 +0000 | [diff] [blame] | 902 | InterruptSignalHandler sigintHandler = null; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 903 | try { |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 904 | final RPCServer[] rpcServer = new RPCServer[1]; |
| 905 | Runnable prepareForAbruptShutdown = () -> rpcServer[0].prepareForAbruptShutdown(); |
| 906 | BlazeRuntime runtime = newRuntime(modules, Arrays.asList(args), prepareForAbruptShutdown); |
| 907 | BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime); |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 908 | BlazeServerStartupOptions startupOptions = |
| 909 | runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class); |
| 910 | try { |
| 911 | // This is necessary so that Bazel kind of works during bootstrapping, at which time the |
| 912 | // gRPC server is not compiled in so that we don't need gRPC for bootstrapping. |
| 913 | Class<?> factoryClass = Class.forName( |
| 914 | "com.google.devtools.build.lib.server.GrpcServerImpl$Factory"); |
| 915 | RPCServer.Factory factory = (RPCServer.Factory) factoryClass.getConstructor().newInstance(); |
lberki | bbcf203 | 2018-02-06 10:11:56 -0800 | [diff] [blame] | 916 | rpcServer[0] = factory.create(dispatcher, runtime.getClock(), |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 917 | startupOptions.commandPort, |
| 918 | runtime.getWorkspace().getWorkspace(), |
| 919 | runtime.getServerDirectory(), |
| 920 | startupOptions.maxIdleSeconds); |
| 921 | } catch (ReflectiveOperationException | IllegalArgumentException e) { |
| 922 | throw new AbruptExitException("gRPC server not compiled in", ExitCode.BLAZE_INTERNAL_ERROR); |
| 923 | } |
Lukacs Berki | f763379 | 2016-04-21 12:38:29 +0000 | [diff] [blame] | 924 | |
| 925 | // Register the signal handler. |
Janak Ramakrishnan | 7be337f | 2017-01-05 20:21:35 +0000 | [diff] [blame] | 926 | sigintHandler = |
| 927 | new InterruptSignalHandler() { |
| 928 | @Override |
| 929 | public void run() { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 930 | logger.severe("User interrupt"); |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 931 | rpcServer[0].interrupt(); |
Janak Ramakrishnan | 7be337f | 2017-01-05 20:21:35 +0000 | [diff] [blame] | 932 | } |
| 933 | }; |
Lukacs Berki | f763379 | 2016-04-21 12:38:29 +0000 | [diff] [blame] | 934 | |
lberki | ac8eb42 | 2018-02-06 09:27:30 -0800 | [diff] [blame] | 935 | rpcServer[0].serve(); |
| 936 | runtime.shutdown(); |
| 937 | dispatcher.shutdown(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 938 | return ExitCode.SUCCESS.getNumericExitCode(); |
| 939 | } catch (OptionsParsingException e) { |
| 940 | outErr.printErr(e.getMessage()); |
| 941 | return ExitCode.COMMAND_LINE_ERROR.getNumericExitCode(); |
| 942 | } catch (IOException e) { |
| 943 | outErr.printErr("I/O Error: " + e.getMessage()); |
| 944 | return ExitCode.BUILD_FAILURE.getNumericExitCode(); |
| 945 | } catch (AbruptExitException e) { |
| 946 | outErr.printErr(e.getMessage()); |
| 947 | return e.getExitCode().getNumericExitCode(); |
Lukacs Berki | f763379 | 2016-04-21 12:38:29 +0000 | [diff] [blame] | 948 | } finally { |
| 949 | if (sigintHandler != null) { |
| 950 | sigintHandler.uninstall(); |
| 951 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 952 | } |
| 953 | } |
| 954 | |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 955 | private static FileSystem defaultFileSystemImplementation() { |
Lukacs Berki | 2821972 | 2016-06-30 12:34:39 +0000 | [diff] [blame] | 956 | if ("0".equals(System.getProperty("io.bazel.EnableJni"))) { |
Damien Martin-Guillerez | 8c04e9e | 2016-01-18 12:53:39 +0000 | [diff] [blame] | 957 | // Ignore UnixFileSystem, to be used for bootstrapping. |
Dmitry Lomov | be93951 | 2016-02-02 20:26:25 +0000 | [diff] [blame] | 958 | return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new JavaIoFileSystem(); |
Damien Martin-Guillerez | 8c04e9e | 2016-01-18 12:53:39 +0000 | [diff] [blame] | 959 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 960 | // The JNI-based UnixFileSystem is faster, but on Windows it is not available. |
Lukacs Berki | 58dbb7f | 2016-01-29 11:56:29 +0000 | [diff] [blame] | 961 | return OS.getCurrent() == OS.WINDOWS ? new WindowsFileSystem() : new UnixFileSystem(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 962 | } |
| 963 | |
ulfjack | 27758e4 | 2017-09-07 13:41:33 +0200 | [diff] [blame] | 964 | private static SubprocessFactory subprocessFactoryImplementation() { |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 965 | if (!"0".equals(System.getProperty("io.bazel.EnableJni")) && OS.getCurrent() == OS.WINDOWS) { |
| 966 | return WindowsSubprocessFactory.INSTANCE; |
| 967 | } else { |
| 968 | return JavaSubprocessFactory.INSTANCE; |
| 969 | } |
| 970 | } |
| 971 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 972 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 973 | * Parses the command line arguments into a {@link OptionsParser} object. |
| 974 | * |
ccalvarin | 7a491b7 | 2018-05-16 09:13:27 -0700 | [diff] [blame] | 975 | * <p>This function needs to parse the --option_sources option manually so that the real option |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 976 | * parser can set the source for every option correctly. If that cannot be parsed or is missing, |
| 977 | * we just report an unknown source for every startup option. |
| 978 | */ |
ccalvarin | 7a491b7 | 2018-05-16 09:13:27 -0700 | [diff] [blame] | 979 | private static OptionsProvider parseStartupOptions( |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 980 | Iterable<BlazeModule> modules, List<String> args) throws OptionsParsingException { |
Ulf Adams | 7e71f89 | 2016-08-10 09:06:50 +0000 | [diff] [blame] | 981 | ImmutableList<Class<? extends OptionsBase>> optionClasses = |
| 982 | BlazeCommandUtils.getStartupOptions(modules); |
| 983 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 984 | // First parse the command line so that we get the option_sources argument |
| 985 | OptionsParser parser = OptionsParser.newOptionsParser(optionClasses); |
| 986 | parser.setAllowResidue(false); |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 987 | parser.parse(PriorityCategory.COMMAND_LINE, null, args); |
lberki | 6a8a174 | 2017-06-29 10:09:42 +0200 | [diff] [blame] | 988 | Map<String, String> optionSources = |
| 989 | parser.getOptions(BlazeServerStartupOptions.class).optionSources; |
ccalvarin | 1dce097 | 2017-09-11 20:03:02 +0200 | [diff] [blame] | 990 | Function<OptionDefinition, String> sourceFunction = |
| 991 | option -> |
| 992 | !optionSources.containsKey(option.getOptionName()) |
| 993 | ? "default" |
| 994 | : optionSources.get(option.getOptionName()).isEmpty() |
| 995 | ? "command line" |
| 996 | : optionSources.get(option.getOptionName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 997 | |
| 998 | // Then parse the command line again, this time with the correct option sources |
| 999 | parser = OptionsParser.newOptionsParser(optionClasses); |
| 1000 | parser.setAllowResidue(false); |
ccalvarin | 7cd9e88 | 2017-10-16 22:18:32 +0200 | [diff] [blame] | 1001 | parser.parseWithSourceFunction(PriorityCategory.COMMAND_LINE, sourceFunction, args); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1002 | return parser; |
| 1003 | } |
| 1004 | |
| 1005 | /** |
| 1006 | * Creates a new blaze runtime, given the install and output base directories. |
| 1007 | * |
| 1008 | * <p>Note: This method can and should only be called once per startup, as it also creates the |
| 1009 | * filesystem object that will be used for the runtime. So it should only ever be called from the |
| 1010 | * main method of the Blaze program. |
| 1011 | * |
Ulf Adams | 7db73d3 | 2016-08-10 12:41:02 +0000 | [diff] [blame] | 1012 | * @param args Blaze startup options. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1013 | * |
| 1014 | * @return a new BlazeRuntime instance initialized with the given filesystem and directories, and |
| 1015 | * an error string that, if not null, describes a fatal initialization failure that makes |
| 1016 | * this runtime unsuitable for real commands |
| 1017 | */ |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 1018 | private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<String> args, |
| 1019 | Runnable abruptShutdownHandler) |
Ulf Adams | 7db73d3 | 2016-08-10 12:41:02 +0000 | [diff] [blame] | 1020 | throws AbruptExitException, OptionsParsingException { |
ccalvarin | 7a491b7 | 2018-05-16 09:13:27 -0700 | [diff] [blame] | 1021 | OptionsProvider options = parseStartupOptions(blazeModules, args); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1022 | for (BlazeModule module : blazeModules) { |
| 1023 | module.globalInit(options); |
| 1024 | } |
| 1025 | |
| 1026 | BlazeServerStartupOptions startupOptions = options.getOptions(BlazeServerStartupOptions.class); |
Ulf Adams | b5a06f33 | 2016-06-21 14:55:49 +0000 | [diff] [blame] | 1027 | String productName = startupOptions.productName.toLowerCase(Locale.US); |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 1028 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1029 | PathFragment workspaceDirectory = startupOptions.workspaceDirectory; |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 1030 | PathFragment defaultSystemJavabase = startupOptions.defaultSystemJavabase; |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 1031 | PathFragment outputUserRoot = startupOptions.outputUserRoot; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1032 | PathFragment installBase = startupOptions.installBase; |
| 1033 | PathFragment outputBase = startupOptions.outputBase; |
| 1034 | |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 1035 | maybeForceJNIByGettingPid(installBase); // Must be before first use of JNI. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1036 | |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 1037 | // From the point of view of the Java program --install_base, --output_base, and |
| 1038 | // --output_user_root are mandatory options, despite the comment in their declarations. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1039 | if (installBase == null || !installBase.isAbsolute()) { // (includes "" default case) |
| 1040 | throw new IllegalArgumentException( |
| 1041 | "Bad --install_base option specified: '" + installBase + "'"); |
| 1042 | } |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 1043 | if (outputUserRoot != null && !outputUserRoot.isAbsolute()) { // (includes "" default case) |
| 1044 | throw new IllegalArgumentException( |
| 1045 | "Bad --output_user_root option specified: '" + outputUserRoot + "'"); |
| 1046 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1047 | if (outputBase != null && !outputBase.isAbsolute()) { // (includes "" default case) |
| 1048 | throw new IllegalArgumentException( |
| 1049 | "Bad --output_base option specified: '" + outputBase + "'"); |
| 1050 | } |
| 1051 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1052 | FileSystem fs = null; |
| 1053 | for (BlazeModule module : blazeModules) { |
Ulf Adams | 124bd0a | 2016-08-10 13:10:46 +0000 | [diff] [blame] | 1054 | FileSystem moduleFs = module.getFileSystem(options); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1055 | if (moduleFs != null) { |
| 1056 | Preconditions.checkState(fs == null, "more than one module returns a file system"); |
| 1057 | fs = moduleFs; |
| 1058 | } |
| 1059 | } |
| 1060 | |
| 1061 | if (fs == null) { |
ccalvarin | fb3293c | 2017-09-26 11:13:33 -0400 | [diff] [blame] | 1062 | fs = defaultFileSystemImplementation(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1063 | } |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 1064 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1065 | Path.setFileSystemForSerialization(fs); |
Lukacs Berki | 74dcfee | 2016-07-04 12:57:25 +0000 | [diff] [blame] | 1066 | SubprocessBuilder.setSubprocessFactory(subprocessFactoryImplementation()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1067 | |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 1068 | Path outputUserRootPath = fs.getPath(outputUserRoot); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1069 | Path installBasePath = fs.getPath(installBase); |
| 1070 | Path outputBasePath = fs.getPath(outputBase); |
| 1071 | Path workspaceDirectoryPath = null; |
| 1072 | if (!workspaceDirectory.equals(PathFragment.EMPTY_FRAGMENT)) { |
| 1073 | workspaceDirectoryPath = fs.getPath(workspaceDirectory); |
| 1074 | } |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 1075 | Path defaultSystemJavabasePath = null; |
| 1076 | if (!defaultSystemJavabase.equals(PathFragment.EMPTY_FRAGMENT)) { |
| 1077 | defaultSystemJavabasePath = fs.getPath(defaultSystemJavabase); |
| 1078 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1079 | |
Ulf Adams | 15a23b9 | 2016-08-09 11:46:00 +0000 | [diff] [blame] | 1080 | ServerDirectories serverDirectories = |
Klaus Aehlig | c2499c4 | 2018-02-27 05:47:21 -0800 | [diff] [blame] | 1081 | new ServerDirectories( |
| 1082 | installBasePath, outputBasePath, outputUserRootPath, startupOptions.installMD5); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1083 | Clock clock = BlazeClock.instance(); |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1084 | BlazeRuntime.Builder runtimeBuilder = |
| 1085 | new BlazeRuntime.Builder() |
| 1086 | .setProductName(productName) |
| 1087 | .setFileSystem(fs) |
| 1088 | .setServerDirectories(serverDirectories) |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1089 | .setActionKeyContext(new ActionKeyContext()) |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1090 | .setStartupOptionsProvider(options) |
| 1091 | .setClock(clock) |
| 1092 | .setAbruptShutdownHandler(abruptShutdownHandler) |
| 1093 | // TODO(bazel-team): Make BugReportingExceptionHandler the default. |
| 1094 | // See bug "Make exceptions in EventBus subscribers fatal" |
| 1095 | .setEventBusExceptionHandler( |
| 1096 | startupOptions.fatalEventBusExceptions |
| 1097 | || !BlazeVersionInfo.instance().isReleasedBlaze() |
| 1098 | ? new BlazeRuntime.BugReportingExceptionHandler() |
| 1099 | : new BlazeRuntime.RemoteExceptionHandler()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1100 | |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 1101 | if (System.getenv("TEST_TMPDIR") != null |
| 1102 | && System.getenv("NO_CRASH_ON_LOGGING_IN_TEST") == null) { |
| 1103 | LoggingUtil.installRemoteLogger(getTestCrashLogger()); |
| 1104 | } |
| 1105 | |
ulfjack | 9274cba | 2017-08-11 23:19:48 +0200 | [diff] [blame] | 1106 | // This module needs to be registered before any module providing a SpawnCache implementation. |
| 1107 | runtimeBuilder.addBlazeModule(new NoSpawnCacheModule()); |
ulfjack | 236635a | 2017-06-26 09:25:52 +0200 | [diff] [blame] | 1108 | runtimeBuilder.addBlazeModule(new CommandLogModule()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1109 | for (BlazeModule blazeModule : blazeModules) { |
| 1110 | runtimeBuilder.addBlazeModule(blazeModule); |
| 1111 | } |
| 1112 | |
| 1113 | BlazeRuntime runtime = runtimeBuilder.build(); |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1114 | |
| 1115 | BlazeDirectories directories = |
cushon | 849df36 | 2018-05-14 01:51:45 -0700 | [diff] [blame] | 1116 | new BlazeDirectories( |
| 1117 | serverDirectories, workspaceDirectoryPath, defaultSystemJavabasePath, productName); |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1118 | BinTools binTools; |
| 1119 | try { |
| 1120 | binTools = BinTools.forProduction(directories); |
| 1121 | } catch (IOException e) { |
| 1122 | throw new AbruptExitException( |
| 1123 | "Cannot enumerate embedded binaries: " + e.getMessage(), |
| 1124 | ExitCode.LOCAL_ENVIRONMENTAL_ERROR); |
| 1125 | } |
| 1126 | runtime.initWorkspace(directories, binTools); |
janakr | 5bfe343 | 2018-04-20 14:45:13 -0700 | [diff] [blame] | 1127 | CustomExitCodePublisher.setAbruptExitStatusFileDir( |
| 1128 | serverDirectories.getOutputBase().getPathString()); |
ccalvarin | 1419dda | 2017-03-28 21:12:28 +0000 | [diff] [blame] | 1129 | |
brandjon | 73f1a0a | 2018-03-06 14:19:47 -0800 | [diff] [blame] | 1130 | // Most static initializers for @SkylarkSignature-containing classes have already run by this |
| 1131 | // point, but this will pick up the stragglers. |
| 1132 | initSkylarkBuiltinsRegistry(); |
| 1133 | |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 1134 | AutoProfiler.setClock(runtime.getClock()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1135 | BugReport.setRuntime(runtime); |
| 1136 | return runtime; |
| 1137 | } |
| 1138 | |
brandjon | 73f1a0a | 2018-03-06 14:19:47 -0800 | [diff] [blame] | 1139 | /** |
| 1140 | * Configures the Skylark builtins registry. |
| 1141 | * |
| 1142 | * <p>Any class containing {@link SkylarkSignature}-annotated fields should call |
| 1143 | * {@link SkylarkSignatureProcessor#configureSkylarkFunctions} on itself. This serves two |
| 1144 | * purposes: 1) it initializes those fields for use, and 2) it registers them with the Skylark |
| 1145 | * builtins registry object |
| 1146 | * ({@link com.google.devtools.build.lib.syntax.Runtime#getBuiltinRegistry}). Unfortunately |
| 1147 | * there's some technical debt here: The registry object is static and the registration occurs |
| 1148 | * inside static initializer blocks. |
| 1149 | * |
| 1150 | * <p>The registry supports concurrent read/write access, but read access is not actually |
| 1151 | * efficient (lockless) until write access is disallowed by calling its |
| 1152 | * {@link com.google.devtools.build.lib.syntax.Runtime.BuiltinRegistry#freeze freeze} method. |
| 1153 | * We want to freeze before the build begins, but not before all classes have had a chance to run |
| 1154 | * their static initializers. |
| 1155 | * |
| 1156 | * <p>Therefore, this method first ensures that the initializers have run, and then explicitly |
| 1157 | * freezes the registry. It ensures initialization by calling a no-op static method on the class. |
| 1158 | * Only classes whose initializers have been observed to cause {@code BuiltinRegistry} to throw an |
| 1159 | * exception need to be included here, since that indicates that their initialization did not |
| 1160 | * happen by this point in time. |
| 1161 | * |
| 1162 | * <p>Unit tests don't need to worry about registry freeze exceptions, since the registry isn't |
| 1163 | * frozen at all for them. They just pay the cost of extra synchronization on every access. |
| 1164 | */ |
| 1165 | private static void initSkylarkBuiltinsRegistry() { |
cparsons | 2c81467 | 2018-03-22 09:07:25 -0700 | [diff] [blame] | 1166 | // Currently no classes need to be initialized here. The hook's still here because it's |
| 1167 | // possible it may be needed again in the future. |
brandjon | 73f1a0a | 2018-03-06 14:19:47 -0800 | [diff] [blame] | 1168 | com.google.devtools.build.lib.syntax.Runtime.getBuiltinRegistry().freeze(); |
| 1169 | } |
| 1170 | |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 1171 | private static String maybeGetPidString() { |
| 1172 | Integer pid = maybeForceJNIByGettingPid(null); |
| 1173 | return pid == null ? "" : "pid " + pid + " and "; |
| 1174 | } |
| 1175 | |
| 1176 | /** Loads JNI libraries, if necessary under the current platform. */ |
| 1177 | @Nullable |
| 1178 | private static Integer maybeForceJNIByGettingPid(@Nullable PathFragment installBase) { |
| 1179 | return jniLibsAvailable() ? getPidUsingJNI(installBase) : null; |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | private static boolean jniLibsAvailable() { |
| 1183 | return !"0".equals(System.getProperty("io.bazel.EnableJni")); |
| 1184 | } |
| 1185 | |
| 1186 | // Force JNI linking at a moment when we have 'installBase' handy, and print |
| 1187 | // an informative error if it fails. |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 1188 | private static int getPidUsingJNI(@Nullable PathFragment installBase) { |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 1189 | try { |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 1190 | return ProcessUtils.getpid(); // force JNI initialization |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 1191 | } catch (UnsatisfiedLinkError t) { |
janakr | 1cb9a01 | 2017-03-28 20:18:21 +0000 | [diff] [blame] | 1192 | System.err.println( |
| 1193 | "JNI initialization failed: " |
| 1194 | + t.getMessage() |
| 1195 | + ". " |
| 1196 | + "Possibly your installation has been corrupted" |
| 1197 | + (installBase == null |
| 1198 | ? "" |
| 1199 | : "; if this problem persists, try 'rm -fr " + installBase + "'") |
| 1200 | + "."); |
Ulf Adams | 8afbd3c | 2017-02-28 10:42:48 +0000 | [diff] [blame] | 1201 | throw t; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /** |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 1206 | * Returns a logger that crashes as soon as it's written to, since tests should not cause events |
| 1207 | * that would be logged. |
| 1208 | */ |
| 1209 | @VisibleForTesting |
| 1210 | public static Future<Logger> getTestCrashLogger() { |
| 1211 | Logger crashLogger = Logger.getAnonymousLogger(); |
| 1212 | crashLogger.addHandler( |
| 1213 | new Handler() { |
| 1214 | @Override |
| 1215 | public void publish(LogRecord record) { |
Michajlo Matijkiw | f5cc0b2 | 2016-01-20 17:44:44 +0000 | [diff] [blame] | 1216 | System.err.println("Remote logging disabled for testing, forcing abrupt shutdown."); |
| 1217 | System.err.printf("%s#%s: %s\n", |
| 1218 | record.getSourceClassName(), |
| 1219 | record.getSourceMethodName(), |
| 1220 | record.getMessage()); |
| 1221 | |
Janak Ramakrishnan | a88e65b | 2015-09-17 15:07:26 +0000 | [diff] [blame] | 1222 | Throwable e = record.getThrown(); |
Michajlo Matijkiw | f5cc0b2 | 2016-01-20 17:44:44 +0000 | [diff] [blame] | 1223 | if (e != null) { |
| 1224 | e.printStackTrace(); |
Janak Ramakrishnan | a88e65b | 2015-09-17 15:07:26 +0000 | [diff] [blame] | 1225 | } |
Michajlo Matijkiw | f5cc0b2 | 2016-01-20 17:44:44 +0000 | [diff] [blame] | 1226 | |
| 1227 | Runtime.getRuntime().halt(ExitCode.BLAZE_INTERNAL_ERROR.getNumericExitCode()); |
Janak Ramakrishnan | ae9b95f | 2015-08-04 03:47:04 +0000 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | @Override |
| 1231 | public void flush() { |
| 1232 | throw new IllegalStateException(); |
| 1233 | } |
| 1234 | |
| 1235 | @Override |
| 1236 | public void close() { |
| 1237 | throw new IllegalStateException(); |
| 1238 | } |
| 1239 | }); |
| 1240 | return Futures.immediateFuture(crashLogger); |
| 1241 | } |
| 1242 | |
| 1243 | /** |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1244 | * Make sure async threads cannot be orphaned. This method makes sure bugs are reported to |
| 1245 | * telemetry and the proper exit code is reported. |
| 1246 | */ |
| 1247 | private static void setupUncaughtHandler(final String[] args) { |
Janak Ramakrishnan | b6582fa | 2016-03-14 16:19:40 +0000 | [diff] [blame] | 1248 | Thread.setDefaultUncaughtExceptionHandler( |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 1249 | (thread, throwable) -> BugReport.handleCrash(throwable, args)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1250 | } |
| 1251 | |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 1252 | public String getProductName() { |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 1253 | return productName; |
Luis Fernando Pino Duque | be10218 | 2016-05-23 14:03:55 +0000 | [diff] [blame] | 1254 | } |
| 1255 | |
buchgr | 35ff63a | 2018-07-09 05:55:40 -0700 | [diff] [blame^] | 1256 | public BuildEventArtifactUploaderFactoryMap getBuildEventArtifactUploaderFactoryMap() { |
| 1257 | return buildEventArtifactUploaderFactoryMap; |
Klaus Aehlig | dadde6f | 2017-01-18 17:32:07 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1260 | /** |
| 1261 | * A builder for {@link BlazeRuntime} objects. The only required fields are the {@link |
| 1262 | * BlazeDirectories}, and the {@link RuleClassProvider} (except for testing). All other fields |
| 1263 | * have safe default values. |
| 1264 | * |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1265 | * <p>The default behavior of the BlazeRuntime's EventBus is to exit when a subscriber throws |
| 1266 | * an exception. Please plan appropriately. |
| 1267 | */ |
| 1268 | public static class Builder { |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1269 | private FileSystem fileSystem; |
Ulf Adams | 15a23b9 | 2016-08-09 11:46:00 +0000 | [diff] [blame] | 1270 | private ServerDirectories serverDirectories; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1271 | private Clock clock; |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 1272 | private Runnable abruptShutdownHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1273 | private OptionsProvider startupOptionsProvider; |
Ulf Adams | 47cb916 | 2015-09-18 08:12:30 +0000 | [diff] [blame] | 1274 | private final List<BlazeModule> blazeModules = new ArrayList<>(); |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 1275 | private SubscriberExceptionHandler eventBusExceptionHandler = new RemoteExceptionHandler(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1276 | private UUID instanceId; |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 1277 | private String productName; |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1278 | private ActionKeyContext actionKeyContext; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1279 | |
| 1280 | public BlazeRuntime build() throws AbruptExitException { |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 1281 | Preconditions.checkNotNull(productName); |
Ulf Adams | 15a23b9 | 2016-08-09 11:46:00 +0000 | [diff] [blame] | 1282 | Preconditions.checkNotNull(serverDirectories); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1283 | Preconditions.checkNotNull(startupOptionsProvider); |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1284 | ActionKeyContext actionKeyContext = |
| 1285 | this.actionKeyContext != null ? this.actionKeyContext : new ActionKeyContext(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1286 | Clock clock = (this.clock == null) ? BlazeClock.instance() : this.clock; |
| 1287 | UUID instanceId = (this.instanceId == null) ? UUID.randomUUID() : this.instanceId; |
| 1288 | |
| 1289 | Preconditions.checkNotNull(clock); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1290 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1291 | for (BlazeModule module : blazeModules) { |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1292 | module.blazeStartup( |
| 1293 | startupOptionsProvider, |
| 1294 | BlazeVersionInfo.instance(), |
| 1295 | instanceId, |
| 1296 | fileSystem, |
| 1297 | serverDirectories, |
| 1298 | clock); |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 1299 | } |
Ulf Adams | 345e15e | 2016-07-07 13:27:28 +0000 | [diff] [blame] | 1300 | ServerBuilder serverBuilder = new ServerBuilder(); |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 1301 | serverBuilder.addQueryOutputFormatters(OutputFormatter.getDefaultFormatters()); |
Ulf Adams | 8b56c8c | 2016-04-12 13:45:56 +0000 | [diff] [blame] | 1302 | for (BlazeModule module : blazeModules) { |
Ulf Adams | 345e15e | 2016-07-07 13:27:28 +0000 | [diff] [blame] | 1303 | module.serverInit(startupOptionsProvider, serverBuilder); |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 1304 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1305 | |
| 1306 | ConfiguredRuleClassProvider.Builder ruleClassBuilder = |
| 1307 | new ConfiguredRuleClassProvider.Builder(); |
| 1308 | for (BlazeModule module : blazeModules) { |
| 1309 | module.initializeRuleClasses(ruleClassBuilder); |
| 1310 | } |
| 1311 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1312 | ConfiguredRuleClassProvider ruleClassProvider = ruleClassBuilder.build(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1313 | |
Nathan Harmata | 42fb560 | 2016-05-25 20:32:08 +0000 | [diff] [blame] | 1314 | Package.Builder.Helper packageBuilderHelper = null; |
| 1315 | for (BlazeModule module : blazeModules) { |
nharmata | e57e9a3 | 2018-04-02 15:10:24 -0700 | [diff] [blame] | 1316 | Package.Builder.Helper candidateHelper = module.getPackageBuilderHelper(ruleClassProvider); |
Nathan Harmata | 42fb560 | 2016-05-25 20:32:08 +0000 | [diff] [blame] | 1317 | if (candidateHelper != null) { |
| 1318 | Preconditions.checkState(packageBuilderHelper == null, |
| 1319 | "more than one module defines a package builder helper"); |
| 1320 | packageBuilderHelper = candidateHelper; |
| 1321 | } |
| 1322 | } |
| 1323 | if (packageBuilderHelper == null) { |
| 1324 | packageBuilderHelper = Package.Builder.DefaultHelper.INSTANCE; |
| 1325 | } |
| 1326 | |
Janak Ramakrishnan | 0249841 | 2016-05-11 22:28:43 +0000 | [diff] [blame] | 1327 | PackageFactory packageFactory = |
| 1328 | new PackageFactory( |
| 1329 | ruleClassProvider, |
Ulf Adams | 345e15e | 2016-07-07 13:27:28 +0000 | [diff] [blame] | 1330 | serverBuilder.getAttributeContainerFactory(), |
Ulf Adams | ed5be45 | 2016-11-02 16:33:57 +0000 | [diff] [blame] | 1331 | serverBuilder.getEnvironmentExtensions(), |
Nathan Harmata | 42fb560 | 2016-05-25 20:32:08 +0000 | [diff] [blame] | 1332 | BlazeVersionInfo.instance().getVersion(), |
| 1333 | packageBuilderHelper); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1334 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1335 | ProjectFile.Provider projectFileProvider = null; |
| 1336 | for (BlazeModule module : blazeModules) { |
| 1337 | ProjectFile.Provider candidate = module.createProjectFileProvider(); |
| 1338 | if (candidate != null) { |
| 1339 | Preconditions.checkState(projectFileProvider == null, |
| 1340 | "more than one module defines a project file provider"); |
| 1341 | projectFileProvider = candidate; |
| 1342 | } |
| 1343 | } |
| 1344 | |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1345 | return new BlazeRuntime( |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1346 | fileSystem, |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1347 | serverBuilder.getQueryEnvironmentFactory(), |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 1348 | serverBuilder.getQueryFunctions(), |
Ulf Adams | 54eeac5 | 2016-08-29 09:07:35 +0000 | [diff] [blame] | 1349 | serverBuilder.getQueryOutputFormatters(), |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1350 | packageFactory, |
| 1351 | ruleClassProvider, |
gregce | d79b937 | 2017-08-10 21:51:15 +0200 | [diff] [blame] | 1352 | ruleClassProvider.getConfigurationFragments(), |
Ulf Adams | 6bfd2c5 | 2016-08-29 08:00:39 +0000 | [diff] [blame] | 1353 | serverBuilder.getInfoItems(), |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1354 | actionKeyContext, |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1355 | clock, |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 1356 | abruptShutdownHandler, |
Ulf Adams | ea46dfc | 2016-08-10 08:44:19 +0000 | [diff] [blame] | 1357 | startupOptionsProvider, |
| 1358 | ImmutableList.copyOf(blazeModules), |
| 1359 | eventBusExceptionHandler, |
| 1360 | projectFileProvider, |
| 1361 | serverBuilder.getInvocationPolicy(), |
| 1362 | serverBuilder.getCommands(), |
Klaus Aehlig | dadde6f | 2017-01-18 17:32:07 +0000 | [diff] [blame] | 1363 | productName, |
ulfjack | 68aa410 | 2018-06-15 01:40:02 -0700 | [diff] [blame] | 1364 | serverBuilder.getBuildEventArtifactUploaderMap()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1365 | } |
| 1366 | |
Luis Fernando Pino Duque | 623cdf8 | 2016-05-31 16:21:46 +0000 | [diff] [blame] | 1367 | public Builder setProductName(String productName) { |
| 1368 | this.productName = productName; |
| 1369 | return this; |
| 1370 | } |
| 1371 | |
tomlu | f903eb5 | 2017-10-27 12:12:11 -0400 | [diff] [blame] | 1372 | public Builder setFileSystem(FileSystem fileSystem) { |
| 1373 | this.fileSystem = fileSystem; |
| 1374 | return this; |
| 1375 | } |
| 1376 | |
Ulf Adams | 15a23b9 | 2016-08-09 11:46:00 +0000 | [diff] [blame] | 1377 | public Builder setServerDirectories(ServerDirectories serverDirectories) { |
| 1378 | this.serverDirectories = serverDirectories; |
| 1379 | return this; |
| 1380 | } |
| 1381 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1382 | public Builder setClock(Clock clock) { |
| 1383 | this.clock = clock; |
| 1384 | return this; |
| 1385 | } |
| 1386 | |
Lukacs Berki | 9f60395 | 2017-01-13 14:04:08 +0000 | [diff] [blame] | 1387 | public Builder setAbruptShutdownHandler(Runnable handler) { |
| 1388 | this.abruptShutdownHandler = handler; |
| 1389 | return this; |
| 1390 | } |
| 1391 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1392 | public Builder setStartupOptionsProvider(OptionsProvider startupOptionsProvider) { |
| 1393 | this.startupOptionsProvider = startupOptionsProvider; |
| 1394 | return this; |
| 1395 | } |
| 1396 | |
| 1397 | public Builder addBlazeModule(BlazeModule blazeModule) { |
| 1398 | blazeModules.add(blazeModule); |
| 1399 | return this; |
| 1400 | } |
| 1401 | |
| 1402 | public Builder setInstanceId(UUID id) { |
| 1403 | instanceId = id; |
| 1404 | return this; |
| 1405 | } |
| 1406 | |
| 1407 | @VisibleForTesting |
| 1408 | public Builder setEventBusExceptionHandler( |
| 1409 | SubscriberExceptionHandler eventBusExceptionHandler) { |
| 1410 | this.eventBusExceptionHandler = eventBusExceptionHandler; |
| 1411 | return this; |
| 1412 | } |
tomlu | 3d1a194 | 2017-11-29 14:01:21 -0800 | [diff] [blame] | 1413 | |
| 1414 | public Builder setActionKeyContext(ActionKeyContext actionKeyContext) { |
| 1415 | this.actionKeyContext = actionKeyContext; |
| 1416 | return this; |
| 1417 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 1418 | } |
| 1419 | } |