blob: 851a22ae1c524c294eaf9557806e0841d69839e2 [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 Jonesece626c2024-09-03 11:19:31 -0700300 ret.ninjaCommand = NINJA_NINJA
301 switch os.Getenv("SOONG_NINJA") {
302 case "n2":
303 ret.ninjaCommand = NINJA_N2
304 case "siso":
305 ret.ninjaCommand = NINJA_SISO
306 default:
307 if os.Getenv("SOONG_USE_N2") == "true" {
308 ret.ninjaCommand = NINJA_N2
309 }
Cole Faustbee030d2024-01-03 13:45:48 -0800310 }
311
Dan Willemsen1e704462016-08-21 15:17:17 -0700312 ret.environ.Unset(
313 // We're already using it
314 "USE_SOONG_UI",
315
316 // We should never use GOROOT/GOPATH from the shell environment
317 "GOROOT",
318 "GOPATH",
319
320 // These should only come from Soong, not the environment.
321 "CLANG",
322 "CLANG_CXX",
323 "CCC_CC",
324 "CCC_CXX",
325
326 // Used by the goma compiler wrapper, but should only be set by
327 // gomacc
328 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800329
330 // We handle this above
331 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700332
Dan Willemsen2d31a442018-10-20 21:33:41 -0700333 // This is handled above too, and set for individual commands later
334 "DIST_DIR",
335
Dan Willemsen68a09852017-04-18 13:56:57 -0700336 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000337 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700338 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700339 "DISPLAY",
340 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000341 "JAVAC",
Nathan Egge978c9342024-07-03 21:13:03 +0000342 "LEX",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700343 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700344 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700345
346 // Drop make flags
347 "MAKEFLAGS",
348 "MAKELEVEL",
349 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700350
351 // Set in envsetup.sh, reset in makefiles
352 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700353
354 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
355 "ANDROID_BUILD_TOP",
356 "ANDROID_HOST_OUT",
357 "ANDROID_PRODUCT_OUT",
358 "ANDROID_HOST_OUT_TESTCASES",
359 "ANDROID_TARGET_OUT_TESTCASES",
360 "ANDROID_TOOLCHAIN",
361 "ANDROID_TOOLCHAIN_2ND_ARCH",
362 "ANDROID_DEV_SCRIPTS",
363 "ANDROID_EMULATOR_PREBUILTS",
364 "ANDROID_PRE_BUILD_PATHS",
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800365
366 // We read it here already, don't let others share in the fun
367 "GENERATE_SOONG_DEBUG",
Cole Faustbee030d2024-01-03 13:45:48 -0800368
LaMont Jonesece626c2024-09-03 11:19:31 -0700369 // Use config.ninjaCommand instead.
370 "SOONG_NINJA",
Cole Faustbee030d2024-01-03 13:45:48 -0800371 "SOONG_USE_N2",
Dan Willemsen1e704462016-08-21 15:17:17 -0700372 )
373
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400374 if ret.UseGoma() || ret.ForceUseGoma() {
375 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
376 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400377 }
378
Dan Willemsen1e704462016-08-21 15:17:17 -0700379 // Tell python not to spam the source tree with .pyc files.
380 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
381
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400382 tmpDir := absPath(ctx, ret.TempDir())
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700383 ret.environ.Set("TMPDIR", ret.sandboxPath(wd, tmpDir))
Dan Willemsen32a669b2018-03-08 19:42:00 -0800384
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700385 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
386 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
387 "llvm-binutils-stable/llvm-symbolizer")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700388 ret.environ.Set("ASAN_SYMBOLIZER_PATH", ret.sandboxPath(wd, absPath(ctx, symbolizerPath)))
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700389
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800390 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700391 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800392
Yu Liu6e13b402021-07-27 14:29:06 -0700393 srcDir := absPath(ctx, ".")
394 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700395 ctx.Println("You are building in a directory whose absolute path contains a space character:")
396 ctx.Println()
397 ctx.Printf("%q\n", srcDir)
398 ctx.Println()
399 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700400 }
401
Yu Liu6e13b402021-07-27 14:29:06 -0700402 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
403
Dan Willemsendb8457c2017-05-12 16:38:17 -0700404 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700405 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
406 ctx.Println()
407 ctx.Printf("%q\n", outDir)
408 ctx.Println()
409 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700410 }
411
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000412 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700413 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
414 ctx.Println()
415 ctx.Printf("%q\n", distDir)
416 ctx.Println()
417 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700418 }
419
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700420 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000421 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
Sorin Basca0760c892023-11-29 19:13:55 +0000422 java21Home := filepath.Join("prebuilts/jdk/jdk21", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700423 javaHome := func() string {
424 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
425 return override
426 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000427 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000428 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 +0100429 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000430 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000431 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 +0000432 }
Sorin Basca5dfa2382024-03-11 17:23:06 +0000433 if toolchain21, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN"); ok && toolchain21 != "true" {
434 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
435 }
436 return java21Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700437 }()
438 absJavaHome := absPath(ctx, javaHome)
439
Dan Willemsened869522018-01-08 14:58:46 -0800440 ret.configureLocale(ctx)
441
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000442 newPath := []string{filepath.Join(absJavaHome, "bin")}
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700443 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000444 newPath = append(newPath, path)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700445 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100446
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700447 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700448 ret.environ.Set("JAVA_HOME", ret.sandboxPath(wd, absJavaHome))
449 ret.environ.Set("ANDROID_JAVA_HOME", ret.sandboxPath(wd, javaHome))
450 ret.environ.Set("ANDROID_JAVA8_HOME", ret.sandboxPath(wd, java8Home))
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700451 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
452
Colin Crossfe5ed4d2023-07-28 09:27:23 -0700453 // b/286885495, https://bugzilla.redhat.com/show_bug.cgi?id=2227130: some versions of Fedora include patches
454 // to unzip to enable zipbomb detection that incorrectly handle zip64 and data descriptors and fail on large
455 // zip files produced by soong_zip. Disable zipbomb detection.
456 ret.environ.Set("UNZIP_DISABLE_ZIPBOMB_DETECTION", "TRUE")
457
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800458 outDir := ret.OutDir()
459 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800460 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800461 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800462 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800463 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800464 }
Colin Cross28f527c2019-11-26 16:19:04 -0800465
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700466 ret.environ.Set("BUILD_DATETIME_FILE", ret.sandboxPath(wd, buildDateTimeFile))
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800467
Cole Faust583dfb42023-09-28 13:56:30 -0700468 if _, ok := ret.environ.Get("BUILD_USERNAME"); !ok {
469 username := "unknown"
470 if u, err := user.Current(); err == nil {
471 username = u.Username
472 } else {
473 ctx.Println("Failed to get current user:", err)
474 }
475 ret.environ.Set("BUILD_USERNAME", username)
476 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700477 ret.environ.Set("PWD", ret.sandboxPath(wd, wd))
Cole Faust583dfb42023-09-28 13:56:30 -0700478
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400479 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400480 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400481 ret.environ.Set(k, v)
482 }
483 }
484
Patrice Arruda96850362020-08-11 20:41:11 +0000485 c := Config{ret}
486 storeConfigMetrics(ctx, c)
487 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700488}
489
Patrice Arruda13848222019-04-22 17:12:02 -0700490// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
491// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700492func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
493 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700494}
495
LaMont Jones9a912862023-11-06 22:11:08 +0000496// Prepare for getting make variables. For them to be accurate, we need to have
497// obtained PRODUCT_RELEASE_CONFIG_MAPS.
498//
499// Returns:
500//
501// Whether config should be called again.
502//
503// TODO: when converting product config to a declarative language, make sure
504// that PRODUCT_RELEASE_CONFIG_MAPS is properly handled as a separate step in
505// that process.
506func SetProductReleaseConfigMaps(ctx Context, config Config) bool {
507 ctx.BeginTrace(metrics.RunKati, "SetProductReleaseConfigMaps")
508 defer ctx.EndTrace()
509
510 if config.SkipConfig() {
511 // This duplicates the logic from Build to skip product config
512 // if the user has explicitly said to.
513 return false
514 }
515
516 releaseConfigVars := []string{
517 "PRODUCT_RELEASE_CONFIG_MAPS",
518 }
519
520 origValue, _ := config.environ.Get("PRODUCT_RELEASE_CONFIG_MAPS")
521 // Get the PRODUCT_RELEASE_CONFIG_MAPS for this product, to avoid polluting the environment
522 // when we run product config to get the rest of the make vars.
523 releaseMapVars, err := dumpMakeVars(ctx, config, nil, releaseConfigVars, false, "")
524 if err != nil {
525 ctx.Fatalln("Error getting PRODUCT_RELEASE_CONFIG_MAPS:", err)
526 }
527 productReleaseConfigMaps := releaseMapVars["PRODUCT_RELEASE_CONFIG_MAPS"]
528 os.Setenv("PRODUCT_RELEASE_CONFIG_MAPS", productReleaseConfigMaps)
529 return origValue != productReleaseConfigMaps
530}
531
Patrice Arruda96850362020-08-11 20:41:11 +0000532// storeConfigMetrics selects a set of configuration information and store in
533// the metrics system for further analysis.
534func storeConfigMetrics(ctx Context, config Config) {
535 if ctx.Metrics == nil {
536 return
537 }
538
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400539 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000540
541 s := &smpb.SystemResourceInfo{
542 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
543 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
544 }
545 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000546}
547
Jeongik Cha8d63d562023-03-17 03:52:13 +0900548func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900549 switch s {
550 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900551 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900552 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900553 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900554 case EXTERNAL_FILE:
555 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900556 case HINT_FROM_SOONG:
557 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900558 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900559 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900560 }
561}
562
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400563func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700564 c := &smpb.BuildConfig{
Colin Cross8d411ff2023-12-07 10:31:24 -0800565 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
566 UseGoma: proto.Bool(config.UseGoma()),
567 UseRbe: proto.Bool(config.UseRBE()),
568 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400569 }
Yu Liue737a992021-10-04 13:21:41 -0700570 c.Targets = append(c.Targets, config.arguments...)
571
572 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400573}
574
Patrice Arruda13848222019-04-22 17:12:02 -0700575// getConfigArgs processes the command arguments based on the build action and creates a set of new
576// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700577func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700578 // The next block of code verifies that the current directory is the root directory of the source
579 // tree. It then finds the relative path of dir based on the root directory of the source tree
580 // and verify that dir is inside of the source tree.
581 checkTopDir(ctx)
582 topDir, err := os.Getwd()
583 if err != nil {
584 ctx.Fatalf("Error retrieving top directory: %v", err)
585 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700586 dir, err = filepath.EvalSymlinks(dir)
587 if err != nil {
588 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
589 }
Patrice Arruda13848222019-04-22 17:12:02 -0700590 dir, err = filepath.Abs(dir)
591 if err != nil {
592 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
593 }
594 relDir, err := filepath.Rel(topDir, dir)
595 if err != nil {
596 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
597 }
598 // If there are ".." in the path, it's not in the source tree.
599 if strings.Contains(relDir, "..") {
600 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
601 }
602
603 configArgs := args[:]
604
605 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
606 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
607 targetNamePrefix := "MODULES-IN-"
608 if inList("GET-INSTALL-PATH", configArgs) {
609 targetNamePrefix = "GET-INSTALL-PATH-IN-"
610 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
611 }
612
Patrice Arruda13848222019-04-22 17:12:02 -0700613 var targets []string
614
615 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700616 case BUILD_MODULES:
617 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700618 case BUILD_MODULES_IN_A_DIRECTORY:
619 // If dir is the root source tree, all the modules are built of the source tree are built so
620 // no need to find the build file.
621 if topDir == dir {
622 break
623 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700624
Patrice Arruda13848222019-04-22 17:12:02 -0700625 buildFile := findBuildFile(ctx, relDir)
626 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700627 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700628 }
Patrice Arruda13848222019-04-22 17:12:02 -0700629 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
630 case BUILD_MODULES_IN_DIRECTORIES:
631 newConfigArgs, dirs := splitArgs(configArgs)
632 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700633 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700634 }
635
636 // Tidy only override all other specified targets.
637 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
638 if tidyOnly == "true" || tidyOnly == "1" {
639 configArgs = append(configArgs, "tidy_only")
640 } else {
641 configArgs = append(configArgs, targets...)
642 }
643
644 return configArgs
645}
646
647// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
648func convertToTarget(dir string, targetNamePrefix string) string {
649 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
650}
651
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700652// hasBuildFile returns true if dir contains an Android build file.
653func hasBuildFile(ctx Context, dir string) bool {
654 for _, buildFile := range buildFiles {
655 _, err := os.Stat(filepath.Join(dir, buildFile))
656 if err == nil {
657 return true
658 }
659 if !os.IsNotExist(err) {
660 ctx.Fatalf("Error retrieving the build file stats: %v", err)
661 }
662 }
663 return false
664}
665
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700666// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
667// in the current and any sub directory of dir. If a build file is not found, traverse the path
668// up by one directory and repeat again until either a build file is found or reached to the root
669// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
670// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700671func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700672 // If the string is empty or ".", assume it is top directory of the source tree.
673 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700674 return ""
675 }
676
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700677 found := false
678 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
679 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
680 if err != nil {
681 return err
682 }
683 if found {
684 return filepath.SkipDir
685 }
686 if info.IsDir() {
687 return nil
688 }
689 for _, buildFile := range buildFiles {
690 if info.Name() == buildFile {
691 found = true
692 return filepath.SkipDir
693 }
694 }
695 return nil
696 })
697 if err != nil {
698 ctx.Fatalf("Error finding Android build file: %v", err)
699 }
700
701 if found {
702 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700703 }
704 }
705
706 return ""
707}
708
709// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
710func splitArgs(args []string) (newArgs []string, dirs []string) {
711 specialArgs := map[string]bool{
712 "showcommands": true,
713 "snod": true,
714 "dist": true,
715 "checkbuild": true,
716 }
717
718 newArgs = []string{}
719 dirs = []string{}
720
721 for _, arg := range args {
722 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
723 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
724 newArgs = append(newArgs, arg)
725 continue
726 }
727
728 if _, ok := specialArgs[arg]; ok {
729 newArgs = append(newArgs, arg)
730 continue
731 }
732
733 dirs = append(dirs, arg)
734 }
735
736 return newArgs, dirs
737}
738
739// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
740// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
741// source root tree where the build action command was invoked. Each directory is validated if the
742// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700743func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700744 for _, dir := range dirs {
745 // The directory may have specified specific modules to build. ":" is the separator to separate
746 // the directory and the list of modules.
747 s := strings.Split(dir, ":")
748 l := len(s)
749 if l > 2 { // more than one ":" was specified.
750 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
751 }
752
753 dir = filepath.Join(relDir, s[0])
754 if _, err := os.Stat(dir); err != nil {
755 ctx.Fatalf("couldn't find directory %s", dir)
756 }
757
758 // Verify that if there are any targets specified after ":". Each target is separated by ",".
759 var newTargets []string
760 if l == 2 && s[1] != "" {
761 newTargets = strings.Split(s[1], ",")
762 if inList("", newTargets) {
763 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
764 }
765 }
766
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700767 // If there are specified targets to build in dir, an android build file must exist for the one
768 // shot build. For the non-targets case, find the appropriate build file and build all the
769 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700770 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700771 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700772 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
773 }
774 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700775 buildFile := findBuildFile(ctx, dir)
776 if buildFile == "" {
777 ctx.Fatalf("Build file not found for %s directory", dir)
778 }
779 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700780 }
781
Patrice Arruda13848222019-04-22 17:12:02 -0700782 targets = append(targets, newTargets...)
783 }
784
Dan Willemsence41e942019-07-29 23:39:30 -0700785 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700786}
787
Dan Willemsen9b587492017-07-10 22:13:00 -0700788func (c *configImpl) parseArgs(ctx Context, args []string) {
789 for i := 0; i < len(args); i++ {
790 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100791 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700792 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200793 } else if arg == "--empty-ninja-file" {
794 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100795 } else if arg == "--skip-ninja" {
796 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700797 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700798 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
799 // but it does run a Kati ninja file if the .kati_enabled marker file was created
800 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000801 c.skipConfig = true
802 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100803 } else if arg == "--soong-only" {
804 c.skipKati = true
805 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200806 } else if arg == "--config-only" {
807 c.skipKati = true
808 c.skipKatiNinja = true
809 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700810 } else if arg == "--skip-config" {
811 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700812 } else if arg == "--skip-soong-tests" {
813 c.skipSoongTests = true
Colin Cross106d6ef2023-10-24 10:34:56 -0700814 } else if arg == "--no-skip-soong-tests" {
815 c.skipSoongTests = false
MarkDacekd0e7cd32022-12-02 22:22:40 +0000816 } else if arg == "--skip-metrics-upload" {
817 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500818 } else if arg == "--mk-metrics" {
819 c.reportMkMetrics = true
Spandan Das394aa322022-11-03 17:02:10 +0000820 } else if arg == "--search-api-dir" {
821 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900822 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
823 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
824 if source == "ninja_log" {
825 c.ninjaWeightListSource = NINJA_LOG
826 } else if source == "evenly_distributed" {
827 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
828 } else if source == "not_used" {
829 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900830 } else if source == "soong" {
831 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900832 } else if strings.HasPrefix(source, "file,") {
833 c.ninjaWeightListSource = EXTERNAL_FILE
834 filePath := strings.TrimPrefix(source, "file,")
835 err := validateNinjaWeightList(filePath)
836 if err != nil {
837 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
838 }
839 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
840 if err != nil {
841 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
842 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900843 } else {
844 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
845 }
Jihoon Kang2a929ad2023-06-08 19:02:07 +0000846 } else if arg == "--build-from-source-stub" {
847 c.buildFromSourceStub = true
Yu Liufa297642024-06-11 00:13:02 +0000848 } else if arg == "--incremental-build-actions" {
849 c.incrementalBuildActions = true
MarkDacekb96561e2022-12-02 04:34:43 +0000850 } else if strings.HasPrefix(arg, "--build-command=") {
851 buildCmd := strings.TrimPrefix(arg, "--build-command=")
852 // remove quotations
853 buildCmd = strings.TrimPrefix(buildCmd, "\"")
854 buildCmd = strings.TrimSuffix(buildCmd, "\"")
855 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacek6614d9c2022-12-07 21:57:38 +0000856 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
857 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
858 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
859 if err == nil {
860 c.buildStartedTime = val
861 } else {
862 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
863 }
MarkDacekf47e1422023-04-19 16:47:36 +0000864 } else if arg == "--ensure-allowlist-integrity" {
865 c.ensureAllowlistIntegrity = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700866 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700867 parseArgNum := func(def int) int {
868 if len(arg) > 2 {
869 p, err := strconv.ParseUint(arg[2:], 10, 31)
870 if err != nil {
871 ctx.Fatalf("Failed to parse %q: %v", arg, err)
872 }
873 return int(p)
874 } else if i+1 < len(args) {
875 p, err := strconv.ParseUint(args[i+1], 10, 31)
876 if err == nil {
877 i++
878 return int(p)
879 }
880 }
881 return def
882 }
883
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700884 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700885 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700886 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700887 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700888 } else {
889 ctx.Fatalln("Unknown option:", arg)
890 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700891 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700892 if k == "OUT_DIR" {
893 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
894 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700895 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700896 } else if arg == "dist" {
897 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200898 } else if arg == "json-module-graph" {
899 c.jsonModuleGraph = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200900 } else if arg == "queryview" {
901 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200902 } else if arg == "soong_docs" {
903 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700904 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700905 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800906 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700907 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700908 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700909 }
910 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700911}
912
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900913func validateNinjaWeightList(weightListFilePath string) (err error) {
914 data, err := os.ReadFile(weightListFilePath)
915 if err != nil {
916 return
917 }
918 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
919 for _, line := range lines {
920 fields := strings.Split(line, ",")
921 if len(fields) != 2 {
922 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
923 }
924 _, err = strconv.Atoi(fields[1])
925 if err != nil {
926 return
927 }
928 }
929 return
930}
931
Dan Willemsened869522018-01-08 14:58:46 -0800932func (c *configImpl) configureLocale(ctx Context) {
933 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
934 output, err := cmd.Output()
935
936 var locales []string
937 if err == nil {
938 locales = strings.Split(string(output), "\n")
939 } else {
940 // If we're unable to list the locales, let's assume en_US.UTF-8
941 locales = []string{"en_US.UTF-8"}
942 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
943 }
944
945 // gettext uses LANGUAGE, which is passed directly through
946
947 // For LANG and LC_*, only preserve the evaluated version of
948 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800949 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800950 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800951 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800952 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800953 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800954 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800955 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800956 }
957
958 c.environ.UnsetWithPrefix("LC_")
959
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800960 if userLang != "" {
961 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800962 }
963
964 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
965 // for others)
966 if inList("C.UTF-8", locales) {
967 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500968 } else if inList("C.utf8", locales) {
969 // These normalize to the same thing
970 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800971 } else if inList("en_US.UTF-8", locales) {
972 c.environ.Set("LANG", "en_US.UTF-8")
973 } else if inList("en_US.utf8", locales) {
974 // These normalize to the same thing
975 c.environ.Set("LANG", "en_US.UTF-8")
976 } else {
977 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
978 }
979}
980
Dan Willemsen1e704462016-08-21 15:17:17 -0700981func (c *configImpl) Environment() *Environment {
982 return c.environ
983}
984
985func (c *configImpl) Arguments() []string {
986 return c.arguments
987}
988
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200989func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200990 if len(c.Arguments()) > 0 {
991 // Explicit targets requested that are not special targets like b2pbuild
992 // or the JSON module graph
993 return true
994 }
995
Colin Cross8d411ff2023-12-07 10:31:24 -0800996 if !c.JsonModuleGraph() && !c.Queryview() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200997 // Command line was empty, the default Ninja target is built
998 return true
999 }
1000
Colin Cross8d411ff2023-12-07 10:31:24 -08001001 if c.Dist() {
Liz Kammer88677422021-12-15 15:03:19 -05001002 return true
1003 }
1004
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001005 // build.ninja doesn't need to be generated
1006 return false
1007}
1008
Dan Willemsen1e704462016-08-21 15:17:17 -07001009func (c *configImpl) OutDir() string {
1010 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -07001011 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -07001012 }
1013 return "out"
1014}
1015
Dan Willemsen8a073a82017-02-04 17:30:44 -08001016func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -04001017 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001018}
1019
1020func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001021 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001022}
1023
Dan Willemsen1e704462016-08-21 15:17:17 -07001024func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001025 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001026 return c.arguments
1027 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001028 return c.ninjaArgs
1029}
1030
1031func (c *configImpl) SoongOutDir() string {
1032 return filepath.Join(c.OutDir(), "soong")
1033}
1034
Spandan Das394aa322022-11-03 17:02:10 +00001035func (c *configImpl) ApiSurfacesOutDir() string {
1036 return filepath.Join(c.OutDir(), "api_surfaces")
1037}
1038
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001039func (c *configImpl) PrebuiltOS() string {
1040 switch runtime.GOOS {
1041 case "linux":
1042 return "linux-x86"
1043 case "darwin":
1044 return "darwin-x86"
1045 default:
1046 panic("Unknown GOOS")
1047 }
1048}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001049
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001050func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001051 if c.SkipKatiNinja() {
1052 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1053 } else {
1054 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1055 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001056}
1057
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001058func (c *configImpl) UsedEnvFile(tag string) string {
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001059 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
Qing Shen713c5422024-08-23 04:09:18 +00001060 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+c.CoverageSuffix()+"."+tag)
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001061 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001062 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1063}
1064
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001065func (c *configImpl) SoongDocsHtml() string {
1066 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1067}
1068
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001069func (c *configImpl) QueryviewMarkerFile() string {
1070 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1071}
1072
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001073func (c *configImpl) ModuleGraphFile() string {
1074 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1075}
1076
kgui67007242022-01-25 13:50:25 +08001077func (c *configImpl) ModuleActionsFile() string {
1078 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1079}
1080
Jeff Gastonefc1b412017-03-29 17:29:06 -07001081func (c *configImpl) TempDir() string {
1082 return shared.TempDirForOutDir(c.SoongOutDir())
1083}
1084
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001085func (c *configImpl) FileListDir() string {
1086 return filepath.Join(c.OutDir(), ".module_paths")
1087}
1088
Dan Willemsen1e704462016-08-21 15:17:17 -07001089func (c *configImpl) KatiSuffix() string {
1090 if c.katiSuffix != "" {
1091 return c.katiSuffix
1092 }
1093 panic("SetKatiSuffix has not been called")
1094}
1095
Colin Cross37193492017-11-16 17:55:00 -08001096// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1097// user is interested in additional checks at the expense of build time.
1098func (c *configImpl) Checkbuild() bool {
1099 return c.checkbuild
1100}
1101
Dan Willemsen8a073a82017-02-04 17:30:44 -08001102func (c *configImpl) Dist() bool {
1103 return c.dist
1104}
1105
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001106func (c *configImpl) JsonModuleGraph() bool {
1107 return c.jsonModuleGraph
1108}
1109
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001110func (c *configImpl) Queryview() bool {
1111 return c.queryview
1112}
1113
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001114func (c *configImpl) SoongDocs() bool {
1115 return c.soongDocs
1116}
1117
Dan Willemsen1e704462016-08-21 15:17:17 -07001118func (c *configImpl) IsVerbose() bool {
1119 return c.verbose
1120}
1121
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001122func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1123 return c.ninjaWeightListSource
1124}
1125
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001126func (c *configImpl) SkipKati() bool {
1127 return c.skipKati
1128}
1129
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001130func (c *configImpl) SkipKatiNinja() bool {
1131 return c.skipKatiNinja
1132}
1133
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001134func (c *configImpl) SkipSoong() bool {
1135 return c.skipSoong
1136}
1137
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001138func (c *configImpl) SkipNinja() bool {
1139 return c.skipNinja
1140}
1141
Anton Hansson5a7861a2021-06-04 10:09:01 +01001142func (c *configImpl) SetSkipNinja(v bool) {
1143 c.skipNinja = v
1144}
1145
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001146func (c *configImpl) SkipConfig() bool {
1147 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001148}
1149
Jihoon Kang1bff0342023-01-17 20:40:22 +00001150func (c *configImpl) BuildFromTextStub() bool {
Jihoon Kang2a929ad2023-06-08 19:02:07 +00001151 return !c.buildFromSourceStub
Jihoon Kang1bff0342023-01-17 20:40:22 +00001152}
1153
Dan Willemsen1e704462016-08-21 15:17:17 -07001154func (c *configImpl) TargetProduct() string {
1155 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1156 return v
1157 }
1158 panic("TARGET_PRODUCT is not defined")
1159}
1160
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001161func (c *configImpl) TargetProductOrErr() (string, error) {
1162 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1163 return v, nil
1164 }
1165 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1166}
1167
Qing Shen713c5422024-08-23 04:09:18 +00001168func (c *configImpl) CoverageSuffix() string {
1169 if v := c.environ.IsEnvTrue("EMMA_INSTRUMENT"); v {
1170 return ".coverage"
1171 }
1172 return ""
1173}
1174
Dan Willemsen02781d52017-05-12 19:28:13 -07001175func (c *configImpl) TargetDevice() string {
1176 return c.targetDevice
1177}
1178
1179func (c *configImpl) SetTargetDevice(device string) {
1180 c.targetDevice = device
1181}
1182
1183func (c *configImpl) TargetBuildVariant() string {
1184 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1185 return v
1186 }
1187 panic("TARGET_BUILD_VARIANT is not defined")
1188}
1189
Dan Willemsen1e704462016-08-21 15:17:17 -07001190func (c *configImpl) KatiArgs() []string {
1191 return c.katiArgs
1192}
1193
1194func (c *configImpl) Parallel() int {
1195 return c.parallel
1196}
1197
Sam Delmerico98a73292023-02-21 11:50:29 -05001198func (c *configImpl) GetSourceRootDirs() []string {
1199 return c.sourceRootDirs
1200}
1201
1202func (c *configImpl) SetSourceRootDirs(i []string) {
1203 c.sourceRootDirs = i
1204}
1205
MarkDacek6614d9c2022-12-07 21:57:38 +00001206func (c *configImpl) GetLogsPrefix() string {
1207 return c.logsPrefix
1208}
1209
1210func (c *configImpl) SetLogsPrefix(prefix string) {
1211 c.logsPrefix = prefix
1212}
1213
Colin Cross8b8bec32019-11-15 13:18:43 -08001214func (c *configImpl) HighmemParallel() int {
1215 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1216 return i
1217 }
1218
1219 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1220 parallel := c.Parallel()
1221 if c.UseRemoteBuild() {
1222 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1223 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1224 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1225 // Return 1/16th of the size of the local pool, rounding up.
1226 return (parallel + 15) / 16
1227 } else if c.totalRAM == 0 {
1228 // Couldn't detect the total RAM, don't restrict highmem processes.
1229 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001230 } else if c.totalRAM <= 16*1024*1024*1024 {
1231 // Less than 16GB of ram, restrict to 1 highmem processes
1232 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001233 } else if c.totalRAM <= 32*1024*1024*1024 {
1234 // Less than 32GB of ram, restrict to 2 highmem processes
1235 return 2
1236 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1237 // If less than 8GB total RAM per process, reduce the number of highmem processes
1238 return p
1239 }
1240 // No restriction on highmem processes
1241 return parallel
1242}
1243
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001244func (c *configImpl) TotalRAM() uint64 {
1245 return c.totalRAM
1246}
1247
Kousik Kumarec478642020-09-21 13:39:24 -04001248// ForceUseGoma determines whether we should override Goma deprecation
1249// and use Goma for the current build or not.
1250func (c *configImpl) ForceUseGoma() bool {
1251 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1252 v = strings.TrimSpace(v)
1253 if v != "" && v != "false" {
1254 return true
1255 }
1256 }
1257 return false
1258}
1259
Dan Willemsen1e704462016-08-21 15:17:17 -07001260func (c *configImpl) UseGoma() bool {
1261 if v, ok := c.environ.Get("USE_GOMA"); ok {
1262 v = strings.TrimSpace(v)
1263 if v != "" && v != "false" {
1264 return true
1265 }
1266 }
1267 return false
1268}
1269
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001270func (c *configImpl) StartGoma() bool {
1271 if !c.UseGoma() {
1272 return false
1273 }
1274
1275 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1276 v = strings.TrimSpace(v)
1277 if v != "" && v != "false" {
1278 return false
1279 }
1280 }
1281 return true
1282}
1283
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001284func (c *configImpl) canSupportRBE() bool {
Joe Onorato86f50e72024-06-24 14:28:25 -07001285 // Only supported on linux
1286 if runtime.GOOS != "linux" {
1287 return false
1288 }
1289
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001290 // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since
1291 // its unlikely that we will be able to obtain necessary creds without stubby.
1292 authType, _ := c.rbeAuth()
1293 if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") {
1294 return false
1295 }
Taylor Santiago3c16e612024-05-30 14:41:31 -07001296 if c.UseABFS() {
1297 return false
1298 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001299 return true
1300}
1301
Taylor Santiago3c16e612024-05-30 14:41:31 -07001302func (c *configImpl) UseABFS() bool {
1303 if v, ok := c.environ.Get("NO_ABFS"); ok {
1304 v = strings.ToLower(strings.TrimSpace(v))
1305 if v == "true" || v == "1" {
1306 return false
1307 }
1308 }
1309
1310 abfsBox := c.PrebuiltBuildTool("abfsbox")
1311 err := exec.Command(abfsBox, "hash", srcDirFileCheck).Run()
1312 return err == nil
1313}
1314
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001315func (c *configImpl) sandboxPath(base, in string) string {
1316 if !c.UseABFS() {
1317 return in
1318 }
1319
1320 rel, err := filepath.Rel(base, in)
1321 if err != nil {
1322 return in
1323 }
1324
1325 return filepath.Join(abfsSrcDir, rel)
1326}
1327
Ramy Medhatbbf25672019-07-17 12:30:04 +00001328func (c *configImpl) UseRBE() bool {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001329 // These alternate modes of running Soong do not use RBE / reclient.
Colin Cross8d411ff2023-12-07 10:31:24 -08001330 if c.Queryview() || c.JsonModuleGraph() {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001331 return false
1332 }
1333
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001334 if !c.canSupportRBE() {
Kousik Kumar67ad4342023-06-06 15:09:27 -04001335 return false
1336 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001337
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001338 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001339 v = strings.TrimSpace(v)
1340 if v != "" && v != "false" {
1341 return true
1342 }
1343 }
1344 return false
1345}
1346
1347func (c *configImpl) StartRBE() bool {
1348 if !c.UseRBE() {
1349 return false
1350 }
1351
1352 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1353 v = strings.TrimSpace(v)
1354 if v != "" && v != "false" {
1355 return false
1356 }
1357 }
1358 return true
1359}
1360
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001361func (c *configImpl) rbeProxyLogsDir() string {
1362 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001363 if v, ok := c.environ.Get(f); ok {
1364 return v
1365 }
1366 }
Ramy Medhatbc061762023-10-10 18:36:59 +00001367 return c.rbeTmpDir()
1368}
1369
1370func (c *configImpl) rbeDownloadTmpDir() string {
Cole Faust06ea5312023-10-18 17:38:40 -07001371 for _, f := range []string{"RBE_download_tmp_dir", "FLAG_download_tmp_dir"} {
Ramy Medhatbc061762023-10-10 18:36:59 +00001372 if v, ok := c.environ.Get(f); ok {
1373 return v
1374 }
1375 }
1376 return c.rbeTmpDir()
1377}
1378
1379func (c *configImpl) rbeTmpDir() string {
Yaowen Meid4da2662024-07-24 08:25:12 +00001380 return filepath.Join(c.SoongOutDir(), "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001381}
1382
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001383func (c *configImpl) rbeCacheDir() string {
1384 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1385 if v, ok := c.environ.Get(f); ok {
1386 return v
1387 }
1388 }
1389 return shared.JoinPath(c.SoongOutDir(), "rbe")
1390}
1391
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001392func (c *configImpl) shouldCleanupRBELogsDir() bool {
1393 // Perform a log directory cleanup only when the log directory
1394 // is auto created by the build rather than user-specified.
1395 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Yaowen Meid9108d22024-08-29 16:56:32 +00001396 if v, ok := c.environ.Get(f); ok {
1397 if v != c.rbeTmpDir() {
1398 return false
1399 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001400 }
1401 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001402 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001403}
1404
1405func (c *configImpl) rbeExecRoot() string {
1406 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1407 if v, ok := c.environ.Get(f); ok {
1408 return v
1409 }
1410 }
1411 wd, err := os.Getwd()
1412 if err != nil {
1413 return ""
1414 }
1415 return wd
1416}
1417
1418func (c *configImpl) rbeDir() string {
1419 if v, ok := c.environ.Get("RBE_DIR"); ok {
1420 return v
1421 }
1422 return "prebuilts/remoteexecution-client/live/"
1423}
1424
1425func (c *configImpl) rbeReproxy() string {
1426 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1427 if v, ok := c.environ.Get(f); ok {
1428 return v
1429 }
1430 }
1431 return filepath.Join(c.rbeDir(), "reproxy")
1432}
1433
1434func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001435 credFlags := []string{
1436 "use_application_default_credentials",
1437 "use_gce_credentials",
1438 "credential_file",
1439 "use_google_prod_creds",
1440 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001441 for _, cf := range credFlags {
1442 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1443 if v, ok := c.environ.Get(f); ok {
1444 v = strings.TrimSpace(v)
1445 if v != "" && v != "false" && v != "0" {
1446 return "RBE_" + cf, v
1447 }
1448 }
1449 }
1450 }
1451 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001452}
1453
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001454func (c *configImpl) rbeSockAddr(dir string) (string, error) {
Andus Yuc917eb82024-03-06 21:54:15 +00001455 // Absolute path socket addresses have a prefix of //. This should
1456 // be included in the length limit.
1457 maxNameLen := len(syscall.RawSockaddrUnix{}.Path) - 2
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001458 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1459
1460 name := filepath.Join(dir, base)
1461 if len(name) < maxNameLen {
1462 return name, nil
1463 }
1464
1465 name = filepath.Join("/tmp", base)
1466 if len(name) < maxNameLen {
1467 return name, nil
1468 }
1469
1470 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1471}
1472
Kousik Kumar7bc78192022-04-27 14:52:56 -04001473// IsGooglerEnvironment returns true if the current build is running
1474// on a Google developer machine and false otherwise.
1475func (c *configImpl) IsGooglerEnvironment() bool {
1476 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1477 if v, ok := c.environ.Get(cf); ok {
1478 return v == "googler"
1479 }
1480 return false
1481}
1482
1483// GoogleProdCredsExist determine whether credentials exist on the
1484// Googler machine to use remote execution.
1485func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001486 if googleProdCredsExistCache {
1487 return googleProdCredsExistCache
1488 }
andusyu0b3dc032023-06-21 17:29:32 -04001489 if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001490 return false
1491 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001492 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001493 return true
1494}
1495
1496// UseRemoteBuild indicates whether to use a remote build acceleration system
1497// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001498func (c *configImpl) UseRemoteBuild() bool {
1499 return c.UseGoma() || c.UseRBE()
1500}
1501
Kousik Kumar7bc78192022-04-27 14:52:56 -04001502// StubbyExists checks whether the stubby binary exists on the machine running
1503// the build.
1504func (c *configImpl) StubbyExists() bool {
1505 if _, err := exec.LookPath("stubby"); err != nil {
1506 return false
1507 }
1508 return true
1509}
1510
Dan Willemsen1e704462016-08-21 15:17:17 -07001511// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001512// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001513// still limited by Parallel()
1514func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001515 if !c.UseRemoteBuild() {
1516 return 0
1517 }
1518 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1519 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001520 }
1521 return 500
1522}
1523
1524func (c *configImpl) SetKatiArgs(args []string) {
1525 c.katiArgs = args
1526}
1527
1528func (c *configImpl) SetNinjaArgs(args []string) {
1529 c.ninjaArgs = args
1530}
1531
1532func (c *configImpl) SetKatiSuffix(suffix string) {
1533 c.katiSuffix = suffix
1534}
1535
Dan Willemsene0879fc2017-08-04 15:06:27 -07001536func (c *configImpl) LastKatiSuffixFile() string {
1537 return filepath.Join(c.OutDir(), "last_kati_suffix")
1538}
1539
1540func (c *configImpl) HasKatiSuffix() bool {
1541 return c.katiSuffix != ""
1542}
1543
Dan Willemsen1e704462016-08-21 15:17:17 -07001544func (c *configImpl) KatiEnvFile() string {
1545 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1546}
1547
Dan Willemsen29971232018-09-26 14:58:30 -07001548func (c *configImpl) KatiBuildNinjaFile() string {
1549 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001550}
1551
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001552func (c *configImpl) KatiPackageNinjaFile() string {
1553 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1554}
1555
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001556func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001557 targetProduct, err := c.TargetProductOrErr()
1558 if err != nil {
1559 return filepath.Join(c.SoongOutDir(), "soong.variables")
1560 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001561 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".variables")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001562 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001563}
1564
Inseob Kim58c802f2024-06-11 10:59:00 +09001565func (c *configImpl) SoongExtraVarsFile() string {
1566 targetProduct, err := c.TargetProductOrErr()
1567 if err != nil {
1568 return filepath.Join(c.SoongOutDir(), "soong.extra.variables")
1569 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001570 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".extra.variables")
Inseob Kim58c802f2024-06-11 10:59:00 +09001571 }
1572}
1573
Dan Willemsen1e704462016-08-21 15:17:17 -07001574func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001575 targetProduct, err := c.TargetProductOrErr()
1576 if err != nil {
1577 return filepath.Join(c.SoongOutDir(), "build.ninja")
1578 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001579 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+c.CoverageSuffix()+".ninja")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001580 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001581}
1582
1583func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001584 if c.katiSuffix == "" {
1585 return filepath.Join(c.OutDir(), "combined.ninja")
1586 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001587 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1588}
1589
1590func (c *configImpl) SoongAndroidMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001591 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001592}
1593
1594func (c *configImpl) SoongMakeVarsMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001595 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001596}
1597
Colin Crossaa9a2732023-10-27 10:54:27 -07001598func (c *configImpl) SoongBuildMetrics() string {
Colin Crossb67b0612023-10-31 10:02:45 -07001599 return filepath.Join(c.LogsDir(), "soong_build_metrics.pb")
Colin Crossaa9a2732023-10-27 10:54:27 -07001600}
1601
Dan Willemsenf052f782017-05-18 15:29:04 -07001602func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001603 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001604}
1605
Dan Willemsen02781d52017-05-12 19:28:13 -07001606func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001607 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1608}
1609
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001610func (c *configImpl) KatiPackageMkDir() string {
1611 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1612}
1613
Dan Willemsenf052f782017-05-18 15:29:04 -07001614func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001615 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001616}
1617
1618func (c *configImpl) HostOut() string {
1619 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1620}
1621
1622// This probably needs to be multi-valued, so not exporting it for now
1623func (c *configImpl) hostCrossOut() string {
1624 if runtime.GOOS == "linux" {
1625 return filepath.Join(c.hostOutRoot(), "windows-x86")
1626 } else {
1627 return ""
1628 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001629}
1630
Dan Willemsen1e704462016-08-21 15:17:17 -07001631func (c *configImpl) HostPrebuiltTag() string {
1632 if runtime.GOOS == "linux" {
1633 return "linux-x86"
1634 } else if runtime.GOOS == "darwin" {
1635 return "darwin-x86"
1636 } else {
1637 panic("Unsupported OS")
1638 }
1639}
Dan Willemsenf173d592017-04-27 14:28:00 -07001640
Taylor Santiago3c16e612024-05-30 14:41:31 -07001641func (c *configImpl) KatiBin() string {
1642 binName := "ckati"
1643 if c.UseABFS() {
1644 binName = "ckati-wrap"
1645 }
1646
1647 return c.PrebuiltBuildTool(binName)
1648}
1649
1650func (c *configImpl) NinjaBin() string {
1651 binName := "ninja"
1652 if c.UseABFS() {
1653 binName = "ninjago"
1654 }
1655 return c.PrebuiltBuildTool(binName)
1656}
1657
Cole Faust4e58bba2024-08-22 14:27:03 -07001658func (c *configImpl) N2Bin() string {
1659 path := c.PrebuiltBuildTool("n2")
1660 // Use musl instead of glibc because glibc on the build server is old and has bugs
1661 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1662}
1663
LaMont Jonesece626c2024-09-03 11:19:31 -07001664func (c *configImpl) SisoBin() string {
1665 path := c.PrebuiltBuildTool("siso")
1666 // Use musl instead of glibc because glibc on the build server is old and has bugs
1667 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1668}
1669
Dan Willemsen8122bd52017-10-12 20:20:41 -07001670func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001671 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1672 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001673 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1674 if _, err := os.Stat(asan); err == nil {
1675 return asan
1676 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001677 }
1678 }
1679 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1680}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001681
1682func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1683 c.brokenDupRules = val
1684}
1685
1686func (c *configImpl) BuildBrokenDupRules() bool {
1687 return c.brokenDupRules
1688}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001689
Dan Willemsen25e6f092019-04-09 10:22:43 -07001690func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1691 c.brokenUsesNetwork = val
1692}
1693
1694func (c *configImpl) BuildBrokenUsesNetwork() bool {
1695 return c.brokenUsesNetwork
1696}
1697
Dan Willemsene3336352020-01-02 19:10:38 -08001698func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1699 c.brokenNinjaEnvVars = val
1700}
1701
1702func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1703 return c.brokenNinjaEnvVars
1704}
1705
Spandan Das28a6f192024-07-01 21:00:25 +00001706func (c *configImpl) SetBuildBrokenMissingOutputs(val bool) {
1707 c.brokenMissingOutputs = val
1708}
1709
1710func (c *configImpl) BuildBrokenMissingOutputs() bool {
1711 return c.brokenMissingOutputs
1712}
1713
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001714func (c *configImpl) SetTargetDeviceDir(dir string) {
1715 c.targetDeviceDir = dir
1716}
1717
1718func (c *configImpl) TargetDeviceDir() string {
1719 return c.targetDeviceDir
1720}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001721
Patrice Arruda219eef32020-06-01 17:29:30 +00001722func (c *configImpl) BuildDateTime() string {
1723 return c.buildDateTime
1724}
1725
1726func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001727 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001728}
Patrice Arruda83842d72020-12-08 19:42:08 +00001729
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001730// LogsDir returns the absolute path to the logs directory where build log and
1731// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001732// directory. If the argument dist is specified, the logs directory
1733// is <dist_dir>/logs.
1734func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001735 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001736 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001737 // 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 -05001738 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001739 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001740 absDir, err := filepath.Abs(dir)
1741 if err != nil {
1742 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1743 os.Exit(1)
1744 }
1745 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001746}
1747
Chris Parsons53f68ae2022-03-03 12:01:40 -05001748// MkFileMetrics returns the file path for make-related metrics.
1749func (c *configImpl) MkMetrics() string {
1750 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1751}
1752
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001753func (c *configImpl) SetEmptyNinjaFile(v bool) {
1754 c.emptyNinjaFile = v
1755}
1756
1757func (c *configImpl) EmptyNinjaFile() bool {
1758 return c.emptyNinjaFile
1759}
Yu Liu6e13b402021-07-27 14:29:06 -07001760
MarkDacekd0e7cd32022-12-02 22:22:40 +00001761func (c *configImpl) SkipMetricsUpload() bool {
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001762 // b/362625275 - Metrics upload sometimes prevents abfs unmount
1763 if c.UseABFS() {
1764 return true
1765 }
1766
MarkDacekd0e7cd32022-12-02 22:22:40 +00001767 return c.skipMetricsUpload
1768}
1769
MarkDacekf47e1422023-04-19 16:47:36 +00001770func (c *configImpl) EnsureAllowlistIntegrity() bool {
1771 return c.ensureAllowlistIntegrity
1772}
1773
MarkDacek6614d9c2022-12-07 21:57:38 +00001774// Returns a Time object if one was passed via a command-line flag.
1775// Otherwise returns the passed default.
1776func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1777 if c.buildStartedTime == 0 {
1778 return defaultTime
1779 }
1780 return time.UnixMilli(c.buildStartedTime)
1781}
1782
Yu Liu6e13b402021-07-27 14:29:06 -07001783func GetMetricsUploader(topDir string, env *Environment) string {
1784 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1785 metricsUploader := filepath.Join(topDir, p)
1786 if _, err := os.Stat(metricsUploader); err == nil {
1787 return metricsUploader
1788 }
1789 }
1790
1791 return ""
1792}