Skip to content

Commit

Permalink
Add a new provider for injecting native libs in android_binary (#18753)
Browse files Browse the repository at this point in the history
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
(cherry picked from commit e3fae33)
  • Loading branch information
hvadehra authored Jun 23, 2023
1 parent 351188d commit 7541379
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.google.devtools.build.lib.rules.android.AndroidApplicationResourceInfo;
import com.google.devtools.build.lib.rules.android.AndroidAssetsInfo;
import com.google.devtools.build.lib.rules.android.AndroidBinaryDataInfo;
import com.google.devtools.build.lib.rules.android.AndroidBinaryNativeLibsInfo;
import com.google.devtools.build.lib.rules.android.AndroidCcLinkParamsProvider;
import com.google.devtools.build.lib.rules.android.AndroidConfiguration;
import com.google.devtools.build.lib.rules.android.AndroidDeviceBrokerInfo;
Expand Down Expand Up @@ -421,6 +422,7 @@ public void init(ConfiguredRuleClassProvider.Builder builder) {
AndroidFeatureFlagSetProvider.PROVIDER,
ProguardMappingProvider.PROVIDER,
AndroidBinaryDataInfo.PROVIDER,
AndroidBinaryNativeLibsInfo.PROVIDER,
BaselineProfileProvider.PROVIDER);
builder.addStarlarkBootstrap(bootstrap);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,11 @@

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

import com.google.common.collect.Maps;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidApplicationResourceInfoApi;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;

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

AndroidApplicationResourceInfo(
Artifact resourceApk,
Expand All @@ -61,9 +54,7 @@ public class AndroidApplicationResourceInfo extends NativeInfo
Artifact resourcesZip,
Artifact databindingLayoutInfoZip,
Artifact buildStampJar,
boolean shouldCompileJavaSrcs,
NativeLibs nativeLibs,
NestedSet<Artifact> transitiveNativeLibs) {
boolean shouldCompileJavaSrcs) {
this.resourceApk = resourceApk;
this.resourceJavaSrcJar = resourceJavaSrcJar;
this.resourceJavaClassJar = resourceJavaClassJar;
Expand All @@ -75,8 +66,6 @@ public class AndroidApplicationResourceInfo extends NativeInfo
this.databindingLayoutInfoZip = databindingLayoutInfoZip;
this.buildStampJar = buildStampJar;
this.shouldCompileJavaSrcs = shouldCompileJavaSrcs;
this.nativeLibs = nativeLibs;
this.transitiveNativeLibs = transitiveNativeLibs;
}

@Override
Expand Down Expand Up @@ -145,44 +134,6 @@ public boolean shouldCompileJavaSrcs() {
return shouldCompileJavaSrcs;
}

@Nullable
@Override
public Dict<String, Depset> getNativeLibsStarlark() {
if (nativeLibs == null) {
return null;
}
return Dict.immutableCopyOf(
Maps.transformValues(nativeLibs.getMap(), set -> Depset.of(Artifact.TYPE, set)));
}

@Nullable
@Override
public Artifact getNativeLibsNameStarlark() {
if (nativeLibs == null) {
return null;
}
return nativeLibs.getName();
}

@Nullable
@Override
public Depset getTransitiveNativeLibsStarlark() {
if (transitiveNativeLibs == null) {
return null;
}
return Depset.of(Artifact.TYPE, transitiveNativeLibs);
}

@Nullable
public NativeLibs getNativeLibs() {
return nativeLibs;
}

@Nullable
public NestedSet<Artifact> getTransitiveNativeLibs() {
return transitiveNativeLibs;
}

/** Provider for {@link AndroidApplicationResourceInfo}. */
public static class AndroidApplicationResourceInfoProvider
extends BuiltinProvider<AndroidApplicationResourceInfo>
Expand All @@ -204,9 +155,7 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
Object resourcesZip,
Object databindingLayoutInfoZip,
Object buildStampJar,
boolean shouldCompileJavaSrcs,
Object nativeLibs,
Object transitiveNativeLibs)
boolean shouldCompileJavaSrcs)
throws EvalException {

return new AndroidApplicationResourceInfo(
Expand All @@ -220,9 +169,7 @@ public AndroidApplicationResourceInfoApi<Artifact> createInfo(
fromNoneable(resourcesZip, Artifact.class),
fromNoneable(databindingLayoutInfoZip, Artifact.class),
fromNoneable(buildStampJar, Artifact.class),
shouldCompileJavaSrcs,
AndroidStarlarkData.getNativeLibs(nativeLibs),
AndroidStarlarkData.fromNoneableDepset(transitiveNativeLibs, "transitive_native_libs"));
shouldCompileJavaSrcs);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,12 @@ private static RuleConfiguredTargetBuilder init(
.build(ruleContext));
}

