blob: 711774a8a33ecff2e67659600f6ce238fc478c62 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Kousik Kumar3ff037e2022-01-25 22:11:01 -050018 "encoding/json"
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040019 "fmt"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050020 "io/ioutil"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040021 "math/rand"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080022 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050023 "os/exec"
Dan Willemsen1e704462016-08-21 15:17:17 -070024 "path/filepath"
25 "runtime"
26 "strconv"
27 "strings"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040028 "syscall"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080029 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070030
31 "android/soong/shared"
Kousik Kumarec478642020-09-21 13:39:24 -040032
Dan Willemsen4591b642021-05-24 14:24:12 -070033 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000034
35 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070036)
37
Kousik Kumar3ff037e2022-01-25 22:11:01 -050038const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050039 envConfigDir = "vendor/google/tools/soong_config"
40 jsonSuffix = "json"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050041)
42
Kousik Kumar4c180ad2022-05-27 07:48:37 -040043var (
Kevin Dagostino096ab2f2023-03-03 19:47:17 +000044 rbeRandPrefix int
45 googleProdCredsExistCache bool
Kousik Kumar4c180ad2022-05-27 07:48:37 -040046)
47
48func init() {
49 rand.Seed(time.Now().UnixNano())
50 rbeRandPrefix = rand.Intn(1000)
51}
52
Dan Willemsen1e704462016-08-21 15:17:17 -070053type Config struct{ *configImpl }
54
55type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020056 // Some targets that are implemented in soong_build
57 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080058 arguments []string
59 goma bool
60 environ *Environment
61 distDir string
62 buildDateTime string
MarkDacek6614d9c2022-12-07 21:57:38 +000063 logsPrefix string
Dan Willemsen1e704462016-08-21 15:17:17 -070064
65 // From the arguments
MarkDacekf47e1422023-04-19 16:47:36 +000066 parallel int
67 keepGoing int
68 verbose bool
69 checkbuild bool
70 dist bool
71 jsonModuleGraph bool
72 apiBp2build bool // Generate BUILD files for Soong modules that contribute APIs
73 bp2build bool
74 queryview bool
75 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
76 soongDocs bool
77 multitreeBuild bool // This is a multitree build.
78 skipConfig bool
79 skipKati bool
80 skipKatiNinja bool
81 skipSoong bool
82 skipNinja bool
83 skipSoongTests bool
84 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
85 skipMetricsUpload bool
86 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
87 buildFromTextStub bool
MarkDacekd33c2fd2023-05-04 20:40:04 +000088 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
89 bazelExitCode int32 // For b-runs - necessary for updating NonZeroExit
Dan Willemsen1e704462016-08-21 15:17:17 -070090
91 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070092 katiArgs []string
93 ninjaArgs []string
94 katiSuffix string
95 targetDevice string
96 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +000097 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -070098
Dan Willemsen2bb82d02019-12-27 09:35:42 -080099 // Autodetected
100 totalRAM uint64
101
Dan Willemsene3336352020-01-02 19:10:38 -0800102 brokenDupRules bool
103 brokenUsesNetwork bool
104 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -0700105
106 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000107
MarkDacekb78465d2022-10-18 20:10:16 +0000108 bazelProdMode bool
109 bazelDevMode bool
110 bazelStagingMode bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000111
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700112 // Set by multiproduct_kati
113 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700114
115 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000116
117 bazelForceEnabledModules string
Spandan Dasc5763832022-11-08 18:42:16 +0000118
Sam Delmerico98a73292023-02-21 11:50:29 -0500119 includeTags []string
120 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900121
122 // Data source to write ninja weight list
123 ninjaWeightListSource NinjaWeightListSource
Dan Willemsen1e704462016-08-21 15:17:17 -0700124}
125
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900126type NinjaWeightListSource uint
127
128const (
129 // ninja doesn't use weight list.
130 NOT_USED NinjaWeightListSource = iota
131 // ninja uses weight list based on previous builds by ninja log
132 NINJA_LOG
133 // ninja thinks every task has the same weight.
134 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900135 // ninja uses an external custom weight list
136 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900137 // ninja uses a prioritized module list from Soong
138 HINT_FROM_SOONG
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900139)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800140const srcDirFileCheck = "build/soong/root.bp"
141
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700142var buildFiles = []string{"Android.mk", "Android.bp"}
143
Patrice Arruda13848222019-04-22 17:12:02 -0700144type BuildAction uint
145
146const (
147 // Builds all of the modules and their dependencies of a specified directory, relative to the root
148 // directory of the source tree.
149 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
150
151 // Builds all of the modules and their dependencies of a list of specified directories. All specified
152 // directories are relative to the root directory of the source tree.
153 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700154
155 // Build a list of specified modules. If none was specified, simply build the whole source tree.
156 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700157)
158
159// checkTopDir validates that the current directory is at the root directory of the source tree.
160func checkTopDir(ctx Context) {
161 if _, err := os.Stat(srcDirFileCheck); err != nil {
162 if os.IsNotExist(err) {
163 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
164 }
165 ctx.Fatalln("Error verifying tree state:", err)
166 }
167}
168
MarkDacek7901e582023-01-09 19:48:01 +0000169func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500170 if bc == "" {
171 return nil
172 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500173
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500174 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500175 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500176 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500177 envConfigDir,
178 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500179 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500180 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
181 envVarsJSON, err := ioutil.ReadFile(cfgFile)
182 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500183 continue
184 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500185 ctx.Verbosef("Loading config file %v\n", cfgFile)
186 var envVars map[string]map[string]string
187 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
188 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
189 continue
190 }
191 for k, v := range envVars["env"] {
192 if os.Getenv(k) != "" {
193 continue
194 }
195 config.environ.Set(k, v)
196 }
197 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
198 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500199 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500200
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500201 return nil
202}
203
Chris Parsonsb6e96902022-10-31 20:08:45 -0400204func defaultBazelProdMode(cfg *configImpl) bool {
MarkDacekd06db5d2022-11-29 00:47:59 +0000205 // Environment flag to disable Bazel for users which experience
Chris Parsonsb6e96902022-10-31 20:08:45 -0400206 // broken bazel-handled builds, or significant performance regressions.
207 if cfg.IsBazelMixedBuildForceDisabled() {
208 return false
209 }
210 // Darwin-host builds are currently untested with Bazel.
211 if runtime.GOOS == "darwin" {
212 return false
213 }
Chris Parsons035e03a2022-11-01 14:25:45 -0400214 return true
Chris Parsonsb6e96902022-10-31 20:08:45 -0400215}
216
MarkDacekd33c2fd2023-05-04 20:40:04 +0000217func UploadOnlyConfig(ctx Context, args ...string) Config {
MarkDacek6614d9c2022-12-07 21:57:38 +0000218 ret := &configImpl{
219 environ: OsEnvironment(),
220 sandboxConfig: &SandboxConfig{},
221 }
MarkDacekd33c2fd2023-05-04 20:40:04 +0000222 ret.parseArgs(ctx, args)
MarkDacek7901e582023-01-09 19:48:01 +0000223 srcDir := absPath(ctx, ".")
224 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
225 if err := loadEnvConfig(ctx, ret, bc); err != nil {
226 ctx.Fatalln("Failed to parse env config files: %v", err)
227 }
228 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
MarkDacek6614d9c2022-12-07 21:57:38 +0000229 return Config{ret}
230}
231
Dan Willemsen1e704462016-08-21 15:17:17 -0700232func NewConfig(ctx Context, args ...string) Config {
233 ret := &configImpl{
Jeongik Chaf2ecf762023-05-19 14:03:45 +0900234 environ: OsEnvironment(),
235 sandboxConfig: &SandboxConfig{},
236 ninjaWeightListSource: NINJA_LOG,
Dan Willemsen1e704462016-08-21 15:17:17 -0700237 }
238
Patrice Arruda90109172020-07-28 18:07:27 +0000239 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700240 ret.parallel = runtime.NumCPU() + 2
241 ret.keepGoing = 1
242
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800243 ret.totalRAM = detectTotalRAM(ctx)
Dan Willemsen9b587492017-07-10 22:13:00 -0700244 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900245
246 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
247 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "true")
248 }
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800249 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700250 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
251 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
252 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800253 outDir := "out"
254 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
255 if wd, err := os.Getwd(); err != nil {
256 ctx.Fatalln("Failed to get working directory:", err)
257 } else {
258 outDir = filepath.Join(baseDir, filepath.Base(wd))
259 }
260 }
261 ret.environ.Set("OUT_DIR", outDir)
262 }
263
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500264 // loadEnvConfig needs to know what the OUT_DIR is, so it should
265 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000266 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
267
268 if bc != "" {
Kousik Kumarc8818332023-01-16 16:33:05 +0000269 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000270 ctx.Fatalln("Failed to parse env config files: %v", err)
271 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500272 }
273
Dan Willemsen2d31a442018-10-20 21:33:41 -0700274 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
275 ret.distDir = filepath.Clean(distDir)
276 } else {
277 ret.distDir = filepath.Join(ret.OutDir(), "dist")
278 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700279
Spandan Das05063612021-06-25 01:39:04 +0000280 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
281 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
282 }
283
Dan Willemsen1e704462016-08-21 15:17:17 -0700284 ret.environ.Unset(
285 // We're already using it
286 "USE_SOONG_UI",
287
288 // We should never use GOROOT/GOPATH from the shell environment
289 "GOROOT",
290 "GOPATH",
291
292 // These should only come from Soong, not the environment.
293 "CLANG",
294 "CLANG_CXX",
295 "CCC_CC",
296 "CCC_CXX",
297
298 // Used by the goma compiler wrapper, but should only be set by
299 // gomacc
300 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800301
302 // We handle this above
303 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700304
Dan Willemsen2d31a442018-10-20 21:33:41 -0700305 // This is handled above too, and set for individual commands later
306 "DIST_DIR",
307
Dan Willemsen68a09852017-04-18 13:56:57 -0700308 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000309 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700310 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700311 "DISPLAY",
312 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000313 "JAVAC",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700314 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700315 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700316
317 // Drop make flags
318 "MAKEFLAGS",
319 "MAKELEVEL",
320 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700321
322 // Set in envsetup.sh, reset in makefiles
323 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700324
325 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
326 "ANDROID_BUILD_TOP",
327 "ANDROID_HOST_OUT",
328 "ANDROID_PRODUCT_OUT",
329 "ANDROID_HOST_OUT_TESTCASES",
330 "ANDROID_TARGET_OUT_TESTCASES",
331 "ANDROID_TOOLCHAIN",
332 "ANDROID_TOOLCHAIN_2ND_ARCH",
333 "ANDROID_DEV_SCRIPTS",
334 "ANDROID_EMULATOR_PREBUILTS",
335 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700336 )
337
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400338 if ret.UseGoma() || ret.ForceUseGoma() {
339 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
340 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400341 }
342
Dan Willemsen1e704462016-08-21 15:17:17 -0700343 // Tell python not to spam the source tree with .pyc files.
344 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
345
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400346 tmpDir := absPath(ctx, ret.TempDir())
347 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800348
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700349 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
350 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
351 "llvm-binutils-stable/llvm-symbolizer")
352 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
353
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800354 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700355 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800356
Yu Liu6e13b402021-07-27 14:29:06 -0700357 srcDir := absPath(ctx, ".")
358 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700359 ctx.Println("You are building in a directory whose absolute path contains a space character:")
360 ctx.Println()
361 ctx.Printf("%q\n", srcDir)
362 ctx.Println()
363 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700364 }
365
Yu Liu6e13b402021-07-27 14:29:06 -0700366 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
367
Dan Willemsendb8457c2017-05-12 16:38:17 -0700368 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700369 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
370 ctx.Println()
371 ctx.Printf("%q\n", outDir)
372 ctx.Println()
373 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700374 }
375
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000376 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700377 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
378 ctx.Println()
379 ctx.Printf("%q\n", distDir)
380 ctx.Println()
381 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700382 }
383
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700384 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000385 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
386 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100387 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Colin Cross59c1e6a2022-03-04 13:37:19 -0800388 java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700389 javaHome := func() string {
390 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
391 return override
392 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000393 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
394 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 +0100395 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000396 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
397 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 17 toolchain is now the global default.")
398 }
399 return java17Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700400 }()
401 absJavaHome := absPath(ctx, javaHome)
402
Dan Willemsened869522018-01-08 14:58:46 -0800403 ret.configureLocale(ctx)
404
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700405 newPath := []string{filepath.Join(absJavaHome, "bin")}
406 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
407 newPath = append(newPath, path)
408 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100409
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700410 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
411 ret.environ.Set("JAVA_HOME", absJavaHome)
412 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000413 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
414 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100415 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700416 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
417
LaMont Jones52a72432023-03-09 18:19:35 +0000418 if ret.MultitreeBuild() {
419 ret.environ.Set("MULTITREE_BUILD", "true")
420 }
421
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800422 outDir := ret.OutDir()
423 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800424 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800425 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800426 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800427 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800428 }
Colin Cross28f527c2019-11-26 16:19:04 -0800429
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800430 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
431
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400432 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400433 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400434 ret.environ.Set(k, v)
435 }
436 }
437
Jihoon Kang1bff0342023-01-17 20:40:22 +0000438 if ret.BuildFromTextStub() {
439 // TODO(b/271443071): support hidden api check for from-text stub build
440 ret.environ.Set("UNSAFE_DISABLE_HIDDENAPI_FLAGS", "true")
441 }
442
Patrice Arruda83842d72020-12-08 19:42:08 +0000443 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800444 if err := os.RemoveAll(bpd); err != nil {
445 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
446 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000447
Patrice Arruda96850362020-08-11 20:41:11 +0000448 c := Config{ret}
449 storeConfigMetrics(ctx, c)
450 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700451}
452
Patrice Arruda13848222019-04-22 17:12:02 -0700453// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
454// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700455func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
456 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700457}
458
Patrice Arruda96850362020-08-11 20:41:11 +0000459// storeConfigMetrics selects a set of configuration information and store in
460// the metrics system for further analysis.
461func storeConfigMetrics(ctx Context, config Config) {
462 if ctx.Metrics == nil {
463 return
464 }
465
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400466 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000467
468 s := &smpb.SystemResourceInfo{
469 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
470 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
471 }
472 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000473}
474
Jeongik Cha8d63d562023-03-17 03:52:13 +0900475func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900476 switch s {
477 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900478 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900479 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900480 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900481 case EXTERNAL_FILE:
482 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900483 case HINT_FROM_SOONG:
484 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900485 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900486 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900487 }
488}
489
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400490func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700491 c := &smpb.BuildConfig{
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -0400492 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
493 UseGoma: proto.Bool(config.UseGoma()),
494 UseRbe: proto.Bool(config.UseRBE()),
495 BazelMixedBuild: proto.Bool(config.BazelBuildEnabled()),
496 ForceDisableBazelMixedBuild: proto.Bool(config.IsBazelMixedBuildForceDisabled()),
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900497 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400498 }
Yu Liue737a992021-10-04 13:21:41 -0700499 c.Targets = append(c.Targets, config.arguments...)
500
501 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400502}
503
Patrice Arruda13848222019-04-22 17:12:02 -0700504// getConfigArgs processes the command arguments based on the build action and creates a set of new
505// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700506func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700507 // The next block of code verifies that the current directory is the root directory of the source
508 // tree. It then finds the relative path of dir based on the root directory of the source tree
509 // and verify that dir is inside of the source tree.
510 checkTopDir(ctx)
511 topDir, err := os.Getwd()
512 if err != nil {
513 ctx.Fatalf("Error retrieving top directory: %v", err)
514 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700515 dir, err = filepath.EvalSymlinks(dir)
516 if err != nil {
517 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
518 }
Patrice Arruda13848222019-04-22 17:12:02 -0700519 dir, err = filepath.Abs(dir)
520 if err != nil {
521 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
522 }
523 relDir, err := filepath.Rel(topDir, dir)
524 if err != nil {
525 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
526 }
527 // If there are ".." in the path, it's not in the source tree.
528 if strings.Contains(relDir, "..") {
529 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
530 }
531
532 configArgs := args[:]
533
534 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
535 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
536 targetNamePrefix := "MODULES-IN-"
537 if inList("GET-INSTALL-PATH", configArgs) {
538 targetNamePrefix = "GET-INSTALL-PATH-IN-"
539 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
540 }
541
Patrice Arruda13848222019-04-22 17:12:02 -0700542 var targets []string
543
544 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700545 case BUILD_MODULES:
546 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700547 case BUILD_MODULES_IN_A_DIRECTORY:
548 // If dir is the root source tree, all the modules are built of the source tree are built so
549 // no need to find the build file.
550 if topDir == dir {
551 break
552 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700553
Patrice Arruda13848222019-04-22 17:12:02 -0700554 buildFile := findBuildFile(ctx, relDir)
555 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700556 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700557 }
Patrice Arruda13848222019-04-22 17:12:02 -0700558 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
559 case BUILD_MODULES_IN_DIRECTORIES:
560 newConfigArgs, dirs := splitArgs(configArgs)
561 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700562 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700563 }
564
565 // Tidy only override all other specified targets.
566 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
567 if tidyOnly == "true" || tidyOnly == "1" {
568 configArgs = append(configArgs, "tidy_only")
569 } else {
570 configArgs = append(configArgs, targets...)
571 }
572
573 return configArgs
574}
575
576// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
577func convertToTarget(dir string, targetNamePrefix string) string {
578 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
579}
580
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700581// hasBuildFile returns true if dir contains an Android build file.
582func hasBuildFile(ctx Context, dir string) bool {
583 for _, buildFile := range buildFiles {
584 _, err := os.Stat(filepath.Join(dir, buildFile))
585 if err == nil {
586 return true
587 }
588 if !os.IsNotExist(err) {
589 ctx.Fatalf("Error retrieving the build file stats: %v", err)
590 }
591 }
592 return false
593}
594
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700595// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
596// in the current and any sub directory of dir. If a build file is not found, traverse the path
597// up by one directory and repeat again until either a build file is found or reached to the root
598// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
599// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700600func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700601 // If the string is empty or ".", assume it is top directory of the source tree.
602 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700603 return ""
604 }
605
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700606 found := false
607 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
608 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
609 if err != nil {
610 return err
611 }
612 if found {
613 return filepath.SkipDir
614 }
615 if info.IsDir() {
616 return nil
617 }
618 for _, buildFile := range buildFiles {
619 if info.Name() == buildFile {
620 found = true
621 return filepath.SkipDir
622 }
623 }
624 return nil
625 })
626 if err != nil {
627 ctx.Fatalf("Error finding Android build file: %v", err)
628 }
629
630 if found {
631 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700632 }
633 }
634
635 return ""
636}
637
638// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
639func splitArgs(args []string) (newArgs []string, dirs []string) {
640 specialArgs := map[string]bool{
641 "showcommands": true,
642 "snod": true,
643 "dist": true,
644 "checkbuild": true,
645 }
646
647 newArgs = []string{}
648 dirs = []string{}
649
650 for _, arg := range args {
651 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
652 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
653 newArgs = append(newArgs, arg)
654 continue
655 }
656
657 if _, ok := specialArgs[arg]; ok {
658 newArgs = append(newArgs, arg)
659 continue
660 }
661
662 dirs = append(dirs, arg)
663 }
664
665 return newArgs, dirs
666}
667
668// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
669// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
670// source root tree where the build action command was invoked. Each directory is validated if the
671// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700672func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700673 for _, dir := range dirs {
674 // The directory may have specified specific modules to build. ":" is the separator to separate
675 // the directory and the list of modules.
676 s := strings.Split(dir, ":")
677 l := len(s)
678 if l > 2 { // more than one ":" was specified.
679 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
680 }
681
682 dir = filepath.Join(relDir, s[0])
683 if _, err := os.Stat(dir); err != nil {
684 ctx.Fatalf("couldn't find directory %s", dir)
685 }
686
687 // Verify that if there are any targets specified after ":". Each target is separated by ",".
688 var newTargets []string
689 if l == 2 && s[1] != "" {
690 newTargets = strings.Split(s[1], ",")
691 if inList("", newTargets) {
692 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
693 }
694 }
695
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700696 // If there are specified targets to build in dir, an android build file must exist for the one
697 // shot build. For the non-targets case, find the appropriate build file and build all the
698 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700699 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700700 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700701 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
702 }
703 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700704 buildFile := findBuildFile(ctx, dir)
705 if buildFile == "" {
706 ctx.Fatalf("Build file not found for %s directory", dir)
707 }
708 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700709 }
710
Patrice Arruda13848222019-04-22 17:12:02 -0700711 targets = append(targets, newTargets...)
712 }
713
Dan Willemsence41e942019-07-29 23:39:30 -0700714 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700715}
716
Dan Willemsen9b587492017-07-10 22:13:00 -0700717func (c *configImpl) parseArgs(ctx Context, args []string) {
718 for i := 0; i < len(args); i++ {
719 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100720 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700721 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200722 } else if arg == "--empty-ninja-file" {
723 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100724 } else if arg == "--skip-ninja" {
725 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700726 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700727 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
728 // but it does run a Kati ninja file if the .kati_enabled marker file was created
729 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000730 c.skipConfig = true
731 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100732 } else if arg == "--soong-only" {
733 c.skipKati = true
734 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200735 } else if arg == "--config-only" {
736 c.skipKati = true
737 c.skipKatiNinja = true
738 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700739 } else if arg == "--skip-config" {
740 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700741 } else if arg == "--skip-soong-tests" {
742 c.skipSoongTests = true
MarkDacekd0e7cd32022-12-02 22:22:40 +0000743 } else if arg == "--skip-metrics-upload" {
744 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500745 } else if arg == "--mk-metrics" {
746 c.reportMkMetrics = true
LaMont Jones52a72432023-03-09 18:19:35 +0000747 } else if arg == "--multitree-build" {
748 c.multitreeBuild = true
Chris Parsonsef615e52022-08-18 22:04:11 -0400749 } else if arg == "--bazel-mode" {
750 c.bazelProdMode = true
751 } else if arg == "--bazel-mode-dev" {
752 c.bazelDevMode = true
MarkDacekb78465d2022-10-18 20:10:16 +0000753 } else if arg == "--bazel-mode-staging" {
754 c.bazelStagingMode = true
Spandan Das394aa322022-11-03 17:02:10 +0000755 } else if arg == "--search-api-dir" {
756 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900757 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
758 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
759 if source == "ninja_log" {
760 c.ninjaWeightListSource = NINJA_LOG
761 } else if source == "evenly_distributed" {
762 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
763 } else if source == "not_used" {
764 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900765 } else if source == "soong" {
766 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900767 } else if strings.HasPrefix(source, "file,") {
768 c.ninjaWeightListSource = EXTERNAL_FILE
769 filePath := strings.TrimPrefix(source, "file,")
770 err := validateNinjaWeightList(filePath)
771 if err != nil {
772 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
773 }
774 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
775 if err != nil {
776 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
777 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900778 } else {
779 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
780 }
Jihoon Kang1bff0342023-01-17 20:40:22 +0000781 } else if arg == "--build-from-text-stub" {
782 c.buildFromTextStub = true
MarkDacekb96561e2022-12-02 04:34:43 +0000783 } else if strings.HasPrefix(arg, "--build-command=") {
784 buildCmd := strings.TrimPrefix(arg, "--build-command=")
785 // remove quotations
786 buildCmd = strings.TrimPrefix(buildCmd, "\"")
787 buildCmd = strings.TrimSuffix(buildCmd, "\"")
788 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacekd06db5d2022-11-29 00:47:59 +0000789 } else if strings.HasPrefix(arg, "--bazel-force-enabled-modules=") {
790 c.bazelForceEnabledModules = strings.TrimPrefix(arg, "--bazel-force-enabled-modules=")
MarkDacek6614d9c2022-12-07 21:57:38 +0000791 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
792 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
793 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
794 if err == nil {
795 c.buildStartedTime = val
796 } else {
797 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
798 }
MarkDacekf47e1422023-04-19 16:47:36 +0000799 } else if arg == "--ensure-allowlist-integrity" {
800 c.ensureAllowlistIntegrity = true
MarkDacekd33c2fd2023-05-04 20:40:04 +0000801 } else if strings.HasPrefix(arg, "--bazel-exit-code=") {
802 bazelExitCodeStr := strings.TrimPrefix(arg, "--bazel-exit-code=")
803 val, err := strconv.Atoi(bazelExitCodeStr)
804 if err == nil {
805 c.bazelExitCode = int32(val)
806 } else {
807 ctx.Fatalf("Error parsing bazel-exit-code", err)
808 }
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700809 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700810 parseArgNum := func(def int) int {
811 if len(arg) > 2 {
812 p, err := strconv.ParseUint(arg[2:], 10, 31)
813 if err != nil {
814 ctx.Fatalf("Failed to parse %q: %v", arg, err)
815 }
816 return int(p)
817 } else if i+1 < len(args) {
818 p, err := strconv.ParseUint(args[i+1], 10, 31)
819 if err == nil {
820 i++
821 return int(p)
822 }
823 }
824 return def
825 }
826
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700827 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700828 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700829 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700830 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700831 } else {
832 ctx.Fatalln("Unknown option:", arg)
833 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700834 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700835 if k == "OUT_DIR" {
836 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
837 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700838 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700839 } else if arg == "dist" {
840 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200841 } else if arg == "json-module-graph" {
842 c.jsonModuleGraph = true
843 } else if arg == "bp2build" {
844 c.bp2build = true
Spandan Das5af0bd32022-09-28 20:43:08 +0000845 } else if arg == "api_bp2build" {
846 c.apiBp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200847 } else if arg == "queryview" {
848 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200849 } else if arg == "soong_docs" {
850 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700851 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700852 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800853 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700854 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700855 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700856 }
857 }
Chris Parsonsb6e96902022-10-31 20:08:45 -0400858 if (!c.bazelProdMode) && (!c.bazelDevMode) && (!c.bazelStagingMode) {
859 c.bazelProdMode = defaultBazelProdMode(c)
860 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700861}
862
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900863func validateNinjaWeightList(weightListFilePath string) (err error) {
864 data, err := os.ReadFile(weightListFilePath)
865 if err != nil {
866 return
867 }
868 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
869 for _, line := range lines {
870 fields := strings.Split(line, ",")
871 if len(fields) != 2 {
872 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
873 }
874 _, err = strconv.Atoi(fields[1])
875 if err != nil {
876 return
877 }
878 }
879 return
880}
881
Dan Willemsened869522018-01-08 14:58:46 -0800882func (c *configImpl) configureLocale(ctx Context) {
883 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
884 output, err := cmd.Output()
885
886 var locales []string
887 if err == nil {
888 locales = strings.Split(string(output), "\n")
889 } else {
890 // If we're unable to list the locales, let's assume en_US.UTF-8
891 locales = []string{"en_US.UTF-8"}
892 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
893 }
894
895 // gettext uses LANGUAGE, which is passed directly through
896
897 // For LANG and LC_*, only preserve the evaluated version of
898 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800899 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800900 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800901 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800902 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800903 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800904 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800905 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800906 }
907
908 c.environ.UnsetWithPrefix("LC_")
909
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800910 if userLang != "" {
911 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800912 }
913
914 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
915 // for others)
916 if inList("C.UTF-8", locales) {
917 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500918 } else if inList("C.utf8", locales) {
919 // These normalize to the same thing
920 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800921 } else if inList("en_US.UTF-8", locales) {
922 c.environ.Set("LANG", "en_US.UTF-8")
923 } else if inList("en_US.utf8", locales) {
924 // These normalize to the same thing
925 c.environ.Set("LANG", "en_US.UTF-8")
926 } else {
927 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
928 }
929}
930
Dan Willemsen1e704462016-08-21 15:17:17 -0700931func (c *configImpl) Environment() *Environment {
932 return c.environ
933}
934
935func (c *configImpl) Arguments() []string {
936 return c.arguments
937}
938
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200939func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200940 if len(c.Arguments()) > 0 {
941 // Explicit targets requested that are not special targets like b2pbuild
942 // or the JSON module graph
943 return true
944 }
945
Spandan Das5af0bd32022-09-28 20:43:08 +0000946 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() && !c.ApiBp2build() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200947 // Command line was empty, the default Ninja target is built
948 return true
949 }
950
Liz Kammer88677422021-12-15 15:03:19 -0500951 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
952 if c.Dist() && !c.Bp2Build() {
953 return true
954 }
955
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200956 // build.ninja doesn't need to be generated
957 return false
958}
959
Dan Willemsen1e704462016-08-21 15:17:17 -0700960func (c *configImpl) OutDir() string {
961 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700962 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700963 }
964 return "out"
965}
966
Dan Willemsen8a073a82017-02-04 17:30:44 -0800967func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -0400968 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000969}
970
971func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700972 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800973}
974
Dan Willemsen1e704462016-08-21 15:17:17 -0700975func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000976 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700977 return c.arguments
978 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700979 return c.ninjaArgs
980}
981
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500982func (c *configImpl) BazelOutDir() string {
983 return filepath.Join(c.OutDir(), "bazel")
984}
985
Liz Kammer2af5ea82022-11-11 14:21:03 -0500986func (c *configImpl) bazelOutputBase() string {
987 return filepath.Join(c.BazelOutDir(), "output")
988}
989
Dan Willemsen1e704462016-08-21 15:17:17 -0700990func (c *configImpl) SoongOutDir() string {
991 return filepath.Join(c.OutDir(), "soong")
992}
993
Spandan Das394aa322022-11-03 17:02:10 +0000994func (c *configImpl) ApiSurfacesOutDir() string {
995 return filepath.Join(c.OutDir(), "api_surfaces")
996}
997
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200998func (c *configImpl) PrebuiltOS() string {
999 switch runtime.GOOS {
1000 case "linux":
1001 return "linux-x86"
1002 case "darwin":
1003 return "darwin-x86"
1004 default:
1005 panic("Unknown GOOS")
1006 }
1007}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001008
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001009func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001010 if c.SkipKatiNinja() {
1011 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1012 } else {
1013 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1014 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001015}
1016
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001017func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001018 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +02001019}
1020
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001021func (c *configImpl) UsedEnvFile(tag string) string {
1022 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1023}
1024
Lukacs T. Berkic541cd22022-10-26 07:26:50 +00001025func (c *configImpl) Bp2BuildFilesMarkerFile() string {
1026 return shared.JoinPath(c.SoongOutDir(), "bp2build_files_marker")
1027}
1028
1029func (c *configImpl) Bp2BuildWorkspaceMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001030 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +02001031}
1032
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001033func (c *configImpl) SoongDocsHtml() string {
1034 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1035}
1036
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001037func (c *configImpl) QueryviewMarkerFile() string {
1038 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1039}
1040
Spandan Das5af0bd32022-09-28 20:43:08 +00001041func (c *configImpl) ApiBp2buildMarkerFile() string {
1042 return shared.JoinPath(c.SoongOutDir(), "api_bp2build.marker")
1043}
1044
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001045func (c *configImpl) ModuleGraphFile() string {
1046 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1047}
1048
kgui67007242022-01-25 13:50:25 +08001049func (c *configImpl) ModuleActionsFile() string {
1050 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1051}
1052
Jeff Gastonefc1b412017-03-29 17:29:06 -07001053func (c *configImpl) TempDir() string {
1054 return shared.TempDirForOutDir(c.SoongOutDir())
1055}
1056
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001057func (c *configImpl) FileListDir() string {
1058 return filepath.Join(c.OutDir(), ".module_paths")
1059}
1060
Dan Willemsen1e704462016-08-21 15:17:17 -07001061func (c *configImpl) KatiSuffix() string {
1062 if c.katiSuffix != "" {
1063 return c.katiSuffix
1064 }
1065 panic("SetKatiSuffix has not been called")
1066}
1067
Colin Cross37193492017-11-16 17:55:00 -08001068// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1069// user is interested in additional checks at the expense of build time.
1070func (c *configImpl) Checkbuild() bool {
1071 return c.checkbuild
1072}
1073
Dan Willemsen8a073a82017-02-04 17:30:44 -08001074func (c *configImpl) Dist() bool {
1075 return c.dist
1076}
1077
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001078func (c *configImpl) JsonModuleGraph() bool {
1079 return c.jsonModuleGraph
1080}
1081
1082func (c *configImpl) Bp2Build() bool {
1083 return c.bp2build
1084}
1085
Spandan Das5af0bd32022-09-28 20:43:08 +00001086func (c *configImpl) ApiBp2build() bool {
1087 return c.apiBp2build
1088}
1089
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001090func (c *configImpl) Queryview() bool {
1091 return c.queryview
1092}
1093
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001094func (c *configImpl) SoongDocs() bool {
1095 return c.soongDocs
1096}
1097
Dan Willemsen1e704462016-08-21 15:17:17 -07001098func (c *configImpl) IsVerbose() bool {
1099 return c.verbose
1100}
1101
LaMont Jones52a72432023-03-09 18:19:35 +00001102func (c *configImpl) MultitreeBuild() bool {
1103 return c.multitreeBuild
1104}
1105
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001106func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1107 return c.ninjaWeightListSource
1108}
1109
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001110func (c *configImpl) SkipKati() bool {
1111 return c.skipKati
1112}
1113
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001114func (c *configImpl) SkipKatiNinja() bool {
1115 return c.skipKatiNinja
1116}
1117
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001118func (c *configImpl) SkipSoong() bool {
1119 return c.skipSoong
1120}
1121
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001122func (c *configImpl) SkipNinja() bool {
1123 return c.skipNinja
1124}
1125
Anton Hansson5a7861a2021-06-04 10:09:01 +01001126func (c *configImpl) SetSkipNinja(v bool) {
1127 c.skipNinja = v
1128}
1129
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001130func (c *configImpl) SkipConfig() bool {
1131 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001132}
1133
Jihoon Kang1bff0342023-01-17 20:40:22 +00001134func (c *configImpl) BuildFromTextStub() bool {
1135 return c.buildFromTextStub
1136}
1137
Dan Willemsen1e704462016-08-21 15:17:17 -07001138func (c *configImpl) TargetProduct() string {
1139 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1140 return v
1141 }
1142 panic("TARGET_PRODUCT is not defined")
1143}
1144
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001145func (c *configImpl) TargetProductOrErr() (string, error) {
1146 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1147 return v, nil
1148 }
1149 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1150}
1151
Dan Willemsen02781d52017-05-12 19:28:13 -07001152func (c *configImpl) TargetDevice() string {
1153 return c.targetDevice
1154}
1155
1156func (c *configImpl) SetTargetDevice(device string) {
1157 c.targetDevice = device
1158}
1159
1160func (c *configImpl) TargetBuildVariant() string {
1161 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1162 return v
1163 }
1164 panic("TARGET_BUILD_VARIANT is not defined")
1165}
1166
Dan Willemsen1e704462016-08-21 15:17:17 -07001167func (c *configImpl) KatiArgs() []string {
1168 return c.katiArgs
1169}
1170
1171func (c *configImpl) Parallel() int {
1172 return c.parallel
1173}
1174
Sam Delmerico98a73292023-02-21 11:50:29 -05001175func (c *configImpl) GetSourceRootDirs() []string {
1176 return c.sourceRootDirs
1177}
1178
1179func (c *configImpl) SetSourceRootDirs(i []string) {
1180 c.sourceRootDirs = i
1181}
1182
Spandan Dasc5763832022-11-08 18:42:16 +00001183func (c *configImpl) GetIncludeTags() []string {
1184 return c.includeTags
1185}
1186
1187func (c *configImpl) SetIncludeTags(i []string) {
1188 c.includeTags = i
1189}
1190
MarkDacek6614d9c2022-12-07 21:57:38 +00001191func (c *configImpl) GetLogsPrefix() string {
1192 return c.logsPrefix
1193}
1194
1195func (c *configImpl) SetLogsPrefix(prefix string) {
1196 c.logsPrefix = prefix
1197}
1198
Colin Cross8b8bec32019-11-15 13:18:43 -08001199func (c *configImpl) HighmemParallel() int {
1200 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1201 return i
1202 }
1203
1204 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1205 parallel := c.Parallel()
1206 if c.UseRemoteBuild() {
1207 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1208 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1209 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1210 // Return 1/16th of the size of the local pool, rounding up.
1211 return (parallel + 15) / 16
1212 } else if c.totalRAM == 0 {
1213 // Couldn't detect the total RAM, don't restrict highmem processes.
1214 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001215 } else if c.totalRAM <= 16*1024*1024*1024 {
1216 // Less than 16GB of ram, restrict to 1 highmem processes
1217 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001218 } else if c.totalRAM <= 32*1024*1024*1024 {
1219 // Less than 32GB of ram, restrict to 2 highmem processes
1220 return 2
1221 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1222 // If less than 8GB total RAM per process, reduce the number of highmem processes
1223 return p
1224 }
1225 // No restriction on highmem processes
1226 return parallel
1227}
1228
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001229func (c *configImpl) TotalRAM() uint64 {
1230 return c.totalRAM
1231}
1232
Kousik Kumarec478642020-09-21 13:39:24 -04001233// ForceUseGoma determines whether we should override Goma deprecation
1234// and use Goma for the current build or not.
1235func (c *configImpl) ForceUseGoma() bool {
1236 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1237 v = strings.TrimSpace(v)
1238 if v != "" && v != "false" {
1239 return true
1240 }
1241 }
1242 return false
1243}
1244
Dan Willemsen1e704462016-08-21 15:17:17 -07001245func (c *configImpl) UseGoma() bool {
1246 if v, ok := c.environ.Get("USE_GOMA"); ok {
1247 v = strings.TrimSpace(v)
1248 if v != "" && v != "false" {
1249 return true
1250 }
1251 }
1252 return false
1253}
1254
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001255func (c *configImpl) StartGoma() bool {
1256 if !c.UseGoma() {
1257 return false
1258 }
1259
1260 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1261 v = strings.TrimSpace(v)
1262 if v != "" && v != "false" {
1263 return false
1264 }
1265 }
1266 return true
1267}
1268
Ramy Medhatbbf25672019-07-17 12:30:04 +00001269func (c *configImpl) UseRBE() bool {
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001270 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001271 v = strings.TrimSpace(v)
1272 if v != "" && v != "false" {
1273 return true
1274 }
1275 }
1276 return false
1277}
1278
Chris Parsonsef615e52022-08-18 22:04:11 -04001279func (c *configImpl) BazelBuildEnabled() bool {
MarkDacekb78465d2022-10-18 20:10:16 +00001280 return c.bazelProdMode || c.bazelDevMode || c.bazelStagingMode
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001281}
1282
Ramy Medhatbbf25672019-07-17 12:30:04 +00001283func (c *configImpl) StartRBE() bool {
1284 if !c.UseRBE() {
1285 return false
1286 }
1287
1288 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1289 v = strings.TrimSpace(v)
1290 if v != "" && v != "false" {
1291 return false
1292 }
1293 }
1294 return true
1295}
1296
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001297func (c *configImpl) rbeProxyLogsDir() string {
1298 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001299 if v, ok := c.environ.Get(f); ok {
1300 return v
1301 }
1302 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001303 buildTmpDir := shared.TempDirForOutDir(c.SoongOutDir())
1304 return filepath.Join(buildTmpDir, "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001305}
1306
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001307func (c *configImpl) rbeCacheDir() string {
1308 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1309 if v, ok := c.environ.Get(f); ok {
1310 return v
1311 }
1312 }
1313 return shared.JoinPath(c.SoongOutDir(), "rbe")
1314}
1315
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001316func (c *configImpl) shouldCleanupRBELogsDir() bool {
1317 // Perform a log directory cleanup only when the log directory
1318 // is auto created by the build rather than user-specified.
1319 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
1320 if _, ok := c.environ.Get(f); ok {
1321 return false
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001322 }
1323 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001324 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001325}
1326
1327func (c *configImpl) rbeExecRoot() string {
1328 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1329 if v, ok := c.environ.Get(f); ok {
1330 return v
1331 }
1332 }
1333 wd, err := os.Getwd()
1334 if err != nil {
1335 return ""
1336 }
1337 return wd
1338}
1339
1340func (c *configImpl) rbeDir() string {
1341 if v, ok := c.environ.Get("RBE_DIR"); ok {
1342 return v
1343 }
1344 return "prebuilts/remoteexecution-client/live/"
1345}
1346
1347func (c *configImpl) rbeReproxy() string {
1348 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1349 if v, ok := c.environ.Get(f); ok {
1350 return v
1351 }
1352 }
1353 return filepath.Join(c.rbeDir(), "reproxy")
1354}
1355
1356func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001357 credFlags := []string{
1358 "use_application_default_credentials",
1359 "use_gce_credentials",
1360 "credential_file",
1361 "use_google_prod_creds",
1362 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001363 for _, cf := range credFlags {
1364 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1365 if v, ok := c.environ.Get(f); ok {
1366 v = strings.TrimSpace(v)
1367 if v != "" && v != "false" && v != "0" {
1368 return "RBE_" + cf, v
1369 }
1370 }
1371 }
1372 }
1373 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001374}
1375
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001376func (c *configImpl) rbeSockAddr(dir string) (string, error) {
1377 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
1378 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1379
1380 name := filepath.Join(dir, base)
1381 if len(name) < maxNameLen {
1382 return name, nil
1383 }
1384
1385 name = filepath.Join("/tmp", base)
1386 if len(name) < maxNameLen {
1387 return name, nil
1388 }
1389
1390 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1391}
1392
Kousik Kumar7bc78192022-04-27 14:52:56 -04001393// IsGooglerEnvironment returns true if the current build is running
1394// on a Google developer machine and false otherwise.
1395func (c *configImpl) IsGooglerEnvironment() bool {
1396 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1397 if v, ok := c.environ.Get(cf); ok {
1398 return v == "googler"
1399 }
1400 return false
1401}
1402
1403// GoogleProdCredsExist determine whether credentials exist on the
1404// Googler machine to use remote execution.
1405func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001406 if googleProdCredsExistCache {
1407 return googleProdCredsExistCache
1408 }
Saagar Jhaf513b232023-05-26 00:17:55 +00001409 if _, err := exec.Command("/usr/bin/gcertstatus").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001410 return false
1411 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001412 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001413 return true
1414}
1415
1416// UseRemoteBuild indicates whether to use a remote build acceleration system
1417// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001418func (c *configImpl) UseRemoteBuild() bool {
1419 return c.UseGoma() || c.UseRBE()
1420}
1421
Kousik Kumar7bc78192022-04-27 14:52:56 -04001422// StubbyExists checks whether the stubby binary exists on the machine running
1423// the build.
1424func (c *configImpl) StubbyExists() bool {
1425 if _, err := exec.LookPath("stubby"); err != nil {
1426 return false
1427 }
1428 return true
1429}
1430
Dan Willemsen1e704462016-08-21 15:17:17 -07001431// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001432// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001433// still limited by Parallel()
1434func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001435 if !c.UseRemoteBuild() {
1436 return 0
1437 }
1438 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1439 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001440 }
1441 return 500
1442}
1443
1444func (c *configImpl) SetKatiArgs(args []string) {
1445 c.katiArgs = args
1446}
1447
1448func (c *configImpl) SetNinjaArgs(args []string) {
1449 c.ninjaArgs = args
1450}
1451
1452func (c *configImpl) SetKatiSuffix(suffix string) {
1453 c.katiSuffix = suffix
1454}
1455
Dan Willemsene0879fc2017-08-04 15:06:27 -07001456func (c *configImpl) LastKatiSuffixFile() string {
1457 return filepath.Join(c.OutDir(), "last_kati_suffix")
1458}
1459
1460func (c *configImpl) HasKatiSuffix() bool {
1461 return c.katiSuffix != ""
1462}
1463
Dan Willemsen1e704462016-08-21 15:17:17 -07001464func (c *configImpl) KatiEnvFile() string {
1465 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1466}
1467
Dan Willemsen29971232018-09-26 14:58:30 -07001468func (c *configImpl) KatiBuildNinjaFile() string {
1469 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001470}
1471
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001472func (c *configImpl) KatiPackageNinjaFile() string {
1473 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1474}
1475
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001476func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001477 targetProduct, err := c.TargetProductOrErr()
1478 if err != nil {
1479 return filepath.Join(c.SoongOutDir(), "soong.variables")
1480 } else {
1481 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+".variables")
1482 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001483}
1484
Dan Willemsen1e704462016-08-21 15:17:17 -07001485func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001486 targetProduct, err := c.TargetProductOrErr()
1487 if err != nil {
1488 return filepath.Join(c.SoongOutDir(), "build.ninja")
1489 } else {
1490 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+".ninja")
1491 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001492}
1493
1494func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001495 if c.katiSuffix == "" {
1496 return filepath.Join(c.OutDir(), "combined.ninja")
1497 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001498 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1499}
1500
1501func (c *configImpl) SoongAndroidMk() string {
1502 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1503}
1504
1505func (c *configImpl) SoongMakeVarsMk() string {
1506 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1507}
1508
Dan Willemsenf052f782017-05-18 15:29:04 -07001509func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001510 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001511}
1512
Dan Willemsen02781d52017-05-12 19:28:13 -07001513func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001514 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1515}
1516
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001517func (c *configImpl) KatiPackageMkDir() string {
1518 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1519}
1520
Dan Willemsenf052f782017-05-18 15:29:04 -07001521func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001522 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001523}
1524
1525func (c *configImpl) HostOut() string {
1526 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1527}
1528
1529// This probably needs to be multi-valued, so not exporting it for now
1530func (c *configImpl) hostCrossOut() string {
1531 if runtime.GOOS == "linux" {
1532 return filepath.Join(c.hostOutRoot(), "windows-x86")
1533 } else {
1534 return ""
1535 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001536}
1537
Dan Willemsen1e704462016-08-21 15:17:17 -07001538func (c *configImpl) HostPrebuiltTag() string {
1539 if runtime.GOOS == "linux" {
1540 return "linux-x86"
1541 } else if runtime.GOOS == "darwin" {
1542 return "darwin-x86"
1543 } else {
1544 panic("Unsupported OS")
1545 }
1546}
Dan Willemsenf173d592017-04-27 14:28:00 -07001547
Dan Willemsen8122bd52017-10-12 20:20:41 -07001548func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001549 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1550 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001551 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1552 if _, err := os.Stat(asan); err == nil {
1553 return asan
1554 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001555 }
1556 }
1557 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1558}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001559
1560func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1561 c.brokenDupRules = val
1562}
1563
1564func (c *configImpl) BuildBrokenDupRules() bool {
1565 return c.brokenDupRules
1566}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001567
Dan Willemsen25e6f092019-04-09 10:22:43 -07001568func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1569 c.brokenUsesNetwork = val
1570}
1571
1572func (c *configImpl) BuildBrokenUsesNetwork() bool {
1573 return c.brokenUsesNetwork
1574}
1575
Dan Willemsene3336352020-01-02 19:10:38 -08001576func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1577 c.brokenNinjaEnvVars = val
1578}
1579
1580func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1581 return c.brokenNinjaEnvVars
1582}
1583
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001584func (c *configImpl) SetTargetDeviceDir(dir string) {
1585 c.targetDeviceDir = dir
1586}
1587
1588func (c *configImpl) TargetDeviceDir() string {
1589 return c.targetDeviceDir
1590}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001591
Patrice Arruda219eef32020-06-01 17:29:30 +00001592func (c *configImpl) BuildDateTime() string {
1593 return c.buildDateTime
1594}
1595
1596func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001597 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001598}
Patrice Arruda83842d72020-12-08 19:42:08 +00001599
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001600// LogsDir returns the absolute path to the logs directory where build log and
1601// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001602// directory. If the argument dist is specified, the logs directory
1603// is <dist_dir>/logs.
1604func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001605 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001606 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001607 // 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 -05001608 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001609 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001610 absDir, err := filepath.Abs(dir)
1611 if err != nil {
1612 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1613 os.Exit(1)
1614 }
1615 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001616}
1617
1618// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1619// where the bazel profiles are located.
1620func (c *configImpl) BazelMetricsDir() string {
1621 return filepath.Join(c.LogsDir(), "bazel_metrics")
1622}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001623
Chris Parsons53f68ae2022-03-03 12:01:40 -05001624// MkFileMetrics returns the file path for make-related metrics.
1625func (c *configImpl) MkMetrics() string {
1626 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1627}
1628
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001629func (c *configImpl) SetEmptyNinjaFile(v bool) {
1630 c.emptyNinjaFile = v
1631}
1632
1633func (c *configImpl) EmptyNinjaFile() bool {
1634 return c.emptyNinjaFile
1635}
Yu Liu6e13b402021-07-27 14:29:06 -07001636
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -04001637func (c *configImpl) IsBazelMixedBuildForceDisabled() bool {
1638 return c.Environment().IsEnvTrue("BUILD_BROKEN_DISABLE_BAZEL")
1639}
1640
Chris Parsons9402ca82023-02-23 17:28:06 -05001641func (c *configImpl) IsPersistentBazelEnabled() bool {
1642 return c.Environment().IsEnvTrue("USE_PERSISTENT_BAZEL")
1643}
1644
Chris Parsonsc83398f2023-05-31 18:41:41 +00001645// GetBazeliskBazelVersion returns the Bazel version to use for this build,
1646// or the empty string if the current canonical prod Bazel should be used.
1647// This environment variable should only be set to debug the build system.
1648// The Bazel version, if set, will be passed to Bazelisk, and Bazelisk will
1649// handle downloading and invoking the correct Bazel binary.
1650func (c *configImpl) GetBazeliskBazelVersion() string {
1651 value, _ := c.Environment().Get("USE_BAZEL_VERSION")
1652 return value
1653}
1654
MarkDacekd06db5d2022-11-29 00:47:59 +00001655func (c *configImpl) BazelModulesForceEnabledByFlag() string {
1656 return c.bazelForceEnabledModules
1657}
1658
MarkDacekd0e7cd32022-12-02 22:22:40 +00001659func (c *configImpl) SkipMetricsUpload() bool {
1660 return c.skipMetricsUpload
1661}
1662
MarkDacekf47e1422023-04-19 16:47:36 +00001663func (c *configImpl) EnsureAllowlistIntegrity() bool {
1664 return c.ensureAllowlistIntegrity
1665}
1666
MarkDacek6614d9c2022-12-07 21:57:38 +00001667// Returns a Time object if one was passed via a command-line flag.
1668// Otherwise returns the passed default.
1669func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1670 if c.buildStartedTime == 0 {
1671 return defaultTime
1672 }
1673 return time.UnixMilli(c.buildStartedTime)
1674}
1675
MarkDacekd33c2fd2023-05-04 20:40:04 +00001676func (c *configImpl) BazelExitCode() int32 {
1677 return c.bazelExitCode
1678}
1679
Yu Liu6e13b402021-07-27 14:29:06 -07001680func GetMetricsUploader(topDir string, env *Environment) string {
1681 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1682 metricsUploader := filepath.Join(topDir, p)
1683 if _, err := os.Stat(metricsUploader); err == nil {
1684 return metricsUploader
1685 }
1686 }
1687
1688 return ""
1689}