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