Skip to content

Commit e3fae33

Browse files
hvadehracopybara-github
authored andcommitted
Add a new provider for injecting native libs in android_binary
This reverts the changes made recently to AndroidApplicationResourceInfo and moves them to a separate provider. This helps keep the native libs related processing in Starlark separate from resources processing. PiperOrigin-RevId: 485555064 Change-Id: I78725fe022f1e0089f063681b42c857d001165cf
1 parent bbc221f commit e3fae33

File tree

8 files changed

+247
-132
lines changed

8 files changed

+247
-132
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/BazelRuleClassProvider.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import com.google.devtools.build.lib.rules.android.AndroidApplicationResourceInfo;
6565
import com.google.devtools.build.lib.rules.android.AndroidAssetsInfo;
6666
import com.google.devtools.build.lib.rules.android.AndroidBinaryDataInfo;
67+
import com.google.devtools.build.lib.rules.android.AndroidBinaryNativeLibsInfo;
6768
import com.google.devtools.build.lib.rules.android.AndroidCcLinkParamsProvider;
6869
import com.google.devtools.build.lib.rules.android.AndroidConfiguration;
6970
import com.google.devtools.build.lib.rules.android.AndroidDeviceBrokerInfo;
@@ -424,6 +425,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
424425
AndroidFeatureFlagSetProvider.PROVIDER,
425426
ProguardMappingProvider.PROVIDER,
426427
AndroidBinaryDataInfo.PROVIDER,
428+
AndroidBinaryNativeLibsInfo.PROVIDER,
427429
BaselineProfileProvider.PROVIDER);
428430
builder.addStarlarkBootstrap(bootstrap);
429431

src/main/java/com/google/devtools/build/lib/rules/android/AndroidApplicationResourceInfo.java

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,11 @@
1515

1616
import static com.google.devtools.build.lib.rules.android.AndroidStarlarkData.fromNoneable;
1717

18-
import com.google.common.collect.Maps;
1918
import com.google.devtools.build.lib.actions.Artifact;
20-
import com.google.devtools.build.lib.collect.nestedset.Depset;
21-
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
2219
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
2320
import com.google.devtools.build.lib.packages.BuiltinProvider;
2421
import com.google.devtools.build.lib.packages.NativeInfo;
2522
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidApplicationResourceInfoApi;
26-
import javax.annotation.Nullable;
27-
import net.starlark.java.eval.Dict;
2823
import net.starlark.java.eval.EvalException;
2924

3025
/** A provider for Android resource APKs (".ap_") and related info. */
@@ -47,8 +42,6 @@ public class AndroidApplicationResourceInfo extends NativeInfo
4742
private final Artifact databindingLayoutInfoZip;
4843
private final Artifact buildStampJar;
4944
private final boolean shouldCompileJavaSrcs;
50-
private final NativeLibs nativeLibs;
51-
private final NestedSet<Artifact> transitiveNativeLibs;
5245

5346
AndroidApplicationResourceInfo(
5447
Artifact resourceApk,
@@ -61,9 +54,7 @@ public class AndroidApplicationResourceInfo extends NativeInfo
6154
Artifact resourcesZip,
6255
Artifact databindingLayoutInfoZip,
6356
Artifact buildStampJar,
64-
boolean shouldCompileJavaSrcs,
65-
NativeLibs nativeLibs,
66-
NestedSet<Artifact> transitiveNativeLibs) {
57+
boolean shouldCompileJavaSrcs) {
6758
this.resourceApk = resourceApk;
6859
this.resourceJavaSrcJar = resourceJavaSrcJar;
6960
this.resourceJavaClassJar = resourceJavaClassJar;
@@ -75,8 +66,6 @@ public class AndroidApplicationResourceInfo extends NativeInfo
7566
this.databindingLayoutInfoZip = databindingLayoutInfoZip;
7667
this.buildStampJar = buildStampJar;
7768
this.shouldCompileJavaSrcs = shouldCompileJavaSrcs;
78-
this.nativeLibs = nativeLibs;
79-
this.transitiveNativeLibs = transitiveNativeLibs;
8069
}
8170

8271
@Override
@@ -145,44 +134,6 @@ public boolean shouldCompileJavaSrcs() {
145134
return shouldCompileJavaSrcs;
146135
}
147136

