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 | |
| 30 | func runNinja(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 31 | ctx.BeginTrace(metrics.PrimaryNinja, "ninja") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 32 | defer ctx.EndTrace() |
| 33 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 34 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 35 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 36 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 37 | |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 38 | executable := config.PrebuiltBuildTool("ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 39 | args := []string{ |
| 40 | "-d", "keepdepfile", |
Dan Willemsen | 6d3cad9 | 2020-03-12 10:30:35 -0700 | [diff] [blame] | 41 | "-d", "keeprsp", |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 42 | "--frontend_file", fifo, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | args = append(args, config.NinjaArgs()...) |
| 46 | |
| 47 | var parallel int |
Colin Cross | 9016b91 | 2019-11-11 14:57:42 -0800 | [diff] [blame] | 48 | if config.UseRemoteBuild() { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 49 | parallel = config.RemoteParallel() |
| 50 | } else { |
| 51 | parallel = config.Parallel() |
| 52 | } |
| 53 | args = append(args, "-j", strconv.Itoa(parallel)) |
| 54 | if config.keepGoing != 1 { |
| 55 | args = append(args, "-k", strconv.Itoa(config.keepGoing)) |
| 56 | } |
| 57 | |
| 58 | args = append(args, "-f", config.CombinedNinjaFile()) |
| 59 | |
Dan Willemsen | f793933 | 2019-01-05 19:31:32 -0800 | [diff] [blame] | 60 | args = append(args, |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 61 | "-o", "usesphonyoutputs=yes", |
Dan Willemsen | f793933 | 2019-01-05 19:31:32 -0800 | [diff] [blame] | 62 | "-w", "dupbuild=err", |
| 63 | "-w", "missingdepfile=err") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 64 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 65 | cmd := Command(ctx, config, "ninja", executable, args...) |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame] | 66 | cmd.Sandbox = ninjaSandbox |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 67 | if config.HasKatiSuffix() { |
| 68 | cmd.Environment.AppendFromKati(config.KatiEnvFile()) |
| 69 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 70 | |
| 71 | // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been |
| 72 | // used in the past to specify extra ninja arguments. |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 73 | if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok { |
| 74 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 75 | } |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 76 | if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok { |
| 77 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 80 | logPath := filepath.Join(config.OutDir(), ".ninja_log") |
| 81 | ninjaHeartbeatDuration := time.Minute * 5 |
| 82 | if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok { |
| 83 | // For example, "1m" |
| 84 | overrideDuration, err := time.ParseDuration(overrideText) |
| 85 | if err == nil && overrideDuration.Seconds() > 0 { |
| 86 | ninjaHeartbeatDuration = overrideDuration |
| 87 | } |
| 88 | } |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 89 | |
| 90 | // Filter the environment, as ninja does not rebuild files when environment variables change. |
| 91 | // |
| 92 | // Anything listed here must not change the output of rules/actions when the value changes, |
| 93 | // otherwise incremental builds may be unsafe. Vars explicitly set to stable values |
| 94 | // elsewhere in soong_ui are fine. |
| 95 | // |
| 96 | // For the majority of cases, either Soong or the makefiles should be replicating any |
| 97 | // necessary environment variables in the command line of each action that needs it. |
Dan Willemsen | 260db53 | 2020-01-02 20:12:09 -0800 | [diff] [blame] | 98 | if cmd.Environment.IsEnvTrue("ALLOW_NINJA_ENV") { |
| 99 | ctx.Println("Allowing all environment variables during ninja; incremental builds may be unsafe.") |
| 100 | } else { |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 101 | cmd.Environment.Allow(append([]string{ |
| 102 | "ASAN_SYMBOLIZER_PATH", |
| 103 | "HOME", |
| 104 | "JAVA_HOME", |
| 105 | "LANG", |
| 106 | "LC_MESSAGES", |
| 107 | "OUT_DIR", |
| 108 | "PATH", |
| 109 | "PWD", |
| 110 | "PYTHONDONTWRITEBYTECODE", |
| 111 | "TMPDIR", |
| 112 | "USER", |
| 113 | |
| 114 | // TODO: remove these carefully |
Dan Willemsen | 7da0429 | 2020-01-04 13:58:54 -0800 | [diff] [blame] | 115 | "ASAN_OPTIONS", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 116 | "TARGET_BUILD_APPS", |
| 117 | "TARGET_BUILD_VARIANT", |
| 118 | "TARGET_PRODUCT", |
Dan Willemsen | 5cacfe1 | 2020-01-06 12:25:40 -0800 | [diff] [blame] | 119 | // b/147197813 - used by art-check-debug-apex-gen |
| 120 | "EMMA_INSTRUMENT_FRAMEWORK", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 121 | |
| 122 | // Goma -- gomacc may not need all of these |
| 123 | "GOMA_DIR", |
| 124 | "GOMA_DISABLED", |
| 125 | "GOMA_FAIL_FAST", |
| 126 | "GOMA_FALLBACK", |
| 127 | "GOMA_GCE_SERVICE_ACCOUNT", |
| 128 | "GOMA_TMP_DIR", |
| 129 | "GOMA_USE_LOCAL", |
| 130 | |
| 131 | // RBE client |
Ola Rozenfeld | 3992e37 | 2020-03-19 20:04:13 -0400 | [diff] [blame] | 132 | "RBE_compare", |
| 133 | "RBE_exec_root", |
| 134 | "RBE_exec_strategy", |
| 135 | "RBE_invocation_id", |
| 136 | "RBE_log_dir", |
| 137 | "RBE_platform", |
| 138 | "RBE_remote_accept_cache", |
| 139 | "RBE_remote_update_cache", |
| 140 | "RBE_server_address", |
| 141 | // TODO: remove old FLAG_ variables. |
Kousik Kumar | ade12e7 | 2020-01-09 08:52:59 -0800 | [diff] [blame] | 142 | "FLAG_compare", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 143 | "FLAG_exec_root", |
| 144 | "FLAG_exec_strategy", |
| 145 | "FLAG_invocation_id", |
| 146 | "FLAG_log_dir", |
| 147 | "FLAG_platform", |
Kousik Kumar | 0f095e1 | 2020-01-28 10:48:46 -0800 | [diff] [blame] | 148 | "FLAG_remote_accept_cache", |
| 149 | "FLAG_remote_update_cache", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 150 | "FLAG_server_address", |
| 151 | |
| 152 | // ccache settings |
| 153 | "CCACHE_COMPILERCHECK", |
| 154 | "CCACHE_SLOPPINESS", |
| 155 | "CCACHE_BASEDIR", |
| 156 | "CCACHE_CPP2", |
John Eckerdal | 974b0e8 | 2020-02-04 15:59:37 +0100 | [diff] [blame] | 157 | "CCACHE_DIR", |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 158 | }, config.BuildBrokenNinjaUsesEnvVars()...)...) |
| 159 | } |
| 160 | |
| 161 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 162 | cmd.Environment.Set("SHELL", "/bin/bash") |
| 163 | |
| 164 | ctx.Verboseln("Ninja environment: ") |
| 165 | envVars := cmd.Environment.Environ() |
| 166 | sort.Strings(envVars) |
| 167 | for _, envVar := range envVars { |
| 168 | ctx.Verbosef(" %s", envVar) |
| 169 | } |
| 170 | |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 171 | // Poll the ninja log for updates; if it isn't updated enough, then we want to show some diagnostics |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 172 | done := make(chan struct{}) |
| 173 | defer close(done) |
| 174 | ticker := time.NewTicker(ninjaHeartbeatDuration) |
| 175 | defer ticker.Stop() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 176 | checker := &statusChecker{} |
| 177 | go func() { |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 178 | for { |
| 179 | select { |
| 180 | case <-ticker.C: |
| 181 | checker.check(ctx, config, logPath) |
| 182 | case <-done: |
| 183 | return |
| 184 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 185 | } |
| 186 | }() |
| 187 | |
Dan Willemsen | 7f30c07 | 2019-01-02 12:50:49 -0800 | [diff] [blame] | 188 | ctx.Status.Status("Starting ninja...") |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 189 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 190 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 191 | |
| 192 | type statusChecker struct { |
| 193 | prevTime time.Time |
| 194 | } |
| 195 | |
| 196 | func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) { |
| 197 | info, err := os.Stat(pathToCheck) |
| 198 | var newTime time.Time |
| 199 | if err == nil { |
| 200 | newTime = info.ModTime() |
| 201 | } |
| 202 | if newTime == c.prevTime { |
| 203 | // ninja may be stuck |
| 204 | dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime) |
| 205 | } |
| 206 | c.prevTime = newTime |
| 207 | } |
| 208 | |
| 209 | // dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics |
| 210 | func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) { |
| 211 | |
| 212 | ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated) |
| 213 | |
| 214 | // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps" |
| 215 | // So, we try pstree first, and ps second |
| 216 | pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid()) |
| 217 | psCommandText := "ps -ef" |
| 218 | commandText := pstreeCommandText + " || " + psCommandText |
| 219 | |
| 220 | cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText) |
| 221 | output := cmd.CombinedOutputOrFatal() |
| 222 | ctx.Verbose(string(output)) |
| 223 | |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 224 | ctx.Verbosef("done\n") |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 225 | } |