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 | "crypto/md5" |
| 19 | "fmt" |
| 20 | "io/ioutil" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "strconv" |
| 23 | "strings" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 24 | |
| 25 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_") |
| 29 | |
| 30 | // genKatiSuffix creates a suffix for kati-generated files so that we can cache |
| 31 | // them based on their inputs. So this should encode all common changes to Kati |
| 32 | // inputs. Currently that includes the TARGET_PRODUCT, kati-processed command |
| 33 | // line arguments, and the directories specified by mm/mmm. |
| 34 | func genKatiSuffix(ctx Context, config Config) { |
| 35 | katiSuffix := "-" + config.TargetProduct() |
| 36 | if args := config.KatiArgs(); len(args) > 0 { |
| 37 | katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_")) |
| 38 | } |
| 39 | if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok { |
| 40 | katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot) |
| 41 | } |
| 42 | |
| 43 | // If the suffix is too long, replace it with a md5 hash and write a |
| 44 | // file that contains the original suffix. |
| 45 | if len(katiSuffix) > 64 { |
| 46 | shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix))) |
| 47 | config.SetKatiSuffix(shortSuffix) |
| 48 | |
| 49 | ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix) |
| 50 | ctx.Verbosef("Replacing with: %q", shortSuffix) |
| 51 | |
| 52 | if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil { |
| 53 | ctx.Println("Error writing suffix file:", err) |
| 54 | } |
| 55 | } else { |
| 56 | config.SetKatiSuffix(katiSuffix) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func runKati(ctx Context, config Config) { |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 61 | genKatiSuffix(ctx, config) |
| 62 | |
| 63 | runKatiCleanSpec(ctx, config) |
| 64 | |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 65 | ctx.BeginTrace("kati") |
| 66 | defer ctx.EndTrace() |
| 67 | |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 68 | executable := config.PrebuiltBuildTool("ckati") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 69 | args := []string{ |
| 70 | "--ninja", |
| 71 | "--ninja_dir=" + config.OutDir(), |
| 72 | "--ninja_suffix=" + config.KatiSuffix(), |
| 73 | "--regen", |
| 74 | "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"), |
| 75 | "--detect_android_echo", |
Dan Willemsen | c38d366 | 2017-02-24 10:53:23 -0800 | [diff] [blame] | 76 | "--color_warnings", |
| 77 | "--gen_all_targets", |
Dan Willemsen | 418420e | 2017-05-30 14:07:45 -0700 | [diff] [blame] | 78 | "--werror_find_emulator", |
Dan Willemsen | d368d6f | 2018-06-15 21:53:18 -0700 | [diff] [blame] | 79 | "--no_builtin_rules", |
| 80 | "--werror_suffix_rules", |
Dan Willemsen | f880b58 | 2018-07-23 23:09:05 -0700 | [diff] [blame^] | 81 | "--warn_real_to_phony", |
| 82 | "--warn_phony_looks_real", |
Dan Willemsen | 75d2c17 | 2017-10-12 20:46:34 -0700 | [diff] [blame] | 83 | "--kati_stats", |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 84 | "-f", "build/make/core/main.mk", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 87 | // PDK builds still uses a few implicit rules |
| 88 | if !config.IsPdkBuild() { |
Dan Willemsen | 2bc456e | 2018-06-21 21:41:35 -0700 | [diff] [blame] | 89 | args = append(args, "--werror_implicit_rules") |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 92 | if !config.BuildBrokenDupRules() { |
| 93 | args = append(args, "--werror_overriding_commands") |
| 94 | } |
| 95 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 96 | if !config.Environment().IsFalse("KATI_EMULATE_FIND") { |
| 97 | args = append(args, "--use_find_emulator") |
| 98 | } |
| 99 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 100 | args = append(args, config.KatiArgs()...) |
| 101 | |
| 102 | args = append(args, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 103 | "BUILDING_WITH_NINJA=true", |
| 104 | "SOONG_ANDROID_MK="+config.SoongAndroidMk(), |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 105 | "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(), |
| 106 | "TARGET_DEVICE_DIR="+config.TargetDeviceDir()) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 107 | |
| 108 | if config.UseGoma() { |
| 109 | args = append(args, "-j"+strconv.Itoa(config.Parallel())) |
| 110 | } |
| 111 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 112 | cmd := Command(ctx, config, "ckati", executable, args...) |
| 113 | cmd.Sandbox = katiSandbox |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 114 | pipe, err := cmd.StdoutPipe() |
| 115 | if err != nil { |
| 116 | ctx.Fatalln("Error getting output pipe for ckati:", err) |
| 117 | } |
| 118 | cmd.Stderr = cmd.Stdout |
| 119 | |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 120 | cmd.StartOrFatal() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 121 | status.KatiReader(ctx.Status.StartTool(), pipe) |
Dan Willemsen | 269a8c7 | 2017-05-03 17:15:47 -0700 | [diff] [blame] | 122 | cmd.WaitOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 123 | } |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 124 | |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 125 | func runKatiCleanSpec(ctx Context, config Config) { |
| 126 | ctx.BeginTrace("kati cleanspec") |
| 127 | defer ctx.EndTrace() |
| 128 | |
| 129 | executable := config.PrebuiltBuildTool("ckati") |
| 130 | args := []string{ |
| 131 | "--ninja", |
| 132 | "--ninja_dir=" + config.OutDir(), |
| 133 | "--ninja_suffix=" + config.KatiSuffix() + "-cleanspec", |
| 134 | "--regen", |
| 135 | "--detect_android_echo", |
| 136 | "--color_warnings", |
| 137 | "--gen_all_targets", |
| 138 | "--werror_find_emulator", |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 139 | "--werror_overriding_commands", |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 140 | "--use_find_emulator", |
Dan Willemsen | 8175984 | 2018-05-01 23:34:56 -0700 | [diff] [blame] | 141 | "--kati_stats", |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 142 | "-f", "build/make/core/cleanbuild.mk", |
| 143 | "BUILDING_WITH_NINJA=true", |
| 144 | "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(), |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 145 | "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(), |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | cmd := Command(ctx, config, "ckati", executable, args...) |
| 149 | cmd.Sandbox = katiCleanSpecSandbox |
Dan Willemsen | 8175984 | 2018-05-01 23:34:56 -0700 | [diff] [blame] | 150 | pipe, err := cmd.StdoutPipe() |
| 151 | if err != nil { |
| 152 | ctx.Fatalln("Error getting output pipe for ckati:", err) |
| 153 | } |
| 154 | cmd.Stderr = cmd.Stdout |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 155 | |
Dan Willemsen | 8175984 | 2018-05-01 23:34:56 -0700 | [diff] [blame] | 156 | cmd.StartOrFatal() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 157 | status.KatiReader(ctx.Status.StartTool(), pipe) |
Dan Willemsen | 8175984 | 2018-05-01 23:34:56 -0700 | [diff] [blame] | 158 | cmd.WaitOrFatal() |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 159 | } |