Search in sources :

Example 11 with AbruptExitException

use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.

the class BlazeRuntime method newRuntime.

/**
   * Creates a new blaze runtime, given the install and output base directories.
   *
   * <p>Note: This method can and should only be called once per startup, as it also creates the
   * filesystem object that will be used for the runtime. So it should only ever be called from the
   * main method of the Blaze program.
   *
   * @param args Blaze startup options.
   *
   * @return a new BlazeRuntime instance initialized with the given filesystem and directories, and
   *         an error string that, if not null, describes a fatal initialization failure that makes
   *         this runtime unsuitable for real commands
   */
private static BlazeRuntime newRuntime(Iterable<BlazeModule> blazeModules, List<String> args, Runnable abruptShutdownHandler) throws AbruptExitException, OptionsParsingException {
    OptionsProvider options = parseOptions(blazeModules, args);
    for (BlazeModule module : blazeModules) {
        module.globalInit(options);
    }
    BlazeServerStartupOptions startupOptions = options.getOptions(BlazeServerStartupOptions.class);
    String productName = startupOptions.productName.toLowerCase(Locale.US);
    if (startupOptions.oomMoreEagerlyThreshold != 100) {
        new RetainedHeapLimiter(startupOptions.oomMoreEagerlyThreshold).install();
    }
    PathFragment workspaceDirectory = startupOptions.workspaceDirectory;
    PathFragment installBase = startupOptions.installBase;
    PathFragment outputBase = startupOptions.outputBase;
    // Must be before first use of JNI.
    maybeForceJNI(installBase);
    // are mandatory options, despite the comment in their declarations.
    if (installBase == null || !installBase.isAbsolute()) {
        // (includes "" default case)
        throw new IllegalArgumentException("Bad --install_base option specified: '" + installBase + "'");
    }
    if (outputBase != null && !outputBase.isAbsolute()) {
        // (includes "" default case)
        throw new IllegalArgumentException("Bad --output_base option specified: '" + outputBase + "'");
    }
    FileSystem fs = null;
    for (BlazeModule module : blazeModules) {
        FileSystem moduleFs = module.getFileSystem(options);
        if (moduleFs != null) {
            Preconditions.checkState(fs == null, "more than one module returns a file system");
            fs = moduleFs;
        }
    }
    if (fs == null) {
        fs = fileSystemImplementation();
    }
    Path.setFileSystemForSerialization(fs);
    SubprocessBuilder.setSubprocessFactory(subprocessFactoryImplementation());
    Path installBasePath = fs.getPath(installBase);
    Path outputBasePath = fs.getPath(outputBase);
    Path workspaceDirectoryPath = null;
    if (!workspaceDirectory.equals(PathFragment.EMPTY_FRAGMENT)) {
        workspaceDirectoryPath = fs.getPath(workspaceDirectory);
    }
    ServerDirectories serverDirectories = new ServerDirectories(installBasePath, outputBasePath, startupOptions.installMD5);
    Clock clock = BlazeClock.instance();
    BlazeRuntime.Builder runtimeBuilder = new BlazeRuntime.Builder().setProductName(productName).setServerDirectories(serverDirectories).setStartupOptionsProvider(options).setClock(clock).setAbruptShutdownHandler(abruptShutdownHandler).setEventBusExceptionHandler(startupOptions.fatalEventBusExceptions || !BlazeVersionInfo.instance().isReleasedBlaze() ? new BlazeRuntime.BugReportingExceptionHandler() : new BlazeRuntime.RemoteExceptionHandler());
    if (System.getenv("TEST_TMPDIR") != null && System.getenv("NO_CRASH_ON_LOGGING_IN_TEST") == null) {
        LoggingUtil.installRemoteLogger(getTestCrashLogger());
    }
    runtimeBuilder.addBlazeModule(new BuiltinCommandModule());
    for (BlazeModule blazeModule : blazeModules) {
        runtimeBuilder.addBlazeModule(blazeModule);
    }
    BlazeRuntime runtime = runtimeBuilder.build();
    BlazeDirectories directories = new BlazeDirectories(serverDirectories, workspaceDirectoryPath, startupOptions.deepExecRoot, productName);
    BinTools binTools;
    try {
        binTools = BinTools.forProduction(directories);
    } catch (IOException e) {
        throw new AbruptExitException("Cannot enumerate embedded binaries: " + e.getMessage(), ExitCode.LOCAL_ENVIRONMENTAL_ERROR);
    }
    runtime.initWorkspace(directories, binTools);
    if (startupOptions.useCustomExitCodeOnAbruptExit) {
        CustomExitCodePublisher.setAbruptExitStatusFileDir(serverDirectories.getOutputBase());
    }
    AutoProfiler.setClock(runtime.getClock());
    BugReport.setRuntime(runtime);
    return runtime;
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) OptionsProvider(com.google.devtools.common.options.OptionsProvider) SubprocessBuilder(com.google.devtools.build.lib.shell.SubprocessBuilder) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) IOException(java.io.IOException) Clock(com.google.devtools.build.lib.util.Clock) BlazeClock(com.google.devtools.build.lib.util.BlazeClock) ServerDirectories(com.google.devtools.build.lib.analysis.ServerDirectories) BinTools(com.google.devtools.build.lib.analysis.config.BinTools) BlazeDirectories(com.google.devtools.build.lib.analysis.BlazeDirectories) JavaIoFileSystem(com.google.devtools.build.lib.vfs.JavaIoFileSystem) FileSystem(com.google.devtools.build.lib.vfs.FileSystem) WindowsFileSystem(com.google.devtools.build.lib.windows.WindowsFileSystem) UnixFileSystem(com.google.devtools.build.lib.unix.UnixFileSystem) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 12 with AbruptExitException

