Skip to content

Commit

Permalink
cxx: add deps_query parameter to C/C++ binary rules
Browse files Browse the repository at this point in the history
Summary:
This adds a new `deps_query` parameter to C/C++ binary rules which
behaves identically as the parameter of the same name for `android_library`
rules.

Test Plan: Added unittest.

Reviewed By: Coneko

fbshipit-source-id: b8f16e4
  • Loading branch information
andrewjcg authored and facebook-github-bot committed Feb 6, 2017
1 parent 8d507f7 commit e97ed9a
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 53 deletions.
43 changes: 43 additions & 0 deletions docs/__common.soy
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,49 @@ docsearch({
<a href="{ROOT}concept/buckconfig.html"><code>.buckconfig</code></a>
{/template}

/***/
{template .deps_query_arg}
{call buck.arg}
{param name: 'deps_query' /}
{param default : 'None' /}
{param desc}
Status: <strong>experimental/unstable</strong>.
The deps query takes a query string that accepts the following query functions, and appends the
output of the query to the declared deps:
{literal}
<ul style="line-height:0.5em">
<li><code>attrfilter</code></li>
<li><code>deps</code></li>
<li><code>except</code></li>
<li><code>intersect</code></li>
<li><code>filter</code></li>
<li><code>kind</code></li>
<li><code>set</code></li>
<li><code>union</code></li>
</ul>
{/literal}

The macro <code>$declared_deps</code> may be used anywhere a target literal pattern is expected
in order to refer to the explicit deps of this rule as they appear in the rule's definition.
For example, if your build rule declares
{literal}<pre class="prettyprint lang-py">
android_library(
name = 'lib',
deps = ['//foo:foo'],
deps_query = '$declared_deps',
)</pre>
{/literal}
then the macro <code>$declared_deps</code> would be expanded to a
literal <code>set(//foo:foo)</code>.
Some example queries:
{literal}<pre>
"filter({name_regex}, $declared_deps)".format(name_regex='//.*')
"attrfilter(annotation_processors, com.foo.Processor, $declared_deps)"</pre>
{/literal}
{/param}
{/call}
{/template}

/***/
{template .licenses_arg}
{call buck.arg}
Expand Down
40 changes: 1 addition & 39 deletions docs/rule/android_library.soy
Original file line number Diff line number Diff line change
Expand Up @@ -149,45 +149,7 @@ compiled class files and resources.
{/param}
{/call}

{call buck.arg}
{param name: 'deps_query' /}
{param default : 'None' /}
{param desc}
Status: <strong>experimental/unstable</strong>.
The deps query takes a query string that accepts the following query functions, and appends the
output of the query to the declared deps:
{literal}
<ul style="line-height:0.5em">
<li><code>attrfilter</code></li>
<li><code>deps</code></li>
<li><code>except</code></li>
<li><code>intersect</code></li>
<li><code>filter</code></li>
<li><code>kind</code></li>
<li><code>set</code></li>
<li><code>union</code></li>
</ul>
{/literal}

The macro <code>$declared_deps</code> may be used anywhere a target literal pattern is expected
in order to refer to the explicit deps of this rule as they appear in the rule's definition.
For example, if your build rule declares
{literal}<pre class="prettyprint lang-py">
android_library(
name = 'lib',
deps = ['//foo:foo'],
deps_query = '$declared_deps',
)</pre>
{/literal}
then the macro <code>$declared_deps</code> would be expanded to a
literal <code>set(//foo:foo)</code>.
Some example queries:
{literal}<pre>
"filter({name_regex}, $declared_deps)".format(name_regex='//.*')
"attrfilter(annotation_processors, com.foo.Processor, $declared_deps)"</pre>
{/literal}
{/param}
{/call}
{call buck.deps_query_arg /}

{call buck.tests_arg /}

Expand Down
2 changes: 2 additions & 0 deletions docs/rule/cxx_binary.soy
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ and link against their static archives (which are *not* built using {sp}

{call native_common.link_style /}

{call buck.deps_query_arg /}

{call buck.tests_arg /}

{/param} // close args
Expand Down
2 changes: 2 additions & 0 deletions docs/rule/cxx_test.soy
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ it as part of {call buck.cmd_test /}.

{call cxx_common.precompiled_header_arg /}

{call buck.deps_query_arg /}

{call test_common.resources_arg /}

{call buck.arg}
Expand Down
2 changes: 2 additions & 0 deletions src/com/facebook/buck/cxx/BUCK.autodeps
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"//src/com/facebook/buck/log:api",
"//src/com/facebook/buck/model:simple_types",
"//src/com/facebook/buck/rules:command_tool",
"//src/com/facebook/buck/rules/query:query",
"//src/com/facebook/buck/step/fs:fs",
"//src/com/facebook/buck/test/result/type:type",
"//src/com/facebook/buck/util:process_executor",
Expand All @@ -73,6 +74,7 @@
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/macros:macros",
"//src/com/facebook/buck/rules/macros:types",
"//src/com/facebook/buck/rules/query:types",
"//src/com/facebook/buck/shell:rules",
"//src/com/facebook/buck/shell:steps",
"//src/com/facebook/buck/step:step",
Expand Down
29 changes: 23 additions & 6 deletions src/com/facebook/buck/cxx/CxxBinaryDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import com.facebook.buck.rules.SourcePathResolver;
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.query.DepQuery;
import com.facebook.buck.rules.query.DepQueryUtils;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.versions.VersionRoot;
import com.facebook.infer.annotation.SuppressFieldNotInitialized;
Expand Down Expand Up @@ -151,6 +153,7 @@ public <A extends Arg> BuildRule createBuildRule(
params.withoutFlavor(CxxCompilationDatabase.COMPILATION_DATABASE);
CxxLinkAndCompileRules cxxLinkAndCompileRules = CxxDescriptionEnhancer
.createBuildRulesForCxxBinaryDescriptionArg(
targetGraph,
paramsWithoutFlavor,
resolver,
cxxBuckConfig,
Expand Down Expand Up @@ -226,6 +229,7 @@ public <A extends Arg> BuildRule createBuildRule(

CxxLinkAndCompileRules cxxLinkAndCompileRules =
CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(
targetGraph,
params,
resolver,
cxxBuckConfig,
Expand All @@ -248,7 +252,9 @@ public <A extends Arg> BuildRule createBuildRule(
params = CxxStrip.restoreStripStyleFlavorInParams(params, flavoredStripStyle);
params = LinkerMapMode.restoreLinkerMapModeFlavorInParams(params, flavoredLinkerMapMode);
CxxBinary cxxBinary = new CxxBinary(
params.appendExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder)),
params
.copyWithDeps(() -> cxxLinkAndCompileRules.deps, params.getExtraDeps())
.appendExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder)),
resolver,
ruleFinder,
cxxLinkAndCompileRules.getBinaryRule(),
Expand All @@ -265,11 +271,21 @@ public Iterable<BuildTarget> findDepsForTargetFromConstructorArgs(
BuildTarget buildTarget,
CellPathResolver cellRoots,
Arg constructorArg) {
return findDepsForTargetFromConstructorArgs(
buildTarget,
cellRoots,
constructorArg.linkerFlags,
constructorArg.platformLinkerFlags.getValues());
ImmutableSet.Builder<BuildTarget> deps = ImmutableSet.builder();

deps.addAll(
findDepsForTargetFromConstructorArgs(
buildTarget,
cellRoots,
constructorArg.linkerFlags,
constructorArg.platformLinkerFlags.getValues()));

constructorArg.depsQuery.ifPresent(
depsQuery ->
DepQueryUtils.extractParseTimeTargets(buildTarget, cellRoots, depsQuery)
.forEach(deps::add));

return deps.build();
}

public Iterable<BuildTarget> findDepsForTargetFromConstructorArgs(
Expand Down Expand Up @@ -407,6 +423,7 @@ public boolean isVersionRoot(ImmutableSet<Flavor> flavors) {

@SuppressFieldNotInitialized
public static class Arg extends LinkableCxxConstructorArg {
public Optional<DepQuery> depsQuery = Optional.empty();
}

}
28 changes: 22 additions & 6 deletions src/com/facebook/buck/cxx/CxxDescriptionEnhancer.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.rules.SourceWithFlags;
import com.facebook.buck.rules.SymlinkTree;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.args.Arg;
import com.facebook.buck.rules.args.FileListableLinkerInputArg;
import com.facebook.buck.rules.args.MacroArg;
Expand All @@ -47,6 +48,7 @@
import com.facebook.buck.rules.coercer.SourceList;
import com.facebook.buck.rules.macros.LocationMacroExpander;
import com.facebook.buck.rules.macros.MacroHandler;
import com.facebook.buck.rules.query.DepQueryUtils;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.util.RichStream;
Expand Down Expand Up @@ -621,6 +623,7 @@ public Path apply(FrameworkPath input) {
}

public static CxxLinkAndCompileRules createBuildRulesForCxxBinaryDescriptionArg(
TargetGraph targetGraph,
BuildRuleParams params,
BuildRuleResolver resolver,
CxxBuckConfig cxxBuckConfig,
Expand All @@ -629,26 +632,38 @@ public static CxxLinkAndCompileRules createBuildRulesForCxxBinaryDescriptionArg(
Optional<StripStyle> stripStyle,
Optional<LinkerMapMode> flavoredLinkerMapMode) throws NoSuchBuildTargetException {

SourcePathResolver sourcePathResolver =
new SourcePathResolver(new SourcePathRuleFinder(resolver));
SourcePathRuleFinder ruleFinder = new SourcePathRuleFinder(resolver);
SourcePathResolver pathResolver = new SourcePathResolver(ruleFinder);
ImmutableMap<String, CxxSource> srcs = parseCxxSources(
params.getBuildTarget(),
sourcePathResolver,
pathResolver,
cxxPlatform,
args);
ImmutableMap<Path, SourcePath> headers = parseHeaders(
params.getBuildTarget(),
new SourcePathResolver(new SourcePathRuleFinder(resolver)),
Optional.of(cxxPlatform),
args);

// Build the binary deps.
ImmutableSortedSet.Builder<BuildRule> depsBuilder = ImmutableSortedSet.naturalOrder();
// Add original declared and extra deps.
depsBuilder.addAll(params.getDeps());
// Add in deps found via deps query.
args.depsQuery.ifPresent(
depsQuery ->
DepQueryUtils.resolveDepQuery(params, depsQuery, resolver, targetGraph)
.forEach(depsBuilder::add));
ImmutableSortedSet<BuildRule> deps = depsBuilder.build();

return createBuildRulesForCxxBinary(
params,
resolver,
cxxBuckConfig,
cxxPlatform,
srcs,
headers,
params.getDeps(),
deps,
stripStyle,
flavoredLinkerMapMode,
args.linkStyle.orElse(Linker.LinkableDepType.STATIC),
Expand Down Expand Up @@ -676,7 +691,7 @@ public static CxxLinkAndCompileRules createBuildRulesForCxxBinary(
CxxPlatform cxxPlatform,
ImmutableMap<String, CxxSource> srcs,
ImmutableMap<Path, SourcePath> headers,
Iterable<? extends BuildRule> deps,
ImmutableSortedSet<BuildRule> deps,
Optional<StripStyle> stripStyle,
Optional<LinkerMapMode> flavoredLinkerMapMode,
Linker.LinkableDepType linkStyle,
Expand Down Expand Up @@ -883,7 +898,8 @@ public static CxxLinkAndCompileRules createBuildRulesForCxxBinary(
cxxLink,
cxxStrip,
ImmutableSortedSet.copyOf(objects.keySet()),
executableBuilder.build());
executableBuilder.build(),
deps);
}

private static CxxLink createCxxLinkRule(
Expand Down
7 changes: 6 additions & 1 deletion src/com/facebook/buck/cxx/CxxLinkAndCompileRules.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import java.util.Optional;

public class CxxLinkAndCompileRules {

public final ImmutableSortedSet<BuildRule> deps;
private final CxxLink cxxLink;
private final Optional<CxxStrip> cxxStrip;
final ImmutableSortedSet<CxxPreprocessAndCompile> compileRules;
Expand All @@ -32,11 +34,13 @@ public class CxxLinkAndCompileRules {
CxxLink cxxLink,
Optional<CxxStrip> cxxStrip,
ImmutableSortedSet<CxxPreprocessAndCompile> compileRules,
Tool executable) {
Tool executable,
ImmutableSortedSet<BuildRule> deps) {
this.cxxLink = cxxLink;
this.cxxStrip = cxxStrip;
this.compileRules = compileRules;
this.executable = executable;
this.deps = deps;
}

public BuildRule getBinaryRule() {
Expand All @@ -46,4 +50,5 @@ public BuildRule getBinaryRule() {
return cxxLink;
}
}

}
11 changes: 10 additions & 1 deletion src/com/facebook/buck/cxx/CxxTestDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.buck.rules.SourcePathRuleFinder;
import com.facebook.buck.rules.SourcePaths;
import com.facebook.buck.rules.TargetGraph;
import com.facebook.buck.rules.query.DepQueryUtils;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.MoreCollectors;
import com.facebook.buck.versions.VersionRoot;
Expand Down Expand Up @@ -140,6 +141,7 @@ public <A extends Arg> BuildRule createBuildRule(
// Generate the link rule that builds the test binary.
final CxxLinkAndCompileRules cxxLinkAndCompileRules =
CxxDescriptionEnhancer.createBuildRulesForCxxBinaryDescriptionArg(
targetGraph,
params,
resolver,
cxxBuckConfig,
Expand All @@ -151,7 +153,9 @@ public <A extends Arg> BuildRule createBuildRule(
// Construct the actual build params we'll use, notably with an added dependency on the
// CxxLink rule above which builds the test binary.
BuildRuleParams testParams =
params.appendExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder));
params
.copyWithDeps(() -> cxxLinkAndCompileRules.deps, params.getExtraDeps())
.appendExtraDeps(cxxLinkAndCompileRules.executable.getDeps(ruleFinder));
testParams = CxxStrip.restoreStripStyleFlavorInParams(
testParams,
flavoredStripStyle);
Expand Down Expand Up @@ -316,6 +320,11 @@ public Iterable<BuildTarget> findDepsForTargetFromConstructorArgs(
}
}

constructorArg.depsQuery.ifPresent(
depsQuery ->
DepQueryUtils.extractParseTimeTargets(buildTarget, cellRoots, depsQuery)
.forEach(deps::add));

return deps.build();
}

Expand Down
2 changes: 2 additions & 0 deletions test/com/facebook/buck/cxx/BUCK.autodeps
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"//src/com/facebook/buck/rules/coercer:coercer",
"//src/com/facebook/buck/rules/keys:keys",
"//src/com/facebook/buck/rules/macros:types",
"//src/com/facebook/buck/rules/query:types",
"//src/com/facebook/buck/shell:rules",
"//src/com/facebook/buck/step:step",
"//src/com/facebook/buck/step/fs:fs",
Expand Down Expand Up @@ -104,6 +105,7 @@
"//src/com/facebook/buck/rules:build_rule",
"//src/com/facebook/buck/rules:rules",
"//src/com/facebook/buck/rules/coercer:coercer",
"//src/com/facebook/buck/rules/query:types",
"//test/com/facebook/buck/rules:testutil",
"//test/com/facebook/buck/testutil/integration:util",
"//third-party/java/gson:gson",
Expand Down
8 changes: 8 additions & 0 deletions test/com/facebook/buck/cxx/CxxBinaryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import com.facebook.buck.cli.FakeBuckConfig;
import com.facebook.buck.model.BuildTarget;
import com.facebook.buck.model.FlavorDomain;
import com.facebook.buck.rules.query.DepQuery;

import java.util.Optional;

public class CxxBinaryBuilder
extends
Expand Down Expand Up @@ -64,4 +67,9 @@ protected CxxBinaryBuilder getThis() {
return this;
}

public CxxBinaryBuilder setDepQuery(DepQuery depQuery) {
arg.depsQuery = Optional.of(depQuery);
return this;
}

}
Loading

0 comments on commit e97ed9a

Please sign in to comment.