blob: 4dfb7108ba23204a217b76ec4d223c1a6dc66e50 [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"
Dan Willemsen29971232018-09-26 14:58:30 -070034
Jingwen Chenb1d30d62020-11-18 02:43:19 -050035// genKatiSuffix creates a filename suffix for kati-generated files so that we
36// can cache them based on their inputs. Such files include the generated Ninja
37// files and env.sh environment variable setup files.
38//
39// The filename suffix should encode all common changes to Kati inputs.
40// Currently that includes the TARGET_PRODUCT and kati-processed command line
41// arguments.
Dan Willemsen1e704462016-08-21 15:17:17 -070042func genKatiSuffix(ctx Context, config Config) {
Jingwen Chenb1d30d62020-11-18 02:43:19 -050043 // Construct the base suffix.
Qing Shen713c5422024-08-23 04:09:18 +000044 katiSuffix := "-" + config.TargetProduct() + config.CoverageSuffix()
Jingwen Chenb1d30d62020-11-18 02:43:19 -050045
46 // Append kati arguments to the suffix.
Dan Willemsen1e704462016-08-21 15:17:17 -070047 if args := config.KatiArgs(); len(args) > 0 {
48 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
49 }
Dan Willemsen1e704462016-08-21 15:17:17 -070050
51 // If the suffix is too long, replace it with a md5 hash and write a
52 // file that contains the original suffix.
53 if len(katiSuffix) > 64 {
54 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
55 config.SetKatiSuffix(shortSuffix)
56
57 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
58 ctx.Verbosef("Replacing with: %q", shortSuffix)
59
Dan Willemsen29971232018-09-26 14:58:30 -070060 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070061 ctx.Println("Error writing suffix file:", err)
62 }
63 } else {
64 config.SetKatiSuffix(katiSuffix)
65 }
66}
67
Jeongik Cha4e49bbd2023-04-26 21:06:24 +090068func writeValueIfChanged(ctx Context, config Config, dir string, filename string, value string) {
69 filePath := filepath.Join(dir, filename)
70 previousValue := ""
71 rawPreviousValue, err := ioutil.ReadFile(filePath)
72 if err == nil {
73 previousValue = string(rawPreviousValue)
74 }
75
76 if previousValue != value {
77 if err = ioutil.WriteFile(filePath, []byte(value), 0666); err != nil {
78 ctx.Fatalf("Failed to write: %v", err)
79 }
80 }
81}
82
Jingwen Chenb1d30d62020-11-18 02:43:19 -050083// Base function to construct and run the Kati command line with additional
84// arguments, and a custom function closure to mutate the environment Kati runs
85// in.
Dan Willemsenfb1271a2018-09-26 15:00:42 -070086func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Taylor Santiago3c16e612024-05-30 14:41:31 -070087 executable := config.KatiBin()
Jingwen Chenb1d30d62020-11-18 02:43:19 -050088 // cKati arguments.
Dan Willemsen29971232018-09-26 14:58:30 -070089 args = append([]string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -050090 // Instead of executing commands directly, generate a Ninja file.
Dan Willemsen1e704462016-08-21 15:17:17 -070091 "--ninja",
Jingwen Chenb1d30d62020-11-18 02:43:19 -050092 // Generate Ninja files in the output directory.
Dan Willemsen1e704462016-08-21 15:17:17 -070093 "--ninja_dir=" + config.OutDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -050094 // Filename suffix of the generated Ninja file.
Dan Willemsen29971232018-09-26 14:58:30 -070095 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
Jingwen Chenb1d30d62020-11-18 02:43:19 -050096 // Remove common parts at the beginning of a Ninja file, like build_dir,
97 // local_pool and _kati_always_build_. Allows Kati to be run multiple
98 // times, with generated Ninja files combined in a single invocation
99 // using 'include'.
Dan Willemsen29971232018-09-26 14:58:30 -0700100 "--no_ninja_prelude",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500101 // Support declaring phony outputs in AOSP Ninja.
Dan Willemsen6587bed2020-04-18 20:25:59 -0700102 "--use_ninja_phony_output",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500103 // Regenerate the Ninja file if environment inputs have changed. e.g.
104 // CLI flags, .mk file timestamps, env vars, $(wildcard ..) and some
105 // $(shell ..) results.
Dan Willemsen1e704462016-08-21 15:17:17 -0700106 "--regen",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500107 // Skip '-include' directives starting with the specified path. Used to
108 // ignore generated .mk files.
Dan Willemsen1e704462016-08-21 15:17:17 -0700109 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500110 // Detect the use of $(shell echo ...).
Dan Willemsen1e704462016-08-21 15:17:17 -0700111 "--detect_android_echo",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500112 // Colorful ANSI-based warning and error messages.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800113 "--color_warnings",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500114 // Generate all targets, not just the top level requested ones.
Dan Willemsenc38d3662017-02-24 10:53:23 -0800115 "--gen_all_targets",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500116 // Use the built-in emulator of GNU find for better file finding
117 // performance. Used with $(shell find ...).
Dan Willemsen29971232018-09-26 14:58:30 -0700118 "--use_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500119 // Fail when the find emulator encounters problems.
Dan Willemsen418420e2017-05-30 14:07:45 -0700120 "--werror_find_emulator",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500121 // Do not provide any built-in rules.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700122 "--no_builtin_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500123 // Fail when suffix rules are used.
Dan Willemsend368d6f2018-06-15 21:53:18 -0700124 "--werror_suffix_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500125 // Fail when a real target depends on a phony target.
Dan Willemsen60977462019-04-18 09:40:15 -0700126 "--werror_real_to_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500127 // Makes real_to_phony checks assume that any top-level or leaf
128 // dependencies that does *not* have a '/' in it is a phony target.
Dan Willemsencc628902019-01-24 15:53:06 -0800129 "--top_level_phony",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500130 // Fail when a phony target contains slashes.
131 "--werror_phony_looks_real",
132 // Fail when writing to a read-only directory.
133 "--werror_writable",
134 // Print Kati's internal statistics, such as the number of variables,
135 // implicit/explicit/suffix rules, and so on.
Dan Willemsen75d2c172017-10-12 20:46:34 -0700136 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -0700137 }, args...)
138
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500139 // Generate a minimal Ninja file.
140 //
141 // Used for build_test and multiproduct_kati, which runs Kati several
142 // hundred times for different configurations to test file generation logic.
143 // These can result in generating Ninja files reaching ~1GB or more,
144 // resulting in ~hundreds of GBs of writes.
145 //
146 // Since we don't care about executing the Ninja files in these test cases,
147 // generating the Ninja file content wastes time, so skip writing any
148 // information out with --empty_ninja_file.
149 //
150 // From https://github.com/google/kati/commit/87b8da7af2c8bea28b1d8ab17679453d859f96e5
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700151 if config.EmptyNinjaFile() {
Dan Willemsenf99915f2018-10-25 22:04:42 -0700152 args = append(args, "--empty_ninja_file")
153 }
154
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500155 // Apply 'local_pool' to to all rules that don't specify a pool.
Colin Cross9016b912019-11-11 14:57:42 -0800156 if config.UseRemoteBuild() {
157 args = append(args, "--default_pool=local_pool")
158 }
159
Dan Willemsen29971232018-09-26 14:58:30 -0700160 cmd := Command(ctx, config, "ckati", executable, args...)
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500161
162 // Set up the nsjail sandbox.
Dan Willemsen29971232018-09-26 14:58:30 -0700163 cmd.Sandbox = katiSandbox
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500164
165 // Set up stdout and stderr.
Dan Willemsen29971232018-09-26 14:58:30 -0700166 pipe, err := cmd.StdoutPipe()
167 if err != nil {
168 ctx.Fatalln("Error getting output pipe for ckati:", err)
169 }
170 cmd.Stderr = cmd.Stdout
171
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900172 var username string
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500173 // Pass on various build environment metadata to Kati.
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900174 if usernameFromEnv, ok := cmd.Environment.Get("BUILD_USERNAME"); !ok {
175 username = "unknown"
Dan Willemsena17ac962020-03-10 15:38:57 -0700176 if u, err := user.Current(); err == nil {
177 username = u.Username
178 } else {
179 ctx.Println("Failed to get current user:", err)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800180 }
Dan Willemsena17ac962020-03-10 15:38:57 -0700181 cmd.Environment.Set("BUILD_USERNAME", username)
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900182 } else {
183 username = usernameFromEnv
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800184 }
185
LaMont Jones99f18962024-10-17 11:50:44 -0700186 // SOONG_USE_PARTIAL_COMPILE may be used in makefiles, but both cases must be supported.
187 //
188 // In general, the partial compile features will be implemented in Soong-based rules. We
189 // also allow them to be used in makefiles. Clear the environment variable when calling
190 // kati so that we avoid reanalysis when the user changes it. We will pass it to Ninja.
191 // As a result, rules where we want to allow the developer to toggle the feature ("use
192 // the partial compile feature" vs "legacy, aka full compile behavior") need to use this
193 // in the rule, since changing it will not cause reanalysis.
194 //
195 // Shell syntax in the rule might look something like this:
196 // if [[ -n ${SOONG_USE_PARTIAL_COMPILE} ]]; then
197 // # partial compile behavior
198 // else
199 // # legacy behavior
200 // fi
201 cmd.Environment.Unset("SOONG_USE_PARTIAL_COMPILE")
202
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900203 hostname, ok := cmd.Environment.Get("BUILD_HOSTNAME")
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900204 // Unset BUILD_HOSTNAME during kati run to avoid kati rerun, kati will use BUILD_HOSTNAME from a file.
205 cmd.Environment.Unset("BUILD_HOSTNAME")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900206 if !ok {
207 hostname, err = os.Hostname()
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800208 if err != nil {
Dan Willemsena17ac962020-03-10 15:38:57 -0700209 ctx.Println("Failed to read hostname:", err)
210 hostname = "unknown"
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800211 }
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800212 }
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900213 writeValueIfChanged(ctx, config, config.SoongOutDir(), "build_hostname.txt", hostname)
Cole Faust583dfb42023-09-28 13:56:30 -0700214 _, ok = cmd.Environment.Get("BUILD_NUMBER")
Jeongik Chaf2af38d2023-05-23 06:59:39 +0900215 // Unset BUILD_NUMBER during kati run to avoid kati rerun, kati will use BUILD_NUMBER from a file.
216 cmd.Environment.Unset("BUILD_NUMBER")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900217 if ok {
218 cmd.Environment.Set("HAS_BUILD_NUMBER", "true")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900219 } else {
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900220 cmd.Environment.Set("HAS_BUILD_NUMBER", "false")
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900221 }
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900222
223 // Apply the caller's function closure to mutate the environment variables.
224 envFunc(cmd.Environment)
Dan Willemsen71edc8b2019-01-02 12:21:18 -0800225
Dan Willemsen29971232018-09-26 14:58:30 -0700226 cmd.StartOrFatal()
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500227 // Set up the ToolStatus command line reader for Kati for a consistent UI
228 // for the user.
Dan Willemsen29971232018-09-26 14:58:30 -0700229 status.KatiReader(ctx.Status.StartTool(), pipe)
230 cmd.WaitOrFatal()
231}
232
233func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800234 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700235 defer ctx.EndTrace()
236
237 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500238 // Mark the output directory as writable.
Dan Willemsen25a56182018-08-31 20:25:32 -0700239 "--writable", config.OutDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500240 // Fail when encountering implicit rules. e.g.
241 // %.foo: %.bar
242 // cp $< $@
Dan Willemsen9f435972020-05-28 15:28:00 -0700243 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500244 // Entry point for the Kati Ninja file generation.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700245 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700246 }
247
Dan Willemsen3d60b112018-04-04 22:25:56 -0700248 if !config.BuildBrokenDupRules() {
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500249 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700250 args = append(args, "--werror_overriding_commands")
251 }
252
Dan Willemsen1e704462016-08-21 15:17:17 -0700253 args = append(args, config.KatiArgs()...)
254
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700255 args = append(args,
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500256 // Location of the Make vars .mk file generated by Soong.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700257 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500258 // Location of the Android.mk file generated by Soong. This
259 // file contains Soong modules represented as Kati modules,
260 // allowing Kati modules to depend on Soong modules.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700261 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500262 // Directory containing outputs for the target device.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700263 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500264 // Directory containing .mk files for packaging purposes, such as
265 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700266 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700267
Dan Willemsen2d31a442018-10-20 21:33:41 -0700268 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsen78428262019-12-13 18:50:24 -0800269
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500270 // compress and dist the main build ninja file.
Colin Cross8ba7d472020-06-25 11:27:52 -0700271 distGzipFile(ctx, config, config.KatiBuildNinjaFile())
272
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500273 // Cleanup steps.
Dan Willemsen78428262019-12-13 18:50:24 -0800274 cleanCopyHeaders(ctx, config)
Colin Cross41ad6b62021-03-09 12:02:15 -0800275 cleanOldInstalledFiles(ctx, config)
Dan Willemsen78428262019-12-13 18:50:24 -0800276}
277
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500278// Clean out obsolete header files on the disk that were *not copied* during the
279// build with BUILD_COPY_HEADERS and LOCAL_COPY_HEADERS.
280//
281// These should be increasingly uncommon, as it's a deprecated feature and there
282// isn't an equivalent feature in Soong.
Dan Willemsen78428262019-12-13 18:50:24 -0800283func cleanCopyHeaders(ctx Context, config Config) {
284 ctx.BeginTrace("clean", "clean copy headers")
285 defer ctx.EndTrace()
286
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500287 // Read and parse the list of copied headers from a file in the product
288 // output directory.
Dan Willemsen78428262019-12-13 18:50:24 -0800289 data, err := ioutil.ReadFile(filepath.Join(config.ProductOut(), ".copied_headers_list"))
290 if err != nil {
291 if os.IsNotExist(err) {
292 return
293 }
294 ctx.Fatalf("Failed to read copied headers list: %v", err)
295 }
296
297 headers := strings.Fields(string(data))
298 if len(headers) < 1 {
299 ctx.Fatal("Failed to parse copied headers list: %q", string(data))
300 }
301 headerDir := headers[0]
302 headers = headers[1:]
303
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500304 // Walk the tree and remove any headers that are not in the list of copied
305 // headers in the current build.
Dan Willemsen78428262019-12-13 18:50:24 -0800306 filepath.Walk(headerDir,
307 func(path string, info os.FileInfo, err error) error {
308 if err != nil {
309 return nil
310 }
311 if info.IsDir() {
312 return nil
313 }
314 if !inList(path, headers) {
315 ctx.Printf("Removing obsolete header %q", path)
316 if err := os.Remove(path); err != nil {
317 ctx.Fatalf("Failed to remove obsolete header %q: %v", path, err)
318 }
319 }
320 return nil
321 })
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700322}
323
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500324// Clean out any previously installed files from the disk that are not installed
325// in the current build.
Dan Willemsen1e775d72020-01-03 13:40:45 -0800326func cleanOldInstalledFiles(ctx Context, config Config) {
327 ctx.BeginTrace("clean", "clean old installed files")
328 defer ctx.EndTrace()
329
330 // We shouldn't be removing files from one side of the two-step asan builds
331 var suffix string
332 if v, ok := config.Environment().Get("SANITIZE_TARGET"); ok {
333 if sanitize := strings.Fields(v); inList("address", sanitize) {
334 suffix = "_asan"
335 }
336 }
337
338 cleanOldFiles(ctx, config.ProductOut(), ".installable_files"+suffix)
339
340 cleanOldFiles(ctx, config.HostOut(), ".installable_test_files")
341}
342
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500343// Generate the Ninja file containing the packaging command lines for the dist
344// dir.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700345func runKatiPackage(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800346 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700347 defer ctx.EndTrace()
348
349 args := []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500350 // Mark the dist dir as writable.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700351 "--writable", config.DistDir() + "/",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500352 // Fail when encountering implicit rules. e.g.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700353 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500354 // Fail when redefining / duplicating a target.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700355 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500356 // Entry point.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700357 "-f", "build/make/packaging/main.mk",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500358 // Directory containing .mk files for packaging purposes, such as
359 // the dist.mk file, containing dist-for-goals data.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700360 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
361 }
362
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500363 // Run Kati against a restricted set of environment variables.
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700364 runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) {
365 env.Allow([]string{
366 // Some generic basics
367 "LANG",
368 "LC_MESSAGES",
369 "PATH",
370 "PWD",
371 "TMPDIR",
372
373 // Tool configs
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700374 "ASAN_SYMBOLIZER_PATH",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700375 "JAVA_HOME",
376 "PYTHONDONTWRITEBYTECODE",
377
378 // Build configuration
379 "ANDROID_BUILD_SHELL",
380 "DIST_DIR",
381 "OUT_DIR",
Jeongik Cha4e49bbd2023-04-26 21:06:24 +0900382 "FILE_NAME_TAG",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700383 }...)
384
385 if config.Dist() {
386 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700387 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700388 }
389 })
Colin Cross8ba7d472020-06-25 11:27:52 -0700390
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500391 // Compress and dist the packaging Ninja file.
Colin Cross8ba7d472020-06-25 11:27:52 -0700392 distGzipFile(ctx, config, config.KatiPackageNinjaFile())
Dan Willemsen1e704462016-08-21 15:17:17 -0700393}
Dan Willemsen29f88272017-02-18 18:12:41 -0800394
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500395// Run Kati on the cleanspec files to clean the build.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700396func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800397 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700398 defer ctx.EndTrace()
399
Dan Willemsen29971232018-09-26 14:58:30 -0700400 runKati(ctx, config, katiCleanspecSuffix, []string{
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500401 // Fail when encountering implicit rules. e.g.
Dan Willemsen29971232018-09-26 14:58:30 -0700402 "--werror_implicit_rules",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500403 // Fail when redefining / duplicating a target.
Dan Willemsen3d60b112018-04-04 22:25:56 -0700404 "--werror_overriding_commands",
Jingwen Chenb1d30d62020-11-18 02:43:19 -0500405 // Entry point.
Dan Willemsen59fdf962017-07-24 22:26:54 -0700406 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700407 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
408 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700409 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700410}