blob: 81ea69e86cd7cbb535ae9a69d349ae71f37b892f [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 Kumar84bd5bf2022-01-26 23:32:22 -050018 "context"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050019 "encoding/json"
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"
Dan Willemsen1e704462016-08-21 15:17:17 -070025 "path/filepath"
26 "runtime"
27 "strconv"
28 "strings"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040029 "syscall"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080030 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070031
32 "android/soong/shared"
Kousik Kumarec478642020-09-21 13:39:24 -040033
Dan Willemsen4591b642021-05-24 14:24:12 -070034 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000035
36 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070037)
38
Kousik Kumar3ff037e2022-01-25 22:11:01 -050039const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050040 envConfigDir = "vendor/google/tools/soong_config"
41 jsonSuffix = "json"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050042
Chris Parsons53f68ae2022-03-03 12:01:40 -050043 configFetcher = "vendor/google/tools/soong/expconfigfetcher"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050044 envConfigFetchTimeout = 10 * time.Second
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
Dan Willemsen1e704462016-08-21 15:17:17 -070057type Config struct{ *configImpl }
58
59type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020060 // Some targets that are implemented in soong_build
61 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080062 arguments []string
63 goma bool
64 environ *Environment
65 distDir string
66 buildDateTime string
MarkDacek6614d9c2022-12-07 21:57:38 +000067 logsPrefix string
Dan Willemsen1e704462016-08-21 15:17:17 -070068
69 // From the arguments
MarkDacekf47e1422023-04-19 16:47:36 +000070 parallel int
71 keepGoing int
72 verbose bool
73 checkbuild bool
74 dist bool
75 jsonModuleGraph bool
76 apiBp2build bool // Generate BUILD files for Soong modules that contribute APIs
77 bp2build bool
78 queryview bool
79 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
80 soongDocs bool
81 multitreeBuild bool // This is a multitree build.
82 skipConfig bool
83 skipKati bool
84 skipKatiNinja bool
85 skipSoong bool
86 skipNinja bool
87 skipSoongTests bool
88 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
89 skipMetricsUpload bool
90 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
91 buildFromTextStub bool
92 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
Dan Willemsen1e704462016-08-21 15:17:17 -070093
94 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070095 katiArgs []string
96 ninjaArgs []string
97 katiSuffix string
98 targetDevice string
99 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +0000100 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -0700101
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800102 // Autodetected
103 totalRAM uint64
104
Dan Willemsene3336352020-01-02 19:10:38 -0800105 brokenDupRules bool
106 brokenUsesNetwork bool
107 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -0700108
109 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000110
MarkDacekb78465d2022-10-18 20:10:16 +0000111 bazelProdMode bool
112 bazelDevMode bool
113 bazelStagingMode bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000114
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700115 // Set by multiproduct_kati
116 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700117
118 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000119
120 bazelForceEnabledModules string
Spandan Dasc5763832022-11-08 18:42:16 +0000121
Sam Delmerico98a73292023-02-21 11:50:29 -0500122 includeTags []string
123 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900124
125 // Data source to write ninja weight list
126 ninjaWeightListSource NinjaWeightListSource
Dan Willemsen1e704462016-08-21 15:17:17 -0700127}
128
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900129type NinjaWeightListSource uint
130
131const (
132 // ninja doesn't use weight list.
133 NOT_USED NinjaWeightListSource = iota
134 // ninja uses weight list based on previous builds by ninja log
135 NINJA_LOG
136 // ninja thinks every task has the same weight.
137 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900138 // ninja uses an external custom weight list
139 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900140 // ninja uses a prioritized module list from Soong
141 HINT_FROM_SOONG
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900142)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800143const srcDirFileCheck = "build/soong/root.bp"
144
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700145var buildFiles = []string{"Android.mk", "Android.bp"}
146
Patrice Arruda13848222019-04-22 17:12:02 -0700147type BuildAction uint
148
149const (
150 // Builds all of the modules and their dependencies of a specified directory, relative to the root
151 // directory of the source tree.
152 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
153
154 // Builds all of the modules and their dependencies of a list of specified directories. All specified
155 // directories are relative to the root directory of the source tree.
156 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700157
158 // Build a list of specified modules. If none was specified, simply build the whole source tree.
159 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700160)
161
162// checkTopDir validates that the current directory is at the root directory of the source tree.
163func checkTopDir(ctx Context) {
164 if _, err := os.Stat(srcDirFileCheck); err != nil {
165 if os.IsNotExist(err) {
166 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
167 }
168 ctx.Fatalln("Error verifying tree state:", err)
169 }
170}
171
Kousik Kumarc8818332023-01-16 16:33:05 +0000172// fetchEnvConfig optionally fetches a configuration file that can then subsequently be
173// loaded into Soong environment to control certain aspects of build behavior (e.g., enabling RBE).
174// If a configuration file already exists on disk, the fetch is run in the background
175// so as to NOT block the rest of the build execution.
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500176func fetchEnvConfig(ctx Context, config *configImpl, envConfigName string) error {
David Goldsmith62243a32022-04-08 13:42:04 +0000177 configName := envConfigName + "." + jsonSuffix
Kousik Kumarc75e1292022-07-07 02:20:51 +0000178 expConfigFetcher := &smpb.ExpConfigFetcher{Filename: &configName}
David Goldsmith62243a32022-04-08 13:42:04 +0000179 defer func() {
180 ctx.Metrics.ExpConfigFetcher(expConfigFetcher)
181 }()
Kousik Kumarc75e1292022-07-07 02:20:51 +0000182 if !config.GoogleProdCredsExist() {
183 status := smpb.ExpConfigFetcher_MISSING_GCERT
184 expConfigFetcher.Status = &status
185 return nil
186 }
David Goldsmith62243a32022-04-08 13:42:04 +0000187
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500188 s, err := os.Stat(configFetcher)
189 if err != nil {
190 if os.IsNotExist(err) {
191 return nil
192 }
193 return err
194 }
195 if s.Mode()&0111 == 0 {
David Goldsmith62243a32022-04-08 13:42:04 +0000196 status := smpb.ExpConfigFetcher_ERROR
197 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500198 return fmt.Errorf("configuration fetcher binary %v is not executable: %v", configFetcher, s.Mode())
199 }
200
Kousik Kumarc8818332023-01-16 16:33:05 +0000201 configExists := false
202 outConfigFilePath := filepath.Join(config.OutDir(), configName)
203 if _, err := os.Stat(outConfigFilePath); err == nil {
204 configExists = true
205 }
206
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500207 tCtx, cancel := context.WithTimeout(ctx, envConfigFetchTimeout)
David Goldsmith62243a32022-04-08 13:42:04 +0000208 fetchStart := time.Now()
209 cmd := exec.CommandContext(tCtx, configFetcher, "-output_config_dir", config.OutDir(),
210 "-output_config_name", configName)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500211 if err := cmd.Start(); err != nil {
David Goldsmith62243a32022-04-08 13:42:04 +0000212 status := smpb.ExpConfigFetcher_ERROR
213 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500214 return err
215 }
216
Kousik Kumarc8818332023-01-16 16:33:05 +0000217 fetchCfg := func() error {
218 if err := cmd.Wait(); err != nil {
219 status := smpb.ExpConfigFetcher_ERROR
220 expConfigFetcher.Status = &status
221 return err
222 }
223 fetchEnd := time.Now()
224 expConfigFetcher.Micros = proto.Uint64(uint64(fetchEnd.Sub(fetchStart).Microseconds()))
225 expConfigFetcher.Filename = proto.String(outConfigFilePath)
226
227 if _, err := os.Stat(outConfigFilePath); err != nil {
228 status := smpb.ExpConfigFetcher_NO_CONFIG
229 expConfigFetcher.Status = &status
230 return err
231 }
David Goldsmith62243a32022-04-08 13:42:04 +0000232 status := smpb.ExpConfigFetcher_CONFIG
233 expConfigFetcher.Status = &status
Kousik Kumarc8818332023-01-16 16:33:05 +0000234 return nil
David Goldsmith62243a32022-04-08 13:42:04 +0000235 }
Kousik Kumarc8818332023-01-16 16:33:05 +0000236
237 // If a config file does not exist, wait for the config file to be fetched. Otherwise
238 // fetch the config file in the background and return immediately.
239 if !configExists {
240 defer cancel()
241 return fetchCfg()
242 }
243
244 go func() {
245 defer cancel()
246 if err := fetchCfg(); err != nil {
247 ctx.Verbosef("Failed to fetch config file %v: %v\n", configName, err)
248 }
249 }()
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500250 return nil
251}
252
MarkDacek7901e582023-01-09 19:48:01 +0000253func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500254 if bc == "" {
255 return nil
256 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500257
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500258 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500259 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500260 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500261 envConfigDir,
262 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500263 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500264 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
265 envVarsJSON, err := ioutil.ReadFile(cfgFile)
266 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500267 continue
268 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500269 ctx.Verbosef("Loading config file %v\n", cfgFile)
270 var envVars map[string]map[string]string
271 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
272 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
273 continue
274 }
275 for k, v := range envVars["env"] {
276 if os.Getenv(k) != "" {
277 continue
278 }
279 config.environ.Set(k, v)
280 }
281 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
282 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500283 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500284
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500285 return nil
286}
287
Chris Parsonsb6e96902022-10-31 20:08:45 -0400288func defaultBazelProdMode(cfg *configImpl) bool {
MarkDacekd06db5d2022-11-29 00:47:59 +0000289 // Environment flag to disable Bazel for users which experience
Chris Parsonsb6e96902022-10-31 20:08:45 -0400290 // broken bazel-handled builds, or significant performance regressions.
291 if cfg.IsBazelMixedBuildForceDisabled() {
292 return false
293 }
294 // Darwin-host builds are currently untested with Bazel.
295 if runtime.GOOS == "darwin" {
296 return false
297 }
Chris Parsons035e03a2022-11-01 14:25:45 -0400298 return true
Chris Parsonsb6e96902022-10-31 20:08:45 -0400299}
300
MarkDacek6614d9c2022-12-07 21:57:38 +0000301func UploadOnlyConfig(ctx Context, _ ...string) Config {
302 ret := &configImpl{
303 environ: OsEnvironment(),
304 sandboxConfig: &SandboxConfig{},
305 }
MarkDacek7901e582023-01-09 19:48:01 +0000306 srcDir := absPath(ctx, ".")
307 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
308 if err := loadEnvConfig(ctx, ret, bc); err != nil {
309 ctx.Fatalln("Failed to parse env config files: %v", err)
310 }
311 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
MarkDacek6614d9c2022-12-07 21:57:38 +0000312 return Config{ret}
313}
314
Dan Willemsen1e704462016-08-21 15:17:17 -0700315func NewConfig(ctx Context, args ...string) Config {
316 ret := &configImpl{
Spandan Dasa3639e62021-05-25 19:14:02 +0000317 environ: OsEnvironment(),
318 sandboxConfig: &SandboxConfig{},
Dan Willemsen1e704462016-08-21 15:17:17 -0700319 }
320
Patrice Arruda90109172020-07-28 18:07:27 +0000321 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700322 ret.parallel = runtime.NumCPU() + 2
323 ret.keepGoing = 1
324
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800325 ret.totalRAM = detectTotalRAM(ctx)
Dan Willemsen9b587492017-07-10 22:13:00 -0700326 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900327
328 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
329 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "true")
330 }
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800331 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700332 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
333 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
334 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800335 outDir := "out"
336 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
337 if wd, err := os.Getwd(); err != nil {
338 ctx.Fatalln("Failed to get working directory:", err)
339 } else {
340 outDir = filepath.Join(baseDir, filepath.Base(wd))
341 }
342 }
343 ret.environ.Set("OUT_DIR", outDir)
344 }
345
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500346 // loadEnvConfig needs to know what the OUT_DIR is, so it should
347 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000348 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
349
350 if bc != "" {
351 if err := fetchEnvConfig(ctx, ret, bc); err != nil {
352 ctx.Verbosef("Failed to fetch config file: %v\n", err)
Kousik Kumarc8818332023-01-16 16:33:05 +0000353 }
354 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000355 ctx.Fatalln("Failed to parse env config files: %v", err)
356 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500357 }
358
Dan Willemsen2d31a442018-10-20 21:33:41 -0700359 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
360 ret.distDir = filepath.Clean(distDir)
361 } else {
362 ret.distDir = filepath.Join(ret.OutDir(), "dist")
363 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700364
Spandan Das05063612021-06-25 01:39:04 +0000365 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
366 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
367 }
368
Dan Willemsen1e704462016-08-21 15:17:17 -0700369 ret.environ.Unset(
370 // We're already using it
371 "USE_SOONG_UI",
372
373 // We should never use GOROOT/GOPATH from the shell environment
374 "GOROOT",
375 "GOPATH",
376
377 // These should only come from Soong, not the environment.
378 "CLANG",
379 "CLANG_CXX",
380 "CCC_CC",
381 "CCC_CXX",
382
383 // Used by the goma compiler wrapper, but should only be set by
384 // gomacc
385 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800386
387 // We handle this above
388 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700389
Dan Willemsen2d31a442018-10-20 21:33:41 -0700390 // This is handled above too, and set for individual commands later
391 "DIST_DIR",
392
Dan Willemsen68a09852017-04-18 13:56:57 -0700393 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000394 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700395 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700396 "DISPLAY",
397 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000398 "JAVAC",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700399 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700400 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700401
402 // Drop make flags
403 "MAKEFLAGS",
404 "MAKELEVEL",
405 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700406
407 // Set in envsetup.sh, reset in makefiles
408 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700409
410 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
411 "ANDROID_BUILD_TOP",
412 "ANDROID_HOST_OUT",
413 "ANDROID_PRODUCT_OUT",
414 "ANDROID_HOST_OUT_TESTCASES",
415 "ANDROID_TARGET_OUT_TESTCASES",
416 "ANDROID_TOOLCHAIN",
417 "ANDROID_TOOLCHAIN_2ND_ARCH",
418 "ANDROID_DEV_SCRIPTS",
419 "ANDROID_EMULATOR_PREBUILTS",
420 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700421 )
422
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400423 if ret.UseGoma() || ret.ForceUseGoma() {
424 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
425 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400426 }
427
Dan Willemsen1e704462016-08-21 15:17:17 -0700428 // Tell python not to spam the source tree with .pyc files.
429 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
430
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400431 tmpDir := absPath(ctx, ret.TempDir())
432 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800433
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700434 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
435 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
436 "llvm-binutils-stable/llvm-symbolizer")
437 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
438
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800439 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700440 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800441
Yu Liu6e13b402021-07-27 14:29:06 -0700442 srcDir := absPath(ctx, ".")
443 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700444 ctx.Println("You are building in a directory whose absolute path contains a space character:")
445 ctx.Println()
446 ctx.Printf("%q\n", srcDir)
447 ctx.Println()
448 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700449 }
450
Yu Liu6e13b402021-07-27 14:29:06 -0700451 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
452
Dan Willemsendb8457c2017-05-12 16:38:17 -0700453 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700454 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
455 ctx.Println()
456 ctx.Printf("%q\n", outDir)
457 ctx.Println()
458 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700459 }
460
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000461 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700462 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
463 ctx.Println()
464 ctx.Printf("%q\n", distDir)
465 ctx.Println()
466 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700467 }
468
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700469 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000470 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
471 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100472 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Colin Cross59c1e6a2022-03-04 13:37:19 -0800473 java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700474 javaHome := func() string {
475 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
476 return override
477 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000478 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
479 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 +0100480 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000481 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
482 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 17 toolchain is now the global default.")
483 }
484 return java17Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700485 }()
486 absJavaHome := absPath(ctx, javaHome)
487
Dan Willemsened869522018-01-08 14:58:46 -0800488 ret.configureLocale(ctx)
489
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700490 newPath := []string{filepath.Join(absJavaHome, "bin")}
491 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
492 newPath = append(newPath, path)
493 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100494
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700495 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
496 ret.environ.Set("JAVA_HOME", absJavaHome)
497 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000498 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
499 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100500 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700501 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
502
LaMont Jones52a72432023-03-09 18:19:35 +0000503 if ret.MultitreeBuild() {
504 ret.environ.Set("MULTITREE_BUILD", "true")
505 }
506
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800507 outDir := ret.OutDir()
508 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800509 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800510 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800511 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800512 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800513 }
Colin Cross28f527c2019-11-26 16:19:04 -0800514
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800515 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
516
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400517 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400518 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400519 ret.environ.Set(k, v)
520 }
521 }
522
Jihoon Kang1bff0342023-01-17 20:40:22 +0000523 if ret.BuildFromTextStub() {
524 // TODO(b/271443071): support hidden api check for from-text stub build
525 ret.environ.Set("UNSAFE_DISABLE_HIDDENAPI_FLAGS", "true")
526 }
527
Patrice Arruda83842d72020-12-08 19:42:08 +0000528 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800529 if err := os.RemoveAll(bpd); err != nil {
530 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
531 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000532
Patrice Arruda96850362020-08-11 20:41:11 +0000533 c := Config{ret}
534 storeConfigMetrics(ctx, c)
535 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700536}
537
Patrice Arruda13848222019-04-22 17:12:02 -0700538// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
539// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700540func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
541 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700542}
543
Patrice Arruda96850362020-08-11 20:41:11 +0000544// storeConfigMetrics selects a set of configuration information and store in
545// the metrics system for further analysis.
546func storeConfigMetrics(ctx Context, config Config) {
547 if ctx.Metrics == nil {
548 return
549 }
550
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400551 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000552
553 s := &smpb.SystemResourceInfo{
554 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
555 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
556 }
557 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000558}
559
Jeongik Cha8d63d562023-03-17 03:52:13 +0900560func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900561 switch s {
562 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900563 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900564 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900565 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900566 case EXTERNAL_FILE:
567 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900568 case HINT_FROM_SOONG:
569 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900570 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900571 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900572 }
573}
574
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400575func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700576 c := &smpb.BuildConfig{
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -0400577 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
578 UseGoma: proto.Bool(config.UseGoma()),
579 UseRbe: proto.Bool(config.UseRBE()),
580 BazelMixedBuild: proto.Bool(config.BazelBuildEnabled()),
581 ForceDisableBazelMixedBuild: proto.Bool(config.IsBazelMixedBuildForceDisabled()),
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900582 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400583 }
Yu Liue737a992021-10-04 13:21:41 -0700584 c.Targets = append(c.Targets, config.arguments...)
585
586 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400587}
588
Patrice Arruda13848222019-04-22 17:12:02 -0700589// getConfigArgs processes the command arguments based on the build action and creates a set of new
590// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700591func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700592 // The next block of code verifies that the current directory is the root directory of the source
593 // tree. It then finds the relative path of dir based on the root directory of the source tree
594 // and verify that dir is inside of the source tree.
595 checkTopDir(ctx)
596 topDir, err := os.Getwd()
597 if err != nil {
598 ctx.Fatalf("Error retrieving top directory: %v", err)
599 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700600 dir, err = filepath.EvalSymlinks(dir)
601 if err != nil {
602 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
603 }
Patrice Arruda13848222019-04-22 17:12:02 -0700604 dir, err = filepath.Abs(dir)
605 if err != nil {
606 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
607 }
608 relDir, err := filepath.Rel(topDir, dir)
609 if err != nil {
610 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
611 }
612 // If there are ".." in the path, it's not in the source tree.
613 if strings.Contains(relDir, "..") {
614 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
615 }
616
617 configArgs := args[:]
618
619 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
620 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
621 targetNamePrefix := "MODULES-IN-"
622 if inList("GET-INSTALL-PATH", configArgs) {
623 targetNamePrefix = "GET-INSTALL-PATH-IN-"
624 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
625 }
626
Patrice Arruda13848222019-04-22 17:12:02 -0700627 var targets []string
628
629 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700630 case BUILD_MODULES:
631 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700632 case BUILD_MODULES_IN_A_DIRECTORY:
633 // If dir is the root source tree, all the modules are built of the source tree are built so
634 // no need to find the build file.
635 if topDir == dir {
636 break
637 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700638
Patrice Arruda13848222019-04-22 17:12:02 -0700639 buildFile := findBuildFile(ctx, relDir)
640 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700641 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700642 }
Patrice Arruda13848222019-04-22 17:12:02 -0700643 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
644 case BUILD_MODULES_IN_DIRECTORIES:
645 newConfigArgs, dirs := splitArgs(configArgs)
646 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700647 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700648 }
649
650 // Tidy only override all other specified targets.
651 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
652 if tidyOnly == "true" || tidyOnly == "1" {
653 configArgs = append(configArgs, "tidy_only")
654 } else {
655 configArgs = append(configArgs, targets...)
656 }
657
658 return configArgs
659}
660
661// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
662func convertToTarget(dir string, targetNamePrefix string) string {
663 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
664}
665
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700666// hasBuildFile returns true if dir contains an Android build file.
667func hasBuildFile(ctx Context, dir string) bool {
668 for _, buildFile := range buildFiles {
669 _, err := os.Stat(filepath.Join(dir, buildFile))
670 if err == nil {
671 return true
672 }
673 if !os.IsNotExist(err) {
674 ctx.Fatalf("Error retrieving the build file stats: %v", err)
675 }
676 }
677 return false
678}
679
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700680// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
681// in the current and any sub directory of dir. If a build file is not found, traverse the path
682// up by one directory and repeat again until either a build file is found or reached to the root
683// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
684// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700685func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700686 // If the string is empty or ".", assume it is top directory of the source tree.
687 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700688 return ""
689 }
690
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700691 found := false
692 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
693 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
694 if err != nil {
695 return err
696 }
697 if found {
698 return filepath.SkipDir
699 }
700 if info.IsDir() {
701 return nil
702 }
703 for _, buildFile := range buildFiles {
704 if info.Name() == buildFile {
705 found = true
706 return filepath.SkipDir
707 }
708 }
709 return nil
710 })
711 if err != nil {
712 ctx.Fatalf("Error finding Android build file: %v", err)
713 }
714
715 if found {
716 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700717 }
718 }
719
720 return ""
721}
722
723// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
724func splitArgs(args []string) (newArgs []string, dirs []string) {
725 specialArgs := map[string]bool{
726 "showcommands": true,
727 "snod": true,
728 "dist": true,
729 "checkbuild": true,
730 }
731
732 newArgs = []string{}
733 dirs = []string{}
734
735 for _, arg := range args {
736 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
737 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
738 newArgs = append(newArgs, arg)
739 continue
740 }
741
742 if _, ok := specialArgs[arg]; ok {
743 newArgs = append(newArgs, arg)
744 continue
745 }
746
747 dirs = append(dirs, arg)
748 }
749
750 return newArgs, dirs
751}
752
753// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
754// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
755// source root tree where the build action command was invoked. Each directory is validated if the
756// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700757func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700758 for _, dir := range dirs {
759 // The directory may have specified specific modules to build. ":" is the separator to separate
760 // the directory and the list of modules.
761 s := strings.Split(dir, ":")
762 l := len(s)
763 if l > 2 { // more than one ":" was specified.
764 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
765 }
766
767 dir = filepath.Join(relDir, s[0])
768 if _, err := os.Stat(dir); err != nil {
769 ctx.Fatalf("couldn't find directory %s", dir)
770 }
771
772 // Verify that if there are any targets specified after ":". Each target is separated by ",".
773 var newTargets []string
774 if l == 2 && s[1] != "" {
775 newTargets = strings.Split(s[1], ",")
776 if inList("", newTargets) {
777 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
778 }
779 }
780
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700781 // If there are specified targets to build in dir, an android build file must exist for the one
782 // shot build. For the non-targets case, find the appropriate build file and build all the
783 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700784 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700785 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700786 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
787 }
788 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700789 buildFile := findBuildFile(ctx, dir)
790 if buildFile == "" {
791 ctx.Fatalf("Build file not found for %s directory", dir)
792 }
793 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700794 }
795
Patrice Arruda13848222019-04-22 17:12:02 -0700796 targets = append(targets, newTargets...)
797 }
798
Dan Willemsence41e942019-07-29 23:39:30 -0700799 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700800}
801
Dan Willemsen9b587492017-07-10 22:13:00 -0700802func (c *configImpl) parseArgs(ctx Context, args []string) {
803 for i := 0; i < len(args); i++ {
804 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100805 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700806 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200807 } else if arg == "--empty-ninja-file" {
808 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100809 } else if arg == "--skip-ninja" {
810 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700811 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700812 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
813 // but it does run a Kati ninja file if the .kati_enabled marker file was created
814 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000815 c.skipConfig = true
816 c.skipKati = true
817 } else if arg == "--skip-kati" {
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100818 // TODO: remove --skip-kati once module builds have been migrated to --song-only
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000819 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100820 } else if arg == "--soong-only" {
821 c.skipKati = true
822 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200823 } else if arg == "--config-only" {
824 c.skipKati = true
825 c.skipKatiNinja = true
826 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700827 } else if arg == "--skip-config" {
828 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700829 } else if arg == "--skip-soong-tests" {
830 c.skipSoongTests = true
MarkDacekd0e7cd32022-12-02 22:22:40 +0000831 } else if arg == "--skip-metrics-upload" {
832 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500833 } else if arg == "--mk-metrics" {
834 c.reportMkMetrics = true
LaMont Jones52a72432023-03-09 18:19:35 +0000835 } else if arg == "--multitree-build" {
836 c.multitreeBuild = true
Chris Parsonsef615e52022-08-18 22:04:11 -0400837 } else if arg == "--bazel-mode" {
838 c.bazelProdMode = true
839 } else if arg == "--bazel-mode-dev" {
840 c.bazelDevMode = true
MarkDacekb78465d2022-10-18 20:10:16 +0000841 } else if arg == "--bazel-mode-staging" {
842 c.bazelStagingMode = true
Spandan Das394aa322022-11-03 17:02:10 +0000843 } else if arg == "--search-api-dir" {
844 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900845 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
846 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
847 if source == "ninja_log" {
848 c.ninjaWeightListSource = NINJA_LOG
849 } else if source == "evenly_distributed" {
850 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
851 } else if source == "not_used" {
852 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900853 } else if source == "soong" {
854 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900855 } else if strings.HasPrefix(source, "file,") {
856 c.ninjaWeightListSource = EXTERNAL_FILE
857 filePath := strings.TrimPrefix(source, "file,")
858 err := validateNinjaWeightList(filePath)
859 if err != nil {
860 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
861 }
862 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
863 if err != nil {
864 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
865 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900866 } else {
867 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
868 }
Jihoon Kang1bff0342023-01-17 20:40:22 +0000869 } else if arg == "--build-from-text-stub" {
870 c.buildFromTextStub = true
MarkDacekb96561e2022-12-02 04:34:43 +0000871 } else if strings.HasPrefix(arg, "--build-command=") {
872 buildCmd := strings.TrimPrefix(arg, "--build-command=")
873 // remove quotations
874 buildCmd = strings.TrimPrefix(buildCmd, "\"")
875 buildCmd = strings.TrimSuffix(buildCmd, "\"")
876 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacekd06db5d2022-11-29 00:47:59 +0000877 } else if strings.HasPrefix(arg, "--bazel-force-enabled-modules=") {
878 c.bazelForceEnabledModules = strings.TrimPrefix(arg, "--bazel-force-enabled-modules=")
MarkDacek6614d9c2022-12-07 21:57:38 +0000879 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
880 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
881 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
882 if err == nil {
883 c.buildStartedTime = val
884 } else {
885 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
886 }
MarkDacekf47e1422023-04-19 16:47:36 +0000887 } else if arg == "--ensure-allowlist-integrity" {
888 c.ensureAllowlistIntegrity = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700889 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700890 parseArgNum := func(def int) int {
891 if len(arg) > 2 {
892 p, err := strconv.ParseUint(arg[2:], 10, 31)
893 if err != nil {
894 ctx.Fatalf("Failed to parse %q: %v", arg, err)
895 }
896 return int(p)
897 } else if i+1 < len(args) {
898 p, err := strconv.ParseUint(args[i+1], 10, 31)
899 if err == nil {
900 i++
901 return int(p)
902 }
903 }
904 return def
905 }
906
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700907 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700908 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700909 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700910 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700911 } else {
912 ctx.Fatalln("Unknown option:", arg)
913 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700914 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700915 if k == "OUT_DIR" {
916 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
917 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700918 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700919 } else if arg == "dist" {
920 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200921 } else if arg == "json-module-graph" {
922 c.jsonModuleGraph = true
923 } else if arg == "bp2build" {
924 c.bp2build = true
Spandan Das5af0bd32022-09-28 20:43:08 +0000925 } else if arg == "api_bp2build" {
926 c.apiBp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200927 } else if arg == "queryview" {
928 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200929 } else if arg == "soong_docs" {
930 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700931 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700932 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800933 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700934 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700935 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700936 }
937 }
Chris Parsonsb6e96902022-10-31 20:08:45 -0400938 if (!c.bazelProdMode) && (!c.bazelDevMode) && (!c.bazelStagingMode) {
939 c.bazelProdMode = defaultBazelProdMode(c)
940 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700941}
942
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900943func validateNinjaWeightList(weightListFilePath string) (err error) {
944 data, err := os.ReadFile(weightListFilePath)
945 if err != nil {
946 return
947 }
948 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
949 for _, line := range lines {
950 fields := strings.Split(line, ",")
951 if len(fields) != 2 {
952 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
953 }
954 _, err = strconv.Atoi(fields[1])
955 if err != nil {
956 return
957 }
958 }
959 return
960}
961
Dan Willemsened869522018-01-08 14:58:46 -0800962func (c *configImpl) configureLocale(ctx Context) {
963 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
964 output, err := cmd.Output()
965
966 var locales []string
967 if err == nil {
968 locales = strings.Split(string(output), "\n")
969 } else {
970 // If we're unable to list the locales, let's assume en_US.UTF-8
971 locales = []string{"en_US.UTF-8"}
972 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
973 }
974
975 // gettext uses LANGUAGE, which is passed directly through
976
977 // For LANG and LC_*, only preserve the evaluated version of
978 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800979 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800980 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800981 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800982 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800983 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800984 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800985 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800986 }
987
988 c.environ.UnsetWithPrefix("LC_")
989
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800990 if userLang != "" {
991 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800992 }
993
994 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
995 // for others)
996 if inList("C.UTF-8", locales) {
997 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500998 } else if inList("C.utf8", locales) {
999 // These normalize to the same thing
1000 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -08001001 } else if inList("en_US.UTF-8", locales) {
1002 c.environ.Set("LANG", "en_US.UTF-8")
1003 } else if inList("en_US.utf8", locales) {
1004 // These normalize to the same thing
1005 c.environ.Set("LANG", "en_US.UTF-8")
1006 } else {
1007 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
1008 }
1009}
1010
Dan Willemsen1e704462016-08-21 15:17:17 -07001011func (c *configImpl) Environment() *Environment {
1012 return c.environ
1013}
1014
1015func (c *configImpl) Arguments() []string {
1016 return c.arguments
1017}
1018
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001019func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001020 if len(c.Arguments()) > 0 {
1021 // Explicit targets requested that are not special targets like b2pbuild
1022 // or the JSON module graph
1023 return true
1024 }
1025
Spandan Das5af0bd32022-09-28 20:43:08 +00001026 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() && !c.ApiBp2build() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001027 // Command line was empty, the default Ninja target is built
1028 return true
1029 }
1030
Liz Kammer88677422021-12-15 15:03:19 -05001031 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
1032 if c.Dist() && !c.Bp2Build() {
1033 return true
1034 }
1035
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001036 // build.ninja doesn't need to be generated
1037 return false
1038}
1039
Dan Willemsen1e704462016-08-21 15:17:17 -07001040func (c *configImpl) OutDir() string {
1041 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -07001042 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -07001043 }
1044 return "out"
1045}
1046
Dan Willemsen8a073a82017-02-04 17:30:44 -08001047func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -04001048 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001049}
1050
1051func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001052 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001053}
1054
Dan Willemsen1e704462016-08-21 15:17:17 -07001055func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001056 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001057 return c.arguments
1058 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001059 return c.ninjaArgs
1060}
1061
Jingwen Chen7c6089a2020-11-02 02:56:20 -05001062func (c *configImpl) BazelOutDir() string {
1063 return filepath.Join(c.OutDir(), "bazel")
1064}
1065
Liz Kammer2af5ea82022-11-11 14:21:03 -05001066func (c *configImpl) bazelOutputBase() string {
1067 return filepath.Join(c.BazelOutDir(), "output")
1068}
1069
Dan Willemsen1e704462016-08-21 15:17:17 -07001070func (c *configImpl) SoongOutDir() string {
1071 return filepath.Join(c.OutDir(), "soong")
1072}
1073
Spandan Das394aa322022-11-03 17:02:10 +00001074func (c *configImpl) ApiSurfacesOutDir() string {
1075 return filepath.Join(c.OutDir(), "api_surfaces")
1076}
1077
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001078func (c *configImpl) PrebuiltOS() string {
1079 switch runtime.GOOS {
1080 case "linux":
1081 return "linux-x86"
1082 case "darwin":
1083 return "darwin-x86"
1084 default:
1085 panic("Unknown GOOS")
1086 }
1087}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001088
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001089func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001090 if c.SkipKatiNinja() {
1091 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1092 } else {
1093 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1094 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001095}
1096
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001097func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001098 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001099}
1100
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001101func (c *configImpl) UsedEnvFile(tag string) string {
1102 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1103}
1104
Lukacs T. Berkic541cd22022-10-26 07:26:50 +00001105func (c *configImpl) Bp2BuildFilesMarkerFile() string {
1106 return shared.JoinPath(c.SoongOutDir(), "bp2build_files_marker")
1107}
1108
1109func (c *configImpl) Bp2BuildWorkspaceMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001110 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +02001111}
1112
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001113func (c *configImpl) SoongDocsHtml() string {
1114 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1115}
1116
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001117func (c *configImpl) QueryviewMarkerFile() string {
1118 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1119}
1120
Spandan Das5af0bd32022-09-28 20:43:08 +00001121func (c *configImpl) ApiBp2buildMarkerFile() string {
1122 return shared.JoinPath(c.SoongOutDir(), "api_bp2build.marker")
1123}
1124
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001125func (c *configImpl) ModuleGraphFile() string {
1126 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1127}
1128
kgui67007242022-01-25 13:50:25 +08001129func (c *configImpl) ModuleActionsFile() string {
1130 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1131}
1132
Jeff Gastonefc1b412017-03-29 17:29:06 -07001133func (c *configImpl) TempDir() string {
1134 return shared.TempDirForOutDir(c.SoongOutDir())
1135}
1136
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001137func (c *configImpl) FileListDir() string {
1138 return filepath.Join(c.OutDir(), ".module_paths")
1139}
1140
Dan Willemsen1e704462016-08-21 15:17:17 -07001141func (c *configImpl) KatiSuffix() string {
1142 if c.katiSuffix != "" {
1143 return c.katiSuffix
1144 }
1145 panic("SetKatiSuffix has not been called")
1146}
1147
Colin Cross37193492017-11-16 17:55:00 -08001148// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1149// user is interested in additional checks at the expense of build time.
1150func (c *configImpl) Checkbuild() bool {
1151 return c.checkbuild
1152}
1153
Dan Willemsen8a073a82017-02-04 17:30:44 -08001154func (c *configImpl) Dist() bool {
1155 return c.dist
1156}
1157
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001158func (c *configImpl) JsonModuleGraph() bool {
1159 return c.jsonModuleGraph
1160}
1161
1162func (c *configImpl) Bp2Build() bool {
1163 return c.bp2build
1164}
1165
Spandan Das5af0bd32022-09-28 20:43:08 +00001166func (c *configImpl) ApiBp2build() bool {
1167 return c.apiBp2build
1168}
1169
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001170func (c *configImpl) Queryview() bool {
1171 return c.queryview
1172}
1173
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001174func (c *configImpl) SoongDocs() bool {
1175 return c.soongDocs
1176}
1177
Dan Willemsen1e704462016-08-21 15:17:17 -07001178func (c *configImpl) IsVerbose() bool {
1179 return c.verbose
1180}
1181
LaMont Jones52a72432023-03-09 18:19:35 +00001182func (c *configImpl) MultitreeBuild() bool {
1183 return c.multitreeBuild
1184}
1185
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001186func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1187 return c.ninjaWeightListSource
1188}
1189
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001190func (c *configImpl) SkipKati() bool {
1191 return c.skipKati
1192}
1193
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001194func (c *configImpl) SkipKatiNinja() bool {
1195 return c.skipKatiNinja
1196}
1197
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001198func (c *configImpl) SkipSoong() bool {
1199 return c.skipSoong
1200}
1201
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001202func (c *configImpl) SkipNinja() bool {
1203 return c.skipNinja
1204}
1205
Anton Hansson5a7861a2021-06-04 10:09:01 +01001206func (c *configImpl) SetSkipNinja(v bool) {
1207 c.skipNinja = v
1208}
1209
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001210func (c *configImpl) SkipConfig() bool {
1211 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001212}
1213
Jihoon Kang1bff0342023-01-17 20:40:22 +00001214func (c *configImpl) BuildFromTextStub() bool {
1215 return c.buildFromTextStub
1216}
1217
Dan Willemsen1e704462016-08-21 15:17:17 -07001218func (c *configImpl) TargetProduct() string {
1219 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1220 return v
1221 }
1222 panic("TARGET_PRODUCT is not defined")
1223}
1224
Dan Willemsen02781d52017-05-12 19:28:13 -07001225func (c *configImpl) TargetDevice() string {
1226 return c.targetDevice
1227}
1228
1229func (c *configImpl) SetTargetDevice(device string) {
1230 c.targetDevice = device
1231}
1232
1233func (c *configImpl) TargetBuildVariant() string {
1234 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1235 return v
1236 }
1237 panic("TARGET_BUILD_VARIANT is not defined")
1238}
1239
Dan Willemsen1e704462016-08-21 15:17:17 -07001240func (c *configImpl) KatiArgs() []string {
1241 return c.katiArgs
1242}
1243
1244func (c *configImpl) Parallel() int {
1245 return c.parallel
1246}
1247
Sam Delmerico98a73292023-02-21 11:50:29 -05001248func (c *configImpl) GetSourceRootDirs() []string {
1249 return c.sourceRootDirs
1250}
1251
1252func (c *configImpl) SetSourceRootDirs(i []string) {
1253 c.sourceRootDirs = i
1254}
1255
Spandan Dasc5763832022-11-08 18:42:16 +00001256func (c *configImpl) GetIncludeTags() []string {
1257 return c.includeTags
1258}
1259
1260func (c *configImpl) SetIncludeTags(i []string) {
1261 c.includeTags = i
1262}
1263
MarkDacek6614d9c2022-12-07 21:57:38 +00001264func (c *configImpl) GetLogsPrefix() string {
1265 return c.logsPrefix
1266}
1267
1268func (c *configImpl) SetLogsPrefix(prefix string) {
1269 c.logsPrefix = prefix
1270}
1271
Colin Cross8b8bec32019-11-15 13:18:43 -08001272func (c *configImpl) HighmemParallel() int {
1273 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1274 return i
1275 }
1276
1277 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1278 parallel := c.Parallel()
1279 if c.UseRemoteBuild() {
1280 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1281 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1282 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1283 // Return 1/16th of the size of the local pool, rounding up.
1284 return (parallel + 15) / 16
1285 } else if c.totalRAM == 0 {
1286 // Couldn't detect the total RAM, don't restrict highmem processes.
1287 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001288 } else if c.totalRAM <= 16*1024*1024*1024 {
1289 // Less than 16GB of ram, restrict to 1 highmem processes
1290 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001291 } else if c.totalRAM <= 32*1024*1024*1024 {
1292 // Less than 32GB of ram, restrict to 2 highmem processes
1293 return 2
1294 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1295 // If less than 8GB total RAM per process, reduce the number of highmem processes
1296 return p
1297 }
1298 // No restriction on highmem processes
1299 return parallel
1300}
1301
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001302func (c *configImpl) TotalRAM() uint64 {
1303 return c.totalRAM
1304}
1305
Kousik Kumarec478642020-09-21 13:39:24 -04001306// ForceUseGoma determines whether we should override Goma deprecation
1307// and use Goma for the current build or not.
1308func (c *configImpl) ForceUseGoma() bool {
1309 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1310 v = strings.TrimSpace(v)
1311 if v != "" && v != "false" {
1312 return true
1313 }
1314 }
1315 return false
1316}
1317
Dan Willemsen1e704462016-08-21 15:17:17 -07001318func (c *configImpl) UseGoma() bool {
1319 if v, ok := c.environ.Get("USE_GOMA"); ok {
1320 v = strings.TrimSpace(v)
1321 if v != "" && v != "false" {
1322 return true
1323 }
1324 }
1325 return false
1326}
1327
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001328func (c *configImpl) StartGoma() bool {
1329 if !c.UseGoma() {
1330 return false
1331 }
1332
1333 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1334 v = strings.TrimSpace(v)
1335 if v != "" && v != "false" {
1336 return false
1337 }
1338 }
1339 return true
1340}
1341
Ramy Medhatbbf25672019-07-17 12:30:04 +00001342func (c *configImpl) UseRBE() bool {
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001343 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001344 v = strings.TrimSpace(v)
1345 if v != "" && v != "false" {
1346 return true
1347 }
1348 }
1349 return false
1350}
1351
Chris Parsonsef615e52022-08-18 22:04:11 -04001352func (c *configImpl) BazelBuildEnabled() bool {
MarkDacekb78465d2022-10-18 20:10:16 +00001353 return c.bazelProdMode || c.bazelDevMode || c.bazelStagingMode
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001354}
1355
Ramy Medhatbbf25672019-07-17 12:30:04 +00001356func (c *configImpl) StartRBE() bool {
1357 if !c.UseRBE() {
1358 return false
1359 }
1360
1361 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1362 v = strings.TrimSpace(v)
1363 if v != "" && v != "false" {
1364 return false
1365 }
1366 }
1367 return true
1368}
1369
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001370func (c *configImpl) rbeProxyLogsDir() string {
1371 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001372 if v, ok := c.environ.Get(f); ok {
1373 return v
1374 }
1375 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001376 buildTmpDir := shared.TempDirForOutDir(c.SoongOutDir())
1377 return filepath.Join(buildTmpDir, "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001378}
1379
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001380func (c *configImpl) rbeCacheDir() string {
1381 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1382 if v, ok := c.environ.Get(f); ok {
1383 return v
1384 }
1385 }
1386 return shared.JoinPath(c.SoongOutDir(), "rbe")
1387}
1388
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001389func (c *configImpl) shouldCleanupRBELogsDir() bool {
1390 // Perform a log directory cleanup only when the log directory
1391 // is auto created by the build rather than user-specified.
1392 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
1393 if _, ok := c.environ.Get(f); ok {
1394 return false
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001395 }
1396 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001397 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001398}
1399
1400func (c *configImpl) rbeExecRoot() string {
1401 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1402 if v, ok := c.environ.Get(f); ok {
1403 return v
1404 }
1405 }
1406 wd, err := os.Getwd()
1407 if err != nil {
1408 return ""
1409 }
1410 return wd
1411}
1412
1413func (c *configImpl) rbeDir() string {
1414 if v, ok := c.environ.Get("RBE_DIR"); ok {
1415 return v
1416 }
1417 return "prebuilts/remoteexecution-client/live/"
1418}
1419
1420func (c *configImpl) rbeReproxy() string {
1421 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1422 if v, ok := c.environ.Get(f); ok {
1423 return v
1424 }
1425 }
1426 return filepath.Join(c.rbeDir(), "reproxy")
1427}
1428
1429func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001430 credFlags := []string{
1431 "use_application_default_credentials",
1432 "use_gce_credentials",
1433 "credential_file",
1434 "use_google_prod_creds",
1435 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001436 for _, cf := range credFlags {
1437 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1438 if v, ok := c.environ.Get(f); ok {
1439 v = strings.TrimSpace(v)
1440 if v != "" && v != "false" && v != "0" {
1441 return "RBE_" + cf, v
1442 }
1443 }
1444 }
1445 }
1446 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001447}
1448
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001449func (c *configImpl) rbeSockAddr(dir string) (string, error) {
1450 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
1451 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1452
1453 name := filepath.Join(dir, base)
1454 if len(name) < maxNameLen {
1455 return name, nil
1456 }
1457
1458 name = filepath.Join("/tmp", base)
1459 if len(name) < maxNameLen {
1460 return name, nil
1461 }
1462
1463 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1464}
1465
Kousik Kumar7bc78192022-04-27 14:52:56 -04001466// IsGooglerEnvironment returns true if the current build is running
1467// on a Google developer machine and false otherwise.
1468func (c *configImpl) IsGooglerEnvironment() bool {
1469 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1470 if v, ok := c.environ.Get(cf); ok {
1471 return v == "googler"
1472 }
1473 return false
1474}
1475
1476// GoogleProdCredsExist determine whether credentials exist on the
1477// Googler machine to use remote execution.
1478func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001479 if googleProdCredsExistCache {
1480 return googleProdCredsExistCache
1481 }
Kousik Kumar7bc78192022-04-27 14:52:56 -04001482 if _, err := exec.Command("/usr/bin/prodcertstatus", "--simple_output", "--nocheck_loas").Output(); err != nil {
1483 return false
1484 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001485 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001486 return true
1487}
1488
1489// UseRemoteBuild indicates whether to use a remote build acceleration system
1490// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001491func (c *configImpl) UseRemoteBuild() bool {
1492 return c.UseGoma() || c.UseRBE()
1493}
1494
Kousik Kumar7bc78192022-04-27 14:52:56 -04001495// StubbyExists checks whether the stubby binary exists on the machine running
1496// the build.
1497func (c *configImpl) StubbyExists() bool {
1498 if _, err := exec.LookPath("stubby"); err != nil {
1499 return false
1500 }
1501 return true
1502}
1503
Dan Willemsen1e704462016-08-21 15:17:17 -07001504// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001505// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001506// still limited by Parallel()
1507func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001508 if !c.UseRemoteBuild() {
1509 return 0
1510 }
1511 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1512 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001513 }
1514 return 500
1515}
1516
1517func (c *configImpl) SetKatiArgs(args []string) {
1518 c.katiArgs = args
1519}
1520
1521func (c *configImpl) SetNinjaArgs(args []string) {
1522 c.ninjaArgs = args
1523}
1524
1525func (c *configImpl) SetKatiSuffix(suffix string) {
1526 c.katiSuffix = suffix
1527}
1528
Dan Willemsene0879fc2017-08-04 15:06:27 -07001529func (c *configImpl) LastKatiSuffixFile() string {
1530 return filepath.Join(c.OutDir(), "last_kati_suffix")
1531}
1532
1533func (c *configImpl) HasKatiSuffix() bool {
1534 return c.katiSuffix != ""
1535}
1536
Dan Willemsen1e704462016-08-21 15:17:17 -07001537func (c *configImpl) KatiEnvFile() string {
1538 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1539}
1540
Dan Willemsen29971232018-09-26 14:58:30 -07001541func (c *configImpl) KatiBuildNinjaFile() string {
1542 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001543}
1544
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001545func (c *configImpl) KatiPackageNinjaFile() string {
1546 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1547}
1548
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001549func (c *configImpl) SoongVarsFile() string {
1550 return filepath.Join(c.SoongOutDir(), "soong.variables")
1551}
1552
Dan Willemsen1e704462016-08-21 15:17:17 -07001553func (c *configImpl) SoongNinjaFile() string {
1554 return filepath.Join(c.SoongOutDir(), "build.ninja")
1555}
1556
1557func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001558 if c.katiSuffix == "" {
1559 return filepath.Join(c.OutDir(), "combined.ninja")
1560 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001561 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1562}
1563
1564func (c *configImpl) SoongAndroidMk() string {
1565 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1566}
1567
1568func (c *configImpl) SoongMakeVarsMk() string {
1569 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1570}
1571
Dan Willemsenf052f782017-05-18 15:29:04 -07001572func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001573 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001574}
1575
Dan Willemsen02781d52017-05-12 19:28:13 -07001576func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001577 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1578}
1579
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001580func (c *configImpl) KatiPackageMkDir() string {
1581 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1582}
1583
Dan Willemsenf052f782017-05-18 15:29:04 -07001584func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001585 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001586}
1587
1588func (c *configImpl) HostOut() string {
1589 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1590}
1591
1592// This probably needs to be multi-valued, so not exporting it for now
1593func (c *configImpl) hostCrossOut() string {
1594 if runtime.GOOS == "linux" {
1595 return filepath.Join(c.hostOutRoot(), "windows-x86")
1596 } else {
1597 return ""
1598 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001599}
1600
Dan Willemsen1e704462016-08-21 15:17:17 -07001601func (c *configImpl) HostPrebuiltTag() string {
1602 if runtime.GOOS == "linux" {
1603 return "linux-x86"
1604 } else if runtime.GOOS == "darwin" {
1605 return "darwin-x86"
1606 } else {
1607 panic("Unsupported OS")
1608 }
1609}
Dan Willemsenf173d592017-04-27 14:28:00 -07001610
Dan Willemsen8122bd52017-10-12 20:20:41 -07001611func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001612 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1613 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001614 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1615 if _, err := os.Stat(asan); err == nil {
1616 return asan
1617 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001618 }
1619 }
1620 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1621}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001622
1623func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1624 c.brokenDupRules = val
1625}
1626
1627func (c *configImpl) BuildBrokenDupRules() bool {
1628 return c.brokenDupRules
1629}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001630
Dan Willemsen25e6f092019-04-09 10:22:43 -07001631func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1632 c.brokenUsesNetwork = val
1633}
1634
1635func (c *configImpl) BuildBrokenUsesNetwork() bool {
1636 return c.brokenUsesNetwork
1637}
1638
Dan Willemsene3336352020-01-02 19:10:38 -08001639func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1640 c.brokenNinjaEnvVars = val
1641}
1642
1643func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1644 return c.brokenNinjaEnvVars
1645}
1646
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001647func (c *configImpl) SetTargetDeviceDir(dir string) {
1648 c.targetDeviceDir = dir
1649}
1650
1651func (c *configImpl) TargetDeviceDir() string {
1652 return c.targetDeviceDir
1653}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001654
Patrice Arruda219eef32020-06-01 17:29:30 +00001655func (c *configImpl) BuildDateTime() string {
1656 return c.buildDateTime
1657}
1658
1659func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001660 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001661}
Patrice Arruda83842d72020-12-08 19:42:08 +00001662
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001663// LogsDir returns the absolute path to the logs directory where build log and
1664// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001665// directory. If the argument dist is specified, the logs directory
1666// is <dist_dir>/logs.
1667func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001668 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001669 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001670 // 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 -05001671 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001672 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001673 absDir, err := filepath.Abs(dir)
1674 if err != nil {
1675 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1676 os.Exit(1)
1677 }
1678 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001679}
1680
1681// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1682// where the bazel profiles are located.
1683func (c *configImpl) BazelMetricsDir() string {
1684 return filepath.Join(c.LogsDir(), "bazel_metrics")
1685}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001686
Chris Parsons53f68ae2022-03-03 12:01:40 -05001687// MkFileMetrics returns the file path for make-related metrics.
1688func (c *configImpl) MkMetrics() string {
1689 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1690}
1691
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001692func (c *configImpl) SetEmptyNinjaFile(v bool) {
1693 c.emptyNinjaFile = v
1694}
1695
1696func (c *configImpl) EmptyNinjaFile() bool {
1697 return c.emptyNinjaFile
1698}
Yu Liu6e13b402021-07-27 14:29:06 -07001699
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -04001700func (c *configImpl) IsBazelMixedBuildForceDisabled() bool {
1701 return c.Environment().IsEnvTrue("BUILD_BROKEN_DISABLE_BAZEL")
1702}
1703
Chris Parsons9402ca82023-02-23 17:28:06 -05001704func (c *configImpl) IsPersistentBazelEnabled() bool {
1705 return c.Environment().IsEnvTrue("USE_PERSISTENT_BAZEL")
1706}
1707
MarkDacekd06db5d2022-11-29 00:47:59 +00001708func (c *configImpl) BazelModulesForceEnabledByFlag() string {
1709 return c.bazelForceEnabledModules
1710}
1711
MarkDacekd0e7cd32022-12-02 22:22:40 +00001712func (c *configImpl) SkipMetricsUpload() bool {
1713 return c.skipMetricsUpload
1714}
1715
MarkDacekf47e1422023-04-19 16:47:36 +00001716func (c *configImpl) EnsureAllowlistIntegrity() bool {
1717 return c.ensureAllowlistIntegrity
1718}
1719
MarkDacek6614d9c2022-12-07 21:57:38 +00001720// Returns a Time object if one was passed via a command-line flag.
1721// Otherwise returns the passed default.
1722func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1723 if c.buildStartedTime == 0 {
1724 return defaultTime
1725 }
1726 return time.UnixMilli(c.buildStartedTime)
1727}
1728
Yu Liu6e13b402021-07-27 14:29:06 -07001729func GetMetricsUploader(topDir string, env *Environment) string {
1730 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1731 metricsUploader := filepath.Join(topDir, p)
1732 if _, err := os.Stat(metricsUploader); err == nil {
1733 return metricsUploader
1734 }
1735 }
1736
1737 return ""
1738}