Merge "Stop generating unnecessary hidden API rules" am: b7c78731d4 am: 97961f4837

Original change: https://android-review.googlesource.com/c/platform/build/soong/+/1707574

Change-Id: Ida624f82ac4394cad434a943271ecc64203fce26
diff --git a/java/base.go b/java/base.go
index 2cc0e76..03652be 100644
--- a/java/base.go
+++ b/java/base.go
@@ -1223,9 +1223,11 @@
 				return
 			}
 
-			// Hidden API CSV generation and dex encoding
-			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, j.implementationJarFile,
-				proptools.Bool(j.dexProperties.Uncompress_dex))
+			// Update hidden API paths.
+			j.hiddenAPIUpdatePaths(ctx, dexOutputFile, j.implementationJarFile)
+
+			// Encode hidden API flags in dex file.
+			dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile, proptools.Bool(j.dexProperties.Uncompress_dex))
 
 			// merge dex jar with resources if necessary
 			if j.resourceJar != nil {
diff --git a/java/hiddenapi.go b/java/hiddenapi.go
index 2f73262..2c0655f 100644
--- a/java/hiddenapi.go
+++ b/java/hiddenapi.go
@@ -56,35 +56,6 @@
 	// this file so using the encoded dex jar here would result in a cycle in the ninja rules.
 	bootDexJarPath android.Path
 
-	// The path to the CSV file that contains mappings from Java signature to various flags derived
-	// from annotations in the source, e.g. whether it is public or the sdk version above which it
-	// can no longer be used.
-	//
-	// It is created by the Class2NonSdkList tool which processes the .class files in the class
-	// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
-	// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
-	// consistency checks on the information in the annotations and to filter out bridge methods
-	// that are already part of the public API.
-	flagsCSVPath android.Path
-
-	// The path to the CSV file that contains mappings from Java signature to the value of properties
-	// specified on UnsupportedAppUsage annotations in the source.
-	//
-	// Like the flagsCSVPath file this is also created by the Class2NonSdkList in the same way.
-	// Although the two files could potentially be created in a single invocation of the
-	// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
-	// being determined by the property that is used.
-	metadataCSVPath android.Path
-
-	// The path to the CSV file that contains mappings from Java signature to source location
-	// information.
-	//
-	// It is created by the merge_csv tool which processes the class implementation jar, extracting
-	// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
-	// created by the unsupported app usage annotation processor during compilation of the class
-	// implementation jar.
-	indexCSVPath android.Path
-
 	// The paths to the classes jars that contain classes and class members annotated with
 	// the UnsupportedAppUsage annotation that need to be extracted as part of the hidden API
 	// processing.
@@ -182,25 +153,14 @@
 	return active
 }
 
-// hiddenAPIExtractAndEncode is called by any module that could contribute to the hiddenapi
-// processing.
+// hiddenAPIEncodeDex is called by any module that needs to encode dex files.
 //
 // It ignores any module that has not had initHiddenApi() called on it and which is not in the boot
-// jar list.
+// jar list. In that case it simply returns the supplied dex jar path.
 //
