blob: 33f9a07bf39109f86f57675b8bb7728eabebaf62 [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 (
18 "os/exec"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070019 "path/filepath"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "strconv"
21 "strings"
Dan Willemsend9f6fa22016-08-21 15:17:17 -070022 "time"
Dan Willemsen1e704462016-08-21 15:17:17 -070023)
24
25func runNinja(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070026 ctx.BeginTrace("ninja")
27 defer ctx.EndTrace()
28
Dan Willemsen1e704462016-08-21 15:17:17 -070029 executable := "prebuilts/build-tools/" + config.HostPrebuiltTag() + "/bin/ninja"
30 args := []string{
31 "-d", "keepdepfile",
32 }
33
34 args = append(args, config.NinjaArgs()...)
35
36 var parallel int
37 if config.UseGoma() {
38 parallel = config.RemoteParallel()
39 } else {
40 parallel = config.Parallel()
41 }
42 args = append(args, "-j", strconv.Itoa(parallel))
43 if config.keepGoing != 1 {
44 args = append(args, "-k", strconv.Itoa(config.keepGoing))
45 }
46
47 args = append(args, "-f", config.CombinedNinjaFile())
48
49 if config.IsVerbose() {
50 args = append(args, "-v")
51 }
52 args = append(args, "-w", "dupbuild=err")
53
54 env := config.Environment().Copy()
55 env.AppendFromKati(config.KatiEnvFile())
56
57 // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been
58 // used in the past to specify extra ninja arguments.
59 if extra, ok := env.Get("NINJA_ARGS"); ok {
60 args = append(args, strings.Fields(extra)...)
61 }
62 if extra, ok := env.Get("NINJA_EXTRA_ARGS"); ok {
63 args = append(args, strings.Fields(extra)...)
64 }
65
66 if _, ok := env.Get("NINJA_STATUS"); !ok {
67 env.Set("NINJA_STATUS", "[%p %f/%t] ")
68 }
69
70 cmd := exec.CommandContext(ctx.Context, executable, args...)
71 cmd.Env = env.Environ()
72 cmd.Stdin = ctx.Stdin()
73 cmd.Stdout = ctx.Stdout()
74 cmd.Stderr = ctx.Stderr()
75 ctx.Verboseln(cmd.Path, cmd.Args)
Dan Willemsend9f6fa22016-08-21 15:17:17 -070076 startTime := time.Now()
77 defer ctx.ImportNinjaLog(filepath.Join(config.OutDir(), ".ninja_log"), startTime)
Dan Willemsen1e704462016-08-21 15:17:17 -070078 if err := cmd.Run(); err != nil {
79 if e, ok := err.(*exec.ExitError); ok {
80 ctx.Fatalln("ninja failed with:", e.ProcessState.String())
81 } else {
82 ctx.Fatalln("Failed to run ninja:", err)
83 }
84 }
85}