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