blob: ada8367946e6199280f6a054d8066be3a4c24c89 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.runtime;
15
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016import com.google.common.collect.ImmutableList;
philwod3de5cc2018-04-16 06:40:19 -070017import com.google.devtools.build.lib.actions.ExecutorInitException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018import com.google.devtools.build.lib.analysis.BlazeDirectories;
19import com.google.devtools.build.lib.analysis.BlazeVersionInfo;
20import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
Ulf Adams15a23b92016-08-09 11:46:00 +000021import com.google.devtools.build.lib.analysis.ServerDirectories;
mjhalupka5d7fa7b2018-03-22 13:37:38 -070022import com.google.devtools.build.lib.analysis.config.BuildOptions;
ulfjackab21d182017-08-10 15:36:14 +020023import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory;
Ulf Adamsa0e3af42016-10-31 16:52:48 +000024import com.google.devtools.build.lib.buildtool.BuildRequest;
philwo3bcb9f62017-09-06 12:52:21 +020025import com.google.devtools.build.lib.clock.Clock;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000026import com.google.devtools.build.lib.cmdline.Label;
Ulf Adamsdba3c832016-12-21 16:50:02 +000027import com.google.devtools.build.lib.exec.ExecutorBuilder;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028import com.google.devtools.build.lib.packages.NoSuchThingException;
Nathan Harmata42fb5602016-05-25 20:32:08 +000029import com.google.devtools.build.lib.packages.Package;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030import com.google.devtools.build.lib.packages.PackageFactory;
Nathan Harmatacaa000a2016-06-07 17:46:19 +000031import com.google.devtools.build.lib.packages.RuleClassProvider;
ulfjackbc4eb272017-08-10 17:33:22 +020032import com.google.devtools.build.lib.skyframe.OutputService;
kchodorowdfcd5da82017-04-19 18:58:50 +020033import com.google.devtools.build.lib.skyframe.PrecomputedValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034import com.google.devtools.build.lib.util.AbruptExitException;
Klaus Aehligd000e0c2017-04-28 13:35:05 +020035import com.google.devtools.build.lib.util.io.OutErr;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036import com.google.devtools.build.lib.vfs.FileSystem;
37import com.google.devtools.build.lib.vfs.Path;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038import com.google.devtools.common.options.OptionsBase;
Ulf Adams33644a42016-08-10 11:32:41 +000039import com.google.devtools.common.options.OptionsClassProvider;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040import com.google.devtools.common.options.OptionsProvider;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010042import java.util.UUID;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010043import javax.annotation.Nullable;
44
45/**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000046 * A module Bazel can load at the beginning of its execution. Modules are supplied with extension
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010047 * points to augment the functionality at specific, well-defined places.
48 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +000049 * <p>The constructors of individual Bazel modules should be empty. All work should be done in the
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 * methods (e.g. {@link #blazeStartup}).
51 */
52public abstract class BlazeModule {
53
54 /**
55 * Returns the extra startup options this module contributes.
56 *
Ulf Adams124bd0a2016-08-10 13:10:46 +000057 * <p>This method will be called at the beginning of Blaze startup (before {@link #globalInit}).
Ulf Adams5c54c1c2016-08-17 13:31:22 +000058 * The startup options need to be parsed very early in the process, which requires this to be
59 * separate from {@link #serverInit}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 */
61 public Iterable<Class<? extends OptionsBase>> getStartupOptions() {
62 return ImmutableList.of();
63 }
64
65 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000066 * Called at the beginning of Bazel startup, before {@link #getFileSystem} and
67 * {@link #blazeStartup}.
Ulf Adams124bd0a2016-08-10 13:10:46 +000068 *
69 * @param startupOptions the server's startup options
70 *
71 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010073 public void globalInit(OptionsProvider startupOptions) throws AbruptExitException {
74 }
75
76 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000077 * Returns the file system implementation used by Bazel. It is an error if more than one module
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010078 * returns a file system. If all return null, the default unix file system is used.
79 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +000080 * <p>This method will be called at the beginning of Bazel startup (in-between {@link #globalInit}
81 * and {@link #blazeStartup}).
Ulf Adams124bd0a2016-08-10 13:10:46 +000082 *
83 * @param startupOptions the server's startup options
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 */
buchgr559a07d2017-11-30 11:09:35 -080085 public FileSystem getFileSystem(OptionsProvider startupOptions) throws AbruptExitException {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010086 return null;
87 }
88
89 /**
tomluf903eb52017-10-27 12:12:11 -040090 * Called when Bazel starts up after {@link #getStartupOptions}, {@link #globalInit}, and {@link
91 * #getFileSystem}.
Ulf Adams5c54c1c2016-08-17 13:31:22 +000092 *
93 * @param startupOptions the server's startup options
94 * @param versionInfo the Bazel version currently running
95 * @param instanceId the id of the current Bazel server
tomluf903eb52017-10-27 12:12:11 -040096 * @param fileSystem
Ulf Adams5c54c1c2016-08-17 13:31:22 +000097 * @param directories the install directory
98 * @param clock the clock
Ulf Adams5c54c1c2016-08-17 13:31:22 +000099 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100100 */
tomluf903eb52017-10-27 12:12:11 -0400101 public void blazeStartup(
102 OptionsProvider startupOptions,
103 BlazeVersionInfo versionInfo,
104 UUID instanceId,
105 FileSystem fileSystem,
106 ServerDirectories directories,
107 Clock clock)
108 throws AbruptExitException {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100109
110 /**
Ulf Adams345e15e2016-07-07 13:27:28 +0000111 * Called to initialize a new server ({@link BlazeRuntime}). Modules can override this method to
112 * affect how the server is configured. This is called after the startup options have been
113 * collected and parsed, and after the file system was setup.
114 *
115 * @param startupOptions the server startup options
116 * @param builder builder class that collects the server configuration
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000117 *
118 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100119 */
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000120 public void serverInit(OptionsProvider startupOptions, ServerBuilder builder)
121 throws AbruptExitException {
122 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100123
124 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000125 * Sets up the configured rule class provider, which contains the built-in rule classes, aspects,
philwo3bcb9f62017-09-06 12:52:21 +0200126 * configuration fragments, and other things; called during Blaze startup (after {@link
127 * #blazeStartup}).
128 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000129 * <p>Bazel only creates one provider per server, so it is not possible to have different contents
130 * for different workspaces.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100131 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000132 * @param builder the configured rule class provider builder
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100133 */
philwo3bcb9f62017-09-06 12:52:21 +0200134 public void initializeRuleClasses(ConfiguredRuleClassProvider.Builder builder) {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100135
136 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000137 * Called when Bazel initializes a new workspace; this is only called after {@link #serverInit},
138 * and only if the server initialization was successful. Modules can override this method to
139 * affect how the workspace is configured.
140 *
ulfjack94bee752017-06-13 19:13:35 +0200141 * @param runtime the blaze runtime
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000142 * @param directories the workspace directories
143 * @param builder the workspace builder
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100144 */
ulfjack94bee752017-06-13 19:13:35 +0200145 public void workspaceInit(
146 BlazeRuntime runtime, BlazeDirectories directories, WorkspaceBuilder builder) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100147 }
148
149 /**
ulfjacka6a99102017-06-13 17:15:45 +0200150 * Called to notify modules that the given command is about to be executed. This allows capturing
151 * the {@link com.google.common.eventbus.EventBus}, {@link Command}, or {@link OptionsProvider}.
152 *
153 * @param env the command
154 * @throws AbruptExitException modules can throw this exception to abort the command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100155 */
ulfjacka6a99102017-06-13 17:15:45 +0200156 public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
Ulf Adams633f5392015-09-15 11:13:08 +0000157 }
158
159 /**
Klaus Aehligd000e0c2017-04-28 13:35:05 +0200160 * Returns additional listeners to the console output stream. Called at the beginning of each
161 * command (after #beforeCommand).
162 */
163 @SuppressWarnings("unused")
164 @Nullable
165 public OutErr getOutputListener() {
166 return null;
167 }
168
169 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100170 * Returns the output service to be used. It is an error if more than one module returns an
171 * output service.
172 *
173 * <p>This method will be called at the beginning of each command (after #beforeCommand).
174 */
175 @SuppressWarnings("unused")
176 public OutputService getOutputService() throws AbruptExitException {
177 return null;
178 }
179
180 /**
Ulf Adams755dd842016-06-22 13:43:38 +0000181 * Returns extra options this module contributes to a specific command. Note that option
182 * inheritance applies: if this method returns a non-empty list, then the returned options are
183 * added to every command that depends on this command.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100184 *
Ulf Adams755dd842016-06-22 13:43:38 +0000185 * <p>This method may be called at any time, and the returned value may be cached. Implementations
186 * must be thread-safe and never return different lists for the same command object. Typical
187 * implementations look like this:
Philipp Wollermann49c20aa2016-08-25 12:59:52 +0000188 *
Ulf Adams755dd842016-06-22 13:43:38 +0000189 * <pre>
190 * return "build".equals(command.name())
191 * ? ImmutableList.<Class<? extends OptionsBase>>of(MyOptions.class)
192 * : ImmutableList.<Class<? extends OptionsBase>>of();
193 * </pre>
Philipp Wollermann49c20aa2016-08-25 12:59:52 +0000194 *
Ulf Adams755dd842016-06-22 13:43:38 +0000195 * Note that this example adds options to all commands that inherit from the build command.
196 *
197 * <p>This method is also used to generate command-line documentation; in order to avoid
198 * duplicated options descriptions, this method should never return the same options class for two
199 * different commands if one of them inherits the other.
200 *
201 * <p>If you want to add options to all commands, override {@link #getCommonCommandOptions}
202 * instead.
203 *
204 * @param command the command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100205 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100206 public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) {
207 return ImmutableList.of();
208 }
209
210 /**
Ulf Adams755dd842016-06-22 13:43:38 +0000211 * Returns extra options this module contributes to all commands.
212 */
213 public Iterable<Class<? extends OptionsBase>> getCommonCommandOptions() {
214 return ImmutableList.of();
215 }
216
217 /**
mjhalupka5d7fa7b2018-03-22 13:37:38 -0700218 * Returns an instance of BuildOptions to be used to create {@link
219 * BuildOptions.OptionsDiffForReconstruction} with. Only one installed Module should override
220 * this.
221 */
222 public BuildOptions getDefaultBuildOptions(BlazeRuntime runtime) {
223 return null;
224 }
225
226 /**
Ulf Adamsa0e3af42016-10-31 16:52:48 +0000227 * Called when Bazel initializes the action execution subsystem. This is called once per build if
228 * action execution is enabled. Modules can override this method to affect how execution is
229 * performed.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100230 *
Ulf Adamsa0e3af42016-10-31 16:52:48 +0000231 * @param env the command environment
232 * @param request the build request
233 * @param builder the builder to add action context providers and consumers to
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100234 */
philwod3de5cc2018-04-16 06:40:19 -0700235 public void executorInit(CommandEnvironment env, BuildRequest request, ExecutorBuilder builder)
236 throws ExecutorInitException {}
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100237
238 /**
239 * Called after each command.
240 */
241 public void afterCommand() {
242 }
243
244 /**
245 * Called when Blaze shuts down.
Julio Merinod1619b752016-10-17 09:50:11 +0000246 *
ajmichaelb31e4652017-11-01 16:45:02 -0400247 * <p>If you are also implementing {@link #blazeShutdownOnCrash()}, consider putting the common
Julio Merinod1619b752016-10-17 09:50:11 +0000248 * shutdown code in the latter and calling that other hook from here.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100249 */
250 public void blazeShutdown() {
251 }
252
253 /**
Julio Merinod1619b752016-10-17 09:50:11 +0000254 * Called when Blaze shuts down due to a crash.
255 *
256 * <p>Modules may use this to flush pending state, but they must be careful to only do a minimal
257 * number of things. Keep in mind that we are crashing so who knows what state we are in. Modules
258 * rarely need to implement this.
259 */
260 public void blazeShutdownOnCrash() {}
261
262 /**
Ulf Adams4b004af2015-09-16 10:22:26 +0000263 * Perform module specific check of current command environment.
Marian Lobur7beee712015-08-17 11:48:57 +0000264 */
Ulf Adams4b004af2015-09-16 10:22:26 +0000265 public void checkEnvironment(CommandEnvironment env) {
Marian Lobur7beee712015-08-17 11:48:57 +0000266 }
267
268 /**
Nathan Harmata42fb5602016-05-25 20:32:08 +0000269 * Returns a helper that the {@link PackageFactory} will use during package loading. If the module
Nathan Harmatacaa000a2016-06-07 17:46:19 +0000270 * does not provide any helper, it should return null. Note that only one helper per Bazel/Blaze
Nathan Harmata42fb5602016-05-25 20:32:08 +0000271 * runtime is allowed.
272 */
nharmatae57e9a32018-04-02 15:10:24 -0700273 public Package.Builder.Helper getPackageBuilderHelper(RuleClassProvider ruleClassProvider) {
Nathan Harmata42fb5602016-05-25 20:32:08 +0000274 return null;
275 }
276
277 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100278 * Optionally returns a provider for project files that can be used to bundle targets and
279 * command-line options.
280 */
281 @Nullable
282 public ProjectFile.Provider createProjectFileProvider() {
283 return null;
284 }
285
286 /**
Ulf Adams33644a42016-08-10 11:32:41 +0000287 * Optionally returns a factory to create coverage report actions; this is called once per build,
philwo3bcb9f62017-09-06 12:52:21 +0200288 * such that it can be affected by command options.
Ulf Adams33644a42016-08-10 11:32:41 +0000289 *
290 * <p>It is an error if multiple modules return non-null values.
291 *
292 * @param commandOptions the options for the current command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100293 */
294 @Nullable
Ulf Adams33644a42016-08-10 11:32:41 +0000295 public CoverageReportActionFactory getCoverageReportFactory(OptionsClassProvider commandOptions) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100296 return null;
297 }
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000298
299 /**
300 * Services provided for Blaze modules via BlazeRuntime.
301 */
302 public interface ModuleEnvironment {
303 /**
304 * Gets a file from the depot based on its label and returns the {@link Path} where it can
305 * be found.
306 */
307 Path getFileFromWorkspace(Label label)
308 throws NoSuchThingException, InterruptedException, IOException;
309
310 /**
Michael Staib6e5e8fb2016-10-04 21:26:37 +0000311 * Exits Blaze as early as possible by sending an interrupt to the command's main thread.
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000312 */
313 void exit(AbruptExitException exception);
314 }
kchodorowdfcd5da82017-04-19 18:58:50 +0200315
316 public ImmutableList<PrecomputedValue.Injected> getPrecomputedValues() {
317 return ImmutableList.of();
318 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100319}