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