use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.

the class BlazeRuntime method createBlazeRPCServer.

/**
   * Creates and returns a new Blaze RPCServer. Call {@link RPCServer#serve()} to start the server.
   */
private static RPCServer createBlazeRPCServer(Iterable<BlazeModule> modules, List<String> args) throws IOException, OptionsParsingException, AbruptExitException {
    final RPCServer[] rpcServer = new RPCServer[1];
    Runnable prepareForAbruptShutdown = new Runnable() {

        @Override
        public void run() {
            rpcServer[0].prepareForAbruptShutdown();
        }
    };
    BlazeRuntime runtime = newRuntime(modules, args, prepareForAbruptShutdown);
    BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime);
    CommandExecutor commandExecutor = new CommandExecutor(runtime, dispatcher);
    BlazeServerStartupOptions startupOptions = runtime.getStartupOptionsProvider().getOptions(BlazeServerStartupOptions.class);
    try {
        // This is necessary so that Bazel kind of works during bootstrapping, at which time the
        // gRPC server is not compiled in so that we don't need gRPC for bootstrapping.
        Class<?> factoryClass = Class.forName("com.google.devtools.build.lib.server.GrpcServerImpl$Factory");
        RPCServer.Factory factory = (RPCServer.Factory) factoryClass.getConstructor().newInstance();
        rpcServer[0] = factory.create(commandExecutor, runtime.getClock(), startupOptions.commandPort, runtime.getWorkspace().getWorkspace(), runtime.getServerDirectory(), startupOptions.maxIdleSeconds);
        return rpcServer[0];
    } catch (ReflectiveOperationException | IllegalArgumentException e) {
        throw new AbruptExitException("gRPC server not compiled in", ExitCode.BLAZE_INTERNAL_ERROR);
    }
}
Also used : CoverageReportActionFactory(com.google.devtools.build.lib.rules.test.CoverageReportActionFactory) QueryEnvironmentFactory(com.google.devtools.build.lib.query2.QueryEnvironmentFactory) WindowsSubprocessFactory(com.google.devtools.build.lib.windows.WindowsSubprocessFactory) JavaSubprocessFactory(com.google.devtools.build.lib.shell.JavaSubprocessFactory) ConfigurationFactory(com.google.devtools.build.lib.analysis.config.ConfigurationFactory) PackageFactory(com.google.devtools.build.lib.packages.PackageFactory) RPCServer(com.google.devtools.build.lib.server.RPCServer) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 13 with AbruptExitException

use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.

the class BlazeRuntime method serverMain.

/**
   * A main method that does not send email. The return value indicates the desired exit status of
   * the program.
   */
