soong_zip: add support for -j to junk paths

When -j is specified ignore the path to the source file and just
zip it with its filename.  -j overrides -C, and -C overrides -j,
so -j -f path1 -C dir -f path2 will junk the path for path1, but
treat path2 as relative to dir.

Remove the filepath.Clean for the FileArgs, it would convert ""
to "." sometimes, and everything gets cleaned in zip already for
the non-command-line use cases.

Test: m checkbuild
Change-Id: I7d90572c622ee0c6f35967ff31d067b5312c72eb
This commit is contained in:
Colin Cross 2018-09-18 16:51:43 -07:00
parent df3f4c81f3
commit b7c6911dd1
2 changed files with 59 additions and 23 deletions

View File

@ -21,8 +21,8 @@ import (
"io" "io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strconv"
"strings" "strings"
"android/soong/zip" "android/soong/zip"
@ -65,13 +65,14 @@ func (f *file) String() string {
} }
func (f *file) Set(s string) error { func (f *file) Set(s string) error {
if *relativeRoot == "" { if relativeRoot == "" && !junkPaths {
return fmt.Errorf("must pass -C before -f") return fmt.Errorf("must pass -C or -j before -f")
} }
fArgs = append(fArgs, zip.FileArg{ fArgs = append(fArgs, zip.FileArg{
PathPrefixInZip: filepath.Clean(*rootPrefix), PathPrefixInZip: *rootPrefix,
SourcePrefixToStrip: filepath.Clean(*relativeRoot), SourcePrefixToStrip: relativeRoot,
JunkPaths: junkPaths,
SourceFiles: []string{s}, SourceFiles: []string{s},
}) })
@ -83,8 +84,8 @@ func (l *listFiles) String() string {
} }
func (l *listFiles) Set(s string) error { func (l *listFiles) Set(s string) error {
if *relativeRoot == "" { if relativeRoot == "" && !junkPaths {
return fmt.Errorf("must pass -C before -l") return fmt.Errorf("must pass -C or -j before -l")
} }
list, err := ioutil.ReadFile(s) list, err := ioutil.ReadFile(s)
@ -93,8 +94,9 @@ func (l *listFiles) Set(s string) error {
} }
fArgs = append(fArgs, zip.FileArg{ fArgs = append(fArgs, zip.FileArg{
PathPrefixInZip: filepath.Clean(*rootPrefix), PathPrefixInZip: *rootPrefix,
SourcePrefixToStrip: filepath.Clean(*relativeRoot), SourcePrefixToStrip: relativeRoot,
JunkPaths: junkPaths,
SourceFiles: strings.Split(string(list), "\n"), SourceFiles: strings.Split(string(list), "\n"),
}) })
@ -106,21 +108,47 @@ func (d *dir) String() string {
} }
func (d *dir) Set(s string) error { func (d *dir) Set(s string) error {
if *relativeRoot == "" { if relativeRoot == "" && !junkPaths {
return fmt.Errorf("must pass -C before -D") return fmt.Errorf("must pass -C or -j before -D")
} }
fArgs = append(fArgs, zip.FileArg{ fArgs = append(fArgs, zip.FileArg{
PathPrefixInZip: filepath.Clean(*rootPrefix), PathPrefixInZip: *rootPrefix,
SourcePrefixToStrip: filepath.Clean(*relativeRoot), SourcePrefixToStrip: relativeRoot,
GlobDir: filepath.Clean(s), JunkPaths: junkPaths,
GlobDir: s,
}) })
return nil return nil
} }
type relativeRootImpl struct{}
func (*relativeRootImpl) String() string { return relativeRoot }
func (*relativeRootImpl) Set(s string) error {
relativeRoot = s
junkPaths = false
return nil
}
type junkPathsImpl struct{}
func (*junkPathsImpl) IsBoolFlag() bool { return true }
func (*junkPathsImpl) String() string { return relativeRoot }
func (*junkPathsImpl) Set(s string) error {
var err error
junkPaths, err = strconv.ParseBool(s)
relativeRoot = ""
return err
}
var ( var (
rootPrefix, relativeRoot *string rootPrefix *string
relativeRoot string
junkPaths bool
fArgs zip.FileArgs fArgs zip.FileArgs
nonDeflatedFiles = make(uniqueSet) nonDeflatedFiles = make(uniqueSet)
@ -154,7 +182,6 @@ func main() {
manifest := flags.String("m", "", "input jar manifest file name") manifest := flags.String("m", "", "input jar manifest file name")
directories := flags.Bool("d", false, "include directories in zip") directories := flags.Bool("d", false, "include directories in zip")
rootPrefix = flags.String("P", "", "path prefix within the zip at which to place files") rootPrefix = flags.String("P", "", "path prefix within the zip at which to place files")
relativeRoot = flags.String("C", "", "path to use as relative root of files in following -f, -l, or -D arguments")
compLevel := flags.Int("L", 5, "deflate compression level (0-9)") compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'") emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed") writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
@ -167,6 +194,8 @@ func main() {
flags.Var(&dir{}, "D", "directory to include in zip") flags.Var(&dir{}, "D", "directory to include in zip")
flags.Var(&file{}, "f", "file to include in zip") flags.Var(&file{}, "f", "file to include in zip")
flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression") flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
flags.Var(&relativeRootImpl{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
flags.Var(&junkPathsImpl{}, "j", "junk paths, zip files without directory names")
flags.Parse(expandedArgs[1:]) flags.Parse(expandedArgs[1:])

View File

@ -87,6 +87,7 @@ func (u *uniqueSet) Set(s string) error {
type FileArg struct { type FileArg struct {
PathPrefixInZip, SourcePrefixToStrip string PathPrefixInZip, SourcePrefixToStrip string
SourceFiles []string SourceFiles []string
JunkPaths bool
GlobDir string GlobDir string
} }
@ -228,8 +229,7 @@ func Run(args ZipArgs) (err error) {
srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...) srcs = append(srcs, recursiveGlobFiles(fa.GlobDir)...)
} }
for _, src := range srcs { for _, src := range srcs {
err := fillPathPairs(fa.PathPrefixInZip, fa.SourcePrefixToStrip, src, err := fillPathPairs(fa, src, &pathMappings, args.NonDeflatedFiles, noCompression)
&pathMappings, args.NonDeflatedFiles, noCompression)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -270,7 +270,7 @@ func Run(args ZipArgs) (err error) {
return nil return nil
} }
func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping, func fillPathPairs(fa FileArg, src string, pathMappings *[]pathMapping,
nonDeflatedFiles map[string]bool, noCompression bool) error { nonDeflatedFiles map[string]bool, noCompression bool) error {
src = strings.TrimSpace(src) src = strings.TrimSpace(src)
@ -278,11 +278,18 @@ func fillPathPairs(prefix, rel, src string, pathMappings *[]pathMapping,
return nil return nil
} }
src = filepath.Clean(src) src = filepath.Clean(src)
dest, err := filepath.Rel(rel, src) var dest string
if err != nil {
return err if fa.JunkPaths {
dest = filepath.Base(src)
} else {
var err error
dest, err = filepath.Rel(fa.SourcePrefixToStrip, src)
if err != nil {
return err
}
} }
dest = filepath.Join(prefix, dest) dest = filepath.Join(fa.PathPrefixInZip, dest)
zipMethod := zip.Deflate zipMethod := zip.Deflate
if _, found := nonDeflatedFiles[dest]; found || noCompression { if _, found := nonDeflatedFiles[dest]; found || noCompression {