blob: 58d8d345e94750843244f21b1b27af05d08803bf [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
Patrice Arrudaa5c25422019-04-09 18:49:49 -070035// A command represents an operation to be executed in the soong build
36// system.
37type command struct {
38 // the flag name (must have double dashes)
39 flag string
40
41 // description for the flag (to display when running help)
42 description string
43
44 // Creates the build configuration based on the args and build context.
45 config func(ctx build.Context, args ...string) build.Config
46
47 // Returns what type of IO redirection this Command requires.
48 stdio func() terminal.StdioInterface
49
50 // run the command
51 run func(ctx build.Context, config build.Config, args []string, logsDir string)
52}
53
54const makeModeFlagName = "--make-mode"
55
56// list of supported commands (flags) supported by soong ui
57var commands []command = []command{
58 {
59 flag: makeModeFlagName,
60 description: "build the modules by the target name (i.e. soong_docs)",
61 config: func(ctx build.Context, args ...string) build.Config {
62 return build.NewConfig(ctx, args...)
63 },
64 stdio: func() terminal.StdioInterface {
65 return terminal.StdioImpl{}
66 },
67 run: make,
68 }, {
69 flag: "--dumpvar-mode",
70 description: "print the value of the legacy make variable VAR to stdout",
71 config: dumpVarConfig,
72 stdio: customStdio,
73 run: dumpVar,
74 }, {
75 flag: "--dumpvars-mode",
76 description: "dump the values of one or more legacy make variables, in shell syntax",
77 config: dumpVarConfig,
78 stdio: customStdio,
79 run: dumpVars,
80 },
81}
82
83// indexList returns the index of first found s. -1 is return if s is not
84// found.
Dan Willemsen1e704462016-08-21 15:17:17 -070085func indexList(s string, list []string) int {
86 for i, l := range list {
87 if l == s {
88 return i
89 }
90 }
Dan Willemsen1e704462016-08-21 15:17:17 -070091 return -1
92}
93
Patrice Arrudaa5c25422019-04-09 18:49:49 -070094// inList returns true if one or more of s is in the list.
Dan Willemsen1e704462016-08-21 15:17:17 -070095func inList(s string, list []string) bool {
96 return indexList(s, list) != -1
97}
98
Patrice Arrudaa5c25422019-04-09 18:49:49 -070099// Main execution of soong_ui. The command format is as follows:
100//
101// soong_ui <command> [<arg 1> <arg 2> ... <arg n>]
102//
103// Command is the type of soong_ui execution. Only one type of
104// execution is specified. The args are specific to the command.
Dan Willemsen1e704462016-08-21 15:17:17 -0700105func main() {
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700106 c, args := getCommand(os.Args)
107 if c == nil {
108 fmt.Fprintf(os.Stderr, "The `soong` native UI is not yet available.\n")
109 os.Exit(1)
Dan Willemsenc35b3812018-07-16 19:59:10 -0700110 }
111
Colin Cross097ed2a2019-06-08 21:48:58 -0700112 log := logger.New(c.stdio().Stdout())
Dan Willemsen1e704462016-08-21 15:17:17 -0700113 defer log.Cleanup()
114
Dan Willemsen1e704462016-08-21 15:17:17 -0700115 ctx, cancel := context.WithCancel(context.Background())
116 defer cancel()
117
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700118 trace := tracer.New(log)
119 defer trace.Close()
Dan Willemsen1e704462016-08-21 15:17:17 -0700120
Nan Zhang17f27672018-12-12 16:01:49 -0800121 met := metrics.New()
122
Dan Willemsenb82471a2018-05-17 16:37:09 -0700123 stat := &status.Status{}
124 defer stat.Finish()
Colin Cross097ed2a2019-06-08 21:48:58 -0700125 stat.AddOutput(terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"),
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800126 build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD")))
Dan Willemsenb82471a2018-05-17 16:37:09 -0700127 stat.AddOutput(trace.StatusTracer())
128
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700129 build.SetupSignals(log, cancel, func() {
130 trace.Close()
131 log.Cleanup()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700132 stat.Finish()
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700133 })
134
Dan Willemsen59339a22018-07-22 21:18:45 -0700135 buildCtx := build.Context{ContextImpl: &build.ContextImpl{
Dan Willemsenb82471a2018-05-17 16:37:09 -0700136 Context: ctx,
137 Logger: log,
Nan Zhang17f27672018-12-12 16:01:49 -0800138 Metrics: met,
Dan Willemsenb82471a2018-05-17 16:37:09 -0700139 Tracer: trace,
Colin Cross097ed2a2019-06-08 21:48:58 -0700140 Writer: c.stdio().Stdout(),
Dan Willemsenb82471a2018-05-17 16:37:09 -0700141 Status: stat,
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700142 }}
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700143
144 config := c.config(buildCtx, args...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700145
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700146 build.SetupOutDir(buildCtx, config)
Dan Willemsen8a073a82017-02-04 17:30:44 -0800147
Dan Willemsenb82471a2018-05-17 16:37:09 -0700148 logsDir := config.OutDir()
Dan Willemsen8a073a82017-02-04 17:30:44 -0800149 if config.Dist() {
Dan Willemsenb82471a2018-05-17 16:37:09 -0700150 logsDir = filepath.Join(config.DistDir(), "logs")
Dan Willemsen8a073a82017-02-04 17:30:44 -0800151 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700152
Dan Willemsenb82471a2018-05-17 16:37:09 -0700153 os.MkdirAll(logsDir, 0777)
154 log.SetOutput(filepath.Join(logsDir, "soong.log"))
155 trace.SetOutput(filepath.Join(logsDir, "build.trace"))
156 stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, "verbose.log")))
157 stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, "error.log")))
158
Nan Zhangd50f53b2019-01-07 20:26:51 -0800159 defer met.Dump(filepath.Join(logsDir, "build_metrics"))
160
Dan Willemsen1e704462016-08-21 15:17:17 -0700161 if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok {
162 if !strings.HasSuffix(start, "N") {
163 if start_time, err := strconv.ParseUint(start, 10, 64); err == nil {
164 log.Verbosef("Took %dms to start up.",
165 time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds())
Nan Zhang17f27672018-12-12 16:01:49 -0800166 buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano()))
Dan Willemsen1e704462016-08-21 15:17:17 -0700167 }
168 }
Dan Willemsencae59bc2017-07-13 14:27:31 -0700169
170 if executable, err := os.Executable(); err == nil {
171 trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace"))
172 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700173 }
174
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700175 f := build.NewSourceFinder(buildCtx, config)
176 defer f.Shutdown()
177 build.FindSources(buildCtx, config, f)
178
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700179 c.run(buildCtx, config, args, logsDir)
Dan Willemsen051133b2017-07-14 11:29:29 -0700180}
181
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700182func dumpVar(ctx build.Context, config build.Config, args []string, _ string) {
Dan Willemsen051133b2017-07-14 11:29:29 -0700183 flags := flag.NewFlagSet("dumpvar", flag.ExitOnError)
184 flags.Usage = func() {
185 fmt.Fprintf(os.Stderr, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0])
186 fmt.Fprintln(os.Stderr, "In dumpvar mode, print the value of the legacy make variable VAR to stdout")
187 fmt.Fprintln(os.Stderr, "")
188
189 fmt.Fprintln(os.Stderr, "'report_config' is a special case that prints the human-readable config banner")
190 fmt.Fprintln(os.Stderr, "from the beginning of the build.")
191 fmt.Fprintln(os.Stderr, "")
192 flags.PrintDefaults()
193 }
194 abs := flags.Bool("abs", false, "Print the absolute path of the value")
195 flags.Parse(args)
196
197 if flags.NArg() != 1 {
198 flags.Usage()
199 os.Exit(1)
200 }
201
202 varName := flags.Arg(0)
203 if varName == "report_config" {
204 varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars)
205 if err != nil {
206 ctx.Fatal(err)
207 }
208
209 fmt.Println(build.Banner(varData))
210 } else {
211 varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName})
212 if err != nil {
213 ctx.Fatal(err)
214 }
215
216 if *abs {
217 var res []string
218 for _, path := range strings.Fields(varData[varName]) {
219 if abs, err := filepath.Abs(path); err == nil {
220 res = append(res, abs)
221 } else {
222 ctx.Fatalln("Failed to get absolute path of", path, err)
223 }
224 }
225 fmt.Println(strings.Join(res, " "))
226 } else {
227 fmt.Println(varData[varName])
228 }
229 }
230}
231
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700232func dumpVars(ctx build.Context, config build.Config, args []string, _ string) {
Dan Willemsen051133b2017-07-14 11:29:29 -0700233 flags := flag.NewFlagSet("dumpvars", flag.ExitOnError)
234 flags.Usage = func() {
235 fmt.Fprintf(os.Stderr, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0])
236 fmt.Fprintln(os.Stderr, "In dumpvars mode, dump the values of one or more legacy make variables, in")
237 fmt.Fprintln(os.Stderr, "shell syntax. The resulting output may be sourced directly into a shell to")
238 fmt.Fprintln(os.Stderr, "set corresponding shell variables.")
239 fmt.Fprintln(os.Stderr, "")
240
241 fmt.Fprintln(os.Stderr, "'report_config' is a special case that dumps a variable containing the")
242 fmt.Fprintln(os.Stderr, "human-readable config banner from the beginning of the build.")
243 fmt.Fprintln(os.Stderr, "")
244 flags.PrintDefaults()
245 }
246
247 varsStr := flags.String("vars", "", "Space-separated list of variables to dump")
248 absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)")
249
250 varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping")
251 absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping")
252
253 flags.Parse(args)
254
255 if flags.NArg() != 0 {
256 flags.Usage()
257 os.Exit(1)
258 }
259
260 vars := strings.Fields(*varsStr)
261 absVars := strings.Fields(*absVarsStr)
262
263 allVars := append([]string{}, vars...)
264 allVars = append(allVars, absVars...)
265
266 if i := indexList("report_config", allVars); i != -1 {
267 allVars = append(allVars[:i], allVars[i+1:]...)
268 allVars = append(allVars, build.BannerVars...)
269 }
270
271 if len(allVars) == 0 {
272 return
273 }
274
275 varData, err := build.DumpMakeVars(ctx, config, nil, allVars)
276 if err != nil {
277 ctx.Fatal(err)
278 }
279
280 for _, name := range vars {
281 if name == "report_config" {
282 fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData))
283 } else {
284 fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name])
285 }
286 }
287 for _, name := range absVars {
288 var res []string
289 for _, path := range strings.Fields(varData[name]) {
290 abs, err := filepath.Abs(path)
291 if err != nil {
292 ctx.Fatalln("Failed to get absolute path of", path, err)
293 }
294 res = append(res, abs)
295 }
296 fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " "))
297 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700298}
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700299
300func customStdio() terminal.StdioInterface {
301 return terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr)
302}
303
304// dumpVarConfig does not require any arguments to be parsed by the NewConfig.
305func dumpVarConfig(ctx build.Context, args ...string) build.Config {
306 return build.NewConfig(ctx)
307}
308
309func make(ctx build.Context, config build.Config, _ []string, logsDir string) {
310 if config.IsVerbose() {
311 writer := ctx.Writer
Colin Cross097ed2a2019-06-08 21:48:58 -0700312 fmt.Fprintln(writer, "! The argument `showcommands` is no longer supported.")
313 fmt.Fprintln(writer, "! Instead, the verbose log is always written to a compressed file in the output dir:")
314 fmt.Fprintln(writer, "!")
315 fmt.Fprintf(writer, "! gzip -cd %s/verbose.log.gz | less -R\n", logsDir)
316 fmt.Fprintln(writer, "!")
317 fmt.Fprintln(writer, "! Older versions are saved in verbose.log.#.gz files")
318 fmt.Fprintln(writer, "")
Patrice Arrudaa5c25422019-04-09 18:49:49 -0700319 time.Sleep(5 * time.Second)
320 }
321
322 toBuild := build.BuildAll
323 if config.Checkbuild() {
324 toBuild |= build.RunBuildTests
325 }
326 build.Build(ctx, config, toBuild)
327}
328
329// getCommand finds the appropriate command based on args[1] flag. args[0]
330// is the soong_ui filename.
331func getCommand(args []string) (*command, []string) {
332 if len(args) < 2 {
333 return nil, args
334 }
335
336 for _, c := range commands {
337 if c.flag == args[1] {
338 return &c, args[2:]
339 }
340
341 // special case for --make-mode: if soong_ui was called from
342 // build/make/core/main.mk, the makeparallel with --ninja
343 // option specified puts the -j<num> before --make-mode.
344 // TODO: Remove this hack once it has been fixed.
345 if c.flag == makeModeFlagName {
346 if inList(makeModeFlagName, args) {
347 return &c, args[1:]
348 }
349 }
350 }
351
352 // command not found
353 return nil, args
354}