Merge changes from topic "merge_zips_strip"

am: 6d6faa1a1f

Change-Id: I5f8d1c2aaff59a2af9156d71532fbe9da7181a4b
This commit is contained in:
Colin Cross
2018-07-16 17:19:09 -07:00
committed by android-build-merger
5 changed files with 72 additions and 20 deletions

View File

@@ -16,6 +16,7 @@ blueprint_go_binary {
name: "merge_zips", name: "merge_zips",
deps: [ deps: [
"android-archive-zip", "android-archive-zip",
"blueprint-pathtools",
"soong-jar", "soong-jar",
], ],
srcs: [ srcs: [

View File

@@ -24,7 +24,8 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"sort" "sort"
"strings"
"github.com/google/blueprint/pathtools"
"android/soong/jar" "android/soong/jar"
"android/soong/third_party/zip" "android/soong/third_party/zip"
@@ -69,8 +70,8 @@ var (
) )
func init() { func init() {
flag.Var(&stripDirs, "stripDir", "the prefix of file path to be excluded from the output zip") flag.Var(&stripDirs, "stripDir", "directories to be excluded from the output zip, accepts wildcards")
flag.Var(&stripFiles, "stripFile", "filenames to be excluded from the output zip, accepts wildcards") flag.Var(&stripFiles, "stripFile", "files to be excluded from the output zip, accepts wildcards")
flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping") flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping")
} }
@@ -339,9 +340,13 @@ func mergeZips(readers []namedZipReader, writer *zip.Writer, manifest, entrypoin
for _, namedReader := range readers { for _, namedReader := range readers {
_, skipStripThisZip := zipsToNotStrip[namedReader.path] _, skipStripThisZip := zipsToNotStrip[namedReader.path]
for _, file := range namedReader.reader.File { for _, file := range namedReader.reader.File {
if !skipStripThisZip && shouldStripFile(emulateJar, stripFiles, stripDirs, file.Name) { if !skipStripThisZip {
if skip, err := shouldStripEntry(emulateJar, stripFiles, stripDirs, file.Name); err != nil {
return err
} else if skip {
continue continue
} }
}
if stripDirEntries && file.FileInfo().IsDir() { if stripDirEntries && file.FileInfo().IsDir() {
continue continue
@@ -420,26 +425,41 @@ func pathBeforeLastSlash(path string) string {
return ret return ret
} }
func shouldStripFile(emulateJar bool, stripFiles, stripDirs []string, name string) bool { func shouldStripEntry(emulateJar bool, stripFiles, stripDirs []string, name string) (bool, error) {
for _, dir := range stripDirs { for _, dir := range stripDirs {
if strings.HasPrefix(name, dir+"/") { dir = filepath.Clean(dir)
if emulateJar { patterns := []string{
if name != jar.MetaDir && name != jar.ManifestFile { dir + "/", // the directory itself
return true dir + "/**/*", // files recursively in the directory
dir + "/**/*/", // directories recursively in the directory
} }
} else {
return true for _, pattern := range patterns {
} match, err := pathtools.Match(pattern, name)
} if err != nil {
} return false, fmt.Errorf("%s: %s", err.Error(), pattern)
for _, pattern := range stripFiles {
if match, err := filepath.Match(pattern, filepath.Base(name)); err != nil {
panic(fmt.Errorf("%s: %s", err.Error(), pattern))
} else if match { } else if match {
return true if emulateJar {
// When merging jar files, don't strip META-INF/MANIFEST.MF even if stripping META-INF is
// requested.
// TODO(ccross): which files does this affect?
if name != jar.MetaDir && name != jar.ManifestFile {
return true, nil
} }
} }
return false return true, nil
}
}
}
for _, pattern := range stripFiles {
if match, err := pathtools.Match(pattern, name); err != nil {
return false, fmt.Errorf("%s: %s", err.Error(), pattern)
} else if match {
return true, nil
}
}
return false, nil
} }
func jarSort(files []fileMapping) { func jarSort(files []fileMapping) {

View File

@@ -135,13 +135,33 @@ func TestMergeZips(t *testing.T) {
stripDirEntries: true, stripDirEntries: true,
}, },
{ {
name: "strip files",
in: [][]testZipEntry{
{a, bDir, bbDir, bbb, bc, bd, be},
},
out: []testZipEntry{a, bDir, bbDir, bbb, bc},
stripFiles: []string{"b/d", "b/e"},
},
{
// merge_zips used to treat -stripFile a as stripping any file named a, it now only strips a in the
// root of the zip.
name: "strip file name", name: "strip file name",
in: [][]testZipEntry{ in: [][]testZipEntry{
{a, bDir, ba}, {a, bDir, ba},
}, },
out: []testZipEntry{bDir, ba},
stripFiles: []string{"a"},
},
{
name: "strip files glob",
in: [][]testZipEntry{
{a, bDir, ba},
},
out: []testZipEntry{bDir}, out: []testZipEntry{bDir},
stripFiles: []string{"a"}, stripFiles: []string{"**/a"},
}, },
{ {
name: "strip dirs", name: "strip dirs",
@@ -152,6 +172,15 @@ func TestMergeZips(t *testing.T) {
stripDirs: []string{"b"}, stripDirs: []string{"b"},
}, },
{
name: "strip dirs glob",
in: [][]testZipEntry{
{a, bDir, bbDir, bbb, bc, bd, be},
},
out: []testZipEntry{a, bDir, bc, bd, be},
stripDirs: []string{"b/*"},
},
{ {
name: "zips to not strip", name: "zips to not strip",
in: [][]testZipEntry{ in: [][]testZipEntry{

View File

@@ -27,7 +27,7 @@ var d8 = pctx.AndroidStaticRule("d8",
Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` + Command: `rm -rf "$outDir" && mkdir -p "$outDir" && ` +
`${config.D8Cmd} --output $outDir $dxFlags $in && ` + `${config.D8Cmd} --output $outDir $dxFlags $in && ` +
`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
`${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`, `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
CommandDeps: []string{ CommandDeps: []string{
"${config.D8Cmd}", "${config.D8Cmd}",
"${config.SoongZipCmd}", "${config.SoongZipCmd}",
@@ -44,7 +44,7 @@ var r8 = pctx.AndroidStaticRule("r8",
`-printmapping $outDict ` + `-printmapping $outDict ` +
`$dxFlags $r8Flags && ` + `$dxFlags $r8Flags && ` +
`${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` + `${config.SoongZipCmd} -o $outDir/classes.dex.jar -C $outDir -D $outDir && ` +
`${config.MergeZipsCmd} -D -stripFile "*.class" $out $outDir/classes.dex.jar $in`, `${config.MergeZipsCmd} -D -stripFile "**/*.class" $out $outDir/classes.dex.jar $in`,
CommandDeps: []string{ CommandDeps: []string{
"${config.R8Cmd}", "${config.R8Cmd}",
"${config.SoongZipCmd}", "${config.SoongZipCmd}",

View File

@@ -1032,7 +1032,9 @@ func (j *Module) compile(ctx android.ModuleContext, extraSrcJars ...android.Path
if Bool(j.properties.Renamed_kotlin_stdlib) { if Bool(j.properties.Renamed_kotlin_stdlib) {
// Remove any kotlin-reflect related files // Remove any kotlin-reflect related files
// TODO(pszczepaniak): Support kotlin-reflect // TODO(pszczepaniak): Support kotlin-reflect
stripFiles = append(stripFiles, "*.kotlin_module", "*.kotlin_builtin") stripFiles = append(stripFiles,
"**/*.kotlin_module",
"**/*.kotlin_builtin")
} else { } else {
// Only add kotlin-stdlib if not using (on-device) renamed stdlib // Only add kotlin-stdlib if not using (on-device) renamed stdlib
// (it's expected to be on device bootclasspath) // (it's expected to be on device bootclasspath)