blob: f6c0f52a396506a2591efc2733373132f5e5479d [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 }
Dan Willemsen1e704462016-08-21 15:17:17 -070045
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 Willemsen29971232018-09-26 14:58:30 -070055 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070056 ctx.Println("Error writing suffix file:", err)
57 }
58 } else {
59 config.SetKatiSuffix(katiSuffix)
60 }
61}
62
Dan Willemsenfb1271a2018-09-26 15:00:42 -070063func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Dan Willemsenf173d592017-04-27 14:28:00 -070064 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen29971232018-09-26 14:58:30 -070065 args = append([]string{
Dan Willemsen1e704462016-08-21 15:17:17 -070066 "--ninja",
67 "--ninja_dir=" + config.OutDir(),
Dan Willemsen29971232018-09-26 14:58:30 -070068 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
69 "--no_ninja_prelude",
Dan Willemsen6587bed2020-04-18 20:25:59 -070070 "--use_ninja_phony_output",
Jingwen Chence679d22020-09-23 04:30:02 +000071 "--use_ninja_symlink_outputs",
Dan Willemsen1e704462016-08-21 15:17:17 -070072 "--regen",
73 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
74 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080075 "--color_warnings",
76 "--gen_all_targets",
Dan Willemsen29971232018-09-26 14:58:30 -070077 "--use_find_emulator",
Dan Willemsen418420e2017-05-30 14:07:45 -070078 "--werror_find_emulator",
Dan Willemsend368d6f2018-06-15 21:53:18 -070079 "--no_builtin_rules",
80 "--werror_suffix_rules",
Dan Willemsenf880b582018-07-23 23:09:05 -070081 "--warn_real_to_phony",
82 "--warn_phony_looks_real",
Dan Willemsen60977462019-04-18 09:40:15 -070083 "--werror_real_to_phony",
84 "--werror_phony_looks_real",
85 "--werror_writable",
Dan Willemsencc628902019-01-24 15:53:06 -080086 "--top_level_phony",
Dan Willemsen75d2c172017-10-12 20:46:34 -070087 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -070088 }, args...)
89
Dan Willemsenf99915f2018-10-25 22:04:42 -070090 if config.Environment().IsEnvTrue("EMPTY_NINJA_FILE") {
91 args = append(args, "--empty_ninja_file")
92 }
93
Colin Cross9016b912019-11-11 14:57:42 -080094 if config.UseRemoteBuild() {
95 args = append(args, "--default_pool=local_pool")
96 }
97
Dan Willemsen29971232018-09-26 14:58:30 -070098 cmd := Command(ctx, config, "ckati", executable, args...)
99 cmd.Sandbox = katiSandbox
100 pipe, err := cmd.StdoutPipe()
101 if err != nil {
102 ctx.Fatalln("Error getting output pipe for ckati:", err)
103 }
104 cmd.Stderr = cmd.Stdout
105
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700106 envFunc(cmd.Environment)
107
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800108 if _, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
Dan Willemsena17ac962020-03-10 15:38:57 -0700109 username := "unknown"
110 if u, err := user.Current(); err == nil {
111 username = u.Username
112 } else {
113 ctx.Println("Failed to get current user:", err)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800114 }
Dan Willemsena17ac962020-03-10 15:38:57 -0700115 cmd.Environment.Set("BUILD_USERNAME", username)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800116 }
117
118 if _, ok := cmd.Environment.Get("BUILD_HOSTNAME"); !ok {
119 hostname, err := os.Hostname()
120 if err != nil {
Dan Willemsena17ac962020-03-10 15:38:57 -0700121 ctx.Println("Failed to read hostname:", err)
122 hostname = "unknown"
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800123 }
124 cmd.Environment.Set("BUILD_HOSTNAME", hostname)
125 }
126
Dan Willemsen29971232018-09-26 14:58:30 -0700127 cmd.StartOrFatal()
128 status.KatiReader(ctx.Status.StartTool(), pipe)
129 cmd.WaitOrFatal()
130}
131
132func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800133 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700134 defer ctx.EndTrace()
135
136 args := []string{
Dan Willemsen25a56182018-08-31 20:25:32 -0700137 "--writable", config.OutDir() + "/",
Dan Willemsen9f435972020-05-28 15:28:00 -0700138 "--werror_implicit_rules",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700139 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700140 }
141
Dan Willemsen3d60b112018-04-04 22:25:56 -0700142 if !config.BuildBrokenDupRules() {
143 args = append(args, "--werror_overriding_commands")
144 }
145
Dan Willemsen1e704462016-08-21 15:17:17 -0700146 args = append(args, config.KatiArgs()...)
147
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700148 args = append(args,
149 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
150 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
151 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
152 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700153
Dan Willemsen2d31a442018-10-20 21:33:41 -0700154 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsen78428262019-12-13 18:50:24 -0800155
Colin Cross8ba7d472020-06-25 11:27:52 -0700156 distGzipFile(ctx, config, config.KatiBuildNinjaFile())
157
Dan Willemsen78428262019-12-13 18:50:24 -0800158 cleanCopyHeaders(ctx, config)
Dan Willemsen1e775d72020-01-03 13:40:45 -0800159 cleanOldInstalledFiles(ctx, config)
Dan Willemsen78428262019-12-13 18:50:24 -0800160}
161
162func cleanCopyHeaders(ctx Context, config Config) {
163 ctx.BeginTrace("clean", "clean copy headers")
164 defer ctx.EndTrace()
165
166 data, err := ioutil.ReadFile(filepath.Join(config.ProductOut(), ".copied_headers_list"))
167 if err != nil {
168 if os.IsNotExist(err) {
169 return
170 }
171 ctx.Fatalf("Failed to read copied headers list: %v", err)
172 }
173
174 headers := strings.Fields(string(data))
175 if len(headers) < 1 {
176 ctx.Fatal("Failed to parse copied headers list: %q", string(data))
177 }
178 headerDir := headers[0]
179 headers = headers[1:]
180
181 filepath.Walk(headerDir,
182 func(path string, info os.FileInfo, err error) error {
183 if err != nil {
184 return nil
185 }
186 if info.IsDir() {
187 return nil
188 }
189 if !inList(path, headers) {
190 ctx.Printf("Removing obsolete header %q", path)
191 if err := os.Remove(path); err != nil {
192 ctx.Fatalf("Failed to remove obsolete header %q: %v", path, err)
193 }
194 }
195 return nil
196 })
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700197}
198
Dan Willemsen1e775d72020-01-03 13:40:45 -0800199func cleanOldInstalledFiles(ctx Context, config Config) {
200 ctx.BeginTrace("clean", "clean old installed files")
201 defer ctx.EndTrace()
202
203 // We shouldn't be removing files from one side of the two-step asan builds
204 var suffix string
205 if v, ok := config.Environment().Get("SANITIZE_TARGET"); ok {
206 if sanitize := strings.Fields(v); inList("address", sanitize) {
207 suffix = "_asan"
208 }
209 }
210
211 cleanOldFiles(ctx, config.ProductOut(), ".installable_files"+suffix)
212
213 cleanOldFiles(ctx, config.HostOut(), ".installable_test_files")
214}
215
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700216func runKatiPackage(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800217 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700218 defer ctx.EndTrace()
219
220 args := []string{
221 "--writable", config.DistDir() + "/",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700222 "--werror_implicit_rules",
223 "--werror_overriding_commands",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700224 "-f", "build/make/packaging/main.mk",
225 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
226 }
227
228 runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) {
229 env.Allow([]string{
230 // Some generic basics
231 "LANG",
232 "LC_MESSAGES",
233 "PATH",
234 "PWD",
235 "TMPDIR",
236
237 // Tool configs
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700238 "ASAN_SYMBOLIZER_PATH",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700239 "JAVA_HOME",
240 "PYTHONDONTWRITEBYTECODE",
241
242 // Build configuration
243 "ANDROID_BUILD_SHELL",
244 "DIST_DIR",
245 "OUT_DIR",
246 }...)
247
248 if config.Dist() {
249 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700250 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700251 }
252 })
Colin Cross8ba7d472020-06-25 11:27:52 -0700253
254 distGzipFile(ctx, config, config.KatiPackageNinjaFile())
Dan Willemsen1e704462016-08-21 15:17:17 -0700255}
Dan Willemsen29f88272017-02-18 18:12:41 -0800256
Dan Willemsen59fdf962017-07-24 22:26:54 -0700257func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800258 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700259 defer ctx.EndTrace()
260
Dan Willemsen29971232018-09-26 14:58:30 -0700261 runKati(ctx, config, katiCleanspecSuffix, []string{
262 "--werror_implicit_rules",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700263 "--werror_overriding_commands",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700264 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700265 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
266 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700267 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700268}