Merge "Bp2build support for app R8-related fields" into main

This commit is contained in:
Romain Jobredeaux 2023-08-31 18:54:49 +00:00 committed by Gerrit Code Review
commit 765beb237a
3 changed files with 121 additions and 27 deletions

View file

@ -47,6 +47,11 @@ android_app {
name: "TestApp", name: "TestApp",
srcs: ["app.java"], srcs: ["app.java"],
sdk_version: "current", sdk_version: "current",
optimize: {
shrink: true,
optimize: true,
obfuscate: true,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -75,17 +80,25 @@ func TestAndroidAppAllSupportedFields(t *testing.T) {
}, },
Blueprint: simpleModuleDoNotConvertBp2build("android_app", "static_lib_dep") + ` Blueprint: simpleModuleDoNotConvertBp2build("android_app", "static_lib_dep") + `
android_app { android_app {
name: "TestApp", name: "TestApp",
srcs: ["app.java"], srcs: ["app.java"],
sdk_version: "current", sdk_version: "current",
package_name: "com.google", package_name: "com.google",
resource_dirs: ["resa", "resb"], resource_dirs: ["resa", "resb"],
manifest: "manifest/AndroidManifest.xml", manifest: "manifest/AndroidManifest.xml",
static_libs: ["static_lib_dep"], static_libs: ["static_lib_dep"],
java_version: "7", java_version: "7",
certificate: "foocert", certificate: "foocert",
required: ["static_lib_dep"], required: ["static_lib_dep"],
asset_dirs: ["assets_"], asset_dirs: ["assets_"],
optimize: {
enabled: true,
optimize: false,
proguard_flags_files: ["proguard.flags"],
shrink: false,
obfuscate: false,
ignore_warnings: true,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -103,6 +116,14 @@ android_app {
"java_version": `"7"`, "java_version": `"7"`,
"sdk_version": `"current"`, "sdk_version": `"current"`,
"certificate_name": `"foocert"`, "certificate_name": `"foocert"`,
"proguard_specs": `[
"proguard.flags",
":TestApp_proguard_flags",
]`,
}),
MakeBazelTarget("genrule", "TestApp_proguard_flags", AttrNameToString{
"outs": `["TestApp_proguard.flags"]`,
"cmd": `"echo -ignorewarning -dontshrink -dontoptimize -dontobfuscate > $(OUTS)"`,
}), }),
}}) }})
} }
@ -129,7 +150,10 @@ android_app {
x86: { x86: {
srcs: ["x86.java"], srcs: ["x86.java"],
} }
} },
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -142,6 +166,7 @@ android_app {
"manifest": `"AndroidManifest.xml"`, "manifest": `"AndroidManifest.xml"`,
"resource_files": `["res/res.png"]`, "resource_files": `["res/res.png"]`,
"sdk_version": `"current"`, "sdk_version": `"current"`,
"optimize": `False`,
}), }),
}}) }})
} }
@ -157,6 +182,9 @@ android_app {
name: "TestApp", name: "TestApp",
certificate: ":foocert", certificate: ":foocert",
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -165,6 +193,7 @@ android_app {
"manifest": `"AndroidManifest.xml"`, "manifest": `"AndroidManifest.xml"`,
"resource_files": `[]`, "resource_files": `[]`,
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
"optimize": `False`,
}), }),
}}) }})
} }
@ -182,6 +211,9 @@ android_app {
name: "TestApp", name: "TestApp",
certificate: "foocert", certificate: "foocert",
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -190,6 +222,7 @@ android_app {
"manifest": `"AndroidManifest.xml"`, "manifest": `"AndroidManifest.xml"`,
"resource_files": `[]`, "resource_files": `[]`,
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
"optimize": `False`,
}), }),
}}) }})
} }
@ -207,6 +240,9 @@ android_app {
name: "TestApp", name: "TestApp",
certificate: "foocert", certificate: "foocert",
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -215,6 +251,7 @@ android_app {
"manifest": `"AndroidManifest.xml"`, "manifest": `"AndroidManifest.xml"`,
"resource_files": `[]`, "resource_files": `[]`,
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
"optimize": `False`,
}), }),
}}) }})
} }
@ -225,12 +262,14 @@ func TestAndroidAppLibs(t *testing.T) {
ModuleTypeUnderTest: "android_app", ModuleTypeUnderTest: "android_app",
ModuleTypeUnderTestFactory: java.AndroidAppFactory, ModuleTypeUnderTestFactory: java.AndroidAppFactory,
Filesystem: map[string]string{}, Filesystem: map[string]string{},
Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + Blueprint: simpleModuleDoNotConvertBp2build("java_library", "barLib") + `
simpleModuleDoNotConvertBp2build("java_library", "barLib") + `
android_app { android_app {
name: "foo", name: "foo",
libs: ["barLib"], libs: ["barLib"],
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -239,6 +278,7 @@ android_app {
"resource_files": `[]`, "resource_files": `[]`,
"deps": `[":barLib-neverlink"]`, "deps": `[":barLib-neverlink"]`,
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
"optimize": `False`,
}), }),
}}) }})
} }
@ -260,6 +300,9 @@ android_app {
manifest: "fooManifest.xml", manifest: "fooManifest.xml",
libs: ["barLib"], libs: ["barLib"],
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -278,6 +321,7 @@ android_app {
"certificate": `":foocert"`, "certificate": `":foocert"`,
"manifest": `"fooManifest.xml"`, "manifest": `"fooManifest.xml"`,
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
"optimize": `False`,
}), }),
}}) }})
} }
@ -290,15 +334,17 @@ func TestAndroidAppCommonSrcs(t *testing.T) {
Filesystem: map[string]string{ Filesystem: map[string]string{
"res/res.png": "", "res/res.png": "",
}, },
Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` Blueprint: `
android_app { android_app {
name: "foo", name: "foo",
srcs: ["a.java"], srcs: ["a.java"],
common_srcs: ["b.kt"], common_srcs: ["b.kt"],
certificate: "foocert",
manifest: "fooManifest.xml", manifest: "fooManifest.xml",
libs: ["barLib"], libs: ["barLib"],
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
java_library{ java_library{
name: "barLib", name: "barLib",
@ -315,10 +361,10 @@ java_library{
"sdk_version": `"current"`, // use as default "sdk_version": `"current"`, // use as default
}), }),
MakeBazelTarget("android_binary", "foo", AttrNameToString{ MakeBazelTarget("android_binary", "foo", AttrNameToString{
"deps": `[":foo_kt"]`, "deps": `[":foo_kt"]`,
"certificate_name": `"foocert"`, "manifest": `"fooManifest.xml"`,
"manifest": `"fooManifest.xml"`, "sdk_version": `"current"`, // use as default
"sdk_version": `"current"`, // use as default "optimize": `False`,
}), }),
}}) }})
} }
@ -331,14 +377,16 @@ func TestAndroidAppKotlinCflags(t *testing.T) {
Filesystem: map[string]string{ Filesystem: map[string]string{
"res/res.png": "", "res/res.png": "",
}, },
Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` Blueprint: `
android_app { android_app {
name: "foo", name: "foo",
srcs: ["a.java", "b.kt"], srcs: ["a.java", "b.kt"],
certificate: ":foocert",
manifest: "fooManifest.xml", manifest: "fooManifest.xml",
kotlincflags: ["-flag1", "-flag2"], kotlincflags: ["-flag1", "-flag2"],
sdk_version: "current", sdk_version: "current",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -357,9 +405,9 @@ android_app {
}), }),
MakeBazelTarget("android_binary", "foo", AttrNameToString{ MakeBazelTarget("android_binary", "foo", AttrNameToString{
"deps": `[":foo_kt"]`, "deps": `[":foo_kt"]`,
"certificate": `":foocert"`,
"manifest": `"fooManifest.xml"`, "manifest": `"fooManifest.xml"`,
"sdk_version": `"current"`, "sdk_version": `"current"`,
"optimize": `False`,
}), }),
}}) }})
} }
@ -370,13 +418,16 @@ func TestAndroidAppManifestSdkVersionsProvided(t *testing.T) {
ModuleTypeUnderTest: "android_app", ModuleTypeUnderTest: "android_app",
ModuleTypeUnderTestFactory: java.AndroidAppFactory, ModuleTypeUnderTestFactory: java.AndroidAppFactory,
Filesystem: map[string]string{}, Filesystem: map[string]string{},
Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` Blueprint: `
android_app { android_app {
name: "foo", name: "foo",
sdk_version: "current", sdk_version: "current",
min_sdk_version: "24", min_sdk_version: "24",
max_sdk_version: "30", max_sdk_version: "30",
target_sdk_version: "29", target_sdk_version: "29",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -389,6 +440,7 @@ android_app {
"targetSdkVersion": "29", "targetSdkVersion": "29",
}`, }`,
"sdk_version": `"current"`, "sdk_version": `"current"`,
"optimize": `False`,
}), }),
}}) }})
} }
@ -399,10 +451,13 @@ func TestAndroidAppMinAndTargetSdkDefaultToSdkVersion(t *testing.T) {
ModuleTypeUnderTest: "android_app", ModuleTypeUnderTest: "android_app",
ModuleTypeUnderTestFactory: java.AndroidAppFactory, ModuleTypeUnderTestFactory: java.AndroidAppFactory,
Filesystem: map[string]string{}, Filesystem: map[string]string{},
Blueprint: simpleModuleDoNotConvertBp2build("filegroup", "foocert") + ` Blueprint: `
android_app { android_app {
name: "foo", name: "foo",
sdk_version: "30", sdk_version: "30",
optimize: {
enabled: false,
},
} }
`, `,
ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{
@ -414,6 +469,7 @@ android_app {
"targetSdkVersion": "30", "targetSdkVersion": "30",
}`, }`,
"sdk_version": `"30"`, "sdk_version": `"30"`,
"optimize": `False`,
}), }),
}}) }})
} }

