blob: 4fc1f01e5472a26e031610c116dfe86943d964b6 [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 Willemsen6d3cad92020-03-12 10:30:35 -070041 "-d", "keeprsp",
Dan Willemsen02736672018-07-17 17:54:31 -070042 "--frontend_file", fifo,
Dan Willemsen1e704462016-08-21 15:17:17 -070043 }
44
45 args = append(args, config.NinjaArgs()...)
46
47 var parallel int
Colin Cross9016b912019-11-11 14:57:42 -080048 if config.UseRemoteBuild() {
Dan Willemsen1e704462016-08-21 15:17:17 -070049 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 Willemsenf7939332019-01-05 19:31:32 -080060 args = append(args,
61 "-w", "dupbuild=err",
62 "-w", "missingdepfile=err")
Dan Willemsen1e704462016-08-21 15:17:17 -070063
Dan Willemsen269a8c72017-05-03 17:15:47 -070064 cmd := Command(ctx, config, "ninja", executable, args...)
Dan Willemsen63663c62019-01-02 12:24:44 -080065 cmd.Sandbox = ninjaSandbox
Dan Willemsene0879fc2017-08-04 15:06:27 -070066 if config.HasKatiSuffix() {
67 cmd.Environment.AppendFromKati(config.KatiEnvFile())
68 }
Dan Willemsen1e704462016-08-21 15:17:17 -070069
70 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
71 // used in the past to specify extra ninja arguments.
Dan Willemsen269a8c72017-05-03 17:15:47 -070072 if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {
73 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070074 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070075 if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok {
76 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070077 }
78
Jeff Gaston809cc6f2017-05-25 15:44:36 -070079 logPath := filepath.Join(config.OutDir(), ".ninja_log")
80 ninjaHeartbeatDuration := time.Minute * 5
81 if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok {
82 // For example, "1m"
83 overrideDuration, err := time.ParseDuration(overrideText)
84 if err == nil && overrideDuration.Seconds() > 0 {
85 ninjaHeartbeatDuration = overrideDuration
86 }
87 }
Dan Willemsene3336352020-01-02 19:10:38 -080088
89 // Filter the environment, as ninja does not rebuild files when environment variables change.
90 //
91 // Anything listed here must not change the output of rules/actions when the value changes,
92 // otherwise incremental builds may be unsafe. Vars explicitly set to stable values
93 // elsewhere in soong_ui are fine.
94 //
95 // For the majority of cases, either Soong or the makefiles should be replicating any
96 // necessary environment variables in the command line of each action that needs it.
Dan Willemsen260db532020-01-02 20:12:09 -080097 if cmd.Environment.IsEnvTrue("ALLOW_NINJA_ENV") {
98 ctx.Println("Allowing all environment variables during ninja; incremental builds may be unsafe.")
99 } else {
Dan Willemsene3336352020-01-02 19:10:38 -0800100 cmd.Environment.Allow(append([]string{
101 "ASAN_SYMBOLIZER_PATH",
102 "HOME",
103 "JAVA_HOME",
104 "LANG",
105 "LC_MESSAGES",
106 "OUT_DIR",
107 "PATH",
108 "PWD",
109 "PYTHONDONTWRITEBYTECODE",
110 "TMPDIR",
111 "USER",
112
113 // TODO: remove these carefully
Dan Willemsen7da04292020-01-04 13:58:54 -0800114 "ASAN_OPTIONS",
Dan Willemsene3336352020-01-02 19:10:38 -0800115 "TARGET_BUILD_APPS",
116 "TARGET_BUILD_VARIANT",
117 "TARGET_PRODUCT",
Dan Willemsen5cacfe12020-01-06 12:25:40 -0800118 // b/147197813 - used by art-check-debug-apex-gen
119 "EMMA_INSTRUMENT_FRAMEWORK",
Dan Willemsene3336352020-01-02 19:10:38 -0800120
121 // Goma -- gomacc may not need all of these
122 "GOMA_DIR",
123 "GOMA_DISABLED",
124 "GOMA_FAIL_FAST",
125 "GOMA_FALLBACK",
126 "GOMA_GCE_SERVICE_ACCOUNT",
127 "GOMA_TMP_DIR",
128 "GOMA_USE_LOCAL",
129
130 // RBE client
Kousik Kumarade12e72020-01-09 08:52:59 -0800131 "FLAG_compare",
Dan Willemsene3336352020-01-02 19:10:38 -0800132 "FLAG_exec_root",
133 "FLAG_exec_strategy",
134 "FLAG_invocation_id",
135 "FLAG_log_dir",
136 "FLAG_platform",
Kousik Kumar0f095e12020-01-28 10:48:46 -0800137 "FLAG_remote_accept_cache",
138 "FLAG_remote_update_cache",
Dan Willemsene3336352020-01-02 19:10:38 -0800139 "FLAG_server_address",
140
141 // ccache settings
142 "CCACHE_COMPILERCHECK",
143 "CCACHE_SLOPPINESS",
144 "CCACHE_BASEDIR",
145 "CCACHE_CPP2",
John Eckerdal974b0e82020-02-04 15:59:37 +0100146 "CCACHE_DIR",
Dan Willemsene3336352020-01-02 19:10:38 -0800147 }, config.BuildBrokenNinjaUsesEnvVars()...)...)
148 }
149
150 cmd.Environment.Set("DIST_DIR", config.DistDir())
151 cmd.Environment.Set("SHELL", "/bin/bash")
152
153 ctx.Verboseln("Ninja environment: ")
154 envVars := cmd.Environment.Environ()
155 sort.Strings(envVars)
156 for _, envVar := range envVars {
157 ctx.Verbosef(" %s", envVar)
158 }
159
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700160 // 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 -0700161 done := make(chan struct{})
162 defer close(done)
163 ticker := time.NewTicker(ninjaHeartbeatDuration)
164 defer ticker.Stop()
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700165 checker := &statusChecker{}
166 go func() {
Jeff Gastona6697e82017-06-13 12:51:50 -0700167 for {
168 select {
169 case <-ticker.C:
170 checker.check(ctx, config, logPath)
171 case <-done:
172 return
173 }
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700174 }
175 }()
176
Dan Willemsen7f30c072019-01-02 12:50:49 -0800177 ctx.Status.Status("Starting ninja...")
Colin Cross7b97ecd2019-06-19 13:17:59 -0700178 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700179}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700180
181type statusChecker struct {
182 prevTime time.Time
183}
184
185func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
186 info, err := os.Stat(pathToCheck)
187 var newTime time.Time
188 if err == nil {
189 newTime = info.ModTime()
190 }
191 if newTime == c.prevTime {
192 // ninja may be stuck
193 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
194 }
195 c.prevTime = newTime
196}
197
198// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
199func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
200
201 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
202
203 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
204 // So, we try pstree first, and ps second
205 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
206 psCommandText := "ps -ef"
207 commandText := pstreeCommandText + " || " + psCommandText
208
209 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
210 output := cmd.CombinedOutputOrFatal()
211 ctx.Verbose(string(output))
212
Jeff Gastona6697e82017-06-13 12:51:50 -0700213 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700214}