Search in sources :

Example 1 with BlazeRuntime

use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.

the class ProjectFileSupport method handleProjectFiles.

/**
   * Reads any project files specified on the command line and updates the options parser
   * accordingly. If project files cannot be read or if they contain unparsable options, or if they
   * are not enabled, then it throws an exception instead.
   */
public static void handleProjectFiles(CommandEnvironment env, OptionsParser optionsParser, String command) throws OptionsParsingException {
    BlazeRuntime runtime = env.getRuntime();
    List<String> targets = optionsParser.getResidue();
    ProjectFile.Provider projectFileProvider = runtime.getProjectFileProvider();
    if (projectFileProvider != null && !targets.isEmpty() && targets.get(0).startsWith(PROJECT_FILE_PREFIX)) {
        if (targets.size() > 1) {
            throw new OptionsParsingException("Cannot handle more than one +<file> argument yet");
        }
        if (!optionsParser.getOptions(CommonCommandOptions.class).allowProjectFiles) {
            throw new OptionsParsingException("project file support is not enabled");
        }
        // TODO(bazel-team): This is currently treated as a path relative to the workspace - if the
        // cwd is a subdirectory of the workspace, that will be surprising, and we should interpret it
        // relative to the cwd instead.
        PathFragment projectFilePath = new PathFragment(targets.get(0).substring(1));
        List<Path> packagePath = PathPackageLocator.create(env.getOutputBase(), optionsParser.getOptions(PackageCacheOptions.class).packagePath, env.getReporter(), env.getWorkspace(), env.getWorkingDirectory()).getPathEntries();
        ProjectFile projectFile = projectFileProvider.getProjectFile(env.getWorkingDirectory(), packagePath, projectFilePath);
        env.getReporter().handle(Event.info("Using " + projectFile.getName()));
        optionsParser.parse(OptionPriority.RC_FILE, projectFile.getName(), projectFile.getCommandLineFor(command));
        env.getEventBus().post(new GotProjectFileEvent(projectFile.getName()));
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) ProjectFile(com.google.devtools.build.lib.runtime.ProjectFile) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) PackageCacheOptions(com.google.devtools.build.lib.pkgcache.PackageCacheOptions) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 2 with BlazeRuntime

use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.

the class QueryCommand method exec.

/**
   * Exit codes:
   *   0   on successful evaluation.
   *   1   if query evaluation did not complete.
   *   2   if query parsing failed.
   *   3   if errors were reported but evaluation produced a partial result
   *        (only when --keep_going is in effect.)
   */
