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 { |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 38 | // The flag name (must have double dashes). |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 39 | flag string |
| 40 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 41 | // Description for the flag (to display when running help). |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 42 | description string |
| 43 | |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 44 | // Stream the build status output into the simple terminal mode. |
| 45 | simpleOutput bool |
Colin Cross | c0b9f6b | 2019-09-23 12:44:54 -0700 | [diff] [blame] | 46 | |
| 47 | // Sets a prefix string to use for filenames of log files. |
| 48 | logsPrefix string |
| 49 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 50 | // Creates the build configuration based on the args and build context. |
| 51 | config func(ctx build.Context, args ...string) build.Config |
| 52 | |
| 53 | // Returns what type of IO redirection this Command requires. |
| 54 | stdio func() terminal.StdioInterface |
| 55 | |
| 56 | // run the command |
| 57 | run func(ctx build.Context, config build.Config, args []string, logsDir string) |
| 58 | } |
| 59 | |
| 60 | const makeModeFlagName = "--make-mode" |
| 61 | |
| 62 | // list of supported commands (flags) supported by soong ui |
| 63 | var commands []command = []command{ |
| 64 | { |
| 65 | flag: makeModeFlagName, |
| 66 | description: "build the modules by the target name (i.e. soong_docs)", |
| 67 | config: func(ctx build.Context, args ...string) build.Config { |
| 68 | return build.NewConfig(ctx, args...) |
| 69 | }, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 70 | stdio: stdio, |
| 71 | run: make, |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 72 | }, { |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 73 | flag: "--dumpvar-mode", |
| 74 | description: "print the value of the legacy make variable VAR to stdout", |
| 75 | simpleOutput: true, |
| 76 | logsPrefix: "dumpvars-", |
| 77 | config: dumpVarConfig, |
| 78 | stdio: customStdio, |
| 79 | run: dumpVar, |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 80 | }, { |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 81 | flag: "--dumpvars-mode", |
| 82 | description: "dump the values of one or more legacy make variables, in shell syntax", |
| 83 | simpleOutput: true, |
| 84 | logsPrefix: "dumpvars-", |
| 85 | config: dumpVarConfig, |
| 86 | stdio: customStdio, |
| 87 | run: dumpVars, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 88 | }, { |
| 89 | flag: "--build-mode", |
| 90 | description: "build modules based on the specified build action", |
| 91 | config: buildActionConfig, |
| 92 | stdio: stdio, |
| 93 | run: make, |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 94 | }, |
| 95 | } |
| 96 | |
| 97 | // indexList returns the index of first found s. -1 is return if s is not |
| 98 | // found. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 99 | func indexList(s string, list []string) int { |
| 100 | for i, l := range list { |
| 101 | if l == s { |
| 102 | return i |
| 103 | } |
| 104 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 105 | return -1 |
| 106 | } |
| 107 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 108 | // 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] | 109 | func inList(s string, list []string) bool { |
| 110 | return indexList(s, list) != -1 |
| 111 | } |
| 112 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 113 | // Main execution of soong_ui. The command format is as follows: |
| 114 | // |
| 115 | // soong_ui <command> [<arg 1> <arg 2> ... <arg n>] |
| 116 | // |
| 117 | // Command is the type of soong_ui execution. Only one type of |
| 118 | // execution is specified. The args are specific to the command. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 119 | func main() { |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 120 | buildStarted := time.Now() |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 121 | |
Liz Kammer | 0e7993e | 2020-10-15 11:07:13 -0700 | [diff] [blame] | 122 | c, args, err := getCommand(os.Args) |
| 123 | if err != nil { |
| 124 | fmt.Fprintf(os.Stderr, "Error parsing `soong` args: %s.\n", err) |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 125 | os.Exit(1) |
Dan Willemsen | c35b381 | 2018-07-16 19:59:10 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 128 | // Create a terminal output that mimics Ninja's. |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 129 | output := terminal.NewStatusOutput(c.stdio().Stdout(), os.Getenv("NINJA_STATUS"), c.simpleOutput, |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 130 | build.OsEnvironment().IsEnvTrue("ANDROID_QUIET_BUILD")) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 131 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 132 | // Attach a new logger instance to the terminal output. |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 133 | log := logger.New(output) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 134 | defer log.Cleanup() |
| 135 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 136 | // Create a context to simplify the program termination process. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 137 | ctx, cancel := context.WithCancel(context.Background()) |
| 138 | defer cancel() |
| 139 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 140 | // Create a new trace file writer, making it log events to the log instance. |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 141 | trace := tracer.New(log) |
| 142 | defer trace.Close() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 143 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 144 | // Create and start a new metric record. |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 145 | met := metrics.New() |
Patrice Arruda | 73c790f | 2020-07-13 23:01:18 +0000 | [diff] [blame] | 146 | met.SetBuildDateTime(buildStarted) |
Patrice Arruda | e92c30d | 2020-10-29 11:01:32 -0700 | [diff] [blame] | 147 | met.SetBuildCommand(os.Args) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 148 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 149 | // Create a new Status instance, which manages action counts and event output channels. |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 150 | stat := &status.Status{} |
| 151 | defer stat.Finish() |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 152 | // Hook up the terminal output and tracer to Status. |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 153 | stat.AddOutput(output) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 154 | stat.AddOutput(trace.StatusTracer()) |
| 155 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 156 | // Set up a cleanup procedure in case the normal termination process doesn't work. |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 157 | build.SetupSignals(log, cancel, func() { |
| 158 | trace.Close() |
| 159 | log.Cleanup() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 160 | stat.Finish() |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 161 | }) |
| 162 | |
Dan Willemsen | 59339a2 | 2018-07-22 21:18:45 -0700 | [diff] [blame] | 163 | buildCtx := build.Context{ContextImpl: &build.ContextImpl{ |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 164 | Context: ctx, |
| 165 | Logger: log, |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 166 | Metrics: met, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 167 | Tracer: trace, |
Colin Cross | e0df1a3 | 2019-06-09 19:40:08 -0700 | [diff] [blame] | 168 | Writer: output, |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 169 | Status: stat, |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 170 | }} |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 171 | |
| 172 | config := c.config(buildCtx, args...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 173 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 174 | build.SetupOutDir(buildCtx, config) |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 175 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 176 | // Set up files to be outputted in the log directory. |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 177 | logsDir := config.OutDir() |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 178 | if config.Dist() { |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 179 | logsDir = filepath.Join(config.DistDir(), "logs") |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 180 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 181 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 182 | buildErrorFile := filepath.Join(logsDir, c.logsPrefix+"build_error") |
| 183 | rbeMetricsFile := filepath.Join(logsDir, c.logsPrefix+"rbe_metrics.pb") |
| 184 | soongMetricsFile := filepath.Join(logsDir, c.logsPrefix+"soong_metrics") |
Patrice Arruda | f445ba1 | 2020-07-28 17:49:01 +0000 | [diff] [blame] | 185 | defer build.UploadMetrics(buildCtx, config, c.simpleOutput, buildStarted, buildErrorFile, rbeMetricsFile, soongMetricsFile) |
Kousik Kumar | a0a44a8 | 2020-10-08 02:33:29 -0400 | [diff] [blame] | 186 | build.PrintOutDirWarning(buildCtx, config) |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 187 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 188 | os.MkdirAll(logsDir, 0777) |
Colin Cross | c0b9f6b | 2019-09-23 12:44:54 -0700 | [diff] [blame] | 189 | log.SetOutput(filepath.Join(logsDir, c.logsPrefix+"soong.log")) |
| 190 | trace.SetOutput(filepath.Join(logsDir, c.logsPrefix+"build.trace")) |
| 191 | stat.AddOutput(status.NewVerboseLog(log, filepath.Join(logsDir, c.logsPrefix+"verbose.log"))) |
| 192 | stat.AddOutput(status.NewErrorLog(log, filepath.Join(logsDir, c.logsPrefix+"error.log"))) |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 193 | stat.AddOutput(status.NewProtoErrorLog(log, buildErrorFile)) |
Colin Cross | 7b62453 | 2019-06-21 15:08:30 -0700 | [diff] [blame] | 194 | stat.AddOutput(status.NewCriticalPath(log)) |
Patrice Arruda | 74b4399 | 2020-03-11 08:21:05 -0700 | [diff] [blame] | 195 | stat.AddOutput(status.NewBuildProgressLog(log, filepath.Join(logsDir, c.logsPrefix+"build_progress.pb"))) |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 196 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 197 | buildCtx.Verbosef("Detected %.3v GB total RAM", float32(config.TotalRAM())/(1024*1024*1024)) |
| 198 | buildCtx.Verbosef("Parallelism (local/remote/highmem): %v/%v/%v", |
| 199 | config.Parallel(), config.RemoteParallel(), config.HighmemParallel()) |
| 200 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 201 | defer met.Dump(soongMetricsFile) |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 202 | defer build.DumpRBEMetrics(buildCtx, config, rbeMetricsFile) |
Nan Zhang | d50f53b | 2019-01-07 20:26:51 -0800 | [diff] [blame] | 203 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 204 | // Read the time at the starting point. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 205 | if start, ok := os.LookupEnv("TRACE_BEGIN_SOONG"); ok { |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 206 | // soong_ui.bash uses the date command's %N (nanosec) flag when getting the start time, |
| 207 | // which Darwin doesn't support. Check if it was executed properly before parsing the value. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 208 | if !strings.HasSuffix(start, "N") { |
| 209 | if start_time, err := strconv.ParseUint(start, 10, 64); err == nil { |
| 210 | log.Verbosef("Took %dms to start up.", |
| 211 | time.Since(time.Unix(0, int64(start_time))).Nanoseconds()/time.Millisecond.Nanoseconds()) |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 212 | buildCtx.CompleteTrace(metrics.RunSetupTool, "startup", start_time, uint64(time.Now().UnixNano())) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 213 | } |
| 214 | } |
Dan Willemsen | cae59bc | 2017-07-13 14:27:31 -0700 | [diff] [blame] | 215 | |
| 216 | if executable, err := os.Executable(); err == nil { |
| 217 | trace.ImportMicrofactoryLog(filepath.Join(filepath.Dir(executable), "."+filepath.Base(executable)+".trace")) |
| 218 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Dan Willemsen | 6b783c8 | 2019-03-08 11:42:28 -0800 | [diff] [blame] | 221 | // Fix up the source tree due to a repo bug where it doesn't remove |
| 222 | // linkfiles that have been removed |
| 223 | fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.bp") |
| 224 | fixBadDanglingLink(buildCtx, "hardware/qcom/sdm710/Android.mk") |
| 225 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 226 | // Create a source finder. |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 227 | f := build.NewSourceFinder(buildCtx, config) |
| 228 | defer f.Shutdown() |
| 229 | build.FindSources(buildCtx, config, f) |
| 230 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 231 | c.run(buildCtx, config, args, logsDir) |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 232 | } |
| 233 | |
Dan Willemsen | 6b783c8 | 2019-03-08 11:42:28 -0800 | [diff] [blame] | 234 | func fixBadDanglingLink(ctx build.Context, name string) { |
| 235 | _, err := os.Lstat(name) |
| 236 | if err != nil { |
| 237 | return |
| 238 | } |
| 239 | _, err = os.Stat(name) |
| 240 | if os.IsNotExist(err) { |
| 241 | err = os.Remove(name) |
| 242 | if err != nil { |
| 243 | ctx.Fatalf("Failed to remove dangling link %q: %v", name, err) |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 248 | func dumpVar(ctx build.Context, config build.Config, args []string, _ string) { |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 249 | flags := flag.NewFlagSet("dumpvar", flag.ExitOnError) |
| 250 | flags.Usage = func() { |
Patrice Arruda | db4c2f1 | 2019-06-17 17:27:09 -0700 | [diff] [blame] | 251 | fmt.Fprintf(ctx.Writer, "usage: %s --dumpvar-mode [--abs] <VAR>\n\n", os.Args[0]) |
| 252 | fmt.Fprintln(ctx.Writer, "In dumpvar mode, print the value of the legacy make variable VAR to stdout") |
| 253 | fmt.Fprintln(ctx.Writer, "") |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 254 | |
Patrice Arruda | db4c2f1 | 2019-06-17 17:27:09 -0700 | [diff] [blame] | 255 | fmt.Fprintln(ctx.Writer, "'report_config' is a special case that prints the human-readable config banner") |
| 256 | fmt.Fprintln(ctx.Writer, "from the beginning of the build.") |
| 257 | fmt.Fprintln(ctx.Writer, "") |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 258 | flags.PrintDefaults() |
| 259 | } |
| 260 | abs := flags.Bool("abs", false, "Print the absolute path of the value") |
| 261 | flags.Parse(args) |
| 262 | |
| 263 | if flags.NArg() != 1 { |
| 264 | flags.Usage() |
| 265 | os.Exit(1) |
| 266 | } |
| 267 | |
| 268 | varName := flags.Arg(0) |
| 269 | if varName == "report_config" { |
| 270 | varData, err := build.DumpMakeVars(ctx, config, nil, build.BannerVars) |
| 271 | if err != nil { |
| 272 | ctx.Fatal(err) |
| 273 | } |
| 274 | |
| 275 | fmt.Println(build.Banner(varData)) |
| 276 | } else { |
| 277 | varData, err := build.DumpMakeVars(ctx, config, nil, []string{varName}) |
| 278 | if err != nil { |
| 279 | ctx.Fatal(err) |
| 280 | } |
| 281 | |
| 282 | if *abs { |
| 283 | var res []string |
| 284 | for _, path := range strings.Fields(varData[varName]) { |
| 285 | if abs, err := filepath.Abs(path); err == nil { |
| 286 | res = append(res, abs) |
| 287 | } else { |
| 288 | ctx.Fatalln("Failed to get absolute path of", path, err) |
| 289 | } |
| 290 | } |
| 291 | fmt.Println(strings.Join(res, " ")) |
| 292 | } else { |
| 293 | fmt.Println(varData[varName]) |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 298 | func dumpVars(ctx build.Context, config build.Config, args []string, _ string) { |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 299 | flags := flag.NewFlagSet("dumpvars", flag.ExitOnError) |
| 300 | flags.Usage = func() { |
Patrice Arruda | db4c2f1 | 2019-06-17 17:27:09 -0700 | [diff] [blame] | 301 | fmt.Fprintf(ctx.Writer, "usage: %s --dumpvars-mode [--vars=\"VAR VAR ...\"]\n\n", os.Args[0]) |
| 302 | fmt.Fprintln(ctx.Writer, "In dumpvars mode, dump the values of one or more legacy make variables, in") |
| 303 | fmt.Fprintln(ctx.Writer, "shell syntax. The resulting output may be sourced directly into a shell to") |
| 304 | fmt.Fprintln(ctx.Writer, "set corresponding shell variables.") |
| 305 | fmt.Fprintln(ctx.Writer, "") |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 306 | |
Patrice Arruda | db4c2f1 | 2019-06-17 17:27:09 -0700 | [diff] [blame] | 307 | fmt.Fprintln(ctx.Writer, "'report_config' is a special case that dumps a variable containing the") |
| 308 | fmt.Fprintln(ctx.Writer, "human-readable config banner from the beginning of the build.") |
| 309 | fmt.Fprintln(ctx.Writer, "") |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 310 | flags.PrintDefaults() |
| 311 | } |
| 312 | |
| 313 | varsStr := flags.String("vars", "", "Space-separated list of variables to dump") |
| 314 | absVarsStr := flags.String("abs-vars", "", "Space-separated list of variables to dump (using absolute paths)") |
| 315 | |
| 316 | varPrefix := flags.String("var-prefix", "", "String to prepend to all variable names when dumping") |
| 317 | absVarPrefix := flags.String("abs-var-prefix", "", "String to prepent to all absolute path variable names when dumping") |
| 318 | |
| 319 | flags.Parse(args) |
| 320 | |
| 321 | if flags.NArg() != 0 { |
| 322 | flags.Usage() |
| 323 | os.Exit(1) |
| 324 | } |
| 325 | |
| 326 | vars := strings.Fields(*varsStr) |
| 327 | absVars := strings.Fields(*absVarsStr) |
| 328 | |
| 329 | allVars := append([]string{}, vars...) |
| 330 | allVars = append(allVars, absVars...) |
| 331 | |
| 332 | if i := indexList("report_config", allVars); i != -1 { |
| 333 | allVars = append(allVars[:i], allVars[i+1:]...) |
| 334 | allVars = append(allVars, build.BannerVars...) |
| 335 | } |
| 336 | |
| 337 | if len(allVars) == 0 { |
| 338 | return |
| 339 | } |
| 340 | |
| 341 | varData, err := build.DumpMakeVars(ctx, config, nil, allVars) |
| 342 | if err != nil { |
| 343 | ctx.Fatal(err) |
| 344 | } |
| 345 | |
| 346 | for _, name := range vars { |
| 347 | if name == "report_config" { |
| 348 | fmt.Printf("%sreport_config='%s'\n", *varPrefix, build.Banner(varData)) |
| 349 | } else { |
| 350 | fmt.Printf("%s%s='%s'\n", *varPrefix, name, varData[name]) |
| 351 | } |
| 352 | } |
| 353 | for _, name := range absVars { |
| 354 | var res []string |
| 355 | for _, path := range strings.Fields(varData[name]) { |
| 356 | abs, err := filepath.Abs(path) |
| 357 | if err != nil { |
| 358 | ctx.Fatalln("Failed to get absolute path of", path, err) |
| 359 | } |
| 360 | res = append(res, abs) |
| 361 | } |
| 362 | fmt.Printf("%s%s='%s'\n", *absVarPrefix, name, strings.Join(res, " ")) |
| 363 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 364 | } |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 365 | |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 366 | func stdio() terminal.StdioInterface { |
| 367 | return terminal.StdioImpl{} |
| 368 | } |
| 369 | |
Jaewoong Jung | 9f98d3f | 2020-11-17 18:20:14 -0800 | [diff] [blame] | 370 | // dumpvar and dumpvars use stdout to output variable values, so use stderr instead of stdout when |
| 371 | // reporting events to keep stdout clean from noise. |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 372 | func customStdio() terminal.StdioInterface { |
| 373 | return terminal.NewCustomStdio(os.Stdin, os.Stderr, os.Stderr) |
| 374 | } |
| 375 | |
| 376 | // dumpVarConfig does not require any arguments to be parsed by the NewConfig. |
| 377 | func dumpVarConfig(ctx build.Context, args ...string) build.Config { |
| 378 | return build.NewConfig(ctx) |
| 379 | } |
| 380 | |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 381 | func buildActionConfig(ctx build.Context, args ...string) build.Config { |
| 382 | flags := flag.NewFlagSet("build-mode", flag.ContinueOnError) |
| 383 | flags.Usage = func() { |
| 384 | fmt.Fprintf(ctx.Writer, "usage: %s --build-mode --dir=<path> <build action> [<build arg 1> <build arg 2> ...]\n\n", os.Args[0]) |
| 385 | fmt.Fprintln(ctx.Writer, "In build mode, build the set of modules based on the specified build") |
| 386 | fmt.Fprintln(ctx.Writer, "action. The --dir flag is required to determine what is needed to") |
| 387 | fmt.Fprintln(ctx.Writer, "build in the source tree based on the build action. See below for") |
| 388 | fmt.Fprintln(ctx.Writer, "the list of acceptable build action flags.") |
| 389 | fmt.Fprintln(ctx.Writer, "") |
| 390 | flags.PrintDefaults() |
| 391 | } |
| 392 | |
| 393 | buildActionFlags := []struct { |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 394 | name string |
| 395 | description string |
| 396 | action build.BuildAction |
| 397 | set bool |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 398 | }{{ |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 399 | name: "all-modules", |
| 400 | description: "Build action: build from the top of the source tree.", |
| 401 | action: build.BUILD_MODULES, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 402 | }, { |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 403 | // This is redirecting to mma build command behaviour. Once it has soaked for a |
| 404 | // while, the build command is deleted from here once it has been removed from the |
| 405 | // envsetup.sh. |
| 406 | name: "modules-in-a-dir-no-deps", |
| 407 | description: "Build action: builds all of the modules in the current directory without their dependencies.", |
| 408 | action: build.BUILD_MODULES_IN_A_DIRECTORY, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 409 | }, { |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 410 | // This is redirecting to mmma build command behaviour. Once it has soaked for a |
| 411 | // while, the build command is deleted from here once it has been removed from the |
| 412 | // envsetup.sh. |
| 413 | name: "modules-in-dirs-no-deps", |
| 414 | description: "Build action: builds all of the modules in the supplied directories without their dependencies.", |
| 415 | action: build.BUILD_MODULES_IN_DIRECTORIES, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 416 | }, { |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 417 | name: "modules-in-a-dir", |
| 418 | description: "Build action: builds all of the modules in the current directory and their dependencies.", |
| 419 | action: build.BUILD_MODULES_IN_A_DIRECTORY, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 420 | }, { |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 421 | name: "modules-in-dirs", |
| 422 | description: "Build action: builds all of the modules in the supplied directories and their dependencies.", |
| 423 | action: build.BUILD_MODULES_IN_DIRECTORIES, |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 424 | }} |
| 425 | for i, flag := range buildActionFlags { |
| 426 | flags.BoolVar(&buildActionFlags[i].set, flag.name, false, flag.description) |
| 427 | } |
| 428 | dir := flags.String("dir", "", "Directory of the executed build command.") |
| 429 | |
| 430 | // Only interested in the first two args which defines the build action and the directory. |
| 431 | // The remaining arguments are passed down to the config. |
| 432 | const numBuildActionFlags = 2 |
| 433 | if len(args) < numBuildActionFlags { |
| 434 | flags.Usage() |
| 435 | ctx.Fatalln("Improper build action arguments.") |
| 436 | } |
| 437 | flags.Parse(args[0:numBuildActionFlags]) |
| 438 | |
| 439 | // The next block of code is to validate that exactly one build action is set and the dir flag |
| 440 | // is specified. |
| 441 | buildActionCount := 0 |
| 442 | var buildAction build.BuildAction |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 443 | for _, flag := range buildActionFlags { |
| 444 | if flag.set { |
| 445 | buildActionCount++ |
| 446 | buildAction = flag.action |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | if buildActionCount != 1 { |
| 450 | ctx.Fatalln("Build action not defined.") |
| 451 | } |
| 452 | if *dir == "" { |
| 453 | ctx.Fatalln("-dir not specified.") |
| 454 | } |
| 455 | |
| 456 | // Remove the build action flags from the args as they are not recognized by the config. |
| 457 | args = args[numBuildActionFlags:] |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 458 | return build.NewBuildActionConfig(buildAction, *dir, ctx, args...) |
Patrice Arruda | b7b2282 | 2019-05-21 17:46:23 -0700 | [diff] [blame] | 459 | } |
| 460 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 461 | func make(ctx build.Context, config build.Config, _ []string, logsDir string) { |
| 462 | if config.IsVerbose() { |
| 463 | writer := ctx.Writer |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 464 | fmt.Fprintln(writer, "! The argument `showcommands` is no longer supported.") |
| 465 | fmt.Fprintln(writer, "! Instead, the verbose log is always written to a compressed file in the output dir:") |
| 466 | fmt.Fprintln(writer, "!") |
| 467 | fmt.Fprintf(writer, "! gzip -cd %s/verbose.log.gz | less -R\n", logsDir) |
| 468 | fmt.Fprintln(writer, "!") |
| 469 | fmt.Fprintln(writer, "! Older versions are saved in verbose.log.#.gz files") |
| 470 | fmt.Fprintln(writer, "") |
Dan Willemsen | c636083 | 2019-07-25 14:07:36 -0700 | [diff] [blame] | 471 | select { |
| 472 | case <-time.After(5 * time.Second): |
| 473 | case <-ctx.Done(): |
| 474 | return |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if _, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok { |
| 479 | writer := ctx.Writer |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 480 | fmt.Fprintln(writer, "! The variable `ONE_SHOT_MAKEFILE` is obsolete.") |
Dan Willemsen | c636083 | 2019-07-25 14:07:36 -0700 | [diff] [blame] | 481 | fmt.Fprintln(writer, "!") |
| 482 | fmt.Fprintln(writer, "! If you're using `mm`, you'll need to run `source build/envsetup.sh` to update.") |
| 483 | fmt.Fprintln(writer, "!") |
| 484 | fmt.Fprintln(writer, "! Otherwise, either specify a module name with m, or use mma / MODULES-IN-...") |
| 485 | fmt.Fprintln(writer, "") |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 486 | ctx.Fatal("done") |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Patrice Arruda | 0c1c456 | 2020-11-11 13:01:25 -0800 | [diff] [blame] | 489 | toBuild := build.BuildAll |
| 490 | if config.UseBazel() { |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 491 | toBuild = build.BuildAllWithBazel |
Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 494 | if config.Checkbuild() { |
| 495 | toBuild |= build.RunBuildTests |
| 496 | } |
| 497 | build.Build(ctx, config, toBuild) |
| 498 | } |
| 499 | |
| 500 | // getCommand finds the appropriate command based on args[1] flag. args[0] |
| 501 | // is the soong_ui filename. |
Liz Kammer | 0e7993e | 2020-10-15 11:07:13 -0700 | [diff] [blame] | 502 | func getCommand(args []string) (*command, []string, error) { |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 503 | if len(args) < 2 { |
Liz Kammer | 0e7993e | 2020-10-15 11:07:13 -0700 | [diff] [blame] | 504 | return nil, nil, fmt.Errorf("Too few arguments: %q", args) |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | for _, c := range commands { |
| 508 | if c.flag == args[1] { |
Liz Kammer | 0e7993e | 2020-10-15 11:07:13 -0700 | [diff] [blame] | 509 | return &c, args[2:], nil |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 510 | } |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 511 | } |
| 512 | |
| 513 | // command not found |
Liz Kammer | 0e7993e | 2020-10-15 11:07:13 -0700 | [diff] [blame] | 514 | return nil, nil, fmt.Errorf("Command not found: %q", args) |
Patrice Arruda | a5c2542 | 2019-04-09 18:49:49 -0700 | [diff] [blame] | 515 | } |