blob: def76aa6290a4658982f742f7244885308adf153 [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 "os"
Jeff Gaston11b5c512017-10-12 12:19:14 -070028 "runtime"
Colin Crossb051ab52018-09-27 15:04:24 -070029 "runtime/pprof"
30 "runtime/trace"
Colin Crossb7c69112018-09-18 16:51:43 -070031 "strconv"
Jeff Gaston11b5c512017-10-12 12:19:14 -070032 "strings"
33
Colin Crossfd708b52021-03-23 14:16:05 -070034 "android/soong/response"
Jeff Gaston11b5c512017-10-12 12:19:14 -070035 "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, "@") {
Colin Crossfd708b52021-03-23 14:16:05 -0700128 f, err := os.Open(strings.TrimPrefix(arg, "@"))
Nan Zhang674dd932018-01-26 18:30:36 -0800129 if err != nil {
130 fmt.Fprintln(os.Stderr, err.Error())
131 os.Exit(1)
132 }
Colin Crossfd708b52021-03-23 14:16:05 -0700133
134 respArgs, err := response.ReadRspFile(f)
135 f.Close()
136 if err != nil {
137 fmt.Fprintln(os.Stderr, err.Error())
138 os.Exit(1)
139 }
Nan Zhang674dd932018-01-26 18:30:36 -0800140 expandedArgs = append(expandedArgs, respArgs...)
141 } else {
142 expandedArgs = append(expandedArgs, arg)
143 }
144 }
145
146 flags := flag.NewFlagSet("flags", flag.ExitOnError)
Dan Willemsen7f276c62019-04-20 21:44:18 -0700147 flags.Usage = func() {
148 fmt.Fprintf(os.Stderr, "usage: soong_zip -o zipfile [-m manifest] [-C dir] [-f|-l file] [-D dir]...\n")
149 flags.PrintDefaults()
150 os.Exit(2)
151 }
Nan Zhang674dd932018-01-26 18:30:36 -0800152
153 out := flags.String("o", "", "file to write zip file to")
154 manifest := flags.String("m", "", "input jar manifest file name")
155 directories := flags.Bool("d", false, "include directories in zip")
Nan Zhang674dd932018-01-26 18:30:36 -0800156 compLevel := flags.Int("L", 5, "deflate compression level (0-9)")
157 emulateJar := flags.Bool("jar", false, "modify the resultant .zip to emulate the output of 'jar'")
158 writeIfChanged := flags.Bool("write_if_changed", false, "only update resultant .zip if it has changed")
Colin Cross4be8f9e2018-09-28 15:16:48 -0700159 ignoreMissingFiles := flags.Bool("ignore_missing_files", false, "continue if a requested file does not exist")
Colin Cross09f11052018-09-21 15:12:39 -0700160 symlinks := flags.Bool("symlinks", true, "store symbolic links in zip instead of following them")
Colin Cross9cb51db2019-06-17 14:12:41 -0700161 srcJar := flags.Bool("srcjar", false, "move .java files to locations that match their package statement")
Colin Cross09f11052018-09-21 15:12:39 -0700162
Colin Crossdf3f4c82018-09-18 16:31:09 -0700163 parallelJobs := flags.Int("parallel", runtime.NumCPU(), "number of parallel threads to use")
Nan Zhang674dd932018-01-26 18:30:36 -0800164 cpuProfile := flags.String("cpuprofile", "", "write cpu profile to file")
165 traceFile := flags.String("trace", "", "write trace to file")
Zhenhuang Wangb8451b82023-01-06 20:58:01 +0800166 sha256Checksum := flags.Bool("sha256", false, "add a zip header to each file containing its SHA256 digest")
Nan Zhang674dd932018-01-26 18:30:36 -0800167
Colin Crossfe945b42018-09-27 15:00:07 -0700168 flags.Var(&rootPrefix{}, "P", "path prefix within the zip at which to place files")
Colin Cross053fca12020-08-19 13:51:47 -0700169 flags.Var(&listFiles{}, "l", "file containing list of files to zip")
170 flags.Var(&rspFiles{}, "r", "file containing list of files to zip with Ninja rsp file escaping")
Nan Zhang674dd932018-01-26 18:30:36 -0800171 flags.Var(&dir{}, "D", "directory to include in zip")
172 flags.Var(&file{}, "f", "file to include in zip")
173 flags.Var(&nonDeflatedFiles, "s", "file path to be stored within the zip without compression")
Colin Crossfe945b42018-09-27 15:00:07 -0700174 flags.Var(&relativeRoot{}, "C", "path to use as relative root of files in following -f, -l, or -D arguments")
175 flags.Var(&junkPaths{}, "j", "junk paths, zip files without directory names")
Nan Zhang674dd932018-01-26 18:30:36 -0800176
177 flags.Parse(expandedArgs[1:])
Jeff Gaston11b5c512017-10-12 12:19:14 -0700178
Colin Crossfe945b42018-09-27 15:00:07 -0700179 if flags.NArg() > 0 {
180 fmt.Fprintf(os.Stderr, "unexpected arguments %s\n", strings.Join(flags.Args(), " "))
181 flags.Usage()
182 }
183
Colin Crossb051ab52018-09-27 15:04:24 -0700184 if *cpuProfile != "" {
185 f, err := os.Create(*cpuProfile)
186 if err != nil {
187 fmt.Fprintln(os.Stderr, err.Error())
188 os.Exit(1)
189 }
190 defer f.Close()
191 pprof.StartCPUProfile(f)
192 defer pprof.StopCPUProfile()
193 }
194
195 if *traceFile != "" {
196 f, err := os.Create(*traceFile)
197 if err != nil {
198 fmt.Fprintln(os.Stderr, err.Error())
199 os.Exit(1)
200 }
201 defer f.Close()
202 err = trace.Start(f)
203 if err != nil {
204 fmt.Fprintln(os.Stderr, err.Error())
205 os.Exit(1)
206 }
207 defer trace.Stop()
208 }
209
Colin Crossfe945b42018-09-27 15:00:07 -0700210 if fileArgsBuilder.Error() != nil {
211 fmt.Fprintln(os.Stderr, fileArgsBuilder.Error())
212 os.Exit(1)
213 }
214
Colin Cross05518bc2018-09-27 15:06:19 -0700215 err := zip.Zip(zip.ZipArgs{
Colin Crossfe945b42018-09-27 15:00:07 -0700216 FileArgs: fileArgsBuilder.FileArgs(),
Jeff Gaston11b5c512017-10-12 12:19:14 -0700217 OutputFilePath: *out,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700218 EmulateJar: *emulateJar,
Colin Cross9cb51db2019-06-17 14:12:41 -0700219 SrcJar: *srcJar,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700220 AddDirectoryEntriesToZip: *directories,
221 CompressionLevel: *compLevel,
222 ManifestSourcePath: *manifest,
223 NumParallelJobs: *parallelJobs,
224 NonDeflatedFiles: nonDeflatedFiles,
Colin Crossf83c1502017-11-10 13:11:02 -0800225 WriteIfChanged: *writeIfChanged,
Colin Cross09f11052018-09-21 15:12:39 -0700226 StoreSymlinks: *symlinks,
Colin Cross4be8f9e2018-09-28 15:16:48 -0700227 IgnoreMissingFiles: *ignoreMissingFiles,
Zhenhuang Wangb8451b82023-01-06 20:58:01 +0800228 Sha256Checksum: *sha256Checksum,
Jeff Gaston11b5c512017-10-12 12:19:14 -0700229 })
230 if err != nil {
Colin Cross09f11052018-09-21 15:12:39 -0700231 fmt.Fprintln(os.Stderr, "error:", err.Error())
Jeff Gaston11b5c512017-10-12 12:19:14 -0700232 os.Exit(1)
233 }
234}