From 0856f9e2273073df6cf4e512e593933b8d449dd3 Mon Sep 17 00:00:00 2001 From: Alix Date: Fri, 20 Jan 2023 22:15:30 +0000 Subject: [PATCH] bp2build kt for android_binary Test: bp2build tests and locally built MusicKotlin (cannot allowlist yet due to soong/bazel custom_package differences) Change-Id: I1cbd0fe2af0e6eeed65cf8d57dc85c1f54646c50 --- bp2build/android_app_conversion_test.go | 79 +++++++++++++++++++++++++ java/app.go | 44 +++++++++++--- 2 files changed, 115 insertions(+), 8 deletions(-) diff --git a/bp2build/android_app_conversion_test.go b/bp2build/android_app_conversion_test.go index 067e34fac..4d18f83de 100644 --- a/bp2build/android_app_conversion_test.go +++ b/bp2build/android_app_conversion_test.go @@ -227,3 +227,82 @@ java_library{ }), }}) } + +func TestAndroidAppKotlinSrcs(t *testing.T) { + runAndroidAppTestCase(t, Bp2buildTestCase{ + Description: "Android app with kotlin sources and common_srcs", + ModuleTypeUnderTest: "android_app", + ModuleTypeUnderTestFactory: java.AndroidAppFactory, + Filesystem: map[string]string{ + "res/res.png": "", + }, + Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` +android_app { + name: "foo", + srcs: ["a.java", "b.kt"], + certificate: ":foocert", + manifest: "fooManifest.xml", + libs: ["barLib"] +} +java_library{ + name: "barLib", +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_library", "barLib", AttrNameToString{}), + MakeNeverlinkDuplicateTarget("java_library", "barLib"), + MakeBazelTarget("android_library", "foo_kt", AttrNameToString{ + "srcs": `[ + "a.java", + "b.kt", + ]`, + "manifest": `"fooManifest.xml"`, + "resource_files": `["res/res.png"]`, + "deps": `[":barLib-neverlink"]`, + }), + MakeBazelTarget("android_binary", "foo", AttrNameToString{ + "deps": `[":foo_kt"]`, + "certificate": `":foocert"`, + "manifest": `"fooManifest.xml"`, + }), + }}) +} + +func TestAndroidAppCommonSrcs(t *testing.T) { + runAndroidAppTestCase(t, Bp2buildTestCase{ + Description: "Android app with common_srcs", + ModuleTypeUnderTest: "android_app", + ModuleTypeUnderTestFactory: java.AndroidAppFactory, + Filesystem: map[string]string{ + "res/res.png": "", + }, + Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` +android_app { + name: "foo", + srcs: ["a.java"], + common_srcs: ["b.kt"], + certificate: "foocert", + manifest: "fooManifest.xml", + libs: ["barLib"], +} +java_library{ + name: "barLib", +} +`, + ExpectedBazelTargets: []string{ + MakeBazelTarget("java_library", "barLib", AttrNameToString{}), + MakeNeverlinkDuplicateTarget("java_library", "barLib"), + MakeBazelTarget("android_library", "foo_kt", AttrNameToString{ + "srcs": `["a.java"]`, + "common_srcs": `["b.kt"]`, + "manifest": `"fooManifest.xml"`, + "resource_files": `["res/res.png"]`, + "deps": `[":barLib-neverlink"]`, + }), + MakeBazelTarget("android_binary", "foo", AttrNameToString{ + "deps": `[":foo_kt"]`, + "certificate_name": `"foocert"`, + "manifest": `"fooManifest.xml"`, + }), + }}) +} diff --git a/java/app.go b/java/app.go index 4d9c407c5..e7e52d471 100755 --- a/java/app.go +++ b/java/app.go @@ -1508,20 +1508,48 @@ func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) { certificate, certificateName := android.BazelStringOrLabelFromProp(ctx, a.overridableAppProperties.Certificate) - attrs := &bazelAndroidAppAttributes{ - commonAttrs, - aapt, - deps, + appAttrs := &bazelAndroidAppAttributes{ // TODO(b/209576404): handle package name override by product variable PRODUCT_MANIFEST_PACKAGE_NAME_OVERRIDES - a.overridableAppProperties.Package_name, - certificate, - certificateName, + Custom_package: a.overridableAppProperties.Package_name, + Certificate: certificate, + Certificate_name: certificateName, } props := bazel.BazelTargetModuleProperties{ Rule_class: "android_binary", Bzl_load_location: "//build/bazel/rules/android:rules.bzl", } - ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: a.Name()}, attrs) + + if !bp2BuildInfo.hasKotlinSrcs && len(a.properties.Common_srcs) == 0 { + appAttrs.javaCommonAttributes = commonAttrs + appAttrs.bazelAapt = aapt + appAttrs.Deps = deps + ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: a.Name()}, appAttrs) + } else { + ktName := a.Name() + "_kt" + commonAttrs.Common_srcs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, a.properties.Common_srcs)) + ctx.CreateBazelTargetModule( + bazel.BazelTargetModuleProperties{ + Rule_class: "android_library", + Bzl_load_location: "//build/bazel/rules/android:rules.bzl", + }, + android.CommonAttributes{Name: ktName}, + &bazelAndroidLibrary{ + javaLibraryAttributes: &javaLibraryAttributes{ + javaCommonAttributes: commonAttrs, + Deps: deps, + }, + bazelAapt: aapt, + }, + ) + + appAttrs.bazelAapt = &bazelAapt{Manifest: aapt.Manifest} + appAttrs.Deps = bazel.MakeSingleLabelListAttribute(bazel.Label{Label: ":" + ktName}) + ctx.CreateBazelTargetModule( + props, + android.CommonAttributes{Name: a.Name()}, + appAttrs, + ) + } }