From 4fd997bc137e31ce66d6ef4ad64d166d72692187 Mon Sep 17 00:00:00 2001 From: Paul Duffin Date: Wed, 3 Feb 2021 20:06:33 +0000 Subject: [PATCH] Refactor the hiddenAPI() method for reusability A follow up change needs to be able to contribute to the information the hiddenapi process collates without having a dex file encoded. This change pushes all the functionality related to information gathering into the hiddenAPIGenerateCSV() method and then renames it and the hiddenAPI() method to make it clearer what they do. Bug: 178361284 Test: m droid Verified that hiddenapi files (both aggregated ones and for the individual modules) are not affected by this change. Change-Id: I04417720216a0fbadcd88e6185e7de6570af6216 --- apex/apex_test.go | 25 ++++++++++++++++++++++++- java/hiddenapi.go | 27 ++++++++++++++++----------- java/hiddenapi_singleton_test.go | 10 +--------- java/java.go | 4 ++-- java/testing.go | 9 +++++++++ 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/apex/apex_test.go b/apex/apex_test.go index 05ad79b63..12201a341 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -4495,6 +4495,12 @@ func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) { } } + checkHiddenAPIIndexInputs := func(t *testing.T, ctx *android.TestContext, expectedInputs string) { + hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index") + indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index") + java.CheckHiddenAPIRuleInputs(t, expectedInputs, indexRule) + } + t.Run("prebuilt only", func(t *testing.T) { bp := ` prebuilt_apex { @@ -4519,6 +4525,10 @@ func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) { ctx := testDexpreoptWithApexes(t, bp, "", transform) checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar") + + // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index + // file. + checkHiddenAPIIndexInputs(t, ctx, ``) }) t.Run("prebuilt with source library preferred", func(t *testing.T) { @@ -4588,6 +4598,10 @@ func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) { ctx := testDexpreoptWithApexes(t, bp, "", transform) checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar") + + // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index + // file. + checkHiddenAPIIndexInputs(t, ctx, ``) }) t.Run("prebuilt with source apex preferred", func(t *testing.T) { @@ -4632,6 +4646,11 @@ func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) { ctx := testDexpreoptWithApexes(t, bp, "", transform) checkBootDexJarPath(t, ctx, ".intermediates/libfoo/android_common_apex10000/hiddenapi/libfoo.jar") + + // Make sure that the dex file from the prebuilt_apex contributes to the hiddenapi index file. + checkHiddenAPIIndexInputs(t, ctx, ` +.intermediates/libfoo/android_common_apex10000/hiddenapi/index.csv +`) }) t.Run("prebuilt preferred with source apex disabled", func(t *testing.T) { @@ -4678,6 +4697,10 @@ func TestBootDexJarsFromSourcesAndPrebuilts(t *testing.T) { ctx := testDexpreoptWithApexes(t, bp, "", transform) checkBootDexJarPath(t, ctx, ".intermediates/myapex.deapexer/android_common/deapexer/javalib/libfoo.jar") + + // Make sure that the dex file from the prebuilt_apex does NOT contribute to the hiddenapi index + // file. + checkHiddenAPIIndexInputs(t, ctx, ``) }) } @@ -6317,7 +6340,7 @@ func testDexpreoptWithApexes(t *testing.T, bp, errmsg string, transformDexpreopt dexpreopt.SetTestGlobalConfig(config, dexpreoptConfig) // Make sure that any changes to these dexpreopt properties are mirrored in the corresponding - // product variables that are used by hiddenapi. + // product variables. config.TestProductVariables.BootJars = dexpreoptConfig.BootJars config.TestProductVariables.UpdatableBootJars = dexpreoptConfig.UpdatableBootJars diff --git a/java/hiddenapi.go b/java/hiddenapi.go index 7e34c8381..66d9ca050 100644 --- a/java/hiddenapi.go +++ b/java/hiddenapi.go @@ -123,19 +123,21 @@ func (h *hiddenAPI) initHiddenAPI(ctx android.BaseModuleContext, name string) { h.annotationsOnly = strings.HasSuffix(name, "-hiddenapi") } -// hiddenAPI is called by any module that could contribute to the hiddenapi processing. +// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi +// processing. // // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot // jar list. // // Otherwise, it generates ninja rules to do the following: -// 1. Generates CSV files needed for hiddenapi processing. +// 1. Extract information needed for hiddenapi processing from the module and output it into CSV +// files. // 2. Conditionally adds the supplied dex file to the list of files used to generate the // hiddenAPISingletonPathsStruct.stubsFlag file. // 3. Conditionally creates a copy of the supplied dex file into which it has encoded the hiddenapi // flags and returns this instead of the supplied dex jar, otherwise simply returns the supplied // dex jar. -func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath, +func (h *hiddenAPI) hiddenAPIExtractAndEncode(ctx android.ModuleContext, name string, primary bool, dexJar android.OutputPath, implementationJar android.Path, uncompressDex bool) android.OutputPath { if !h.active { @@ -146,13 +148,7 @@ func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bo // used as a source of information for hidden API processing otherwise it will result in // duplicate entries in the files. if primary { - // Create ninja rules to generate various CSV files needed by hiddenapi and store the paths - // in the hiddenAPI structure. - h.hiddenAPIGenerateCSV(ctx, implementationJar) - - // Save the unencoded dex jar so it can be used when generating the - // hiddenAPISingletonPathsStruct.stubFlags file. - h.bootDexJarPath = dexJar + h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar) } if !h.annotationsOnly { @@ -168,7 +164,12 @@ func (h *hiddenAPI) hiddenAPI(ctx android.ModuleContext, name string, primary bo return dexJar } -func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar android.Path) { +// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes +// jar, and outputs it to the appropriate module specific CSV file. +// +// It also makes the dex jar available for use when generating the +// hiddenAPISingletonPathsStruct.stubFlags. +func (h *hiddenAPI) hiddenAPIExtractInformation(ctx android.ModuleContext, dexJar, classesJar android.Path) { stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv") @@ -207,6 +208,10 @@ func (h *hiddenAPI) hiddenAPIGenerateCSV(ctx android.ModuleContext, classesJar a FlagWithOutput("--output=", indexCSV) rule.Build("merged-hiddenapi-index", "Merged Hidden API index") h.indexCSVPath = indexCSV + + // Save the unencoded dex jar so it can be used when generating the + // hiddenAPISingletonPathsStruct.stubFlags file. + h.bootDexJarPath = dexJar } var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", blueprint.RuleParams{ diff --git a/java/hiddenapi_singleton_test.go b/java/hiddenapi_singleton_test.go index fcaa94adc..77cfff45c 100644 --- a/java/hiddenapi_singleton_test.go +++ b/java/hiddenapi_singleton_test.go @@ -76,14 +76,6 @@ func TestHiddenAPISingleton(t *testing.T) { } } -func checkRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) { - actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n")) - expected = strings.TrimSpace(expected) - if actual != expected { - t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual) - } -} - func TestHiddenAPIIndexSingleton(t *testing.T) { ctx, _ := testHiddenAPIBootJars(t, ` java_library { @@ -108,7 +100,7 @@ func TestHiddenAPIIndexSingleton(t *testing.T) { hiddenAPIIndex := ctx.SingletonForTests("hiddenapi_index") indexRule := hiddenAPIIndex.Rule("singleton-merged-hiddenapi-index") - checkRuleInputs(t, ` + CheckHiddenAPIRuleInputs(t, ` .intermediates/bar/android_common/hiddenapi/index.csv .intermediates/foo/android_common/hiddenapi/index.csv `, diff --git a/java/java.go b/java/java.go index cee14cc4b..dfbcd6f54 100644 --- a/java/java.go +++ b/java/java.go @@ -1805,7 +1805,7 @@ func (j *Module) compile(ctx android.ModuleContext, aaptSrcJar android.Path) { primary = primary && !j.IsReplacedByPrebuilt() // Hidden API CSV generation and dex encoding - dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile, + dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, j.implementationJarFile, proptools.Bool(j.dexProperties.Uncompress_dex)) // merge dex jar with resources if necessary @@ -2965,7 +2965,7 @@ func (j *Import) GenerateAndroidBuildActions(ctx android.ModuleContext) { primary := j.Prebuilt().UsePrebuilt() // Hidden API CSV generation and dex encoding - dexOutputFile = j.hiddenAPI.hiddenAPI(ctx, configurationName, primary, dexOutputFile, outputFile, + dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, configurationName, primary, dexOutputFile, outputFile, proptools.Bool(j.dexProperties.Uncompress_dex)) j.dexJarFile = dexOutputFile diff --git a/java/testing.go b/java/testing.go index 5fcf84c6b..781106ff2 100644 --- a/java/testing.go +++ b/java/testing.go @@ -18,6 +18,7 @@ import ( "fmt" "reflect" "sort" + "strings" "testing" "android/soong/android" @@ -237,3 +238,11 @@ func CheckModuleDependencies(t *testing.T, ctx *android.TestContext, name, varia t.Errorf("expected %#q, found %#q", expected, actual) } } + +func CheckHiddenAPIRuleInputs(t *testing.T, expected string, hiddenAPIRule android.TestingBuildParams) { + actual := strings.TrimSpace(strings.Join(android.NormalizePathsForTesting(hiddenAPIRule.Implicits), "\n")) + expected = strings.TrimSpace(expected) + if actual != expected { + t.Errorf("Expected hiddenapi rule inputs:\n%s\nactual inputs:\n%s", expected, actual) + } +}