aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/buildtool/CqueryBuildTool.java
blob: 8bf7a0b112c442c3009c7384e5b1580908a935d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2018 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.buildtool;

import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.BuildView.AnalysisResult;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.TargetAndConfiguration;
import com.google.devtools.build.lib.analysis.ViewCreationFailedException;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.BuildConfigurationCollection;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.query2.ConfiguredTargetQueryEnvironment;
import com.google.devtools.build.lib.query2.TransitionsOutputFormatterCallback;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryFunction;
import com.google.devtools.build.lib.query2.engine.QueryEvalResult;
import com.google.devtools.build.lib.query2.engine.QueryException;
import com.google.devtools.build.lib.query2.engine.QueryExpression;
import com.google.devtools.build.lib.query2.engine.TargetLiteral;
import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback;
import com.google.devtools.build.lib.query2.output.CqueryOptions;
import com.google.devtools.build.lib.query2.output.CqueryOptions.Transitions;
import com.google.devtools.build.lib.runtime.CommandEnvironment;
import com.google.devtools.build.lib.skyframe.SkyframeExecutorWrappingWalkableGraph;
import com.google.devtools.build.skyframe.WalkableGraph;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/** A version of {@link BuildTool} that handles all cquery work. */
public class CqueryBuildTool extends BuildTool {

  private final QueryExpression queryExpression;

  public CqueryBuildTool(CommandEnvironment env, QueryExpression queryExpression) {
    super(env);
    this.queryExpression = queryExpression;
  }

  @Override
  protected void postProcessAnalysisResult(
      BuildRequest request,
      AnalysisResult analysisResult,
      BuildConfigurationCollection configurations)
      throws InterruptedException, ViewCreationFailedException,
          ConfiguredTargetQueryCommandLineException {
    // TODO: b/71905538 - this query will operate over the graph as constructed by analysis, but
    // will also pick up any nodes that are in the graph from prior builds. This makes the results
    // not reproducible at the level of a single command. Either tolerate, or wipe the analysis
    // graph beforehand if this option is specified, or add another option to wipe if desired
    // (SkyframeExecutor#handleConfiguredTargetChange should be sufficient).
    if (queryExpression != null) {
      if (!env.getSkyframeExecutor().tracksStateForIncrementality()) {
        throw new ConfiguredTargetQueryCommandLineException(
            "Configured query is not allowed if incrementality state is not being kept");
      }
      try {
        doConfiguredTargetQuery(
            request,
            configurations.getHostConfiguration(),
            analysisResult.getTopLevelTargetsWithConfigs(),
            queryExpression);
      } catch (QueryException | IOException e) {
        if (!request.getKeepGoing()) {
          throw new ViewCreationFailedException("Error doing configured target query", e);
        }
        env.getReporter().error(null, "Error doing configured target query", e);
      }
    }
  }

  private void doConfiguredTargetQuery(
      BuildRequest request,
      BuildConfiguration hostConfiguration,
      List<TargetAndConfiguration> topLevelTargetsWithConfigs,
      QueryExpression queryExpression)
      throws InterruptedException, QueryException, IOException {

    // Currently, CTQE assumes that all top level targets take on the same default config and we
    // don't have the ability to map multiple configs to multiple top level targets.
    // So for now, we only allow multiple targets when they all carry the same config.
    // TODO: b/71508373 - fully support multiple top level targets
    List<TargetAndConfiguration> nonNullTargets =
        topLevelTargetsWithConfigs
            .stream()
            .filter(targetAndConfig -> targetAndConfig.getConfiguration() != null)
            .collect(Collectors.toList());
    BuildConfiguration targetConfig = null;
    if (!nonNullTargets.isEmpty()) {
      targetConfig = nonNullTargets.get(0).getConfiguration();
      for (TargetAndConfiguration targAndConfig : topLevelTargetsWithConfigs) {
        if (targAndConfig.getConfiguration() != null
            && !targAndConfig.getConfiguration().equals(targetConfig)) {
          throw new QueryException(
              new TargetLiteral(queryExpression.toString()),
              String.format(
                  "Top-level targets %s and %s have different configurations (top-level "
                      + "targets with different configurations is not supported)",
                  nonNullTargets.get(0).getLabel(), targAndConfig.getLabel()));
        }
      }
    }
    WalkableGraph walkableGraph =
        SkyframeExecutorWrappingWalkableGraph.of(env.getSkyframeExecutor());
    ImmutableList<QueryFunction> extraFunctions =
        new ImmutableList.Builder<QueryFunction>()
            .addAll(ConfiguredTargetQueryEnvironment.CQUERY_FUNCTIONS)
            .addAll(env.getRuntime().getQueryFunctions())
            .build();
    CqueryOptions cqueryOptions = request.getOptions(CqueryOptions.class);
    ConfiguredTargetQueryEnvironment configuredTargetQueryEnvironment =
        new ConfiguredTargetQueryEnvironment(
            request.getKeepGoing(),
            env.getReporter(),
            extraFunctions,
            targetConfig,
            hostConfiguration,
            env.newTargetPatternEvaluator().getOffset(),
            env.getPackageManager().getPackagePath(),
            () -> walkableGraph,
            cqueryOptions.toSettings());
    CqueryOptions.Transitions transitions = cqueryOptions.transitions;
    ThreadSafeOutputFormatterCallback<ConfiguredTarget> callback =
        !transitions.equals(Transitions.NONE)
            ? new TransitionsOutputFormatterCallback(
                configuredTargetQueryEnvironment.getAccessor(),
                transitions,
                env.getReporter().getOutErr().getOutputStream(),
                env.getSkyframeExecutor(),
                hostConfiguration)
            : new ThreadSafeOutputFormatterCallback<ConfiguredTarget>() {
              @Override
              public void processOutput(Iterable<ConfiguredTarget> partialResult)
                  throws IOException, InterruptedException {
                for (ConfiguredTarget configuredTarget : partialResult) {
                  BuildConfiguration config = configuredTarget.getConfiguration();
                  StringBuilder output =
                      new StringBuilder()
                          .append(
                              ConfiguredTargetQueryEnvironment.getCorrectLabel(configuredTarget))
                          .append(" (")
                          .append(config != null && config.isHostConfiguration() ? "HOST" : config)
                          .append(")");
                  env.getReporter().getOutErr().printOutLn(output.toString());
                }
              }
            };
    QueryEvalResult result =
        configuredTargetQueryEnvironment.evaluateQuery(
            queryExpression,
            callback);
    if (result.isEmpty()) {
      env.getReporter().handle(Event.info("Empty query results"));
    }
  }

  /** Cquery specific command line exception. */
  protected static class ConfiguredTargetQueryCommandLineException extends Exception {
    ConfiguredTargetQueryCommandLineException(String message) {
      super(message);
    }
  }
}