Skip to content

Commit

Permalink
cquery: support --output=build
Browse files Browse the repository at this point in the history
This outputs rules as they would appear in BUILD files, except selects() are replaced with the actual paths that get chosen.

More details: https://docs.bazel.build/versions/master/query.html#output-build

RELNOTES[NEW]: cquery supports --output=build

PiperOrigin-RevId: 246571183
  • Loading branch information
gregestren authored and copybara-github committed May 3, 2019
1 parent 4929441 commit c5b0c79
Show file tree
Hide file tree
Showing 6 changed files with 647 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.google.devtools.build.lib.analysis.ToolchainContext;
import com.google.devtools.build.lib.analysis.config.BuildConfiguration;
import com.google.devtools.build.lib.analysis.config.ConfigMatchingProvider;
import com.google.devtools.build.lib.analysis.configuredtargets.OutputFileConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
import com.google.devtools.build.lib.cmdline.Label;
import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper;
Expand All @@ -40,6 +41,7 @@
import com.google.devtools.build.lib.rules.AliasConfiguredTarget;
import com.google.devtools.build.lib.skyframe.BuildConfigurationValue;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetFunction;
import com.google.devtools.build.lib.skyframe.ConfiguredTargetValue;
import com.google.devtools.build.lib.skyframe.PackageValue;
import com.google.devtools.build.lib.skyframe.UnloadedToolchainContext;
import com.google.devtools.build.skyframe.WalkableGraph;
Expand Down Expand Up @@ -189,6 +191,18 @@ public static Target getTargetFromConfiguredTarget(
return target;
}

/** Returns the rule that generates the given output file. */
public RuleConfiguredTarget getGeneratingConfiguredTarget(OutputFileConfiguredTarget oct)
throws InterruptedException {
return (RuleConfiguredTarget)
((ConfiguredTargetValue)
walkableGraph.getValue(
ConfiguredTargetValue.key(
oct.getGeneratingRule().getLabel(),
queryEnvironment.getConfiguration(oct))))
.getConfiguredTarget();
}

@Nullable
public ToolchainContext getToolchainContext(Target target, BuildConfiguration config) {
return getToolchainContext(target, config, walkableGraph);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,35 @@ private static ImmutableList<QueryFunction> getCqueryFunctions() {
PackageManager packageManager) {
AspectResolver aspectResolver =
cqueryOptions.aspectDeps.createResolver(packageManager, eventHandler);
return new ImmutableList.Builder<NamedThreadSafeOutputFormatterCallback<ConfiguredTarget>>()
.add(
new LabelAndConfigurationOutputFormatterCallback(
eventHandler, cqueryOptions, out, skyframeExecutor, accessor))
.add(
new TransitionsOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
hostConfiguration,
trimmingTransitionFactory))
.add(
new ProtoOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
aspectResolver,
OutputType.BINARY))
.add(
new ProtoOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
aspectResolver,
OutputType.TEXT))
.build();
return ImmutableList.of(
new LabelAndConfigurationOutputFormatterCallback(
eventHandler, cqueryOptions, out, skyframeExecutor, accessor),
new TransitionsOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
hostConfiguration,
trimmingTransitionFactory),
new ProtoOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
aspectResolver,
OutputType.BINARY),
new ProtoOutputFormatterCallback(
eventHandler,
cqueryOptions,
out,
skyframeExecutor,
accessor,
aspectResolver,
OutputType.TEXT),
new CqueryBuildOutputFormatterCallback(
eventHandler, cqueryOptions, out, skyframeExecutor, accessor));
}

public String getOutputFormat() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright 2019 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.query2;

