Skip to content

Commit

Permalink
Allow cquery to filter out incompatible targets
Browse files Browse the repository at this point in the history
This patch exposes the `IncompatiblePlatformProvider` to Starlark just
enough that it can be used with `cquery`'s `platforms()` function. I
added an example of this to the documentation. The motivation here is
to let users filter out incompatible targets from queries that
provide things like the list of targets to build for CI.

This patch is minimal on purpose. It does not allow users to
instantiate an `IncompatiblePlatformProvider` in Starlark. This may be
added in a future patch to address a different use case.
  • Loading branch information
philsc committed Jan 30, 2021
1 parent d258263 commit ed184e9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
16 changes: 16 additions & 0 deletions site/docs/cquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,22 @@ <h4>Examples</h4>
$ bazel cquery //baz --output=starlark --starlark:file=example.cquery
</pre>

<p>
Filter out <a href="platforms.html#skipping-incompatible-targets">incompatible targets</a> and
only print compatible targets.
</p>
<pre>
$ cat example.cquery

def format(target):
if "IncompatiblePlatformProvider" not in providers(target):
return target.label
return ""


$ bazel cquery //... --output=starlark --starlark:file=example.cquery
</pre>


<h2 id='compare'>cquery vs. query</h2>

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/google/devtools/build/lib/analysis/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,9 @@ java_library(
":configured_target",
":transitive_info_provider",
"//src/main/java/com/google/devtools/build/lib/analysis/platform",
"//src/main/java/com/google/devtools/build/lib/starlarkbuildapi/platform",
"//src/main/java/com/google/devtools/build/lib/concurrent",
"//src/main/java/com/google/devtools/build/lib/packages",
"//third_party:auto_value",
"//third_party:guava",
"//third_party:jsr305",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.devtools.build.lib.analysis.platform.ConstraintValueInfo;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.Info;
import com.google.devtools.build.lib.starlarkbuildapi.platform.IncompatiblePlatformProviderApi;
import javax.annotation.Nullable;

/**
Expand All @@ -34,8 +38,23 @@
* target is incompatible because one of its dependencies is incompatible, then all the incompatible
* dependencies are available via {@code getTargetResponsibleForIncompatibility()}.
*/
@Immutable
@AutoValue
public abstract class IncompatiblePlatformProvider implements TransitiveInfoProvider {
public abstract class IncompatiblePlatformProvider
implements Info, IncompatiblePlatformProviderApi {
/** Name used in Starlark for accessing this provider. */
public static final String STARLARK_NAME = "IncompatiblePlatformProvider";

/** Provider singleton constant. */
public static final BuiltinProvider<IncompatiblePlatformProvider> PROVIDER =
new BuiltinProvider<IncompatiblePlatformProvider>(
STARLARK_NAME, IncompatiblePlatformProvider.class) {};

@Override
public BuiltinProvider<IncompatiblePlatformProvider> getProvider() {
return PROVIDER;
}

public static IncompatiblePlatformProvider incompatibleDueToTargets(
ImmutableList<ConfiguredTarget> targetsResponsibleForIncompatibility) {
Preconditions.checkNotNull(targetsResponsibleForIncompatibility);
Expand All @@ -50,6 +69,11 @@ public static IncompatiblePlatformProvider incompatibleDueToConstraints(
return new AutoValue_IncompatiblePlatformProvider(null, constraints);
}

@Override
public boolean isImmutable() {
return true; // immutable and Starlark-hashable
}

/**
* Returns the incompatible dependencies that caused this provider to be present.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ public static IncompatibleCheckResult checkForIncompatibility(ConfiguredTarget t
target = ((OutputFileConfiguredTarget) target).getGeneratingRule();
}
return IncompatibleCheckResult.create(
target.getProvider(IncompatiblePlatformProvider.class) != null, target);
target.get(IncompatiblePlatformProvider.PROVIDER) != null, target);
}

/**
Expand Down Expand Up @@ -996,13 +996,11 @@ private static ConfiguredTarget createIncompatibleConfiguredTarget(
builder.setFilesToBuild(filesToBuild);

if (targetsResponsibleForIncompatibility != null) {
builder.add(
IncompatiblePlatformProvider.class,
builder.addNativeDeclaredProvider(
IncompatiblePlatformProvider.incompatibleDueToTargets(
targetsResponsibleForIncompatibility));
} else if (violatedConstraints != null) {
builder.add(
IncompatiblePlatformProvider.class,
builder.addNativeDeclaredProvider(
IncompatiblePlatformProvider.incompatibleDueToConstraints(violatedConstraints));
} else {
throw new IllegalArgumentException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static String reportOnIncompatibility(ConfiguredTarget target) {
// save the user bazel round trips.
while (target != null) {
message += "\n " + target.getLabel();
provider = target.getProvider(IncompatiblePlatformProvider.class);
provider = target.get(IncompatiblePlatformProvider.PROVIDER);
ImmutableList<ConfiguredTarget> targetList = provider.targetsResponsibleForIncompatibility();
if (targetList == null) {
target = null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.google.devtools.build.lib.starlarkbuildapi.platform;

import com.google.devtools.build.docgen.annot.DocCategory;
import net.starlark.java.annot.StarlarkBuiltin;
import net.starlark.java.eval.StarlarkValue;

@StarlarkBuiltin(
name = "IncompatiblePlatformProvider",
doc = "An interface for targets that are incompatible with the target platform.",
category = DocCategory.PROVIDER)
public interface IncompatiblePlatformProviderApi extends StarlarkValue {}
38 changes: 38 additions & 0 deletions src/test/shell/integration/target_compatible_with_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,44 @@ function test_cquery_incompatible_target() {
expect_log "target platform didn't satisfy constraint //target_skipping:foo1"
}
# Runs a cquery and makes sure that we can properly distinguish between
# incompatible targets and compatible targets.
function test_cquery_with_starlark_formatting() {
cat > target_skipping/compatibility.cquery <<EOF
def format(target):
if "IncompatiblePlatformProvider" in providers(target):
result = "incompatible"
else:
result = "compatible"
return "%s is %s" % (target.label, result)
EOF
cd target_skipping || fail "couldn't cd into workspace"
bazel cquery \
--host_platform=//target_skipping:foo1_bar1_platform \
--platforms=//target_skipping:foo1_bar1_platform \
:all \
--output=starlark --starlark:file=target_skipping/compatibility.cquery \
&> "${TEST_log}"
expect_log '^//target_skipping:pass_on_foo1 is compatible$'
expect_log '^//target_skipping:fail_on_foo2 is incompatible$'
expect_log '^//target_skipping:some_foo3_target is incompatible$'
bazel cquery \
--host_platform=//target_skipping:foo3_platform \
--platforms=//target_skipping:foo3_platform \
:all \
--output=starlark --starlark:file=target_skipping/compatibility.cquery \
&> "${TEST_log}"
expect_log '^//target_skipping:pass_on_foo1 is incompatible$'
expect_log '^//target_skipping:fail_on_foo2 is incompatible$'
expect_log '^//target_skipping:some_foo3_target is compatible$'
}
# Run an aquery on a target that is compatible. This should pass.
function test_aquery_compatible_target() {
write_query_test_targets
Expand Down

0 comments on commit ed184e9

Please sign in to comment.