blob: 1125602d011c3b42265b79af3010e2df05540537 [file] [log] [blame]
Jeff Gaston11b5c512017-10-12 12:19:14 -07001// Copyright 2015 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package main
16
17import (
Jeff Gaston11b5c512017-10-12 12:19:14 -070018 "flag"
19 "fmt"
Jeff Gaston11b5c512017-10-12 12:19:14 -070020 "io/ioutil"
21 "os"
Jeff Gaston11b5c512017-10-12 12:19:14 -070022 "runtime"
Colin Crossb051ab52018-09-27 15:04:24 -070023 "runtime/pprof"
24 "runtime/trace"
Colin Crossb7c69112018-09-18 16:51:43 -070025 "strconv"
Jeff Gaston11b5c512017-10-12 12:19:14 -070026 "strings"
27
28 "android/soong/zip"
29)
30
Jeff Gaston11b5c512017-10-12 12:19:14 -070031type uniqueSet map[string]bool
32
33func (u *uniqueSet) String() string {
34 return `""`
35}
36
37func (u *uniqueSet) Set(s string) error {
38 if _, found := (*u)[s]; found {
39 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
40 } else {
41 (*u)[s] = true
42 }
43
44 return nil
45}
46
47type file struct{}
48
Colin Crossfe945b42018-09-27 15:00:07 -070049func (file) String() string { return `""` }
50
51func (file) Set(s string) error {
52 fileArgsBuilder.File(s)
53 return nil
54}
55
Jeff Gaston11b5c512017-10-12 12:19:14 -070056type listFiles struct{}
57
Colin Crossfe945b42018-09-27 15:00:07 -070058func (listFiles) String() string { return `""` }
59
60func (listFiles) Set(s string) error {
61 fileArgsBuilder.List(s)
62 return nil
63}
64
Jeff Gaston11b5c512017-10-12 12:19:14 -070065type dir struct{}
66
Colin Crossfe945b42018-09-27 15:00:07 -070067func (dir) String() string { return `""` }
Jeff Gaston11b5c512017-10-12 12:19:14 -070068
Colin Crossfe945b42018-09-27 15:00:07 -070069func (dir) Set(s string) error {
70 fileArgsBuilder.Dir(s)
Jeff Gaston11b5c512017-10-12 12:19:14 -070071 return nil
72}
73
Colin Crossfe945b42018-09-27 15:00:07 -070074type relativeRoot struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -070075
Colin Crossfe945b42018-09-27 15:00:07 -070076func (relativeRoot) String() string { return "" }
Jeff Gaston11b5c512017-10-12 12:19:14 -070077
Colin Crossfe945b42018-09-27 15:00:07 -070078func (relativeRoot) Set(s string) error {
79 fileArgsBuilder.SourcePrefixToStrip(s)
Jeff Gaston11b5c512017-10-12 12:19:14 -070080 return nil
81}
82
Colin Crossfe945b42018-09-27 15:00:07 -070083type junkPaths struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -070084
Colin Crossfe945b42018-09-27 15:00:07 -070085func (junkPaths) IsBoolFlag() bool { return true }
86func (junkPaths) String() string { return "" }
Jeff Gaston11b5c512017-10-12 12:19:14 -070087
Colin Crossfe945b42018-09-27 15:00:07 -070088func (junkPaths) Set(s string) error {
89 v, err := strconv.ParseBool(s)
90 fileArgsBuilder.JunkPaths(v)
Colin Crossb7c69112018-09-18 16:51:43 -070091 return err
92}
93
Colin Crossfe945b42018-09-27 15:00:07 -070094type rootPrefix struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -070095
Colin Crossfe945b42018-09-27 15:00:07 -070096func (rootPrefix) String() string { return "" }
97
98func (rootPrefix) Set(s string) error {
99 fileArgsBuilder.PathPrefixInZip(s)
100 return nil
101}
102
103var (
104 fileArgsBuilder = zip.NewFileArgsBuilder()
Jeff Gaston11b5c512017-10-12 12:19:14 -0700105 nonDeflatedFiles = make(uniqueSet)
Jeff Gaston11b5c512017-10-12 12:19:14 -0700106)
107
Jeff Gaston11b5c512017-10-12 12:19:14 -0700108func usage() {
Colin Crossfe945b42018-09-27 15:00:07 -0700109 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] [-C dir] [-f|-l file] [-D dir]...\n")
Jeff Gaston11b5c512017-10-12 12:19:14 -0700110 flag.PrintDefaults()
111 os.Exit(2)
112}
113
114func main() {
Nan Zhang674dd932018-01-26 18:30:36 -0800115 var expandedArgs []string
116 for _, arg := range os.Args {
117 if strings.HasPrefix(arg, "@") {
118 bytes, err := ioutil.ReadFile(strings.TrimPrefix(arg, "@"))
119 if err != nil {
120 fmt.Fprintln(os.Stderr, err.Error())
121 os.Exit(1)
122 }
123 respArgs := zip.ReadRespFile(bytes)
124 expandedArgs = append(expandedArgs, respArgs...)
125 } else {
126 expandedArgs = append(expandedArgs, arg)
127 }
128 }
129
130 flags := flag.NewFlagSet("flags", flag.ExitOnError)
Colin Crossfe945b42018-09-27 15:00:07 -0700131 flags.Usage = usage
Nan Zhang674dd932018-01-26 18:30:36 -0800132
133 out := flags.String("o", "", "file to write zip file to")
134 manifest := flags.String("m", "", "input jar manifest file name")
135 directories := flags.Bool("d", false, "include directories in zip")
Nan Zhang674dd932018-01-26 18:30:36 -0800136 compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
137 emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
138 writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
139
Colin Crossdf3f4c82018-09-18 16:31:09 -0700140 parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
Nan Zhang674dd932018-01-26 18:30:36 -0800141 cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
142 traceFile := flags.String("trace", "", "write trace to file")
143
Colin Crossfe945b42018-09-27 15:00:07 -0700144 flags.Var(&rootPrefix{}, "P", "path prefix within the zip at which to place files")
Nan Zhang674dd932018-01-26 18:30:36 -0800145 flags.Var(&listFiles{}, "l", "file containing list of .class files")
146 flags.Var(&dir{}, "D", "directory to include in zip")
147 flags.Var(&file{}, "f", "file to include in zip")
148 flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Crossfe945b42018-09-27 15:00:07 -0700149 flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
150 flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
Nan Zhang674dd932018-01-26 18:30:36 -0800151
152 flags.Parse(expandedArgs[1:])
Jeff Gaston11b5c512017-10-12 12:19:14 -0700153
Colin Crossfe945b42018-09-27 15:00:07 -0700154 if flags.NArg() > 0 {
155 fmt.Fprintf(os.Stderr, "unexpected arguments %s\n", strings.Join(flags.Args(), " "))
156 flags.Usage()
157 }
158
Colin Crossb051ab52018-09-27 15:04:24 -0700159 if *cpuProfile != "" {
160 f, err := os.Create(*cpuProfile)
161 if err != nil {
162 fmt.Fprintln(os.Stderr, err.Error())
163 os.Exit(1)
164 }
165 defer f.Close()
166 pprof.StartCPUProfile(f)
167 defer pprof.StopCPUProfile()
168 }
169
170 if *traceFile != "" {
171 f, err := os.Create(*traceFile)
172 if err != nil {
173 fmt.Fprintln(os.Stderr, err.Error())
174 os.Exit(1)
175 }
176 defer f.Close()
177 err = trace.Start(f)
178 if err != nil {
179 fmt.Fprintln(os.Stderr, err.Error())
180 os.Exit(1)
181 }
182 defer trace.Stop()
183 }
184
Colin Crossfe945b42018-09-27 15:00:07 -0700185 if fileArgsBuilder.Error() != nil {
186 fmt.Fprintln(os.Stderr, fileArgsBuilder.Error())
187 os.Exit(1)
188 }
189
Colin Cross05518bc2018-09-27 15:06:19 -0700190 err := zip.Zip(zip.ZipArgs{
Colin Crossfe945b42018-09-27 15:00:07 -0700191 FileArgs: fileArgsBuilder.FileArgs(),
Jeff Gaston11b5c512017-10-12 12:19:14 -0700192 OutputFilePath: *out,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700193 EmulateJar: *emulateJar,
194 AddDirectoryEntriesToZip: *directories,
195 CompressionLevel: *compLevel,
196 ManifestSourcePath: *manifest,
197 NumParallelJobs: *parallelJobs,
198 NonDeflatedFiles: nonDeflatedFiles,
Colin Crossf83c1502017-11-10 13:11:02 -0800199 WriteIfChanged: *writeIfChanged,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700200 })
201 if err != nil {
Colin Crossc7feeff2018-09-26 21:36:44 +0000202 fmt.Fprintln(os.Stderr, err.Error())
Jeff Gaston11b5c512017-10-12 12:19:14 -0700203 os.Exit(1)
204 }
205}