blob: 78d11707e79503ed17074892a708c64472903932 [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 Willemsen1e704462016-08-21 15:17:17 -070024)
25
26func runNinja(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070027 ctx.BeginTrace("ninja")
28 defer ctx.EndTrace()
29
Dan Willemsenf173d592017-04-27 14:28:00 -070030 executable := config.PrebuiltBuildTool("ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -070031 args := []string{
32 "-d", "keepdepfile",
33 }
34
35 args = append(args, config.NinjaArgs()...)
36
37 var parallel int
38 if config.UseGoma() {
39 parallel = config.RemoteParallel()
40 } else {
41 parallel = config.Parallel()
42 }
43 args = append(args, "-j", strconv.Itoa(parallel))
44 if config.keepGoing != 1 {
45 args = append(args, "-k", strconv.Itoa(config.keepGoing))
46 }
47
48 args = append(args, "-f", config.CombinedNinjaFile())
49
50 if config.IsVerbose() {
51 args = append(args, "-v")
52 }
53 args = append(args, "-w", "dupbuild=err")
54
Dan Willemsen269a8c72017-05-03 17:15:47 -070055 cmd := Command(ctx, config, "ninja", executable, args...)
56 cmd.Environment.AppendFromKati(config.KatiEnvFile())
Dan Willemsen1e704462016-08-21 15:17:17 -070057
58 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
59 // used in the past to specify extra ninja arguments.
Dan Willemsen269a8c72017-05-03 17:15:47 -070060 if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok {
61 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070062 }
Dan Willemsen269a8c72017-05-03 17:15:47 -070063 if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok {
64 cmd.Args = append(cmd.Args, strings.Fields(extra)...)
Dan Willemsen1e704462016-08-21 15:17:17 -070065 }
66
Dan Willemsen269a8c72017-05-03 17:15:47 -070067 if _, ok := cmd.Environment.Get("NINJA_STATUS"); !ok {
68 cmd.Environment.Set("NINJA_STATUS", "[%p %f/%t] ")
Dan Willemsen1e704462016-08-21 15:17:17 -070069 }
70
Dan Willemsen1e704462016-08-21 15:17:17 -070071 cmd.Stdin = ctx.Stdin()
72 cmd.Stdout = ctx.Stdout()
73 cmd.Stderr = ctx.Stderr()
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 Willemsend9f6fa22016-08-21 15:17:17 -0700100 startTime := time.Now()
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700101 defer ctx.ImportNinjaLog(logPath, startTime)
102
Dan Willemsen269a8c72017-05-03 17:15:47 -0700103 cmd.RunOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700104}
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700105
106type statusChecker struct {
107 prevTime time.Time
108}
109
110func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) {
111 info, err := os.Stat(pathToCheck)
112 var newTime time.Time
113 if err == nil {
114 newTime = info.ModTime()
115 }
116 if newTime == c.prevTime {
117 // ninja may be stuck
118 dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime)
119 }
120 c.prevTime = newTime
121}
122
123// dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics
124func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) {
125
126 ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated)
127
128 // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps"
129 // So, we try pstree first, and ps second
130 pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid())
131 psCommandText := "ps -ef"
132 commandText := pstreeCommandText + " || " + psCommandText
133
134 cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText)
135 output := cmd.CombinedOutputOrFatal()
136 ctx.Verbose(string(output))
137
Jeff Gastona6697e82017-06-13 12:51:50 -0700138 ctx.Verbosef("done\n")
Jeff Gaston809cc6f2017-05-25 15:44:36 -0700139}