blob: 2a00c412406f23577b77c886c782ed731b11d87a [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 (
Kousik Kumar3ff037e2022-01-25 22:11:01 -050018 "encoding/json"
Jeongik Chaa87506f2023-06-01 23:16:41 +090019 "errors"
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040020 "fmt"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050021 "io/ioutil"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040022 "math/rand"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080023 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050024 "os/exec"
Cole Faust583dfb42023-09-28 13:56:30 -070025 "os/user"
Dan Willemsen1e704462016-08-21 15:17:17 -070026 "path/filepath"
27 "runtime"
28 "strconv"
29 "strings"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040030 "syscall"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080031 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070032
LaMont Jones54b01cd2024-10-23 13:59:40 -070033 "android/soong/finder/fs"
Jeff Gastonefc1b412017-03-29 17:29:06 -070034 "android/soong/shared"
LaMont Jones9a912862023-11-06 22:11:08 +000035 "android/soong/ui/metrics"
Kousik Kumarec478642020-09-21 13:39:24 -040036
Dan Willemsen4591b642021-05-24 14:24:12 -070037 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000038
39 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070040)
41
Kousik Kumar3ff037e2022-01-25 22:11:01 -050042const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050043 envConfigDir = "vendor/google/tools/soong_config"
44 jsonSuffix = "json"
Taylor Santiago8b0bed72024-09-03 13:30:22 -070045 abfsSrcDir = "/src"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050046)
47
Kousik Kumar4c180ad2022-05-27 07:48:37 -040048var (
Kevin Dagostino096ab2f2023-03-03 19:47:17 +000049 rbeRandPrefix int
50 googleProdCredsExistCache bool
Kousik Kumar4c180ad2022-05-27 07:48:37 -040051)
52
53func init() {
54 rand.Seed(time.Now().UnixNano())
55 rbeRandPrefix = rand.Intn(1000)
56}
57
LaMont Jonesece626c2024-09-03 11:19:31 -070058// Which builder are we using?
59type ninjaCommandType = int
60
61const (
62 _ = iota
63 NINJA_NINJA
64 NINJA_N2
65 NINJA_SISO
Taylor Santiago2fa40d02025-01-20 20:36:37 -080066 NINJA_NINJAGO
LaMont Jonesece626c2024-09-03 11:19:31 -070067)
68
Dan Willemsen1e704462016-08-21 15:17:17 -070069type Config struct{ *configImpl }
70
71type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020072 // Some targets that are implemented in soong_build
Colin Cross28f527c2019-11-26 16:19:04 -080073 arguments []string
74 goma bool
75 environ *Environment
76 distDir string
77 buildDateTime string
MarkDacek6614d9c2022-12-07 21:57:38 +000078 logsPrefix string
Dan Willemsen1e704462016-08-21 15:17:17 -070079
80 // From the arguments
Joe Onorato8ae66df2025-01-29 13:21:58 -080081 parallel int
82 keepGoing int
83 verbose bool
84 checkbuild bool
85 dist bool
86 jsonModuleGraph bool
87 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
88 soongDocs bool
89 skipConfig bool
90 // Either the user or product config requested that we skip soong (for the banner). The other
91 // skip flags tell whether *this* soong_ui invocation will skip kati - which will be true
92 // during lunch.
93 soongOnlyRequested bool
Cole Faust3740b282025-01-21 15:59:50 -080094 skipKati bool
95 skipKatiControlledByFlags bool
96 skipKatiNinja bool
97 skipSoong bool
98 skipNinja bool
99 skipSoongTests bool
100 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
101 skipMetricsUpload bool
102 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
103 buildFromSourceStub bool
104 incrementalBuildActions bool
105 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
Dan Willemsen1e704462016-08-21 15:17:17 -0700106
107 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700108 katiArgs []string
109 ninjaArgs []string
110 katiSuffix string
111 targetDevice string
112 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +0000113 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -0700114
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800115 // Autodetected
LaMont Jones54b01cd2024-10-23 13:59:40 -0700116 totalRAM uint64
117 systemCpuInfo *metrics.CpuInfo
118 systemMemInfo *metrics.MemInfo
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800119
Spandan Das28a6f192024-07-01 21:00:25 +0000120 brokenDupRules bool
121 brokenUsesNetwork bool
122 brokenNinjaEnvVars []string
123 brokenMissingOutputs bool
Dan Willemsen18490112018-05-25 16:30:04 -0700124
125 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000126
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700127 // Set by multiproduct_kati
128 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700129
130 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000131
Sam Delmerico98a73292023-02-21 11:50:29 -0500132 includeTags []string
133 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900134
135 // Data source to write ninja weight list
136 ninjaWeightListSource NinjaWeightListSource
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800137
138 // This file is a detailed dump of all soong-defined modules for debugging purposes.
139 // There's quite a bit of overlap with module-info.json and soong module graph. We
140 // could consider merging them.
141 moduleDebugFile string
Cole Faustbee030d2024-01-03 13:45:48 -0800142
LaMont Jonesece626c2024-09-03 11:19:31 -0700143 // Which builder are we using
144 ninjaCommand ninjaCommandType
Dan Willemsen1e704462016-08-21 15:17:17 -0700145}
146
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900147type NinjaWeightListSource uint
148
149const (
150 // ninja doesn't use weight list.
151 NOT_USED NinjaWeightListSource = iota
152 // ninja uses weight list based on previous builds by ninja log
153 NINJA_LOG
154 // ninja thinks every task has the same weight.
155 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900156 // ninja uses an external custom weight list
157 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900158 // ninja uses a prioritized module list from Soong
159 HINT_FROM_SOONG
Jeongik Chaa87506f2023-06-01 23:16:41 +0900160 // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead.
161 // We can assume it is an incremental build if ninja log exists.
162 DEFAULT
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900163)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800164const srcDirFileCheck = "build/soong/root.bp"
165
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700166var buildFiles = []string{"Android.mk", "Android.bp"}
167
Patrice Arruda13848222019-04-22 17:12:02 -0700168type BuildAction uint
169
170const (
171 // Builds all of the modules and their dependencies of a specified directory, relative to the root
172 // directory of the source tree.
173 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
174
175 // Builds all of the modules and their dependencies of a list of specified directories. All specified
176 // directories are relative to the root directory of the source tree.
177 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700178
179 // Build a list of specified modules. If none was specified, simply build the whole source tree.
180 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700181)
182
183// checkTopDir validates that the current directory is at the root directory of the source tree.
184func checkTopDir(ctx Context) {
185 if _, err := os.Stat(srcDirFileCheck); err != nil {
186 if os.IsNotExist(err) {
187 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
188 }
189 ctx.Fatalln("Error verifying tree state:", err)
190 }
191}
192
MarkDacek7901e582023-01-09 19:48:01 +0000193func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500194 if bc == "" {
195 return nil
196 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500197
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500198 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500199 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500200 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500201 envConfigDir,
202 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500203 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500204 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
205 envVarsJSON, err := ioutil.ReadFile(cfgFile)
206 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500207 continue
208 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500209 ctx.Verbosef("Loading config file %v\n", cfgFile)
210 var envVars map[string]map[string]string
211 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
212 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
213 continue
214 }
215 for k, v := range envVars["env"] {
216 if os.Getenv(k) != "" {
217 continue
218 }
219 config.environ.Set(k, v)
220 }
221 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
222 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500223 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500224
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500225 return nil
226}
227
Dan Willemsen1e704462016-08-21 15:17:17 -0700228func NewConfig(ctx Context, args ...string) Config {
229 ret := &configImpl{
Jeongik Chaf2ecf762023-05-19 14:03:45 +0900230 environ: OsEnvironment(),
231 sandboxConfig: &SandboxConfig{},
Jeongik Chaa87506f2023-06-01 23:16:41 +0900232 ninjaWeightListSource: DEFAULT,
Dan Willemsen1e704462016-08-21 15:17:17 -0700233 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700234 wd, err := os.Getwd()
235 if err != nil {
236 ctx.Fatalln("Failed to get working directory:", err)
237 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700238
Colin Cross106d6ef2023-10-24 10:34:56 -0700239 // Skip soong tests by default on Linux
240 if runtime.GOOS == "linux" {
241 ret.skipSoongTests = true
242 }
243
Patrice Arruda90109172020-07-28 18:07:27 +0000244 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700245 ret.parallel = runtime.NumCPU() + 2
246 ret.keepGoing = 1
247
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800248 ret.totalRAM = detectTotalRAM(ctx)
LaMont Jones54b01cd2024-10-23 13:59:40 -0700249 ret.systemCpuInfo, err = metrics.NewCpuInfo(fs.OsFs)
250 if err != nil {
251 ctx.Fatalln("Failed to get cpuinfo:", err)
252 }
253 ret.systemMemInfo, err = metrics.NewMemInfo(fs.OsFs)
254 if err != nil {
255 ctx.Fatalln("Failed to get meminfo:", err)
256 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700257 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900258
Cole Faustca27d212025-01-24 10:59:39 -0800259 if value, ok := ret.environ.Get("SOONG_ONLY"); ok && !ret.skipKatiControlledByFlags {
260 if value == "true" || value == "1" || value == "y" || value == "yes" {
Joe Onorato8ae66df2025-01-29 13:21:58 -0800261 ret.soongOnlyRequested = true
Cole Faustca27d212025-01-24 10:59:39 -0800262 ret.skipKatiControlledByFlags = true
263 ret.skipKati = true
264 ret.skipKatiNinja = true
265 } else {
266 ret.skipKatiControlledByFlags = true
267 ret.skipKati = false
268 ret.skipKatiNinja = false
269 }
270 }
271
Jeongik Chae114e602023-03-19 00:12:39 +0900272 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
Jeongik Chaa87506f2023-06-01 23:16:41 +0900273 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always")
274 } else if ret.ninjaWeightListSource == DEFAULT {
275 defaultNinjaWeightListSource := NINJA_LOG
276 if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) {
277 ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead")
278 defaultNinjaWeightListSource = HINT_FROM_SOONG
279 } else {
280 ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG")
281 }
282 ret.ninjaWeightListSource = defaultNinjaWeightListSource
283 // soong_build generates ninja hint depending on ninja log existence.
284 // Set it "depend" to avoid soong re-run due to env variable change.
285 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend")
Jeongik Chae114e602023-03-19 00:12:39 +0900286 }
Jeongik Chaa87506f2023-06-01 23:16:41 +0900287
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800288 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700289 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700290 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, filepath.Clean(outDir)))
Dan Willemsen02f3add2017-05-12 13:50:19 -0700291 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800292 outDir := "out"
293 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700294 outDir = filepath.Join(baseDir, filepath.Base(wd))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800295 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700296 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, outDir))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800297 }
298
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500299 // loadEnvConfig needs to know what the OUT_DIR is, so it should
300 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000301 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
302
303 if bc != "" {
Kousik Kumarc8818332023-01-16 16:33:05 +0000304 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000305 ctx.Fatalln("Failed to parse env config files: %v", err)
306 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +0000307 if !ret.canSupportRBE() {
308 // Explicitly set USE_RBE env variable to false when we cannot run
309 // an RBE build to avoid ninja local execution pool issues.
310 ret.environ.Set("USE_RBE", "false")
311 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500312 }
313
Dan Willemsen2d31a442018-10-20 21:33:41 -0700314 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
315 ret.distDir = filepath.Clean(distDir)
316 } else {
317 ret.distDir = filepath.Join(ret.OutDir(), "dist")
318 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700319
Spandan Das05063612021-06-25 01:39:04 +0000320 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
321 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
322 }
323
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800324 if os.Getenv("GENERATE_SOONG_DEBUG") == "true" {
325 ret.moduleDebugFile, _ = filepath.Abs(shared.JoinPath(ret.SoongOutDir(), "soong-debug-info.json"))
326 }
327
LaMont Jones99f18962024-10-17 11:50:44 -0700328 // If SOONG_USE_PARTIAL_COMPILE is set, make it one of "true" or the empty string.
329 // This simplifies the generated Ninja rules, so that they only need to check for the empty string.
LaMont Jonesb547c7e2024-12-19 09:52:01 -0800330 if value, ok := ret.environ.Get("SOONG_USE_PARTIAL_COMPILE"); ok {
LaMont Jones99f18962024-10-17 11:50:44 -0700331 if value == "true" || value == "1" || value == "y" || value == "yes" {
332 value = "true"
333 } else {
334 value = ""
335 }
LaMont Jonesb547c7e2024-12-19 09:52:01 -0800336 ret.environ.Set("SOONG_USE_PARTIAL_COMPILE", value)
LaMont Jones99f18962024-10-17 11:50:44 -0700337 }
338
LaMont Jonesece626c2024-09-03 11:19:31 -0700339 ret.ninjaCommand = NINJA_NINJA
340 switch os.Getenv("SOONG_NINJA") {
341 case "n2":
342 ret.ninjaCommand = NINJA_N2
343 case "siso":
344 ret.ninjaCommand = NINJA_SISO
Taylor Santiago2fa40d02025-01-20 20:36:37 -0800345 case "ninjago":
346 ret.ninjaCommand = NINJA_NINJAGO
LaMont Jonesece626c2024-09-03 11:19:31 -0700347 default:
348 if os.Getenv("SOONG_USE_N2") == "true" {
349 ret.ninjaCommand = NINJA_N2
350 }
Cole Faustbee030d2024-01-03 13:45:48 -0800351 }
352
Dan Willemsen1e704462016-08-21 15:17:17 -0700353 ret.environ.Unset(
354 // We're already using it
355 "USE_SOONG_UI",
356
357 // We should never use GOROOT/GOPATH from the shell environment
358 "GOROOT",
359 "GOPATH",
360
361 // These should only come from Soong, not the environment.
362 "CLANG",
363 "CLANG_CXX",
364 "CCC_CC",
365 "CCC_CXX",
366
367 // Used by the goma compiler wrapper, but should only be set by
368 // gomacc
369 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800370
371 // We handle this above
372 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700373
Dan Willemsen2d31a442018-10-20 21:33:41 -0700374 // This is handled above too, and set for individual commands later
375 "DIST_DIR",
376
Dan Willemsen68a09852017-04-18 13:56:57 -0700377 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000378 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700379 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700380 "DISPLAY",
381 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000382 "JAVAC",
Nathan Egge978c9342024-07-03 21:13:03 +0000383 "LEX",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700384 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700385 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700386
387 // Drop make flags
388 "MAKEFLAGS",
389 "MAKELEVEL",
390 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700391
392 // Set in envsetup.sh, reset in makefiles
393 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700394
395 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
396 "ANDROID_BUILD_TOP",
397 "ANDROID_HOST_OUT",
398 "ANDROID_PRODUCT_OUT",
399 "ANDROID_HOST_OUT_TESTCASES",
400 "ANDROID_TARGET_OUT_TESTCASES",
401 "ANDROID_TOOLCHAIN",
402 "ANDROID_TOOLCHAIN_2ND_ARCH",
403 "ANDROID_DEV_SCRIPTS",
404 "ANDROID_EMULATOR_PREBUILTS",
405 "ANDROID_PRE_BUILD_PATHS",
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800406
407 // We read it here already, don't let others share in the fun
408 "GENERATE_SOONG_DEBUG",
Cole Faustbee030d2024-01-03 13:45:48 -0800409
LaMont Jonesece626c2024-09-03 11:19:31 -0700410 // Use config.ninjaCommand instead.
411 "SOONG_NINJA",
Cole Faustbee030d2024-01-03 13:45:48 -0800412 "SOONG_USE_N2",
Cole Faustca27d212025-01-24 10:59:39 -0800413
414 // Already incorporated into the config object
415 "SOONG_ONLY",
Dan Willemsen1e704462016-08-21 15:17:17 -0700416 )
417
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400418 if ret.UseGoma() || ret.ForceUseGoma() {
419 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
420 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400421 }
422
Dan Willemsen1e704462016-08-21 15:17:17 -0700423 // Tell python not to spam the source tree with .pyc files.
424 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
425
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400426 tmpDir := absPath(ctx, ret.TempDir())
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700427 ret.environ.Set("TMPDIR", ret.sandboxPath(wd, tmpDir))
Dan Willemsen32a669b2018-03-08 19:42:00 -0800428
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700429 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
430 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
431 "llvm-binutils-stable/llvm-symbolizer")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700432 ret.environ.Set("ASAN_SYMBOLIZER_PATH", ret.sandboxPath(wd, absPath(ctx, symbolizerPath)))
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700433
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800434 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700435 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800436
Yu Liu6e13b402021-07-27 14:29:06 -0700437 srcDir := absPath(ctx, ".")
438 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700439 ctx.Println("You are building in a directory whose absolute path contains a space character:")
440 ctx.Println()
441 ctx.Printf("%q\n", srcDir)
442 ctx.Println()
443 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700444 }
445
Yu Liu6e13b402021-07-27 14:29:06 -0700446 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
447
Dan Willemsendb8457c2017-05-12 16:38:17 -0700448 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700449 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
450 ctx.Println()
451 ctx.Printf("%q\n", outDir)
452 ctx.Println()
453 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700454 }
455
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000456 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700457 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
458 ctx.Println()
459 ctx.Printf("%q\n", distDir)
460 ctx.Println()
461 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700462 }
463
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700464 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000465 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
Sorin Basca0760c892023-11-29 19:13:55 +0000466 java21Home := filepath.Join("prebuilts/jdk/jdk21", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700467 javaHome := func() string {
468 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
469 return override
470 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000471 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000472 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
Pete Gillin1f52e932019-10-09 17:10:08 +0100473 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000474 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000475 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
Sorin Basca7e094b32022-10-05 08:20:12 +0000476 }
Sorin Basca5dfa2382024-03-11 17:23:06 +0000477 if toolchain21, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN"); ok && toolchain21 != "true" {
478 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
479 }
480 return java21Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700481 }()
482 absJavaHome := absPath(ctx, javaHome)
483
Dan Willemsened869522018-01-08 14:58:46 -0800484 ret.configureLocale(ctx)
485
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000486 newPath := []string{filepath.Join(absJavaHome, "bin")}
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700487 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000488 newPath = append(newPath, path)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700489 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100490
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700491 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700492 ret.environ.Set("JAVA_HOME", ret.sandboxPath(wd, absJavaHome))
493 ret.environ.Set("ANDROID_JAVA_HOME", ret.sandboxPath(wd, javaHome))
494 ret.environ.Set("ANDROID_JAVA8_HOME", ret.sandboxPath(wd, java8Home))
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700495 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
496
Colin Crossfe5ed4d2023-07-28 09:27:23 -0700497 // b/286885495, https://bugzilla.redhat.com/show_bug.cgi?id=2227130: some versions of Fedora include patches
498 // to unzip to enable zipbomb detection that incorrectly handle zip64 and data descriptors and fail on large
499 // zip files produced by soong_zip. Disable zipbomb detection.
500 ret.environ.Set("UNZIP_DISABLE_ZIPBOMB_DETECTION", "TRUE")
501
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800502 outDir := ret.OutDir()
503 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800504 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800505 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800506 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800507 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800508 }
Colin Cross28f527c2019-11-26 16:19:04 -0800509
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700510 ret.environ.Set("BUILD_DATETIME_FILE", ret.sandboxPath(wd, buildDateTimeFile))
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800511
Cole Faust583dfb42023-09-28 13:56:30 -0700512 if _, ok := ret.environ.Get("BUILD_USERNAME"); !ok {
513 username := "unknown"
514 if u, err := user.Current(); err == nil {
515 username = u.Username
516 } else {
517 ctx.Println("Failed to get current user:", err)
518 }
519 ret.environ.Set("BUILD_USERNAME", username)
520 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700521 ret.environ.Set("PWD", ret.sandboxPath(wd, wd))
Cole Faust583dfb42023-09-28 13:56:30 -0700522
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400523 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400524 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400525 ret.environ.Set(k, v)
526 }
527 }
528
Patrice Arruda96850362020-08-11 20:41:11 +0000529 c := Config{ret}
530 storeConfigMetrics(ctx, c)
531 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700532}
533
Patrice Arruda13848222019-04-22 17:12:02 -0700534// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
535// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700536func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
537 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700538}
539
LaMont Jones9a912862023-11-06 22:11:08 +0000540// Prepare for getting make variables. For them to be accurate, we need to have
541// obtained PRODUCT_RELEASE_CONFIG_MAPS.
542//
543// Returns:
544//
545// Whether config should be called again.
546//
547// TODO: when converting product config to a declarative language, make sure
548// that PRODUCT_RELEASE_CONFIG_MAPS is properly handled as a separate step in
549// that process.
550func SetProductReleaseConfigMaps(ctx Context, config Config) bool {
551 ctx.BeginTrace(metrics.RunKati, "SetProductReleaseConfigMaps")
552 defer ctx.EndTrace()
553
554 if config.SkipConfig() {
555 // This duplicates the logic from Build to skip product config
556 // if the user has explicitly said to.
557 return false
558 }
559
560 releaseConfigVars := []string{
561 "PRODUCT_RELEASE_CONFIG_MAPS",
562 }
563
564 origValue, _ := config.environ.Get("PRODUCT_RELEASE_CONFIG_MAPS")
565 // Get the PRODUCT_RELEASE_CONFIG_MAPS for this product, to avoid polluting the environment
566 // when we run product config to get the rest of the make vars.
567 releaseMapVars, err := dumpMakeVars(ctx, config, nil, releaseConfigVars, false, "")
568 if err != nil {
569 ctx.Fatalln("Error getting PRODUCT_RELEASE_CONFIG_MAPS:", err)
570 }
571 productReleaseConfigMaps := releaseMapVars["PRODUCT_RELEASE_CONFIG_MAPS"]
572 os.Setenv("PRODUCT_RELEASE_CONFIG_MAPS", productReleaseConfigMaps)
573 return origValue != productReleaseConfigMaps
574}
575
Patrice Arruda96850362020-08-11 20:41:11 +0000576// storeConfigMetrics selects a set of configuration information and store in
577// the metrics system for further analysis.
578func storeConfigMetrics(ctx Context, config Config) {
579 if ctx.Metrics == nil {
580 return
581 }
582
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400583 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000584
LaMont Jones54b01cd2024-10-23 13:59:40 -0700585 cpuInfo := &smpb.SystemCpuInfo{
586 VendorId: proto.String(config.systemCpuInfo.VendorId),
587 ModelName: proto.String(config.systemCpuInfo.ModelName),
588 CpuCores: proto.Int32(config.systemCpuInfo.CpuCores),
589 Flags: proto.String(config.systemCpuInfo.Flags),
590 }
591 memInfo := &smpb.SystemMemInfo{
592 MemTotal: proto.Uint64(config.systemMemInfo.MemTotal),
593 MemFree: proto.Uint64(config.systemMemInfo.MemFree),
594 MemAvailable: proto.Uint64(config.systemMemInfo.MemAvailable),
595 }
596
Patrice Arruda3edfd482020-10-13 23:58:41 +0000597 s := &smpb.SystemResourceInfo{
598 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
599 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
LaMont Jones54b01cd2024-10-23 13:59:40 -0700600 CpuInfo: cpuInfo,
601 MemInfo: memInfo,
Patrice Arruda3edfd482020-10-13 23:58:41 +0000602 }
603 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000604}
605
Jeongik Cha8d63d562023-03-17 03:52:13 +0900606func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900607 switch s {
608 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900609 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900610 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900611 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900612 case EXTERNAL_FILE:
613 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900614 case HINT_FROM_SOONG:
615 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900616 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900617 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900618 }
619}
620
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400621func buildConfig(config Config) *smpb.BuildConfig {
LaMont Jonesb547c7e2024-12-19 09:52:01 -0800622 var soongEnvVars *smpb.SoongEnvVars
623 ensure := func() *smpb.SoongEnvVars {
624 // Create soongEnvVars if it doesn't already exist.
625 if soongEnvVars == nil {
626 soongEnvVars = &smpb.SoongEnvVars{}
627 }
628 return soongEnvVars
629 }
630 if value, ok := config.environ.Get("SOONG_PARTIAL_COMPILE"); ok {
631 ensure().PartialCompile = proto.String(value)
632 }
633 if value, ok := config.environ.Get("SOONG_USE_PARTIAL_COMPILE"); ok {
634 ensure().UsePartialCompile = proto.String(value)
635 }
Yu Liue737a992021-10-04 13:21:41 -0700636 c := &smpb.BuildConfig{
Colin Cross8d411ff2023-12-07 10:31:24 -0800637 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
638 UseGoma: proto.Bool(config.UseGoma()),
639 UseRbe: proto.Bool(config.UseRBE()),
640 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
LaMont Jonesb547c7e2024-12-19 09:52:01 -0800641 SoongEnvVars: soongEnvVars,
Joe Onoratoe84ec902025-01-29 17:56:57 -0800642 SoongOnly: proto.Bool(config.soongOnlyRequested),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400643 }
Yu Liue737a992021-10-04 13:21:41 -0700644 c.Targets = append(c.Targets, config.arguments...)
645
646 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400647}
648
Patrice Arruda13848222019-04-22 17:12:02 -0700649// getConfigArgs processes the command arguments based on the build action and creates a set of new
650// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700651func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700652 // The next block of code verifies that the current directory is the root directory of the source
653 // tree. It then finds the relative path of dir based on the root directory of the source tree
654 // and verify that dir is inside of the source tree.
655 checkTopDir(ctx)
656 topDir, err := os.Getwd()
657 if err != nil {
658 ctx.Fatalf("Error retrieving top directory: %v", err)
659 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700660 dir, err = filepath.EvalSymlinks(dir)
661 if err != nil {
662 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
663 }
Patrice Arruda13848222019-04-22 17:12:02 -0700664 dir, err = filepath.Abs(dir)
665 if err != nil {
666 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
667 }
668 relDir, err := filepath.Rel(topDir, dir)
669 if err != nil {
670 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
671 }
672 // If there are ".." in the path, it's not in the source tree.
673 if strings.Contains(relDir, "..") {
674 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
675 }
676
677 configArgs := args[:]
678
679 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
680 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
681 targetNamePrefix := "MODULES-IN-"
682 if inList("GET-INSTALL-PATH", configArgs) {
683 targetNamePrefix = "GET-INSTALL-PATH-IN-"
684 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
685 }
686
Patrice Arruda13848222019-04-22 17:12:02 -0700687 var targets []string
688
689 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700690 case BUILD_MODULES:
691 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700692 case BUILD_MODULES_IN_A_DIRECTORY:
693 // If dir is the root source tree, all the modules are built of the source tree are built so
694 // no need to find the build file.
695 if topDir == dir {
696 break
697 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700698
Patrice Arruda13848222019-04-22 17:12:02 -0700699 buildFile := findBuildFile(ctx, relDir)
700 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700701 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700702 }
Patrice Arruda13848222019-04-22 17:12:02 -0700703 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
704 case BUILD_MODULES_IN_DIRECTORIES:
705 newConfigArgs, dirs := splitArgs(configArgs)
706 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700707 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700708 }
709
710 // Tidy only override all other specified targets.
711 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
712 if tidyOnly == "true" || tidyOnly == "1" {
713 configArgs = append(configArgs, "tidy_only")
714 } else {
715 configArgs = append(configArgs, targets...)
716 }
717
718 return configArgs
719}
720
721// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
722func convertToTarget(dir string, targetNamePrefix string) string {
723 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
724}
725
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700726// hasBuildFile returns true if dir contains an Android build file.
727func hasBuildFile(ctx Context, dir string) bool {
728 for _, buildFile := range buildFiles {
729 _, err := os.Stat(filepath.Join(dir, buildFile))
730 if err == nil {
731 return true
732 }
733 if !os.IsNotExist(err) {
734 ctx.Fatalf("Error retrieving the build file stats: %v", err)
735 }
736 }
737 return false
738}
739
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700740// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
741// in the current and any sub directory of dir. If a build file is not found, traverse the path
742// up by one directory and repeat again until either a build file is found or reached to the root
743// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
744// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700745func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700746 // If the string is empty or ".", assume it is top directory of the source tree.
747 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700748 return ""
749 }
750
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700751 found := false
752 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
753 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
754 if err != nil {
755 return err
756 }
757 if found {
758 return filepath.SkipDir
759 }
760 if info.IsDir() {
761 return nil
762 }
763 for _, buildFile := range buildFiles {
764 if info.Name() == buildFile {
765 found = true
766 return filepath.SkipDir
767 }
768 }
769 return nil
770 })
771 if err != nil {
772 ctx.Fatalf("Error finding Android build file: %v", err)
773 }
774
775 if found {
776 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700777 }
778 }
779
780 return ""
781}
782
783// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
784func splitArgs(args []string) (newArgs []string, dirs []string) {
785 specialArgs := map[string]bool{
786 "showcommands": true,
787 "snod": true,
788 "dist": true,
789 "checkbuild": true,
790 }
791
792 newArgs = []string{}
793 dirs = []string{}
794
795 for _, arg := range args {
796 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
797 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
798 newArgs = append(newArgs, arg)
799 continue
800 }
801
802 if _, ok := specialArgs[arg]; ok {
803 newArgs = append(newArgs, arg)
804 continue
805 }
806
807 dirs = append(dirs, arg)
808 }
809
810 return newArgs, dirs
811}
812
813// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
814// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
815// source root tree where the build action command was invoked. Each directory is validated if the
816// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700817func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700818 for _, dir := range dirs {
819 // The directory may have specified specific modules to build. ":" is the separator to separate
820 // the directory and the list of modules.
821 s := strings.Split(dir, ":")
822 l := len(s)
823 if l > 2 { // more than one ":" was specified.
824 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
825 }
826
827 dir = filepath.Join(relDir, s[0])
828 if _, err := os.Stat(dir); err != nil {
829 ctx.Fatalf("couldn't find directory %s", dir)
830 }
831
832 // Verify that if there are any targets specified after ":". Each target is separated by ",".
833 var newTargets []string
834 if l == 2 && s[1] != "" {
835 newTargets = strings.Split(s[1], ",")
836 if inList("", newTargets) {
837 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
838 }
839 }
840
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700841 // If there are specified targets to build in dir, an android build file must exist for the one
842 // shot build. For the non-targets case, find the appropriate build file and build all the
843 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700844 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700845 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700846 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
847 }
848 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700849 buildFile := findBuildFile(ctx, dir)
850 if buildFile == "" {
851 ctx.Fatalf("Build file not found for %s directory", dir)
852 }
853 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700854 }
855
Patrice Arruda13848222019-04-22 17:12:02 -0700856 targets = append(targets, newTargets...)
857 }
858
Dan Willemsence41e942019-07-29 23:39:30 -0700859 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700860}
861
Dan Willemsen9b587492017-07-10 22:13:00 -0700862func (c *configImpl) parseArgs(ctx Context, args []string) {
863 for i := 0; i < len(args); i++ {
864 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100865 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700866 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200867 } else if arg == "--empty-ninja-file" {
868 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100869 } else if arg == "--skip-ninja" {
870 c.skipNinja = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100871 } else if arg == "--soong-only" {
Cole Faust3740b282025-01-21 15:59:50 -0800872 if c.skipKatiControlledByFlags {
873 ctx.Fatalf("Cannot specify both --soong-only and --no-soong-only")
874 }
Joe Onorato8ae66df2025-01-29 13:21:58 -0800875 c.soongOnlyRequested = true
Cole Faust3740b282025-01-21 15:59:50 -0800876 c.skipKatiControlledByFlags = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100877 c.skipKati = true
878 c.skipKatiNinja = true
Cole Faust3740b282025-01-21 15:59:50 -0800879 } else if arg == "--no-soong-only" {
880 if c.skipKatiControlledByFlags {
881 ctx.Fatalf("Cannot specify both --soong-only and --no-soong-only")
882 }
883 c.skipKatiControlledByFlags = true
884 c.skipKati = false
885 c.skipKatiNinja = false
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200886 } else if arg == "--config-only" {
887 c.skipKati = true
888 c.skipKatiNinja = true
889 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700890 } else if arg == "--skip-config" {
891 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700892 } else if arg == "--skip-soong-tests" {
893 c.skipSoongTests = true
Colin Cross106d6ef2023-10-24 10:34:56 -0700894 } else if arg == "--no-skip-soong-tests" {
895 c.skipSoongTests = false
MarkDacekd0e7cd32022-12-02 22:22:40 +0000896 } else if arg == "--skip-metrics-upload" {
897 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500898 } else if arg == "--mk-metrics" {
899 c.reportMkMetrics = true
Spandan Das394aa322022-11-03 17:02:10 +0000900 } else if arg == "--search-api-dir" {
901 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900902 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
903 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
904 if source == "ninja_log" {
905 c.ninjaWeightListSource = NINJA_LOG
906 } else if source == "evenly_distributed" {
907 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
908 } else if source == "not_used" {
909 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900910 } else if source == "soong" {
911 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900912 } else if strings.HasPrefix(source, "file,") {
913 c.ninjaWeightListSource = EXTERNAL_FILE
914 filePath := strings.TrimPrefix(source, "file,")
915 err := validateNinjaWeightList(filePath)
916 if err != nil {
917 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
918 }
919 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
920 if err != nil {
921 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
922 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900923 } else {
924 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
925 }
Jihoon Kang2a929ad2023-06-08 19:02:07 +0000926 } else if arg == "--build-from-source-stub" {
927 c.buildFromSourceStub = true
Yu Liufa297642024-06-11 00:13:02 +0000928 } else if arg == "--incremental-build-actions" {
929 c.incrementalBuildActions = true
MarkDacekb96561e2022-12-02 04:34:43 +0000930 } else if strings.HasPrefix(arg, "--build-command=") {
931 buildCmd := strings.TrimPrefix(arg, "--build-command=")
932 // remove quotations
933 buildCmd = strings.TrimPrefix(buildCmd, "\"")
934 buildCmd = strings.TrimSuffix(buildCmd, "\"")
935 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacek6614d9c2022-12-07 21:57:38 +0000936 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
937 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
938 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
939 if err == nil {
940 c.buildStartedTime = val
941 } else {
942 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
943 }
MarkDacekf47e1422023-04-19 16:47:36 +0000944 } else if arg == "--ensure-allowlist-integrity" {
945 c.ensureAllowlistIntegrity = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700946 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700947 parseArgNum := func(def int) int {
948 if len(arg) > 2 {
949 p, err := strconv.ParseUint(arg[2:], 10, 31)
950 if err != nil {
951 ctx.Fatalf("Failed to parse %q: %v", arg, err)
952 }
953 return int(p)
954 } else if i+1 < len(args) {
955 p, err := strconv.ParseUint(args[i+1], 10, 31)
956 if err == nil {
957 i++
958 return int(p)
959 }
960 }
961 return def
962 }
963
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700964 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700965 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700966 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700967 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700968 } else {
969 ctx.Fatalln("Unknown option:", arg)
970 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700971 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700972 if k == "OUT_DIR" {
973 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
974 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700975 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700976 } else if arg == "dist" {
977 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200978 } else if arg == "json-module-graph" {
979 c.jsonModuleGraph = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200980 } else if arg == "soong_docs" {
981 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700982 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700983 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800984 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700985 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700986 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700987 }
988 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700989}
990
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900991func validateNinjaWeightList(weightListFilePath string) (err error) {
992 data, err := os.ReadFile(weightListFilePath)
993 if err != nil {
994 return
995 }
996 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
997 for _, line := range lines {
998 fields := strings.Split(line, ",")
999 if len(fields) != 2 {
1000 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
1001 }
1002 _, err = strconv.Atoi(fields[1])
1003 if err != nil {
1004 return
1005 }
1006 }
1007 return
1008}
1009
Dan Willemsened869522018-01-08 14:58:46 -08001010func (c *configImpl) configureLocale(ctx Context) {
1011 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
1012 output, err := cmd.Output()
1013
1014 var locales []string
1015 if err == nil {
1016 locales = strings.Split(string(output), "\n")
1017 } else {
1018 // If we're unable to list the locales, let's assume en_US.UTF-8
1019 locales = []string{"en_US.UTF-8"}
1020 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
1021 }
1022
1023 // gettext uses LANGUAGE, which is passed directly through
1024
1025 // For LANG and LC_*, only preserve the evaluated version of
1026 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001027 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -08001028 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001029 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -08001030 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001031 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -08001032 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001033 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -08001034 }
1035
1036 c.environ.UnsetWithPrefix("LC_")
1037
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001038 if userLang != "" {
1039 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -08001040 }
1041
1042 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
1043 // for others)
1044 if inList("C.UTF-8", locales) {
1045 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -05001046 } else if inList("C.utf8", locales) {
1047 // These normalize to the same thing
1048 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -08001049 } else if inList("en_US.UTF-8", locales) {
1050 c.environ.Set("LANG", "en_US.UTF-8")
1051 } else if inList("en_US.utf8", locales) {
1052 // These normalize to the same thing
1053 c.environ.Set("LANG", "en_US.UTF-8")
1054 } else {
1055 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
1056 }
1057}
1058
Dan Willemsen1e704462016-08-21 15:17:17 -07001059func (c *configImpl) Environment() *Environment {
1060 return c.environ
1061}
1062
1063func (c *configImpl) Arguments() []string {
1064 return c.arguments
1065}
1066
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001067func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001068 if len(c.Arguments()) > 0 {
1069 // Explicit targets requested that are not special targets like b2pbuild
1070 // or the JSON module graph
1071 return true
1072 }
1073
Joe Onorato35f300d2024-10-21 15:02:44 -07001074 if !c.JsonModuleGraph() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001075 // Command line was empty, the default Ninja target is built
1076 return true
1077 }
1078
Colin Cross8d411ff2023-12-07 10:31:24 -08001079 if c.Dist() {
Liz Kammer88677422021-12-15 15:03:19 -05001080 return true
1081 }
1082
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001083 // build.ninja doesn't need to be generated
1084 return false
1085}
1086
Dan Willemsen1e704462016-08-21 15:17:17 -07001087func (c *configImpl) OutDir() string {
1088 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -07001089 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -07001090 }
1091 return "out"
1092}
1093
Dan Willemsen8a073a82017-02-04 17:30:44 -08001094func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -04001095 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001096}
1097
1098func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001099 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001100}
1101
Dan Willemsen1e704462016-08-21 15:17:17 -07001102func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001103 if c.skipKati {
Spandan Dascba050e2025-01-31 00:35:01 +00001104 return append(c.arguments, c.ninjaArgs...)
Dan Willemsene0879fc2017-08-04 15:06:27 -07001105 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001106 return c.ninjaArgs
1107}
1108
1109func (c *configImpl) SoongOutDir() string {
1110 return filepath.Join(c.OutDir(), "soong")
1111}
1112
Spandan Das394aa322022-11-03 17:02:10 +00001113func (c *configImpl) ApiSurfacesOutDir() string {
1114 return filepath.Join(c.OutDir(), "api_surfaces")
1115}
1116
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001117func (c *configImpl) PrebuiltOS() string {
1118 switch runtime.GOOS {
1119 case "linux":
1120 return "linux-x86"
1121 case "darwin":
1122 return "darwin-x86"
1123 default:
1124 panic("Unknown GOOS")
1125 }
1126}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001127
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001128func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001129 if c.SkipKatiNinja() {
1130 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1131 } else {
1132 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1133 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001134}
1135
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001136func (c *configImpl) UsedEnvFile(tag string) string {
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001137 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
Qing Shen713c5422024-08-23 04:09:18 +00001138 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+c.CoverageSuffix()+"."+tag)
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001139 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001140 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1141}
1142
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001143func (c *configImpl) SoongDocsHtml() string {
1144 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1145}
1146
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001147func (c *configImpl) ModuleGraphFile() string {
1148 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1149}
1150
kgui67007242022-01-25 13:50:25 +08001151func (c *configImpl) ModuleActionsFile() string {
1152 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1153}
1154
Jeff Gastonefc1b412017-03-29 17:29:06 -07001155func (c *configImpl) TempDir() string {
1156 return shared.TempDirForOutDir(c.SoongOutDir())
1157}
1158
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001159func (c *configImpl) FileListDir() string {
1160 return filepath.Join(c.OutDir(), ".module_paths")
1161}
1162
Dan Willemsen1e704462016-08-21 15:17:17 -07001163func (c *configImpl) KatiSuffix() string {
1164 if c.katiSuffix != "" {
1165 return c.katiSuffix
1166 }
1167 panic("SetKatiSuffix has not been called")
1168}
1169
Colin Cross37193492017-11-16 17:55:00 -08001170// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1171// user is interested in additional checks at the expense of build time.
1172func (c *configImpl) Checkbuild() bool {
1173 return c.checkbuild
1174}
1175
Dan Willemsen8a073a82017-02-04 17:30:44 -08001176func (c *configImpl) Dist() bool {
1177 return c.dist
1178}
1179
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001180func (c *configImpl) JsonModuleGraph() bool {
1181 return c.jsonModuleGraph
1182}
1183
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001184func (c *configImpl) SoongDocs() bool {
1185 return c.soongDocs
1186}
1187
Dan Willemsen1e704462016-08-21 15:17:17 -07001188func (c *configImpl) IsVerbose() bool {
1189 return c.verbose
1190}
1191
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001192func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1193 return c.ninjaWeightListSource
1194}
1195
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001196func (c *configImpl) SkipKati() bool {
1197 return c.skipKati
1198}
1199
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001200func (c *configImpl) SkipKatiNinja() bool {
1201 return c.skipKatiNinja
1202}
1203
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001204func (c *configImpl) SkipSoong() bool {
1205 return c.skipSoong
1206}
1207
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001208func (c *configImpl) SkipNinja() bool {
1209 return c.skipNinja
1210}
1211
Anton Hansson5a7861a2021-06-04 10:09:01 +01001212func (c *configImpl) SetSkipNinja(v bool) {
1213 c.skipNinja = v
1214}
1215
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001216func (c *configImpl) SkipConfig() bool {
1217 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001218}
1219
Jihoon Kang1bff0342023-01-17 20:40:22 +00001220func (c *configImpl) BuildFromTextStub() bool {
Jihoon Kang2a929ad2023-06-08 19:02:07 +00001221 return !c.buildFromSourceStub
Jihoon Kang1bff0342023-01-17 20:40:22 +00001222}
1223
Dan Willemsen1e704462016-08-21 15:17:17 -07001224func (c *configImpl) TargetProduct() string {
1225 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1226 return v
1227 }
1228 panic("TARGET_PRODUCT is not defined")
1229}
1230
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001231func (c *configImpl) TargetProductOrErr() (string, error) {
1232 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1233 return v, nil
1234 }
1235 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1236}
1237
Qing Shen713c5422024-08-23 04:09:18 +00001238func (c *configImpl) CoverageSuffix() string {
1239 if v := c.environ.IsEnvTrue("EMMA_INSTRUMENT"); v {
1240 return ".coverage"
1241 }
1242 return ""
1243}
1244
Dan Willemsen02781d52017-05-12 19:28:13 -07001245func (c *configImpl) TargetDevice() string {
1246 return c.targetDevice
1247}
1248
1249func (c *configImpl) SetTargetDevice(device string) {
1250 c.targetDevice = device
1251}
1252
1253func (c *configImpl) TargetBuildVariant() string {
1254 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1255 return v
1256 }
1257 panic("TARGET_BUILD_VARIANT is not defined")
1258}
1259
Dan Willemsen1e704462016-08-21 15:17:17 -07001260func (c *configImpl) KatiArgs() []string {
1261 return c.katiArgs
1262}
1263
1264func (c *configImpl) Parallel() int {
1265 return c.parallel
1266}
1267
Sam Delmerico98a73292023-02-21 11:50:29 -05001268func (c *configImpl) GetSourceRootDirs() []string {
1269 return c.sourceRootDirs
1270}
1271
1272func (c *configImpl) SetSourceRootDirs(i []string) {
1273 c.sourceRootDirs = i
1274}
1275
MarkDacek6614d9c2022-12-07 21:57:38 +00001276func (c *configImpl) GetLogsPrefix() string {
1277 return c.logsPrefix
1278}
1279
1280func (c *configImpl) SetLogsPrefix(prefix string) {
1281 c.logsPrefix = prefix
1282}
1283
Colin Cross8b8bec32019-11-15 13:18:43 -08001284func (c *configImpl) HighmemParallel() int {
1285 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1286 return i
1287 }
1288
1289 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1290 parallel := c.Parallel()
1291 if c.UseRemoteBuild() {
1292 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1293 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1294 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1295 // Return 1/16th of the size of the local pool, rounding up.
1296 return (parallel + 15) / 16
1297 } else if c.totalRAM == 0 {
1298 // Couldn't detect the total RAM, don't restrict highmem processes.
1299 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001300 } else if c.totalRAM <= 16*1024*1024*1024 {
1301 // Less than 16GB of ram, restrict to 1 highmem processes
1302 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001303 } else if c.totalRAM <= 32*1024*1024*1024 {
1304 // Less than 32GB of ram, restrict to 2 highmem processes
1305 return 2
1306 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1307 // If less than 8GB total RAM per process, reduce the number of highmem processes
1308 return p
1309 }
1310 // No restriction on highmem processes
1311 return parallel
1312}
1313
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001314func (c *configImpl) TotalRAM() uint64 {
1315 return c.totalRAM
1316}
1317
Kousik Kumarec478642020-09-21 13:39:24 -04001318// ForceUseGoma determines whether we should override Goma deprecation
1319// and use Goma for the current build or not.
1320func (c *configImpl) ForceUseGoma() bool {
1321 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1322 v = strings.TrimSpace(v)
1323 if v != "" && v != "false" {
1324 return true
1325 }
1326 }
1327 return false
1328}
1329
Dan Willemsen1e704462016-08-21 15:17:17 -07001330func (c *configImpl) UseGoma() bool {
1331 if v, ok := c.environ.Get("USE_GOMA"); ok {
1332 v = strings.TrimSpace(v)
1333 if v != "" && v != "false" {
1334 return true
1335 }
1336 }
1337 return false
1338}
1339
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001340func (c *configImpl) StartGoma() bool {
1341 if !c.UseGoma() {
1342 return false
1343 }
1344
1345 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1346 v = strings.TrimSpace(v)
1347 if v != "" && v != "false" {
1348 return false
1349 }
1350 }
1351 return true
1352}
1353
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001354func (c *configImpl) canSupportRBE() bool {
Joe Onorato86f50e72024-06-24 14:28:25 -07001355 // Only supported on linux
1356 if runtime.GOOS != "linux" {
1357 return false
1358 }
1359
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001360 // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since
1361 // its unlikely that we will be able to obtain necessary creds without stubby.
1362 authType, _ := c.rbeAuth()
1363 if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") {
1364 return false
1365 }
Taylor Santiago3c16e612024-05-30 14:41:31 -07001366 if c.UseABFS() {
1367 return false
1368 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001369 return true
1370}
1371
Taylor Santiago3c16e612024-05-30 14:41:31 -07001372func (c *configImpl) UseABFS() bool {
Taylor Santiago2fa40d02025-01-20 20:36:37 -08001373 if c.ninjaCommand == NINJA_NINJAGO {
1374 return true
1375 }
1376
Taylor Santiago3c16e612024-05-30 14:41:31 -07001377 if v, ok := c.environ.Get("NO_ABFS"); ok {
1378 v = strings.ToLower(strings.TrimSpace(v))
1379 if v == "true" || v == "1" {
1380 return false
1381 }
1382 }
1383
1384 abfsBox := c.PrebuiltBuildTool("abfsbox")
1385 err := exec.Command(abfsBox, "hash", srcDirFileCheck).Run()
1386 return err == nil
1387}
1388
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001389func (c *configImpl) sandboxPath(base, in string) string {
1390 if !c.UseABFS() {
1391 return in
1392 }
1393
1394 rel, err := filepath.Rel(base, in)
1395 if err != nil {
1396 return in
1397 }
1398
1399 return filepath.Join(abfsSrcDir, rel)
1400}
1401
Ramy Medhatbbf25672019-07-17 12:30:04 +00001402func (c *configImpl) UseRBE() bool {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001403 // These alternate modes of running Soong do not use RBE / reclient.
Joe Onorato35f300d2024-10-21 15:02:44 -07001404 if c.JsonModuleGraph() {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001405 return false
1406 }
1407
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001408 if !c.canSupportRBE() {
Kousik Kumar67ad4342023-06-06 15:09:27 -04001409 return false
1410 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001411
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001412 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001413 v = strings.TrimSpace(v)
1414 if v != "" && v != "false" {
1415 return true
1416 }
1417 }
1418 return false
1419}
1420
1421func (c *configImpl) StartRBE() bool {
1422 if !c.UseRBE() {
1423 return false
1424 }
1425
1426 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1427 v = strings.TrimSpace(v)
1428 if v != "" && v != "false" {
1429 return false
1430 }
1431 }
1432 return true
1433}
1434
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001435func (c *configImpl) rbeProxyLogsDir() string {
1436 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001437 if v, ok := c.environ.Get(f); ok {
1438 return v
1439 }
1440 }
Ramy Medhatbc061762023-10-10 18:36:59 +00001441 return c.rbeTmpDir()
1442}
1443
1444func (c *configImpl) rbeDownloadTmpDir() string {
Cole Faust06ea5312023-10-18 17:38:40 -07001445 for _, f := range []string{"RBE_download_tmp_dir", "FLAG_download_tmp_dir"} {
Ramy Medhatbc061762023-10-10 18:36:59 +00001446 if v, ok := c.environ.Get(f); ok {
1447 return v
1448 }
1449 }
1450 return c.rbeTmpDir()
1451}
1452
1453func (c *configImpl) rbeTmpDir() string {
Yaowen Meid4da2662024-07-24 08:25:12 +00001454 return filepath.Join(c.SoongOutDir(), "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001455}
1456
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001457func (c *configImpl) rbeCacheDir() string {
1458 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1459 if v, ok := c.environ.Get(f); ok {
1460 return v
1461 }
1462 }
1463 return shared.JoinPath(c.SoongOutDir(), "rbe")
1464}
1465
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001466func (c *configImpl) shouldCleanupRBELogsDir() bool {
1467 // Perform a log directory cleanup only when the log directory
1468 // is auto created by the build rather than user-specified.
1469 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Yaowen Meid9108d22024-08-29 16:56:32 +00001470 if v, ok := c.environ.Get(f); ok {
1471 if v != c.rbeTmpDir() {
1472 return false
1473 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001474 }
1475 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001476 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001477}
1478
1479func (c *configImpl) rbeExecRoot() string {
1480 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1481 if v, ok := c.environ.Get(f); ok {
1482 return v
1483 }
1484 }
1485 wd, err := os.Getwd()
1486 if err != nil {
1487 return ""
1488 }
1489 return wd
1490}
1491
1492func (c *configImpl) rbeDir() string {
1493 if v, ok := c.environ.Get("RBE_DIR"); ok {
1494 return v
1495 }
1496 return "prebuilts/remoteexecution-client/live/"
1497}
1498
1499func (c *configImpl) rbeReproxy() string {
1500 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1501 if v, ok := c.environ.Get(f); ok {
1502 return v
1503 }
1504 }
1505 return filepath.Join(c.rbeDir(), "reproxy")
1506}
1507
1508func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001509 credFlags := []string{
1510 "use_application_default_credentials",
1511 "use_gce_credentials",
1512 "credential_file",
1513 "use_google_prod_creds",
1514 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001515 for _, cf := range credFlags {
1516 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1517 if v, ok := c.environ.Get(f); ok {
1518 v = strings.TrimSpace(v)
1519 if v != "" && v != "false" && v != "0" {
1520 return "RBE_" + cf, v
1521 }
1522 }
1523 }
1524 }
1525 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001526}
1527
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001528func (c *configImpl) rbeSockAddr(dir string) (string, error) {
Andus Yuc917eb82024-03-06 21:54:15 +00001529 // Absolute path socket addresses have a prefix of //. This should
1530 // be included in the length limit.
1531 maxNameLen := len(syscall.RawSockaddrUnix{}.Path) - 2
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001532 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1533
1534 name := filepath.Join(dir, base)
1535 if len(name) < maxNameLen {
1536 return name, nil
1537 }
1538
1539 name = filepath.Join("/tmp", base)
1540 if len(name) < maxNameLen {
1541 return name, nil
1542 }
1543
1544 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1545}
1546
Kousik Kumar7bc78192022-04-27 14:52:56 -04001547// IsGooglerEnvironment returns true if the current build is running
1548// on a Google developer machine and false otherwise.
1549func (c *configImpl) IsGooglerEnvironment() bool {
1550 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1551 if v, ok := c.environ.Get(cf); ok {
1552 return v == "googler"
1553 }
1554 return false
1555}
1556
1557// GoogleProdCredsExist determine whether credentials exist on the
1558// Googler machine to use remote execution.
1559func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001560 if googleProdCredsExistCache {
1561 return googleProdCredsExistCache
1562 }
andusyu0b3dc032023-06-21 17:29:32 -04001563 if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001564 return false
1565 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001566 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001567 return true
1568}
1569
1570// UseRemoteBuild indicates whether to use a remote build acceleration system
1571// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001572func (c *configImpl) UseRemoteBuild() bool {
1573 return c.UseGoma() || c.UseRBE()
1574}
1575
Kousik Kumar7bc78192022-04-27 14:52:56 -04001576// StubbyExists checks whether the stubby binary exists on the machine running
1577// the build.
1578func (c *configImpl) StubbyExists() bool {
1579 if _, err := exec.LookPath("stubby"); err != nil {
1580 return false
1581 }
1582 return true
1583}
1584
Dan Willemsen1e704462016-08-21 15:17:17 -07001585// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001586// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001587// still limited by Parallel()
1588func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001589 if !c.UseRemoteBuild() {
1590 return 0
1591 }
1592 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1593 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001594 }
1595 return 500
1596}
1597
1598func (c *configImpl) SetKatiArgs(args []string) {
1599 c.katiArgs = args
1600}
1601
1602func (c *configImpl) SetNinjaArgs(args []string) {
1603 c.ninjaArgs = args
1604}
1605
1606func (c *configImpl) SetKatiSuffix(suffix string) {
1607 c.katiSuffix = suffix
1608}
1609
Dan Willemsene0879fc2017-08-04 15:06:27 -07001610func (c *configImpl) LastKatiSuffixFile() string {
1611 return filepath.Join(c.OutDir(), "last_kati_suffix")
1612}
1613
1614func (c *configImpl) HasKatiSuffix() bool {
1615 return c.katiSuffix != ""
1616}
1617
Dan Willemsen1e704462016-08-21 15:17:17 -07001618func (c *configImpl) KatiEnvFile() string {
1619 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1620}
1621
Dan Willemsen29971232018-09-26 14:58:30 -07001622func (c *configImpl) KatiBuildNinjaFile() string {
1623 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001624}
1625
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001626func (c *configImpl) KatiPackageNinjaFile() string {
1627 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1628}
1629
Cole Faustc5bfbdd2025-01-08 13:05:40 -08001630func (c *configImpl) KatiSoongOnlyPackageNinjaFile() string {
1631 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiSoongOnlyPackageSuffix+".ninja")
1632}
1633
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001634func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001635 targetProduct, err := c.TargetProductOrErr()
1636 if err != nil {
1637 return filepath.Join(c.SoongOutDir(), "soong.variables")
1638 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001639 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".variables")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001640 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001641}
1642
Inseob Kim58c802f2024-06-11 10:59:00 +09001643func (c *configImpl) SoongExtraVarsFile() string {
1644 targetProduct, err := c.TargetProductOrErr()
1645 if err != nil {
1646 return filepath.Join(c.SoongOutDir(), "soong.extra.variables")
1647 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001648 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".extra.variables")
Inseob Kim58c802f2024-06-11 10:59:00 +09001649 }
1650}
1651
Dan Willemsen1e704462016-08-21 15:17:17 -07001652func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001653 targetProduct, err := c.TargetProductOrErr()
1654 if err != nil {
1655 return filepath.Join(c.SoongOutDir(), "build.ninja")
1656 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001657 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+c.CoverageSuffix()+".ninja")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001658 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001659}
1660
1661func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001662 if c.katiSuffix == "" {
1663 return filepath.Join(c.OutDir(), "combined.ninja")
1664 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001665 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1666}
1667
1668func (c *configImpl) SoongAndroidMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001669 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001670}
1671
1672func (c *configImpl) SoongMakeVarsMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001673 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001674}
1675
Colin Crossaa9a2732023-10-27 10:54:27 -07001676func (c *configImpl) SoongBuildMetrics() string {
Colin Crossb67b0612023-10-31 10:02:45 -07001677 return filepath.Join(c.LogsDir(), "soong_build_metrics.pb")
Colin Crossaa9a2732023-10-27 10:54:27 -07001678}
1679
Dan Willemsenf052f782017-05-18 15:29:04 -07001680func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001681 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001682}
1683
Dan Willemsen02781d52017-05-12 19:28:13 -07001684func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001685 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1686}
1687
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001688func (c *configImpl) KatiPackageMkDir() string {
Cole Faustc5bfbdd2025-01-08 13:05:40 -08001689 return filepath.Join(c.SoongOutDir(), "kati_packaging"+c.KatiSuffix())
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001690}
1691
Dan Willemsenf052f782017-05-18 15:29:04 -07001692func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001693 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001694}
1695
1696func (c *configImpl) HostOut() string {
1697 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1698}
1699
1700// This probably needs to be multi-valued, so not exporting it for now
1701func (c *configImpl) hostCrossOut() string {
1702 if runtime.GOOS == "linux" {
1703 return filepath.Join(c.hostOutRoot(), "windows-x86")
1704 } else {
1705 return ""
1706 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001707}
1708
Dan Willemsen1e704462016-08-21 15:17:17 -07001709func (c *configImpl) HostPrebuiltTag() string {
1710 if runtime.GOOS == "linux" {
1711 return "linux-x86"
1712 } else if runtime.GOOS == "darwin" {
1713 return "darwin-x86"
1714 } else {
1715 panic("Unsupported OS")
1716 }
1717}
Dan Willemsenf173d592017-04-27 14:28:00 -07001718
Taylor Santiago3c16e612024-05-30 14:41:31 -07001719func (c *configImpl) KatiBin() string {
1720 binName := "ckati"
1721 if c.UseABFS() {
1722 binName = "ckati-wrap"
1723 }
1724
1725 return c.PrebuiltBuildTool(binName)
1726}
1727
1728func (c *configImpl) NinjaBin() string {
1729 binName := "ninja"
1730 if c.UseABFS() {
1731 binName = "ninjago"
1732 }
1733 return c.PrebuiltBuildTool(binName)
1734}
1735
Cole Faust4e58bba2024-08-22 14:27:03 -07001736func (c *configImpl) N2Bin() string {
1737 path := c.PrebuiltBuildTool("n2")
1738 // Use musl instead of glibc because glibc on the build server is old and has bugs
1739 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1740}
1741
LaMont Jonesece626c2024-09-03 11:19:31 -07001742func (c *configImpl) SisoBin() string {
1743 path := c.PrebuiltBuildTool("siso")
1744 // Use musl instead of glibc because glibc on the build server is old and has bugs
1745 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1746}
1747
Dan Willemsen8122bd52017-10-12 20:20:41 -07001748func (c *configImpl) PrebuiltBuildTool(name string) string {
Colin Cross7077be42024-10-04 20:37:16 +00001749 if c.environ.IsEnvTrue("SANITIZE_BUILD_TOOL_PREBUILTS") {
1750 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1751 if _, err := os.Stat(asan); err == nil {
1752 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -07001753 }
1754 }
1755 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1756}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001757
1758func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1759 c.brokenDupRules = val
1760}
1761
1762func (c *configImpl) BuildBrokenDupRules() bool {
1763 return c.brokenDupRules
1764}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001765
Dan Willemsen25e6f092019-04-09 10:22:43 -07001766func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1767 c.brokenUsesNetwork = val
1768}
1769
1770func (c *configImpl) BuildBrokenUsesNetwork() bool {
1771 return c.brokenUsesNetwork
1772}
1773
Dan Willemsene3336352020-01-02 19:10:38 -08001774func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1775 c.brokenNinjaEnvVars = val
1776}
1777
1778func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1779 return c.brokenNinjaEnvVars
1780}
1781
Spandan Das28a6f192024-07-01 21:00:25 +00001782func (c *configImpl) SetBuildBrokenMissingOutputs(val bool) {
1783 c.brokenMissingOutputs = val
1784}
1785
1786func (c *configImpl) BuildBrokenMissingOutputs() bool {
1787 return c.brokenMissingOutputs
1788}
1789
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001790func (c *configImpl) SetTargetDeviceDir(dir string) {
1791 c.targetDeviceDir = dir
1792}
1793
1794func (c *configImpl) TargetDeviceDir() string {
1795 return c.targetDeviceDir
1796}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001797
Patrice Arruda219eef32020-06-01 17:29:30 +00001798func (c *configImpl) BuildDateTime() string {
1799 return c.buildDateTime
1800}
1801
1802func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001803 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001804}
Patrice Arruda83842d72020-12-08 19:42:08 +00001805
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001806// LogsDir returns the absolute path to the logs directory where build log and
1807// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001808// directory. If the argument dist is specified, the logs directory
1809// is <dist_dir>/logs.
1810func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001811 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001812 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001813 // Always write logs to the real dist dir, even if Bazel is using a rigged dist dir for other files
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001814 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001815 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001816 absDir, err := filepath.Abs(dir)
1817 if err != nil {
1818 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1819 os.Exit(1)
1820 }
1821 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001822}
1823
Chris Parsons53f68ae2022-03-03 12:01:40 -05001824// MkFileMetrics returns the file path for make-related metrics.
1825func (c *configImpl) MkMetrics() string {
1826 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1827}
1828
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001829func (c *configImpl) SetEmptyNinjaFile(v bool) {
1830 c.emptyNinjaFile = v
1831}
1832
1833func (c *configImpl) EmptyNinjaFile() bool {
1834 return c.emptyNinjaFile
1835}
Yu Liu6e13b402021-07-27 14:29:06 -07001836
MarkDacekd0e7cd32022-12-02 22:22:40 +00001837func (c *configImpl) SkipMetricsUpload() bool {
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001838 // b/362625275 - Metrics upload sometimes prevents abfs unmount
1839 if c.UseABFS() {
1840 return true
1841 }
1842
MarkDacekd0e7cd32022-12-02 22:22:40 +00001843 return c.skipMetricsUpload
1844}
1845
MarkDacekf47e1422023-04-19 16:47:36 +00001846func (c *configImpl) EnsureAllowlistIntegrity() bool {
1847 return c.ensureAllowlistIntegrity
1848}
1849
MarkDacek6614d9c2022-12-07 21:57:38 +00001850// Returns a Time object if one was passed via a command-line flag.
1851// Otherwise returns the passed default.
1852func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1853 if c.buildStartedTime == 0 {
1854 return defaultTime
1855 }
1856 return time.UnixMilli(c.buildStartedTime)
1857}
1858
Yu Liu6e13b402021-07-27 14:29:06 -07001859func GetMetricsUploader(topDir string, env *Environment) string {
1860 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1861 metricsUploader := filepath.Join(topDir, p)
1862 if _, err := os.Stat(metricsUploader); err == nil {
1863 return metricsUploader
1864 }
1865 }
1866
1867 return ""
1868}