blob: 9ec04a0dcfae1909e98d30ba54c2a69e4af180c1 [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
33 "android/soong/shared"
LaMont Jones9a912862023-11-06 22:11:08 +000034 "android/soong/ui/metrics"
Kousik Kumarec478642020-09-21 13:39:24 -040035
Dan Willemsen4591b642021-05-24 14:24:12 -070036 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000037
38 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070039)
40
Kousik Kumar3ff037e2022-01-25 22:11:01 -050041const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050042 envConfigDir = "vendor/google/tools/soong_config"
43 jsonSuffix = "json"
Taylor Santiago8b0bed72024-09-03 13:30:22 -070044 abfsSrcDir = "/src"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050045)
46
Kousik Kumar4c180ad2022-05-27 07:48:37 -040047var (
Kevin Dagostino096ab2f2023-03-03 19:47:17 +000048 rbeRandPrefix int
49 googleProdCredsExistCache bool
Kousik Kumar4c180ad2022-05-27 07:48:37 -040050)
51
52func init() {
53 rand.Seed(time.Now().UnixNano())
54 rbeRandPrefix = rand.Intn(1000)
55}
56
LaMont Jonesece626c2024-09-03 11:19:31 -070057// Which builder are we using?
58type ninjaCommandType = int
59
60const (
61 _ = iota
62 NINJA_NINJA
63 NINJA_N2
64 NINJA_SISO
65)
66
Dan Willemsen1e704462016-08-21 15:17:17 -070067type Config struct{ *configImpl }
68
69type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020070 // Some targets that are implemented in soong_build
71 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080072 arguments []string
73 goma bool
74 environ *Environment
75 distDir string
76 buildDateTime string
MarkDacek6614d9c2022-12-07 21:57:38 +000077 logsPrefix string
Dan Willemsen1e704462016-08-21 15:17:17 -070078
79 // From the arguments
MarkDacekf47e1422023-04-19 16:47:36 +000080 parallel int
81 keepGoing int
82 verbose bool
83 checkbuild bool
84 dist bool
85 jsonModuleGraph bool
MarkDacekf47e1422023-04-19 16:47:36 +000086 queryview bool
87 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
88 soongDocs bool
MarkDacekf47e1422023-04-19 16:47:36 +000089 skipConfig bool
90 skipKati bool
91 skipKatiNinja bool
92 skipSoong bool
93 skipNinja bool
94 skipSoongTests bool
95 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
96 skipMetricsUpload bool
97 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
Jihoon Kang2a929ad2023-06-08 19:02:07 +000098 buildFromSourceStub bool
Yu Liufa297642024-06-11 00:13:02 +000099 incrementalBuildActions bool
Colin Cross8d411ff2023-12-07 10:31:24 -0800100 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
Dan Willemsen1e704462016-08-21 15:17:17 -0700101
102 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700103 katiArgs []string
104 ninjaArgs []string
105 katiSuffix string
106 targetDevice string
107 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +0000108 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -0700109
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800110 // Autodetected
111 totalRAM uint64
112
Spandan Das28a6f192024-07-01 21:00:25 +0000113 brokenDupRules bool
114 brokenUsesNetwork bool
115 brokenNinjaEnvVars []string
116 brokenMissingOutputs bool
Dan Willemsen18490112018-05-25 16:30:04 -0700117
118 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000119
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700120 // Set by multiproduct_kati
121 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700122
123 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000124
Sam Delmerico98a73292023-02-21 11:50:29 -0500125 includeTags []string
126 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900127
128 // Data source to write ninja weight list
129 ninjaWeightListSource NinjaWeightListSource
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800130
131 // This file is a detailed dump of all soong-defined modules for debugging purposes.
132 // There's quite a bit of overlap with module-info.json and soong module graph. We
133 // could consider merging them.
134 moduleDebugFile string
Cole Faustbee030d2024-01-03 13:45:48 -0800135
LaMont Jonesece626c2024-09-03 11:19:31 -0700136 // Which builder are we using
137 ninjaCommand ninjaCommandType
Dan Willemsen1e704462016-08-21 15:17:17 -0700138}
139
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900140type NinjaWeightListSource uint
141
142const (
143 // ninja doesn't use weight list.
144 NOT_USED NinjaWeightListSource = iota
145 // ninja uses weight list based on previous builds by ninja log
146 NINJA_LOG
147 // ninja thinks every task has the same weight.
148 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900149 // ninja uses an external custom weight list
150 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900151 // ninja uses a prioritized module list from Soong
152 HINT_FROM_SOONG
Jeongik Chaa87506f2023-06-01 23:16:41 +0900153 // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead.
154 // We can assume it is an incremental build if ninja log exists.
155 DEFAULT
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900156)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800157const srcDirFileCheck = "build/soong/root.bp"
158
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700159var buildFiles = []string{"Android.mk", "Android.bp"}
160
Patrice Arruda13848222019-04-22 17:12:02 -0700161type BuildAction uint
162
163const (
164 // Builds all of the modules and their dependencies of a specified directory, relative to the root
165 // directory of the source tree.
166 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
167
168 // Builds all of the modules and their dependencies of a list of specified directories. All specified
169 // directories are relative to the root directory of the source tree.
170 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700171
172 // Build a list of specified modules. If none was specified, simply build the whole source tree.
173 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700174)
175
176// checkTopDir validates that the current directory is at the root directory of the source tree.
177func checkTopDir(ctx Context) {
178 if _, err := os.Stat(srcDirFileCheck); err != nil {
179 if os.IsNotExist(err) {
180 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
181 }
182 ctx.Fatalln("Error verifying tree state:", err)
183 }
184}
185
MarkDacek7901e582023-01-09 19:48:01 +0000186func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500187 if bc == "" {
188 return nil
189 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500190
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500191 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500192 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500193 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500194 envConfigDir,
195 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500196 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500197 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
198 envVarsJSON, err := ioutil.ReadFile(cfgFile)
199 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500200 continue
201 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500202 ctx.Verbosef("Loading config file %v\n", cfgFile)
203 var envVars map[string]map[string]string
204 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
205 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
206 continue
207 }
208 for k, v := range envVars["env"] {
209 if os.Getenv(k) != "" {
210 continue
211 }
212 config.environ.Set(k, v)
213 }
214 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
215 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500216 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500217
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500218 return nil
219}
220
Dan Willemsen1e704462016-08-21 15:17:17 -0700221func NewConfig(ctx Context, args ...string) Config {
222 ret := &configImpl{
Jeongik Chaf2ecf762023-05-19 14:03:45 +0900223 environ: OsEnvironment(),
224 sandboxConfig: &SandboxConfig{},
Jeongik Chaa87506f2023-06-01 23:16:41 +0900225 ninjaWeightListSource: DEFAULT,
Dan Willemsen1e704462016-08-21 15:17:17 -0700226 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700227 wd, err := os.Getwd()
228 if err != nil {
229 ctx.Fatalln("Failed to get working directory:", err)
230 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700231
Colin Cross106d6ef2023-10-24 10:34:56 -0700232 // Skip soong tests by default on Linux
233 if runtime.GOOS == "linux" {
234 ret.skipSoongTests = true
235 }
236
Patrice Arruda90109172020-07-28 18:07:27 +0000237 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700238 ret.parallel = runtime.NumCPU() + 2
239 ret.keepGoing = 1
240
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800241 ret.totalRAM = detectTotalRAM(ctx)
Dan Willemsen9b587492017-07-10 22:13:00 -0700242 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900243
244 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
Jeongik Chaa87506f2023-06-01 23:16:41 +0900245 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always")
246 } else if ret.ninjaWeightListSource == DEFAULT {
247 defaultNinjaWeightListSource := NINJA_LOG
248 if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) {
249 ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead")
250 defaultNinjaWeightListSource = HINT_FROM_SOONG
251 } else {
252 ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG")
253 }
254 ret.ninjaWeightListSource = defaultNinjaWeightListSource
255 // soong_build generates ninja hint depending on ninja log existence.
256 // Set it "depend" to avoid soong re-run due to env variable change.
257 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend")
Jeongik Chae114e602023-03-19 00:12:39 +0900258 }
Jeongik Chaa87506f2023-06-01 23:16:41 +0900259
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800260 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700261 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700262 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, filepath.Clean(outDir)))
Dan Willemsen02f3add2017-05-12 13:50:19 -0700263 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800264 outDir := "out"
265 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700266 outDir = filepath.Join(baseDir, filepath.Base(wd))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800267 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700268 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, outDir))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800269 }
270
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500271 // loadEnvConfig needs to know what the OUT_DIR is, so it should
272 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000273 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
274
275 if bc != "" {
Kousik Kumarc8818332023-01-16 16:33:05 +0000276 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000277 ctx.Fatalln("Failed to parse env config files: %v", err)
278 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +0000279 if !ret.canSupportRBE() {
280 // Explicitly set USE_RBE env variable to false when we cannot run
281 // an RBE build to avoid ninja local execution pool issues.
282 ret.environ.Set("USE_RBE", "false")
283 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500284 }
285
Dan Willemsen2d31a442018-10-20 21:33:41 -0700286 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
287 ret.distDir = filepath.Clean(distDir)
288 } else {
289 ret.distDir = filepath.Join(ret.OutDir(), "dist")
290 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700291
Spandan Das05063612021-06-25 01:39:04 +0000292 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
293 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
294 }
295
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800296 if os.Getenv("GENERATE_SOONG_DEBUG") == "true" {
297 ret.moduleDebugFile, _ = filepath.Abs(shared.JoinPath(ret.SoongOutDir(), "soong-debug-info.json"))
298 }
299
LaMont Jones99f18962024-10-17 11:50:44 -0700300 // If SOONG_USE_PARTIAL_COMPILE is set, make it one of "true" or the empty string.
301 // This simplifies the generated Ninja rules, so that they only need to check for the empty string.
302 if value, ok := os.LookupEnv("SOONG_USE_PARTIAL_COMPILE"); ok {
303 if value == "true" || value == "1" || value == "y" || value == "yes" {
304 value = "true"
305 } else {
306 value = ""
307 }
308 err = os.Setenv("SOONG_USE_PARTIAL_COMPILE", value)
309 if err != nil {
310 ctx.Fatalln("Failed to set SOONG_USE_PARTIAL_COMPILE: %v", err)
311 }
312 }
313
LaMont Jonesece626c2024-09-03 11:19:31 -0700314 ret.ninjaCommand = NINJA_NINJA
315 switch os.Getenv("SOONG_NINJA") {
316 case "n2":
317 ret.ninjaCommand = NINJA_N2
318 case "siso":
319 ret.ninjaCommand = NINJA_SISO
320 default:
321 if os.Getenv("SOONG_USE_N2") == "true" {
322 ret.ninjaCommand = NINJA_N2
323 }
Cole Faustbee030d2024-01-03 13:45:48 -0800324 }
325
Dan Willemsen1e704462016-08-21 15:17:17 -0700326 ret.environ.Unset(
327 // We're already using it
328 "USE_SOONG_UI",
329
330 // We should never use GOROOT/GOPATH from the shell environment
331 "GOROOT",
332 "GOPATH",
333
334 // These should only come from Soong, not the environment.
335 "CLANG",
336 "CLANG_CXX",
337 "CCC_CC",
338 "CCC_CXX",
339
340 // Used by the goma compiler wrapper, but should only be set by
341 // gomacc
342 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800343
344 // We handle this above
345 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700346
Dan Willemsen2d31a442018-10-20 21:33:41 -0700347 // This is handled above too, and set for individual commands later
348 "DIST_DIR",
349
Dan Willemsen68a09852017-04-18 13:56:57 -0700350 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000351 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700352 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700353 "DISPLAY",
354 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000355 "JAVAC",
Nathan Egge978c9342024-07-03 21:13:03 +0000356 "LEX",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700357 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700358 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700359
360 // Drop make flags
361 "MAKEFLAGS",
362 "MAKELEVEL",
363 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700364
365 // Set in envsetup.sh, reset in makefiles
366 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700367
368 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
369 "ANDROID_BUILD_TOP",
370 "ANDROID_HOST_OUT",
371 "ANDROID_PRODUCT_OUT",
372 "ANDROID_HOST_OUT_TESTCASES",
373 "ANDROID_TARGET_OUT_TESTCASES",
374 "ANDROID_TOOLCHAIN",
375 "ANDROID_TOOLCHAIN_2ND_ARCH",
376 "ANDROID_DEV_SCRIPTS",
377 "ANDROID_EMULATOR_PREBUILTS",
378 "ANDROID_PRE_BUILD_PATHS",
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800379
380 // We read it here already, don't let others share in the fun
381 "GENERATE_SOONG_DEBUG",
Cole Faustbee030d2024-01-03 13:45:48 -0800382
LaMont Jonesece626c2024-09-03 11:19:31 -0700383 // Use config.ninjaCommand instead.
384 "SOONG_NINJA",
Cole Faustbee030d2024-01-03 13:45:48 -0800385 "SOONG_USE_N2",
Dan Willemsen1e704462016-08-21 15:17:17 -0700386 )
387
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400388 if ret.UseGoma() || ret.ForceUseGoma() {
389 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
390 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400391 }
392
Dan Willemsen1e704462016-08-21 15:17:17 -0700393 // Tell python not to spam the source tree with .pyc files.
394 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
395
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400396 tmpDir := absPath(ctx, ret.TempDir())
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700397 ret.environ.Set("TMPDIR", ret.sandboxPath(wd, tmpDir))
Dan Willemsen32a669b2018-03-08 19:42:00 -0800398
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700399 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
400 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
401 "llvm-binutils-stable/llvm-symbolizer")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700402 ret.environ.Set("ASAN_SYMBOLIZER_PATH", ret.sandboxPath(wd, absPath(ctx, symbolizerPath)))
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700403
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800404 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700405 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800406
Yu Liu6e13b402021-07-27 14:29:06 -0700407 srcDir := absPath(ctx, ".")
408 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700409 ctx.Println("You are building in a directory whose absolute path contains a space character:")
410 ctx.Println()
411 ctx.Printf("%q\n", srcDir)
412 ctx.Println()
413 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700414 }
415
Yu Liu6e13b402021-07-27 14:29:06 -0700416 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
417
Dan Willemsendb8457c2017-05-12 16:38:17 -0700418 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700419 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
420 ctx.Println()
421 ctx.Printf("%q\n", outDir)
422 ctx.Println()
423 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700424 }
425
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000426 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700427 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
428 ctx.Println()
429 ctx.Printf("%q\n", distDir)
430 ctx.Println()
431 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700432 }
433
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700434 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000435 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
Sorin Basca0760c892023-11-29 19:13:55 +0000436 java21Home := filepath.Join("prebuilts/jdk/jdk21", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700437 javaHome := func() string {
438 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
439 return override
440 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000441 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000442 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 +0100443 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000444 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000445 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 +0000446 }
Sorin Basca5dfa2382024-03-11 17:23:06 +0000447 if toolchain21, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN"); ok && toolchain21 != "true" {
448 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
449 }
450 return java21Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700451 }()
452 absJavaHome := absPath(ctx, javaHome)
453
Dan Willemsened869522018-01-08 14:58:46 -0800454 ret.configureLocale(ctx)
455
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000456 newPath := []string{filepath.Join(absJavaHome, "bin")}
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700457 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000458 newPath = append(newPath, path)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700459 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100460
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700461 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700462 ret.environ.Set("JAVA_HOME", ret.sandboxPath(wd, absJavaHome))
463 ret.environ.Set("ANDROID_JAVA_HOME", ret.sandboxPath(wd, javaHome))
464 ret.environ.Set("ANDROID_JAVA8_HOME", ret.sandboxPath(wd, java8Home))
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700465 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
466
Colin Crossfe5ed4d2023-07-28 09:27:23 -0700467 // b/286885495, https://bugzilla.redhat.com/show_bug.cgi?id=2227130: some versions of Fedora include patches
468 // to unzip to enable zipbomb detection that incorrectly handle zip64 and data descriptors and fail on large
469 // zip files produced by soong_zip. Disable zipbomb detection.
470 ret.environ.Set("UNZIP_DISABLE_ZIPBOMB_DETECTION", "TRUE")
471
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800472 outDir := ret.OutDir()
473 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800474 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800475 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800476 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800477 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800478 }
Colin Cross28f527c2019-11-26 16:19:04 -0800479
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700480 ret.environ.Set("BUILD_DATETIME_FILE", ret.sandboxPath(wd, buildDateTimeFile))
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800481
Cole Faust583dfb42023-09-28 13:56:30 -0700482 if _, ok := ret.environ.Get("BUILD_USERNAME"); !ok {
483 username := "unknown"
484 if u, err := user.Current(); err == nil {
485 username = u.Username
486 } else {
487 ctx.Println("Failed to get current user:", err)
488 }
489 ret.environ.Set("BUILD_USERNAME", username)
490 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700491 ret.environ.Set("PWD", ret.sandboxPath(wd, wd))
Cole Faust583dfb42023-09-28 13:56:30 -0700492
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400493 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400494 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400495 ret.environ.Set(k, v)
496 }
497 }
498
Patrice Arruda96850362020-08-11 20:41:11 +0000499 c := Config{ret}
500 storeConfigMetrics(ctx, c)
501 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700502}
503
Patrice Arruda13848222019-04-22 17:12:02 -0700504// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
505// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700506func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
507 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700508}
509
LaMont Jones9a912862023-11-06 22:11:08 +0000510// Prepare for getting make variables. For them to be accurate, we need to have
511// obtained PRODUCT_RELEASE_CONFIG_MAPS.
512//
513// Returns:
514//
515// Whether config should be called again.
516//
517// TODO: when converting product config to a declarative language, make sure
518// that PRODUCT_RELEASE_CONFIG_MAPS is properly handled as a separate step in
519// that process.
520func SetProductReleaseConfigMaps(ctx Context, config Config) bool {
521 ctx.BeginTrace(metrics.RunKati, "SetProductReleaseConfigMaps")
522 defer ctx.EndTrace()
523
524 if config.SkipConfig() {
525 // This duplicates the logic from Build to skip product config
526 // if the user has explicitly said to.
527 return false
528 }
529
530 releaseConfigVars := []string{
531 "PRODUCT_RELEASE_CONFIG_MAPS",
532 }
533
534 origValue, _ := config.environ.Get("PRODUCT_RELEASE_CONFIG_MAPS")
535 // Get the PRODUCT_RELEASE_CONFIG_MAPS for this product, to avoid polluting the environment
536 // when we run product config to get the rest of the make vars.
537 releaseMapVars, err := dumpMakeVars(ctx, config, nil, releaseConfigVars, false, "")
538 if err != nil {
539 ctx.Fatalln("Error getting PRODUCT_RELEASE_CONFIG_MAPS:", err)
540 }
541 productReleaseConfigMaps := releaseMapVars["PRODUCT_RELEASE_CONFIG_MAPS"]
542 os.Setenv("PRODUCT_RELEASE_CONFIG_MAPS", productReleaseConfigMaps)
543 return origValue != productReleaseConfigMaps
544}
545
Patrice Arruda96850362020-08-11 20:41:11 +0000546// storeConfigMetrics selects a set of configuration information and store in
547// the metrics system for further analysis.
548func storeConfigMetrics(ctx Context, config Config) {
549 if ctx.Metrics == nil {
550 return
551 }
552
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400553 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000554
555 s := &smpb.SystemResourceInfo{
556 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
557 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
558 }
559 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000560}
561
Jeongik Cha8d63d562023-03-17 03:52:13 +0900562func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900563 switch s {
564 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900565 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900566 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900567 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900568 case EXTERNAL_FILE:
569 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900570 case HINT_FROM_SOONG:
571 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900572 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900573 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900574 }
575}
576
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400577func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700578 c := &smpb.BuildConfig{
Colin Cross8d411ff2023-12-07 10:31:24 -0800579 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
580 UseGoma: proto.Bool(config.UseGoma()),
581 UseRbe: proto.Bool(config.UseRBE()),
582 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400583 }
Yu Liue737a992021-10-04 13:21:41 -0700584 c.Targets = append(c.Targets, config.arguments...)
585
586 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400587}
588
Patrice Arruda13848222019-04-22 17:12:02 -0700589// getConfigArgs processes the command arguments based on the build action and creates a set of new
590// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700591func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700592 // The next block of code verifies that the current directory is the root directory of the source
593 // tree. It then finds the relative path of dir based on the root directory of the source tree
594 // and verify that dir is inside of the source tree.
595 checkTopDir(ctx)
596 topDir, err := os.Getwd()
597 if err != nil {
598 ctx.Fatalf("Error retrieving top directory: %v", err)
599 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700600 dir, err = filepath.EvalSymlinks(dir)
601 if err != nil {
602 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
603 }
Patrice Arruda13848222019-04-22 17:12:02 -0700604 dir, err = filepath.Abs(dir)
605 if err != nil {
606 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
607 }
608 relDir, err := filepath.Rel(topDir, dir)
609 if err != nil {
610 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
611 }
612 // If there are ".." in the path, it's not in the source tree.
613 if strings.Contains(relDir, "..") {
614 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
615 }
616
617 configArgs := args[:]
618
619 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
620 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
621 targetNamePrefix := "MODULES-IN-"
622 if inList("GET-INSTALL-PATH", configArgs) {
623 targetNamePrefix = "GET-INSTALL-PATH-IN-"
624 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
625 }
626
Patrice Arruda13848222019-04-22 17:12:02 -0700627 var targets []string
628
629 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700630 case BUILD_MODULES:
631 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700632 case BUILD_MODULES_IN_A_DIRECTORY:
633 // If dir is the root source tree, all the modules are built of the source tree are built so
634 // no need to find the build file.
635 if topDir == dir {
636 break
637 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700638
Patrice Arruda13848222019-04-22 17:12:02 -0700639 buildFile := findBuildFile(ctx, relDir)
640 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700641 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700642 }
Patrice Arruda13848222019-04-22 17:12:02 -0700643 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
644 case BUILD_MODULES_IN_DIRECTORIES:
645 newConfigArgs, dirs := splitArgs(configArgs)
646 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700647 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700648 }
649
650 // Tidy only override all other specified targets.
651 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
652 if tidyOnly == "true" || tidyOnly == "1" {
653 configArgs = append(configArgs, "tidy_only")
654 } else {
655 configArgs = append(configArgs, targets...)
656 }
657
658 return configArgs
659}
660
661// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
662func convertToTarget(dir string, targetNamePrefix string) string {
663 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
664}
665
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700666// hasBuildFile returns true if dir contains an Android build file.
667func hasBuildFile(ctx Context, dir string) bool {
668 for _, buildFile := range buildFiles {
669 _, err := os.Stat(filepath.Join(dir, buildFile))
670 if err == nil {
671 return true
672 }
673 if !os.IsNotExist(err) {
674 ctx.Fatalf("Error retrieving the build file stats: %v", err)
675 }
676 }
677 return false
678}
679
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700680// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
681// in the current and any sub directory of dir. If a build file is not found, traverse the path
682// up by one directory and repeat again until either a build file is found or reached to the root
683// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
684// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700685func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700686 // If the string is empty or ".", assume it is top directory of the source tree.
687 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700688 return ""
689 }
690
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700691 found := false
692 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
693 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
694 if err != nil {
695 return err
696 }
697 if found {
698 return filepath.SkipDir
699 }
700 if info.IsDir() {
701 return nil
702 }
703 for _, buildFile := range buildFiles {
704 if info.Name() == buildFile {
705 found = true
706 return filepath.SkipDir
707 }
708 }
709 return nil
710 })
711 if err != nil {
712 ctx.Fatalf("Error finding Android build file: %v", err)
713 }
714
715 if found {
716 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700717 }
718 }
719
720 return ""
721}
722
723// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
724func splitArgs(args []string) (newArgs []string, dirs []string) {
725 specialArgs := map[string]bool{
726 "showcommands": true,
727 "snod": true,
728 "dist": true,
729 "checkbuild": true,
730 }
731
732 newArgs = []string{}
733 dirs = []string{}
734
735 for _, arg := range args {
736 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
737 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
738 newArgs = append(newArgs, arg)
739 continue
740 }
741
742 if _, ok := specialArgs[arg]; ok {
743 newArgs = append(newArgs, arg)
744 continue
745 }
746
747 dirs = append(dirs, arg)
748 }
749
750 return newArgs, dirs
751}
752
753// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
754// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
755// source root tree where the build action command was invoked. Each directory is validated if the
756// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700757func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700758 for _, dir := range dirs {
759 // The directory may have specified specific modules to build. ":" is the separator to separate
760 // the directory and the list of modules.
761 s := strings.Split(dir, ":")
762 l := len(s)
763 if l > 2 { // more than one ":" was specified.
764 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
765 }
766
767 dir = filepath.Join(relDir, s[0])
768 if _, err := os.Stat(dir); err != nil {
769 ctx.Fatalf("couldn't find directory %s", dir)
770 }
771
772 // Verify that if there are any targets specified after ":". Each target is separated by ",".
773 var newTargets []string
774 if l == 2 && s[1] != "" {
775 newTargets = strings.Split(s[1], ",")
776 if inList("", newTargets) {
777 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
778 }
779 }
780
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700781 // If there are specified targets to build in dir, an android build file must exist for the one
782 // shot build. For the non-targets case, find the appropriate build file and build all the
783 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700784 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700785 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700786 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
787 }
788 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700789 buildFile := findBuildFile(ctx, dir)
790 if buildFile == "" {
791 ctx.Fatalf("Build file not found for %s directory", dir)
792 }
793 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700794 }
795
Patrice Arruda13848222019-04-22 17:12:02 -0700796 targets = append(targets, newTargets...)
797 }
798
Dan Willemsence41e942019-07-29 23:39:30 -0700799 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700800}
801
Dan Willemsen9b587492017-07-10 22:13:00 -0700802func (c *configImpl) parseArgs(ctx Context, args []string) {
803 for i := 0; i < len(args); i++ {
804 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100805 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700806 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200807 } else if arg == "--empty-ninja-file" {
808 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100809 } else if arg == "--skip-ninja" {
810 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700811 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700812 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
813 // but it does run a Kati ninja file if the .kati_enabled marker file was created
814 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000815 c.skipConfig = true
816 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100817 } else if arg == "--soong-only" {
818 c.skipKati = true
819 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200820 } else if arg == "--config-only" {
821 c.skipKati = true
822 c.skipKatiNinja = true
823 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700824 } else if arg == "--skip-config" {
825 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700826 } else if arg == "--skip-soong-tests" {
827 c.skipSoongTests = true
Colin Cross106d6ef2023-10-24 10:34:56 -0700828 } else if arg == "--no-skip-soong-tests" {
829 c.skipSoongTests = false
MarkDacekd0e7cd32022-12-02 22:22:40 +0000830 } else if arg == "--skip-metrics-upload" {
831 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500832 } else if arg == "--mk-metrics" {
833 c.reportMkMetrics = true
Spandan Das394aa322022-11-03 17:02:10 +0000834 } else if arg == "--search-api-dir" {
835 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900836 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
837 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
838 if source == "ninja_log" {
839 c.ninjaWeightListSource = NINJA_LOG
840 } else if source == "evenly_distributed" {
841 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
842 } else if source == "not_used" {
843 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900844 } else if source == "soong" {
845 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900846 } else if strings.HasPrefix(source, "file,") {
847 c.ninjaWeightListSource = EXTERNAL_FILE
848 filePath := strings.TrimPrefix(source, "file,")
849 err := validateNinjaWeightList(filePath)
850 if err != nil {
851 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
852 }
853 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
854 if err != nil {
855 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
856 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900857 } else {
858 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
859 }
Jihoon Kang2a929ad2023-06-08 19:02:07 +0000860 } else if arg == "--build-from-source-stub" {
861 c.buildFromSourceStub = true
Yu Liufa297642024-06-11 00:13:02 +0000862 } else if arg == "--incremental-build-actions" {
863 c.incrementalBuildActions = true
MarkDacekb96561e2022-12-02 04:34:43 +0000864 } else if strings.HasPrefix(arg, "--build-command=") {
865 buildCmd := strings.TrimPrefix(arg, "--build-command=")
866 // remove quotations
867 buildCmd = strings.TrimPrefix(buildCmd, "\"")
868 buildCmd = strings.TrimSuffix(buildCmd, "\"")
869 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacek6614d9c2022-12-07 21:57:38 +0000870 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
871 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
872 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
873 if err == nil {
874 c.buildStartedTime = val
875 } else {
876 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
877 }
MarkDacekf47e1422023-04-19 16:47:36 +0000878 } else if arg == "--ensure-allowlist-integrity" {
879 c.ensureAllowlistIntegrity = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700880 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700881 parseArgNum := func(def int) int {
882 if len(arg) > 2 {
883 p, err := strconv.ParseUint(arg[2:], 10, 31)
884 if err != nil {
885 ctx.Fatalf("Failed to parse %q: %v", arg, err)
886 }
887 return int(p)
888 } else if i+1 < len(args) {
889 p, err := strconv.ParseUint(args[i+1], 10, 31)
890 if err == nil {
891 i++
892 return int(p)
893 }
894 }
895 return def
896 }
897
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700898 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700899 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700900 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700901 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700902 } else {
903 ctx.Fatalln("Unknown option:", arg)
904 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700905 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700906 if k == "OUT_DIR" {
907 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
908 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700909 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700910 } else if arg == "dist" {
911 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200912 } else if arg == "json-module-graph" {
913 c.jsonModuleGraph = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200914 } else if arg == "queryview" {
915 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200916 } else if arg == "soong_docs" {
917 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700918 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700919 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800920 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700921 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700922 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700923 }
924 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700925}
926
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900927func validateNinjaWeightList(weightListFilePath string) (err error) {
928 data, err := os.ReadFile(weightListFilePath)
929 if err != nil {
930 return
931 }
932 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
933 for _, line := range lines {
934 fields := strings.Split(line, ",")
935 if len(fields) != 2 {
936 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
937 }
938 _, err = strconv.Atoi(fields[1])
939 if err != nil {
940 return
941 }
942 }
943 return
944}
945
Dan Willemsened869522018-01-08 14:58:46 -0800946func (c *configImpl) configureLocale(ctx Context) {
947 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
948 output, err := cmd.Output()
949
950 var locales []string
951 if err == nil {
952 locales = strings.Split(string(output), "\n")
953 } else {
954 // If we're unable to list the locales, let's assume en_US.UTF-8
955 locales = []string{"en_US.UTF-8"}
956 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
957 }
958
959 // gettext uses LANGUAGE, which is passed directly through
960
961 // For LANG and LC_*, only preserve the evaluated version of
962 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800963 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800964 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800965 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800966 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800967 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800968 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800969 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800970 }
971
972 c.environ.UnsetWithPrefix("LC_")
973
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800974 if userLang != "" {
975 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800976 }
977
978 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
979 // for others)
980 if inList("C.UTF-8", locales) {
981 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500982 } else if inList("C.utf8", locales) {
983 // These normalize to the same thing
984 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800985 } else if inList("en_US.UTF-8", locales) {
986 c.environ.Set("LANG", "en_US.UTF-8")
987 } else if inList("en_US.utf8", locales) {
988 // These normalize to the same thing
989 c.environ.Set("LANG", "en_US.UTF-8")
990 } else {
991 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
992 }
993}
994
Dan Willemsen1e704462016-08-21 15:17:17 -0700995func (c *configImpl) Environment() *Environment {
996 return c.environ
997}
998
999func (c *configImpl) Arguments() []string {
1000 return c.arguments
1001}
1002
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001003func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001004 if len(c.Arguments()) > 0 {
1005 // Explicit targets requested that are not special targets like b2pbuild
1006 // or the JSON module graph
1007 return true
1008 }
1009
Colin Cross8d411ff2023-12-07 10:31:24 -08001010 if !c.JsonModuleGraph() && !c.Queryview() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001011 // Command line was empty, the default Ninja target is built
1012 return true
1013 }
1014
Colin Cross8d411ff2023-12-07 10:31:24 -08001015 if c.Dist() {
Liz Kammer88677422021-12-15 15:03:19 -05001016 return true
1017 }
1018
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001019 // build.ninja doesn't need to be generated
1020 return false
1021}
1022
Dan Willemsen1e704462016-08-21 15:17:17 -07001023func (c *configImpl) OutDir() string {
1024 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -07001025 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -07001026 }
1027 return "out"
1028}
1029
Dan Willemsen8a073a82017-02-04 17:30:44 -08001030func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -04001031 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001032}
1033
1034func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001035 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001036}
1037
Dan Willemsen1e704462016-08-21 15:17:17 -07001038func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001039 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001040 return c.arguments
1041 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001042 return c.ninjaArgs
1043}
1044
1045func (c *configImpl) SoongOutDir() string {
1046 return filepath.Join(c.OutDir(), "soong")
1047}
1048
Spandan Das394aa322022-11-03 17:02:10 +00001049func (c *configImpl) ApiSurfacesOutDir() string {
1050 return filepath.Join(c.OutDir(), "api_surfaces")
1051}
1052
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001053func (c *configImpl) PrebuiltOS() string {
1054 switch runtime.GOOS {
1055 case "linux":
1056 return "linux-x86"
1057 case "darwin":
1058 return "darwin-x86"
1059 default:
1060 panic("Unknown GOOS")
1061 }
1062}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001063
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001064func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001065 if c.SkipKatiNinja() {
1066 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1067 } else {
1068 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1069 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001070}
1071
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001072func (c *configImpl) UsedEnvFile(tag string) string {
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001073 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
Qing Shen713c5422024-08-23 04:09:18 +00001074 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+c.CoverageSuffix()+"."+tag)
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001075 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001076 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1077}
1078
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001079func (c *configImpl) SoongDocsHtml() string {
1080 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1081}
1082
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001083func (c *configImpl) QueryviewMarkerFile() string {
1084 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1085}
1086
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001087func (c *configImpl) ModuleGraphFile() string {
1088 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1089}
1090
kgui67007242022-01-25 13:50:25 +08001091func (c *configImpl) ModuleActionsFile() string {
1092 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1093}
1094
Jeff Gastonefc1b412017-03-29 17:29:06 -07001095func (c *configImpl) TempDir() string {
1096 return shared.TempDirForOutDir(c.SoongOutDir())
1097}
1098
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001099func (c *configImpl) FileListDir() string {
1100 return filepath.Join(c.OutDir(), ".module_paths")
1101}
1102
Dan Willemsen1e704462016-08-21 15:17:17 -07001103func (c *configImpl) KatiSuffix() string {
1104 if c.katiSuffix != "" {
1105 return c.katiSuffix
1106 }
1107 panic("SetKatiSuffix has not been called")
1108}
1109
Colin Cross37193492017-11-16 17:55:00 -08001110// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1111// user is interested in additional checks at the expense of build time.
1112func (c *configImpl) Checkbuild() bool {
1113 return c.checkbuild
1114}
1115
Dan Willemsen8a073a82017-02-04 17:30:44 -08001116func (c *configImpl) Dist() bool {
1117 return c.dist
1118}
1119
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001120func (c *configImpl) JsonModuleGraph() bool {
1121 return c.jsonModuleGraph
1122}
1123
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001124func (c *configImpl) Queryview() bool {
1125 return c.queryview
1126}
1127
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001128func (c *configImpl) SoongDocs() bool {
1129 return c.soongDocs
1130}
1131
Dan Willemsen1e704462016-08-21 15:17:17 -07001132func (c *configImpl) IsVerbose() bool {
1133 return c.verbose
1134}
1135
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001136func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1137 return c.ninjaWeightListSource
1138}
1139
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001140func (c *configImpl) SkipKati() bool {
1141 return c.skipKati
1142}
1143
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001144func (c *configImpl) SkipKatiNinja() bool {
1145 return c.skipKatiNinja
1146}
1147
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001148func (c *configImpl) SkipSoong() bool {
1149 return c.skipSoong
1150}
1151
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001152func (c *configImpl) SkipNinja() bool {
1153 return c.skipNinja
1154}
1155
Anton Hansson5a7861a2021-06-04 10:09:01 +01001156func (c *configImpl) SetSkipNinja(v bool) {
1157 c.skipNinja = v
1158}
1159
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001160func (c *configImpl) SkipConfig() bool {
1161 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001162}
1163
Jihoon Kang1bff0342023-01-17 20:40:22 +00001164func (c *configImpl) BuildFromTextStub() bool {
Jihoon Kang2a929ad2023-06-08 19:02:07 +00001165 return !c.buildFromSourceStub
Jihoon Kang1bff0342023-01-17 20:40:22 +00001166}
1167
Dan Willemsen1e704462016-08-21 15:17:17 -07001168func (c *configImpl) TargetProduct() string {
1169 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1170 return v
1171 }
1172 panic("TARGET_PRODUCT is not defined")
1173}
1174
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001175func (c *configImpl) TargetProductOrErr() (string, error) {
1176 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1177 return v, nil
1178 }
1179 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1180}
1181
Qing Shen713c5422024-08-23 04:09:18 +00001182func (c *configImpl) CoverageSuffix() string {
1183 if v := c.environ.IsEnvTrue("EMMA_INSTRUMENT"); v {
1184 return ".coverage"
1185 }
1186 return ""
1187}
1188
Dan Willemsen02781d52017-05-12 19:28:13 -07001189func (c *configImpl) TargetDevice() string {
1190 return c.targetDevice
1191}
1192
1193func (c *configImpl) SetTargetDevice(device string) {
1194 c.targetDevice = device
1195}
1196
1197func (c *configImpl) TargetBuildVariant() string {
1198 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1199 return v
1200 }
1201 panic("TARGET_BUILD_VARIANT is not defined")
1202}
1203
Dan Willemsen1e704462016-08-21 15:17:17 -07001204func (c *configImpl) KatiArgs() []string {
1205 return c.katiArgs
1206}
1207
1208func (c *configImpl) Parallel() int {
1209 return c.parallel
1210}
1211
Sam Delmerico98a73292023-02-21 11:50:29 -05001212func (c *configImpl) GetSourceRootDirs() []string {
1213 return c.sourceRootDirs
1214}
1215
1216func (c *configImpl) SetSourceRootDirs(i []string) {
1217 c.sourceRootDirs = i
1218}
1219
MarkDacek6614d9c2022-12-07 21:57:38 +00001220func (c *configImpl) GetLogsPrefix() string {
1221 return c.logsPrefix
1222}
1223
1224func (c *configImpl) SetLogsPrefix(prefix string) {
1225 c.logsPrefix = prefix
1226}
1227
Colin Cross8b8bec32019-11-15 13:18:43 -08001228func (c *configImpl) HighmemParallel() int {
1229 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1230 return i
1231 }
1232
1233 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1234 parallel := c.Parallel()
1235 if c.UseRemoteBuild() {
1236 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1237 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1238 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1239 // Return 1/16th of the size of the local pool, rounding up.
1240 return (parallel + 15) / 16
1241 } else if c.totalRAM == 0 {
1242 // Couldn't detect the total RAM, don't restrict highmem processes.
1243 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001244 } else if c.totalRAM <= 16*1024*1024*1024 {
1245 // Less than 16GB of ram, restrict to 1 highmem processes
1246 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001247 } else if c.totalRAM <= 32*1024*1024*1024 {
1248 // Less than 32GB of ram, restrict to 2 highmem processes
1249 return 2
1250 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1251 // If less than 8GB total RAM per process, reduce the number of highmem processes
1252 return p
1253 }
1254 // No restriction on highmem processes
1255 return parallel
1256}
1257
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001258func (c *configImpl) TotalRAM() uint64 {
1259 return c.totalRAM
1260}
1261
Kousik Kumarec478642020-09-21 13:39:24 -04001262// ForceUseGoma determines whether we should override Goma deprecation
1263// and use Goma for the current build or not.
1264func (c *configImpl) ForceUseGoma() bool {
1265 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1266 v = strings.TrimSpace(v)
1267 if v != "" && v != "false" {
1268 return true
1269 }
1270 }
1271 return false
1272}
1273
Dan Willemsen1e704462016-08-21 15:17:17 -07001274func (c *configImpl) UseGoma() bool {
1275 if v, ok := c.environ.Get("USE_GOMA"); ok {
1276 v = strings.TrimSpace(v)
1277 if v != "" && v != "false" {
1278 return true
1279 }
1280 }
1281 return false
1282}
1283
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001284func (c *configImpl) StartGoma() bool {
1285 if !c.UseGoma() {
1286 return false
1287 }
1288
1289 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1290 v = strings.TrimSpace(v)
1291 if v != "" && v != "false" {
1292 return false
1293 }
1294 }
1295 return true
1296}
1297
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001298func (c *configImpl) canSupportRBE() bool {
Joe Onorato86f50e72024-06-24 14:28:25 -07001299 // Only supported on linux
1300 if runtime.GOOS != "linux" {
1301 return false
1302 }
1303
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001304 // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since
1305 // its unlikely that we will be able to obtain necessary creds without stubby.
1306 authType, _ := c.rbeAuth()
1307 if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") {
1308 return false
1309 }
Taylor Santiago3c16e612024-05-30 14:41:31 -07001310 if c.UseABFS() {
1311 return false
1312 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001313 return true
1314}
1315
Taylor Santiago3c16e612024-05-30 14:41:31 -07001316func (c *configImpl) UseABFS() bool {
1317 if v, ok := c.environ.Get("NO_ABFS"); ok {
1318 v = strings.ToLower(strings.TrimSpace(v))
1319 if v == "true" || v == "1" {
1320 return false
1321 }
1322 }
1323
1324 abfsBox := c.PrebuiltBuildTool("abfsbox")
1325 err := exec.Command(abfsBox, "hash", srcDirFileCheck).Run()
1326 return err == nil
1327}
1328
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001329func (c *configImpl) sandboxPath(base, in string) string {
1330 if !c.UseABFS() {
1331 return in
1332 }
1333
1334 rel, err := filepath.Rel(base, in)
1335 if err != nil {
1336 return in
1337 }
1338
1339 return filepath.Join(abfsSrcDir, rel)
1340}
1341
Ramy Medhatbbf25672019-07-17 12:30:04 +00001342func (c *configImpl) UseRBE() bool {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001343 // These alternate modes of running Soong do not use RBE / reclient.
Colin Cross8d411ff2023-12-07 10:31:24 -08001344 if c.Queryview() || c.JsonModuleGraph() {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001345 return false
1346 }
1347
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001348 if !c.canSupportRBE() {
Kousik Kumar67ad4342023-06-06 15:09:27 -04001349 return false
1350 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001351
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001352 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001353 v = strings.TrimSpace(v)
1354 if v != "" && v != "false" {
1355 return true
1356 }
1357 }
1358 return false
1359}
1360
1361func (c *configImpl) StartRBE() bool {
1362 if !c.UseRBE() {
1363 return false
1364 }
1365
1366 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1367 v = strings.TrimSpace(v)
1368 if v != "" && v != "false" {
1369 return false
1370 }
1371 }
1372 return true
1373}
1374
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001375func (c *configImpl) rbeProxyLogsDir() string {
1376 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001377 if v, ok := c.environ.Get(f); ok {
1378 return v
1379 }
1380 }
Ramy Medhatbc061762023-10-10 18:36:59 +00001381 return c.rbeTmpDir()
1382}
1383
1384func (c *configImpl) rbeDownloadTmpDir() string {
Cole Faust06ea5312023-10-18 17:38:40 -07001385 for _, f := range []string{"RBE_download_tmp_dir", "FLAG_download_tmp_dir"} {
Ramy Medhatbc061762023-10-10 18:36:59 +00001386 if v, ok := c.environ.Get(f); ok {
1387 return v
1388 }
1389 }
1390 return c.rbeTmpDir()
1391}
1392
1393func (c *configImpl) rbeTmpDir() string {
Yaowen Meid4da2662024-07-24 08:25:12 +00001394 return filepath.Join(c.SoongOutDir(), "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001395}
1396
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001397func (c *configImpl) rbeCacheDir() string {
1398 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1399 if v, ok := c.environ.Get(f); ok {
1400 return v
1401 }
1402 }
1403 return shared.JoinPath(c.SoongOutDir(), "rbe")
1404}
1405
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001406func (c *configImpl) shouldCleanupRBELogsDir() bool {
1407 // Perform a log directory cleanup only when the log directory
1408 // is auto created by the build rather than user-specified.
1409 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Yaowen Meid9108d22024-08-29 16:56:32 +00001410 if v, ok := c.environ.Get(f); ok {
1411 if v != c.rbeTmpDir() {
1412 return false
1413 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001414 }
1415 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001416 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001417}
1418
1419func (c *configImpl) rbeExecRoot() string {
1420 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1421 if v, ok := c.environ.Get(f); ok {
1422 return v
1423 }
1424 }
1425 wd, err := os.Getwd()
1426 if err != nil {
1427 return ""
1428 }
1429 return wd
1430}
1431
1432func (c *configImpl) rbeDir() string {
1433 if v, ok := c.environ.Get("RBE_DIR"); ok {
1434 return v
1435 }
1436 return "prebuilts/remoteexecution-client/live/"
1437}
1438
1439func (c *configImpl) rbeReproxy() string {
1440 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1441 if v, ok := c.environ.Get(f); ok {
1442 return v
1443 }
1444 }
1445 return filepath.Join(c.rbeDir(), "reproxy")
1446}
1447
1448func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001449 credFlags := []string{
1450 "use_application_default_credentials",
1451 "use_gce_credentials",
1452 "credential_file",
1453 "use_google_prod_creds",
1454 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001455 for _, cf := range credFlags {
1456 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1457 if v, ok := c.environ.Get(f); ok {
1458 v = strings.TrimSpace(v)
1459 if v != "" && v != "false" && v != "0" {
1460 return "RBE_" + cf, v
1461 }
1462 }
1463 }
1464 }
1465 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001466}
1467
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001468func (c *configImpl) rbeSockAddr(dir string) (string, error) {
Andus Yuc917eb82024-03-06 21:54:15 +00001469 // Absolute path socket addresses have a prefix of //. This should
1470 // be included in the length limit.
1471 maxNameLen := len(syscall.RawSockaddrUnix{}.Path) - 2
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001472 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1473
1474 name := filepath.Join(dir, base)
1475 if len(name) < maxNameLen {
1476 return name, nil
1477 }
1478
1479 name = filepath.Join("/tmp", base)
1480 if len(name) < maxNameLen {
1481 return name, nil
1482 }
1483
1484 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1485}
1486
Kousik Kumar7bc78192022-04-27 14:52:56 -04001487// IsGooglerEnvironment returns true if the current build is running
1488// on a Google developer machine and false otherwise.
1489func (c *configImpl) IsGooglerEnvironment() bool {
1490 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1491 if v, ok := c.environ.Get(cf); ok {
1492 return v == "googler"
1493 }
1494 return false
1495}
1496
1497// GoogleProdCredsExist determine whether credentials exist on the
1498// Googler machine to use remote execution.
1499func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001500 if googleProdCredsExistCache {
1501 return googleProdCredsExistCache
1502 }
andusyu0b3dc032023-06-21 17:29:32 -04001503 if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001504 return false
1505 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001506 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001507 return true
1508}
1509
1510// UseRemoteBuild indicates whether to use a remote build acceleration system
1511// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001512func (c *configImpl) UseRemoteBuild() bool {
1513 return c.UseGoma() || c.UseRBE()
1514}
1515
Kousik Kumar7bc78192022-04-27 14:52:56 -04001516// StubbyExists checks whether the stubby binary exists on the machine running
1517// the build.
1518func (c *configImpl) StubbyExists() bool {
1519 if _, err := exec.LookPath("stubby"); err != nil {
1520 return false
1521 }
1522 return true
1523}
1524
Dan Willemsen1e704462016-08-21 15:17:17 -07001525// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001526// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001527// still limited by Parallel()
1528func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001529 if !c.UseRemoteBuild() {
1530 return 0
1531 }
1532 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1533 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001534 }
1535 return 500
1536}
1537
1538func (c *configImpl) SetKatiArgs(args []string) {
1539 c.katiArgs = args
1540}
1541
1542func (c *configImpl) SetNinjaArgs(args []string) {
1543 c.ninjaArgs = args
1544}
1545
1546func (c *configImpl) SetKatiSuffix(suffix string) {
1547 c.katiSuffix = suffix
1548}
1549
Dan Willemsene0879fc2017-08-04 15:06:27 -07001550func (c *configImpl) LastKatiSuffixFile() string {
1551 return filepath.Join(c.OutDir(), "last_kati_suffix")
1552}
1553
1554func (c *configImpl) HasKatiSuffix() bool {
1555 return c.katiSuffix != ""
1556}
1557
Dan Willemsen1e704462016-08-21 15:17:17 -07001558func (c *configImpl) KatiEnvFile() string {
1559 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1560}
1561
Dan Willemsen29971232018-09-26 14:58:30 -07001562func (c *configImpl) KatiBuildNinjaFile() string {
1563 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001564}
1565
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001566func (c *configImpl) KatiPackageNinjaFile() string {
1567 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1568}
1569
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001570func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001571 targetProduct, err := c.TargetProductOrErr()
1572 if err != nil {
1573 return filepath.Join(c.SoongOutDir(), "soong.variables")
1574 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001575 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".variables")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001576 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001577}
1578
Inseob Kim58c802f2024-06-11 10:59:00 +09001579func (c *configImpl) SoongExtraVarsFile() string {
1580 targetProduct, err := c.TargetProductOrErr()
1581 if err != nil {
1582 return filepath.Join(c.SoongOutDir(), "soong.extra.variables")
1583 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001584 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".extra.variables")
Inseob Kim58c802f2024-06-11 10:59:00 +09001585 }
1586}
1587
Dan Willemsen1e704462016-08-21 15:17:17 -07001588func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001589 targetProduct, err := c.TargetProductOrErr()
1590 if err != nil {
1591 return filepath.Join(c.SoongOutDir(), "build.ninja")
1592 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001593 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+c.CoverageSuffix()+".ninja")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001594 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001595}
1596
1597func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001598 if c.katiSuffix == "" {
1599 return filepath.Join(c.OutDir(), "combined.ninja")
1600 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001601 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1602}
1603
1604func (c *configImpl) SoongAndroidMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001605 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001606}
1607
1608func (c *configImpl) SoongMakeVarsMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001609 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001610}
1611
Colin Crossaa9a2732023-10-27 10:54:27 -07001612func (c *configImpl) SoongBuildMetrics() string {
Colin Crossb67b0612023-10-31 10:02:45 -07001613 return filepath.Join(c.LogsDir(), "soong_build_metrics.pb")
Colin Crossaa9a2732023-10-27 10:54:27 -07001614}
1615
Dan Willemsenf052f782017-05-18 15:29:04 -07001616func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001617 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001618}
1619
Dan Willemsen02781d52017-05-12 19:28:13 -07001620func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001621 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1622}
1623
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001624func (c *configImpl) KatiPackageMkDir() string {
1625 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1626}
1627
Dan Willemsenf052f782017-05-18 15:29:04 -07001628func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001629 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001630}
1631
1632func (c *configImpl) HostOut() string {
1633 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1634}
1635
1636// This probably needs to be multi-valued, so not exporting it for now
1637func (c *configImpl) hostCrossOut() string {
1638 if runtime.GOOS == "linux" {
1639 return filepath.Join(c.hostOutRoot(), "windows-x86")
1640 } else {
1641 return ""
1642 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001643}
1644
Dan Willemsen1e704462016-08-21 15:17:17 -07001645func (c *configImpl) HostPrebuiltTag() string {
1646 if runtime.GOOS == "linux" {
1647 return "linux-x86"
1648 } else if runtime.GOOS == "darwin" {
1649 return "darwin-x86"
1650 } else {
1651 panic("Unsupported OS")
1652 }
1653}
Dan Willemsenf173d592017-04-27 14:28:00 -07001654
Taylor Santiago3c16e612024-05-30 14:41:31 -07001655func (c *configImpl) KatiBin() string {
1656 binName := "ckati"
1657 if c.UseABFS() {
1658 binName = "ckati-wrap"
1659 }
1660
1661 return c.PrebuiltBuildTool(binName)
1662}
1663
1664func (c *configImpl) NinjaBin() string {
1665 binName := "ninja"
1666 if c.UseABFS() {
1667 binName = "ninjago"
1668 }
1669 return c.PrebuiltBuildTool(binName)
1670}
1671
Cole Faust4e58bba2024-08-22 14:27:03 -07001672func (c *configImpl) N2Bin() string {
1673 path := c.PrebuiltBuildTool("n2")
1674 // Use musl instead of glibc because glibc on the build server is old and has bugs
1675 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1676}
1677
LaMont Jonesece626c2024-09-03 11:19:31 -07001678func (c *configImpl) SisoBin() string {
1679 path := c.PrebuiltBuildTool("siso")
1680 // Use musl instead of glibc because glibc on the build server is old and has bugs
1681 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1682}
1683
Dan Willemsen8122bd52017-10-12 20:20:41 -07001684func (c *configImpl) PrebuiltBuildTool(name string) string {
Colin Cross7077be42024-10-04 20:37:16 +00001685 if c.environ.IsEnvTrue("SANITIZE_BUILD_TOOL_PREBUILTS") {
1686 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1687 if _, err := os.Stat(asan); err == nil {
1688 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -07001689 }
1690 }
1691 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1692}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001693
1694func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1695 c.brokenDupRules = val
1696}
1697
1698func (c *configImpl) BuildBrokenDupRules() bool {
1699 return c.brokenDupRules
1700}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001701
Dan Willemsen25e6f092019-04-09 10:22:43 -07001702func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1703 c.brokenUsesNetwork = val
1704}
1705
1706func (c *configImpl) BuildBrokenUsesNetwork() bool {
1707 return c.brokenUsesNetwork
1708}
1709
Dan Willemsene3336352020-01-02 19:10:38 -08001710func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1711 c.brokenNinjaEnvVars = val
1712}
1713
1714func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1715 return c.brokenNinjaEnvVars
1716}
1717
Spandan Das28a6f192024-07-01 21:00:25 +00001718func (c *configImpl) SetBuildBrokenMissingOutputs(val bool) {
1719 c.brokenMissingOutputs = val
1720}
1721
1722func (c *configImpl) BuildBrokenMissingOutputs() bool {
1723 return c.brokenMissingOutputs
1724}
1725
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001726func (c *configImpl) SetTargetDeviceDir(dir string) {
1727 c.targetDeviceDir = dir
1728}
1729
1730func (c *configImpl) TargetDeviceDir() string {
1731 return c.targetDeviceDir
1732}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001733
Patrice Arruda219eef32020-06-01 17:29:30 +00001734func (c *configImpl) BuildDateTime() string {
1735 return c.buildDateTime
1736}
1737
1738func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001739 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001740}
Patrice Arruda83842d72020-12-08 19:42:08 +00001741
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001742// LogsDir returns the absolute path to the logs directory where build log and
1743// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001744// directory. If the argument dist is specified, the logs directory
1745// is <dist_dir>/logs.
1746func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001747 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001748 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001749 // 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 -05001750 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001751 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001752 absDir, err := filepath.Abs(dir)
1753 if err != nil {
1754 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1755 os.Exit(1)
1756 }
1757 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001758}
1759
Chris Parsons53f68ae2022-03-03 12:01:40 -05001760// MkFileMetrics returns the file path for make-related metrics.
1761func (c *configImpl) MkMetrics() string {
1762 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1763}
1764
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001765func (c *configImpl) SetEmptyNinjaFile(v bool) {
1766 c.emptyNinjaFile = v
1767}
1768
1769func (c *configImpl) EmptyNinjaFile() bool {
1770 return c.emptyNinjaFile
1771}
Yu Liu6e13b402021-07-27 14:29:06 -07001772
MarkDacekd0e7cd32022-12-02 22:22:40 +00001773func (c *configImpl) SkipMetricsUpload() bool {
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001774 // b/362625275 - Metrics upload sometimes prevents abfs unmount
1775 if c.UseABFS() {
1776 return true
1777 }
1778
MarkDacekd0e7cd32022-12-02 22:22:40 +00001779 return c.skipMetricsUpload
1780}
1781
MarkDacekf47e1422023-04-19 16:47:36 +00001782func (c *configImpl) EnsureAllowlistIntegrity() bool {
1783 return c.ensureAllowlistIntegrity
1784}
1785
MarkDacek6614d9c2022-12-07 21:57:38 +00001786// Returns a Time object if one was passed via a command-line flag.
1787// Otherwise returns the passed default.
1788func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1789 if c.buildStartedTime == 0 {
1790 return defaultTime
1791 }
1792 return time.UnixMilli(c.buildStartedTime)
1793}
1794
Yu Liu6e13b402021-07-27 14:29:06 -07001795func GetMetricsUploader(topDir string, env *Environment) string {
1796 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1797 metricsUploader := filepath.Join(topDir, p)
1798 if _, err := os.Stat(metricsUploader); err == nil {
1799 return metricsUploader
1800 }
1801 }
1802
1803 return ""
1804}