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