blob: 3321dab9a1501e154aeb28a996f66a54ce5d40c2 [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;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.devtools.build.lib.analysis.BlazeDirectories;
18import com.google.devtools.build.lib.analysis.BlazeVersionInfo;
19import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider;
Ulf Adams15a23b92016-08-09 11:46:00 +000020import com.google.devtools.build.lib.analysis.ServerDirectories;
ulfjackab21d182017-08-10 15:36:14 +020021import com.google.devtools.build.lib.analysis.test.CoverageReportActionFactory;
Ulf Adamsa0e3af42016-10-31 16:52:48 +000022import com.google.devtools.build.lib.buildtool.BuildRequest;
Lukacs Berki6e91eb92015-09-21 09:12:37 +000023import com.google.devtools.build.lib.cmdline.Label;
Ulf Adamsdba3c832016-12-21 16:50:02 +000024import com.google.devtools.build.lib.exec.ExecutorBuilder;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025import com.google.devtools.build.lib.packages.NoSuchThingException;
Nathan Harmata42fb5602016-05-25 20:32:08 +000026import com.google.devtools.build.lib.packages.Package;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010027import com.google.devtools.build.lib.packages.PackageFactory;
Nathan Harmatacaa000a2016-06-07 17:46:19 +000028import com.google.devtools.build.lib.packages.RuleClassProvider;
ulfjackbc4eb272017-08-10 17:33:22 +020029import com.google.devtools.build.lib.skyframe.OutputService;
kchodorowdfcd5da82017-04-19 18:58:50 +020030import com.google.devtools.build.lib.skyframe.PrecomputedValue;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010031import com.google.devtools.build.lib.util.AbruptExitException;
32import com.google.devtools.build.lib.util.Clock;
Klaus Aehligd000e0c2017-04-28 13:35:05 +020033import com.google.devtools.build.lib.util.io.OutErr;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034import com.google.devtools.build.lib.vfs.FileSystem;
35import com.google.devtools.build.lib.vfs.Path;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036import com.google.devtools.common.options.OptionsBase;
Ulf Adams33644a42016-08-10 11:32:41 +000037import com.google.devtools.common.options.OptionsClassProvider;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010038import com.google.devtools.common.options.OptionsProvider;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040import java.util.UUID;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010041import javax.annotation.Nullable;
42
43/**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000044 * A module Bazel can load at the beginning of its execution. Modules are supplied with extension
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010045 * points to augment the functionality at specific, well-defined places.
46 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +000047 * <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 +010048 * methods (e.g. {@link #blazeStartup}).
49 */
50public abstract class BlazeModule {
51
52 /**
53 * Returns the extra startup options this module contributes.
54 *
Ulf Adams124bd0a2016-08-10 13:10:46 +000055 * <p>This method will be called at the beginning of Blaze startup (before {@link #globalInit}).
Ulf Adams5c54c1c2016-08-17 13:31:22 +000056 * The startup options need to be parsed very early in the process, which requires this to be
57 * separate from {@link #serverInit}.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 */
59 public Iterable<Class<? extends OptionsBase>> getStartupOptions() {
60 return ImmutableList.of();
61 }
62
63 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000064 * Called at the beginning of Bazel startup, before {@link #getFileSystem} and
65 * {@link #blazeStartup}.
Ulf Adams124bd0a2016-08-10 13:10:46 +000066 *
67 * @param startupOptions the server's startup options
68 *
69 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010070 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010071 public void globalInit(OptionsProvider startupOptions) throws AbruptExitException {
72 }
73
74 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000075 * 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 +010076 * returns a file system. If all return null, the default unix file system is used.
77 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +000078 * <p>This method will be called at the beginning of Bazel startup (in-between {@link #globalInit}
79 * and {@link #blazeStartup}).
Ulf Adams124bd0a2016-08-10 13:10:46 +000080 *
81 * @param startupOptions the server's startup options
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010082 */
Ulf Adams124bd0a2016-08-10 13:10:46 +000083 public FileSystem getFileSystem(OptionsProvider startupOptions) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010084 return null;
85 }
86
87 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +000088 * Called when Bazel starts up after {@link #getStartupOptions}, {@link #globalInit}, and
89 * {@link #getFileSystem}.
90 *
91 * @param startupOptions the server's startup options
92 * @param versionInfo the Bazel version currently running
93 * @param instanceId the id of the current Bazel server
94 * @param directories the install directory
95 * @param clock the clock
96 *
97 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010098 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010099 public void blazeStartup(OptionsProvider startupOptions,
Ulf Adams15a23b92016-08-09 11:46:00 +0000100 BlazeVersionInfo versionInfo, UUID instanceId, ServerDirectories directories,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100101 Clock clock) throws AbruptExitException {
102 }
103
104 /**
Ulf Adams345e15e2016-07-07 13:27:28 +0000105 * Called to initialize a new server ({@link BlazeRuntime}). Modules can override this method to
106 * affect how the server is configured. This is called after the startup options have been
107 * collected and parsed, and after the file system was setup.
108 *
109 * @param startupOptions the server startup options
110 * @param builder builder class that collects the server configuration
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000111 *
112 * @throws AbruptExitException to shut down the server immediately
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100113 */
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000114 public void serverInit(OptionsProvider startupOptions, ServerBuilder builder)
115 throws AbruptExitException {
116 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100117
118 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000119 * Sets up the configured rule class provider, which contains the built-in rule classes, aspects,
120 * configuration fragments, and other things; called during Blaze startup (after
121 * {@link #blazeStartup}).
122 *
123 * <p>Bazel only creates one provider per server, so it is not possible to have different contents
124 * for different workspaces.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100125 *
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000126 * @param builder the configured rule class provider builder
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100127 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100128 public void initializeRuleClasses(ConfiguredRuleClassProvider.Builder builder) {
129 }
130
131 /**
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000132 * Called when Bazel initializes a new workspace; this is only called after {@link #serverInit},
133 * and only if the server initialization was successful. Modules can override this method to
134 * affect how the workspace is configured.
135 *
ulfjack94bee752017-06-13 19:13:35 +0200136 * @param runtime the blaze runtime
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000137 * @param directories the workspace directories
138 * @param builder the workspace builder
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100139 */
ulfjack94bee752017-06-13 19:13:35 +0200140 public void workspaceInit(
141 BlazeRuntime runtime, BlazeDirectories directories, WorkspaceBuilder builder) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100142 }
143
144 /**
ulfjacka6a99102017-06-13 17:15:45 +0200145 * Called to notify modules that the given command is about to be executed. This allows capturing
146 * the {@link com.google.common.eventbus.EventBus}, {@link Command}, or {@link OptionsProvider}.
147 *
148 * @param env the command
149 * @throws AbruptExitException modules can throw this exception to abort the command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100150 */
ulfjacka6a99102017-06-13 17:15:45 +0200151 public void beforeCommand(CommandEnvironment env) throws AbruptExitException {
Ulf Adams633f5392015-09-15 11:13:08 +0000152 }
153
154 /**
Klaus Aehligd000e0c2017-04-28 13:35:05 +0200155 * Returns additional listeners to the console output stream. Called at the beginning of each
156 * command (after #beforeCommand).
157 */
158 @SuppressWarnings("unused")
159 @Nullable
160 public OutErr getOutputListener() {
161 return null;
162 }
163
164 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100165 * Returns the output service to be used. It is an error if more than one module returns an
166 * output service.
167 *
168 * <p>This method will be called at the beginning of each command (after #beforeCommand).
169 */
170 @SuppressWarnings("unused")
171 public OutputService getOutputService() throws AbruptExitException {
172 return null;
173 }
174
175 /**
Ulf Adams755dd842016-06-22 13:43:38 +0000176 * Returns extra options this module contributes to a specific command. Note that option
177 * inheritance applies: if this method returns a non-empty list, then the returned options are
178 * added to every command that depends on this command.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100179 *
Ulf Adams755dd842016-06-22 13:43:38 +0000180 * <p>This method may be called at any time, and the returned value may be cached. Implementations
181 * must be thread-safe and never return different lists for the same command object. Typical
182 * implementations look like this:
Philipp Wollermann49c20aa2016-08-25 12:59:52 +0000183 *
Ulf Adams755dd842016-06-22 13:43:38 +0000184 * <pre>
185 * return "build".equals(command.name())
186 * ? ImmutableList.<Class<? extends OptionsBase>>of(MyOptions.class)
187 * : ImmutableList.<Class<? extends OptionsBase>>of();
188 * </pre>
Philipp Wollermann49c20aa2016-08-25 12:59:52 +0000189 *
Ulf Adams755dd842016-06-22 13:43:38 +0000190 * Note that this example adds options to all commands that inherit from the build command.
191 *
192 * <p>This method is also used to generate command-line documentation; in order to avoid
193 * duplicated options descriptions, this method should never return the same options class for two
194 * different commands if one of them inherits the other.
195 *
196 * <p>If you want to add options to all commands, override {@link #getCommonCommandOptions}
197 * instead.
198 *
199 * @param command the command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100200 */
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100201 public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) {
202 return ImmutableList.of();
203 }
204
205 /**
Ulf Adams755dd842016-06-22 13:43:38 +0000206 * Returns extra options this module contributes to all commands.
207 */
208 public Iterable<Class<? extends OptionsBase>> getCommonCommandOptions() {
209 return ImmutableList.of();
210 }
211
212 /**
Ulf Adamsa0e3af42016-10-31 16:52:48 +0000213 * Called when Bazel initializes the action execution subsystem. This is called once per build if
214 * action execution is enabled. Modules can override this method to affect how execution is
215 * performed.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100216 *
Ulf Adamsa0e3af42016-10-31 16:52:48 +0000217 * @param env the command environment
218 * @param request the build request
219 * @param builder the builder to add action context providers and consumers to
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100220 */
Ulf Adamsa0e3af42016-10-31 16:52:48 +0000221 public void executorInit(CommandEnvironment env, BuildRequest request, ExecutorBuilder builder) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100222 }
223
224 /**
225 * Called after each command.
226 */
227 public void afterCommand() {
228 }
229
230 /**
231 * Called when Blaze shuts down.
Julio Merinod1619b752016-10-17 09:50:11 +0000232 *
233 * <p>If you are also implementing {@link #shutdownOnCrash()}, consider putting the common
234 * shutdown code in the latter and calling that other hook from here.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100235 */
236 public void blazeShutdown() {
237 }
238
239 /**
Julio Merinod1619b752016-10-17 09:50:11 +0000240 * Called when Blaze shuts down due to a crash.
241 *
242 * <p>Modules may use this to flush pending state, but they must be careful to only do a minimal
243 * number of things. Keep in mind that we are crashing so who knows what state we are in. Modules
244 * rarely need to implement this.
245 */
246 public void blazeShutdownOnCrash() {}
247
248 /**
Ulf Adams4b004af2015-09-16 10:22:26 +0000249 * Perform module specific check of current command environment.
Marian Lobur7beee712015-08-17 11:48:57 +0000250 */
Ulf Adams4b004af2015-09-16 10:22:26 +0000251 public void checkEnvironment(CommandEnvironment env) {
Marian Lobur7beee712015-08-17 11:48:57 +0000252 }
253
254 /**
Nathan Harmata42fb5602016-05-25 20:32:08 +0000255 * Returns a helper that the {@link PackageFactory} will use during package loading. If the module
Nathan Harmatacaa000a2016-06-07 17:46:19 +0000256 * does not provide any helper, it should return null. Note that only one helper per Bazel/Blaze
Nathan Harmata42fb5602016-05-25 20:32:08 +0000257 * runtime is allowed.
258 */
Nathan Harmatacaa000a2016-06-07 17:46:19 +0000259 public Package.Builder.Helper getPackageBuilderHelper(RuleClassProvider ruleClassProvider,
260 FileSystem fs) {
Nathan Harmata42fb5602016-05-25 20:32:08 +0000261 return null;
262 }
263
264 /**
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100265 * Optionally returns a provider for project files that can be used to bundle targets and
266 * command-line options.
267 */
268 @Nullable
269 public ProjectFile.Provider createProjectFileProvider() {
270 return null;
271 }
272
273 /**
Ulf Adams33644a42016-08-10 11:32:41 +0000274 * Optionally returns a factory to create coverage report actions; this is called once per build,
275 * such that it can be affected by command options.
276 *
277 * <p>It is an error if multiple modules return non-null values.
278 *
279 * @param commandOptions the options for the current command
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100280 */
281 @Nullable
Ulf Adams33644a42016-08-10 11:32:41 +0000282 public CoverageReportActionFactory getCoverageReportFactory(OptionsClassProvider commandOptions) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100283 return null;
284 }
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000285
286 /**
287 * Services provided for Blaze modules via BlazeRuntime.
288 */
289 public interface ModuleEnvironment {
290 /**
291 * Gets a file from the depot based on its label and returns the {@link Path} where it can
292 * be found.
293 */
294 Path getFileFromWorkspace(Label label)
295 throws NoSuchThingException, InterruptedException, IOException;
296
297 /**
Michael Staib6e5e8fb2016-10-04 21:26:37 +0000298 * Exits Blaze as early as possible by sending an interrupt to the command's main thread.
Ulf Adams5c54c1c2016-08-17 13:31:22 +0000299 */
300 void exit(AbruptExitException exception);
301 }
kchodorowdfcd5da82017-04-19 18:58:50 +0200302
303 public ImmutableList<PrecomputedValue.Injected> getPrecomputedValues() {
304 return ImmutableList.of();
305 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +0100306}