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 ( |
| 18 | "os/exec" |
| 19 | "strconv" |
| 20 | "strings" |
| 21 | ) |
| 22 | |
| 23 | func runNinja(ctx Context, config Config) { |
| 24 | executable := "prebuilts/build-tools/" + config.HostPrebuiltTag() + "/bin/ninja" |
| 25 | args := []string{ |
| 26 | "-d", "keepdepfile", |
| 27 | } |
| 28 | |
| 29 | args = append(args, config.NinjaArgs()...) |
| 30 | |
| 31 | var parallel int |
| 32 | if config.UseGoma() { |
| 33 | parallel = config.RemoteParallel() |
| 34 | } else { |
| 35 | parallel = config.Parallel() |
| 36 | } |
| 37 | args = append(args, "-j", strconv.Itoa(parallel)) |
| 38 | if config.keepGoing != 1 { |
| 39 | args = append(args, "-k", strconv.Itoa(config.keepGoing)) |
| 40 | } |
| 41 | |
| 42 | args = append(args, "-f", config.CombinedNinjaFile()) |
| 43 | |
| 44 | if config.IsVerbose() { |
| 45 | args = append(args, "-v") |
| 46 | } |
| 47 | args = append(args, "-w", "dupbuild=err") |
| 48 | |
| 49 | env := config.Environment().Copy() |
| 50 | env.AppendFromKati(config.KatiEnvFile()) |
| 51 | |
| 52 | // Allow both NINJA_ARGS and NINJA_EXTRA_ARGS, since both have been |
| 53 | // used in the past to specify extra ninja arguments. |
| 54 | if extra, ok := env.Get("NINJA_ARGS"); ok { |
| 55 | args = append(args, strings.Fields(extra)...) |
| 56 | } |
| 57 | if extra, ok := env.Get("NINJA_EXTRA_ARGS"); ok { |
| 58 | args = append(args, strings.Fields(extra)...) |
| 59 | } |
| 60 | |
| 61 | if _, ok := env.Get("NINJA_STATUS"); !ok { |
| 62 | env.Set("NINJA_STATUS", "[%p %f/%t] ") |
| 63 | } |
| 64 | |
| 65 | cmd := exec.CommandContext(ctx.Context, executable, args...) |
| 66 | cmd.Env = env.Environ() |
| 67 | cmd.Stdin = ctx.Stdin() |
| 68 | cmd.Stdout = ctx.Stdout() |
| 69 | cmd.Stderr = ctx.Stderr() |
| 70 | ctx.Verboseln(cmd.Path, cmd.Args) |
| 71 | if err := cmd.Run(); err != nil { |
| 72 | if e, ok := err.(*exec.ExitError); ok { |
| 73 | ctx.Fatalln("ninja failed with:", e.ProcessState.String()) |
| 74 | } else { |
| 75 | ctx.Fatalln("Failed to run ninja:", err) |
| 76 | } |
| 77 | } |
| 78 | } |