blob: aea56d31a452074359312ac86101919d16b65726 [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"
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090025 "time"
Dan Willemsenb82471a2018-05-17 16:37:09 -070026
Nan Zhang17f27672018-12-12 16:01:49 -080027 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070028 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070029)
30
31var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
32
Dan Willemsen29971232018-09-26 14:58:30 -070033const katiBuildSuffix = ""
34const katiCleanspecSuffix = "-cleanspec"
Dan Willemsenfb1271a2018-09-26 15:00:42 -070035const katiPackageSuffix = "-package"
Dan Willemsen29971232018-09-26 14:58:30 -070036
Jingwen Chenb1d30d62020-11-18 02:43:19 -050037// genKatiSuffix creates a filename suffix for kati-generated files so that we
38// can cache them based on their inputs. Such files include the generated Ninja
39// files and env.sh environment variable setup files.
40//
41// The filename suffix should encode all common changes to Kati inputs.
42// Currently that includes the TARGET_PRODUCT and kati-processed command line
43// arguments.
Dan Willemsen1e704462016-08-21 15:17:17 -070044func genKatiSuffix(ctx Context, config Config) {
Jingwen Chenb1d30d62020-11-18 02:43:19 -050045 // Construct the base suffix.
Dan Willemsen1e704462016-08-21 15:17:17 -070046 katiSuffix := "-" + config.TargetProduct()
Jingwen Chenb1d30d62020-11-18 02:43:19 -050047
48 // Append kati arguments to the suffix.
Dan Willemsen1e704462016-08-21 15:17:17 -070049 if args := config.KatiArgs(); len(args) > 0 {
50 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
51 }
Dan Willemsen1e704462016-08-21 15:17:17 -070052
53 // If the suffix is too long, replace it with a md5 hash and write a
54 // file that contains the original suffix.
55 if len(katiSuffix) > 64 {
56 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
57 config.SetKatiSuffix(shortSuffix)
58
59 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
60 ctx.Verbosef("Replacing with: %q", shortSuffix)
61
Dan Willemsen29971232018-09-26 14:58:30 -070062 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070063 ctx.Println("Error writing suffix file:", err)
64 }
65 } else {
66 config.SetKatiSuffix(katiSuffix)
67 }
68}
69
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090070func writeValueIfChanged(ctx Context, config Config, dir string, filename string, value string) {
71 filePath := filepath.Join(dir, filename)
72 previousValue := ""
73 rawPreviousValue, err := ioutil.ReadFile(filePath)
74 if err == nil {
75 previousValue = string(rawPreviousValue)
76 }
77
78 if previousValue != value {
79 if err = ioutil.WriteFile(filePath, []byte(value), 0666); err != nil {
80 ctx.Fatalf("Failed to write: %v", err)
81 }
82 }
83}
84
Jingwen Chenb1d30d62020-11-18 02:43:19 -050085// Base function to construct and run the Kati command line with additional
86// arguments, and a custom function closure to mutate the environment Kati runs
87// in.
Dan Willemsenfb1271a2018-09-26 15:00:42 -070088func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Dan Willemsenf173d592017-04-27 14:28:00 -070089 executable := config.PrebuiltBuildTool("ckati")
Jingwen Chenb1d30d62020-11-18 02:43:19 -050090 // cKati arguments.
Dan Willemsen29971232018-09-26 14:58:30 -070091 args = append([]string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -050092 // Instead of executing commands directly, generate a Ninja file.
Dan Willemsen1e704462016-08-21 15:17:17 -070093 "--ninja",
Jingwen Chenb1d30d62020-11-18 02:43:19 -050094 // Generate Ninja files in the output directory.
Dan Willemsen1e704462016-08-21 15:17:17 -070095 "--ninja_dir=" + config.OutDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -050096 // Filename suffix of the generated Ninja file.
Dan Willemsen29971232018-09-26 14:58:30 -070097 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
Jingwen Chenb1d30d62020-11-18 02:43:19 -050098 // Remove common parts at the beginning of a Ninja file, like build_dir,
99 // local_pool and _kati_always_build_. Allows Kati to be run multiple
100 // times, with generated Ninja files combined in a single invocation
101 // using 'include'.
Dan Willemsen29971232018-09-26 14:58:30 -0700102 "--no_ninja_prelude",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500103 // Support declaring phony outputs in AOSP Ninja.
Dan Willemsen6587bed2020-04-18 20:25:59 -0700104 "--use_ninja_phony_output",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500105 // Support declaring symlink outputs in AOSP Ninja.
Jingwen Chence679d22020-09-23 04:30:02 +0000106 "--use_ninja_symlink_outputs",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500107 // Regenerate the Ninja file if environment inputs have changed. e.g.
108 // CLI flags, .mk file timestamps, env vars, $(wildcard ..) and some
109 // $(shell ..) results.
Dan Willemsen1e704462016-08-21 15:17:17 -0700110 "--regen",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500111 // Skip '-include' directives starting with the specified path. Used to
112 // ignore generated .mk files.
Dan Willemsen1e704462016-08-21 15:17:17 -0700113 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500114 // Detect the use of $(shell echo ...).
Dan Willemsen1e704462016-08-21 15:17:17 -0700115 "--detect_android_echo",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500116 // Colorful ANSI-based warning and error messages.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800117 "--color_warnings",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500118 // Generate all targets, not just the top level requested ones.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800119 "--gen_all_targets",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500120 // Use the built-in emulator of GNU find for better file finding
121 // performance. Used with $(shell find ...).
Dan Willemsen29971232018-09-26 14:58:30 -0700122 "--use_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500123 // Fail when the find emulator encounters problems.
Dan Willemsen418420e2017-05-30 14:07:45 -0700124 "--werror_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500125 // Do not provide any built-in rules.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700126 "--no_builtin_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500127 // Fail when suffix rules are used.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700128 "--werror_suffix_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500129 // Fail when a real target depends on a phony target.
Dan Willemsen60977462019-04-18 09:40:15 -0700130 "--werror_real_to_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500131 // Makes real_to_phony checks assume that any top-level or leaf
132 // dependencies that does *not* have a '/' in it is a phony target.
Dan Willemsencc628902019-01-24 15:53:06 -0800133 "--top_level_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500134 // Fail when a phony target contains slashes.
135 "--werror_phony_looks_real",
136 // Fail when writing to a read-only directory.
137 "--werror_writable",
138 // Print Kati's internal statistics, such as the number of variables,
139 // implicit/explicit/suffix rules, and so on.
Dan Willemsen75d2c172017-10-12 20:46:34 -0700140 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -0700141 }, args...)
142
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500143 // Generate a minimal Ninja file.
144 //
145 // Used for build_test and multiproduct_kati, which runs Kati several
146 // hundred times for different configurations to test file generation logic.
147 // These can result in generating Ninja files reaching ~1GB or more,
148 // resulting in ~hundreds of GBs of writes.
149 //
150 // Since we don't care about executing the Ninja files in these test cases,
151 // generating the Ninja file content wastes time, so skip writing any
152 // information out with --empty_ninja_file.
153 //
154 // From https://github.com/google/kati/commit/87b8da7af2c8bea28b1d8ab17679453d859f96e5
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700155 if config.EmptyNinjaFile() {
Dan Willemsenf99915f2018-10-25 22:04:42 -0700156 args = append(args, "--empty_ninja_file")
157 }
158
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500159 // Apply 'local_pool' to to all rules that don't specify a pool.
Colin Cross9016b912019-11-11 14:57:42 -0800160 if config.UseRemoteBuild() {
161 args = append(args, "--default_pool=local_pool")
162 }
163
Dan Willemsen29971232018-09-26 14:58:30 -0700164 cmd := Command(ctx, config, "ckati", executable, args...)
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500165
166 // Set up the nsjail sandbox.
Dan Willemsen29971232018-09-26 14:58:30 -0700167 cmd.Sandbox = katiSandbox
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500168
169 // Set up stdout and stderr.
Dan Willemsen29971232018-09-26 14:58:30 -0700170 pipe, err := cmd.StdoutPipe()
171 if err != nil {
172 ctx.Fatalln("Error getting output pipe for ckati:", err)
173 }
174 cmd.Stderr = cmd.Stdout
175
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900176 var username string
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500177 // Pass on various build environment metadata to Kati.
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900178 if usernameFromEnv, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
179 username = "unknown"
Dan Willemsena17ac962020-03-10 15:38:57 -0700180 if u, err := user.Current(); err == nil {
181 username = u.Username
182 } else {
183 ctx.Println("Failed to get current user:", err)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800184 }
Dan Willemsena17ac962020-03-10 15:38:57 -0700185 cmd.Environment.Set("BUILD_USERNAME", username)
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900186 } else {
187 username = usernameFromEnv
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800188 }
189
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900190 hostname, ok := cmd.Environment.Get("BUILD_HOSTNAME")
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900191 // Unset BUILD_HOSTNAME during kati run to avoid kati rerun, kati will use BUILD_HOSTNAME from a file.
192 cmd.Environment.Unset("BUILD_HOSTNAME")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900193 if !ok {
194 hostname, err = os.Hostname()
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800195 if err != nil {
Dan Willemsena17ac962020-03-10 15:38:57 -0700196 ctx.Println("Failed to read hostname:", err)
197 hostname = "unknown"
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800198 }
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800199 }
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900200 writeValueIfChanged(ctx, config, config.SoongOutDir(), "build_hostname.txt", hostname)
201
202 // BUILD_NUMBER should be set to the source control value that
203 // represents the current state of the source code. E.g., a
204 // perforce changelist number or a git hash. Can be an arbitrary string
205 // (to allow for source control that uses something other than numbers),
206 // but must be a single word and a valid file name.
207 //
208 // If no BUILD_NUMBER is set, create a useful "I am an engineering build
209 // from this date/time" value. Make it start with a non-digit so that
210 // anyone trying to parse it as an integer will probably get "0".
211 cmd.Environment.Unset("HAS_BUILD_NUMBER")
212 buildNumber, ok := cmd.Environment.Get("BUILD_NUMBER")
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900213 // Unset BUILD_NUMBER during kati run to avoid kati rerun, kati will use BUILD_NUMBER from a file.
214 cmd.Environment.Unset("BUILD_NUMBER")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900215 if ok {
216 cmd.Environment.Set("HAS_BUILD_NUMBER", "true")
217 writeValueIfChanged(ctx, config, config.OutDir(), "file_name_tag.txt", buildNumber)
218 } else {
219 buildNumber = fmt.Sprintf("eng.%.6s.%s", username, time.Now().Format("20060102.150405" /* YYYYMMDD.HHMMSS */))
220 cmd.Environment.Set("HAS_BUILD_NUMBER", "false")
221 writeValueIfChanged(ctx, config, config.OutDir(), "file_name_tag.txt", username)
222 }
223 // Write the build number to a file so it can be read back in
224 // without changing the command line every time. Avoids rebuilds
225 // when using ninja.
226 writeValueIfChanged(ctx, config, config.SoongOutDir(), "build_number.txt", buildNumber)
227
228 // Apply the caller's function closure to mutate the environment variables.
229 envFunc(cmd.Environment)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800230
Dan Willemsen29971232018-09-26 14:58:30 -0700231 cmd.StartOrFatal()
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500232 // Set up the ToolStatus command line reader for Kati for a consistent UI
233 // for the user.
Dan Willemsen29971232018-09-26 14:58:30 -0700234 status.KatiReader(ctx.Status.StartTool(), pipe)
235 cmd.WaitOrFatal()
236}
237
238func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800239 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700240 defer ctx.EndTrace()
241
242 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500243 // Mark the output directory as writable.
Dan Willemsen25a56182018-08-31 20:25:32 -0700244 "--writable", config.OutDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500245 // Fail when encountering implicit rules. e.g.
246 // %.foo: %.bar
247 // cp $< $@
Dan Willemsen9f435972020-05-28 15:28:00 -0700248 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500249 // Entry point for the Kati Ninja file generation.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700250 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700251 }
252
Dan Willemsen3d60b112018-04-04 22:25:56 -0700253 if !config.BuildBrokenDupRules() {
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500254 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700255 args = append(args, "--werror_overriding_commands")
256 }
257
Dan Willemsen1e704462016-08-21 15:17:17 -0700258 args = append(args, config.KatiArgs()...)
259
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700260 args = append(args,
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500261 // Location of the Make vars .mk file generated by Soong.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700262 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500263 // Location of the Android.mk file generated by Soong. This
264 // file contains Soong modules represented as Kati modules,
265 // allowing Kati modules to depend on Soong modules.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700266 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500267 // Directory containing outputs for the target device.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700268 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500269 // Directory containing .mk files for packaging purposes, such as
270 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700271 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700272
Dan Willemsen2d31a442018-10-20 21:33:41 -0700273 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsen78428262019-12-13 18:50:24 -0800274
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500275 // compress and dist the main build ninja file.
Colin Cross8ba7d472020-06-25 11:27:52 -0700276 distGzipFile(ctx, config, config.KatiBuildNinjaFile())
277
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500278 // Cleanup steps.
Dan Willemsen78428262019-12-13 18:50:24 -0800279 cleanCopyHeaders(ctx, config)
Colin Cross41ad6b62021-03-09 12:02:15 -0800280 cleanOldInstalledFiles(ctx, config)
Dan Willemsen78428262019-12-13 18:50:24 -0800281}
282
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500283// Clean out obsolete header files on the disk that were *not copied* during the
284// build with BUILD_COPY_HEADERS and LOCAL_COPY_HEADERS.
285//
286// These should be increasingly uncommon, as it's a deprecated feature and there
287// isn't an equivalent feature in Soong.
Dan Willemsen78428262019-12-13 18:50:24 -0800288func cleanCopyHeaders(ctx Context, config Config) {
289 ctx.BeginTrace("clean", "clean copy headers")
290 defer ctx.EndTrace()
291
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500292 // Read and parse the list of copied headers from a file in the product
293 // output directory.
Dan Willemsen78428262019-12-13 18:50:24 -0800294 data, err := ioutil.ReadFile(filepath.Join(config.ProductOut(), ".copied_headers_list"))
295 if err != nil {
296 if os.IsNotExist(err) {
297 return
298 }
299 ctx.Fatalf("Failed to read copied headers list: %v", err)
300 }
301
302 headers := strings.Fields(string(data))
303 if len(headers) < 1 {
304 ctx.Fatal("Failed to parse copied headers list: %q", string(data))
305 }
306 headerDir := headers[0]
307 headers = headers[1:]
308
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500309 // Walk the tree and remove any headers that are not in the list of copied
310 // headers in the current build.
Dan Willemsen78428262019-12-13 18:50:24 -0800311 filepath.Walk(headerDir,
312 func(path string, info os.FileInfo, err error) error {
313 if err != nil {
314 return nil
315 }
316 if info.IsDir() {
317 return nil
318 }
319 if !inList(path, headers) {
320 ctx.Printf("Removing obsolete header %q", path)
321 if err := os.Remove(path); err != nil {
322 ctx.Fatalf("Failed to remove obsolete header %q: %v", path, err)
323 }
324 }
325 return nil
326 })
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700327}
328
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500329// Clean out any previously installed files from the disk that are not installed
330// in the current build.
Dan Willemsen1e775d72020-01-03 13:40:45 -0800331func cleanOldInstalledFiles(ctx Context, config Config) {
332 ctx.BeginTrace("clean", "clean old installed files")
333 defer ctx.EndTrace()
334
335 // We shouldn't be removing files from one side of the two-step asan builds
336 var suffix string
337 if v, ok := config.Environment().Get("SANITIZE_TARGET"); ok {
338 if sanitize := strings.Fields(v); inList("address", sanitize) {
339 suffix = "_asan"
340 }
341 }
342
343 cleanOldFiles(ctx, config.ProductOut(), ".installable_files"+suffix)
344
345 cleanOldFiles(ctx, config.HostOut(), ".installable_test_files")
346}
347
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500348// Generate the Ninja file containing the packaging command lines for the dist
349// dir.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700350func runKatiPackage(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800351 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700352 defer ctx.EndTrace()
353
354 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500355 // Mark the dist dir as writable.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700356 "--writable", config.DistDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500357 // Fail when encountering implicit rules. e.g.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700358 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500359 // Fail when redefining / duplicating a target.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700360 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500361 // Entry point.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700362 "-f", "build/make/packaging/main.mk",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500363 // Directory containing .mk files for packaging purposes, such as
364 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700365 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
366 }
367
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500368 // Run Kati against a restricted set of environment variables.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700369 runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) {
370 env.Allow([]string{
371 // Some generic basics
372 "LANG",
373 "LC_MESSAGES",
374 "PATH",
375 "PWD",
376 "TMPDIR",
377
378 // Tool configs
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700379 "ASAN_SYMBOLIZER_PATH",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700380 "JAVA_HOME",
381 "PYTHONDONTWRITEBYTECODE",
382
383 // Build configuration
384 "ANDROID_BUILD_SHELL",
385 "DIST_DIR",
386 "OUT_DIR",
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900387 "FILE_NAME_TAG",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700388 }...)
389
390 if config.Dist() {
391 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700392 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700393 }
394 })
Colin Cross8ba7d472020-06-25 11:27:52 -0700395
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500396 // Compress and dist the packaging Ninja file.
Colin Cross8ba7d472020-06-25 11:27:52 -0700397 distGzipFile(ctx, config, config.KatiPackageNinjaFile())
Dan Willemsen1e704462016-08-21 15:17:17 -0700398}
Dan Willemsen29f88272017-02-18 18:12:41 -0800399
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500400// Run Kati on the cleanspec files to clean the build.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700401func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800402 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700403 defer ctx.EndTrace()
404
Dan Willemsen29971232018-09-26 14:58:30 -0700405 runKati(ctx, config, katiCleanspecSuffix, []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500406 // Fail when encountering implicit rules. e.g.
Dan Willemsen29971232018-09-26 14:58:30 -0700407 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500408 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700409 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500410 // Entry point.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700411 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700412 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
413 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700414 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700415}