blob: f29d0c19a270ca89eba46d4a2775c7553979d569 [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 (
Dan Willemsen29f88272017-02-18 18:12:41 -080018 "bufio"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "crypto/md5"
20 "fmt"
Dan Willemsen29f88272017-02-18 18:12:41 -080021 "io"
Dan Willemsen1e704462016-08-21 15:17:17 -070022 "io/ioutil"
Dan Willemsen1e704462016-08-21 15:17:17 -070023 "path/filepath"
Dan Willemsen29f88272017-02-18 18:12:41 -080024 "regexp"
Dan Willemsen1e704462016-08-21 15:17:17 -070025 "strconv"
26 "strings"
27)
28
29var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
30
31// genKatiSuffix creates a suffix for kati-generated files so that we can cache
32// them based on their inputs. So this should encode all common changes to Kati
33// inputs. Currently that includes the TARGET_PRODUCT, kati-processed command
34// line arguments, and the directories specified by mm/mmm.
35func genKatiSuffix(ctx Context, config Config) {
36 katiSuffix := "-" + config.TargetProduct()
37 if args := config.KatiArgs(); len(args) > 0 {
38 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
39 }
40 if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
41 katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot)
42 }
43
44 // If the suffix is too long, replace it with a md5 hash and write a
45 // file that contains the original suffix.
46 if len(katiSuffix) > 64 {
47 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
48 config.SetKatiSuffix(shortSuffix)
49
50 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
51 ctx.Verbosef("Replacing with: %q", shortSuffix)
52
53 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
54 ctx.Println("Error writing suffix file:", err)
55 }
56 } else {
57 config.SetKatiSuffix(katiSuffix)
58 }
59}
60
61func runKati(ctx Context, config Config) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070062 ctx.BeginTrace("kati")
63 defer ctx.EndTrace()
64
Dan Willemsen1e704462016-08-21 15:17:17 -070065 genKatiSuffix(ctx, config)
66
Dan Willemsenf173d592017-04-27 14:28:00 -070067 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen1e704462016-08-21 15:17:17 -070068 args := []string{
69 "--ninja",
70 "--ninja_dir=" + config.OutDir(),
71 "--ninja_suffix=" + config.KatiSuffix(),
72 "--regen",
73 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
74 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080075 "--color_warnings",
76 "--gen_all_targets",
77 "-f", "build/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -070078 }
79
80 if !config.Environment().IsFalse("KATI_EMULATE_FIND") {
81 args = append(args, "--use_find_emulator")
82 }
83
Dan Willemsen1e704462016-08-21 15:17:17 -070084 args = append(args, config.KatiArgs()...)
85
86 args = append(args,
Dan Willemsen1e704462016-08-21 15:17:17 -070087 "BUILDING_WITH_NINJA=true",
88 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
89 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk())
90
91 if config.UseGoma() {
92 args = append(args, "-j"+strconv.Itoa(config.Parallel()))
93 }
94
Dan Willemsen269a8c72017-05-03 17:15:47 -070095 cmd := Command(ctx, config, "ckati", executable, args...)
96 cmd.Sandbox = katiSandbox
Dan Willemsen29f88272017-02-18 18:12:41 -080097 pipe, err := cmd.StdoutPipe()
98 if err != nil {
99 ctx.Fatalln("Error getting output pipe for ckati:", err)
100 }
101 cmd.Stderr = cmd.Stdout
102
Dan Willemsen269a8c72017-05-03 17:15:47 -0700103 cmd.StartOrFatal()
Dan Willemsen29f88272017-02-18 18:12:41 -0800104 katiRewriteOutput(ctx, pipe)
Dan Willemsen269a8c72017-05-03 17:15:47 -0700105 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700106}
Dan Willemsen29f88272017-02-18 18:12:41 -0800107
108var katiIncludeRe = regexp.MustCompile(`^(\[\d+/\d+] )?including [^ ]+ ...$`)
109
110func katiRewriteOutput(ctx Context, pipe io.ReadCloser) {
111 haveBlankLine := true
112 smartTerminal := ctx.IsTerminal()
113
114 scanner := bufio.NewScanner(pipe)
115 for scanner.Scan() {
116 line := scanner.Text()
117 verbose := katiIncludeRe.MatchString(line)
118
119 // For verbose lines, write them on the current line without a newline,
120 // then overwrite them if the next thing we're printing is another
121 // verbose line.
122 if smartTerminal && verbose {
123 // Limit line width to the terminal width, otherwise we'll wrap onto
124 // another line and we won't delete the previous line.
125 //
126 // Run this on every line in case the window has been resized while
127 // we're printing. This could be optimized to only re-run when we
128 // get SIGWINCH if it ever becomes too time consuming.
129 if max, ok := termWidth(ctx.Stdout()); ok {
130 if len(line) > max {
131 // Just do a max. Ninja elides the middle, but that's
132 // more complicated and these lines aren't that important.
133 line = line[:max]
134 }
135 }
136
137 // Move to the beginning on the line, print the output, then clear
138 // the rest of the line.
139 fmt.Fprint(ctx.Stdout(), "\r", line, "\x1b[K")
140 haveBlankLine = false
141 continue
142 } else if smartTerminal && !haveBlankLine {
143 // If we've previously written a verbose message, send a newline to save
144 // that message instead of overwriting it.
145 fmt.Fprintln(ctx.Stdout())
146 haveBlankLine = true
147 } else if !smartTerminal {
148 // Most editors display these as garbage, so strip them out.
149 line = string(stripAnsiEscapes([]byte(line)))
150 }
151
152 // Assume that non-verbose lines are important enough for stderr
153 fmt.Fprintln(ctx.Stderr(), line)
154 }
155
156 // Save our last verbose line.
157 if !haveBlankLine {
158 fmt.Fprintln(ctx.Stdout())
159 }
160}