Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "context" |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 19 | "flag" |
| 20 | "fmt" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "os" |
| 22 | "path/filepath" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | "time" |
| 26 | |
| 27 | "android/soong/ui/build" |
| 28 | "android/soong/ui/logger" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 29 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 30 | "android/soong/ui/status" |
| 31 | "android/soong/ui/terminal" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 32 | "android/soong/ui/tracer" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 35 | // A command represents an operation to be executed in the soong build |
| 36 | // system. |
| 37 | type 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 | |
| 54 | const makeModeFlagName = "--make-mode" |
| 55 | |
| 56 | // list of supported commands (flags) supported by soong ui |
| 57 | var 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 85 | func indexList(s string, list []string) int { |
| 86 | for i, l := range list { |
| 87 | if l == s { |
| 88 | return i |
| 89 | } |
| 90 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 91 | return -1 |
| 92 | } |
| 93 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 94 | // inList returns true if one or more of s is in the list. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 95 | func inList(s string, list []string) bool { |
| 96 | return indexList(s, list) != -1 |
| 97 | } |
| 98 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 99 | // 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 105 | func main() { |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 106 | 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 Willemsen | c35b381 | 2018-07-16 19:59:10 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 112 | log := logger.New(c.stdio().Stdout()) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 113 | defer log.Cleanup() |
| 114 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 115 | ctx, cancel := context.WithCancel(context.Background()) |
| 116 | defer cancel() |
| 117 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 118 | trace := tracer.New(log) |
| 119 | defer trace.Close() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 120 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 121 | met := metrics.New() |
| 122 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 123 | stat := &status.Status{} |
| 124 | defer stat.Finish() |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 125 | stat.AddOutput(terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"), |
Sasha Smundak | c0c9ef9 | 2019-01-23 09:52:57 -0800 | [diff] [blame] | 126 | build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD"))) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 127 | stat.AddOutput(trace.StatusTracer()) |
| 128 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 129 | build.SetupSignals(log, cancel, func() { |
| 130 | trace.Close() |
| 131 | log.Cleanup() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 132 | stat.Finish() |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 133 | }) |
| 134 | |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 135 | buildCtx := build.Context{ContextImpl: &build.ContextImpl{ |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 136 | Context: ctx, |
| 137 | Logger: log, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 138 | Metrics: met, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 139 | Tracer: trace, |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 140 | Writer: c.stdio().Stdout(), |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 141 | Status: stat, |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 142 | }} |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 143 | |
| 144 | config := c.config(buildCtx, args...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 145 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 146 | build.SetupOutDir(buildCtx, config) |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 147 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 148 | logsDir := config.OutDir() |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 149 | if config.Dist() { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 150 | logsDir = filepath.Join(config.DistDir(), "logs") |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 151 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 152 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 153 | 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 Zhang | d50f53b | 2019-01-07 20:26:51 -0800 | [diff] [blame] | 159 | defer met.Dump(filepath.Join(logsDir, "build_metrics")) |
| 160 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 161 | 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 Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 166 | buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano())) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
Dan Willemsen | cae59bc | 2017-07-13 14:27:31 -0700 | [diff] [blame] | 169 | |
| 170 | if executable, err := os.Executable(); err == nil { |
| 171 | trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace")) |
| 172 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 175 | f := build.NewSourceFinder(buildCtx, config) |
| 176 | defer f.Shutdown() |
| 177 | build.FindSources(buildCtx, config, f) |
| 178 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 179 | c.run(buildCtx, config, args, logsDir) |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 182 | func dumpVar(ctx build.Context, config build.Config, args []string, _ string) { |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 183 | 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 Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 232 | func dumpVars(ctx build.Context, config build.Config, args []string, _ string) { |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 233 | 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 298 | } |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 299 | |
| 300 | func 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. |
| 305 | func dumpVarConfig(ctx build.Context, args ...string) build.Config { |
| 306 | return build.NewConfig(ctx) |
| 307 | } |
| 308 | |
| 309 | func make(ctx build.Context, config build.Config, _ []string, logsDir string) { |
| 310 | if config.IsVerbose() { |
| 311 | writer := ctx.Writer |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame^] | 312 | 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 Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 319 | 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. |
| 331 | func 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 | } |