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