Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 18 | "fmt" |
| 19 | "os" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "time" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 25 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 26 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | func runNinja(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 30 | ctx.BeginTrace(metrics.PrimaryNinja, "ninja") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 31 | defer ctx.EndTrace() |
| 32 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 33 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
| 34 | status.NinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 35 | |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 36 | executable := config.PrebuiltBuildTool("ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 37 | args := []string{ |
| 38 | "-d", "keepdepfile", |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 39 | "--frontend_file", fifo, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 40 | } |
| 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 Willemsen | f793933 | 2019-01-05 19:31:32 -0800 | [diff] [blame] | 57 | args = append(args, |
| 58 | "-w", "dupbuild=err", |
| 59 | "-w", "missingdepfile=err") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 60 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 61 | cmd := Command(ctx, config, "ninja", executable, args...) |
Dan Willemsen | 63663c6 | 2019-01-02 12:24:44 -0800 | [diff] [blame^] | 62 | cmd.Sandbox = ninjaSandbox |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 63 | if config.HasKatiSuffix() { |
| 64 | cmd.Environment.AppendFromKati(config.KatiEnvFile()) |
| 65 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 66 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 67 | cmd.Environment.Set("DIST_DIR", config.DistDir()) |
| 68 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 69 | // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been |
| 70 | // used in the past to specify extra ninja arguments. |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 71 | if extra, ok := cmd.Environment.Get("NINJA_ARGS"); ok { |
| 72 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 73 | } |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 74 | if extra, ok := cmd.Environment.Get("NINJA_EXTRA_ARGS"); ok { |
| 75 | cmd.Args = append(cmd.Args, strings.Fields(extra)...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 78 | logPath := filepath.Join(config.OutDir(), ".ninja_log") |
| 79 | ninjaHeartbeatDuration := time.Minute * 5 |
| 80 | if overrideText, ok := cmd.Environment.Get("NINJA_HEARTBEAT_INTERVAL"); ok { |
| 81 | // For example, "1m" |
| 82 | overrideDuration, err := time.ParseDuration(overrideText) |
| 83 | if err == nil && overrideDuration.Seconds() > 0 { |
| 84 | ninjaHeartbeatDuration = overrideDuration |
| 85 | } |
| 86 | } |
| 87 | // Poll the ninja log for updates; if it isn't updated enough, then we want to show some diagnostics |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 88 | done := make(chan struct{}) |
| 89 | defer close(done) |
| 90 | ticker := time.NewTicker(ninjaHeartbeatDuration) |
| 91 | defer ticker.Stop() |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 92 | checker := &statusChecker{} |
| 93 | go func() { |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 94 | for { |
| 95 | select { |
| 96 | case <-ticker.C: |
| 97 | checker.check(ctx, config, logPath) |
| 98 | case <-done: |
| 99 | return |
| 100 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 101 | } |
| 102 | }() |
| 103 | |
Dan Willemsen | 7f30c07 | 2019-01-02 12:50:49 -0800 | [diff] [blame] | 104 | ctx.Status.Status("Starting ninja...") |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 105 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 106 | } |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 107 | |
| 108 | type statusChecker struct { |
| 109 | prevTime time.Time |
| 110 | } |
| 111 | |
| 112 | func (c *statusChecker) check(ctx Context, config Config, pathToCheck string) { |
| 113 | info, err := os.Stat(pathToCheck) |
| 114 | var newTime time.Time |
| 115 | if err == nil { |
| 116 | newTime = info.ModTime() |
| 117 | } |
| 118 | if newTime == c.prevTime { |
| 119 | // ninja may be stuck |
| 120 | dumpStucknessDiagnostics(ctx, config, pathToCheck, newTime) |
| 121 | } |
| 122 | c.prevTime = newTime |
| 123 | } |
| 124 | |
| 125 | // dumpStucknessDiagnostics gets called when it is suspected that Ninja is stuck and we want to output some diagnostics |
| 126 | func dumpStucknessDiagnostics(ctx Context, config Config, statusPath string, lastUpdated time.Time) { |
| 127 | |
| 128 | ctx.Verbosef("ninja may be stuck; last update to %v was %v. dumping process tree...", statusPath, lastUpdated) |
| 129 | |
| 130 | // The "pstree" command doesn't exist on Mac, but "pstree" on Linux gives more convenient output than "ps" |
| 131 | // So, we try pstree first, and ps second |
| 132 | pstreeCommandText := fmt.Sprintf("pstree -pal %v", os.Getpid()) |
| 133 | psCommandText := "ps -ef" |
| 134 | commandText := pstreeCommandText + " || " + psCommandText |
| 135 | |
| 136 | cmd := Command(ctx, config, "dump process tree", "bash", "-c", commandText) |
| 137 | output := cmd.CombinedOutputOrFatal() |
| 138 | ctx.Verbose(string(output)) |
| 139 | |
Jeff Gaston | a6697e8 | 2017-06-13 12:51:50 -0700 | [diff] [blame] | 140 | ctx.Verbosef("done\n") |
Jeff Gaston | 809cc6f | 2017-05-25 15:44:36 -0700 | [diff] [blame] | 141 | } |