blob: 743da4aaee9e366c2c1a783bf83cb5da40b6aedf [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 Willemsen59fdf962017-07-24 22:26:54 -070062 genKatiSuffix(ctx, config)
63
64 runKatiCleanSpec(ctx, config)
65
Dan Willemsend9f6fa22016-08-21 15:17:17 -070066 ctx.BeginTrace("kati")
67 defer ctx.EndTrace()
68
Dan Willemsenf173d592017-04-27 14:28:00 -070069 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen1e704462016-08-21 15:17:17 -070070 args := []string{
71 "--ninja",
72 "--ninja_dir=" + config.OutDir(),
73 "--ninja_suffix=" + config.KatiSuffix(),
74 "--regen",
75 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
76 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080077 "--color_warnings",
78 "--gen_all_targets",
Dan Willemsen418420e2017-05-30 14:07:45 -070079 "--werror_find_emulator",
Dan Willemsend368d6f2018-06-15 21:53:18 -070080 "--no_builtin_rules",
81 "--werror_suffix_rules",
Dan Willemsen75d2c172017-10-12 20:46:34 -070082 "--kati_stats",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070083 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -070084 }
85
Dan Willemsen3d60b112018-04-04 22:25:56 -070086 if !config.BuildBrokenDupRules() {
87 args = append(args, "--werror_overriding_commands")
88 }
89
Dan Willemsen1e704462016-08-21 15:17:17 -070090 if !config.Environment().IsFalse("KATI_EMULATE_FIND") {
91 args = append(args, "--use_find_emulator")
92 }
93
Dan Willemsen1e704462016-08-21 15:17:17 -070094 args = append(args, config.KatiArgs()...)
95
96 args = append(args,
Dan Willemsen1e704462016-08-21 15:17:17 -070097 "BUILDING_WITH_NINJA=true",
98 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
Dan Willemsen6ab79db2018-05-02 00:06:28 -070099 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
100 "TARGET_DEVICE_DIR="+config.TargetDeviceDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700101
102 if config.UseGoma() {
103 args = append(args, "-j"+strconv.Itoa(config.Parallel()))
104 }
105
Dan Willemsen269a8c72017-05-03 17:15:47 -0700106 cmd := Command(ctx, config, "ckati", executable, args...)
107 cmd.Sandbox = katiSandbox
Dan Willemsen29f88272017-02-18 18:12:41 -0800108 pipe, err := cmd.StdoutPipe()
109 if err != nil {
110 ctx.Fatalln("Error getting output pipe for ckati:", err)
111 }
112 cmd.Stderr = cmd.Stdout
113
Dan Willemsen269a8c72017-05-03 17:15:47 -0700114 cmd.StartOrFatal()
Dan Willemsen29f88272017-02-18 18:12:41 -0800115 katiRewriteOutput(ctx, pipe)
Dan Willemsen269a8c72017-05-03 17:15:47 -0700116 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700117}
Dan Willemsen29f88272017-02-18 18:12:41 -0800118
119var katiIncludeRe = regexp.MustCompile(`^(\[\d+/\d+] )?including [^ ]+ ...$`)
Dan Willemsen75d2c172017-10-12 20:46:34 -0700120var katiLogRe = regexp.MustCompile(`^\*kati\*: `)
Dan Willemsen29f88272017-02-18 18:12:41 -0800121
122func katiRewriteOutput(ctx Context, pipe io.ReadCloser) {
123 haveBlankLine := true
124 smartTerminal := ctx.IsTerminal()
Dan Willemsen0c518512018-01-09 02:09:52 -0800125 errSmartTerminal := ctx.IsErrTerminal()
Dan Willemsen29f88272017-02-18 18:12:41 -0800126
127 scanner := bufio.NewScanner(pipe)
128 for scanner.Scan() {
129 line := scanner.Text()
130 verbose := katiIncludeRe.MatchString(line)
131
Dan Willemsen75d2c172017-10-12 20:46:34 -0700132 // Only put kati debug/stat lines in our verbose log
133 if katiLogRe.MatchString(line) {
134 ctx.Verbose(line)
135 continue
136 }
137
Dan Willemsen29f88272017-02-18 18:12:41 -0800138 // For verbose lines, write them on the current line without a newline,
139 // then overwrite them if the next thing we're printing is another
140 // verbose line.
141 if smartTerminal && verbose {
142 // Limit line width to the terminal width, otherwise we'll wrap onto
143 // another line and we won't delete the previous line.
144 //
145 // Run this on every line in case the window has been resized while
146 // we're printing. This could be optimized to only re-run when we
147 // get SIGWINCH if it ever becomes too time consuming.
148 if max, ok := termWidth(ctx.Stdout()); ok {
149 if len(line) > max {
150 // Just do a max. Ninja elides the middle, but that's
151 // more complicated and these lines aren't that important.
152 line = line[:max]
153 }
154 }
155
156 // Move to the beginning on the line, print the output, then clear
157 // the rest of the line.
158 fmt.Fprint(ctx.Stdout(), "\r", line, "\x1b[K")
159 haveBlankLine = false
160 continue
161 } else if smartTerminal && !haveBlankLine {
162 // If we've previously written a verbose message, send a newline to save
163 // that message instead of overwriting it.
164 fmt.Fprintln(ctx.Stdout())
165 haveBlankLine = true
Dan Willemsen0c518512018-01-09 02:09:52 -0800166 } else if !errSmartTerminal {
Dan Willemsen29f88272017-02-18 18:12:41 -0800167 // Most editors display these as garbage, so strip them out.
168 line = string(stripAnsiEscapes([]byte(line)))
169 }
170
171 // Assume that non-verbose lines are important enough for stderr
172 fmt.Fprintln(ctx.Stderr(), line)
173 }
174
175 // Save our last verbose line.
176 if !haveBlankLine {
177 fmt.Fprintln(ctx.Stdout())
178 }
Dan Willemsend9429e62018-03-06 22:03:55 -0800179
180 if err := scanner.Err(); err != nil {
181 ctx.Println("Error from kati parser:", err)
182 io.Copy(ctx.Stderr(), pipe)
183 }
Dan Willemsen29f88272017-02-18 18:12:41 -0800184}
Dan Willemsen59fdf962017-07-24 22:26:54 -0700185
186func runKatiCleanSpec(ctx Context, config Config) {
187 ctx.BeginTrace("kati cleanspec")
188 defer ctx.EndTrace()
189
190 executable := config.PrebuiltBuildTool("ckati")
191 args := []string{
192 "--ninja",
193 "--ninja_dir=" + config.OutDir(),
194 "--ninja_suffix=" + config.KatiSuffix() + "-cleanspec",
195 "--regen",
196 "--detect_android_echo",
197 "--color_warnings",
198 "--gen_all_targets",
199 "--werror_find_emulator",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700200 "--werror_overriding_commands",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700201 "--use_find_emulator",
Dan Willemsen81759842018-05-01 23:34:56 -0700202 "--kati_stats",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700203 "-f", "build/make/core/cleanbuild.mk",
204 "BUILDING_WITH_NINJA=true",
205 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700206 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen59fdf962017-07-24 22:26:54 -0700207 }
208
209 cmd := Command(ctx, config, "ckati", executable, args...)
210 cmd.Sandbox = katiCleanSpecSandbox
Dan Willemsen81759842018-05-01 23:34:56 -0700211 pipe, err := cmd.StdoutPipe()
212 if err != nil {
213 ctx.Fatalln("Error getting output pipe for ckati:", err)
214 }
215 cmd.Stderr = cmd.Stdout
Dan Willemsen59fdf962017-07-24 22:26:54 -0700216
Dan Willemsen81759842018-05-01 23:34:56 -0700217 cmd.StartOrFatal()
218 katiRewriteOutput(ctx, pipe)
219 cmd.WaitOrFatal()
Dan Willemsen59fdf962017-07-24 22:26:54 -0700220}