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