148-
@Nullable
149-
@Override
150-
public Dict<String, Depset> getNativeLibsStarlark() {
151-
if (nativeLibs == null) {
152-
return null;
153-
}
154-
return Dict.immutableCopyOf(
155-
Maps.transformValues(nativeLibs.getMap(), set -> Depset.of(Artifact.TYPE, set)));
156-
}
157-
158-
@Nullable
159-
@Override
160-
public Artifact getNativeLibsNameStarlark() {
161-
if (nativeLibs == null) {
162-
return null;
163-
}
164-
return nativeLibs.getName();
165-
}
166-
167-
@Nullable
168-
@Override
169-
public Depset getTransitiveNativeLibsStarlark() {
170-
if (transitiveNativeLibs == null) {
171-
return null;
172-
}
173-
return Depset.of(Artifact.TYPE, transitiveNativeLibs);
174-
}
175-
176-
@Nullable
177-
public NativeLibs getNativeLibs() {
178-
return nativeLibs;
179-
}
180-
181-
@Nullable
182-
public NestedSet<Artifact> getTransitiveNativeLibs() {
183-
return transitiveNativeLibs;
184-
}
185-
186137
/** Provider for {@link AndroidApplicationResourceInfo}. */
187138
public static class AndroidApplicationResourceInfoProvider
188139
extends BuiltinProvider<AndroidApplicationResourceInfo>
@@ -204,9 +155,7 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
204155
Object resourcesZip,
205156
Object databindingLayoutInfoZip,
206157
Object buildStampJar,
207-
boolean shouldCompileJavaSrcs,
208-
Object nativeLibs,
209-
Object transitiveNativeLibs)
158+
boolean shouldCompileJavaSrcs)
210159
throws EvalException {
211160

212161
return new AndroidApplicationResourceInfo(
@@ -220,9 +169,7 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
220169
fromNoneable(resourcesZip, Artifact.class),
221170
fromNoneable(databindingLayoutInfoZip, Artifact.class),
222171
fromNoneable(buildStampJar, Artifact.class),
223-
shouldCompileJavaSrcs,
224-
AndroidStarlarkData.getNativeLibs(nativeLibs),
225-
AndroidStarlarkData.fromNoneableDepset(transitiveNativeLibs, "transitive_native_libs"));
172+
shouldCompileJavaSrcs);
226173
}
227174
}
228175
}

src/main/java/com/google/devtools/build/lib/rules/android/AndroidBinary.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,10 +408,12 @@ private static RuleConfiguredTargetBuilder init(
408408
.build(ruleContext));
409409
}
410410

