diff options
author | 2016-07-07 13:27:28 +0000 | |
---|---|---|
committer | 2016-07-07 14:54:13 +0000 | |
commit | 345e15e9f84c4ab21d26a51d8ed6e62f89210e78 (patch) | |
tree | 5d6ad6a2de1c683641e32585de55e4e55940045c /src/main | |
parent | 5b1be3a09958cd935e36c39c70939dad2f2cc8fb (diff) |
Add a ServerBuilder, and use that in the module API.
This change is similar to a previous change that introduced WorkspaceBuilder.
--
MOS_MIGRATED_REVID=126799657
Diffstat (limited to 'src/main')
7 files changed, 239 insertions, 200 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java index 0c8dd599d2..42c935e817 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/ConfiguredRuleClassProvider.java @@ -18,6 +18,7 @@ import static com.google.common.base.Preconditions.checkNotNull; import static com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType.ABSTRACT; import static com.google.devtools.build.lib.packages.RuleClass.Builder.RuleClassType.TEST; +import com.google.common.base.Preconditions; import com.google.common.cache.CacheBuilder; import com.google.common.cache.CacheLoader; import com.google.common.cache.LoadingCache; @@ -55,6 +56,9 @@ import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TreeMap; + +import javax.annotation.Nullable; /** * Knows about every rule Blaze supports and the associated configuration options. @@ -108,6 +112,7 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { ImmutableList.<Class<?>>builder().addAll(SkylarkModules.MODULES); private ImmutableBiMap.Builder<String, Class<? extends TransitiveInfoProvider>> registeredSkylarkProviders = ImmutableBiMap.builder(); + private Map<String, String> platformRegexps = new TreeMap<>(); public void addWorkspaceFilePrefix(String contents) { defaultWorkspaceFilePrefix.append(contents); @@ -211,6 +216,26 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { return this; } + /** + * Do not use - this only exists for backwards compatibility! Platform regexps are part of a + * legacy mechanism - {@code vardef} - that is not exposed in Bazel. + * + * <p>{@code vardef} needs explicit support in the rule implementations, and cannot express + * conditional dependencies, only conditional attribute values. This mechanism will be + * supplanted by configuration dependent attributes, and its effect can usually also be achieved + * with select(). + * + * <p>This is a map of platform names to regexps. When a name is used as the third argument to + * {@code vardef}, the corresponding regexp is used to match on the C++ abi, and the variable is + * only set to that value if the regexp matches. For example, the entry + * {@code "oldlinux": "i[34]86-libc[345]-linux"} might define a set of platforms representing + * certain older linux releases. + */ + public Builder addPlatformRegexps(Map<String, String> platformRegexps) { + this.platformRegexps.putAll(Preconditions.checkNotNull(platformRegexps)); + return this; + } + private RuleConfiguredTargetFactory createFactory( Class<? extends RuleConfiguredTargetFactory> factoryClass) { try { @@ -304,6 +329,11 @@ public class ConfiguredRuleClassProvider implements RuleClassProvider { public String getToolsRepository() { return toolsRepository; } + + @Nullable + public Map<String, String> getPlatformRegexps() { + return platformRegexps.isEmpty() ? null : ImmutableMap.copyOf(platformRegexps); + } } /** diff --git a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java index 12c4bd208e..365632d196 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java @@ -14,7 +14,6 @@ package com.google.devtools.build.lib.bazel; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.analysis.BlazeDirectories; import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; @@ -52,10 +51,10 @@ import com.google.devtools.build.lib.rules.repository.RepositoryDelegatorFunctio import com.google.devtools.build.lib.rules.repository.RepositoryDirectoryValue; import com.google.devtools.build.lib.rules.repository.RepositoryFunction; import com.google.devtools.build.lib.rules.repository.RepositoryLoaderFunction; -import com.google.devtools.build.lib.runtime.BlazeCommand; import com.google.devtools.build.lib.runtime.BlazeModule; import com.google.devtools.build.lib.runtime.Command; import com.google.devtools.build.lib.runtime.CommandEnvironment; +import com.google.devtools.build.lib.runtime.ServerBuilder; import com.google.devtools.build.lib.runtime.WorkspaceBuilder; import com.google.devtools.build.lib.skyframe.SkyFunctions; import com.google.devtools.build.lib.skyframe.SkyValueDirtinessChecker; @@ -132,6 +131,11 @@ public class BazelRepositoryModule extends BlazeModule { }; @Override + public void serverInit(OptionsProvider startupOptions, ServerBuilder builder) { + builder.addCommands(new FetchCommand()); + } + + @Override public void workspaceInit(BlazeDirectories directories, WorkspaceBuilder builder) { builder.addCustomDirtinessChecker(REPOSITORY_VALUE_CHECKER); // Create the repository function everything flows through. @@ -156,11 +160,6 @@ public class BazelRepositoryModule extends BlazeModule { } @Override - public Iterable<? extends BlazeCommand> getCommands() { - return ImmutableList.of(new FetchCommand()); - } - - @Override public void handleOptions(OptionsProvider optionsProvider) { PackageCacheOptions pkgOptions = optionsProvider.getOptions(PackageCacheOptions.class); isFetch.set(pkgOptions != null && pkgOptions.fetch); diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java index 171a1ffd72..48a4898545 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java @@ -493,6 +493,7 @@ public class BlazeCommandDispatcher { numericExitCode = e.getExitStatus(); throw e; } catch (Throwable e) { + e.printStackTrace(); BugReport.printBug(outErr, e); BugReport.sendBugReport(e, args, crashData); numericExitCode = e instanceof OutOfMemoryError diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java index 03580b2c3a..9879ab05cf 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeModule.java @@ -13,7 +13,6 @@ // limitations under the License. package com.google.devtools.build.lib.runtime; -import com.google.common.base.Function; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.devtools.build.lib.actions.ActionContextConsumer; @@ -24,11 +23,9 @@ import com.google.devtools.build.lib.analysis.BlazeVersionInfo; import com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider; import com.google.devtools.build.lib.cmdline.Label; import com.google.devtools.build.lib.exec.OutputService; -import com.google.devtools.build.lib.packages.AttributeContainer; import com.google.devtools.build.lib.packages.NoSuchThingException; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.PackageFactory; -import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.packages.RuleClassProvider; import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment; import com.google.devtools.build.lib.query2.QueryEnvironmentFactory; @@ -36,7 +33,6 @@ import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunctio import com.google.devtools.build.lib.query2.output.OutputFormatter; import com.google.devtools.build.lib.rules.test.CoverageReportActionFactory; import com.google.devtools.build.lib.runtime.commands.InfoItem; -import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; import com.google.devtools.build.lib.util.AbruptExitException; import com.google.devtools.build.lib.util.Clock; import com.google.devtools.build.lib.vfs.FileSystem; @@ -100,11 +96,18 @@ public abstract class BlazeModule { } /** - * Called when Blaze initializes a new workspace. + * Called to initialize a new server ({@link BlazeRuntime}). Modules can override this method to + * affect how the server is configured. This is called after the startup options have been + * collected and parsed, and after the file system was setup. + * + * @param startupOptions the server startup options + * @param builder builder class that collects the server configuration */ + public void serverInit(OptionsProvider startupOptions, ServerBuilder builder) {} + + /** Called when Blaze initializes a new workspace. */ @SuppressWarnings("unused") - public void workspaceInit(BlazeDirectories directories, WorkspaceBuilder builder) { - } + public void workspaceInit(BlazeDirectories directories, WorkspaceBuilder builder) {} /** * Adds the rule classes supported by this module. @@ -116,15 +119,6 @@ public abstract class BlazeModule { } /** - * Returns the list of commands this module contributes to Blaze. - * - * <p>This method will be called during Blaze startup (after #blazeStartup). - */ - public Iterable<? extends BlazeCommand> getCommands() { - return ImmutableList.of(); - } - - /** * Returns the list of query output formatters this module provides. * * <p>This method will be called during Blaze startup (after #blazeStartup). @@ -134,30 +128,6 @@ public abstract class BlazeModule { } /** - * PlatformSet is a group of platforms characterized by a regular expression. For example, the - * entry "oldlinux": "i[34]86-libc[345]-linux" might define a set of platforms representing - * certain older linux releases. - * - * <p>Platform-set names are used in BUILD files in the third argument to <tt>vardef</tt>, to - * define per-platform tweaks to variables such as CFLAGS. - * - * <p>vardef is a legacy mechanism: it needs explicit support in the rule implementations, - * and cannot express conditional dependencies, only conditional attribute values. This - * mechanism will be supplanted by configuration dependent attributes, and its effect can - * usually also be achieved with select(). - * - * <p>This method will be called during Blaze startup (after #blazeStartup). - */ - public Map<String, String> getPlatformSetRegexps() { - return ImmutableMap.<String, String>of(); - } - - @Nullable - protected Function<RuleClass, AttributeContainer> getAttributeContainerSupplier() { - return null; - } - - /** * Services provided for Blaze modules via BlazeRuntime. */ public interface ModuleEnvironment { @@ -355,12 +325,4 @@ public abstract class BlazeModule { public CoverageReportActionFactory getCoverageReportFactory() { return null; } - - /** - * Optionally returns the invocation policy to override options in blaze. - */ - @Nullable - public InvocationPolicy getInvocationPolicy() { - return null; - } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java index ec7c7a0a6c..2a22272b18 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java @@ -18,7 +18,6 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Function; import com.google.common.base.Predicates; import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; import com.google.common.collect.Lists; import com.google.common.collect.Sets; @@ -35,10 +34,8 @@ import com.google.devtools.build.lib.analysis.config.ConfigurationFactory; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.OutputFilter; import com.google.devtools.build.lib.flags.CommandNameCache; -import com.google.devtools.build.lib.packages.AttributeContainer; import com.google.devtools.build.lib.packages.Package; import com.google.devtools.build.lib.packages.PackageFactory; -import com.google.devtools.build.lib.packages.RuleClass; import com.google.devtools.build.lib.packages.RuleClassProvider; import com.google.devtools.build.lib.profiler.AutoProfiler; import com.google.devtools.build.lib.profiler.MemoryProfiler; @@ -51,19 +48,6 @@ import com.google.devtools.build.lib.query2.QueryEnvironmentFactory; import com.google.devtools.build.lib.query2.output.OutputFormatter; import com.google.devtools.build.lib.rules.test.CoverageReportActionFactory; import com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.LockingMode; -import com.google.devtools.build.lib.runtime.commands.BuildCommand; -import com.google.devtools.build.lib.runtime.commands.CanonicalizeCommand; -import com.google.devtools.build.lib.runtime.commands.CleanCommand; -import com.google.devtools.build.lib.runtime.commands.DumpCommand; -import com.google.devtools.build.lib.runtime.commands.HelpCommand; -import com.google.devtools.build.lib.runtime.commands.InfoCommand; -import com.google.devtools.build.lib.runtime.commands.MobileInstallCommand; -import com.google.devtools.build.lib.runtime.commands.ProfileCommand; -import com.google.devtools.build.lib.runtime.commands.QueryCommand; -import com.google.devtools.build.lib.runtime.commands.RunCommand; -import com.google.devtools.build.lib.runtime.commands.ShutdownCommand; -import com.google.devtools.build.lib.runtime.commands.TestCommand; -import com.google.devtools.build.lib.runtime.commands.VersionCommand; import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; import com.google.devtools.build.lib.server.AfUnixServer; import com.google.devtools.build.lib.server.RPCServer; @@ -190,21 +174,6 @@ public final class BlazeRuntime { this.productName = productName; } - private static InvocationPolicy createInvocationPolicyFromModules( - InvocationPolicy initialInvocationPolicy, - Iterable<BlazeModule> modules) { - InvocationPolicy.Builder builder = InvocationPolicy.newBuilder(); - builder.mergeFrom(initialInvocationPolicy); - // Merge the policies from the modules - for (BlazeModule module : modules) { - InvocationPolicy modulePolicy = module.getInvocationPolicy(); - if (modulePolicy != null) { - builder.mergeFrom(module.getInvocationPolicy()); - } - } - return builder.build(); - } - public void initWorkspace(BlazeDirectories directories, BinTools binTools) throws AbruptExitException { boolean watchFS = startupOptionsProvider != null @@ -248,11 +217,6 @@ public final class BlazeRuntime { for (BlazeCommand command : commands) { addCommand(command); } - for (BlazeModule module : blazeModules) { - for (BlazeCommand command : module.getCommands()) { - addCommand(command); - } - } } public CommandEnvironment initCommand() { @@ -993,10 +957,10 @@ public final class BlazeRuntime { LoggingUtil.installRemoteLogger(getTestCrashLogger()); } + runtimeBuilder.addBlazeModule(new BuiltinCommandModule()); for (BlazeModule blazeModule : blazeModules) { runtimeBuilder.addBlazeModule(blazeModule); } - runtimeBuilder.addCommands(getBuiltinCommandList()); BlazeRuntime runtime = runtimeBuilder.build(); AutoProfiler.setClock(runtime.getClock()); @@ -1056,28 +1020,6 @@ public final class BlazeRuntime { }); } - - /** - * Returns an immutable list containing new instances of each Blaze command. - */ - @VisibleForTesting - public static List<BlazeCommand> getBuiltinCommandList() { - return ImmutableList.of( - new BuildCommand(), - new CanonicalizeCommand(), - new CleanCommand(), - new DumpCommand(), - new HelpCommand(), - new InfoCommand(), - new MobileInstallCommand(), - new ProfileCommand(), - new QueryCommand(), - new RunCommand(), - new ShutdownCommand(), - new TestCommand(), - new VersionCommand()); - } - public String getProductName() { return productName; } @@ -1093,15 +1035,12 @@ public final class BlazeRuntime { */ public static class Builder { private BlazeDirectories directories; - private ConfigurationFactory configurationFactory; private Clock clock; private OptionsProvider startupOptionsProvider; private final List<BlazeModule> blazeModules = new ArrayList<>(); private SubscriberExceptionHandler eventBusExceptionHandler = new RemoteExceptionHandler(); private BinTools binTools; private UUID instanceId; - private final List<BlazeCommand> commands = new ArrayList<>(); - private InvocationPolicy invocationPolicy = InvocationPolicy.getDefaultInstance(); private String productName; public BlazeRuntime build() throws AbruptExitException { @@ -1117,20 +1056,9 @@ public final class BlazeRuntime { module.blazeStartup(startupOptionsProvider, BlazeVersionInfo.instance(), instanceId, directories, clock); } - - QueryEnvironmentFactory queryEnvironmentFactory = null; + ServerBuilder serverBuilder = new ServerBuilder(); for (BlazeModule module : blazeModules) { - QueryEnvironmentFactory queryEnvFactory = module.getQueryEnvironmentFactory(); - if (queryEnvFactory != null) { - Preconditions.checkState(queryEnvironmentFactory == null, - "At most one query environment factory supported. But found two: %s and %s", - queryEnvFactory, - queryEnvironmentFactory); - queryEnvironmentFactory = queryEnvFactory; - } - } - if (queryEnvironmentFactory == null) { - queryEnvironmentFactory = new QueryEnvironmentFactory(); + module.serverInit(startupOptionsProvider, serverBuilder); } ConfiguredRuleClassProvider.Builder ruleClassBuilder = @@ -1139,35 +1067,6 @@ public final class BlazeRuntime { module.initializeRuleClasses(ruleClassBuilder); } - Map<String, String> platformRegexps = null; - { - ImmutableMap.Builder<String, String> builder = new ImmutableMap.Builder<>(); - for (BlazeModule module : blazeModules) { - builder.putAll(module.getPlatformSetRegexps()); - } - platformRegexps = builder.build(); - if (platformRegexps.isEmpty()) { - platformRegexps = null; // Use the default. - } - } - - Function<RuleClass, AttributeContainer> attributeContainerFactory = null; - for (BlazeModule module : blazeModules) { - Function<RuleClass, AttributeContainer> attrContainerFactory = - module.getAttributeContainerSupplier(); - if (attrContainerFactory != null) { - Preconditions.checkState( - attributeContainerFactory == null, - "At most one attribute container supplier supported. But found two: %s and %s", - attrContainerFactory, - attributeContainerFactory); - attributeContainerFactory = attrContainerFactory; - } - } - if (attributeContainerFactory == null) { - attributeContainerFactory = AttributeContainer.ATTRIBUTE_CONTAINER_FACTORY; - } - ConfiguredRuleClassProvider ruleClassProvider = ruleClassBuilder.build(); List<PackageFactory.EnvironmentExtension> extensions = new ArrayList<>(); @@ -1192,17 +1091,16 @@ public final class BlazeRuntime { PackageFactory packageFactory = new PackageFactory( ruleClassProvider, - platformRegexps, - attributeContainerFactory, + ruleClassBuilder.getPlatformRegexps(), + serverBuilder.getAttributeContainerFactory(), extensions, BlazeVersionInfo.instance().getVersion(), packageBuilderHelper); - if (configurationFactory == null) { - configurationFactory = new ConfigurationFactory( - ruleClassProvider.getConfigurationCollectionFactory(), - ruleClassProvider.getConfigurationFragments()); - } + ConfigurationFactory configurationFactory = + new ConfigurationFactory( + ruleClassProvider.getConfigurationCollectionFactory(), + ruleClassProvider.getConfigurationFragments()); ProjectFile.Provider projectFileProvider = null; for (BlazeModule module : blazeModules) { @@ -1214,12 +1112,20 @@ public final class BlazeRuntime { } } - invocationPolicy = createInvocationPolicyFromModules(invocationPolicy, blazeModules); - - BlazeRuntime runtime = new BlazeRuntime(queryEnvironmentFactory, packageFactory, - ruleClassProvider, configurationFactory, clock, startupOptionsProvider, - ImmutableList.copyOf(blazeModules), eventBusExceptionHandler, projectFileProvider, - invocationPolicy, commands, productName); + BlazeRuntime runtime = + new BlazeRuntime( + serverBuilder.getQueryEnvironmentFactory(), + packageFactory, + ruleClassProvider, + configurationFactory, + clock, + startupOptionsProvider, + ImmutableList.copyOf(blazeModules), + eventBusExceptionHandler, + projectFileProvider, + serverBuilder.getInvocationPolicy(), + serverBuilder.getCommands(), + productName); runtime.initWorkspace(directories, binTools); return runtime; } @@ -1234,29 +1140,15 @@ public final class BlazeRuntime { return this; } - public Builder setInvocationPolicy(InvocationPolicy invocationPolicy) { - this.invocationPolicy = invocationPolicy; - return this; - } - public Builder setDirectories(BlazeDirectories directories) { this.directories = directories; return this; } - /** - * Creates and sets a new {@link BlazeDirectories} instance with the given - * parameters. - */ - public Builder setDirectories(Path installBase, Path outputBase, - Path workspace, String productName) { - this.directories = new BlazeDirectories(installBase, outputBase, workspace, productName); - return this; - } - - public Builder setConfigurationFactory(ConfigurationFactory configurationFactory) { - this.configurationFactory = configurationFactory; - return this; + /** Creates and sets a new {@link BlazeDirectories} instance with the given parameters. */ + public Builder setDirectories( + Path installBase, Path outputBase, Path workspace, String productName) { + return setDirectories(new BlazeDirectories(installBase, outputBase, workspace, productName)); } public Builder setClock(Clock clock) { @@ -1285,15 +1177,5 @@ public final class BlazeRuntime { this.eventBusExceptionHandler = eventBusExceptionHandler; return this; } - - public Builder addCommands(BlazeCommand... commands) { - this.commands.addAll(Arrays.asList(commands)); - return this; - } - - public Builder addCommands(Iterable<BlazeCommand> commands) { - Iterables.addAll(this.commands, commands); - return this; - } } } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BuiltinCommandModule.java b/src/main/java/com/google/devtools/build/lib/runtime/BuiltinCommandModule.java new file mode 100644 index 0000000000..506f7ff9fc --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/runtime/BuiltinCommandModule.java @@ -0,0 +1,52 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.runtime; + +import com.google.devtools.build.lib.runtime.commands.BuildCommand; +import com.google.devtools.build.lib.runtime.commands.CanonicalizeCommand; +import com.google.devtools.build.lib.runtime.commands.CleanCommand; +import com.google.devtools.build.lib.runtime.commands.DumpCommand; +import com.google.devtools.build.lib.runtime.commands.HelpCommand; +import com.google.devtools.build.lib.runtime.commands.InfoCommand; +import com.google.devtools.build.lib.runtime.commands.MobileInstallCommand; +import com.google.devtools.build.lib.runtime.commands.ProfileCommand; +import com.google.devtools.build.lib.runtime.commands.QueryCommand; +import com.google.devtools.build.lib.runtime.commands.RunCommand; +import com.google.devtools.build.lib.runtime.commands.ShutdownCommand; +import com.google.devtools.build.lib.runtime.commands.TestCommand; +import com.google.devtools.build.lib.runtime.commands.VersionCommand; +import com.google.devtools.common.options.OptionsProvider; + +/** + * Internal module for the built-in commands. + */ +public final class BuiltinCommandModule extends BlazeModule { + @Override + public void serverInit(OptionsProvider startupOptions, ServerBuilder builder) { + builder.addCommands( + new BuildCommand(), + new CanonicalizeCommand(), + new CleanCommand(), + new DumpCommand(), + new HelpCommand(), + new InfoCommand(), + new MobileInstallCommand(), + new ProfileCommand(), + new QueryCommand(), + new RunCommand(), + new ShutdownCommand(), + new TestCommand(), + new VersionCommand()); + } +} diff --git a/src/main/java/com/google/devtools/build/lib/runtime/ServerBuilder.java b/src/main/java/com/google/devtools/build/lib/runtime/ServerBuilder.java new file mode 100644 index 0000000000..3a7a3a2371 --- /dev/null +++ b/src/main/java/com/google/devtools/build/lib/runtime/ServerBuilder.java @@ -0,0 +1,113 @@ +// Copyright 2016 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.lib.runtime; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Function; +import com.google.common.collect.ImmutableList; +import com.google.devtools.build.lib.packages.AttributeContainer; +import com.google.devtools.build.lib.packages.RuleClass; +import com.google.devtools.build.lib.query2.AbstractBlazeQueryEnvironment; +import com.google.devtools.build.lib.query2.QueryEnvironmentFactory; +import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; +import com.google.devtools.build.lib.util.Preconditions; + +/** + * Builder class to create a {@link BlazeRuntime} instance. This class is part of the module API, + * which allows modules to affect how the server is initialized. + */ +public final class ServerBuilder { + private QueryEnvironmentFactory queryEnvironmentFactory; + private final InvocationPolicy.Builder invocationPolicyBuilder = InvocationPolicy.newBuilder(); + private Function<RuleClass, AttributeContainer> attributeContainerFactory; + private final ImmutableList.Builder<BlazeCommand> commands = ImmutableList.builder(); + + @VisibleForTesting + public ServerBuilder() {} + + QueryEnvironmentFactory getQueryEnvironmentFactory() { + return queryEnvironmentFactory == null + ? new QueryEnvironmentFactory() + : queryEnvironmentFactory; + } + + InvocationPolicy getInvocationPolicy() { + return invocationPolicyBuilder.build(); + } + + Function<RuleClass, AttributeContainer> getAttributeContainerFactory() { + return attributeContainerFactory == null + ? AttributeContainer.ATTRIBUTE_CONTAINER_FACTORY + : attributeContainerFactory; + } + + @VisibleForTesting + public ImmutableList<BlazeCommand> getCommands() { + return commands.build(); + } + + /** + * Merges the given invocation policy into the per-server invocation policy. While this can accept + * any number of policies, the end result is order-dependent if multiple policies attempt to + * police the same options, so it's probably a good idea to not have too many modules that call + * this. + */ + public ServerBuilder addInvocationPolicy(InvocationPolicy policy) { + invocationPolicyBuilder.mergeFrom(Preconditions.checkNotNull(policy)); + return this; + } + + /** + * Sets a factory for creating {@link AbstractBlazeQueryEnvironment} instances. Note that only one + * factory per server is allowed. If none is set, the server uses the default implementation. + */ + public ServerBuilder setQueryEnvironmentFactory(QueryEnvironmentFactory queryEnvironmentFactory) { + Preconditions.checkState( + this.queryEnvironmentFactory == null, + "At most one query environment factory supported. But found two: %s and %s", + this.queryEnvironmentFactory, + queryEnvironmentFactory); + this.queryEnvironmentFactory = Preconditions.checkNotNull(queryEnvironmentFactory); + return this; + } + + /** + * Sets a factory for creating {@link AttributeContainer} instances. Only one factory per server + * is allowed. If none is set, the server uses the default implementation. + */ + public ServerBuilder setAttributeContainerFactory( + Function<RuleClass, AttributeContainer> attributeContainerFactory) { + Preconditions.checkState( + this.attributeContainerFactory == null, + "At most one attribute container factory supported. But found two: %s and %s", + this.attributeContainerFactory, + attributeContainerFactory); + this.attributeContainerFactory = Preconditions.checkNotNull(attributeContainerFactory); + return this; + } + + /** + * Adds the given command to the server. This overload only exists to avoid array object creation + * in the common case. + */ + public ServerBuilder addCommands(BlazeCommand command) { + this.commands.add(Preconditions.checkNotNull(command)); + return this; + } + + /** Adds the given commands to the server. */ + public void addCommands(BlazeCommand... commands) { + this.commands.add(commands); + } +} |