@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    QueryOptions queryOptions = options.getOptions(QueryOptions.class);
    try {
        env.setupPackageCache(options, runtime.getDefaultsPackageContent());
    } catch (InterruptedException e) {
        env.getReporter().handle(Event.error("query interrupted"));
        return ExitCode.INTERRUPTED;
    } catch (AbruptExitException e) {
        env.getReporter().handle(Event.error(null, "Unknown error: " + e.getMessage()));
        return e.getExitCode();
    }
    String query;
    if (!options.getResidue().isEmpty()) {
        if (!queryOptions.queryFile.isEmpty()) {
            env.getReporter().handle(Event.error("Command-line query and --query_file cannot both be specified"));
            return ExitCode.COMMAND_LINE_ERROR;
        }
        query = Joiner.on(' ').join(options.getResidue());
    } else if (!queryOptions.queryFile.isEmpty()) {
        // Works for absolute or relative query file.
        Path residuePath = env.getWorkingDirectory().getRelative(queryOptions.queryFile);
        try {
            query = new String(FileSystemUtils.readContent(residuePath), StandardCharsets.UTF_8);
        } catch (IOException e) {
            env.getReporter().handle(Event.error("I/O error reading from " + residuePath.getPathString()));
            return ExitCode.COMMAND_LINE_ERROR;
        }
    } else {
        env.getReporter().handle(Event.error(String.format("missing query expression. Type '%s help query' for syntax and help", runtime.getProductName())));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Iterable<OutputFormatter> formatters = runtime.getQueryOutputFormatters();
    OutputFormatter formatter = OutputFormatter.getFormatter(formatters, queryOptions.outputFormat);
    if (formatter == null) {
        env.getReporter().handle(Event.error(String.format("Invalid output format '%s'. Valid values are: %s", queryOptions.outputFormat, OutputFormatter.formatterNames(formatters))));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Set<Setting> settings = queryOptions.toSettings();
    boolean streamResults = QueryOutputUtils.shouldStreamResults(queryOptions, formatter);
    QueryEvalResult result;
    AbstractBlazeQueryEnvironment<Target> queryEnv = newQueryEnvironment(env, queryOptions.keepGoing, !streamResults, queryOptions.universeScope, queryOptions.loadingPhaseThreads, settings);
    // 1. Parse and transform query:
    QueryExpression expr;
    try {
        expr = QueryExpression.parse(query, queryEnv);
    } catch (QueryException e) {
        env.getReporter().handle(Event.error(null, "Error while parsing '" + query + "': " + e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    expr = queryEnv.transformParsedQuery(expr);
    OutputStream out = env.getReporter().getOutErr().getOutputStream();
    ThreadSafeOutputFormatterCallback<Target> callback;
    if (streamResults) {
        disableAnsiCharactersFiltering(env);
        // 2. Evaluate expression:
        StreamedFormatter streamedFormatter = ((StreamedFormatter) formatter);
        streamedFormatter.setOptions(queryOptions, queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()));
        callback = streamedFormatter.createStreamCallback(out, queryOptions, queryEnv);
    } else {
        callback = QueryUtil.newOrderedAggregateAllOutputFormatterCallback();
    }
    boolean catastrophe = true;
    try {
        result = queryEnv.evaluateQuery(expr, callback);
        catastrophe = false;
    } catch (QueryException e) {
        catastrophe = false;
        // Keep consistent with reportBuildFileError()
        env.getReporter().handle(Event.error(e.getMessage() == null ? e.toString() : e.getMessage()));
        return ExitCode.ANALYSIS_FAILURE;
    } catch (InterruptedException e) {
        catastrophe = false;
        IOException ioException = callback.getIoException();
        if (ioException == null || ioException instanceof ClosedByInterruptException) {
            env.getReporter().handle(Event.error("query interrupted"));
            return ExitCode.INTERRUPTED;
        } else {
            env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
            return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
        }
    } catch (IOException e) {
        catastrophe = false;
        env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
        return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
    } finally {
        if (!catastrophe) {
            try {
                out.flush();
            } catch (IOException e) {
                env.getReporter().handle(Event.error("Failed to flush query results: " + e.getMessage()));
                return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
            }
        }
    }
    env.getEventBus().post(new NoBuildEvent());
    if (!streamResults) {
        disableAnsiCharactersFiltering(env);
        // 3. Output results:
        try {
            Set<Target> targets = ((AggregateAllOutputFormatterCallback<Target>) callback).getResult();
            QueryOutputUtils.output(queryOptions, result, targets, formatter, env.getReporter().getOutErr().getOutputStream(), queryOptions.aspectDeps.createResolver(env.getPackageManager(), env.getReporter()));
        } catch (ClosedByInterruptException | InterruptedException e) {
            env.getReporter().handle(Event.error("query interrupted"));
            return ExitCode.INTERRUPTED;
        } catch (IOException e) {
            env.getReporter().handle(Event.error("I/O error: " + e.getMessage()));
            return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
        } finally {
            try {
                out.flush();
            } catch (IOException e) {
                env.getReporter().handle(Event.error("Failed to flush query results: " + e.getMessage()));
                return ExitCode.LOCAL_ENVIRONMENTAL_ERROR;
            }
        }
    }
    if (result.isEmpty()) {
        env.getReporter().handle(Event.info("Empty results"));
    }
    return result.getSuccess() ? ExitCode.SUCCESS : ExitCode.PARTIAL_ANALYSIS_FAILURE;
}
Also used : OutputStream(java.io.OutputStream) QueryEvalResult(com.google.devtools.build.lib.query2.engine.QueryEvalResult) StreamedFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter.StreamedFormatter) AggregateAllOutputFormatterCallback(com.google.devtools.build.lib.query2.engine.QueryUtil.AggregateAllOutputFormatterCallback) QueryOptions(com.google.devtools.build.lib.query2.output.QueryOptions) OutputFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) Target(com.google.devtools.build.lib.packages.Target) QueryExpression(com.google.devtools.build.lib.query2.engine.QueryExpression) Path(com.google.devtools.build.lib.vfs.Path) Setting(com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting) NoBuildEvent(com.google.devtools.build.lib.analysis.NoBuildEvent) IOException(java.io.IOException) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) AbruptExitException(com.google.devtools.build.lib.util.AbruptExitException)

Example 3 with BlazeRuntime

use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.

the class TestCommand method doTest.