AndroidBinaryNativeLibsInfo nativeLibsInfo =
ruleContext.getPrerequisite("application_resources", AndroidBinaryNativeLibsInfo.PROVIDER);

NativeLibs nativeLibs;
if (androidApplicationResourceInfo != null
&& androidApplicationResourceInfo.getNativeLibs() != null) {
nativeLibs = androidApplicationResourceInfo.getNativeLibs();
if (nativeLibsInfo != null && nativeLibsInfo.getNativeLibs() != null) {
nativeLibs = nativeLibsInfo.getNativeLibs();
} else {
nativeLibs =
NativeLibs.fromLinkedNativeDeps(
Expand All @@ -422,9 +424,8 @@ private static RuleConfiguredTargetBuilder init(
}

final NestedSet<Artifact> nativeLibsAar;
if (androidApplicationResourceInfo != null
&& androidApplicationResourceInfo.getTransitiveNativeLibs() != null) {
nativeLibsAar = androidApplicationResourceInfo.getTransitiveNativeLibs();
if (nativeLibsInfo != null && nativeLibsInfo.getTransitiveNativeLibs() != null) {
nativeLibsAar = nativeLibsInfo.getTransitiveNativeLibs();
} else {
nativeLibsAar = getTransitiveNativeLibs(ruleContext);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2022 The Bazel Authors. 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.android;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.collect.nestedset.Depset;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.concurrent.ThreadSafety.Immutable;
import com.google.devtools.build.lib.packages.BuiltinProvider;
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidApplicationResourceInfoApi;
import com.google.devtools.build.lib.starlarkbuildapi.android.AndroidBinaryNativeLibsInfoApi;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import net.starlark.java.eval.Dict;
import net.starlark.java.eval.EvalException;

/** A provider for native libs for android_binary. */
@Immutable
public class AndroidBinaryNativeLibsInfo extends NativeInfo
implements AndroidBinaryNativeLibsInfoApi<Artifact> {

/** Singleton instance of the provider type for {@link AndroidBinaryNativeLibsInfo}. */
public static final AndroidBinaryNativeLibsInfoProvider PROVIDER =
new AndroidBinaryNativeLibsInfoProvider();

private final NativeLibs nativeLibs;
private final NestedSet<Artifact> transitiveNativeLibs;

AndroidBinaryNativeLibsInfo(NativeLibs nativeLibs, NestedSet<Artifact> transitiveNativeLibs) {
this.nativeLibs = nativeLibs;
this.transitiveNativeLibs = transitiveNativeLibs;
}

@Override
public AndroidBinaryNativeLibsInfoProvider getProvider() {
return PROVIDER;
}

@Nullable
@Override
public Dict<String, Depset> getNativeLibsStarlark() {
if (nativeLibs == null) {
return null;
}
return Dict.immutableCopyOf(
Maps.transformValues(nativeLibs.getMap(), set -> Depset.of(Artifact.TYPE, set)));
}

@Nullable
@Override
public Artifact getNativeLibsNameStarlark() {
if (nativeLibs == null) {
return null;
}
return nativeLibs.getName();
}

@Nullable
@Override
public Depset getTransitiveNativeLibsStarlark() {
if (transitiveNativeLibs == null) {
return null;
}
return Depset.of(Artifact.TYPE, transitiveNativeLibs);
}

@Nullable
public NativeLibs getNativeLibs() {
return nativeLibs;
}

@Nullable
public NestedSet<Artifact> getTransitiveNativeLibs() {
return transitiveNativeLibs;
}

/** Provider for {@link AndroidBinaryNativeLibsInfo}. */
public static class AndroidBinaryNativeLibsInfoProvider
extends BuiltinProvider<AndroidBinaryNativeLibsInfo>
implements AndroidBinaryNativeLibsInfoApi.Provider<Artifact> {

private AndroidBinaryNativeLibsInfoProvider() {
super(AndroidApplicationResourceInfoApi.NAME, AndroidBinaryNativeLibsInfo.class);
}

@Override
public AndroidBinaryNativeLibsInfoApi<Artifact> createInfo(
Dict<?, ?> nativeLibs, Object nativeLibsName, Object transitiveNativeLibs)
throws EvalException {
Dict<String, Depset> nativeLibsDict =
Dict.cast(nativeLibs, String.class, Depset.class, "native_libs");
ImmutableMap.Builder<String, NestedSet<Artifact>> nativeLibsMapBuilder =
ImmutableMap.builder();
for (Entry<String, Depset> entry : nativeLibsDict.entrySet()) {
nativeLibsMapBuilder.put(
entry.getKey(), Depset.cast(entry.getValue(), Artifact.class, "native_libs"));
}
return new AndroidBinaryNativeLibsInfo(
NativeLibs.of(
nativeLibsMapBuilder.buildOrThrow(),
AndroidStarlarkData.fromNoneable(nativeLibsName, Artifact.class)),
AndroidStarlarkData.fromNoneableDepset(transitiveNativeLibs, "transitive_native_libs"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package com.google.devtools.build.lib.rules.android;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.SpecialArtifact;
import com.google.devtools.build.lib.analysis.ConfiguredTarget;
Expand All @@ -31,7 +30,6 @@
import com.google.devtools.build.lib.packages.NativeInfo;
import com.google.devtools.build.lib.packages.Provider;
import com.google.devtools.build.lib.packages.RuleClass.ConfiguredTargetFactory.RuleErrorException;
import com.google.devtools.build.lib.packages.StructImpl;
import com.google.devtools.build.lib.rules.android.AndroidLibraryAarInfo.Aar;
import com.google.devtools.build.lib.rules.android.databinding.DataBinding;
import com.google.devtools.build.lib.rules.java.JavaCompilationArgsProvider;
Expand All @@ -46,7 +44,6 @@
import com.google.devtools.build.lib.vfs.PathFragment;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
Expand All @@ -71,26 +68,6 @@ public abstract class AndroidStarlarkData
AndroidBinaryDataInfo,
ValidatedAndroidResources> {

@Nullable
public static NativeLibs getNativeLibs(Object struct) throws EvalException {
if (struct != Starlark.NONE) {
StructImpl s = (StructImpl) struct;
Dict<String, Depset> dict =
Dict.cast(s.getValue("libs", Dict.class), String.class, Depset.class, "libs");
ImmutableMap.Builder<String, NestedSet<Artifact>> nativeLibsMapBuilder =
ImmutableMap.builder();
for (Entry<String, Depset> entry : dict.entrySet()) {
nativeLibsMapBuilder.put(
entry.getKey(), Depset.cast(entry.getValue(), Artifact.class, "libs"));
}
return NativeLibs.of(
nativeLibsMapBuilder.buildOrThrow(),
fromNoneable(s.getValue("libs_name"), Artifact.class));
} else {
return null;
}
}

public abstract AndroidSemantics getAndroidSemantics();

@Override
Expand Down
Loading

0 comments on commit 7541379

Please sign in to comment.