Skip to content
This repository was archived by the owner on Nov 10, 2023. It is now read-only.

Commit adc0f78

Browse files
asp2inspfacebook-github-bot
authored andcommitted
Add provided deps query support to android_library
Summary: For global code generation, it's beneficial to use provided_deps instead of deps so that we do not accidentally package items into the app that are used by metagen. This change allows us to use provided_deps instead, and it also adds better syntax for referencing the declared deps. Test Plan: new integration test Reviewed By: dreiss fbshipit-source-id: a796f60
1 parent 1d5622a commit adc0f78

File tree

7 files changed

+116
-2
lines changed

7 files changed

+116
-2
lines changed

docs/__common.soy

+14
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,20 @@ docsearch({
12181218
{/call}
12191219
{/template}
12201220

1221+
/***/
1222+
{template .provided_deps_query_arg}
1223+
{call buck.arg}
1224+
{param name: 'provided_deps_query' /}
1225+
{param default : 'None' /}
1226+
{param desc}
1227+
Status: <strong>experimental/unstable</strong>.
1228+
The provided deps query functions in the same way as the deps query, but the referenced deps
1229+
using <code>$declared</code> are the provided deps of the target, and the results of the query
1230+
are appended to the declared provided deps.
1231+
{/param}
1232+
{/call}
1233+
{/template}
1234+
12211235
/***/
12221236
{template .licenses_arg}
12231237
{call buck.arg}

docs/rule/android_library.soy

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ compiled class files and resources.
151151

152152
{call buck.deps_query_arg /}
153153

154+
{call buck.provided_deps_query_arg /}
155+
154156
{call buck.tests_arg /}
155157

156158
{/param} // args

src/com/facebook/buck/android/AndroidLibraryDescription.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,22 @@ public <A extends Arg> BuildRule createBuildRule(
120120
}
121121
final ImmutableSet<BuildRule> queriedDeps = queriedDepsBuilder.build();
122122

123+
ImmutableSortedSet.Builder<BuildRule> providedDepsBuilder =
124+
ImmutableSortedSet.<BuildRule>naturalOrder()
125+
.addAll(resolver.getAllRules(args.providedDeps));
126+
127+
if (args.providedDepsQuery.isPresent()) {
128+
providedDepsBuilder.addAll(
129+
QueryUtils.resolveDepQuery(
130+
params.getBuildTarget(),
131+
args.providedDepsQuery.get(),
132+
resolver,
133+
cellRoots,
134+
targetGraph,
135+
args.providedDeps)
136+
.collect(Collectors.toList()));
137+
}
138+
123139
AndroidLibraryGraphEnhancer graphEnhancer = new AndroidLibraryGraphEnhancer(
124140
params.getBuildTarget(),
125141
params.copyReplacingExtraDeps(
@@ -157,6 +173,8 @@ public <A extends Arg> BuildRule createBuildRule(
157173
if (hasDummyRDotJavaFlavor) {
158174
return dummyRDotJava.get();
159175
} else {
176+
final ImmutableSortedSet<BuildRule> providedDeps = providedDepsBuilder.build();
177+
160178
if (dummyRDotJava.isPresent()) {
161179
ImmutableSortedSet<BuildRule> newDeclaredDeps = ImmutableSortedSet.<BuildRule>naturalOrder()
162180
.addAll(params.getDeclaredDeps().get())
@@ -187,7 +205,7 @@ public <A extends Arg> BuildRule createBuildRule(
187205
Iterables.concat(
188206
declaredDeps,
189207
exportedDeps,
190-
resolver.getAllRules(args.providedDeps))))
208+
providedDeps)))
191209
.addAll(ruleFinder.filterBuildRuleInputs(
192210
javacOptions.getAnnotationProcessingParams().getInputs()))
193211
.addAll(compiler.getExtraDeps(args, resolver))
@@ -197,12 +215,17 @@ public <A extends Arg> BuildRule createBuildRule(
197215
params.copyReplacingDeclaredAndExtraDeps(
198216
Suppliers.ofInstance(declaredDeps),
199217
Suppliers.ofInstance(extraDeps));
218+
219+
ImmutableSortedSet.Builder<BuildTarget> providedDepsTargetsBuilder =
220+
ImmutableSortedSet.naturalOrder();
221+
providedDeps.forEach(dep -> providedDepsTargetsBuilder.add(dep.getBuildTarget()));
200222
return AndroidLibrary.builder(
201223
androidLibraryParams,
202224
resolver,
203225
compiler.compileToJar(args, javacOptions, resolver),
204226
javacOptions)
205227
.setConfigAndArgs(javaBuckConfig, args)
228+
.setProvidedDeps(providedDepsTargetsBuilder.build())
206229
.setTrackClassUsage(compiler.trackClassUsage(javacOptions))
207230
.setTests(args.tests)
208231
.build();
@@ -239,6 +262,7 @@ public static class Arg extends JavaLibraryDescription.Arg {
239262
public Optional<String> finalRName;
240263
public Optional<JvmLanguage> language;
241264
public Optional<Query> depsQuery;
265+
public Optional<Query> providedDepsQuery;
242266
}
243267
}
244268

src/com/facebook/buck/rules/query/GraphEnhancementQueryEnvironment.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public GraphEnhancementQueryEnvironment(
9898
public ImmutableSet<QueryTarget> getTargetsMatchingPattern(
9999
String pattern,
100100
ListeningExecutorService executor) throws QueryException, InterruptedException {
101-
if ("$declared_deps".equals(pattern)) {
101+
if ("$declared_deps".equals(pattern) ||
102+
"$declared".equals(pattern) ||
103+
"first_order_deps()".equals(pattern)) {
102104
return declaredDeps
103105
.stream()
104106
.map(QueryBuildTarget::of)

test/com/facebook/buck/android/AndroidLibraryDescriptionIntegrationTest.java

+44
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818
package com.facebook.buck.android;
1919

2020

21+
import static org.junit.Assert.fail;
22+
2123
import com.facebook.buck.testutil.integration.ProjectWorkspace;
2224
import com.facebook.buck.testutil.integration.TemporaryPaths;
2325
import com.facebook.buck.testutil.integration.TestDataHelper;
26+
import com.fasterxml.jackson.databind.JsonNode;
27+
import com.fasterxml.jackson.databind.ObjectMapper;
2428

29+
import org.hamcrest.Matchers;
30+
import org.junit.Assert;
2531
import org.junit.Before;
2632
import org.junit.Rule;
2733
import org.junit.Test;
2834

2935
import java.io.IOException;
36+
import java.nio.file.Path;
37+
import java.nio.file.Paths;
3038

3139
/**
3240
* Tests for AndroidLibraryDescription
@@ -140,4 +148,40 @@ public void testDepQueryWithClasspathDoesNotTraverseProvidedDeps() throws Except
140148
// Should fail becuase 'C.class' is not added to the classpath because it's a provided dep
141149
workspace.runBuckCommand("build", "//:no_provided_deps").assertFailure();
142150
}
151+
152+
@Test
153+
public void testProvidedDepsQuery() throws Exception {
154+
AssumeAndroidPlatform.assumeSdkIsAvailable();
155+
// Should succeed because required dep (lib_c) will be added as provided
156+
workspace.runBuckBuild("//:has_lib_c_from_provided_query").assertSuccess();
157+
}
158+
159+
@Test
160+
public void testProvidedDepsQueryDoesNotAffectPackaging() throws Exception {
161+
AssumeAndroidPlatform.assumeSdkIsAvailable();
162+
workspace.runBuckCommand("build", "//:check_output_of_does_not_package_lib_c")
163+
.assertSuccess();
164+
String[] outputs = workspace.getFileContents(
165+
getOutputFile("//:check_output_of_does_not_package_lib_c"))
166+
.split("\\s");
167+
// There should be a class entry for UsesC.java
168+
Assert.assertThat(outputs, Matchers.hasItemInArray("com/facebook/example/UsesC.class"));
169+
// But not one for C.java
170+
Assert.assertThat(outputs,
171+
Matchers.not(Matchers.hasItemInArray("com/facebook/example/C.class")));
172+
}
173+
174+
private Path getOutputFile(String targetName) {
175+
try {
176+
ProjectWorkspace.ProcessResult buildResult =
177+
workspace.runBuckCommand("targets", targetName, "--show-output", "--json");
178+
buildResult.assertSuccess();
179+
JsonNode jsonNode = new ObjectMapper().reader().readTree(buildResult.getStdout()).get(0);
180+
assert jsonNode.has("buck.outputPath");
181+
return Paths.get(jsonNode.get("buck.outputPath").asText());
182+
} catch (Exception e) {
183+
fail(e.getMessage());
184+
return Paths.get("");
185+
}
186+
}
143187
}

test/com/facebook/buck/android/testdata/android_library_dynamic_deps/BUCK.fixture

+24
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ android_library(
6868
deps_query = 'kind(android_resource, deps($declared_deps))',
6969
)
7070

71+
android_library(
72+
name = 'has_lib_c_from_provided_query',
73+
srcs = [
74+
'UsesC.java',
75+
],
76+
provided_deps = [
77+
':top_level',
78+
],
79+
provided_deps_query = 'kind(java_library, deps($declared))',
80+
)
81+
7182
android_library(
7283
name = 'provided_only',
7384
srcs = [ 'UsesC.java', ],
@@ -79,4 +90,17 @@ android_library(
7990
srcs = [ 'UsesC.java', ],
8091
deps = [ ':provided_only', ],
8192
deps_query = 'classpath($declared_deps)',
93+
)
94+
95+
java_binary(
96+
name = 'does_not_package_lib_c',
97+
deps = [
98+
':has_lib_c_from_provided_query',
99+
],
100+
)
101+
102+
genrule(
103+
name = 'check_output_of_does_not_package_lib_c',
104+
cmd = 'echo `unzip -l $(location :does_not_package_lib_c)` > $OUT',
105+
out = 'out',
82106
)

test/com/facebook/buck/android/testdata/android_library_dynamic_deps/UsesC.java

+4
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
public class UsesC {
2020
private C c;
2121
// method
22+
23+
public static void main(String[] args) {
24+
25+
}
2226
}

0 commit comments

Comments
 (0)