blob: 6519573ac6afcaf1b938889cbce986ae022536a0 [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 (
Cole Faust583dfb42023-09-28 13:56:30 -070018 "android/soong/ui/metrics"
19 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "crypto/md5"
21 "fmt"
22 "io/ioutil"
Dan Willemsen71edc8b2019-01-02 12:21:18 -080023 "os"
24 "os/user"
Dan Willemsen1e704462016-08-21 15:17:17 -070025 "path/filepath"
Dan Willemsen1e704462016-08-21 15:17:17 -070026 "strings"
27)
28
29var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
30
Dan Willemsen29971232018-09-26 14:58:30 -070031const katiBuildSuffix = ""
32const katiCleanspecSuffix = "-cleanspec"
Dan Willemsenfb1271a2018-09-26 15:00:42 -070033const katiPackageSuffix = "-package"
Cole Faustc5bfbdd2025-01-08 13:05:40 -080034const katiSoongOnlyPackageSuffix = "-soong-only-package"
Dan Willemsen29971232018-09-26 14:58:30 -070035
Jingwen Chenb1d30d62020-11-18 02:43:19 -050036// genKatiSuffix creates a filename suffix for kati-generated files so that we
37// can cache them based on their inputs. Such files include the generated Ninja
38// files and env.sh environment variable setup files.
39//
40// The filename suffix should encode all common changes to Kati inputs.
41// Currently that includes the TARGET_PRODUCT and kati-processed command line
42// arguments.
Dan Willemsen1e704462016-08-21 15:17:17 -070043func genKatiSuffix(ctx Context, config Config) {
Cole Faustc5bfbdd2025-01-08 13:05:40 -080044 targetProduct := "unknown"
45 if p, err := config.TargetProductOrErr(); err == nil {
46 targetProduct = p
47 }
Jingwen Chenb1d30d62020-11-18 02:43:19 -050048 // Construct the base suffix.
Cole Faustc5bfbdd2025-01-08 13:05:40 -080049 katiSuffix := "-" + targetProduct + config.CoverageSuffix()
Jingwen Chenb1d30d62020-11-18 02:43:19 -050050
51 // Append kati arguments to the suffix.
Dan Willemsen1e704462016-08-21 15:17:17 -070052 if args := config.KatiArgs(); len(args) > 0 {
53 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
54 }
Dan Willemsen1e704462016-08-21 15:17:17 -070055
56 // If the suffix is too long, replace it with a md5 hash and write a
57 // file that contains the original suffix.
58 if len(katiSuffix) > 64 {
59 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
60 config.SetKatiSuffix(shortSuffix)
61
62 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
63 ctx.Verbosef("Replacing with: %q", shortSuffix)
64
Dan Willemsen29971232018-09-26 14:58:30 -070065 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070066 ctx.Println("Error writing suffix file:", err)
67 }
68 } else {
69 config.SetKatiSuffix(katiSuffix)
70 }
71}
72
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090073func writeValueIfChanged(ctx Context, config Config, dir string, filename string, value string) {
74 filePath := filepath.Join(dir, filename)
75 previousValue := ""
Cole Faustc5bfbdd2025-01-08 13:05:40 -080076 rawPreviousValue, err := os.ReadFile(filePath)
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090077 if err == nil {
78 previousValue = string(rawPreviousValue)
79 }
80
81 if previousValue != value {
Cole Faustc5bfbdd2025-01-08 13:05:40 -080082 if err = os.WriteFile(filePath, []byte(value), 0666); err != nil {
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090083 ctx.Fatalf("Failed to write: %v", err)
84 }
85 }
86}
87
Jingwen Chenb1d30d62020-11-18 02:43:19 -050088// Base function to construct and run the Kati command line with additional
89// arguments, and a custom function closure to mutate the environment Kati runs
90// in.
Dan Willemsenfb1271a2018-09-26 15:00:42 -070091func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Taylor Santiago3c16e612024-05-30 14:41:31 -070092 executable := config.KatiBin()
Jingwen Chenb1d30d62020-11-18 02:43:19 -050093 // cKati arguments.
Dan Willemsen29971232018-09-26 14:58:30 -070094 args = append([]string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -050095 // Instead of executing commands directly, generate a Ninja file.
Dan Willemsen1e704462016-08-21 15:17:17 -070096 "--ninja",
Jingwen Chenb1d30d62020-11-18 02:43:19 -050097 // Generate Ninja files in the output directory.
Dan Willemsen1e704462016-08-21 15:17:17 -070098 "--ninja_dir=" + config.OutDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -050099 // Filename suffix of the generated Ninja file.
Dan Willemsen29971232018-09-26 14:58:30 -0700100 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500101 // Remove common parts at the beginning of a Ninja file, like build_dir,
102 // local_pool and _kati_always_build_. Allows Kati to be run multiple
103 // times, with generated Ninja files combined in a single invocation
104 // using 'include'.
Dan Willemsen29971232018-09-26 14:58:30 -0700105 "--no_ninja_prelude",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500106 // Support declaring phony outputs in AOSP Ninja.
Dan Willemsen6587bed2020-04-18 20:25:59 -0700107 "--use_ninja_phony_output",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500108 // Regenerate the Ninja file if environment inputs have changed. e.g.
109 // CLI flags, .mk file timestamps, env vars, $(wildcard ..) and some
110 // $(shell ..) results.
Dan Willemsen1e704462016-08-21 15:17:17 -0700111 "--regen",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500112 // Skip '-include' directives starting with the specified path. Used to
113 // ignore generated .mk files.
Dan Willemsen1e704462016-08-21 15:17:17 -0700114 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500115 // Detect the use of $(shell echo ...).
Dan Willemsen1e704462016-08-21 15:17:17 -0700116 "--detect_android_echo",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500117 // Colorful ANSI-based warning and error messages.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800118 "--color_warnings",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500119 // Generate all targets, not just the top level requested ones.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800120 "--gen_all_targets",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500121 // Use the built-in emulator of GNU find for better file finding
122 // performance. Used with $(shell find ...).
Dan Willemsen29971232018-09-26 14:58:30 -0700123 "--use_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500124 // Fail when the find emulator encounters problems.
Dan Willemsen418420e2017-05-30 14:07:45 -0700125 "--werror_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500126 // Do not provide any built-in rules.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700127 "--no_builtin_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500128 // Fail when suffix rules are used.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700129 "--werror_suffix_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500130 // Fail when a real target depends on a phony target.
Dan Willemsen60977462019-04-18 09:40:15 -0700131 "--werror_real_to_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500132 // Makes real_to_phony checks assume that any top-level or leaf
133 // dependencies that does *not* have a '/' in it is a phony target.
Dan Willemsencc628902019-01-24 15:53:06 -0800134 "--top_level_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500135 // Fail when a phony target contains slashes.
136 "--werror_phony_looks_real",
137 // Fail when writing to a read-only directory.
138 "--werror_writable",
139 // Print Kati's internal statistics, such as the number of variables,
140 // implicit/explicit/suffix rules, and so on.
Dan Willemsen75d2c172017-10-12 20:46:34 -0700141 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -0700142 }, args...)
143
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500144 // Generate a minimal Ninja file.
145 //
146 // Used for build_test and multiproduct_kati, which runs Kati several
147 // hundred times for different configurations to test file generation logic.
148 // These can result in generating Ninja files reaching ~1GB or more,
149 // resulting in ~hundreds of GBs of writes.
150 //
151 // Since we don't care about executing the Ninja files in these test cases,
152 // generating the Ninja file content wastes time, so skip writing any
153 // information out with --empty_ninja_file.
154 //
155 // From https://github.com/google/kati/commit/87b8da7af2c8bea28b1d8ab17679453d859f96e5
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700156 if config.EmptyNinjaFile() {
Dan Willemsenf99915f2018-10-25 22:04:42 -0700157 args = append(args, "--empty_ninja_file")
158 }
159
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500160 // Apply 'local_pool' to to all rules that don't specify a pool.
Colin Cross9016b912019-11-11 14:57:42 -0800161 if config.UseRemoteBuild() {
162 args = append(args, "--default_pool=local_pool")
163 }
164
Dan Willemsen29971232018-09-26 14:58:30 -0700165 cmd := Command(ctx, config, "ckati", executable, args...)
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500166
167 // Set up the nsjail sandbox.
Dan Willemsen29971232018-09-26 14:58:30 -0700168 cmd.Sandbox = katiSandbox
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500169
170 // Set up stdout and stderr.
Dan Willemsen29971232018-09-26 14:58:30 -0700171 pipe, err := cmd.StdoutPipe()
172 if err != nil {
173 ctx.Fatalln("Error getting output pipe for ckati:", err)
174 }
175 cmd.Stderr = cmd.Stdout
176
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900177 var username string
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500178 // Pass on various build environment metadata to Kati.
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900179 if usernameFromEnv, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
180 username = "unknown"
Dan Willemsena17ac962020-03-10 15:38:57 -0700181 if u, err := user.Current(); err == nil {
182 username = u.Username
183 } else {
184 ctx.Println("Failed to get current user:", err)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800185 }
Dan Willemsena17ac962020-03-10 15:38:57 -0700186 cmd.Environment.Set("BUILD_USERNAME", username)
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900187 } else {
188 username = usernameFromEnv
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800189 }
190
LaMont Jones99f18962024-10-17 11:50:44 -0700191 // SOONG_USE_PARTIAL_COMPILE may be used in makefiles, but both cases must be supported.
192 //
193 // In general, the partial compile features will be implemented in Soong-based rules. We
194 // also allow them to be used in makefiles. Clear the environment variable when calling
195 // kati so that we avoid reanalysis when the user changes it. We will pass it to Ninja.
196 // As a result, rules where we want to allow the developer to toggle the feature ("use
197 // the partial compile feature" vs "legacy, aka full compile behavior") need to use this
198 // in the rule, since changing it will not cause reanalysis.
199 //
200 // Shell syntax in the rule might look something like this:
201 // if [[ -n ${SOONG_USE_PARTIAL_COMPILE} ]]; then
202 // # partial compile behavior
203 // else
204 // # legacy behavior
205 // fi
206 cmd.Environment.Unset("SOONG_USE_PARTIAL_COMPILE")
207
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900208 // Unset BUILD_HOSTNAME during kati run to avoid kati rerun, kati will use BUILD_HOSTNAME from a file.
209 cmd.Environment.Unset("BUILD_HOSTNAME")
Cole Fauste14082c2025-01-06 15:38:34 -0800210
211 _, ok := cmd.Environment.Get("BUILD_NUMBER")
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900212 // Unset BUILD_NUMBER during kati run to avoid kati rerun, kati will use BUILD_NUMBER from a file.
213 cmd.Environment.Unset("BUILD_NUMBER")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900214 if ok {
215 cmd.Environment.Set("HAS_BUILD_NUMBER", "true")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900216 } else {
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900217 cmd.Environment.Set("HAS_BUILD_NUMBER", "false")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900218 }
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900219
220 // Apply the caller's function closure to mutate the environment variables.
221 envFunc(cmd.Environment)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800222
Dan Willemsen29971232018-09-26 14:58:30 -0700223 cmd.StartOrFatal()
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500224 // Set up the ToolStatus command line reader for Kati for a consistent UI
225 // for the user.
Dan Willemsen29971232018-09-26 14:58:30 -0700226 status.KatiReader(ctx.Status.StartTool(), pipe)
227 cmd.WaitOrFatal()
228}
229
230func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800231 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700232 defer ctx.EndTrace()
233
234 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500235 // Mark the output directory as writable.
Dan Willemsen25a56182018-08-31 20:25:32 -0700236 "--writable", config.OutDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500237 // Fail when encountering implicit rules. e.g.
238 // %.foo: %.bar
239 // cp $< $@
Dan Willemsen9f435972020-05-28 15:28:00 -0700240 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500241 // Entry point for the Kati Ninja file generation.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700242 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700243 }
244
Dan Willemsen3d60b112018-04-04 22:25:56 -0700245 if !config.BuildBrokenDupRules() {
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500246 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700247 args = append(args, "--werror_overriding_commands")
248 }
249
Dan Willemsen1e704462016-08-21 15:17:17 -0700250 args = append(args, config.KatiArgs()...)
251
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700252 args = append(args,
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500253 // Location of the Make vars .mk file generated by Soong.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700254 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500255 // Location of the Android.mk file generated by Soong. This
256 // file contains Soong modules represented as Kati modules,
257 // allowing Kati modules to depend on Soong modules.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700258 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500259 // Directory containing outputs for the target device.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700260 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500261 // Directory containing .mk files for packaging purposes, such as
262 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700263 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700264
Dan Willemsen2d31a442018-10-20 21:33:41 -0700265 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsen78428262019-12-13 18:50:24 -0800266
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500267 // compress and dist the main build ninja file.
Colin Cross8ba7d472020-06-25 11:27:52 -0700268 distGzipFile(ctx, config, config.KatiBuildNinjaFile())
269
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500270 // Cleanup steps.
Dan Willemsen78428262019-12-13 18:50:24 -0800271 cleanCopyHeaders(ctx, config)
Colin Cross41ad6b62021-03-09 12:02:15 -0800272 cleanOldInstalledFiles(ctx, config)
Dan Willemsen78428262019-12-13 18:50:24 -0800273}
274
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500275// Clean out obsolete header files on the disk that were *not copied* during the
276// build with BUILD_COPY_HEADERS and LOCAL_COPY_HEADERS.
277//
278// These should be increasingly uncommon, as it's a deprecated feature and there
279// isn't an equivalent feature in Soong.
Dan Willemsen78428262019-12-13 18:50:24 -0800280func cleanCopyHeaders(ctx Context, config Config) {
281 ctx.BeginTrace("clean", "clean copy headers")
282 defer ctx.EndTrace()
283
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500284 // Read and parse the list of copied headers from a file in the product
285 // output directory.
Dan Willemsen78428262019-12-13 18:50:24 -0800286 data, err := ioutil.ReadFile(filepath.Join(config.ProductOut(), ".copied_headers_list"))
287 if err != nil {
288 if os.IsNotExist(err) {
289 return
290 }
291 ctx.Fatalf("Failed to read copied headers list: %v", err)
292 }
293
294 headers := strings.Fields(string(data))
295 if len(headers) < 1 {
296 ctx.Fatal("Failed to parse copied headers list: %q", string(data))
297 }
298 headerDir := headers[0]
299 headers = headers[1:]
300
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500301 // Walk the tree and remove any headers that are not in the list of copied
302 // headers in the current build.
Dan Willemsen78428262019-12-13 18:50:24 -0800303 filepath.Walk(headerDir,
304 func(path string, info os.FileInfo, err error) error {
305 if err != nil {
306 return nil
307 }
308 if info.IsDir() {
309 return nil
310 }
311 if !inList(path, headers) {
312 ctx.Printf("Removing obsolete header %q", path)
313 if err := os.Remove(path); err != nil {
314 ctx.Fatalf("Failed to remove obsolete header %q: %v", path, err)
315 }
316 }
317 return nil
318 })
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700319}
320
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500321// Clean out any previously installed files from the disk that are not installed
322// in the current build.
Dan Willemsen1e775d72020-01-03 13:40:45 -0800323func cleanOldInstalledFiles(ctx Context, config Config) {
324 ctx.BeginTrace("clean", "clean old installed files")
325 defer ctx.EndTrace()
326
327 // We shouldn't be removing files from one side of the two-step asan builds
328 var suffix string
329 if v, ok := config.Environment().Get("SANITIZE_TARGET"); ok {
330 if sanitize := strings.Fields(v); inList("address", sanitize) {
331 suffix = "_asan"
332 }
333 }
334
335 cleanOldFiles(ctx, config.ProductOut(), ".installable_files"+suffix)
336
337 cleanOldFiles(ctx, config.HostOut(), ".installable_test_files")
338}
339
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500340// Generate the Ninja file containing the packaging command lines for the dist
341// dir.
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800342func runKatiPackage(ctx Context, config Config, soongOnly bool) {
Nan Zhang17f27672018-12-12 16:01:49 -0800343 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700344 defer ctx.EndTrace()
345
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800346 entryPoint := "build/make/packaging/main.mk"
347 suffix := katiPackageSuffix
348 ninjaFile := config.KatiPackageNinjaFile()
349 if soongOnly {
350 entryPoint = "build/make/packaging/main_soong_only.mk"
351 suffix = katiSoongOnlyPackageSuffix
352 ninjaFile = config.KatiSoongOnlyPackageNinjaFile()
353 }
354
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700355 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500356 // Mark the dist dir as writable.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700357 "--writable", config.DistDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500358 // Fail when encountering implicit rules. e.g.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700359 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500360 // Fail when redefining / duplicating a target.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700361 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500362 // Entry point.
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800363 "-f", entryPoint,
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500364 // Directory containing .mk files for packaging purposes, such as
365 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700366 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
367 }
368
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500369 // Run Kati against a restricted set of environment variables.
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800370 runKati(ctx, config, suffix, args, func(env *Environment) {
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700371 env.Allow([]string{
372 // Some generic basics
373 "LANG",
374 "LC_MESSAGES",
375 "PATH",
376 "PWD",
377 "TMPDIR",
378
379 // Tool configs
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700380 "ASAN_SYMBOLIZER_PATH",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700381 "JAVA_HOME",
382 "PYTHONDONTWRITEBYTECODE",
383
384 // Build configuration
385 "ANDROID_BUILD_SHELL",
386 "DIST_DIR",
387 "OUT_DIR",
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900388 "FILE_NAME_TAG",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700389 }...)
390
391 if config.Dist() {
392 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700393 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700394 }
395 })
Colin Cross8ba7d472020-06-25 11:27:52 -0700396
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500397 // Compress and dist the packaging Ninja file.
Cole Faustc5bfbdd2025-01-08 13:05:40 -0800398 distGzipFile(ctx, config, ninjaFile)
Dan Willemsen1e704462016-08-21 15:17:17 -0700399}
Dan Willemsen29f88272017-02-18 18:12:41 -0800400
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500401// Run Kati on the cleanspec files to clean the build.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700402func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800403 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700404 defer ctx.EndTrace()
405
Dan Willemsen29971232018-09-26 14:58:30 -0700406 runKati(ctx, config, katiCleanspecSuffix, []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500407 // Fail when encountering implicit rules. e.g.
Dan Willemsen29971232018-09-26 14:58:30 -0700408 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500409 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700410 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500411 // Entry point.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700412 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700413 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
414 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700415 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700416}