411+
AndroidBinaryNativeLibsInfo nativeLibsInfo =
412+
ruleContext.getPrerequisite("application_resources", AndroidBinaryNativeLibsInfo.PROVIDER);
413+
411414
NativeLibs nativeLibs;
412-
if (androidApplicationResourceInfo != null
413-
&& androidApplicationResourceInfo.getNativeLibs() != null) {
414-
nativeLibs = androidApplicationResourceInfo.getNativeLibs();
415+
if (nativeLibsInfo != null && nativeLibsInfo.getNativeLibs() != null) {
416+
nativeLibs = nativeLibsInfo.getNativeLibs();
415417
} else {
416418
nativeLibs =
417419
NativeLibs.fromLinkedNativeDeps(
@@ -422,9 +424,8 @@ private static RuleConfiguredTargetBuilder init(
422424
}
423425

424426
final NestedSet<Artifact> nativeLibsAar;
425-
if (androidApplicationResourceInfo != null
426-
&& androidApplicationResourceInfo.getTransitiveNativeLibs() != null) {
427-
nativeLibsAar = androidApplicationResourceInfo.getTransitiveNativeLibs();
427+
if (nativeLibsInfo != null && nativeLibsInfo.getTransitiveNativeLibs() != null) {
428+
nativeLibsAar = nativeLibsInfo.getTransitiveNativeLibs();
428429
} else {
429430
nativeLibsAar = getTransitiveNativeLibs(ruleContext);
430431
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright 2022 The Bazel Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.google.devtools.build.lib.rules.android;
15+
16+
import com.google.common.collect.ImmutableMap;
17+
import com.google.common.collect.Maps;
18+
import com.google.devtools.build.lib.actions.Artifact;
19+
import com.google.devtools.build.lib.collect.nestedset.Depset;
20+
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
21+
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
22+
import com.google.devtools.build.lib.packages.BuiltinProvider;
23+
import com.google.devtools.build.lib.packages.NativeInfo;
24+
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidApplicationResourceInfoApi;
25+
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidBinaryNativeLibsInfoApi;
26+
import java.util.Map.Entry;
27+
import javax.annotation.Nullable;
28+
import net.starlark.java.eval.Dict;
29+
import net.starlark.java.eval.EvalException;
30+
31+
/** A provider for native libs for android_binary. */
32+
@Immutable
33+
public class AndroidBinaryNativeLibsInfo extends NativeInfo
34+
implements AndroidBinaryNativeLibsInfoApi<Artifact> {
35+
36+
/** Singleton instance of the provider type for {@link AndroidBinaryNativeLibsInfo}. */
37+
public static final AndroidBinaryNativeLibsInfoProvider PROVIDER =
38+
new AndroidBinaryNativeLibsInfoProvider();
39+
40+
private final NativeLibs nativeLibs;
41+
private final NestedSet<Artifact> transitiveNativeLibs;
42+
43+
AndroidBinaryNativeLibsInfo(NativeLibs nativeLibs, NestedSet<Artifact> transitiveNativeLibs) {
44+
this.nativeLibs = nativeLibs;
45+
this.transitiveNativeLibs = transitiveNativeLibs;
46+
}
47+
48+
@Override
49+
public AndroidBinaryNativeLibsInfoProvider getProvider() {
50+
return PROVIDER;
51+
}
52+
53+
@Nullable
54+
@Override
55+
public Dict<String, Depset> getNativeLibsStarlark() {
56+
if (nativeLibs == null) {
57+
return null;
58+
}
59+
return Dict.immutableCopyOf(
60+
Maps.transformValues(nativeLibs.getMap(), set -> Depset.of(Artifact.TYPE, set)));
61+
}
62+
63+
@Nullable
64+
@Override
65+
public Artifact getNativeLibsNameStarlark() {
66+
if (nativeLibs == null) {
67+
return null;
68+
}
69+
return nativeLibs.getName();
70+
}
71+
72+
@Nullable
73+
@Override
74+
public Depset getTransitiveNativeLibsStarlark() {
75+
if (transitiveNativeLibs == null) {
76+
return null;
77+
}
78+
return Depset.of(Artifact.TYPE, transitiveNativeLibs);
79+
}
80+
81+
@Nullable
82+
public NativeLibs getNativeLibs() {
83+
return nativeLibs;
84+
}
85+
86+
@Nullable
87+
public NestedSet<Artifact> getTransitiveNativeLibs() {
88+
return transitiveNativeLibs;
89+
}
90+
91+
/** Provider for {@link AndroidBinaryNativeLibsInfo}. */
92+
public static class AndroidBinaryNativeLibsInfoProvider
93+
extends BuiltinProvider<AndroidBinaryNativeLibsInfo>
94+
implements AndroidBinaryNativeLibsInfoApi.Provider<Artifact> {
95+
96+
private AndroidBinaryNativeLibsInfoProvider() {
97+
super(AndroidApplicationResourceInfoApi.NAME, AndroidBinaryNativeLibsInfo.class);
98+
}
99+
100+
@Override
101+
public AndroidBinaryNativeLibsInfoApi<Artifact> createInfo(
102+
Dict<?, ?> nativeLibs, Object nativeLibsName, Object transitiveNativeLibs)
103+
throws EvalException {
104+
Dict<String, Depset> nativeLibsDict =
105+
Dict.cast(nativeLibs, String.class, Depset.class, "native_libs");
106+
ImmutableMap.Builder<String, NestedSet<Artifact>> nativeLibsMapBuilder =
107+
ImmutableMap.builder();
108+
for (Entry<String, Depset> entry : nativeLibsDict.entrySet()) {
109+
nativeLibsMapBuilder.put(
110+
entry.getKey(), Depset.cast(entry.getValue(), Artifact.class, "native_libs"));
111+
}
112+
return new AndroidBinaryNativeLibsInfo(
113+
NativeLibs.of(
114+
nativeLibsMapBuilder.buildOrThrow(),
115+
AndroidStarlarkData.fromNoneable(nativeLibsName, Artifact.class)),
116+
AndroidStarlarkData.fromNoneableDepset(transitiveNativeLibs, "transitive_native_libs"));
117+
}
118+
}
119+
}

src/main/java/com/google/devtools/build/lib/rules/android/AndroidStarlarkData.java

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package com.google.devtools.build.lib.rules.android;
1515

1616
import com.google.common.collect.ImmutableList;
17-
import com.google.common.collect.ImmutableMap;
1817
import com.google.devtools.build.lib.actions.Artifact;
1918
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
2019
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
@@ -31,7 +30,6 @@
3130
import com.google.devtools.build.lib.packages.NativeInfo;
3231
import com.google.devtools.build.lib.packages.Provider;
3332
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
34-
import com.google.devtools.build.lib.packages.StructImpl;
3533
import com.google.devtools.build.lib.rules.android.AndroidLibraryAarInfo.Aar;
3634
import com.google.devtools.build.lib.rules.android.databinding.DataBinding;
3735
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
@@ -46,7 +44,6 @@
4644
import com.google.devtools.build.lib.vfs.PathFragment;
4745
import java.util.List;
4846
import java.util.Map;
49-
import java.util.Map.Entry;
5047
import java.util.Objects;
5148
import java.util.Optional;
5249
import javax.annotation.Nullable;
@@ -71,26 +68,6 @@ public abstract class AndroidStarlarkData
7168
AndroidBinaryDataInfo,
7269
ValidatedAndroidResources> {
7370

74-
@Nullable
75-
public static NativeLibs getNativeLibs(Object struct) throws EvalException {
76-
if (struct != Starlark.NONE) {
77-
StructImpl s = (StructImpl) struct;
78-
Dict<String, Depset> dict =
79-
Dict.cast(s.getValue("libs", Dict.class), String.class, Depset.class, "libs");
80-
ImmutableMap.Builder<String, NestedSet<Artifact>> nativeLibsMapBuilder =
81-
ImmutableMap.builder();
82-
for (Entry<String, Depset> entry : dict.entrySet()) {
83-
nativeLibsMapBuilder.put(
84-
entry.getKey(), Depset.cast(entry.getValue(), Artifact.class, "libs"));
85-
}
86-
return NativeLibs.of(
87-
nativeLibsMapBuilder.buildOrThrow(),
88-
fromNoneable(s.getValue("libs_name"), Artifact.class));
89-
} else {
90-
return null;
91-
}
92-
}
93-
9471
public abstract AndroidSemantics getAndroidSemantics();
9572

9673
@Override

0 commit comments

Comments
 (0)