blob: fc976f6892c69715485f72b7b347e4dcd0df864a [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
Sasha Smundak8eedba62020-11-16 19:00:27 -080015// soong_zip is a utility used during the build to create a zip archive by pulling the entries from
16// various sources:
17// * explicitly specified files
18// * files whose paths are read from a file
19// * directories traversed recursively
20// It can optionally change the recorded path of an entry.
21
Jeff Gaston11b5c512017-10-12 12:19:14 -070022package main
23
24import (
Jeff Gaston11b5c512017-10-12 12:19:14 -070025 "flag"
26 "fmt"
Jeff Gaston11b5c512017-10-12 12:19:14 -070027 "io/ioutil"
28 "os"
Jeff Gaston11b5c512017-10-12 12:19:14 -070029 "runtime"
Colin Crossb051ab52018-09-27 15:04:24 -070030 "runtime/pprof"
31 "runtime/trace"
Colin Crossb7c69112018-09-18 16:51:43 -070032 "strconv"
Jeff Gaston11b5c512017-10-12 12:19:14 -070033 "strings"
34
35 "android/soong/zip"
36)
37
Jeff Gaston11b5c512017-10-12 12:19:14 -070038type uniqueSet map[string]bool
39
40func (u *uniqueSet) String() string {
41 return `""`
42}
43
44func (u *uniqueSet) Set(s string) error {
45 if _, found := (*u)[s]; found {
46 return fmt.Errorf("File %q was specified twice as a file to not deflate", s)
47 } else {
48 (*u)[s] = true
49 }
50
51 return nil
52}
53
54type file struct{}
55
Colin Crossfe945b42018-09-27 15:00:07 -070056func (file) String() string { return `""` }
57
58func (file) Set(s string) error {
59 fileArgsBuilder.File(s)
60 return nil
61}
62
Jeff Gaston11b5c512017-10-12 12:19:14 -070063type listFiles struct{}
64
Colin Crossfe945b42018-09-27 15:00:07 -070065func (listFiles) String() string { return `""` }
66
67func (listFiles) Set(s string) error {
68 fileArgsBuilder.List(s)
69 return nil
70}
71
Colin Cross053fca12020-08-19 13:51:47 -070072type rspFiles struct{}
73
74func (rspFiles) String() string { return `""` }
75
76func (rspFiles) Set(s string) error {
77 fileArgsBuilder.RspFile(s)
78 return nil
79}
80
Jeff Gaston11b5c512017-10-12 12:19:14 -070081type dir struct{}
82
Colin Crossfe945b42018-09-27 15:00:07 -070083func (dir) String() string { return `""` }
Jeff Gaston11b5c512017-10-12 12:19:14 -070084
Colin Crossfe945b42018-09-27 15:00:07 -070085func (dir) Set(s string) error {
86 fileArgsBuilder.Dir(s)
Jeff Gaston11b5c512017-10-12 12:19:14 -070087 return nil
88}
89
Colin Crossfe945b42018-09-27 15:00:07 -070090type relativeRoot struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -070091
Colin Crossfe945b42018-09-27 15:00:07 -070092func (relativeRoot) String() string { return "" }
Jeff Gaston11b5c512017-10-12 12:19:14 -070093
Colin Crossfe945b42018-09-27 15:00:07 -070094func (relativeRoot) Set(s string) error {
95 fileArgsBuilder.SourcePrefixToStrip(s)
Jeff Gaston11b5c512017-10-12 12:19:14 -070096 return nil
97}
98
Colin Crossfe945b42018-09-27 15:00:07 -070099type junkPaths struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -0700100
Colin Crossfe945b42018-09-27 15:00:07 -0700101func (junkPaths) IsBoolFlag() bool { return true }
102func (junkPaths) String() string { return "" }
Jeff Gaston11b5c512017-10-12 12:19:14 -0700103
Colin Crossfe945b42018-09-27 15:00:07 -0700104func (junkPaths) Set(s string) error {
105 v, err := strconv.ParseBool(s)
106 fileArgsBuilder.JunkPaths(v)
Colin Crossb7c69112018-09-18 16:51:43 -0700107 return err
108}
109
Colin Crossfe945b42018-09-27 15:00:07 -0700110type rootPrefix struct{}
Jeff Gaston11b5c512017-10-12 12:19:14 -0700111
Colin Crossfe945b42018-09-27 15:00:07 -0700112func (rootPrefix) String() string { return "" }
113
114func (rootPrefix) Set(s string) error {
115 fileArgsBuilder.PathPrefixInZip(s)
116 return nil
117}
118
119var (
120 fileArgsBuilder = zip.NewFileArgsBuilder()
Jeff Gaston11b5c512017-10-12 12:19:14 -0700121 nonDeflatedFiles = make(uniqueSet)
Jeff Gaston11b5c512017-10-12 12:19:14 -0700122)
123
Jeff Gaston11b5c512017-10-12 12:19:14 -0700124func main() {
Nan Zhang674dd932018-01-26 18:30:36 -0800125 var expandedArgs []string
126 for _, arg := range os.Args {
127 if strings.HasPrefix(arg, "@") {
128 bytes, err := ioutil.ReadFile(strings.TrimPrefix(arg, "@"))
129 if err != nil {
130 fmt.Fprintln(os.Stderr, err.Error())
131 os.Exit(1)
132 }
133 respArgs := zip.ReadRespFile(bytes)
134 expandedArgs = append(expandedArgs, respArgs...)
135 } else {
136 expandedArgs = append(expandedArgs, arg)
137 }
138 }
139
140 flags := flag.NewFlagSet("flags", flag.ExitOnError)
Dan Willemsen7f276c62019-04-20 21:44:18 -0700141 flags.Usage = func() {
142 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] [-C dir] [-f|-l file] [-D dir]...\n")
143 flags.PrintDefaults()
144 os.Exit(2)
145 }
Nan Zhang674dd932018-01-26 18:30:36 -0800146
147 out := flags.String("o", "", "file to write zip file to")
148 manifest := flags.String("m", "", "input jar manifest file name")
149 directories := flags.Bool("d", false, "include directories in zip")
Nan Zhang674dd932018-01-26 18:30:36 -0800150 compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
151 emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
152 writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
Colin Cross4be8f9e2018-09-28 15:16:48 -0700153 ignoreMissingFiles := flags.Bool("ignore_missing_files", false, "continue if a requested file does not exist")
Colin Cross09f11052018-09-21 15:12:39 -0700154 symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
Colin Cross9cb51db2019-06-17 14:12:41 -0700155 srcJar := flags.Bool("srcjar", false, "move .java files to locations that match their package statement")
Colin Cross09f11052018-09-21 15:12:39 -0700156
Colin Crossdf3f4c82018-09-18 16:31:09 -0700157 parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
Nan Zhang674dd932018-01-26 18:30:36 -0800158 cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
159 traceFile := flags.String("trace", "", "write trace to file")
160
Colin Crossfe945b42018-09-27 15:00:07 -0700161 flags.Var(&rootPrefix{}, "P", "path prefix within the zip at which to place files")
Colin Cross053fca12020-08-19 13:51:47 -0700162 flags.Var(&listFiles{}, "l", "file containing list of files to zip")
163 flags.Var(&rspFiles{}, "r", "file containing list of files to zip with Ninja rsp file escaping")
Nan Zhang674dd932018-01-26 18:30:36 -0800164 flags.Var(&dir{}, "D", "directory to include in zip")
165 flags.Var(&file{}, "f", "file to include in zip")
166 flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Crossfe945b42018-09-27 15:00:07 -0700167 flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
168 flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
Nan Zhang674dd932018-01-26 18:30:36 -0800169
170 flags.Parse(expandedArgs[1:])
Jeff Gaston11b5c512017-10-12 12:19:14 -0700171
Colin Crossfe945b42018-09-27 15:00:07 -0700172 if flags.NArg() > 0 {
173 fmt.Fprintf(os.Stderr, "unexpected arguments %s\n", strings.Join(flags.Args(), " "))
174 flags.Usage()
175 }
176
Colin Crossb051ab52018-09-27 15:04:24 -0700177 if *cpuProfile != "" {
178 f, err := os.Create(*cpuProfile)
179 if err != nil {
180 fmt.Fprintln(os.Stderr, err.Error())
181 os.Exit(1)
182 }
183 defer f.Close()
184 pprof.StartCPUProfile(f)
185 defer pprof.StopCPUProfile()
186 }
187
188 if *traceFile != "" {
189 f, err := os.Create(*traceFile)
190 if err != nil {
191 fmt.Fprintln(os.Stderr, err.Error())
192 os.Exit(1)
193 }
194 defer f.Close()
195 err = trace.Start(f)
196 if err != nil {
197 fmt.Fprintln(os.Stderr, err.Error())
198 os.Exit(1)
199 }
200 defer trace.Stop()
201 }
202
Colin Crossfe945b42018-09-27 15:00:07 -0700203 if fileArgsBuilder.Error() != nil {
204 fmt.Fprintln(os.Stderr, fileArgsBuilder.Error())
205 os.Exit(1)
206 }
207
Colin Cross05518bc2018-09-27 15:06:19 -0700208 err := zip.Zip(zip.ZipArgs{
Colin Crossfe945b42018-09-27 15:00:07 -0700209 FileArgs: fileArgsBuilder.FileArgs(),
Jeff Gaston11b5c512017-10-12 12:19:14 -0700210 OutputFilePath: *out,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700211 EmulateJar: *emulateJar,
Colin Cross9cb51db2019-06-17 14:12:41 -0700212 SrcJar: *srcJar,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700213 AddDirectoryEntriesToZip: *directories,
214 CompressionLevel: *compLevel,
215 ManifestSourcePath: *manifest,
216 NumParallelJobs: *parallelJobs,
217 NonDeflatedFiles: nonDeflatedFiles,
Colin Crossf83c1502017-11-10 13:11:02 -0800218 WriteIfChanged: *writeIfChanged,
Colin Cross09f11052018-09-21 15:12:39 -0700219 StoreSymlinks: *symlinks,
Colin Cross4be8f9e2018-09-28 15:16:48 -0700220 IgnoreMissingFiles: *ignoreMissingFiles,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700221 })
222 if err != nil {
Colin Cross09f11052018-09-21 15:12:39 -0700223 fmt.Fprintln(os.Stderr, "error:", err.Error())
Jeff Gaston11b5c512017-10-12 12:19:14 -0700224 os.Exit(1)
225 }
226}