blob: 7994f3a2da65c6494fbc922f9922acc9d2240a62 [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 Willemsen1e704462016-08-21 15:17:17 -070021 "strconv"
22 "strings"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070023 "time"
Dan Willemsenb82471a2018-05-17 16:37:09 -070024
Nan Zhang17f27672018-12-12 16:01:49 -080025 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070026 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070027)
28
29func runNinja(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -080030 ctx.BeginTrace(metrics.PrimaryNinja, "ninja")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070031 defer ctx.EndTrace()
32
Dan Willemsenb82471a2018-05-17 16:37:09 -070033 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -070034 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
35 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -070036
Dan Willemsenf173d592017-04-27 14:28:00 -070037 executable := config.PrebuiltBuildTool("ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -070038 args := []string{
39 "-d", "keepdepfile",
Dan Willemsen02736672018-07-17 17:54:31 -070040 "--frontend_file", fifo,
Dan Willemsen1e704462016-08-21 15:17:17 -070041 }
42
43 args = append(args, config.NinjaArgs()...)
44
45 var parallel int
46 if config.UseGoma() {
47 parallel = config.RemoteParallel()
48 } else {
49 parallel = config.Parallel()
50 }
51 args = append(args, "-j", strconv.Itoa(parallel))
52 if config.keepGoing != 1 {
53 args = append(args, "-k", strconv.Itoa(config.keepGoing))
54 }
55
56 args = append(args, "-f", config.CombinedNinjaFile())
57
Dan Willemsenf7939332019-01-05 19:31:32 -080058 args = append(args,
59 "-w", "dupbuild=err",
60 "-w", "missingdepfile=err")
Dan Willemsen1e704462016-08-21 15:17:17 -070061
Dan Willemsen269a8c72017-05-03 17:15:47 -070062 cmd := Command(ctx, config, "ninja", executable, args...)
Dan Willemsen63663c62019-01-02 12:24:44 -080063 cmd.Sandbox = ninjaSandbox
Dan Willemsene0879fc2017-08-04 15:06:27 -070064 if config.HasKatiSuffix() {
65 cmd.Environment.AppendFromKati(config.KatiEnvFile())
66 }
Dan Willemsen1e704462016-08-21 15:17:17 -070067
Dan Willemsen2d31a442018-10-20 21:33:41 -070068 cmd.Environment.Set("DIST_DIR", config.DistDir())
69
Dan Willemsen1e704462016-08-21 15:17:17 -070070 // 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 }
88 // 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 -070089 done := make(chan struct{})
90 defer close(done)
91 ticker := time.NewTicker(ninjaHeartbeatDuration)
92 defer ticker.Stop()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070093 checker := &statusChecker{}
94 go func() {
Jeff Gastona6697e82017-06-13 12:51:50 -070095 for {
96 select {
97 case <-ticker.C:
98 checker.check(ctx, config, logPath)
99 case <-done:
100 return
101 }
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700102 }
103 }()
104
Dan Willemsen7f30c072019-01-02 12:50:49 -0800105 ctx.Status.Status("Starting ninja...")
Dan Willemsenb82471a2018-05-17 16:37:09 -0700106 cmd.RunAndPrintOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700107}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700108
109type statusChecker struct {
110 prevTime time.Time
111}
112
113func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
114 info, err := os.Stat(pathToCheck)
115 var newTime time.Time
116 if err == nil {
117 newTime = info.ModTime()
118 }
119 if newTime == c.prevTime {
120 // ninja may be stuck
121 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
122 }
123 c.prevTime = newTime
124}
125
126// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
127func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
128
129 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
130
131 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
132 // So, we try pstree first, and ps second
133 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
134 psCommandText := "ps -ef"
135 commandText := pstreeCommandText + " || " + psCommandText
136
137 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
138 output := cmd.CombinedOutputOrFatal()
139 ctx.Verbose(string(output))
140
Jeff Gastona6697e82017-06-13 12:51:50 -0700141 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700142}