private ExitCode doTest(CommandEnvironment env, OptionsProvider options, AggregatingTestListener testListener) {
    BlazeRuntime runtime = env.getRuntime();
    // Run simultaneous build and test.
    List<String> targets = ProjectFileSupport.getTargets(runtime, options);
    BuildRequest request = BuildRequest.create(getClass().getAnnotation(Command.class).name(), options, runtime.getStartupOptionsProvider(), targets, env.getReporter().getOutErr(), env.getCommandId(), env.getCommandStartTime());
    request.setRunTests();
    BuildResult buildResult = new BuildTool(env).processRequest(request, null);
    Collection<ConfiguredTarget> testTargets = buildResult.getTestTargets();
    // TODO(bazel-team): don't handle isEmpty here or fix up a bunch of tests
    if (buildResult.getSuccessfulTargets() == null) {
        // This can happen if there were errors in the target parsing or loading phase
        // (original exitcode=BUILD_FAILURE) or if there weren't but --noanalyze was given
        // (original exitcode=SUCCESS).
        env.getReporter().handle(Event.error("Couldn't start the build. Unable to run tests"));
        return buildResult.getSuccess() ? ExitCode.PARSING_FAILURE : buildResult.getExitCondition();
    }
    // more tests
    if (testTargets.isEmpty()) {
        env.getReporter().handle(Event.error(null, "No test targets were found, yet testing was requested"));
        return buildResult.getSuccess() ? ExitCode.NO_TESTS_FOUND : buildResult.getExitCondition();
    }
    boolean buildSuccess = buildResult.getSuccess();
    boolean testSuccess = analyzeTestResults(testTargets, testListener, options);
    if (testSuccess && !buildSuccess) {
        // If all tests run successfully, test summary should include warning if
        // there were build errors not associated with the test targets.
        printer.printLn(AnsiTerminalPrinter.Mode.ERROR + "All tests passed but there were other errors during the build.\n" + AnsiTerminalPrinter.Mode.DEFAULT);
    }
    return buildSuccess ? (testSuccess ? ExitCode.SUCCESS : ExitCode.TESTS_FAILED) : buildResult.getExitCondition();
}
Also used : BuildResult(com.google.devtools.build.lib.buildtool.BuildResult) BuildTool(com.google.devtools.build.lib.buildtool.BuildTool) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) BuildRequest(com.google.devtools.build.lib.buildtool.BuildRequest) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 4 with BlazeRuntime

use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.

the class BuildCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    List<String> targets = ProjectFileSupport.getTargets(runtime, options);
    BuildRequest request = BuildRequest.create(getClass().getAnnotation(Command.class).name(), options, runtime.getStartupOptionsProvider(), targets, env.getReporter().getOutErr(), env.getCommandId(), env.getCommandStartTime());
    return new BuildTool(env).processRequest(request, null).getExitCondition();
}
Also used : BuildTool(com.google.devtools.build.lib.buildtool.BuildTool) BuildRequest(com.google.devtools.build.lib.buildtool.BuildRequest) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Example 5 with BlazeRuntime

use of com.google.devtools.build.lib.runtime.BlazeRuntime in project bazel by bazelbuild.

the class CanonicalizeCommand method exec.

@Override
public ExitCode exec(CommandEnvironment env, OptionsProvider options) {
    BlazeRuntime runtime = env.getRuntime();
    Options canonicalizeOptions = options.getOptions(Options.class);
    String commandName = canonicalizeOptions.forCommand;
    BlazeCommand command = runtime.getCommandMap().get(commandName);
    if (command == null) {
        env.getReporter().handle(Event.error("Not a valid command: '" + commandName + "' (should be one of " + Joiner.on(", ").join(runtime.getCommandMap().keySet()) + ")"));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    Collection<Class<? extends OptionsBase>> optionsClasses = BlazeCommandUtils.getOptions(command.getClass(), runtime.getBlazeModules(), runtime.getRuleClassProvider());
    try {
        OptionsParser parser = OptionsParser.newOptionsParser(optionsClasses);
        parser.setAllowResidue(false);
        parser.parse(options.getResidue());
        InvocationPolicyEnforcer invocationPolicyEnforcer = InvocationPolicyEnforcer.create(canonicalizeOptions.invocationPolicy);
        invocationPolicyEnforcer.enforce(parser, commandName);
        List<String> result = parser.canonicalize();
        for (String piece : result) {
            env.getReporter().getOutErr().printOutLn(piece);
        }
    } catch (OptionsParsingException e) {
        env.getReporter().handle(Event.error(e.getMessage()));
        return ExitCode.COMMAND_LINE_ERROR;
    }
    return ExitCode.SUCCESS;
}
Also used : OptionsBase(com.google.devtools.common.options.OptionsBase) BlazeCommand(com.google.devtools.build.lib.runtime.BlazeCommand) InvocationPolicyEnforcer(com.google.devtools.build.lib.flags.InvocationPolicyEnforcer) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) BlazeRuntime(com.google.devtools.build.lib.runtime.BlazeRuntime)

Aggregations

BlazeRuntime (com.google.devtools.build.lib.runtime.BlazeRuntime)11 NoBuildEvent (com.google.devtools.build.lib.analysis.NoBuildEvent)3 BuildRequest (com.google.devtools.build.lib.buildtool.BuildRequest)3 BuildTool (com.google.devtools.build.lib.buildtool.BuildTool)3 Target (com.google.devtools.build.lib.packages.Target)3 BlazeCommand (com.google.devtools.build.lib.runtime.BlazeCommand)3 AbruptExitException (com.google.devtools.build.lib.util.AbruptExitException)3 IOException (java.io.IOException)3 RuleClass (com.google.devtools.build.lib.packages.RuleClass)2 PackageCacheOptions (com.google.devtools.build.lib.pkgcache.PackageCacheOptions)2 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)2 QueryExpression (com.google.devtools.build.lib.query2.engine.QueryExpression)2 OutErr (com.google.devtools.build.lib.util.io.OutErr)2 Path (com.google.devtools.build.lib.vfs.Path)2 OptionsBase (com.google.devtools.common.options.OptionsBase)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 ConfiguredRuleClassProvider (com.google.devtools.build.lib.analysis.ConfiguredRuleClassProvider)1