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