blob: 5357d44cc4d3e56eac06009930031e68e99d7581 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
Jeff Gaston809cc6f2017-05-25 15:44:36 -070018 "fmt"
19 "os"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070020 "path/filepath"
Dan Willemsene3336352020-01-02 19:10:38 -080021 "sort"
Dan Willemsen1e704462016-08-21 15:17:17 -070022 "strconv"
23 "strings"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070024 "time"
Dan Willemsenb82471a2018-05-17 16:37:09 -070025
Nan Zhang17f27672018-12-12 16:01:49 -080026 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070027 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070028)
29
30func runNinja(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -080031 ctx.BeginTrace(metrics.PrimaryNinja, "ninja")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070032 defer ctx.EndTrace()
33
Dan Willemsenb82471a2018-05-17 16:37:09 -070034 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -070035 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
36 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -070037
Dan Willemsenf173d592017-04-27 14:28:00 -070038 executable := config.PrebuiltBuildTool("ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -070039 args := []string{
40 "-d", "keepdepfile",
Dan Willemsen02736672018-07-17 17:54:31 -070041 "--frontend_file", fifo,
Dan Willemsen1e704462016-08-21 15:17:17 -070042 }
43
44 args = append(args, config.NinjaArgs()...)
45
46 var parallel int
Colin Cross9016b912019-11-11 14:57:42 -080047 if config.UseRemoteBuild() {
Dan Willemsen1e704462016-08-21 15:17:17 -070048 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 Willemsenf7939332019-01-05 19:31:32 -080059 args = append(args,
60 "-w", "dupbuild=err",
61 "-w", "missingdepfile=err")
Dan Willemsen1e704462016-08-21 15:17:17 -070062
Dan Willemsen269a8c72017-05-03 17:15:47 -070063 cmd := Command(ctx, config, "ninja", executable, args...)
Dan Willemsen63663c62019-01-02 12:24:44 -080064 cmd.Sandbox = ninjaSandbox
Dan Willemsene0879fc2017-08-04 15:06:27 -070065 if config.HasKatiSuffix() {
66 cmd.Environment.AppendFromKati(config.KatiEnvFile())
67 }
Dan Willemsen1e704462016-08-21 15:17:17 -070068
69 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
70 // used in the past to specify extra ninja arguments.
Dan Willemsen269a8c72017-05-03 17:15:47 -070071 if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {
72 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070073 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070074 if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok {
75 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070076 }
77
Jeff Gaston809cc6f2017-05-25 15:44:36 -070078 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 Willemsene3336352020-01-02 19:10:38 -080087
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.
96 if cmd.Environment.IsFalse("ALLOW_NINJA_ENV") {
97 cmd.Environment.Allow(append([]string{
98 "ASAN_SYMBOLIZER_PATH",
99 "HOME",
100 "JAVA_HOME",
101 "LANG",
102 "LC_MESSAGES",
103 "OUT_DIR",
104 "PATH",
105 "PWD",
106 "PYTHONDONTWRITEBYTECODE",
107 "TMPDIR",
108 "USER",
109
110 // TODO: remove these carefully
111 "TARGET_BUILD_APPS",
112 "TARGET_BUILD_VARIANT",
113 "TARGET_PRODUCT",
114
115 // Goma -- gomacc may not need all of these
116 "GOMA_DIR",
117 "GOMA_DISABLED",
118 "GOMA_FAIL_FAST",
119 "GOMA_FALLBACK",
120 "GOMA_GCE_SERVICE_ACCOUNT",
121 "GOMA_TMP_DIR",
122 "GOMA_USE_LOCAL",
123
124 // RBE client
125 "FLAG_exec_root",
126 "FLAG_exec_strategy",
127 "FLAG_invocation_id",
128 "FLAG_log_dir",
129 "FLAG_platform",
130 "FLAG_server_address",
131
132 // ccache settings
133 "CCACHE_COMPILERCHECK",
134 "CCACHE_SLOPPINESS",
135 "CCACHE_BASEDIR",
136 "CCACHE_CPP2",
137 }, config.BuildBrokenNinjaUsesEnvVars()...)...)
138 }
139
140 cmd.Environment.Set("DIST_DIR", config.DistDir())
141 cmd.Environment.Set("SHELL", "/bin/bash")
142
143 ctx.Verboseln("Ninja environment: ")
144 envVars := cmd.Environment.Environ()
145 sort.Strings(envVars)
146 for _, envVar := range envVars {
147 ctx.Verbosef(" %s", envVar)
148 }
149
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700150 // Poll the ninja log for updates; if it isn't updated enough, then we want to show some diagnostics
Jeff Gastona6697e82017-06-13 12:51:50 -0700151 done := make(chan struct{})
152 defer close(done)
153 ticker := time.NewTicker(ninjaHeartbeatDuration)
154 defer ticker.Stop()
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700155 checker := &statusChecker{}
156 go func() {
Jeff Gastona6697e82017-06-13 12:51:50 -0700157 for {
158 select {
159 case <-ticker.C:
160 checker.check(ctx, config, logPath)
161 case <-done:
162 return
163 }
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700164 }
165 }()
166
Dan Willemsen7f30c072019-01-02 12:50:49 -0800167 ctx.Status.Status("Starting ninja...")
Colin Cross7b97ecd2019-06-19 13:17:59 -0700168 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700169}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700170
171type statusChecker struct {
172 prevTime time.Time
173}
174
175func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
176 info, err := os.Stat(pathToCheck)
177 var newTime time.Time
178 if err == nil {
179 newTime = info.ModTime()
180 }
181 if newTime == c.prevTime {
182 // ninja may be stuck
183 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
184 }
185 c.prevTime = newTime
186}
187
188// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
189func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
190
191 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
192
193 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
194 // So, we try pstree first, and ps second
195 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
196 psCommandText := "ps -ef"
197 commandText := pstreeCommandText + " || " + psCommandText
198
199 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
200 output := cmd.CombinedOutputOrFatal()
201 ctx.Verbose(string(output))
202
Jeff Gastona6697e82017-06-13 12:51:50 -0700203 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700204}