-// Otherwise, it generates ninja rules to do the following:
-// 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) hiddenAPIExtractAndEncode(ctx android.ModuleContext, dexJar android.OutputPath,
-	implementationJar android.Path, uncompressDex bool) android.OutputPath {
-
-	// Call before checking if this is active as it will update the hiddenAPI structure.
-	h.hiddenAPIExtractInformation(ctx, dexJar, implementationJar)
+// Otherwise, it 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.
+func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android.OutputPath, uncompressDex bool) android.OutputPath {
 
 	if !h.active {
 		return dexJar
@@ -217,12 +177,12 @@
 	return dexJar
 }
 
-// hiddenAPIExtractInformation generates ninja rules to extract the information from the classes
+// hiddenAPIUpdatePaths 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) {
+func (h *hiddenAPI) hiddenAPIUpdatePaths(ctx android.ModuleContext, dexJar, classesJar android.Path) {
 
 	// Save the classes jars even if this is not active as they may be used by modular hidden API
 	// processing.
@@ -236,35 +196,20 @@
 	// Save the unencoded dex jar so it can be used when generating the
 	// hiddenAPISingletonPathsStruct.stubFlags file.
 	h.bootDexJarPath = dexJar
-
-	if !h.active {
-		return
-	}
-
-	// More than one library with the same classes may need to be encoded but only one should be
-	// used as a source of information for hidden API processing otherwise it will result in
-	// duplicate entries in the files.
-	if !h.primary {
-		return
-	}
-
-	stubFlagsCSV := hiddenAPISingletonPaths(ctx).stubFlags
-
-	flagsCSV := android.PathForModuleOut(ctx, "hiddenapi", "flags.csv")
-	h.flagsCSVPath = flagsCSV
-	buildRuleToGenerateAnnotationFlags(ctx, "hiddenapi flags", classesJars, stubFlagsCSV, flagsCSV)
-
-	metadataCSV := android.PathForModuleOut(ctx, "hiddenapi", "metadata.csv")
-	h.metadataCSVPath = metadataCSV
-	buildRuleToGenerateMetadata(ctx, "hiddenapi metadata", classesJars, stubFlagsCSV, metadataCSV)
-
-	indexCSV := android.PathForModuleOut(ctx, "hiddenapi", "index.csv")
-	h.indexCSVPath = indexCSV
-	buildRuleToGenerateIndex(ctx, "Merged Hidden API index", classesJars, indexCSV)
 }
 
 // buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
 // from the classes jars and stub-flags.csv files.
+//
+// The annotation-flags.csv file contains mappings from Java signature to various flags derived from
+// annotations in the source, e.g. whether it is public or the sdk version above which it can no
+// longer be used.
+//
+// It is created by the Class2NonSdkList tool which processes the .class files in the class
+// implementation jar looking for UnsupportedAppUsage and CovariantReturnType annotations. The
+// tool also consumes the hiddenAPISingletonPathsStruct.stubFlags file in order to perform
+// consistency checks on the information in the annotations and to filter out bridge methods
+// that are already part of the public API.
 func buildRuleToGenerateAnnotationFlags(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, outputPath android.WritablePath) {
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        hiddenAPIGenerateCSVRule,
@@ -281,6 +226,14 @@
 
 // buildRuleToGenerateMetadata builds a ninja rule to generate the metadata.csv file from
 // the classes jars and stub-flags.csv files.
+//
+// The metadata.csv file contains mappings from Java signature to the value of properties specified
+// on UnsupportedAppUsage annotations in the source.
+//
+// Like the annotation-flags.csv file this is also created by the Class2NonSdkList in the same way.
+// Although the two files could potentially be created in a single invocation of the
+// Class2NonSdkList at the moment they are created using their own invocation, with the behavior
+// being determined by the property that is used.
 func buildRuleToGenerateMetadata(ctx android.ModuleContext, desc string, classesJars android.Paths, stubFlagsCSV android.Path, metadataCSV android.WritablePath) {
 	ctx.Build(pctx, android.BuildParams{
 		Rule:        hiddenAPIGenerateCSVRule,
@@ -295,8 +248,15 @@
 	})
 }
 
-// buildRuleToGenerateMetadata builds a ninja rule to generate the index.csv file from the classes
+// buildRuleToGenerateIndex builds a ninja rule to generate the index.csv file from the classes
 // jars.
+//
+// The index.csv file contains mappings from Java signature to source location information.
+//
+// It is created by the merge_csv tool which processes the class implementation jar, extracting
+// all the files ending in .uau (which are CSV files) and merges them together. The .uau files are
+// created by the unsupported app usage annotation processor during compilation of the class
+// implementation jar.
 func buildRuleToGenerateIndex(ctx android.ModuleContext, desc string, classesJars android.Paths, indexCSV android.WritablePath) {
 	rule := android.NewRuleBuilder(pctx, ctx)
 	rule.Command().
diff --git a/java/java.go b/java/java.go
index 9a5fbfc..f85de3d 100644
--- a/java/java.go
+++ b/java/java.go
@@ -1315,7 +1315,7 @@
 			di := ctx.OtherModuleProvider(deapexerModule, android.DeapexerProvider).(android.DeapexerInfo)
 			if dexOutputPath := di.PrebuiltExportPath(j.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
 				j.dexJarFile = dexOutputPath
-				j.hiddenAPIExtractInformation(ctx, dexOutputPath, outputFile)
+				j.hiddenAPIUpdatePaths(ctx, dexOutputPath, outputFile)
 			} else {
 				// This should never happen as a variant for a prebuilt_apex is only created if the
 				// prebuilt_apex has been configured to export the java library dex file.
@@ -1346,9 +1346,11 @@
 				return
 			}
 
-			// Hidden API CSV generation and dex encoding
-			dexOutputFile = j.hiddenAPIExtractAndEncode(ctx, dexOutputFile, outputFile,
-				proptools.Bool(j.dexProperties.Uncompress_dex))
+			// Update hidden API paths.
+			j.hiddenAPIUpdatePaths(ctx, dexOutputFile, outputFile)
+
+			// Encode hidden API flags in dex file.
+			dexOutputFile = j.hiddenAPIEncodeDex(ctx, dexOutputFile, proptools.Bool(j.dexProperties.Uncompress_dex))
 
 			j.dexJarFile = dexOutputFile
 		}
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 6153615..f04f837 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -2128,7 +2128,7 @@
 			if dexOutputPath := di.PrebuiltExportPath(module.BaseModuleName(), ".dexjar"); dexOutputPath != nil {
 				module.dexJarFile = dexOutputPath
 				module.initHiddenAPI(ctx, module.configurationName)
-				module.hiddenAPIExtractInformation(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0])
+				module.hiddenAPIUpdatePaths(ctx, dexOutputPath, module.findScopePaths(apiScopePublic).stubsImplPath[0])
 			} else {
 				// This should never happen as a variant for a prebuilt_apex is only created if the
 				// prebuilt_apex has been configured to export the java library dex file.