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 build |
| 16 | |
| 17 | import ( |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 18 | "fmt" |
| 19 | "os" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 21 | "sort" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 22 | "strconv" |
| 23 | "strings" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 24 | "time" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 25 | |
Spandan Das | 2db59da | 2023-02-16 18:31:43 +0000 | [diff] [blame] | 26 | "android/soong/shared" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 27 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 28 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
Spandan Das | 2db59da | 2023-02-16 18:31:43 +0000 | [diff] [blame] | 31 | const ( |
| 32 | // File containing the environment state when ninja is executed |
| 33 | ninjaEnvFileName = "ninja.environment" |
Jeongik Cha | 96d6227 | 2023-03-17 01:53:11 +0900 | [diff] [blame^] | 34 | ninjaLogFileName = ".ninja_log" |
Spandan Das | 2db59da | 2023-02-16 18:31:43 +0000 | [diff] [blame] | 35 | ) |
| 36 | |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 37 | func useNinjaBuildLog(ctx Context, config Config, cmd *Cmd) { |
Jeongik Cha | 96d6227 | 2023-03-17 01:53:11 +0900 | [diff] [blame^] | 38 | ninjaLogFile := filepath.Join(config.OutDir(), ninjaLogFileName) |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 39 | data, err := os.ReadFile(ninjaLogFile) |
| 40 | var outputBuilder strings.Builder |
| 41 | if err == nil { |
| 42 | lines := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 43 | // ninja log: <start> <end> <restat> <name> <cmdhash> |
| 44 | // ninja weight list: <name>,<end-start+1> |
| 45 | for _, line := range lines { |
| 46 | if strings.HasPrefix(line, "#") { |
| 47 | continue |
| 48 | } |
| 49 | fields := strings.Split(line, "\t") |
| 50 | path := fields[3] |
| 51 | start, err := strconv.Atoi(fields[0]) |
| 52 | if err != nil { |
| 53 | continue |
| 54 | } |
| 55 | end, err := strconv.Atoi(fields[1]) |
| 56 | if err != nil { |
| 57 | continue |
| 58 | } |
| 59 | outputBuilder.WriteString(path) |
| 60 | outputBuilder.WriteString(",") |
| 61 | outputBuilder.WriteString(strconv.Itoa(end-start+1) + "\n") |
| 62 | } |
Jeongik Cha | 96d6227 | 2023-03-17 01:53:11 +0900 | [diff] [blame^] | 63 | } else { |
| 64 | // If there is no ninja log file, just pass empty ninja weight list. |
| 65 | // Because it is still efficient with critical path calculation logic even without weight. |
| 66 | ctx.Verbosef("There is an error during reading ninja log, so ninja will use empty weight list: %s", err) |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 67 | } |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 68 | |
| 69 | weightListFile := filepath.Join(config.OutDir(), ".ninja_weight_list") |
| 70 | |
| 71 | err = os.WriteFile(weightListFile, []byte(outputBuilder.String()), 0644) |
| 72 | if err == nil { |
| 73 | cmd.Args = append(cmd.Args, "-o", "usesweightlist="+weightListFile) |
| 74 | } else { |
| 75 | ctx.Panicf("Could not write ninja weight list file %s", err) |
| 76 | } |
| 77 | } |
| 78 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 79 | // Constructs and runs the Ninja command line with a restricted set of |
| 80 | // environment variables. It's important to restrict the environment Ninja runs |
| 81 | // for hermeticity reasons, and to avoid spurious rebuilds. |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 82 | func runNinjaForBuild(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 83 | ctx.BeginTrace(metrics.PrimaryNinja, "ninja") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 84 | defer ctx.EndTrace() |
| 85 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 86 | // Sets up the FIFO status updater that reads the Ninja protobuf output, and |
| 87 | // translates it to the soong_ui status output, displaying real-time |
| 88 | // progress of the build. |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 89 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 90 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 91 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 92 | |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 93 | executable := config.PrebuiltBuildTool("ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 94 | args := []string{ |
| 95 | "-d", "keepdepfile", |
Dan Willemsen | 6d3cad9 | 2020-03-12 10:30:35 -0700 | [diff] [blame] | 96 | "-d", "keeprsp", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 97 | "-d", "stats", |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 98 | "--frontend_file", fifo, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | args = append(args, config.NinjaArgs()...) |
| 102 | |
| 103 | var parallel int |
Colin Cross | 9016b91 | 2019-11-11 14:57:42 -0800 | [diff] [blame] | 104 | if config.UseRemoteBuild() { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 105 | parallel = config.RemoteParallel() |
| 106 | } else { |
| 107 | parallel = config.Parallel() |
| 108 | } |
| 109 | args = append(args, "-j", strconv.Itoa(parallel)) |
| 110 | if config.keepGoing != 1 { |
| 111 | args = append(args, "-k", strconv.Itoa(config.keepGoing)) |
| 112 | } |
| 113 | |
| 114 | args = append(args, "-f", config.CombinedNinjaFile()) |
| 115 | |
Dan Willemsen | f793933 | 2019-01-05 19:31:32 -0800 | [diff] [blame] | 116 | args = append(args, |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 117 | "-o", "usesphonyoutputs=yes", |
Dan Willemsen | f793933 | 2019-01-05 19:31:32 -0800 | [diff] [blame] | 118 | "-w", "dupbuild=err", |
Steven Moreland | 28d35a1 | 2022-10-18 00:13:59 +0000 | [diff] [blame] | 119 | "-w", "missingdepfile=err") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 120 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 121 | cmd := Command(ctx, config, "ninja", executable, args...) |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 122 | |
| 123 | // Set up the nsjail sandbox Ninja runs in. |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 124 | cmd.Sandbox = ninjaSandbox |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 125 | if config.HasKatiSuffix() { |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 126 | // Reads and executes a shell script from Kati that sets/unsets the |
| 127 | // environment Ninja runs in. |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 128 | cmd.Environment.AppendFromKati(config.KatiEnvFile()) |
| 129 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 130 | |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 131 | switch config.NinjaWeightListSource() { |
| 132 | case NINJA_LOG: |
| 133 | useNinjaBuildLog(ctx, config, cmd) |
| 134 | case EVENLY_DISTRIBUTED: |
| 135 | // pass empty weight list means ninja considers every tasks's weight as 1(default value). |
| 136 | cmd.Args = append(cmd.Args, "-o", "usesweightlist=/dev/null") |
| 137 | } |
| 138 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 139 | // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been |
| 140 | // used in the past to specify extra ninja arguments. |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 141 | if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok { |
| 142 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 143 | } |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 144 | if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok { |
| 145 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 148 | ninjaHeartbeatDuration := time.Minute * 5 |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 149 | // Get the ninja heartbeat interval from the environment before it's filtered away later. |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 150 | if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok { |
| 151 | // For example, "1m" |
| 152 | overrideDuration, err := time.ParseDuration(overrideText) |
| 153 | if err == nil && overrideDuration.Seconds() > 0 { |
| 154 | ninjaHeartbeatDuration = overrideDuration |
| 155 | } |
| 156 | } |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 157 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 158 | // Filter the environment, as ninja does not rebuild files when environment |
| 159 | // variables change. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 160 | // |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 161 | // Anything listed here must not change the output of rules/actions when the |
| 162 | // value changes, otherwise incremental builds may be unsafe. Vars |
| 163 | // explicitly set to stable values elsewhere in soong_ui are fine. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 164 | // |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 165 | // For the majority of cases, either Soong or the makefiles should be |
| 166 | // replicating any necessary environment variables in the command line of |
| 167 | // each action that needs it. |
Dan Willemsen | 260db53 | 2020-01-02 20:12:09 -0800 | [diff] [blame] | 168 | if cmd.Environment.IsEnvTrue("ALLOW_NINJA_ENV") { |
| 169 | ctx.Println("Allowing all environment variables during ninja; incremental builds may be unsafe.") |
| 170 | } else { |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 171 | cmd.Environment.Allow(append([]string{ |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 172 | // Set the path to a symbolizer (e.g. llvm-symbolizer) so ASAN-based |
| 173 | // tools can symbolize crashes. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 174 | "ASAN_SYMBOLIZER_PATH", |
| 175 | "HOME", |
| 176 | "JAVA_HOME", |
| 177 | "LANG", |
| 178 | "LC_MESSAGES", |
| 179 | "OUT_DIR", |
| 180 | "PATH", |
| 181 | "PWD", |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 182 | // https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 183 | "PYTHONDONTWRITEBYTECODE", |
| 184 | "TMPDIR", |
| 185 | "USER", |
| 186 | |
| 187 | // TODO: remove these carefully |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 188 | // Options for the address sanitizer. |
Dan Willemsen | 7da0429 | 2020-01-04 13:58:54 -0800 | [diff] [blame] | 189 | "ASAN_OPTIONS", |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 190 | // The list of Android app modules to be built in an unbundled manner. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 191 | "TARGET_BUILD_APPS", |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 192 | // The variant of the product being built. e.g. eng, userdebug, debug. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 193 | "TARGET_BUILD_VARIANT", |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 194 | // The product name of the product being built, e.g. aosp_arm, aosp_flame. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 195 | "TARGET_PRODUCT", |
Dan Willemsen | 5cacfe1 | 2020-01-06 12:25:40 -0800 | [diff] [blame] | 196 | // b/147197813 - used by art-check-debug-apex-gen |
| 197 | "EMMA_INSTRUMENT_FRAMEWORK", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 198 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 199 | // RBE client |
Ola Rozenfeld | 3992e37 | 2020-03-19 20:04:13 -0400 | [diff] [blame] | 200 | "RBE_compare", |
andusyu | 240660d | 2022-01-20 14:00:58 -0500 | [diff] [blame] | 201 | "RBE_num_local_reruns", |
| 202 | "RBE_num_remote_reruns", |
Ola Rozenfeld | 3992e37 | 2020-03-19 20:04:13 -0400 | [diff] [blame] | 203 | "RBE_exec_root", |
| 204 | "RBE_exec_strategy", |
| 205 | "RBE_invocation_id", |
| 206 | "RBE_log_dir", |
Kousik Kumar | c3a22d8 | 2021-03-17 14:19:27 -0400 | [diff] [blame] | 207 | "RBE_num_retries_if_mismatched", |
Ola Rozenfeld | 3992e37 | 2020-03-19 20:04:13 -0400 | [diff] [blame] | 208 | "RBE_platform", |
| 209 | "RBE_remote_accept_cache", |
| 210 | "RBE_remote_update_cache", |
| 211 | "RBE_server_address", |
| 212 | // TODO: remove old FLAG_ variables. |
Kousik Kumar | ade12e7 | 2020-01-09 08:52:59 -0800 | [diff] [blame] | 213 | "FLAG_compare", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 214 | "FLAG_exec_root", |
| 215 | "FLAG_exec_strategy", |
| 216 | "FLAG_invocation_id", |
| 217 | "FLAG_log_dir", |
| 218 | "FLAG_platform", |
Kousik Kumar | 0f095e1 | 2020-01-28 10:48:46 -0800 | [diff] [blame] | 219 | "FLAG_remote_accept_cache", |
| 220 | "FLAG_remote_update_cache", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 221 | "FLAG_server_address", |
| 222 | |
| 223 | // ccache settings |
| 224 | "CCACHE_COMPILERCHECK", |
| 225 | "CCACHE_SLOPPINESS", |
| 226 | "CCACHE_BASEDIR", |
| 227 | "CCACHE_CPP2", |
John Eckerdal | 974b0e8 | 2020-02-04 15:59:37 +0100 | [diff] [blame] | 228 | "CCACHE_DIR", |
Yi Kong | 6adf258 | 2022-04-17 15:01:06 +0800 | [diff] [blame] | 229 | |
| 230 | // LLVM compiler wrapper options |
| 231 | "TOOLCHAIN_RUSAGE_OUTPUT", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 232 | }, config.BuildBrokenNinjaUsesEnvVars()...)...) |
| 233 | } |
| 234 | |
| 235 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 236 | cmd.Environment.Set("SHELL", "/bin/bash") |
| 237 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 238 | // Print the environment variables that Ninja is operating in. |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 239 | ctx.Verboseln("Ninja environment: ") |
| 240 | envVars := cmd.Environment.Environ() |
| 241 | sort.Strings(envVars) |
| 242 | for _, envVar := range envVars { |
| 243 | ctx.Verbosef(" %s", envVar) |
| 244 | } |
| 245 | |
Spandan Das | 2db59da | 2023-02-16 18:31:43 +0000 | [diff] [blame] | 246 | // Write the env vars available during ninja execution to a file |
| 247 | ninjaEnvVars := cmd.Environment.AsMap() |
| 248 | data, err := shared.EnvFileContents(ninjaEnvVars) |
| 249 | if err != nil { |
| 250 | ctx.Panicf("Could not parse environment variables for ninja run %s", err) |
| 251 | } |
| 252 | // Write the file in every single run. This is fine because |
| 253 | // 1. It is not a dep of Soong analysis, so will not retrigger Soong analysis. |
| 254 | // 2. Is is fairly lightweight (~1Kb) |
| 255 | ninjaEnvVarsFile := shared.JoinPath(config.SoongOutDir(), ninjaEnvFileName) |
| 256 | err = os.WriteFile(ninjaEnvVarsFile, data, 0666) |
| 257 | if err != nil { |
| 258 | ctx.Panicf("Could not write ninja environment file %s", err) |
| 259 | } |
| 260 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 261 | // Poll the Ninja log for updates regularly based on the heartbeat |
| 262 | // frequency. If it isn't updated enough, then we want to surface the |
| 263 | // possibility that Ninja is stuck, to the user. |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 264 | done := make(chan struct{}) |
| 265 | defer close(done) |
| 266 | ticker := time.NewTicker(ninjaHeartbeatDuration) |
| 267 | defer ticker.Stop() |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 268 | ninjaChecker := &ninjaStucknessChecker{ |
Jeongik Cha | 96d6227 | 2023-03-17 01:53:11 +0900 | [diff] [blame^] | 269 | logPath: filepath.Join(config.OutDir(), ninjaLogFileName), |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 270 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 271 | go func() { |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 272 | for { |
| 273 | select { |
| 274 | case <-ticker.C: |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 275 | ninjaChecker.check(ctx, config) |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 276 | case <-done: |
| 277 | return |
| 278 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 279 | } |
| 280 | }() |
| 281 | |
Dan Willemsen | 7f30c07 | 2019-01-02 12:50:49 -0800 | [diff] [blame] | 282 | ctx.Status.Status("Starting ninja...") |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 283 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 284 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 285 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 286 | // A simple struct for checking if Ninja gets stuck, using timestamps. |
| 287 | type ninjaStucknessChecker struct { |
| 288 | logPath string |
| 289 | prevModTime time.Time |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 290 | } |
| 291 | |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 292 | // Check that a file has been modified since the last time it was checked. If |
| 293 | // the mod time hasn't changed, then assume that Ninja got stuck, and print |
| 294 | // diagnostics for debugging. |
| 295 | func (c *ninjaStucknessChecker) check(ctx Context, config Config) { |
| 296 | info, err := os.Stat(c.logPath) |
| 297 | var newModTime time.Time |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 298 | if err == nil { |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 299 | newModTime = info.ModTime() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 300 | } |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 301 | if newModTime == c.prevModTime { |
| 302 | // The Ninja file hasn't been modified since the last time it was |
| 303 | // checked, so Ninja could be stuck. Output some diagnostics. |
| 304 | ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", c.logPath, newModTime) |
| 305 | |
| 306 | // The "pstree" command doesn't exist on Mac, but "pstree" on Linux |
| 307 | // gives more convenient output than "ps" So, we try pstree first, and |
| 308 | // ps second |
| 309 | commandText := fmt.Sprintf("pstree -pal %v || ps -ef", os.Getpid()) |
| 310 | |
| 311 | cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText) |
| 312 | output := cmd.CombinedOutputOrFatal() |
| 313 | ctx.Verbose(string(output)) |
| 314 | |
| 315 | ctx.Verbosef("done\n") |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 316 | } |
Jingwen Chen | 9d1cb49 | 2020-11-17 06:52:28 -0500 | [diff] [blame] | 317 | c.prevModTime = newModTime |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 318 | } |