blob: e5c9a50121f62663f99acbd9dfaa1d3e922fb9d7 [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 Kumard93c67f2023-05-30 20:23:57 +000018 "context"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050019 "encoding/json"
Jeongik Chaa87506f2023-06-01 23:16:41 +090020 "errors"
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040021 "fmt"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050022 "io/ioutil"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040023 "math/rand"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080024 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050025 "os/exec"
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"
Kousik Kumarec478642020-09-21 13:39:24 -040034
Dan Willemsen4591b642021-05-24 14:24:12 -070035 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000036
37 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070038)
39
Kousik Kumar3ff037e2022-01-25 22:11:01 -050040const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050041 envConfigDir = "vendor/google/tools/soong_config"
42 jsonSuffix = "json"
Kousik Kumard93c67f2023-05-30 20:23:57 +000043
44 configFetcher = "vendor/google/tools/soong/expconfigfetcher"
Kousik Kumara3a2af62023-06-06 17:29:11 -040045 envConfigFetchTimeout = 20 * time.Second
Kousik Kumar3ff037e2022-01-25 22:11:01 -050046)
47
Kousik Kumar4c180ad2022-05-27 07:48:37 -040048var (
Kevin Dagostino096ab2f2023-03-03 19:47:17 +000049 rbeRandPrefix int
50 googleProdCredsExistCache bool
Kousik Kumar4c180ad2022-05-27 07:48:37 -040051)
52
53func init() {
54 rand.Seed(time.Now().UnixNano())
55 rbeRandPrefix = rand.Intn(1000)
56}
57
Dan Willemsen1e704462016-08-21 15:17:17 -070058type Config struct{ *configImpl }
59
60type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020061 // Some targets that are implemented in soong_build
62 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080063 arguments []string
64 goma bool
65 environ *Environment
66 distDir string
67 buildDateTime string
MarkDacek6614d9c2022-12-07 21:57:38 +000068 logsPrefix string
Dan Willemsen1e704462016-08-21 15:17:17 -070069
70 // From the arguments
MarkDacekf47e1422023-04-19 16:47:36 +000071 parallel int
72 keepGoing int
73 verbose bool
74 checkbuild bool
75 dist bool
76 jsonModuleGraph bool
77 apiBp2build bool // Generate BUILD files for Soong modules that contribute APIs
78 bp2build bool
79 queryview bool
80 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
81 soongDocs bool
82 multitreeBuild bool // This is a multitree build.
83 skipConfig bool
84 skipKati bool
85 skipKatiNinja bool
86 skipSoong bool
87 skipNinja bool
88 skipSoongTests bool
89 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
90 skipMetricsUpload bool
91 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
92 buildFromTextStub bool
MarkDacek396491e2023-06-14 19:41:18 +000093 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
94 bazelExitCode int32 // For b runs - necessary for updating NonZeroExit
95 besId string // For b runs, to identify the BuildEventService logs
Dan Willemsen1e704462016-08-21 15:17:17 -070096
97 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070098 katiArgs []string
99 ninjaArgs []string
100 katiSuffix string
101 targetDevice string
102 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +0000103 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -0700104
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800105 // Autodetected
106 totalRAM uint64
107
Dan Willemsene3336352020-01-02 19:10:38 -0800108 brokenDupRules bool
109 brokenUsesNetwork bool
110 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -0700111
112 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000113
MarkDacekb78465d2022-10-18 20:10:16 +0000114 bazelProdMode bool
MarkDacekb78465d2022-10-18 20:10:16 +0000115 bazelStagingMode bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000116
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700117 // Set by multiproduct_kati
118 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700119
120 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000121
122 bazelForceEnabledModules string
Spandan Dasc5763832022-11-08 18:42:16 +0000123
Sam Delmerico98a73292023-02-21 11:50:29 -0500124 includeTags []string
125 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900126
127 // Data source to write ninja weight list
128 ninjaWeightListSource NinjaWeightListSource
Dan Willemsen1e704462016-08-21 15:17:17 -0700129}
130
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900131type NinjaWeightListSource uint
132
133const (
134 // ninja doesn't use weight list.
135 NOT_USED NinjaWeightListSource = iota
136 // ninja uses weight list based on previous builds by ninja log
137 NINJA_LOG
138 // ninja thinks every task has the same weight.
139 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900140 // ninja uses an external custom weight list
141 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900142 // ninja uses a prioritized module list from Soong
143 HINT_FROM_SOONG
Jeongik Chaa87506f2023-06-01 23:16:41 +0900144 // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead.
145 // We can assume it is an incremental build if ninja log exists.
146 DEFAULT
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900147)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800148const srcDirFileCheck = "build/soong/root.bp"
149
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700150var buildFiles = []string{"Android.mk", "Android.bp"}
151
Patrice Arruda13848222019-04-22 17:12:02 -0700152type BuildAction uint
153
154const (
155 // Builds all of the modules and their dependencies of a specified directory, relative to the root
156 // directory of the source tree.
157 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
158
159 // Builds all of the modules and their dependencies of a list of specified directories. All specified
160 // directories are relative to the root directory of the source tree.
161 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700162
163 // Build a list of specified modules. If none was specified, simply build the whole source tree.
164 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700165)
166
167// checkTopDir validates that the current directory is at the root directory of the source tree.
168func checkTopDir(ctx Context) {
169 if _, err := os.Stat(srcDirFileCheck); err != nil {
170 if os.IsNotExist(err) {
171 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
172 }
173 ctx.Fatalln("Error verifying tree state:", err)
174 }
175}
176
Kousik Kumard93c67f2023-05-30 20:23:57 +0000177// fetchEnvConfig optionally fetches a configuration file that can then subsequently be
178// loaded into Soong environment to control certain aspects of build behavior (e.g., enabling RBE).
179// If a configuration file already exists on disk, the fetch is run in the background
180// so as to NOT block the rest of the build execution.
181func fetchEnvConfig(ctx Context, config *configImpl, envConfigName string) error {
182 configName := envConfigName + "." + jsonSuffix
183 expConfigFetcher := &smpb.ExpConfigFetcher{Filename: &configName}
184 defer func() {
185 ctx.Metrics.ExpConfigFetcher(expConfigFetcher)
186 }()
187 if !config.GoogleProdCredsExist() {
188 status := smpb.ExpConfigFetcher_MISSING_GCERT
189 expConfigFetcher.Status = &status
190 return nil
191 }
192
193 s, err := os.Stat(configFetcher)
194 if err != nil {
195 if os.IsNotExist(err) {
196 return nil
197 }
198 return err
199 }
200 if s.Mode()&0111 == 0 {
201 status := smpb.ExpConfigFetcher_ERROR
202 expConfigFetcher.Status = &status
203 return fmt.Errorf("configuration fetcher binary %v is not executable: %v", configFetcher, s.Mode())
204 }
205
206 configExists := false
207 outConfigFilePath := filepath.Join(config.OutDir(), configName)
208 if _, err := os.Stat(outConfigFilePath); err == nil {
209 configExists = true
210 }
211
212 tCtx, cancel := context.WithTimeout(ctx, envConfigFetchTimeout)
213 fetchStart := time.Now()
214 cmd := exec.CommandContext(tCtx, configFetcher, "-output_config_dir", config.OutDir(),
215 "-output_config_name", configName)
216 if err := cmd.Start(); err != nil {
217 status := smpb.ExpConfigFetcher_ERROR
218 expConfigFetcher.Status = &status
219 return err
220 }
221
222 fetchCfg := func() error {
223 if err := cmd.Wait(); err != nil {
224 status := smpb.ExpConfigFetcher_ERROR
225 expConfigFetcher.Status = &status
226 return err
227 }
228 fetchEnd := time.Now()
229 expConfigFetcher.Micros = proto.Uint64(uint64(fetchEnd.Sub(fetchStart).Microseconds()))
230 expConfigFetcher.Filename = proto.String(outConfigFilePath)
231
232 if _, err := os.Stat(outConfigFilePath); err != nil {
233 status := smpb.ExpConfigFetcher_NO_CONFIG
234 expConfigFetcher.Status = &status
235 return err
236 }
237 status := smpb.ExpConfigFetcher_CONFIG
238 expConfigFetcher.Status = &status
239 return nil
240 }
241
242 // If a config file does not exist, wait for the config file to be fetched. Otherwise
243 // fetch the config file in the background and return immediately.
244 if !configExists {
245 defer cancel()
246 return fetchCfg()
247 }
248
249 go func() {
250 defer cancel()
251 if err := fetchCfg(); err != nil {
252 ctx.Verbosef("Failed to fetch config file %v: %v\n", configName, err)
253 }
254 }()
255 return nil
256}
257
MarkDacek7901e582023-01-09 19:48:01 +0000258func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500259 if bc == "" {
260 return nil
261 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500262
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500263 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500264 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500265 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500266 envConfigDir,
267 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500268 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500269 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
270 envVarsJSON, err := ioutil.ReadFile(cfgFile)
271 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500272 continue
273 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500274 ctx.Verbosef("Loading config file %v\n", cfgFile)
275 var envVars map[string]map[string]string
276 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
277 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
278 continue
279 }
280 for k, v := range envVars["env"] {
281 if os.Getenv(k) != "" {
282 continue
283 }
284 config.environ.Set(k, v)
285 }
286 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
287 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500288 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500289
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500290 return nil
291}
292
Chris Parsonsb6e96902022-10-31 20:08:45 -0400293func defaultBazelProdMode(cfg *configImpl) bool {
MarkDacekd06db5d2022-11-29 00:47:59 +0000294 // Environment flag to disable Bazel for users which experience
Chris Parsonsb6e96902022-10-31 20:08:45 -0400295 // broken bazel-handled builds, or significant performance regressions.
296 if cfg.IsBazelMixedBuildForceDisabled() {
297 return false
298 }
299 // Darwin-host builds are currently untested with Bazel.
300 if runtime.GOOS == "darwin" {
301 return false
302 }
Chris Parsons035e03a2022-11-01 14:25:45 -0400303 return true
Chris Parsonsb6e96902022-10-31 20:08:45 -0400304}
305
MarkDacekd33c2fd2023-05-04 20:40:04 +0000306func UploadOnlyConfig(ctx Context, args ...string) Config {
MarkDacek6614d9c2022-12-07 21:57:38 +0000307 ret := &configImpl{
308 environ: OsEnvironment(),
309 sandboxConfig: &SandboxConfig{},
310 }
MarkDacekd33c2fd2023-05-04 20:40:04 +0000311 ret.parseArgs(ctx, args)
MarkDacek7901e582023-01-09 19:48:01 +0000312 srcDir := absPath(ctx, ".")
313 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
314 if err := loadEnvConfig(ctx, ret, bc); err != nil {
315 ctx.Fatalln("Failed to parse env config files: %v", err)
316 }
317 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
MarkDacek6614d9c2022-12-07 21:57:38 +0000318 return Config{ret}
319}
320
Dan Willemsen1e704462016-08-21 15:17:17 -0700321func NewConfig(ctx Context, args ...string) Config {
322 ret := &configImpl{
Jeongik Chaf2ecf762023-05-19 14:03:45 +0900323 environ: OsEnvironment(),
324 sandboxConfig: &SandboxConfig{},
Jeongik Chaa87506f2023-06-01 23:16:41 +0900325 ninjaWeightListSource: DEFAULT,
Dan Willemsen1e704462016-08-21 15:17:17 -0700326 }
327
Patrice Arruda90109172020-07-28 18:07:27 +0000328 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700329 ret.parallel = runtime.NumCPU() + 2
330 ret.keepGoing = 1
331
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800332 ret.totalRAM = detectTotalRAM(ctx)
Dan Willemsen9b587492017-07-10 22:13:00 -0700333 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900334
335 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
Jeongik Chaa87506f2023-06-01 23:16:41 +0900336 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always")
337 } else if ret.ninjaWeightListSource == DEFAULT {
338 defaultNinjaWeightListSource := NINJA_LOG
339 if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) {
340 ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead")
341 defaultNinjaWeightListSource = HINT_FROM_SOONG
342 } else {
343 ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG")
344 }
345 ret.ninjaWeightListSource = defaultNinjaWeightListSource
346 // soong_build generates ninja hint depending on ninja log existence.
347 // Set it "depend" to avoid soong re-run due to env variable change.
348 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend")
Jeongik Chae114e602023-03-19 00:12:39 +0900349 }
Jeongik Chaa87506f2023-06-01 23:16:41 +0900350
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800351 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700352 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
353 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
354 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800355 outDir := "out"
356 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
357 if wd, err := os.Getwd(); err != nil {
358 ctx.Fatalln("Failed to get working directory:", err)
359 } else {
360 outDir = filepath.Join(baseDir, filepath.Base(wd))
361 }
362 }
363 ret.environ.Set("OUT_DIR", outDir)
364 }
365
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500366 // loadEnvConfig needs to know what the OUT_DIR is, so it should
367 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000368 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
369
370 if bc != "" {
Kousik Kumard93c67f2023-05-30 20:23:57 +0000371 if err := fetchEnvConfig(ctx, ret, bc); err != nil {
372 ctx.Verbosef("Failed to fetch config file: %v\n", err)
373 }
Kousik Kumarc8818332023-01-16 16:33:05 +0000374 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000375 ctx.Fatalln("Failed to parse env config files: %v", err)
376 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +0000377 if !ret.canSupportRBE() {
378 // Explicitly set USE_RBE env variable to false when we cannot run
379 // an RBE build to avoid ninja local execution pool issues.
380 ret.environ.Set("USE_RBE", "false")
381 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500382 }
383
Dan Willemsen2d31a442018-10-20 21:33:41 -0700384 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
385 ret.distDir = filepath.Clean(distDir)
386 } else {
387 ret.distDir = filepath.Join(ret.OutDir(), "dist")
388 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700389
Spandan Das05063612021-06-25 01:39:04 +0000390 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
391 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
392 }
393
Dan Willemsen1e704462016-08-21 15:17:17 -0700394 ret.environ.Unset(
395 // We're already using it
396 "USE_SOONG_UI",
397
398 // We should never use GOROOT/GOPATH from the shell environment
399 "GOROOT",
400 "GOPATH",
401
402 // These should only come from Soong, not the environment.
403 "CLANG",
404 "CLANG_CXX",
405 "CCC_CC",
406 "CCC_CXX",
407
408 // Used by the goma compiler wrapper, but should only be set by
409 // gomacc
410 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800411
412 // We handle this above
413 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700414
Dan Willemsen2d31a442018-10-20 21:33:41 -0700415 // This is handled above too, and set for individual commands later
416 "DIST_DIR",
417
Dan Willemsen68a09852017-04-18 13:56:57 -0700418 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000419 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700420 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700421 "DISPLAY",
422 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000423 "JAVAC",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700424 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700425 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700426
427 // Drop make flags
428 "MAKEFLAGS",
429 "MAKELEVEL",
430 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700431
432 // Set in envsetup.sh, reset in makefiles
433 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700434
435 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
436 "ANDROID_BUILD_TOP",
437 "ANDROID_HOST_OUT",
438 "ANDROID_PRODUCT_OUT",
439 "ANDROID_HOST_OUT_TESTCASES",
440 "ANDROID_TARGET_OUT_TESTCASES",
441 "ANDROID_TOOLCHAIN",
442 "ANDROID_TOOLCHAIN_2ND_ARCH",
443 "ANDROID_DEV_SCRIPTS",
444 "ANDROID_EMULATOR_PREBUILTS",
445 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700446 )
447
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400448 if ret.UseGoma() || ret.ForceUseGoma() {
449 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
450 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400451 }
452
Dan Willemsen1e704462016-08-21 15:17:17 -0700453 // Tell python not to spam the source tree with .pyc files.
454 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
455
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400456 tmpDir := absPath(ctx, ret.TempDir())
457 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800458
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700459 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
460 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
461 "llvm-binutils-stable/llvm-symbolizer")
462 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
463
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800464 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700465 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800466
Yu Liu6e13b402021-07-27 14:29:06 -0700467 srcDir := absPath(ctx, ".")
468 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700469 ctx.Println("You are building in a directory whose absolute path contains a space character:")
470 ctx.Println()
471 ctx.Printf("%q\n", srcDir)
472 ctx.Println()
473 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700474 }
475
Yu Liu6e13b402021-07-27 14:29:06 -0700476 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
477
Dan Willemsendb8457c2017-05-12 16:38:17 -0700478 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700479 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
480 ctx.Println()
481 ctx.Printf("%q\n", outDir)
482 ctx.Println()
483 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700484 }
485
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000486 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700487 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
488 ctx.Println()
489 ctx.Printf("%q\n", distDir)
490 ctx.Println()
491 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700492 }
493
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700494 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000495 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
496 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100497 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Colin Cross59c1e6a2022-03-04 13:37:19 -0800498 java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700499 javaHome := func() string {
500 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
501 return override
502 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000503 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
504 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 11 toolchain is now the global default.")
Pete Gillin1f52e932019-10-09 17:10:08 +0100505 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000506 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
507 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 17 toolchain is now the global default.")
508 }
509 return java17Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700510 }()
511 absJavaHome := absPath(ctx, javaHome)
512
Dan Willemsened869522018-01-08 14:58:46 -0800513 ret.configureLocale(ctx)
514
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700515 newPath := []string{filepath.Join(absJavaHome, "bin")}
516 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
517 newPath = append(newPath, path)
518 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100519
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700520 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
521 ret.environ.Set("JAVA_HOME", absJavaHome)
522 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000523 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
524 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100525 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700526 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
527
LaMont Jones52a72432023-03-09 18:19:35 +0000528 if ret.MultitreeBuild() {
529 ret.environ.Set("MULTITREE_BUILD", "true")
530 }
531
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800532 outDir := ret.OutDir()
533 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800534 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800535 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800536 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800537 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800538 }
Colin Cross28f527c2019-11-26 16:19:04 -0800539
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800540 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
541
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400542 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400543 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400544 ret.environ.Set(k, v)
545 }
546 }
547
Jihoon Kang1bff0342023-01-17 20:40:22 +0000548 if ret.BuildFromTextStub() {
549 // TODO(b/271443071): support hidden api check for from-text stub build
550 ret.environ.Set("UNSAFE_DISABLE_HIDDENAPI_FLAGS", "true")
551 }
552
Patrice Arruda83842d72020-12-08 19:42:08 +0000553 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800554 if err := os.RemoveAll(bpd); err != nil {
555 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
556 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000557
Patrice Arruda96850362020-08-11 20:41:11 +0000558 c := Config{ret}
559 storeConfigMetrics(ctx, c)
560 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700561}
562
Patrice Arruda13848222019-04-22 17:12:02 -0700563// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
564// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700565func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
566 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700567}
568
Patrice Arruda96850362020-08-11 20:41:11 +0000569// storeConfigMetrics selects a set of configuration information and store in
570// the metrics system for further analysis.
571func storeConfigMetrics(ctx Context, config Config) {
572 if ctx.Metrics == nil {
573 return
574 }
575
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400576 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000577
578 s := &smpb.SystemResourceInfo{
579 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
580 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
581 }
582 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000583}
584
Jeongik Cha8d63d562023-03-17 03:52:13 +0900585func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900586 switch s {
587 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900588 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900589 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900590 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900591 case EXTERNAL_FILE:
592 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900593 case HINT_FROM_SOONG:
594 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900595 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900596 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900597 }
598}
599
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400600func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700601 c := &smpb.BuildConfig{
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -0400602 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
603 UseGoma: proto.Bool(config.UseGoma()),
604 UseRbe: proto.Bool(config.UseRBE()),
605 BazelMixedBuild: proto.Bool(config.BazelBuildEnabled()),
606 ForceDisableBazelMixedBuild: proto.Bool(config.IsBazelMixedBuildForceDisabled()),
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900607 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400608 }
Yu Liue737a992021-10-04 13:21:41 -0700609 c.Targets = append(c.Targets, config.arguments...)
610
611 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400612}
613
Patrice Arruda13848222019-04-22 17:12:02 -0700614// getConfigArgs processes the command arguments based on the build action and creates a set of new
615// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700616func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700617 // The next block of code verifies that the current directory is the root directory of the source
618 // tree. It then finds the relative path of dir based on the root directory of the source tree
619 // and verify that dir is inside of the source tree.
620 checkTopDir(ctx)
621 topDir, err := os.Getwd()
622 if err != nil {
623 ctx.Fatalf("Error retrieving top directory: %v", err)
624 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700625 dir, err = filepath.EvalSymlinks(dir)
626 if err != nil {
627 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
628 }
Patrice Arruda13848222019-04-22 17:12:02 -0700629 dir, err = filepath.Abs(dir)
630 if err != nil {
631 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
632 }
633 relDir, err := filepath.Rel(topDir, dir)
634 if err != nil {
635 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
636 }
637 // If there are ".." in the path, it's not in the source tree.
638 if strings.Contains(relDir, "..") {
639 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
640 }
641
642 configArgs := args[:]
643
644 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
645 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
646 targetNamePrefix := "MODULES-IN-"
647 if inList("GET-INSTALL-PATH", configArgs) {
648 targetNamePrefix = "GET-INSTALL-PATH-IN-"
649 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
650 }
651
Patrice Arruda13848222019-04-22 17:12:02 -0700652 var targets []string
653
654 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700655 case BUILD_MODULES:
656 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700657 case BUILD_MODULES_IN_A_DIRECTORY:
658 // If dir is the root source tree, all the modules are built of the source tree are built so
659 // no need to find the build file.
660 if topDir == dir {
661 break
662 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700663
Patrice Arruda13848222019-04-22 17:12:02 -0700664 buildFile := findBuildFile(ctx, relDir)
665 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700666 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700667 }
Patrice Arruda13848222019-04-22 17:12:02 -0700668 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
669 case BUILD_MODULES_IN_DIRECTORIES:
670 newConfigArgs, dirs := splitArgs(configArgs)
671 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700672 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700673 }
674
675 // Tidy only override all other specified targets.
676 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
677 if tidyOnly == "true" || tidyOnly == "1" {
678 configArgs = append(configArgs, "tidy_only")
679 } else {
680 configArgs = append(configArgs, targets...)
681 }
682
683 return configArgs
684}
685
686// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
687func convertToTarget(dir string, targetNamePrefix string) string {
688 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
689}
690
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700691// hasBuildFile returns true if dir contains an Android build file.
692func hasBuildFile(ctx Context, dir string) bool {
693 for _, buildFile := range buildFiles {
694 _, err := os.Stat(filepath.Join(dir, buildFile))
695 if err == nil {
696 return true
697 }
698 if !os.IsNotExist(err) {
699 ctx.Fatalf("Error retrieving the build file stats: %v", err)
700 }
701 }
702 return false
703}
704
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700705// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
706// in the current and any sub directory of dir. If a build file is not found, traverse the path
707// up by one directory and repeat again until either a build file is found or reached to the root
708// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
709// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700710func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700711 // If the string is empty or ".", assume it is top directory of the source tree.
712 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700713 return ""
714 }
715
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700716 found := false
717 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
718 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
719 if err != nil {
720 return err
721 }
722 if found {
723 return filepath.SkipDir
724 }
725 if info.IsDir() {
726 return nil
727 }
728 for _, buildFile := range buildFiles {
729 if info.Name() == buildFile {
730 found = true
731 return filepath.SkipDir
732 }
733 }
734 return nil
735 })
736 if err != nil {
737 ctx.Fatalf("Error finding Android build file: %v", err)
738 }
739
740 if found {
741 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700742 }
743 }
744
745 return ""
746}
747
748// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
749func splitArgs(args []string) (newArgs []string, dirs []string) {
750 specialArgs := map[string]bool{
751 "showcommands": true,
752 "snod": true,
753 "dist": true,
754 "checkbuild": true,
755 }
756
757 newArgs = []string{}
758 dirs = []string{}
759
760 for _, arg := range args {
761 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
762 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
763 newArgs = append(newArgs, arg)
764 continue
765 }
766
767 if _, ok := specialArgs[arg]; ok {
768 newArgs = append(newArgs, arg)
769 continue
770 }
771
772 dirs = append(dirs, arg)
773 }
774
775 return newArgs, dirs
776}
777
778// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
779// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
780// source root tree where the build action command was invoked. Each directory is validated if the
781// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700782func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700783 for _, dir := range dirs {
784 // The directory may have specified specific modules to build. ":" is the separator to separate
785 // the directory and the list of modules.
786 s := strings.Split(dir, ":")
787 l := len(s)
788 if l > 2 { // more than one ":" was specified.
789 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
790 }
791
792 dir = filepath.Join(relDir, s[0])
793 if _, err := os.Stat(dir); err != nil {
794 ctx.Fatalf("couldn't find directory %s", dir)
795 }
796
797 // Verify that if there are any targets specified after ":". Each target is separated by ",".
798 var newTargets []string
799 if l == 2 && s[1] != "" {
800 newTargets = strings.Split(s[1], ",")
801 if inList("", newTargets) {
802 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
803 }
804 }
805
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700806 // If there are specified targets to build in dir, an android build file must exist for the one
807 // shot build. For the non-targets case, find the appropriate build file and build all the
808 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700809 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700810 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700811 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
812 }
813 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700814 buildFile := findBuildFile(ctx, dir)
815 if buildFile == "" {
816 ctx.Fatalf("Build file not found for %s directory", dir)
817 }
818 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700819 }
820
Patrice Arruda13848222019-04-22 17:12:02 -0700821 targets = append(targets, newTargets...)
822 }
823
Dan Willemsence41e942019-07-29 23:39:30 -0700824 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700825}
826
Dan Willemsen9b587492017-07-10 22:13:00 -0700827func (c *configImpl) parseArgs(ctx Context, args []string) {
828 for i := 0; i < len(args); i++ {
829 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100830 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700831 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200832 } else if arg == "--empty-ninja-file" {
833 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100834 } else if arg == "--skip-ninja" {
835 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700836 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700837 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
838 // but it does run a Kati ninja file if the .kati_enabled marker file was created
839 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000840 c.skipConfig = true
841 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100842 } else if arg == "--soong-only" {
843 c.skipKati = true
844 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200845 } else if arg == "--config-only" {
846 c.skipKati = true
847 c.skipKatiNinja = true
848 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700849 } else if arg == "--skip-config" {
850 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700851 } else if arg == "--skip-soong-tests" {
852 c.skipSoongTests = true
MarkDacekd0e7cd32022-12-02 22:22:40 +0000853 } else if arg == "--skip-metrics-upload" {
854 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500855 } else if arg == "--mk-metrics" {
856 c.reportMkMetrics = true
LaMont Jones52a72432023-03-09 18:19:35 +0000857 } else if arg == "--multitree-build" {
858 c.multitreeBuild = true
Chris Parsonsef615e52022-08-18 22:04:11 -0400859 } else if arg == "--bazel-mode" {
860 c.bazelProdMode = true
MarkDacekb78465d2022-10-18 20:10:16 +0000861 } else if arg == "--bazel-mode-staging" {
862 c.bazelStagingMode = true
Spandan Das394aa322022-11-03 17:02:10 +0000863 } else if arg == "--search-api-dir" {
864 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900865 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
866 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
867 if source == "ninja_log" {
868 c.ninjaWeightListSource = NINJA_LOG
869 } else if source == "evenly_distributed" {
870 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
871 } else if source == "not_used" {
872 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900873 } else if source == "soong" {
874 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900875 } else if strings.HasPrefix(source, "file,") {
876 c.ninjaWeightListSource = EXTERNAL_FILE
877 filePath := strings.TrimPrefix(source, "file,")
878 err := validateNinjaWeightList(filePath)
879 if err != nil {
880 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
881 }
882 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
883 if err != nil {
884 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
885 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900886 } else {
887 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
888 }
Jihoon Kang1bff0342023-01-17 20:40:22 +0000889 } else if arg == "--build-from-text-stub" {
890 c.buildFromTextStub = true
MarkDacekb96561e2022-12-02 04:34:43 +0000891 } else if strings.HasPrefix(arg, "--build-command=") {
892 buildCmd := strings.TrimPrefix(arg, "--build-command=")
893 // remove quotations
894 buildCmd = strings.TrimPrefix(buildCmd, "\"")
895 buildCmd = strings.TrimSuffix(buildCmd, "\"")
896 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacekd06db5d2022-11-29 00:47:59 +0000897 } else if strings.HasPrefix(arg, "--bazel-force-enabled-modules=") {
898 c.bazelForceEnabledModules = strings.TrimPrefix(arg, "--bazel-force-enabled-modules=")
MarkDacek6614d9c2022-12-07 21:57:38 +0000899 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
900 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
901 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
902 if err == nil {
903 c.buildStartedTime = val
904 } else {
905 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
906 }
MarkDacekf47e1422023-04-19 16:47:36 +0000907 } else if arg == "--ensure-allowlist-integrity" {
908 c.ensureAllowlistIntegrity = true
MarkDacekd33c2fd2023-05-04 20:40:04 +0000909 } else if strings.HasPrefix(arg, "--bazel-exit-code=") {
910 bazelExitCodeStr := strings.TrimPrefix(arg, "--bazel-exit-code=")
911 val, err := strconv.Atoi(bazelExitCodeStr)
912 if err == nil {
913 c.bazelExitCode = int32(val)
914 } else {
915 ctx.Fatalf("Error parsing bazel-exit-code", err)
916 }
MarkDacek396491e2023-06-14 19:41:18 +0000917 } else if strings.HasPrefix(arg, "--bes-id=") {
918 c.besId = strings.TrimPrefix(arg, "--bes-id=")
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700919 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700920 parseArgNum := func(def int) int {
921 if len(arg) > 2 {
922 p, err := strconv.ParseUint(arg[2:], 10, 31)
923 if err != nil {
924 ctx.Fatalf("Failed to parse %q: %v", arg, err)
925 }
926 return int(p)
927 } else if i+1 < len(args) {
928 p, err := strconv.ParseUint(args[i+1], 10, 31)
929 if err == nil {
930 i++
931 return int(p)
932 }
933 }
934 return def
935 }
936
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700937 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700938 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700939 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700940 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700941 } else {
942 ctx.Fatalln("Unknown option:", arg)
943 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700944 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700945 if k == "OUT_DIR" {
946 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
947 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700948 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700949 } else if arg == "dist" {
950 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200951 } else if arg == "json-module-graph" {
952 c.jsonModuleGraph = true
953 } else if arg == "bp2build" {
954 c.bp2build = true
Spandan Das5af0bd32022-09-28 20:43:08 +0000955 } else if arg == "api_bp2build" {
956 c.apiBp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200957 } else if arg == "queryview" {
958 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200959 } else if arg == "soong_docs" {
960 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700961 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700962 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800963 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700964 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700965 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700966 }
967 }
Chris Parsons21f80272023-06-15 04:02:28 +0000968 if (!c.bazelProdMode) && (!c.bazelStagingMode) {
Chris Parsonsb6e96902022-10-31 20:08:45 -0400969 c.bazelProdMode = defaultBazelProdMode(c)
970 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700971}
972
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900973func validateNinjaWeightList(weightListFilePath string) (err error) {
974 data, err := os.ReadFile(weightListFilePath)
975 if err != nil {
976 return
977 }
978 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
979 for _, line := range lines {
980 fields := strings.Split(line, ",")
981 if len(fields) != 2 {
982 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
983 }
984 _, err = strconv.Atoi(fields[1])
985 if err != nil {
986 return
987 }
988 }
989 return
990}
991
Dan Willemsened869522018-01-08 14:58:46 -0800992func (c *configImpl) configureLocale(ctx Context) {
993 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
994 output, err := cmd.Output()
995
996 var locales []string
997 if err == nil {
998 locales = strings.Split(string(output), "\n")
999 } else {
1000 // If we're unable to list the locales, let's assume en_US.UTF-8
1001 locales = []string{"en_US.UTF-8"}
1002 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
1003 }
1004
1005 // gettext uses LANGUAGE, which is passed directly through
1006
1007 // For LANG and LC_*, only preserve the evaluated version of
1008 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001009 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -08001010 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001011 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -08001012 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001013 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -08001014 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001015 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -08001016 }
1017
1018 c.environ.UnsetWithPrefix("LC_")
1019
Jaewoong Jung18aefc12020-12-21 09:11:10 -08001020 if userLang != "" {
1021 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -08001022 }
1023
1024 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
1025 // for others)
1026 if inList("C.UTF-8", locales) {
1027 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -05001028 } else if inList("C.utf8", locales) {
1029 // These normalize to the same thing
1030 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -08001031 } else if inList("en_US.UTF-8", locales) {
1032 c.environ.Set("LANG", "en_US.UTF-8")
1033 } else if inList("en_US.utf8", locales) {
1034 // These normalize to the same thing
1035 c.environ.Set("LANG", "en_US.UTF-8")
1036 } else {
1037 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
1038 }
1039}
1040
Dan Willemsen1e704462016-08-21 15:17:17 -07001041func (c *configImpl) Environment() *Environment {
1042 return c.environ
1043}
1044
1045func (c *configImpl) Arguments() []string {
1046 return c.arguments
1047}
1048
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001049func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001050 if len(c.Arguments()) > 0 {
1051 // Explicit targets requested that are not special targets like b2pbuild
1052 // or the JSON module graph
1053 return true
1054 }
1055
Spandan Das5af0bd32022-09-28 20:43:08 +00001056 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() && !c.ApiBp2build() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001057 // Command line was empty, the default Ninja target is built
1058 return true
1059 }
1060
Liz Kammer88677422021-12-15 15:03:19 -05001061 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
1062 if c.Dist() && !c.Bp2Build() {
1063 return true
1064 }
1065
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001066 // build.ninja doesn't need to be generated
1067 return false
1068}
1069
Dan Willemsen1e704462016-08-21 15:17:17 -07001070func (c *configImpl) OutDir() string {
1071 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -07001072 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -07001073 }
1074 return "out"
1075}
1076
Dan Willemsen8a073a82017-02-04 17:30:44 -08001077func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -04001078 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001079}
1080
1081func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001082 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001083}
1084
Dan Willemsen1e704462016-08-21 15:17:17 -07001085func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001086 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001087 return c.arguments
1088 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001089 return c.ninjaArgs
1090}
1091
Jingwen Chen7c6089a2020-11-02 02:56:20 -05001092func (c *configImpl) BazelOutDir() string {
1093 return filepath.Join(c.OutDir(), "bazel")
1094}
1095
Liz Kammer2af5ea82022-11-11 14:21:03 -05001096func (c *configImpl) bazelOutputBase() string {
1097 return filepath.Join(c.BazelOutDir(), "output")
1098}
1099
Dan Willemsen1e704462016-08-21 15:17:17 -07001100func (c *configImpl) SoongOutDir() string {
1101 return filepath.Join(c.OutDir(), "soong")
1102}
1103
Spandan Das394aa322022-11-03 17:02:10 +00001104func (c *configImpl) ApiSurfacesOutDir() string {
1105 return filepath.Join(c.OutDir(), "api_surfaces")
1106}
1107
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001108func (c *configImpl) PrebuiltOS() string {
1109 switch runtime.GOOS {
1110 case "linux":
1111 return "linux-x86"
1112 case "darwin":
1113 return "darwin-x86"
1114 default:
1115 panic("Unknown GOOS")
1116 }
1117}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001118
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001119func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001120 if c.SkipKatiNinja() {
1121 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1122 } else {
1123 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1124 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001125}
1126
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001127func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001128 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001129}
1130
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001131func (c *configImpl) UsedEnvFile(tag string) string {
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001132 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1133 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+"."+tag)
1134 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001135 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1136}
1137
Lukacs T. Berkic541cd22022-10-26 07:26:50 +00001138func (c *configImpl) Bp2BuildFilesMarkerFile() string {
1139 return shared.JoinPath(c.SoongOutDir(), "bp2build_files_marker")
1140}
1141
1142func (c *configImpl) Bp2BuildWorkspaceMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001143 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +02001144}
1145
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001146func (c *configImpl) SoongDocsHtml() string {
1147 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1148}
1149
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001150func (c *configImpl) QueryviewMarkerFile() string {
1151 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1152}
1153
Spandan Das5af0bd32022-09-28 20:43:08 +00001154func (c *configImpl) ApiBp2buildMarkerFile() string {
1155 return shared.JoinPath(c.SoongOutDir(), "api_bp2build.marker")
1156}
1157
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001158func (c *configImpl) ModuleGraphFile() string {
1159 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1160}
1161
kgui67007242022-01-25 13:50:25 +08001162func (c *configImpl) ModuleActionsFile() string {
1163 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1164}
1165
Jeff Gastonefc1b412017-03-29 17:29:06 -07001166func (c *configImpl) TempDir() string {
1167 return shared.TempDirForOutDir(c.SoongOutDir())
1168}
1169
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001170func (c *configImpl) FileListDir() string {
1171 return filepath.Join(c.OutDir(), ".module_paths")
1172}
1173
Dan Willemsen1e704462016-08-21 15:17:17 -07001174func (c *configImpl) KatiSuffix() string {
1175 if c.katiSuffix != "" {
1176 return c.katiSuffix
1177 }
1178 panic("SetKatiSuffix has not been called")
1179}
1180
Colin Cross37193492017-11-16 17:55:00 -08001181// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1182// user is interested in additional checks at the expense of build time.
1183func (c *configImpl) Checkbuild() bool {
1184 return c.checkbuild
1185}
1186
Dan Willemsen8a073a82017-02-04 17:30:44 -08001187func (c *configImpl) Dist() bool {
1188 return c.dist
1189}
1190
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001191func (c *configImpl) JsonModuleGraph() bool {
1192 return c.jsonModuleGraph
1193}
1194
1195func (c *configImpl) Bp2Build() bool {
1196 return c.bp2build
1197}
1198
Spandan Das5af0bd32022-09-28 20:43:08 +00001199func (c *configImpl) ApiBp2build() bool {
1200 return c.apiBp2build
1201}
1202
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001203func (c *configImpl) Queryview() bool {
1204 return c.queryview
1205}
1206
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001207func (c *configImpl) SoongDocs() bool {
1208 return c.soongDocs
1209}
1210
Dan Willemsen1e704462016-08-21 15:17:17 -07001211func (c *configImpl) IsVerbose() bool {
1212 return c.verbose
1213}
1214
LaMont Jones52a72432023-03-09 18:19:35 +00001215func (c *configImpl) MultitreeBuild() bool {
1216 return c.multitreeBuild
1217}
1218
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001219func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1220 return c.ninjaWeightListSource
1221}
1222
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001223func (c *configImpl) SkipKati() bool {
1224 return c.skipKati
1225}
1226
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001227func (c *configImpl) SkipKatiNinja() bool {
1228 return c.skipKatiNinja
1229}
1230
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001231func (c *configImpl) SkipSoong() bool {
1232 return c.skipSoong
1233}
1234
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001235func (c *configImpl) SkipNinja() bool {
1236 return c.skipNinja
1237}
1238
Anton Hansson5a7861a2021-06-04 10:09:01 +01001239func (c *configImpl) SetSkipNinja(v bool) {
1240 c.skipNinja = v
1241}
1242
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001243func (c *configImpl) SkipConfig() bool {
1244 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001245}
1246
Jihoon Kang1bff0342023-01-17 20:40:22 +00001247func (c *configImpl) BuildFromTextStub() bool {
1248 return c.buildFromTextStub
1249}
1250
Dan Willemsen1e704462016-08-21 15:17:17 -07001251func (c *configImpl) TargetProduct() string {
1252 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1253 return v
1254 }
1255 panic("TARGET_PRODUCT is not defined")
1256}
1257
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001258func (c *configImpl) TargetProductOrErr() (string, error) {
1259 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1260 return v, nil
1261 }
1262 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1263}
1264
Dan Willemsen02781d52017-05-12 19:28:13 -07001265func (c *configImpl) TargetDevice() string {
1266 return c.targetDevice
1267}
1268
1269func (c *configImpl) SetTargetDevice(device string) {
1270 c.targetDevice = device
1271}
1272
1273func (c *configImpl) TargetBuildVariant() string {
1274 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1275 return v
1276 }
1277 panic("TARGET_BUILD_VARIANT is not defined")
1278}
1279
Dan Willemsen1e704462016-08-21 15:17:17 -07001280func (c *configImpl) KatiArgs() []string {
1281 return c.katiArgs
1282}
1283
1284func (c *configImpl) Parallel() int {
1285 return c.parallel
1286}
1287
Sam Delmerico98a73292023-02-21 11:50:29 -05001288func (c *configImpl) GetSourceRootDirs() []string {
1289 return c.sourceRootDirs
1290}
1291
1292func (c *configImpl) SetSourceRootDirs(i []string) {
1293 c.sourceRootDirs = i
1294}
1295
Spandan Dasc5763832022-11-08 18:42:16 +00001296func (c *configImpl) GetIncludeTags() []string {
1297 return c.includeTags
1298}
1299
1300func (c *configImpl) SetIncludeTags(i []string) {
1301 c.includeTags = i
1302}
1303
MarkDacek6614d9c2022-12-07 21:57:38 +00001304func (c *configImpl) GetLogsPrefix() string {
1305 return c.logsPrefix
1306}
1307
1308func (c *configImpl) SetLogsPrefix(prefix string) {
1309 c.logsPrefix = prefix
1310}
1311
Colin Cross8b8bec32019-11-15 13:18:43 -08001312func (c *configImpl) HighmemParallel() int {
1313 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1314 return i
1315 }
1316
1317 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1318 parallel := c.Parallel()
1319 if c.UseRemoteBuild() {
1320 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1321 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1322 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1323 // Return 1/16th of the size of the local pool, rounding up.
1324 return (parallel + 15) / 16
1325 } else if c.totalRAM == 0 {
1326 // Couldn't detect the total RAM, don't restrict highmem processes.
1327 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001328 } else if c.totalRAM <= 16*1024*1024*1024 {
1329 // Less than 16GB of ram, restrict to 1 highmem processes
1330 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001331 } else if c.totalRAM <= 32*1024*1024*1024 {
1332 // Less than 32GB of ram, restrict to 2 highmem processes
1333 return 2
1334 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1335 // If less than 8GB total RAM per process, reduce the number of highmem processes
1336 return p
1337 }
1338 // No restriction on highmem processes
1339 return parallel
1340}
1341
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001342func (c *configImpl) TotalRAM() uint64 {
1343 return c.totalRAM
1344}
1345
Kousik Kumarec478642020-09-21 13:39:24 -04001346// ForceUseGoma determines whether we should override Goma deprecation
1347// and use Goma for the current build or not.
1348func (c *configImpl) ForceUseGoma() bool {
1349 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1350 v = strings.TrimSpace(v)
1351 if v != "" && v != "false" {
1352 return true
1353 }
1354 }
1355 return false
1356}
1357
Dan Willemsen1e704462016-08-21 15:17:17 -07001358func (c *configImpl) UseGoma() bool {
1359 if v, ok := c.environ.Get("USE_GOMA"); ok {
1360 v = strings.TrimSpace(v)
1361 if v != "" && v != "false" {
1362 return true
1363 }
1364 }
1365 return false
1366}
1367
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001368func (c *configImpl) StartGoma() bool {
1369 if !c.UseGoma() {
1370 return false
1371 }
1372
1373 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1374 v = strings.TrimSpace(v)
1375 if v != "" && v != "false" {
1376 return false
1377 }
1378 }
1379 return true
1380}
1381
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001382func (c *configImpl) canSupportRBE() bool {
1383 // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since
1384 // its unlikely that we will be able to obtain necessary creds without stubby.
1385 authType, _ := c.rbeAuth()
1386 if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") {
1387 return false
1388 }
1389 return true
1390}
1391
Ramy Medhatbbf25672019-07-17 12:30:04 +00001392func (c *configImpl) UseRBE() bool {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001393 // These alternate modes of running Soong do not use RBE / reclient.
1394 if c.Bp2Build() || c.Queryview() || c.ApiBp2build() || c.JsonModuleGraph() {
1395 return false
1396 }
1397
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001398 if !c.canSupportRBE() {
Kousik Kumar67ad4342023-06-06 15:09:27 -04001399 return false
1400 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001401
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001402 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001403 v = strings.TrimSpace(v)
1404 if v != "" && v != "false" {
1405 return true
1406 }
1407 }
1408 return false
1409}
1410
Chris Parsonsef615e52022-08-18 22:04:11 -04001411func (c *configImpl) BazelBuildEnabled() bool {
Chris Parsons21f80272023-06-15 04:02:28 +00001412 return c.bazelProdMode || c.bazelStagingMode
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001413}
1414
Ramy Medhatbbf25672019-07-17 12:30:04 +00001415func (c *configImpl) StartRBE() bool {
1416 if !c.UseRBE() {
1417 return false
1418 }
1419
1420 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1421 v = strings.TrimSpace(v)
1422 if v != "" && v != "false" {
1423 return false
1424 }
1425 }
1426 return true
1427}
1428
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001429func (c *configImpl) rbeProxyLogsDir() string {
1430 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001431 if v, ok := c.environ.Get(f); ok {
1432 return v
1433 }
1434 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001435 buildTmpDir := shared.TempDirForOutDir(c.SoongOutDir())
1436 return filepath.Join(buildTmpDir, "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001437}
1438
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001439func (c *configImpl) rbeCacheDir() string {
1440 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1441 if v, ok := c.environ.Get(f); ok {
1442 return v
1443 }
1444 }
1445 return shared.JoinPath(c.SoongOutDir(), "rbe")
1446}
1447
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001448func (c *configImpl) shouldCleanupRBELogsDir() bool {
1449 // Perform a log directory cleanup only when the log directory
1450 // is auto created by the build rather than user-specified.
1451 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
1452 if _, ok := c.environ.Get(f); ok {
1453 return false
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001454 }
1455 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001456 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001457}
1458
1459func (c *configImpl) rbeExecRoot() string {
1460 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1461 if v, ok := c.environ.Get(f); ok {
1462 return v
1463 }
1464 }
1465 wd, err := os.Getwd()
1466 if err != nil {
1467 return ""
1468 }
1469 return wd
1470}
1471
1472func (c *configImpl) rbeDir() string {
1473 if v, ok := c.environ.Get("RBE_DIR"); ok {
1474 return v
1475 }
1476 return "prebuilts/remoteexecution-client/live/"
1477}
1478
1479func (c *configImpl) rbeReproxy() string {
1480 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1481 if v, ok := c.environ.Get(f); ok {
1482 return v
1483 }
1484 }
1485 return filepath.Join(c.rbeDir(), "reproxy")
1486}
1487
1488func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001489 credFlags := []string{
1490 "use_application_default_credentials",
1491 "use_gce_credentials",
1492 "credential_file",
1493 "use_google_prod_creds",
1494 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001495 for _, cf := range credFlags {
1496 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1497 if v, ok := c.environ.Get(f); ok {
1498 v = strings.TrimSpace(v)
1499 if v != "" && v != "false" && v != "0" {
1500 return "RBE_" + cf, v
1501 }
1502 }
1503 }
1504 }
1505 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001506}
1507
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001508func (c *configImpl) rbeSockAddr(dir string) (string, error) {
1509 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
1510 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1511
1512 name := filepath.Join(dir, base)
1513 if len(name) < maxNameLen {
1514 return name, nil
1515 }
1516
1517 name = filepath.Join("/tmp", base)
1518 if len(name) < maxNameLen {
1519 return name, nil
1520 }
1521
1522 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1523}
1524
Kousik Kumar7bc78192022-04-27 14:52:56 -04001525// IsGooglerEnvironment returns true if the current build is running
1526// on a Google developer machine and false otherwise.
1527func (c *configImpl) IsGooglerEnvironment() bool {
1528 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1529 if v, ok := c.environ.Get(cf); ok {
1530 return v == "googler"
1531 }
1532 return false
1533}
1534
1535// GoogleProdCredsExist determine whether credentials exist on the
1536// Googler machine to use remote execution.
1537func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001538 if googleProdCredsExistCache {
1539 return googleProdCredsExistCache
1540 }
andusyu0b3dc032023-06-21 17:29:32 -04001541 if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001542 return false
1543 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001544 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001545 return true
1546}
1547
1548// UseRemoteBuild indicates whether to use a remote build acceleration system
1549// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001550func (c *configImpl) UseRemoteBuild() bool {
1551 return c.UseGoma() || c.UseRBE()
1552}
1553
Kousik Kumar7bc78192022-04-27 14:52:56 -04001554// StubbyExists checks whether the stubby binary exists on the machine running
1555// the build.
1556func (c *configImpl) StubbyExists() bool {
1557 if _, err := exec.LookPath("stubby"); err != nil {
1558 return false
1559 }
1560 return true
1561}
1562
Dan Willemsen1e704462016-08-21 15:17:17 -07001563// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001564// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001565// still limited by Parallel()
1566func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001567 if !c.UseRemoteBuild() {
1568 return 0
1569 }
1570 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1571 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001572 }
1573 return 500
1574}
1575
1576func (c *configImpl) SetKatiArgs(args []string) {
1577 c.katiArgs = args
1578}
1579
1580func (c *configImpl) SetNinjaArgs(args []string) {
1581 c.ninjaArgs = args
1582}
1583
1584func (c *configImpl) SetKatiSuffix(suffix string) {
1585 c.katiSuffix = suffix
1586}
1587
Dan Willemsene0879fc2017-08-04 15:06:27 -07001588func (c *configImpl) LastKatiSuffixFile() string {
1589 return filepath.Join(c.OutDir(), "last_kati_suffix")
1590}
1591
1592func (c *configImpl) HasKatiSuffix() bool {
1593 return c.katiSuffix != ""
1594}
1595
Dan Willemsen1e704462016-08-21 15:17:17 -07001596func (c *configImpl) KatiEnvFile() string {
1597 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1598}
1599
Dan Willemsen29971232018-09-26 14:58:30 -07001600func (c *configImpl) KatiBuildNinjaFile() string {
1601 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001602}
1603
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001604func (c *configImpl) KatiPackageNinjaFile() string {
1605 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1606}
1607
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001608func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001609 targetProduct, err := c.TargetProductOrErr()
1610 if err != nil {
1611 return filepath.Join(c.SoongOutDir(), "soong.variables")
1612 } else {
1613 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+".variables")
1614 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001615}
1616
Dan Willemsen1e704462016-08-21 15:17:17 -07001617func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001618 targetProduct, err := c.TargetProductOrErr()
1619 if err != nil {
1620 return filepath.Join(c.SoongOutDir(), "build.ninja")
1621 } else {
1622 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+".ninja")
1623 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001624}
1625
1626func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001627 if c.katiSuffix == "" {
1628 return filepath.Join(c.OutDir(), "combined.ninja")
1629 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001630 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1631}
1632
1633func (c *configImpl) SoongAndroidMk() string {
1634 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1635}
1636
1637func (c *configImpl) SoongMakeVarsMk() string {
1638 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1639}
1640
Dan Willemsenf052f782017-05-18 15:29:04 -07001641func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001642 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001643}
1644
Dan Willemsen02781d52017-05-12 19:28:13 -07001645func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001646 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1647}
1648
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001649func (c *configImpl) KatiPackageMkDir() string {
1650 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1651}
1652
Dan Willemsenf052f782017-05-18 15:29:04 -07001653func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001654 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001655}
1656
1657func (c *configImpl) HostOut() string {
1658 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1659}
1660
1661// This probably needs to be multi-valued, so not exporting it for now
1662func (c *configImpl) hostCrossOut() string {
1663 if runtime.GOOS == "linux" {
1664 return filepath.Join(c.hostOutRoot(), "windows-x86")
1665 } else {
1666 return ""
1667 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001668}
1669
Dan Willemsen1e704462016-08-21 15:17:17 -07001670func (c *configImpl) HostPrebuiltTag() string {
1671 if runtime.GOOS == "linux" {
1672 return "linux-x86"
1673 } else if runtime.GOOS == "darwin" {
1674 return "darwin-x86"
1675 } else {
1676 panic("Unsupported OS")
1677 }
1678}
Dan Willemsenf173d592017-04-27 14:28:00 -07001679
Dan Willemsen8122bd52017-10-12 20:20:41 -07001680func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001681 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1682 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001683 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1684 if _, err := os.Stat(asan); err == nil {
1685 return asan
1686 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001687 }
1688 }
1689 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1690}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001691
1692func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1693 c.brokenDupRules = val
1694}
1695
1696func (c *configImpl) BuildBrokenDupRules() bool {
1697 return c.brokenDupRules
1698}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001699
Dan Willemsen25e6f092019-04-09 10:22:43 -07001700func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1701 c.brokenUsesNetwork = val
1702}
1703
1704func (c *configImpl) BuildBrokenUsesNetwork() bool {
1705 return c.brokenUsesNetwork
1706}
1707
Dan Willemsene3336352020-01-02 19:10:38 -08001708func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1709 c.brokenNinjaEnvVars = val
1710}
1711
1712func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1713 return c.brokenNinjaEnvVars
1714}
1715
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001716func (c *configImpl) SetTargetDeviceDir(dir string) {
1717 c.targetDeviceDir = dir
1718}
1719
1720func (c *configImpl) TargetDeviceDir() string {
1721 return c.targetDeviceDir
1722}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001723
Patrice Arruda219eef32020-06-01 17:29:30 +00001724func (c *configImpl) BuildDateTime() string {
1725 return c.buildDateTime
1726}
1727
1728func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001729 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001730}
Patrice Arruda83842d72020-12-08 19:42:08 +00001731
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001732// LogsDir returns the absolute path to the logs directory where build log and
1733// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001734// directory. If the argument dist is specified, the logs directory
1735// is <dist_dir>/logs.
1736func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001737 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001738 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001739 // 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 -05001740 dir = filepath.Join(c.RealDistDir(), "logs")
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 absDir, err := filepath.Abs(dir)
1743 if err != nil {
1744 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1745 os.Exit(1)
1746 }
1747 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001748}
1749
1750// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1751// where the bazel profiles are located.
1752func (c *configImpl) BazelMetricsDir() string {
1753 return filepath.Join(c.LogsDir(), "bazel_metrics")
1754}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001755
Chris Parsons53f68ae2022-03-03 12:01:40 -05001756// MkFileMetrics returns the file path for make-related metrics.
1757func (c *configImpl) MkMetrics() string {
1758 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1759}
1760
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001761func (c *configImpl) SetEmptyNinjaFile(v bool) {
1762 c.emptyNinjaFile = v
1763}
1764
1765func (c *configImpl) EmptyNinjaFile() bool {
1766 return c.emptyNinjaFile
1767}
Yu Liu6e13b402021-07-27 14:29:06 -07001768
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -04001769func (c *configImpl) IsBazelMixedBuildForceDisabled() bool {
1770 return c.Environment().IsEnvTrue("BUILD_BROKEN_DISABLE_BAZEL")
1771}
1772
Chris Parsons9402ca82023-02-23 17:28:06 -05001773func (c *configImpl) IsPersistentBazelEnabled() bool {
1774 return c.Environment().IsEnvTrue("USE_PERSISTENT_BAZEL")
1775}
1776
Chris Parsonsc83398f2023-05-31 18:41:41 +00001777// GetBazeliskBazelVersion returns the Bazel version to use for this build,
1778// or the empty string if the current canonical prod Bazel should be used.
1779// This environment variable should only be set to debug the build system.
1780// The Bazel version, if set, will be passed to Bazelisk, and Bazelisk will
1781// handle downloading and invoking the correct Bazel binary.
1782func (c *configImpl) GetBazeliskBazelVersion() string {
1783 value, _ := c.Environment().Get("USE_BAZEL_VERSION")
1784 return value
1785}
1786
MarkDacekd06db5d2022-11-29 00:47:59 +00001787func (c *configImpl) BazelModulesForceEnabledByFlag() string {
1788 return c.bazelForceEnabledModules
1789}
1790
MarkDacekd0e7cd32022-12-02 22:22:40 +00001791func (c *configImpl) SkipMetricsUpload() bool {
1792 return c.skipMetricsUpload
1793}
1794
MarkDacekf47e1422023-04-19 16:47:36 +00001795func (c *configImpl) EnsureAllowlistIntegrity() bool {
1796 return c.ensureAllowlistIntegrity
1797}
1798
MarkDacek6614d9c2022-12-07 21:57:38 +00001799// Returns a Time object if one was passed via a command-line flag.
1800// Otherwise returns the passed default.
1801func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1802 if c.buildStartedTime == 0 {
1803 return defaultTime
1804 }
1805 return time.UnixMilli(c.buildStartedTime)
1806}
1807
MarkDacekd33c2fd2023-05-04 20:40:04 +00001808func (c *configImpl) BazelExitCode() int32 {
1809 return c.bazelExitCode
1810}
1811
Yu Liu6e13b402021-07-27 14:29:06 -07001812func GetMetricsUploader(topDir string, env *Environment) string {
1813 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1814 metricsUploader := filepath.Join(topDir, p)
1815 if _, err := os.Stat(metricsUploader); err == nil {
1816 return metricsUploader
1817 }
1818 }
1819
1820 return ""
1821}