Add -stripFile argument to merge_zips

Putting resources in dex jars is going to need to pull files from
classpath jars that do not match *.class.

Test: m -j checkbuild
Change-Id: Ia37b8b9387e5f5636769afc6937b9aa3c9ec1ced
diff --git a/cmd/merge_zips/merge_zips.go b/cmd/merge_zips/merge_zips.go
index a94392b..e16c00e 100644
--- a/cmd/merge_zips/merge_zips.go
+++ b/cmd/merge_zips/merge_zips.go
@@ -29,26 +29,26 @@
 	"android/soong/third_party/zip"
 )
 
-type stripDir struct{}
+type fileList []string
 
-func (s *stripDir) String() string {
+func (f *fileList) String() string {
 	return `""`
 }
 
-func (s *stripDir) Set(dir string) error {
-	stripDirs = append(stripDirs, filepath.Clean(dir))
+func (f *fileList) Set(name string) error {
+	*f = append(*f, filepath.Clean(name))
 
 	return nil
 }
 
-type zipToNotStrip struct{}
+type zipsToNotStripSet map[string]bool
 
-func (s *zipToNotStrip) String() string {
+func (s zipsToNotStripSet) String() string {
 	return `""`
 }
 
-func (s *zipToNotStrip) Set(zip_path string) error {
-	zipsToNotStrip[zip_path] = true
+func (s zipsToNotStripSet) Set(zip_path string) error {
+	s[zip_path] = true
 
 	return nil
 }
@@ -56,15 +56,17 @@
 var (
 	sortEntries     = flag.Bool("s", false, "sort entries (defaults to the order from the input zip files)")
 	emulateJar      = flag.Bool("j", false, "sort zip entries using jar ordering (META-INF first)")
-	stripDirs       []string
-	zipsToNotStrip  = make(map[string]bool)
+	stripDirs       fileList
+	stripFiles      fileList
+	zipsToNotStrip  = make(zipsToNotStripSet)
 	stripDirEntries = flag.Bool("D", false, "strip directory entries from the output zip file")
 	manifest        = flag.String("m", "", "manifest file to insert in jar")
 )
 
 func init() {
-	flag.Var(&stripDir{}, "stripDir", "the prefix of file path to be excluded from the output zip")
-	flag.Var(&zipToNotStrip{}, "zipToNotStrip", "the input zip file which is not applicable for stripping")
+	flag.Var(&stripDirs, "stripDir", "the prefix of file path to be excluded from the output zip")
+	flag.Var(&stripFiles, "stripFile", "filenames to be excluded from the output zip, accepts wildcards")
+	flag.Var(&zipsToNotStrip, "zipToNotStrip", "the input zip file which is not applicable for stripping")
 }
 
 func main() {
@@ -243,20 +245,9 @@
 
 	for _, namedReader := range readers {
 		_, skipStripThisZip := zipsToNotStrip[namedReader.path]
-	FileLoop:
 		for _, file := range namedReader.reader.File {
-			if !skipStripThisZip {
-				for _, dir := range stripDirs {
-					if strings.HasPrefix(file.Name, dir+"/") {
-						if emulateJar {
-							if file.Name != jar.MetaDir && file.Name != jar.ManifestFile {
-								continue FileLoop
-							}
-						} else {
-							continue FileLoop
-						}
-					}
-				}
+			if !skipStripThisZip && shouldStripFile(emulateJar, file.Name) {
+				continue
 			}
 
 			if stripDirEntries && file.FileInfo().IsDir() {
@@ -310,6 +301,28 @@
 	return nil
 }
 
+func shouldStripFile(emulateJar bool, name string) bool {
+	for _, dir := range stripDirs {
+		if strings.HasPrefix(name, dir+"/") {
+			if emulateJar {
+				if name != jar.MetaDir && name != jar.ManifestFile {
+					return true
+				}
+			} else {
+				return true
+			}
+		}
+	}
+	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 {
+			return true
+		}
+	}
+	return false
+}
+
 func jarSort(files []fileMapping) {
 	sort.SliceStable(files, func(i, j int) bool {
 		return jar.EntryNamesLess(files[i].dest, files[j].dest)