Refactor hiddenAPIEncodeDex for use by bootclasspath_fragment

Previously, if the method was called multiple times by the same module
the resulting build rules would all use the same temporary directory
which meant that if run in parallel they would conflict with each
other. This change fixes that by providing a jar specific temporary
directory so it can be used by bootclasspath_fragment to encode its
content modules.

Also, cleans up, simplifies and improves the documentation.

Bug: 179354495
Test: m com.android.art com.android.ipsec com.android.os.statsd com.android.conscrypt
      - verify that this does not change the contents of the apex files
Merged-In: I0ebe61abc8e16111c6e8a822eb96c57846b98461
Change-Id: I0ebe61abc8e16111c6e8a822eb96c57846b98461
(cherry picked from commit 0916595b1c9bf71224b98a6a2fdee941690446f6)
This commit is contained in:
Paul Duffin 2021-05-16 23:32:52 +01:00
parent 5ff8e3f567
commit ed587c367b
1 changed files with 26 additions and 24 deletions

View File

@ -145,15 +145,13 @@ func (h *hiddenAPI) hiddenAPIEncodeDex(ctx android.ModuleContext, dexJar android
} }
uncompressDex := *h.uncompressDexState uncompressDex := *h.uncompressDexState
hiddenAPIJar := android.PathForModuleOut(ctx, "hiddenapi", dexJar.Base()).OutputPath
// Create a copy of the dex jar which has been encoded with hiddenapi flags. // Create a copy of the dex jar which has been encoded with hiddenapi flags.
hiddenAPIEncodeDex(ctx, hiddenAPIJar, dexJar, uncompressDex) flagsCSV := hiddenAPISingletonPaths(ctx).flags
outputDir := android.PathForModuleOut(ctx, "hiddenapi").OutputPath
encodedDex := hiddenAPIEncodeDex(ctx, dexJar, flagsCSV, uncompressDex, outputDir)
// Use the encoded dex jar from here onwards. // Use the encoded dex jar from here onwards.
dexJar = hiddenAPIJar return encodedDex
return dexJar
} }
// buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file // buildRuleToGenerateAnnotationFlags builds a ninja rule to generate the annotation-flags.csv file
@ -243,35 +241,37 @@ var hiddenAPIEncodeDexRule = pctx.AndroidStaticRule("hiddenAPIEncodeDex", bluepr
}, },
}, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags") }, "flagsCsv", "hiddenapiFlags", "tmpDir", "soongZipFlags")
func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath, dexInput android.Path, // hiddenAPIEncodeDex generates the build rule that will encode the supplied dex jar and place the
uncompressDex bool) { // encoded dex jar in a file of the same name in the output directory.
//
// The encode dex rule requires unzipping, encoding and rezipping the classes.dex files along with
// all the resources from the input jar. It also ensures that if it was uncompressed in the input
// it stays uncompressed in the output.
func hiddenAPIEncodeDex(ctx android.ModuleContext, dexInput, flagsCSV android.Path, uncompressDex bool, outputDir android.OutputPath) android.OutputPath {
flagsCSV := hiddenAPISingletonPaths(ctx).flags // The output file has the same name as the input file and is in the output directory.
output := outputDir.Join(ctx, dexInput.Base())
// The encode dex rule requires unzipping and rezipping the classes.dex files, ensure that if it was uncompressed // Create a jar specific temporary directory in which to do the work just in case this is called
// in the input it stays uncompressed in the output. // with the same output directory for multiple modules.
tmpDir := outputDir.Join(ctx, dexInput.Base()+"-tmp")
// If the input is uncompressed then generate the output of the encode rule to an intermediate
// file as the final output will need further processing after encoding.
soongZipFlags := "" soongZipFlags := ""
hiddenapiFlags := "" encodeRuleOutput := output
tmpOutput := output
tmpDir := android.PathForModuleOut(ctx, "hiddenapi", "dex")
if uncompressDex { if uncompressDex {
soongZipFlags = "-L 0" soongZipFlags = "-L 0"
tmpOutput = android.PathForModuleOut(ctx, "hiddenapi", "unaligned", "unaligned.jar") encodeRuleOutput = outputDir.Join(ctx, "unaligned", dexInput.Base())
tmpDir = android.PathForModuleOut(ctx, "hiddenapi", "unaligned")
} }
enforceHiddenApiFlagsToAllMembers := true
// b/149353192: when a module is instrumented, jacoco adds synthetic members // b/149353192: when a module is instrumented, jacoco adds synthetic members
// $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags, // $jacocoData and $jacocoInit. Since they don't exist when building the hidden API flags,
// don't complain when we don't find hidden API flags for the synthetic members. // don't complain when we don't find hidden API flags for the synthetic members.
hiddenapiFlags := ""
if j, ok := ctx.Module().(interface { if j, ok := ctx.Module().(interface {
shouldInstrument(android.BaseModuleContext) bool shouldInstrument(android.BaseModuleContext) bool
}); ok && j.shouldInstrument(ctx) { }); ok && j.shouldInstrument(ctx) {
enforceHiddenApiFlagsToAllMembers = false
}
if !enforceHiddenApiFlagsToAllMembers {
hiddenapiFlags = "--no-force-assign-all" hiddenapiFlags = "--no-force-assign-all"
} }
@ -279,7 +279,7 @@ func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath,
Rule: hiddenAPIEncodeDexRule, Rule: hiddenAPIEncodeDexRule,
Description: "hiddenapi encode dex", Description: "hiddenapi encode dex",
Input: dexInput, Input: dexInput,
Output: tmpOutput, Output: encodeRuleOutput,
Implicit: flagsCSV, Implicit: flagsCSV,
Args: map[string]string{ Args: map[string]string{
"flagsCsv": flagsCSV.String(), "flagsCsv": flagsCSV.String(),
@ -290,8 +290,10 @@ func hiddenAPIEncodeDex(ctx android.ModuleContext, output android.WritablePath,
}) })
if uncompressDex { if uncompressDex {
TransformZipAlign(ctx, output, tmpOutput) TransformZipAlign(ctx, output, encodeRuleOutput)
} }
return output
} }
type hiddenApiAnnotationsDependencyTag struct { type hiddenApiAnnotationsDependencyTag struct {