// Copyright 2015 Google Inc. 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.rules.genquery;
import static com.google.devtools.build.lib.packages.Attribute.attr;
import static com.google.devtools.build.lib.packages.Type.BOOLEAN;
import static com.google.devtools.build.lib.packages.Type.LABEL_LIST;
import static com.google.devtools.build.lib.packages.Type.STRING;
import static com.google.devtools.build.lib.packages.Type.STRING_LIST;
import com.google.devtools.build.lib.analysis.BaseRuleClasses;
import com.google.devtools.build.lib.analysis.RuleDefinition;
import com.google.devtools.build.lib.analysis.RuleDefinitionEnvironment;
import com.google.devtools.build.lib.packages.RuleClass;
import com.google.devtools.build.lib.packages.RuleClass.Builder;
/**
* Rule definition for genquery the rule.
*/
public final class GenQueryRule implements RuleDefinition {
@Override
public RuleClass build(Builder builder, RuleDefinitionEnvironment env) {
return builder
/*
The scope of the query. The query is not allowed to
touch targets outside the transitive closure of these targets.
${SYNOPSIS}
*/
.add(attr("scope", LABEL_LIST).mandatory().legacyAllowAnyFileType())
/*
If true, targets whose queries escape the transitive closure of their
scopes will fail to build. If false, Blaze will print a warning and skip
whatever query path led it outside of the scope, while completing the rest
of the query.
${SYNOPSIS}
*/
.add(attr("strict", BOOLEAN).value(true))
/*
The query to be executed.
${SYNOPSIS}
*/
.add(attr("expression", STRING).mandatory())
/*
The options that are passed to the query engine.
These correspond to the command line options that can be passed to
blaze query
. The only query option that is not allowed here
is --keep_going
.
${SYNOPSIS}
*/
.add(attr("opts", STRING_LIST))
.build();
}
@Override
public Metadata getMetadata() {
return RuleDefinition.Metadata.builder()
.name("genquery")
.ancestors(BaseRuleClasses.RuleBase.class)
.factoryClass(GenQuery.class)
.build();
}
}
/*
${ATTRIBUTE_SIGNATURE}
genquery()
runs a query specified in the
Blaze query language and dumps the result
into a file.
In order to keep the build consistent, the query is allowed only to visit
the transitive closure of the targets specified in the scope
attribute. Queries violating this rule will fail during execution if
strict
is unspecified or true (if strict
is false,
the out of scope targets will simply be skipped with a warning). The
easiest way to make sure this does not happen is to mention the same labels
in the scope as in the query expression. The only difference between the
queries allowed here and on the command line is that queries containing
wildcard target specifications (e.g. //pkg:*
or
//pkg:all
) are not allowed here.
The name of the output file is the name of the rule.
${ATTRIBUTE_DEFINITION}This example writes the list of the labels in the transitive closure of the specified target to a file.
genquery( name = "kiwi-deps", expression = "deps(//kiwi:kiwi_lib)", scope = ["//kiwi:kiwi_lib"], )*/