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 ( |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 18 | "bufio" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 19 | "crypto/md5" |
| 20 | "fmt" |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 21 | "io" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 22 | "io/ioutil" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "path/filepath" |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 24 | "regexp" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 25 | "strconv" |
| 26 | "strings" |
| 27 | ) |
| 28 | |
| 29 | var 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. |
| 35 | func 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 | |
| 61 | func runKati(ctx Context, config Config) { |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 62 | genKatiSuffix(ctx, config) |
| 63 | |
| 64 | runKatiCleanSpec(ctx, config) |
| 65 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 66 | ctx.BeginTrace("kati") |
| 67 | defer ctx.EndTrace() |
| 68 | |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 69 | executable := config.PrebuiltBuildTool("ckati") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 70 | 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 Willemsen | c38d366 | 2017-02-24 10:53:23 -0800 | [diff] [blame] | 77 | "--color_warnings", |
| 78 | "--gen_all_targets", |
Dan Willemsen | 418420e | 2017-05-30 14:07:45 -0700 | [diff] [blame] | 79 | "--werror_find_emulator", |
Dan Willemsen | 75d2c17 | 2017-10-12 20:46:34 -0700 | [diff] [blame] | 80 | "--kati_stats", |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 81 | "-f", "build/make/core/main.mk", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame^] | 84 | if !config.BuildBrokenDupRules() { |
| 85 | args = append(args, "--werror_overriding_commands") |
| 86 | } |
| 87 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 88 | if !config.Environment().IsFalse("KATI_EMULATE_FIND") { |
| 89 | args = append(args, "--use_find_emulator") |
| 90 | } |
| 91 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 92 | args = append(args, config.KatiArgs()...) |
| 93 | |
| 94 | args = append(args, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 95 | "BUILDING_WITH_NINJA=true", |
| 96 | "SOONG_ANDROID_MK="+config.SoongAndroidMk(), |
| 97 | "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk()) |
| 98 | |
| 99 | if config.UseGoma() { |
| 100 | args = append(args, "-j"+strconv.Itoa(config.Parallel())) |
| 101 | } |
| 102 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 103 | cmd := Command(ctx, config, "ckati", executable, args...) |
| 104 | cmd.Sandbox = katiSandbox |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 105 | pipe, err := cmd.StdoutPipe() |
| 106 | if err != nil { |
| 107 | ctx.Fatalln("Error getting output pipe for ckati:", err) |
| 108 | } |
| 109 | cmd.Stderr = cmd.Stdout |
| 110 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 111 | cmd.StartOrFatal() |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 112 | katiRewriteOutput(ctx, pipe) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 113 | cmd.WaitOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 114 | } |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 115 | |
| 116 | var katiIncludeRe = regexp.MustCompile(`^(\[\d+/\d+] )?including [^ ]+ ...$`) |
Dan Willemsen | 75d2c17 | 2017-10-12 20:46:34 -0700 | [diff] [blame] | 117 | var katiLogRe = regexp.MustCompile(`^\*kati\*: `) |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 118 | |
| 119 | func katiRewriteOutput(ctx Context, pipe io.ReadCloser) { |
| 120 | haveBlankLine := true |
| 121 | smartTerminal := ctx.IsTerminal() |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 122 | errSmartTerminal := ctx.IsErrTerminal() |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 123 | |
| 124 | scanner := bufio.NewScanner(pipe) |
| 125 | for scanner.Scan() { |
| 126 | line := scanner.Text() |
| 127 | verbose := katiIncludeRe.MatchString(line) |
| 128 | |
Dan Willemsen | 75d2c17 | 2017-10-12 20:46:34 -0700 | [diff] [blame] | 129 | // Only put kati debug/stat lines in our verbose log |
| 130 | if katiLogRe.MatchString(line) { |
| 131 | ctx.Verbose(line) |
| 132 | continue |
| 133 | } |
| 134 | |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 135 | // For verbose lines, write them on the current line without a newline, |
| 136 | // then overwrite them if the next thing we're printing is another |
| 137 | // verbose line. |
| 138 | if smartTerminal && verbose { |
| 139 | // Limit line width to the terminal width, otherwise we'll wrap onto |
| 140 | // another line and we won't delete the previous line. |
| 141 | // |
| 142 | // Run this on every line in case the window has been resized while |
| 143 | // we're printing. This could be optimized to only re-run when we |
| 144 | // get SIGWINCH if it ever becomes too time consuming. |
| 145 | if max, ok := termWidth(ctx.Stdout()); ok { |
| 146 | if len(line) > max { |
| 147 | // Just do a max. Ninja elides the middle, but that's |
| 148 | // more complicated and these lines aren't that important. |
| 149 | line = line[:max] |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Move to the beginning on the line, print the output, then clear |
| 154 | // the rest of the line. |
| 155 | fmt.Fprint(ctx.Stdout(), "\r", line, "\x1b[K") |
| 156 | haveBlankLine = false |
| 157 | continue |
| 158 | } else if smartTerminal && !haveBlankLine { |
| 159 | // If we've previously written a verbose message, send a newline to save |
| 160 | // that message instead of overwriting it. |
| 161 | fmt.Fprintln(ctx.Stdout()) |
| 162 | haveBlankLine = true |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 163 | } else if !errSmartTerminal { |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 164 | // Most editors display these as garbage, so strip them out. |
| 165 | line = string(stripAnsiEscapes([]byte(line))) |
| 166 | } |
| 167 | |
| 168 | // Assume that non-verbose lines are important enough for stderr |
| 169 | fmt.Fprintln(ctx.Stderr(), line) |
| 170 | } |
| 171 | |
| 172 | // Save our last verbose line. |
| 173 | if !haveBlankLine { |
| 174 | fmt.Fprintln(ctx.Stdout()) |
| 175 | } |
Dan Willemsen | d9429e6 | 2018-03-06 22:03:55 -0800 | [diff] [blame] | 176 | |
| 177 | if err := scanner.Err(); err != nil { |
| 178 | ctx.Println("Error from kati parser:", err) |
| 179 | io.Copy(ctx.Stderr(), pipe) |
| 180 | } |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 181 | } |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 182 | |
| 183 | func runKatiCleanSpec(ctx Context, config Config) { |
| 184 | ctx.BeginTrace("kati cleanspec") |
| 185 | defer ctx.EndTrace() |
| 186 | |
| 187 | executable := config.PrebuiltBuildTool("ckati") |
| 188 | args := []string{ |
| 189 | "--ninja", |
| 190 | "--ninja_dir=" + config.OutDir(), |
| 191 | "--ninja_suffix=" + config.KatiSuffix() + "-cleanspec", |
| 192 | "--regen", |
| 193 | "--detect_android_echo", |
| 194 | "--color_warnings", |
| 195 | "--gen_all_targets", |
| 196 | "--werror_find_emulator", |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame^] | 197 | "--werror_overriding_commands", |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 198 | "--use_find_emulator", |
| 199 | "-f", "build/make/core/cleanbuild.mk", |
| 200 | "BUILDING_WITH_NINJA=true", |
| 201 | "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(), |
| 202 | } |
| 203 | |
| 204 | cmd := Command(ctx, config, "ckati", executable, args...) |
| 205 | cmd.Sandbox = katiCleanSpecSandbox |
| 206 | cmd.Stdout = ctx.Stdout() |
| 207 | cmd.Stderr = ctx.Stderr() |
| 208 | |
| 209 | // Kati leaks memory, so ensure leak detection is turned off |
| 210 | cmd.Environment.Set("ASAN_OPTIONS", "detect_leaks=0") |
| 211 | cmd.RunOrFatal() |
| 212 | } |