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