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 | 71edc8b | 2019-01-02 12:21:18 -0800 | [diff] [blame] | 21 | "os" |
| 22 | "os/user" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "path/filepath" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 24 | "strings" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 25 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 26 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 27 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_") |
| 31 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 32 | const katiBuildSuffix = "" |
| 33 | const katiCleanspecSuffix = "-cleanspec" |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 34 | const katiPackageSuffix = "-package" |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 35 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 36 | // genKatiSuffix creates a suffix for kati-generated files so that we can cache |
| 37 | // them based on their inputs. So this should encode all common changes to Kati |
| 38 | // inputs. Currently that includes the TARGET_PRODUCT, kati-processed command |
| 39 | // line arguments, and the directories specified by mm/mmm. |
| 40 | func genKatiSuffix(ctx Context, config Config) { |
| 41 | katiSuffix := "-" + config.TargetProduct() |
| 42 | if args := config.KatiArgs(); len(args) > 0 { |
| 43 | katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_")) |
| 44 | } |
| 45 | if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok { |
| 46 | katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot) |
| 47 | } |
| 48 | |
| 49 | // If the suffix is too long, replace it with a md5 hash and write a |
| 50 | // file that contains the original suffix. |
| 51 | if len(katiSuffix) > 64 { |
| 52 | shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix))) |
| 53 | config.SetKatiSuffix(shortSuffix) |
| 54 | |
| 55 | ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix) |
| 56 | ctx.Verbosef("Replacing with: %q", shortSuffix) |
| 57 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 58 | if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 59 | ctx.Println("Error writing suffix file:", err) |
| 60 | } |
| 61 | } else { |
| 62 | config.SetKatiSuffix(katiSuffix) |
| 63 | } |
| 64 | } |
| 65 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 66 | func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) { |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 67 | executable := config.PrebuiltBuildTool("ckati") |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 68 | args = append([]string{ |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 69 | "--ninja", |
| 70 | "--ninja_dir=" + config.OutDir(), |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 71 | "--ninja_suffix=" + config.KatiSuffix() + extraSuffix, |
| 72 | "--no_ninja_prelude", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 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 | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 78 | "--use_find_emulator", |
Dan Willemsen | 418420e | 2017-05-30 14:07:45 -0700 | [diff] [blame] | 79 | "--werror_find_emulator", |
Dan Willemsen | d368d6f | 2018-06-15 21:53:18 -0700 | [diff] [blame] | 80 | "--no_builtin_rules", |
| 81 | "--werror_suffix_rules", |
Dan Willemsen | f880b58 | 2018-07-23 23:09:05 -0700 | [diff] [blame] | 82 | "--warn_real_to_phony", |
| 83 | "--warn_phony_looks_real", |
Dan Willemsen | 6097746 | 2019-04-18 09:40:15 -0700 | [diff] [blame^] | 84 | "--werror_real_to_phony", |
| 85 | "--werror_phony_looks_real", |
| 86 | "--werror_writable", |
Dan Willemsen | cc62890 | 2019-01-24 15:53:06 -0800 | [diff] [blame] | 87 | "--top_level_phony", |
Dan Willemsen | 75d2c17 | 2017-10-12 20:46:34 -0700 | [diff] [blame] | 88 | "--kati_stats", |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 89 | }, args...) |
| 90 | |
Dan Willemsen | f99915f | 2018-10-25 22:04:42 -0700 | [diff] [blame] | 91 | if config.Environment().IsEnvTrue("EMPTY_NINJA_FILE") { |
| 92 | args = append(args, "--empty_ninja_file") |
| 93 | } |
| 94 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 95 | cmd := Command(ctx, config, "ckati", executable, args...) |
| 96 | cmd.Sandbox = katiSandbox |
| 97 | 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 Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 103 | envFunc(cmd.Environment) |
| 104 | |
Dan Willemsen | 71edc8b | 2019-01-02 12:21:18 -0800 | [diff] [blame] | 105 | if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok { |
| 106 | u, err := user.Current() |
| 107 | if err != nil { |
| 108 | ctx.Println("Failed to get current user") |
| 109 | } |
| 110 | cmd.Environment.Set("BUILD_USERNAME", u.Username) |
| 111 | } |
| 112 | |
| 113 | if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok { |
| 114 | hostname, err := os.Hostname() |
| 115 | if err != nil { |
| 116 | ctx.Println("Failed to read hostname") |
| 117 | } |
| 118 | cmd.Environment.Set("BUILD_HOSTNAME", hostname) |
| 119 | } |
| 120 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 121 | cmd.StartOrFatal() |
| 122 | status.KatiReader(ctx.Status.StartTool(), pipe) |
| 123 | cmd.WaitOrFatal() |
| 124 | } |
| 125 | |
| 126 | func runKatiBuild(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 127 | ctx.BeginTrace(metrics.RunKati, "kati build") |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 128 | defer ctx.EndTrace() |
| 129 | |
| 130 | args := []string{ |
Dan Willemsen | 25a5618 | 2018-08-31 20:25:32 -0700 | [diff] [blame] | 131 | "--writable", config.OutDir() + "/", |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 132 | "-f", "build/make/core/main.mk", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 135 | // PDK builds still uses a few implicit rules |
| 136 | if !config.IsPdkBuild() { |
Dan Willemsen | 2bc456e | 2018-06-21 21:41:35 -0700 | [diff] [blame] | 137 | args = append(args, "--werror_implicit_rules") |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 140 | if !config.BuildBrokenDupRules() { |
| 141 | args = append(args, "--werror_overriding_commands") |
| 142 | } |
| 143 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 144 | args = append(args, config.KatiArgs()...) |
| 145 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 146 | args = append(args, |
| 147 | "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(), |
| 148 | "SOONG_ANDROID_MK="+config.SoongAndroidMk(), |
| 149 | "TARGET_DEVICE_DIR="+config.TargetDeviceDir(), |
| 150 | "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir()) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 151 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 152 | runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {}) |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | func runKatiPackage(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 156 | ctx.BeginTrace(metrics.RunKati, "kati package") |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 157 | defer ctx.EndTrace() |
| 158 | |
| 159 | args := []string{ |
| 160 | "--writable", config.DistDir() + "/", |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 161 | "--werror_implicit_rules", |
| 162 | "--werror_overriding_commands", |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 163 | "-f", "build/make/packaging/main.mk", |
| 164 | "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(), |
| 165 | } |
| 166 | |
| 167 | runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) { |
| 168 | env.Allow([]string{ |
| 169 | // Some generic basics |
| 170 | "LANG", |
| 171 | "LC_MESSAGES", |
| 172 | "PATH", |
| 173 | "PWD", |
| 174 | "TMPDIR", |
| 175 | |
| 176 | // Tool configs |
| 177 | "JAVA_HOME", |
| 178 | "PYTHONDONTWRITEBYTECODE", |
| 179 | |
| 180 | // Build configuration |
| 181 | "ANDROID_BUILD_SHELL", |
| 182 | "DIST_DIR", |
| 183 | "OUT_DIR", |
| 184 | }...) |
| 185 | |
| 186 | if config.Dist() { |
| 187 | env.Set("DIST", "true") |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 188 | env.Set("DIST_DIR", config.DistDir()) |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 189 | } |
| 190 | }) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 191 | } |
Dan Willemsen | 29f8827 | 2017-02-18 18:12:41 -0800 | [diff] [blame] | 192 | |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 193 | func runKatiCleanSpec(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 194 | ctx.BeginTrace(metrics.RunKati, "kati cleanspec") |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 195 | defer ctx.EndTrace() |
| 196 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 197 | runKati(ctx, config, katiCleanspecSuffix, []string{ |
| 198 | "--werror_implicit_rules", |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 199 | "--werror_overriding_commands", |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 200 | "-f", "build/make/core/cleanbuild.mk", |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 201 | "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(), |
| 202 | "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(), |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 203 | }, func(env *Environment) {}) |
Dan Willemsen | 59fdf96 | 2017-07-24 22:26:54 -0700 | [diff] [blame] | 204 | } |