Search in sources :

Example 6 with Build

use of com.google.devtools.build.lib.query2.proto.proto2api.Build in project bazel by bazelbuild.

the class GenQuery method create.

@Override
@Nullable
public ConfiguredTarget create(RuleContext ruleContext) throws InterruptedException, RuleErrorException {
    Artifact outputArtifact = ruleContext.createOutputArtifact();
    // The query string
    final String query = ruleContext.attributes().get("expression", Type.STRING);
    OptionsParser optionsParser = OptionsParser.newOptionsParser(QueryOptions.class);
    optionsParser.setAllowResidue(false);
    try {
        optionsParser.parse(ruleContext.attributes().get("opts", Type.STRING_LIST));
    } catch (OptionsParsingException e) {
        ruleContext.attributeError("opts", "error while parsing query options: " + e.getMessage());
        return null;
    }
    // Parsed query options
    QueryOptions queryOptions = optionsParser.getOptions(QueryOptions.class);
    if (queryOptions.keepGoing) {
        ruleContext.attributeError("opts", "option --keep_going is not allowed");
        return null;
    }
    if (!queryOptions.universeScope.isEmpty()) {
        ruleContext.attributeError("opts", "option --universe_scope is not allowed");
        return null;
    }
    if (optionsParser.containsExplicitOption("order_results")) {
        ruleContext.attributeError("opts", "option --order_results is not allowed");
        return null;
    }
    if (optionsParser.containsExplicitOption("noorder_results")) {
        ruleContext.attributeError("opts", "option --noorder_results is not allowed");
        return null;
    }
    if (optionsParser.containsExplicitOption("order_output")) {
        ruleContext.attributeError("opts", "option --order_output is not allowed");
        return null;
    }
    // Force results to be deterministic.
    queryOptions.orderOutput = OrderOutput.FULL;
    // force relative_locations to true so it has a deterministic output across machines.
    queryOptions.relativeLocations = true;
    final byte[] result = executeQuery(ruleContext, queryOptions, getScope(ruleContext), query);
    if (result == null || ruleContext.hasErrors()) {
        return null;
    }
    ruleContext.registerAction(new QueryResultAction(ruleContext.getActionOwner(), outputArtifact, result));
    NestedSet<Artifact> filesToBuild = NestedSetBuilder.create(Order.STABLE_ORDER, outputArtifact);
    return new RuleConfiguredTargetBuilder(ruleContext).setFilesToBuild(filesToBuild).add(RunfilesProvider.class, RunfilesProvider.simple(new Runfiles.Builder(ruleContext.getWorkspaceName(), ruleContext.getConfiguration().legacyExternalRunfiles()).addTransitiveArtifacts(filesToBuild).build())).build();
}
Also used : Runfiles(com.google.devtools.build.lib.analysis.Runfiles) RuleConfiguredTargetBuilder(com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder) OptionsParsingException(com.google.devtools.common.options.OptionsParsingException) OptionsParser(com.google.devtools.common.options.OptionsParser) QueryOptions(com.google.devtools.build.lib.query2.output.QueryOptions) RunfilesProvider(com.google.devtools.build.lib.analysis.RunfilesProvider) Artifact(com.google.devtools.build.lib.actions.Artifact) Nullable(javax.annotation.Nullable)

Example 7 with Build

use of com.google.devtools.build.lib.query2.proto.proto2api.Build 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)

Example 8 with Build

use of com.google.devtools.build.lib.query2.proto.proto2api.Build in project bazel by bazelbuild.

the class InfoItem method getBuildLanguageDefinition.

/**
   * Returns a byte array containing a proto-buffer describing the build language.
   */
private static byte[] getBuildLanguageDefinition(RuleClassProvider provider) {
    BuildLanguage.Builder resultPb = BuildLanguage.newBuilder();
    Collection<RuleClass> ruleClasses = provider.getRuleClassMap().values();
    for (RuleClass ruleClass : ruleClasses) {
        if (!ruleClass.isDocumented()) {
            continue;
        }
        RuleDefinition.Builder rulePb = RuleDefinition.newBuilder();
        rulePb.setName(ruleClass.getName());
        for (Attribute attr : ruleClass.getAttributes()) {
            if (!attr.isDocumented()) {
                continue;
            }
            AttributeDefinition.Builder attrPb = AttributeDefinition.newBuilder();
            attrPb.setName(attr.getName());
            // The protocol compiler, in its infinite wisdom, generates the field as one of the
            // integer type and the getTypeEnum() method is missing. WTF?
            attrPb.setType(ProtoUtils.getDiscriminatorFromType(attr.getType()));
            attrPb.setMandatory(attr.isMandatory());
            if (BuildType.isLabelType(attr.getType())) {
                attrPb.setAllowedRuleClasses(getAllowedRuleClasses(ruleClasses, attr));
            }
            rulePb.addAttribute(attrPb);
        }
        resultPb.addRule(rulePb);
    }
    return resultPb.build().toByteArray();
}
Also used : Attribute(com.google.devtools.build.lib.packages.Attribute) AttributeDefinition(com.google.devtools.build.lib.query2.proto.proto2api.Build.AttributeDefinition) RuleDefinition(com.google.devtools.build.lib.query2.proto.proto2api.Build.RuleDefinition) RuleClass(com.google.devtools.build.lib.packages.RuleClass) BuildLanguage(com.google.devtools.build.lib.query2.proto.proto2api.Build.BuildLanguage)

Aggregations

QueryException (com.google.devtools.build.lib.query2.engine.QueryException)4 Attribute (com.google.devtools.build.lib.packages.Attribute)3 Target (com.google.devtools.build.lib.packages.Target)3 IOException (java.io.IOException)3 Label (com.google.devtools.build.lib.cmdline.Label)2 EnvironmentGroup (com.google.devtools.build.lib.packages.EnvironmentGroup)2 InputFile (com.google.devtools.build.lib.packages.InputFile)2 OutputFile (com.google.devtools.build.lib.packages.OutputFile)2 PackageGroup (com.google.devtools.build.lib.packages.PackageGroup)2 Rule (com.google.devtools.build.lib.packages.Rule)2 FakeSubincludeTarget (com.google.devtools.build.lib.query2.FakeSubincludeTarget)2 LinkedHashSet (java.util.LinkedHashSet)2 Nullable (javax.annotation.Nullable)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 ImmutableList (com.google.common.collect.ImmutableList)1 Artifact (com.google.devtools.build.lib.actions.Artifact)1 ConfiguredTarget (com.google.devtools.build.lib.analysis.ConfiguredTarget)1 RuleConfiguredTargetBuilder (com.google.devtools.build.lib.analysis.RuleConfiguredTargetBuilder)1 Runfiles (com.google.devtools.build.lib.analysis.Runfiles)1 RunfilesProvider (com.google.devtools.build.lib.analysis.RunfilesProvider)1