blob: 959d0bdf46c0c9607904862c6a97467021e35688 [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 (
18 "crypto/md5"
19 "fmt"
20 "io/ioutil"
Dan Willemsen71edc8b2019-01-02 12:21:18 -080021 "os"
22 "os/user"
Dan Willemsen1e704462016-08-21 15:17:17 -070023 "path/filepath"
Dan Willemsen1e704462016-08-21 15:17:17 -070024 "strings"
Dan Willemsenb82471a2018-05-17 16:37:09 -070025
Nan Zhang17f27672018-12-12 16:01:49 -080026 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070027 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070028)
29
30var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
31
Dan Willemsen29971232018-09-26 14:58:30 -070032const katiBuildSuffix = ""
33const katiCleanspecSuffix = "-cleanspec"
Dan Willemsenfb1271a2018-09-26 15:00:42 -070034const katiPackageSuffix = "-package"
Dan Willemsen29971232018-09-26 14:58:30 -070035
Dan Willemsen1e704462016-08-21 15:17:17 -070036// 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.
40func 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 Willemsen29971232018-09-26 14:58:30 -070058 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070059 ctx.Println("Error writing suffix file:", err)
60 }
61 } else {
62 config.SetKatiSuffix(katiSuffix)
63 }
64}
65
Dan Willemsenfb1271a2018-09-26 15:00:42 -070066func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Dan Willemsenf173d592017-04-27 14:28:00 -070067 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen29971232018-09-26 14:58:30 -070068 args = append([]string{
Dan Willemsen1e704462016-08-21 15:17:17 -070069 "--ninja",
70 "--ninja_dir=" + config.OutDir(),
Dan Willemsen29971232018-09-26 14:58:30 -070071 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
72 "--no_ninja_prelude",
Dan Willemsen1e704462016-08-21 15:17:17 -070073 "--regen",
74 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
75 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080076 "--color_warnings",
77 "--gen_all_targets",
Dan Willemsen29971232018-09-26 14:58:30 -070078 "--use_find_emulator",
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 Willemsenf880b582018-07-23 23:09:05 -070082 "--warn_real_to_phony",
83 "--warn_phony_looks_real",
Dan Willemsencc628902019-01-24 15:53:06 -080084 "--top_level_phony",
Dan Willemsen75d2c172017-10-12 20:46:34 -070085 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -070086 }, args...)
87
Dan Willemsenf99915f2018-10-25 22:04:42 -070088 if config.Environment().IsEnvTrue("EMPTY_NINJA_FILE") {
89 args = append(args, "--empty_ninja_file")
90 }
91
Dan Willemsen29971232018-09-26 14:58:30 -070092 cmd := Command(ctx, config, "ckati", executable, args...)
93 cmd.Sandbox = katiSandbox
94 pipe, err := cmd.StdoutPipe()
95 if err != nil {
96 ctx.Fatalln("Error getting output pipe for ckati:", err)
97 }
98 cmd.Stderr = cmd.Stdout
99
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700100 envFunc(cmd.Environment)
101
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800102 if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
103 u, err := user.Current()
104 if err != nil {
105 ctx.Println("Failed to get current user")
106 }
107 cmd.Environment.Set("BUILD_USERNAME", u.Username)
108 }
109
110 if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok {
111 hostname, err := os.Hostname()
112 if err != nil {
113 ctx.Println("Failed to read hostname")
114 }
115 cmd.Environment.Set("BUILD_HOSTNAME", hostname)
116 }
117
Dan Willemsen29971232018-09-26 14:58:30 -0700118 cmd.StartOrFatal()
119 status.KatiReader(ctx.Status.StartTool(), pipe)
120 cmd.WaitOrFatal()
121}
122
123func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800124 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700125 defer ctx.EndTrace()
126
127 args := []string{
Dan Willemsen25a56182018-08-31 20:25:32 -0700128 "--writable", config.OutDir() + "/",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700129 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700130 }
131
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700132 // PDK builds still uses a few implicit rules
133 if !config.IsPdkBuild() {
Dan Willemsen2bc456e2018-06-21 21:41:35 -0700134 args = append(args, "--werror_implicit_rules")
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700135 }
136
Dan Willemsen3d60b112018-04-04 22:25:56 -0700137 if !config.BuildBrokenDupRules() {
138 args = append(args, "--werror_overriding_commands")
139 }
140
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700141 if !config.BuildBrokenPhonyTargets() {
Dan Willemsen25a56182018-08-31 20:25:32 -0700142 args = append(args,
143 "--werror_real_to_phony",
144 "--werror_phony_looks_real",
145 "--werror_writable")
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700146 }
147
Dan Willemsen1e704462016-08-21 15:17:17 -0700148 args = append(args, config.KatiArgs()...)
149
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700150 args = append(args,
151 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
152 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
153 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
154 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700155
Dan Willemsen2d31a442018-10-20 21:33:41 -0700156 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700157}
158
159func runKatiPackage(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800160 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700161 defer ctx.EndTrace()
162
163 args := []string{
164 "--writable", config.DistDir() + "/",
165 "--werror_writable",
166 "--werror_implicit_rules",
167 "--werror_overriding_commands",
168 "--werror_real_to_phony",
169 "--werror_phony_looks_real",
170 "-f", "build/make/packaging/main.mk",
171 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
172 }
173
174 runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) {
175 env.Allow([]string{
176 // Some generic basics
177 "LANG",
178 "LC_MESSAGES",
179 "PATH",
180 "PWD",
181 "TMPDIR",
182
183 // Tool configs
184 "JAVA_HOME",
185 "PYTHONDONTWRITEBYTECODE",
186
187 // Build configuration
188 "ANDROID_BUILD_SHELL",
189 "DIST_DIR",
190 "OUT_DIR",
191 }...)
192
193 if config.Dist() {
194 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700195 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700196 }
197 })
Dan Willemsen1e704462016-08-21 15:17:17 -0700198}
Dan Willemsen29f88272017-02-18 18:12:41 -0800199
Dan Willemsen59fdf962017-07-24 22:26:54 -0700200func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800201 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700202 defer ctx.EndTrace()
203
Dan Willemsen29971232018-09-26 14:58:30 -0700204 runKati(ctx, config, katiCleanspecSuffix, []string{
Dan Willemsendcac0782019-02-04 20:58:25 -0800205 "--werror_writable",
Dan Willemsen29971232018-09-26 14:58:30 -0700206 "--werror_implicit_rules",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700207 "--werror_overriding_commands",
Dan Willemsendcac0782019-02-04 20:58:25 -0800208 "--werror_real_to_phony",
209 "--werror_phony_looks_real",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700210 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700211 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
212 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700213 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700214}