private static int serverMain(Iterable<BlazeModule> modules, OutErr outErr, String[] args) {
    InterruptSignalHandler sigintHandler = null;
    try {
        final RPCServer blazeServer = createBlazeRPCServer(modules, Arrays.asList(args));
        // Register the signal handler.
        sigintHandler = new InterruptSignalHandler() {

            @Override
            public void run() {
                LOG.severe("User interrupt");
                blazeServer.interrupt();
            }
        };
        blazeServer.serve();
        return ExitCode.SUCCESS.getNumericExitCode();
    } catch (OptionsParsingException e) {
        outErr.printErr(e.getMessage());
        return ExitCode.COMMAND_LINE_ERROR.getNumericExitCode();
    } catch (IOException e) {
        outErr.printErr("I/O Error: " + e.getMessage());
        return ExitCode.BUILD_FAILURE.getNumericExitCode();
    } catch (AbruptExitException e) {
        outErr.printErr(e.getMessage());
        return e.getExitCode().getNumericExitCode();
    } finally {
        if (sigintHandler != null) {
            sigintHandler.uninstall();
        }
    }
}
Also used : InterruptSignalHandler(com.google.devtools.build.lib.server.signal.InterruptSignalHandler) RPCServer(com.google.devtools.build.lib.server.RPCServer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) IOException(java.io.IOException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 14 with AbruptExitException

use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.

the class InfoCommand method exec.

@Override
public ExitCode exec(final CommandEnvironment env, final OptionsProvider optionsProvider) {
    final BlazeRuntime runtime = env.getRuntime();
    env.getReporter().switchToAnsiAllowingHandler();
    Options infoOptions = optionsProvider.getOptions(Options.class);
    OutErr outErr = env.getReporter().getOutErr();
    // Creating a BuildConfiguration is expensive and often unnecessary. Delay the creation until
    // it is needed.
    Supplier<BuildConfiguration> configurationSupplier = new Supplier<BuildConfiguration>() {

        private BuildConfiguration configuration;

        @Override
        public BuildConfiguration get() {
            if (configuration != null) {
                return configuration;
            }
            try {
                // In order to be able to answer configuration-specific queries, we need to setup the
                // package path. Since info inherits all the build options, all the necessary information
                // is available here.
                env.setupPackageCache(optionsProvider, runtime.getDefaultsPackageContent(optionsProvider));
                // TODO(bazel-team): What if there are multiple configurations? [multi-config]
                configuration = env.getConfigurations(optionsProvider).getTargetConfigurations().get(0);
                return configuration;
            } catch (InvalidConfigurationException e) {
                env.getReporter().handle(Event.error(e.getMessage()));
                throw new ExitCausingRuntimeException(ExitCode.COMMAND_LINE_ERROR);
            } catch (AbruptExitException e) {
                throw new ExitCausingRuntimeException("unknown error: " + e.getMessage(), e.getExitCode());
            } catch (InterruptedException e) {
                env.getReporter().handle(Event.error("interrupted"));
                throw new ExitCausingRuntimeException(ExitCode.INTERRUPTED);
            }
        }
    };
    Map<String, InfoItem> items = getInfoItemMap(env, optionsProvider);
    try {
        if (infoOptions.showMakeEnvironment) {
            Map<String, String> makeEnv = configurationSupplier.get().getMakeEnvironment();
            for (Map.Entry<String, String> entry : makeEnv.entrySet()) {
                InfoItem item = new InfoItem.MakeInfoItem(entry.getKey(), entry.getValue());
                items.put(item.getName(), item);
            }
        }
        List<String> residue = optionsProvider.getResidue();
        if (residue.size() > 1) {
            env.getReporter().handle(Event.error("at most one key may be specified"));
            return ExitCode.COMMAND_LINE_ERROR;
        }
        String key = residue.size() == 1 ? residue.get(0) : null;
        env.getEventBus().post(new NoBuildEvent());
        if (key != null) {
            // print just the value for the specified key:
            byte[] value;
            if (items.containsKey(key)) {
                value = items.get(key).get(configurationSupplier, env);
            } else {
                env.getReporter().handle(Event.error("unknown key: '" + key + "'"));
                return ExitCode.COMMAND_LINE_ERROR;
            }
            try {
                outErr.getOutputStream().write(value);
                outErr.getOutputStream().flush();
            } catch (IOException e) {
                env.getReporter().handle(Event.error("Cannot write info block: " + e.getMessage()));
                return ExitCode.ANALYSIS_FAILURE;
            }
        } else {
            // print them all
            // We'll need this later anyway
            configurationSupplier.get();
            for (InfoItem infoItem : items.values()) {
                if (infoItem.isHidden()) {
                    continue;
                }
                outErr.getOutputStream().write((infoItem.getName() + ": ").getBytes(StandardCharsets.UTF_8));
                outErr.getOutputStream().write(infoItem.get(configurationSupplier, env));
            }
        }
    } catch (AbruptExitException e) {
        return e.getExitCode();
    } catch (ExitCausingRuntimeException e) {
        return e.getExitCode();
    } catch (IOException e) {
        return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
    } catch (InterruptedException e) {
        return ExitCode.INTERRUPTED;
    }
    return ExitCode.SUCCESS;
}
Also used : OutErr(com.google.devtools.build.lib.util.io.OutErr) NoBuildEvent(com.google.devtools.build.lib.analysis.NoBuildEvent) IOException(java.io.IOException) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime) InvalidConfigurationException(com.google.devtools.build.lib.analysis.config.InvalidConfigurationException) BuildConfiguration(com.google.devtools.build.lib.analysis.config.BuildConfiguration) Supplier(com.google.common.base.Supplier) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) ImmutableMap(com.google.common.collect.ImmutableMap) TreeMap(java.util.TreeMap) Map(java.util.Map)

