blob: ab15e863a6a61b39b2f003eb7b1c76540ede760b [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
25 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
28func runNinja(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070029 ctx.BeginTrace("ninja")
30 defer ctx.EndTrace()
31
Dan Willemsenb82471a2018-05-17 16:37:09 -070032 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
33 status.NinjaReader(ctx, ctx.Status.StartTool(), fifo)
34
Dan Willemsenf173d592017-04-27 14:28:00 -070035 executable := config.PrebuiltBuildTool("ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -070036 args := []string{
37 "-d", "keepdepfile",
Dan Willemsen02736672018-07-17 17:54:31 -070038 "--frontend_file", fifo,
Dan Willemsen1e704462016-08-21 15:17:17 -070039 }
40
41 args = append(args, config.NinjaArgs()...)
42
43 var parallel int
44 if config.UseGoma() {
45 parallel = config.RemoteParallel()
46 } else {
47 parallel = config.Parallel()
48 }
49 args = append(args, "-j", strconv.Itoa(parallel))
50 if config.keepGoing != 1 {
51 args = append(args, "-k", strconv.Itoa(config.keepGoing))
52 }
53
54 args = append(args, "-f", config.CombinedNinjaFile())
55
Dan Willemsen1e704462016-08-21 15:17:17 -070056 args = append(args, "-w", "dupbuild=err")
57
Dan Willemsen269a8c72017-05-03 17:15:47 -070058 cmd := Command(ctx, config, "ninja", executable, args...)
Dan Willemsene0879fc2017-08-04 15:06:27 -070059 if config.HasKatiSuffix() {
60 cmd.Environment.AppendFromKati(config.KatiEnvFile())
61 }
Dan Willemsen1e704462016-08-21 15:17:17 -070062
Dan Willemsen2d31a442018-10-20 21:33:41 -070063 cmd.Environment.Set("DIST_DIR", config.DistDir())
64
Dan Willemsen1e704462016-08-21 15:17:17 -070065 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
66 // used in the past to specify extra ninja arguments.
Dan Willemsen269a8c72017-05-03 17:15:47 -070067 if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {
68 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070069 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070070 if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok {
71 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070072 }
73
Jeff Gaston809cc6f2017-05-25 15:44:36 -070074 logPath := filepath.Join(config.OutDir(), ".ninja_log")
75 ninjaHeartbeatDuration := time.Minute * 5
76 if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok {
77 // For example, "1m"
78 overrideDuration, err := time.ParseDuration(overrideText)
79 if err == nil && overrideDuration.Seconds() > 0 {
80 ninjaHeartbeatDuration = overrideDuration
81 }
82 }
83 // 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 -070084 done := make(chan struct{})
85 defer close(done)
86 ticker := time.NewTicker(ninjaHeartbeatDuration)
87 defer ticker.Stop()
Jeff Gaston809cc6f2017-05-25 15:44:36 -070088 checker := &statusChecker{}
89 go func() {
Jeff Gastona6697e82017-06-13 12:51:50 -070090 for {
91 select {
92 case <-ticker.C:
93 checker.check(ctx, config, logPath)
94 case <-done:
95 return
96 }
Jeff Gaston809cc6f2017-05-25 15:44:36 -070097 }
98 }()
99
Dan Willemsen7f30c072019-01-02 12:50:49 -0800100 ctx.Status.Status("Starting ninja...")
Dan Willemsenb82471a2018-05-17 16:37:09 -0700101 cmd.RunAndPrintOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700102}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700103
104type statusChecker struct {
105 prevTime time.Time
106}
107
108func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
109 info, err := os.Stat(pathToCheck)
110 var newTime time.Time
111 if err == nil {
112 newTime = info.ModTime()
113 }
114 if newTime == c.prevTime {
115 // ninja may be stuck
116 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
117 }
118 c.prevTime = newTime
119}
120
121// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
122func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
123
124 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
125
126 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
127 // So, we try pstree first, and ps second
128 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
129 psCommandText := "ps -ef"
130 commandText := pstreeCommandText + " || " + psCommandText
131
132 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
133 output := cmd.CombinedOutputOrFatal()
134 ctx.Verbose(string(output))
135
Jeff Gastona6697e82017-06-13 12:51:50 -0700136 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700137}