View file

@ -912,7 +912,7 @@ type genRuleProperties struct {
Out []string Out []string
} }
type bazelGenruleAttributes struct { type BazelGenruleAttributes struct {
Srcs bazel.LabelListAttribute Srcs bazel.LabelListAttribute
Outs []string Outs []string
Tools bazel.LabelListAttribute Tools bazel.LabelListAttribute
@ -1036,7 +1036,7 @@ func (m *Module) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
break break
} }
} }
attrs := &bazelGenruleAttributes{ attrs := &BazelGenruleAttributes{
Srcs: srcs, Srcs: srcs,
Outs: outs, Outs: outs,
Cmd: cmdProp, Cmd: cmdProp,

View file

@ -29,6 +29,7 @@ import (
"android/soong/bazel" "android/soong/bazel"
"android/soong/cc" "android/soong/cc"
"android/soong/dexpreopt" "android/soong/dexpreopt"
"android/soong/genrule"
"android/soong/tradefed" "android/soong/tradefed"
) )
@ -1614,6 +1615,8 @@ type bazelAndroidAppAttributes struct {
Certificate bazel.LabelAttribute Certificate bazel.LabelAttribute
Certificate_name bazel.StringAttribute Certificate_name bazel.StringAttribute
Manifest_values *manifestValueAttribute Manifest_values *manifestValueAttribute
Optimize *bool
Proguard_specs bazel.LabelListAttribute
} }
// ConvertWithBp2build is used to convert android_app to Bazel. // ConvertWithBp2build is used to convert android_app to Bazel.
@ -1665,6 +1668,41 @@ func (a *AndroidApp) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Manifest_values: manifestValues, Manifest_values: manifestValues,
} }
if !BoolDefault(a.dexProperties.Optimize.Enabled, true) {
appAttrs.Optimize = proptools.BoolPtr(false)
} else {
handCraftedFlags := ""
if Bool(a.dexProperties.Optimize.Ignore_warnings) {
handCraftedFlags += "-ignorewarning "
}
if !Bool(a.dexProperties.Optimize.Shrink) {
handCraftedFlags += "-dontshrink "
}
if !Bool(a.dexProperties.Optimize.Optimize) {
handCraftedFlags += "-dontoptimize "
}
if !Bool(a.dexProperties.Optimize.Obfuscate) {
handCraftedFlags += "-dontobfuscate "
}
appAttrs.Proguard_specs = bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, a.dexProperties.Optimize.Proguard_flags_files))
if handCraftedFlags != "" {
generatedFlagFileRuleName := a.Name() + "_proguard_flags"
ctx.CreateBazelTargetModule(bazel.BazelTargetModuleProperties{
Rule_class: "genrule",
}, android.CommonAttributes{
Name: generatedFlagFileRuleName,
SkipData: proptools.BoolPtr(true),
}, &genrule.BazelGenruleAttributes{
Outs: []string{a.Name() + "_proguard.flags"},
Cmd: bazel.StringAttribute{
Value: proptools.StringPtr("echo " + handCraftedFlags + "> $(OUTS)"),
},
})
appAttrs.Proguard_specs.Add(bazel.MakeLabelAttribute(":" + generatedFlagFileRuleName))
}
}
props := bazel.BazelTargetModuleProperties{ props := bazel.BazelTargetModuleProperties{
Rule_class: "android_binary", Rule_class: "android_binary",
Bzl_load_location: "//build/bazel/rules/android:android_binary.bzl", Bzl_load_location: "//build/bazel/rules/android:android_binary.bzl",