2017-10-12 21:19:14 +02:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
2018-09-19 01:51:43 +02:00
|
|
|
"strconv"
|
2017-10-12 21:19:14 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"android/soong/zip"
|
|
|
|
)
|
|
|
|
|
|
|
|
type byteReaderCloser struct {
|
|
|
|
*bytes.Reader
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
type pathMapping struct {
|
|
|
|
dest, src string
|
|
|
|
zipMethod uint16
|
|
|
|
}
|
|
|
|
|
|
|
|
type uniqueSet map[string]bool
|
|
|
|
|
|
|
|
func (u *uniqueSet) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *uniqueSet) Set(s string) error {
|
|
|
|
if _, found := (*u)[s]; found {
|
|
|
|
return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
|
|
|
|
} else {
|
|
|
|
(*u)[s] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type file struct{}
|
|
|
|
|
|
|
|
type listFiles struct{}
|
|
|
|
|
|
|
|
type dir struct{}
|
|
|
|
|
|
|
|
func (f *file) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *file) Set(s string) error {
|
2018-09-19 01:51:43 +02:00
|
|
|
if relativeRoot == "" && !junkPaths {
|
|
|
|
return fmt.Errorf("must pass -C or -j before -f")
|
2017-10-12 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fArgs = append(fArgs, zip.FileArg{
|
2018-09-19 01:51:43 +02:00
|
|
|
PathPrefixInZip: *rootPrefix,
|
|
|
|
SourcePrefixToStrip: relativeRoot,
|
|
|
|
JunkPaths: junkPaths,
|
2017-10-12 21:19:14 +02:00
|
|
|
SourceFiles: []string{s},
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *listFiles) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *listFiles) Set(s string) error {
|
2018-09-19 01:51:43 +02:00
|
|
|
if relativeRoot == "" && !junkPaths {
|
|
|
|
return fmt.Errorf("must pass -C or -j before -l")
|
2017-10-12 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
list, err := ioutil.ReadFile(s)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fArgs = append(fArgs, zip.FileArg{
|
2018-09-19 01:51:43 +02:00
|
|
|
PathPrefixInZip: *rootPrefix,
|
|
|
|
SourcePrefixToStrip: relativeRoot,
|
|
|
|
JunkPaths: junkPaths,
|
2017-10-12 21:19:14 +02:00
|
|
|
SourceFiles: strings.Split(string(list), "\n"),
|
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) String() string {
|
|
|
|
return `""`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dir) Set(s string) error {
|
2018-09-19 01:51:43 +02:00
|
|
|
if relativeRoot == "" && !junkPaths {
|
|
|
|
return fmt.Errorf("must pass -C or -j before -D")
|
2017-10-12 21:19:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fArgs = append(fArgs, zip.FileArg{
|
2018-09-19 01:51:43 +02:00
|
|
|
PathPrefixInZip: *rootPrefix,
|
|
|
|
SourcePrefixToStrip: relativeRoot,
|
|
|
|
JunkPaths: junkPaths,
|
|
|
|
GlobDir: s,
|
2017-10-12 21:19:14 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-09-19 01:51:43 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-10-12 21:19:14 +02:00
|
|
|
var (
|
2018-09-19 01:51:43 +02:00
|
|
|
rootPrefix *string
|
|
|
|
relativeRoot string
|
|
|
|
junkPaths bool
|
2017-10-12 21:19:14 +02:00
|
|
|
|
|
|
|
fArgs zip.FileArgs
|
|
|
|
nonDeflatedFiles = make(uniqueSet)
|
|
|
|
)
|
|
|
|
|
|
|
|
func usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "usage: zip -o zipfile [-m manifest] -C dir [-f|-l file]...\n")
|
|
|
|
flag.PrintDefaults()
|
|
|
|
os.Exit(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
Add Respfile support for soong_zip.
Sometime the size of our command line passed to soong_zip go program
exceeds the cmdline size limit. So add an RespFile support with "@" special
character prefix.
The args in the cmdline will be considered together with the
args in RespFile during soong_zip running.
Test: real tests in my local machine, and compare the
res/libphonenumber.jar before and after changes.
./cmd -o test.zip '""'-C -> [./cmd,-o,test.zip,""-C]
./cmd -o test.zip '-C -f -> [./cmd,-o,test.zip,-C -f]
./cmd -o test.zip '\"'-C -f -> [./cmd,-o,test.zip,\"-C -f]
./cmd -o test.zip '\\'-C -f -> [./cmd,-o,test.zip,\\-C -f]
./cmd -o test.zip '\a'-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip \'-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip \\-C -> [./cmd,-o,test.zip,\-C]
./cmd -o test.zip \"-C -> [./cmd,-o,test.zip,"-C]
./cmd -o test.zip "'"-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip "\\"-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip "\""-C -f -> [./cmd,-o,test.zip,"a-C -f]
Bug: b/72484223
Change-Id: I83c3630b70c8396c8e8a3f266244d868d754c4e8
2018-01-27 03:30:36 +01:00
|
|
|
var expandedArgs []string
|
|
|
|
for _, arg := range os.Args {
|
|
|
|
if strings.HasPrefix(arg, "@") {
|
|
|
|
bytes, err := ioutil.ReadFile(strings.TrimPrefix(arg, "@"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
respArgs := zip.ReadRespFile(bytes)
|
|
|
|
expandedArgs = append(expandedArgs, respArgs...)
|
|
|
|
} else {
|
|
|
|
expandedArgs = append(expandedArgs, arg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := flag.NewFlagSet("flags", flag.ExitOnError)
|
|
|
|
|
|
|
|
out := flags.String("o", "", "file to write zip file to")
|
|
|
|
manifest := flags.String("m", "", "input jar manifest file name")
|
|
|
|
directories := flags.Bool("d", false, "include directories in zip")
|
|
|
|
rootPrefix = flags.String("P", "", "path prefix within the zip at which to place files")
|
|
|
|
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'")
|
|
|
|
writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
|
|
|
|
|
2018-09-19 01:31:09 +02:00
|
|
|
parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
|
Add Respfile support for soong_zip.
Sometime the size of our command line passed to soong_zip go program
exceeds the cmdline size limit. So add an RespFile support with "@" special
character prefix.
The args in the cmdline will be considered together with the
args in RespFile during soong_zip running.
Test: real tests in my local machine, and compare the
res/libphonenumber.jar before and after changes.
./cmd -o test.zip '""'-C -> [./cmd,-o,test.zip,""-C]
./cmd -o test.zip '-C -f -> [./cmd,-o,test.zip,-C -f]
./cmd -o test.zip '\"'-C -f -> [./cmd,-o,test.zip,\"-C -f]
./cmd -o test.zip '\\'-C -f -> [./cmd,-o,test.zip,\\-C -f]
./cmd -o test.zip '\a'-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip \'-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip \\-C -> [./cmd,-o,test.zip,\-C]
./cmd -o test.zip \"-C -> [./cmd,-o,test.zip,"-C]
./cmd -o test.zip "'"-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip "\\"-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip "\""-C -f -> [./cmd,-o,test.zip,"a-C -f]
Bug: b/72484223
Change-Id: I83c3630b70c8396c8e8a3f266244d868d754c4e8
2018-01-27 03:30:36 +01:00
|
|
|
cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
traceFile := flags.String("trace", "", "write trace to file")
|
|
|
|
|
|
|
|
flags.Var(&listFiles{}, "l", "file containing list of .class files")
|
|
|
|
flags.Var(&dir{}, "D", "directory 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")
|
2018-09-19 01:51:43 +02:00
|
|
|
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")
|
Add Respfile support for soong_zip.
Sometime the size of our command line passed to soong_zip go program
exceeds the cmdline size limit. So add an RespFile support with "@" special
character prefix.
The args in the cmdline will be considered together with the
args in RespFile during soong_zip running.
Test: real tests in my local machine, and compare the
res/libphonenumber.jar before and after changes.
./cmd -o test.zip '""'-C -> [./cmd,-o,test.zip,""-C]
./cmd -o test.zip '-C -f -> [./cmd,-o,test.zip,-C -f]
./cmd -o test.zip '\"'-C -f -> [./cmd,-o,test.zip,\"-C -f]
./cmd -o test.zip '\\'-C -f -> [./cmd,-o,test.zip,\\-C -f]
./cmd -o test.zip '\a'-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip \'-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip \\-C -> [./cmd,-o,test.zip,\-C]
./cmd -o test.zip \"-C -> [./cmd,-o,test.zip,"-C]
./cmd -o test.zip "'"-C -> [./cmd,-o,test.zip,'-C]
./cmd -o test.zip "\\"-C -f -> [./cmd,-o,test.zip,\a-C -f]
./cmd -o test.zip "\""-C -f -> [./cmd,-o,test.zip,"a-C -f]
Bug: b/72484223
Change-Id: I83c3630b70c8396c8e8a3f266244d868d754c4e8
2018-01-27 03:30:36 +01:00
|
|
|
|
|
|
|
flags.Parse(expandedArgs[1:])
|
2017-10-12 21:19:14 +02:00
|
|
|
|
|
|
|
err := zip.Run(zip.ZipArgs{
|
|
|
|
FileArgs: fArgs,
|
|
|
|
OutputFilePath: *out,
|
|
|
|
CpuProfileFilePath: *cpuProfile,
|
|
|
|
TraceFilePath: *traceFile,
|
|
|
|
EmulateJar: *emulateJar,
|
|
|
|
AddDirectoryEntriesToZip: *directories,
|
|
|
|
CompressionLevel: *compLevel,
|
|
|
|
ManifestSourcePath: *manifest,
|
|
|
|
NumParallelJobs: *parallelJobs,
|
|
|
|
NonDeflatedFiles: nonDeflatedFiles,
|
2017-11-10 22:11:02 +01:00
|
|
|
WriteIfChanged: *writeIfChanged,
|
2017-10-12 21:19:14 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2018-09-26 23:36:44 +02:00
|
|
|
fmt.Fprintln(os.Stderr, err.Error())
|
2017-10-12 21:19:14 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|