Refactor .aar resource compilation

Instead of extracting the aar, and passing the res directory to another rule
(along with a dependency to a different known file), don't keep the
extracted res directory, and pass the aar directly to the resource
compliation rule, which will extract just the res directory.

I need this for my RBE experiments, where non-listed output files won't
exist in other rules.

Test: m
Change-Id: I99074381052cbcebb6a402484abae9ab2e40284f
This commit is contained in:
Dan Willemsen 2019-05-28 14:49:06 -07:00
parent ff3f43857f
commit 304cfec778
3 changed files with 35 additions and 38 deletions

View File

@ -30,6 +30,7 @@ var (
outputDir = flag.String("d", "", "output dir") outputDir = flag.String("d", "", "output dir")
outputFile = flag.String("l", "", "output list file") outputFile = flag.String("l", "", "output list file")
filter = flag.String("f", "", "optional filter pattern") filter = flag.String("f", "", "optional filter pattern")
zipPrefix = flag.String("zip-prefix", "", "optional prefix within the zip file to extract, stripping the prefix")
) )
func must(err error) { func must(err error) {
@ -77,6 +78,10 @@ func main() {
var files []string var files []string
seen := make(map[string]string) seen := make(map[string]string)
if *zipPrefix != "" {
*zipPrefix = filepath.Clean(*zipPrefix) + "/"
}
for _, input := range inputs { for _, input := range inputs {
reader, err := zip.OpenReader(input) reader, err := zip.OpenReader(input)
if err != nil { if err != nil {
@ -85,23 +90,30 @@ func main() {
defer reader.Close() defer reader.Close()
for _, f := range reader.File { for _, f := range reader.File {
name := f.Name
if *zipPrefix != "" {
if !strings.HasPrefix(name, *zipPrefix) {
continue
}
name = strings.TrimPrefix(name, *zipPrefix)
}
if *filter != "" { if *filter != "" {
if match, err := filepath.Match(*filter, filepath.Base(f.Name)); err != nil { if match, err := filepath.Match(*filter, filepath.Base(name)); err != nil {
log.Fatal(err) log.Fatal(err)
} else if !match { } else if !match {
continue continue
} }
} }
if filepath.IsAbs(f.Name) { if filepath.IsAbs(name) {
log.Fatalf("%q in %q is an absolute path", f.Name, input) log.Fatalf("%q in %q is an absolute path", name, input)
} }
if prev, exists := seen[f.Name]; exists { if prev, exists := seen[name]; exists {
log.Fatalf("%q found in both %q and %q", f.Name, prev, input) log.Fatalf("%q found in both %q and %q", name, prev, input)
} }
seen[f.Name] = input seen[name] = input
filename := filepath.Join(*outputDir, f.Name) filename := filepath.Join(*outputDir, name)
if f.FileInfo().IsDir() { if f.FileInfo().IsDir() {
must(os.MkdirAll(filename, f.FileInfo().Mode())) must(os.MkdirAll(filename, f.FileInfo().Mode()))
} else { } else {

View File

@ -94,32 +94,20 @@ func aapt2Compile(ctx android.ModuleContext, dir android.Path, paths android.Pat
return ret return ret
} }
func aapt2CompileDirs(ctx android.ModuleContext, flata android.WritablePath, dirs android.Paths, deps android.Paths) {
ctx.Build(pctx, android.BuildParams{
Rule: aapt2CompileRule,
Description: "aapt2 compile dirs",
Implicits: deps,
Output: flata,
Args: map[string]string{
"outDir": flata.String(),
// Always set --pseudo-localize, it will be stripped out later for release
// builds that don't want it.
"cFlags": "--pseudo-localize " + android.JoinWithPrefix(dirs.Strings(), "--dir "),
},
})
}
var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip", var aapt2CompileZipRule = pctx.AndroidStaticRule("aapt2CompileZip",
blueprint.RuleParams{ blueprint.RuleParams{
Command: `${config.ZipSyncCmd} -d $resZipDir $in && ` + Command: `${config.ZipSyncCmd} -d $resZipDir $zipSyncFlags $in && ` +
`${config.Aapt2Cmd} compile -o $out $cFlags --legacy --dir $resZipDir`, `${config.Aapt2Cmd} compile -o $out $cFlags --legacy --dir $resZipDir`,
CommandDeps: []string{ CommandDeps: []string{
"${config.Aapt2Cmd}", "${config.Aapt2Cmd}",
"${config.ZipSyncCmd}", "${config.ZipSyncCmd}",
}, },
}, "cFlags", "resZipDir") }, "cFlags", "resZipDir", "zipSyncFlags")
func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path) { func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip android.Path, zipPrefix string) {
if zipPrefix != "" {
zipPrefix = "--zip-prefix " + zipPrefix
}
ctx.Build(pctx, android.BuildParams{ ctx.Build(pctx, android.BuildParams{
Rule: aapt2CompileZipRule, Rule: aapt2CompileZipRule,
Description: "aapt2 compile zip", Description: "aapt2 compile zip",
@ -130,6 +118,7 @@ func aapt2CompileZip(ctx android.ModuleContext, flata android.WritablePath, zip
// builds that don't want it. // builds that don't want it.
"cFlags": "--pseudo-localize", "cFlags": "--pseudo-localize",
"resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(), "resZipDir": android.PathForModuleOut(ctx, "aapt2", "reszip", flata.Base()).String(),
"zipSyncFlags": zipPrefix,
}, },
}) })
} }

View File

@ -244,7 +244,7 @@ func (a *aapt) buildActions(ctx android.ModuleContext, sdkContext sdkContext, ex
for i, zip := range resZips { for i, zip := range resZips {
flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i)) flata := android.PathForModuleOut(ctx, fmt.Sprintf("reszip.%d.flata", i))
aapt2CompileZip(ctx, flata, zip) aapt2CompileZip(ctx, flata, zip, "")
compiledResDirs = append(compiledResDirs, android.Paths{flata}) compiledResDirs = append(compiledResDirs, android.Paths{flata})
} }
@ -555,13 +555,13 @@ func (a *AARImport) DepsMutator(ctx android.BottomUpMutatorContext) {
} }
// Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be // Unzip an AAR into its constituent files and directories. Any files in Outputs that don't exist in the AAR will be
// touched to create an empty file, and any directories in $expectedDirs will be created. // touched to create an empty file. The res directory is not extracted, as it will be extracted in its own rule.
var unzipAAR = pctx.AndroidStaticRule("unzipAAR", var unzipAAR = pctx.AndroidStaticRule("unzipAAR",
blueprint.RuleParams{ blueprint.RuleParams{
Command: `rm -rf $outDir && mkdir -p $outDir $expectedDirs && ` + Command: `rm -rf $outDir && mkdir -p $outDir && ` +
`unzip -qo -d $outDir $in && touch $out`, `unzip -qo -d $outDir $in && rm -rf $outDir/res && touch $out`,
}, },
"expectedDirs", "outDir") "outDir")
func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) { func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
if len(a.properties.Aars) != 1 { if len(a.properties.Aars) != 1 {
@ -579,7 +579,6 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
} }
extractedAARDir := android.PathForModuleOut(ctx, "aar") extractedAARDir := android.PathForModuleOut(ctx, "aar")
extractedResDir := extractedAARDir.Join(ctx, "res")
a.classpathFile = extractedAARDir.Join(ctx, "classes.jar") a.classpathFile = extractedAARDir.Join(ctx, "classes.jar")
a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt") a.proguardFlags = extractedAARDir.Join(ctx, "proguard.txt")
a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml") a.manifest = extractedAARDir.Join(ctx, "AndroidManifest.xml")
@ -590,16 +589,13 @@ func (a *AARImport) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest}, Outputs: android.WritablePaths{a.classpathFile, a.proguardFlags, a.manifest},
Description: "unzip AAR", Description: "unzip AAR",
Args: map[string]string{ Args: map[string]string{
"expectedDirs": extractedResDir.String(),
"outDir": extractedAARDir.String(), "outDir": extractedAARDir.String(),
}, },
}) })
compiledResDir := android.PathForModuleOut(ctx, "flat-res") compiledResDir := android.PathForModuleOut(ctx, "flat-res")
aaptCompileDeps := android.Paths{a.classpathFile}
aaptCompileDirs := android.Paths{extractedResDir}
flata := compiledResDir.Join(ctx, "gen_res.flata") flata := compiledResDir.Join(ctx, "gen_res.flata")
aapt2CompileDirs(ctx, flata, aaptCompileDirs, aaptCompileDeps) aapt2CompileZip(ctx, flata, aar, "res")
a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk") a.exportPackage = android.PathForModuleOut(ctx, "package-res.apk")
srcJar := android.PathForModuleGen(ctx, "R.jar") srcJar := android.PathForModuleGen(ctx, "R.jar")