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