From 4ec178c5dbf91f80e860de815ee571c1f67108eb Mon Sep 17 00:00:00 2001 From: Cole Faust Date: Fri, 13 Jan 2023 12:03:38 -0800 Subject: [PATCH] Replace RuleBuilder usages with static rules in app_import.go RuleBuilder creates a new rule in the ninja file every time it's used, but in this case the rules were all exactly the same except that the input/output file paths were different. This can be converted to a single static rule instead. Bug: 262629589 Test: m nothing Change-Id: Iaa887c66a757da13293a3614c000d3be02a2a1b0 --- java/app_import.go | 53 +++++++++++++++++++++++------------------ java/app_import_test.go | 27 +++++++-------------- 2 files changed, 38 insertions(+), 42 deletions(-) diff --git a/java/app_import.go b/java/app_import.go index 8c1e19c3e..e24e7804e 100644 --- a/java/app_import.go +++ b/java/app_import.go @@ -17,6 +17,7 @@ package java // This file contains the module implementations for android_app_import and android_test_import. import ( + "github.com/google/blueprint" "reflect" "github.com/google/blueprint/proptools" @@ -31,6 +32,24 @@ func init() { initAndroidAppImportVariantGroupTypes() } +var ( + uncompressEmbeddedJniLibsRule = pctx.AndroidStaticRule("uncompress-embedded-jni-libs", blueprint.RuleParams{ + Command: `if (zipinfo $in 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + + `${config.Zip2ZipCmd} -i $in -o $out -0 'lib/**/*.so'` + + `; else cp -f $in $out; fi`, + CommandDeps: []string{"${config.Zip2ZipCmd}"}, + Description: "Uncompress embedded JNI libs", + }) + + uncompressDexRule = pctx.AndroidStaticRule("uncompress-dex", blueprint.RuleParams{ + Command: `if (zipinfo $in '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then ` + + `${config.Zip2ZipCmd} -i $in -o $out -0 'classes*.dex'` + + `; else cp -f $in $out; fi`, + CommandDeps: []string{"${config.Zip2ZipCmd}"}, + Description: "Uncompress dex files", + }) +) + func RegisterAppImportBuildComponents(ctx android.RegistrationContext) { ctx.RegisterModuleType("android_app_import", AndroidAppImportFactory) ctx.RegisterModuleType("android_test_import", AndroidTestImportFactory) @@ -193,15 +212,12 @@ func (a *AndroidAppImport) uncompressEmbeddedJniLibs( }) return } - rule := android.NewRuleBuilder(pctx, ctx) - rule.Command(). - Textf(`if (zipinfo %s 'lib/*.so' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath). - BuiltTool("zip2zip"). - FlagWithInput("-i ", inputPath). - FlagWithOutput("-o ", outputPath). - FlagWithArg("-0 ", "'lib/**/*.so'"). - Textf(`; else cp -f %s %s; fi`, inputPath, outputPath) - rule.Build("uncompress-embedded-jni-libs", "Uncompress embedded JIN libs") + + ctx.Build(pctx, android.BuildParams{ + Rule: uncompressEmbeddedJniLibsRule, + Input: inputPath, + Output: outputPath, + }) } // Returns whether this module should have the dex file stored uncompressed in the APK. @@ -218,19 +234,6 @@ func (a *AndroidAppImport) shouldUncompressDex(ctx android.ModuleContext) bool { return shouldUncompressDex(ctx, &a.dexpreopter) } -func (a *AndroidAppImport) uncompressDex( - ctx android.ModuleContext, inputPath android.Path, outputPath android.OutputPath) { - rule := android.NewRuleBuilder(pctx, ctx) - rule.Command(). - Textf(`if (zipinfo %s '*.dex' 2>/dev/null | grep -v ' stor ' >/dev/null) ; then`, inputPath). - BuiltTool("zip2zip"). - FlagWithInput("-i ", inputPath). - FlagWithOutput("-o ", outputPath). - FlagWithArg("-0 ", "'classes*.dex'"). - Textf(`; else cp -f %s %s; fi`, inputPath, outputPath) - rule.Build("uncompress-dex", "Uncompress dex files") -} - func (a *AndroidAppImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { a.generateAndroidBuildActions(ctx) } @@ -306,7 +309,11 @@ func (a *AndroidAppImport) generateAndroidBuildActions(ctx android.ModuleContext a.dexpreopter.dexpreopt(ctx, jnisUncompressed) if a.dexpreopter.uncompressedDex { dexUncompressed := android.PathForModuleOut(ctx, "dex-uncompressed", ctx.ModuleName()+".apk") - a.uncompressDex(ctx, jnisUncompressed, dexUncompressed.OutputPath) + ctx.Build(pctx, android.BuildParams{ + Rule: uncompressDexRule, + Input: jnisUncompressed, + Output: dexUncompressed, + }) jnisUncompressed = dexUncompressed } diff --git a/java/app_import_test.go b/java/app_import_test.go index ad27e3ae7..a29606f89 100644 --- a/java/app_import_test.go +++ b/java/app_import_test.go @@ -17,7 +17,6 @@ package java import ( "fmt" "reflect" - "regexp" "strings" "testing" @@ -294,7 +293,6 @@ func TestAndroidAppImport_DpiVariants(t *testing.T) { }, } - jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") for _, test := range testCases { result := android.GroupFixturePreparers( PrepareForTestWithJavaDefaultModules, @@ -305,13 +303,9 @@ func TestAndroidAppImport_DpiVariants(t *testing.T) { ).RunTestWithBp(t, bp) variant := result.ModuleForTests("foo", "android_common") - jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command - matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) - if len(matches) != 2 { - t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) - } - if strings.HasSuffix(matches[1], test.expected) { - t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) + input := variant.Output("jnis-uncompressed/foo.apk").Input.String() + if strings.HasSuffix(input, test.expected) { + t.Errorf("wrong src apk, expected: %q got: %q", test.expected, input) } provenanceMetaDataRule := variant.Rule("genProvenanceMetaData") @@ -456,7 +450,6 @@ func TestAndroidAppImport_ArchVariants(t *testing.T) { }, } - jniRuleRe := regexp.MustCompile("^if \\(zipinfo (\\S+)") for _, test := range testCases { ctx, _ := testJava(t, test.bp) @@ -469,13 +462,9 @@ func TestAndroidAppImport_ArchVariants(t *testing.T) { android.AssertDeepEquals(t, "Provenance metadata is not empty", android.TestingBuildParams{}, rule) continue } - jniRuleCommand := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command - matches := jniRuleRe.FindStringSubmatch(jniRuleCommand) - if len(matches) != 2 { - t.Errorf("failed to extract the src apk path from %q", jniRuleCommand) - } - if strings.HasSuffix(matches[1], test.expected) { - t.Errorf("wrong src apk, expected: %q got: %q", test.expected, matches[1]) + input := variant.Output("jnis-uncompressed/foo.apk").Input.String() + if strings.HasSuffix(input, test.expected) { + t.Errorf("wrong src apk, expected: %q got: %q", test.expected, input) } rule := variant.Rule("genProvenanceMetaData") android.AssertStringEquals(t, "Invalid input", test.artifactPath, rule.Inputs[0].String()) @@ -686,8 +675,8 @@ func TestAndroidTestImport_NoJinUncompressForPresigned(t *testing.T) { `) variant := ctx.ModuleForTests("foo", "android_common") - jniRule := variant.Output("jnis-uncompressed/foo.apk").RuleParams.Command - if !strings.HasPrefix(jniRule, "if (zipinfo") { + jniRule := variant.Output("jnis-uncompressed/foo.apk").BuildParams.Rule.String() + if jniRule == android.Cp.String() { t.Errorf("Unexpected JNI uncompress rule command: " + jniRule) }