Search in sources :

Example 1 with BlazeQueryEnvironment

use of com.google.devtools.build.lib.query2.BlazeQueryEnvironment in project bazel by bazelbuild.

the class GenQuery method doQuery.

@SuppressWarnings("unchecked")
@Nullable
private byte[] doQuery(QueryOptions queryOptions, PackageProvider packageProvider, Predicate<Label> labelFilter, TargetPatternEvaluator evaluator, String query, RuleContext ruleContext) throws InterruptedException {
    DigraphQueryEvalResult<Target> queryResult;
    OutputFormatter formatter;
    AggregateAllOutputFormatterCallback<Target> targets = QueryUtil.newOrderedAggregateAllOutputFormatterCallback();
    try {
        Set<Setting> settings = queryOptions.toSettings();
        // Turns out, if we have two targets with a cycle of length 2 were one of
        // the edges is of type NODEP_LABEL type, the targets both show up in
        // each other's result for deps(X) when the query is executed using
        // 'blaze query'. This obviously does not fly when doing the query as a
        // part of the build, thus, there is a slight discrepancy between the
        // behavior of the query engine in these two use cases.
        settings.add(Setting.NO_NODEP_DEPS);
        ImmutableList<OutputFormatter> outputFormatters = QUERY_OUTPUT_FORMATTERS.get(ruleContext.getAnalysisEnvironment().getSkyframeEnv());
        // This is a precomputed value so it should have been injected by the rules module by the
        // time we get there.
        formatter = OutputFormatter.getFormatter(Preconditions.checkNotNull(outputFormatters), queryOptions.outputFormat);
        // All the packages are already loaded at this point, so there is no need
        // to start up many threads. 4 are started up to make good use of multiple
        // cores.
        BlazeQueryEnvironment queryEnvironment = (BlazeQueryEnvironment) QUERY_ENVIRONMENT_FACTORY.create(/*transitivePackageLoader=*/
        null, /*graph=*/
        null, packageProvider, evaluator, /*keepGoing=*/
        false, ruleContext.attributes().get("strict", Type.BOOLEAN), /*orderedResults=*/
        !QueryOutputUtils.shouldStreamResults(queryOptions, formatter), /*universeScope=*/
        ImmutableList.<String>of(), /*loadingPhaseThreads=*/
        4, labelFilter, getEventHandler(ruleContext), settings, ImmutableList.<QueryFunction>of(), /*packagePath=*/
        null, /*blockUniverseEvaluationErrors=*/
        false);
        queryResult = (DigraphQueryEvalResult<Target>) queryEnvironment.evaluateQuery(query, targets);
    } catch (SkyframeRestartQueryException e) {
        // inconsistent from run to run, and make detecting legitimate errors more difficult.
        return null;
    } catch (QueryException e) {
        ruleContext.ruleError("query failed: " + e.getMessage());
        return null;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try {
        QueryOutputUtils.output(queryOptions, queryResult, targets.getResult(), formatter, outputStream, queryOptions.aspectDeps.createResolver(packageProvider, getEventHandler(ruleContext)));
    } catch (ClosedByInterruptException e) {
        throw new InterruptedException(e.getMessage());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return outputStream.toByteArray();
}
Also used : Setting(com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputFormatter(com.google.devtools.build.lib.query2.output.OutputFormatter) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ConfiguredTarget(com.google.devtools.build.lib.analysis.ConfiguredTarget) Target(com.google.devtools.build.lib.packages.Target) QueryException(com.google.devtools.build.lib.query2.engine.QueryException) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) BlazeQueryEnvironment(com.google.devtools.build.lib.query2.BlazeQueryEnvironment) QueryFunction(com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction) SkyframeRestartQueryException(com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException) Nullable(javax.annotation.Nullable)

Aggregations

ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 Target (com.google.devtools.build.lib.packages.Target)1 BlazeQueryEnvironment (com.google.devtools.build.lib.query2.BlazeQueryEnvironment)1 QueryFunction (com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction)1 Setting (com.google.devtools.build.lib.query2.engine.QueryEnvironment.Setting)1 QueryException (com.google.devtools.build.lib.query2.engine.QueryException)1 SkyframeRestartQueryException (com.google.devtools.build.lib.query2.engine.SkyframeRestartQueryException)1 OutputFormatter (com.google.devtools.build.lib.query2.output.OutputFormatter)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 Nullable (javax.annotation.Nullable)1