blob: d6999c57000fbd6cd2fd44e934d4f23e8f1e2942 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 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 (
18 "context"
Dan Willemsen051133b2017-07-14 11:29:29 -070019 "flag"
20 "fmt"
Dan Willemsen1e704462016-08-21 15:17:17 -070021 "os"
22 "path/filepath"
23 "strconv"
24 "strings"
25 "time"
26
27 "android/soong/ui/build"
28 "android/soong/ui/logger"
Nan Zhang17f27672018-12-12 16:01:49 -080029 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070030 "android/soong/ui/status"
31 "android/soong/ui/terminal"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070032 "android/soong/ui/tracer"
Dan Willemsen1e704462016-08-21 15:17:17 -070033)
34
35func indexList(s string, list []string) int {
36 for i, l := range list {
37 if l == s {
38 return i
39 }
40 }
41
42 return -1
43}
44
45func inList(s string, list []string) bool {
46 return indexList(s, list) != -1
47}
48
49func main() {
Dan Willemsenc35b3812018-07-16 19:59:10 -070050 var stdio terminal.StdioInterface
51 stdio = terminal.StdioImpl{}
52
53 // dumpvar uses stdout, everything else should be in stderr
54 if os.Args[1] == "--dumpvar-mode" || os.Args[1] == "--dumpvars-mode" {
55 stdio = terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
56 }
57
58 writer := terminal.NewWriter(stdio)
Dan Willemsenb82471a2018-05-17 16:37:09 -070059 defer writer.Finish()
60
61 log := logger.New(writer)
Dan Willemsen1e704462016-08-21 15:17:17 -070062 defer log.Cleanup()
63
Dan Willemsen051133b2017-07-14 11:29:29 -070064 if len(os.Args) < 2 || !(inList("--make-mode", os.Args) ||
65 os.Args[1] == "--dumpvars-mode" ||
66 os.Args[1] == "--dumpvar-mode") {
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068 log.Fatalln("The `soong` native UI is not yet available.")
69 }
70
Dan Willemsen1e704462016-08-21 15:17:17 -070071 ctx, cancel := context.WithCancel(context.Background())
72 defer cancel()
73
Dan Willemsend9f6fa22016-08-21 15:17:17 -070074 trace := tracer.New(log)
75 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -070076
Nan Zhang17f27672018-12-12 16:01:49 -080077 met := metrics.New()
78
Dan Willemsenb82471a2018-05-17 16:37:09 -070079 stat := &status.Status{}
80 defer stat.Finish()
Sasha Smundakc0c9ef92019-01-23 09:52:57 -080081 stat.AddOutput(terminal.NewStatusOutput(writer, os.Getenv("NINJA_STATUS"),
82 build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD")))
Dan Willemsenb82471a2018-05-17 16:37:09 -070083 stat.AddOutput(trace.StatusTracer())
84
Dan Willemsend9f6fa22016-08-21 15:17:17 -070085 build.SetupSignals(log, cancel, func() {
86 trace.Close()
87 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -070088 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -070089 })
90
Dan Willemsen59339a22018-07-22 21:18:45 -070091 buildCtx := build.Context{ContextImpl: &build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -070092 Context: ctx,
93 Logger: log,
Nan Zhang17f27672018-12-12 16:01:49 -080094 Metrics: met,
Dan Willemsenb82471a2018-05-17 16:37:09 -070095 Tracer: trace,
96 Writer: writer,
97 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -070098 }}
Dan Willemsen051133b2017-07-14 11:29:29 -070099 var config build.Config
100 if os.Args[1] == "--dumpvars-mode" || os.Args[1] == "--dumpvar-mode" {
101 config = build.NewConfig(buildCtx)
102 } else {
103 config = build.NewConfig(buildCtx, os.Args[1:]...)
104 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700105
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700106 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800107
Dan Willemsenb82471a2018-05-17 16:37:09 -0700108 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -0800109 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700110 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -0800111 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700112
Dan Willemsenb82471a2018-05-17 16:37:09 -0700113 os.MkdirAll(logsDir, 0777)
114 log.SetOutput(filepath.Join(logsDir, "soong.log"))
115 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
116 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
117 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
118
Nan Zhangd50f53b2019-01-07 20:26:51 -0800119 defer met.Dump(filepath.Join(logsDir, "build_metrics"))
120
Dan Willemsen1e704462016-08-21 15:17:17 -0700121 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
122 if !strings.HasSuffix(start, "N") {
123 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
124 log.Verbosef("Took %dms to start up.",
125 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -0800126 buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700127 }
128 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700129
130 if executable, err := os.Executable(); err == nil {
131 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
132 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700133 }
134
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700135 f := build.NewSourceFinder(buildCtx, config)
136 defer f.Shutdown()
137 build.FindSources(buildCtx, config, f)
138
Dan Willemsen051133b2017-07-14 11:29:29 -0700139 if os.Args[1] == "--dumpvar-mode" {
140 dumpVar(buildCtx, config, os.Args[2:])
141 } else if os.Args[1] == "--dumpvars-mode" {
142 dumpVars(buildCtx, config, os.Args[2:])
143 } else {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700144 if config.IsVerbose() {
145 writer.Print("! The argument `showcommands` is no longer supported.")
146 writer.Print("! Instead, the verbose log is always written to a compressed file in the output dir:")
147 writer.Print("!")
148 writer.Print(fmt.Sprintf("! gzip -cd %s/verbose.log.gz | less -R", logsDir))
149 writer.Print("!")
150 writer.Print("! Older versions are saved in verbose.log.#.gz files")
151 writer.Print("")
152 time.Sleep(5 * time.Second)
153 }
154
Colin Cross37193492017-11-16 17:55:00 -0800155 toBuild := build.BuildAll
156 if config.Checkbuild() {
157 toBuild |= build.RunBuildTests
158 }
159 build.Build(buildCtx, config, toBuild)
Dan Willemsen051133b2017-07-14 11:29:29 -0700160 }
161}
162
163func dumpVar(ctx build.Context, config build.Config, args []string) {
164 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
165 flags.Usage = func() {
166 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
167 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
168 fmt.Fprintln(os.Stderr, "")
169
170 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
171 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
172 fmt.Fprintln(os.Stderr, "")
173 flags.PrintDefaults()
174 }
175 abs := flags.Bool("abs", false, "Print the absolute path of the value")
176 flags.Parse(args)
177
178 if flags.NArg() != 1 {
179 flags.Usage()
180 os.Exit(1)
181 }
182
183 varName := flags.Arg(0)
184 if varName == "report_config" {
185 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
186 if err != nil {
187 ctx.Fatal(err)
188 }
189
190 fmt.Println(build.Banner(varData))
191 } else {
192 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
193 if err != nil {
194 ctx.Fatal(err)
195 }
196
197 if *abs {
198 var res []string
199 for _, path := range strings.Fields(varData[varName]) {
200 if abs, err := filepath.Abs(path); err == nil {
201 res = append(res, abs)
202 } else {
203 ctx.Fatalln("Failed to get absolute path of", path, err)
204 }
205 }
206 fmt.Println(strings.Join(res, " "))
207 } else {
208 fmt.Println(varData[varName])
209 }
210 }
211}
212
213func dumpVars(ctx build.Context, config build.Config, args []string) {
214 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
215 flags.Usage = func() {
216 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
217 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
218 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
219 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
220 fmt.Fprintln(os.Stderr, "")
221
222 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
223 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
224 fmt.Fprintln(os.Stderr, "")
225 flags.PrintDefaults()
226 }
227
228 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
229 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
230
231 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
232 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
233
234 flags.Parse(args)
235
236 if flags.NArg() != 0 {
237 flags.Usage()
238 os.Exit(1)
239 }
240
241 vars := strings.Fields(*varsStr)
242 absVars := strings.Fields(*absVarsStr)
243
244 allVars := append([]string{}, vars...)
245 allVars = append(allVars, absVars...)
246
247 if i := indexList("report_config", allVars); i != -1 {
248 allVars = append(allVars[:i], allVars[i+1:]...)
249 allVars = append(allVars, build.BannerVars...)
250 }
251
252 if len(allVars) == 0 {
253 return
254 }
255
256 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
257 if err != nil {
258 ctx.Fatal(err)
259 }
260
261 for _, name := range vars {
262 if name == "report_config" {
263 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
264 } else {
265 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
266 }
267 }
268 for _, name := range absVars {
269 var res []string
270 for _, path := range strings.Fields(varData[name]) {
271 abs, err := filepath.Abs(path)
272 if err != nil {
273 ctx.Fatalln("Failed to get absolute path of", path, err)
274 }
275 res = append(res, abs)
276 }
277 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
278 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700279}