Example 15 with AbruptExitException

use of com.google.devtools.build.lib.util.AbruptExitException in project bazel by bazelbuild.

the class BuildEventStreamerModule method tryCreateStreamer.

@VisibleForTesting
Optional<BuildEventStreamer> tryCreateStreamer(OptionsProvider optionsProvider, ModuleEnvironment moduleEnvironment) {
    try {
        PathConverter pathConverter;
        if (commandEnvironment == null) {
            pathConverter = new PathConverter() {

                @Override
                public String apply(Path path) {
                    return path.getPathString();
                }
            };
        } else {
            pathConverter = commandEnvironment.getRuntime().getPathToUriConverter();
        }
        BuildEventStreamOptions besOptions = checkNotNull(optionsProvider.getOptions(BuildEventStreamOptions.class), "Could not get BuildEventStreamOptions");
        ImmutableSet<BuildEventTransport> buildEventTransports = createFromOptions(besOptions, pathConverter);
        if (!buildEventTransports.isEmpty()) {
            BuildEventStreamer streamer = new BuildEventStreamer(buildEventTransports);
            return Optional.of(streamer);
        }
    } catch (IOException e) {
        moduleEnvironment.exit(new AbruptExitException(ExitCode.LOCAL_ENVIRONMENTAL_ERROR, e));
    }
    return Optional.absent();
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) BuildEventStreamOptions(com.google.devtools.build.lib.buildeventstream.transports.BuildEventStreamOptions) BuildEventTransport(com.google.devtools.build.lib.buildeventstream.BuildEventTransport) PathConverter(com.google.devtools.build.lib.buildeventstream.PathConverter) IOException(java.io.IOException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)15 IOException (java.io.IOException)8 Test (org.junit.Test)5 Path (com.google.devtools.build.lib.vfs.Path)4 BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)2 InvalidConfigurationException (com.google.devtools.build.lib.analysis.config.InvalidConfigurationException)2 Target (com.google.devtools.build.lib.packages.Target)2 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)2 QueryExpression (com.google.devtools.build.lib.query2.engine.QueryExpression)2 RPCServer (com.google.devtools.build.lib.server.RPCServer)2 ExitCode (com.google.devtools.build.lib.util.ExitCode)2 OutErr (com.google.devtools.build.lib.util.io.OutErr)2 OptionsParsingException (com.google.devtools.common.options.OptionsParsingException)2 Supplier (com.google.common.base.Supplier)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 BuildFailedException (com.google.devtools.build.lib.actions.BuildFailedException)1