import com.google.common.base.Verify;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.OutputFileConfiguredTarget;
import com.google.devtools.build.lib.analysis.configuredtargets.RuleConfiguredTarget;
import com.google.devtools.build.lib.events.ExtendedEventHandler;
import com.google.devtools.build.lib.packages.Attribute;
import com.google.devtools.build.lib.packages.ConfiguredAttributeMapper;
import com.google.devtools.build.lib.packages.Rule;
import com.google.devtools.build.lib.packages.Target;
import com.google.devtools.build.lib.query2.engine.QueryEnvironment.TargetAccessor;
import com.google.devtools.build.lib.query2.output.BuildOutputFormatter;
import com.google.devtools.build.lib.query2.output.BuildOutputFormatter.AttributeReader;
import com.google.devtools.build.lib.query2.output.BuildOutputFormatter.TargetOutputter;
import com.google.devtools.build.lib.query2.output.CqueryOptions;
import com.google.devtools.build.lib.query2.output.OutputFormatter;
import com.google.devtools.build.lib.query2.output.OutputFormatter.PossibleAttributeValues;
import com.google.devtools.build.lib.rules.AliasConfiguredTarget;
import com.google.devtools.build.lib.skyframe.SkyframeExecutor;
import com.google.devtools.build.skyframe.BuildDriver;
import java.io.OutputStream;

/** Cquery implementation of BUILD-style output. */
public class CqueryBuildOutputFormatterCallback extends CqueryThreadsafeCallback {
CqueryBuildOutputFormatterCallback(
ExtendedEventHandler eventHandler,
CqueryOptions options,
OutputStream out,
SkyframeExecutor<? extends BuildDriver> skyframeExecutor,
TargetAccessor<ConfiguredTarget> accessor) {
super(eventHandler, options, out, skyframeExecutor, accessor);
}

@Override
public String getName() {
return "build";
}

/** {@link AttributeReader} implementation that returns the exact value an attribute takes. */
private static class CqueryAttributeReader implements AttributeReader {
private final ConfiguredAttributeMapper attributeMap;

CqueryAttributeReader(ConfiguredAttributeMapper attributeMap) {
this.attributeMap = attributeMap;
}

/**
* Cquery knows which select path is taken so it knows the exact value the attribute takes. Note
* that null values are also possible - these are represented as an empty value list.
*/
@Override
public PossibleAttributeValues getPossibleValues(Rule rule, Attribute attr) {
Object actualValue = attributeMap.get(attr.getName(), attr.getType());
return new PossibleAttributeValues(
actualValue == null ? ImmutableList.of() : ImmutableList.of(actualValue),
OutputFormatter.getAttributeSource(rule, attr));
}
}

private ConfiguredAttributeMapper getAttributeMap(ConfiguredTarget ct)
throws InterruptedException {
Rule associatedRule = accessor.getTargetFromConfiguredTarget(ct).getAssociatedRule();
if (associatedRule == null) {
return null;
} else if (ct instanceof AliasConfiguredTarget) {
return ConfiguredAttributeMapper.of(
associatedRule, ((AliasConfiguredTarget) ct).getConfigConditions());
} else if (ct instanceof OutputFileConfiguredTarget) {
return ConfiguredAttributeMapper.of(
associatedRule,
accessor
.getGeneratingConfiguredTarget((OutputFileConfiguredTarget) ct)
.getConfigConditions());
} else {
Verify.verify(ct instanceof RuleConfiguredTarget);
return ConfiguredAttributeMapper.of(
associatedRule, ((RuleConfiguredTarget) ct).getConfigConditions());
}
}

@Override
public void processOutput(Iterable<ConfiguredTarget> partialResult) throws InterruptedException {
BuildOutputFormatter.TargetOutputter outputter =
new TargetOutputter(
printStream,
// This tells TargetOutputter which attributes to print as selects without resolving
// those selects. For cquery we never have to do this since we can always resolve
// selects. Going forward we could expand this to show both the complete select
// and which path is chosen, which people may find even more informative.
(rule, attr) -> false,
System.lineSeparator());
for (ConfiguredTarget configuredTarget : partialResult) {
Target target = accessor.getTargetFromConfiguredTarget(configuredTarget);
outputter.output(target, new CqueryAttributeReader(getAttributeMap(configuredTarget)));
}
}
}
Loading

0 comments on commit c5b0c79

Please sign in to comment.