blob: 835f820307cac253d8917660da8071a534e80341 [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")
34 status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
35
Dan Willemsenf173d592017-04-27 14:28:00 -070036 executable := config.PrebuiltBuildTool("ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -070037 args := []string{
38 "-d", "keepdepfile",
Dan Willemsen02736672018-07-17 17:54:31 -070039 "--frontend_file", fifo,
Dan Willemsen1e704462016-08-21 15:17:17 -070040 }
41
42 args = append(args, config.NinjaArgs()...)
43
44 var parallel int
45 if config.UseGoma() {
46 parallel = config.RemoteParallel()
47 } else {
48 parallel = config.Parallel()
49 }
50 args = append(args, "-j", strconv.Itoa(parallel))
51 if config.keepGoing != 1 {
52 args = append(args, "-k", strconv.Itoa(config.keepGoing))
53 }
54
55 args = append(args, "-f", config.CombinedNinjaFile())
56
Dan Willemsenf7939332019-01-05 19:31:32 -080057 args = append(args,
58 "-w", "dupbuild=err",
59 "-w", "missingdepfile=err")
Dan Willemsen1e704462016-08-21 15:17:17 -070060
Dan Willemsen269a8c72017-05-03 17:15:47 -070061 cmd := Command(ctx, config, "ninja", executable, args...)
Dan Willemsene0879fc2017-08-04 15:06:27 -070062 if config.HasKatiSuffix() {
63 cmd.Environment.AppendFromKati(config.KatiEnvFile())
64 }
Dan Willemsen1e704462016-08-21 15:17:17 -070065
Dan Willemsen2d31a442018-10-20 21:33:41 -070066 cmd.Environment.Set("DIST_DIR", config.DistDir())
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
69 // used in the past to specify extra ninja arguments.
Dan Willemsen269a8c72017-05-03 17:15:47 -070070 if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {
71 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070072 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070073 if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok {
74 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070075 }
76
Jeff Gaston809cc6f2017-05-25 15:44:36 -070077 logPath := filepath.Join(config.OutDir(), ".ninja_log")
78 ninjaHeartbeatDuration := time.Minute * 5
79 if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok {
80 // For example, "1m"
81 overrideDuration, err := time.ParseDuration(overrideText)
82 if err == nil && overrideDuration.Seconds() > 0 {
83 ninjaHeartbeatDuration = overrideDuration
84 }
85 }
86 // 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 -070087 done := make(chan struct{})
88 defer close(done)
89 ticker := time.NewTicker(ninjaHeartbeatDuration)
90 defer ticker.Stop()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070091 checker := &statusChecker{}
92 go func() {
Jeff Gastona6697e82017-06-13 12:51:50 -070093 for {
94 select {
95 case <-ticker.C:
96 checker.check(ctx, config, logPath)
97 case <-done:
98 return
99 }
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700100 }
101 }()
102
Dan Willemsen7f30c072019-01-02 12:50:49 -0800103 ctx.Status.Status("Starting ninja...")
Dan Willemsenb82471a2018-05-17 16:37:09 -0700104 cmd.RunAndPrintOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700105}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700106
107type statusChecker struct {
108 prevTime time.Time
109}
110
111func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
112 info, err := os.Stat(pathToCheck)
113 var newTime time.Time
114 if err == nil {
115 newTime = info.ModTime()
116 }
117 if newTime == c.prevTime {
118 // ninja may be stuck
119 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
120 }
121 c.prevTime = newTime
122}
123
124// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
125func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
126
127 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
128
129 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
130 // So, we try pstree first, and ps second
131 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
132 psCommandText := "ps -ef"
133 commandText := pstreeCommandText + " || " + psCommandText
134
135 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
136 output := cmd.CombinedOutputOrFatal()
137 ctx.Verbose(string(output))
138
Jeff Gastona6697e82017-06-13 12:51:50 -0700139 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700140}