blob: d72c8fa016548216cfa06e046552ad419ed0e248 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Kousik Kumar3ff037e2022-01-25 22:11:01 -050018 "encoding/json"
Jeongik Chaa87506f2023-06-01 23:16:41 +090019 "errors"
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040020 "fmt"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050021 "io/ioutil"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040022 "math/rand"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080023 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050024 "os/exec"
Cole Faust583dfb42023-09-28 13:56:30 -070025 "os/user"
Dan Willemsen1e704462016-08-21 15:17:17 -070026 "path/filepath"
27 "runtime"
28 "strconv"
29 "strings"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040030 "syscall"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080031 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070032
33 "android/soong/shared"
LaMont Jones9a912862023-11-06 22:11:08 +000034 "android/soong/ui/metrics"
Kousik Kumarec478642020-09-21 13:39:24 -040035
Dan Willemsen4591b642021-05-24 14:24:12 -070036 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000037
38 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070039)
40
Kousik Kumar3ff037e2022-01-25 22:11:01 -050041const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050042 envConfigDir = "vendor/google/tools/soong_config"
43 jsonSuffix = "json"
Taylor Santiago8b0bed72024-09-03 13:30:22 -070044 abfsSrcDir = "/src"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050045)
46
Kousik Kumar4c180ad2022-05-27 07:48:37 -040047var (
Kevin Dagostino096ab2f2023-03-03 19:47:17 +000048 rbeRandPrefix int
49 googleProdCredsExistCache bool
Kousik Kumar4c180ad2022-05-27 07:48:37 -040050)
51
52func init() {
53 rand.Seed(time.Now().UnixNano())
54 rbeRandPrefix = rand.Intn(1000)
55}
56
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
MarkDacekf47e1422023-04-19 16:47:36 +000076 queryview bool
77 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
78 soongDocs bool
MarkDacekf47e1422023-04-19 16:47:36 +000079 skipConfig bool
80 skipKati bool
81 skipKatiNinja bool
82 skipSoong bool
83 skipNinja bool
84 skipSoongTests bool
85 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
86 skipMetricsUpload bool
87 buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time
Jihoon Kang2a929ad2023-06-08 19:02:07 +000088 buildFromSourceStub bool
Yu Liufa297642024-06-11 00:13:02 +000089 incrementalBuildActions bool
Colin Cross8d411ff2023-12-07 10:31:24 -080090 ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built
Dan Willemsen1e704462016-08-21 15:17:17 -070091
92 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070093 katiArgs []string
94 ninjaArgs []string
95 katiSuffix string
96 targetDevice string
97 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +000098 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -070099
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800100 // Autodetected
101 totalRAM uint64
102
Spandan Das28a6f192024-07-01 21:00:25 +0000103 brokenDupRules bool
104 brokenUsesNetwork bool
105 brokenNinjaEnvVars []string
106 brokenMissingOutputs bool
Dan Willemsen18490112018-05-25 16:30:04 -0700107
108 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000109
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700110 // Set by multiproduct_kati
111 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700112
113 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000114
Sam Delmerico98a73292023-02-21 11:50:29 -0500115 includeTags []string
116 sourceRootDirs []string
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900117
118 // Data source to write ninja weight list
119 ninjaWeightListSource NinjaWeightListSource
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800120
121 // This file is a detailed dump of all soong-defined modules for debugging purposes.
122 // There's quite a bit of overlap with module-info.json and soong module graph. We
123 // could consider merging them.
124 moduleDebugFile string
Cole Faustbee030d2024-01-03 13:45:48 -0800125
126 // Whether to use n2 instead of ninja. This is controlled with the
127 // environment variable SOONG_USE_N2
128 useN2 bool
Dan Willemsen1e704462016-08-21 15:17:17 -0700129}
130
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900131type NinjaWeightListSource uint
132
133const (
134 // ninja doesn't use weight list.
135 NOT_USED NinjaWeightListSource = iota
136 // ninja uses weight list based on previous builds by ninja log
137 NINJA_LOG
138 // ninja thinks every task has the same weight.
139 EVENLY_DISTRIBUTED
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900140 // ninja uses an external custom weight list
141 EXTERNAL_FILE
Jeongik Chae114e602023-03-19 00:12:39 +0900142 // ninja uses a prioritized module list from Soong
143 HINT_FROM_SOONG
Jeongik Chaa87506f2023-06-01 23:16:41 +0900144 // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead.
145 // We can assume it is an incremental build if ninja log exists.
146 DEFAULT
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900147)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800148const srcDirFileCheck = "build/soong/root.bp"
149
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700150var buildFiles = []string{"Android.mk", "Android.bp"}
151
Patrice Arruda13848222019-04-22 17:12:02 -0700152type BuildAction uint
153
154const (
155 // Builds all of the modules and their dependencies of a specified directory, relative to the root
156 // directory of the source tree.
157 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
158
159 // Builds all of the modules and their dependencies of a list of specified directories. All specified
160 // directories are relative to the root directory of the source tree.
161 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700162
163 // Build a list of specified modules. If none was specified, simply build the whole source tree.
164 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700165)
166
167// checkTopDir validates that the current directory is at the root directory of the source tree.
168func checkTopDir(ctx Context) {
169 if _, err := os.Stat(srcDirFileCheck); err != nil {
170 if os.IsNotExist(err) {
171 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
172 }
173 ctx.Fatalln("Error verifying tree state:", err)
174 }
175}
176
MarkDacek7901e582023-01-09 19:48:01 +0000177func loadEnvConfig(ctx Context, config *configImpl, bc string) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500178 if bc == "" {
179 return nil
180 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500181
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500182 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500183 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500184 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500185 envConfigDir,
186 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500187 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500188 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
189 envVarsJSON, err := ioutil.ReadFile(cfgFile)
190 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500191 continue
192 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500193 ctx.Verbosef("Loading config file %v\n", cfgFile)
194 var envVars map[string]map[string]string
195 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
196 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
197 continue
198 }
199 for k, v := range envVars["env"] {
200 if os.Getenv(k) != "" {
201 continue
202 }
203 config.environ.Set(k, v)
204 }
205 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
206 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500207 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500208
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500209 return nil
210}
211
Dan Willemsen1e704462016-08-21 15:17:17 -0700212func NewConfig(ctx Context, args ...string) Config {
213 ret := &configImpl{
Jeongik Chaf2ecf762023-05-19 14:03:45 +0900214 environ: OsEnvironment(),
215 sandboxConfig: &SandboxConfig{},
Jeongik Chaa87506f2023-06-01 23:16:41 +0900216 ninjaWeightListSource: DEFAULT,
Dan Willemsen1e704462016-08-21 15:17:17 -0700217 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700218 wd, err := os.Getwd()
219 if err != nil {
220 ctx.Fatalln("Failed to get working directory:", err)
221 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700222
Colin Cross106d6ef2023-10-24 10:34:56 -0700223 // Skip soong tests by default on Linux
224 if runtime.GOOS == "linux" {
225 ret.skipSoongTests = true
226 }
227
Patrice Arruda90109172020-07-28 18:07:27 +0000228 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700229 ret.parallel = runtime.NumCPU() + 2
230 ret.keepGoing = 1
231
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800232 ret.totalRAM = detectTotalRAM(ctx)
Dan Willemsen9b587492017-07-10 22:13:00 -0700233 ret.parseArgs(ctx, args)
Jeongik Chae114e602023-03-19 00:12:39 +0900234
235 if ret.ninjaWeightListSource == HINT_FROM_SOONG {
Jeongik Chaa87506f2023-06-01 23:16:41 +0900236 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always")
237 } else if ret.ninjaWeightListSource == DEFAULT {
238 defaultNinjaWeightListSource := NINJA_LOG
239 if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) {
240 ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead")
241 defaultNinjaWeightListSource = HINT_FROM_SOONG
242 } else {
243 ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG")
244 }
245 ret.ninjaWeightListSource = defaultNinjaWeightListSource
246 // soong_build generates ninja hint depending on ninja log existence.
247 // Set it "depend" to avoid soong re-run due to env variable change.
248 ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend")
Jeongik Chae114e602023-03-19 00:12:39 +0900249 }
Jeongik Chaa87506f2023-06-01 23:16:41 +0900250
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800251 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700252 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700253 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, filepath.Clean(outDir)))
Dan Willemsen02f3add2017-05-12 13:50:19 -0700254 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800255 outDir := "out"
256 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700257 outDir = filepath.Join(baseDir, filepath.Base(wd))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800258 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700259 ret.environ.Set("OUT_DIR", ret.sandboxPath(wd, outDir))
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800260 }
261
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500262 // loadEnvConfig needs to know what the OUT_DIR is, so it should
263 // be called after we determine the appropriate out directory.
MarkDacek7901e582023-01-09 19:48:01 +0000264 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
265
266 if bc != "" {
Kousik Kumarc8818332023-01-16 16:33:05 +0000267 if err := loadEnvConfig(ctx, ret, bc); err != nil {
MarkDacek7901e582023-01-09 19:48:01 +0000268 ctx.Fatalln("Failed to parse env config files: %v", err)
269 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +0000270 if !ret.canSupportRBE() {
271 // Explicitly set USE_RBE env variable to false when we cannot run
272 // an RBE build to avoid ninja local execution pool issues.
273 ret.environ.Set("USE_RBE", "false")
274 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500275 }
276
Dan Willemsen2d31a442018-10-20 21:33:41 -0700277 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
278 ret.distDir = filepath.Clean(distDir)
279 } else {
280 ret.distDir = filepath.Join(ret.OutDir(), "dist")
281 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700282
Spandan Das05063612021-06-25 01:39:04 +0000283 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
284 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
285 }
286
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800287 if os.Getenv("GENERATE_SOONG_DEBUG") == "true" {
288 ret.moduleDebugFile, _ = filepath.Abs(shared.JoinPath(ret.SoongOutDir(), "soong-debug-info.json"))
289 }
290
Cole Faustbee030d2024-01-03 13:45:48 -0800291 if os.Getenv("SOONG_USE_N2") == "true" {
292 ret.useN2 = true
293 }
294
Dan Willemsen1e704462016-08-21 15:17:17 -0700295 ret.environ.Unset(
296 // We're already using it
297 "USE_SOONG_UI",
298
299 // We should never use GOROOT/GOPATH from the shell environment
300 "GOROOT",
301 "GOPATH",
302
303 // These should only come from Soong, not the environment.
304 "CLANG",
305 "CLANG_CXX",
306 "CCC_CC",
307 "CCC_CXX",
308
309 // Used by the goma compiler wrapper, but should only be set by
310 // gomacc
311 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800312
313 // We handle this above
314 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700315
Dan Willemsen2d31a442018-10-20 21:33:41 -0700316 // This is handled above too, and set for individual commands later
317 "DIST_DIR",
318
Dan Willemsen68a09852017-04-18 13:56:57 -0700319 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000320 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700321 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700322 "DISPLAY",
323 "GREP_OPTIONS",
Nathan Egge7b067fb2023-02-17 17:54:31 +0000324 "JAVAC",
Nathan Egge978c9342024-07-03 21:13:03 +0000325 "LEX",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700326 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700327 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700328
329 // Drop make flags
330 "MAKEFLAGS",
331 "MAKELEVEL",
332 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700333
334 // Set in envsetup.sh, reset in makefiles
335 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700336
337 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
338 "ANDROID_BUILD_TOP",
339 "ANDROID_HOST_OUT",
340 "ANDROID_PRODUCT_OUT",
341 "ANDROID_HOST_OUT_TESTCASES",
342 "ANDROID_TARGET_OUT_TESTCASES",
343 "ANDROID_TOOLCHAIN",
344 "ANDROID_TOOLCHAIN_2ND_ARCH",
345 "ANDROID_DEV_SCRIPTS",
346 "ANDROID_EMULATOR_PREBUILTS",
347 "ANDROID_PRE_BUILD_PATHS",
Joe Onoratoe5ed3472024-02-02 14:52:05 -0800348
349 // We read it here already, don't let others share in the fun
350 "GENERATE_SOONG_DEBUG",
Cole Faustbee030d2024-01-03 13:45:48 -0800351
352 // Use config.useN2 instead.
353 "SOONG_USE_N2",
Dan Willemsen1e704462016-08-21 15:17:17 -0700354 )
355
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400356 if ret.UseGoma() || ret.ForceUseGoma() {
357 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
358 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400359 }
360
Dan Willemsen1e704462016-08-21 15:17:17 -0700361 // Tell python not to spam the source tree with .pyc files.
362 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
363
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400364 tmpDir := absPath(ctx, ret.TempDir())
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700365 ret.environ.Set("TMPDIR", ret.sandboxPath(wd, tmpDir))
Dan Willemsen32a669b2018-03-08 19:42:00 -0800366
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700367 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
368 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
369 "llvm-binutils-stable/llvm-symbolizer")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700370 ret.environ.Set("ASAN_SYMBOLIZER_PATH", ret.sandboxPath(wd, absPath(ctx, symbolizerPath)))
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700371
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800372 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700373 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800374
Yu Liu6e13b402021-07-27 14:29:06 -0700375 srcDir := absPath(ctx, ".")
376 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700377 ctx.Println("You are building in a directory whose absolute path contains a space character:")
378 ctx.Println()
379 ctx.Printf("%q\n", srcDir)
380 ctx.Println()
381 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700382 }
383
Yu Liu6e13b402021-07-27 14:29:06 -0700384 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
385
Dan Willemsendb8457c2017-05-12 16:38:17 -0700386 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700387 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
388 ctx.Println()
389 ctx.Printf("%q\n", outDir)
390 ctx.Println()
391 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700392 }
393
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000394 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700395 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
396 ctx.Println()
397 ctx.Printf("%q\n", distDir)
398 ctx.Println()
399 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700400 }
401
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700402 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000403 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
Sorin Basca0760c892023-11-29 19:13:55 +0000404 java21Home := filepath.Join("prebuilts/jdk/jdk21", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700405 javaHome := func() string {
406 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
407 return override
408 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000409 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000410 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
Pete Gillin1f52e932019-10-09 17:10:08 +0100411 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000412 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
Sorin Basca5dfa2382024-03-11 17:23:06 +0000413 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
Sorin Basca7e094b32022-10-05 08:20:12 +0000414 }
Sorin Basca5dfa2382024-03-11 17:23:06 +0000415 if toolchain21, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN"); ok && toolchain21 != "true" {
416 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN is no longer supported. An OpenJDK 21 toolchain is now the global default.")
417 }
418 return java21Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700419 }()
420 absJavaHome := absPath(ctx, javaHome)
421
Dan Willemsened869522018-01-08 14:58:46 -0800422 ret.configureLocale(ctx)
423
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000424 newPath := []string{filepath.Join(absJavaHome, "bin")}
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700425 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
PODISHETTY KUMAR (xWF)9543d192024-09-02 03:54:36 +0000426 newPath = append(newPath, path)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700427 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100428
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700429 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700430 ret.environ.Set("JAVA_HOME", ret.sandboxPath(wd, absJavaHome))
431 ret.environ.Set("ANDROID_JAVA_HOME", ret.sandboxPath(wd, javaHome))
432 ret.environ.Set("ANDROID_JAVA8_HOME", ret.sandboxPath(wd, java8Home))
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700433 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
434
Colin Crossfe5ed4d2023-07-28 09:27:23 -0700435 // b/286885495, https://bugzilla.redhat.com/show_bug.cgi?id=2227130: some versions of Fedora include patches
436 // to unzip to enable zipbomb detection that incorrectly handle zip64 and data descriptors and fail on large
437 // zip files produced by soong_zip. Disable zipbomb detection.
438 ret.environ.Set("UNZIP_DISABLE_ZIPBOMB_DETECTION", "TRUE")
439
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800440 outDir := ret.OutDir()
441 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800442 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800443 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800444 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800445 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800446 }
Colin Cross28f527c2019-11-26 16:19:04 -0800447
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700448 ret.environ.Set("BUILD_DATETIME_FILE", ret.sandboxPath(wd, buildDateTimeFile))
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800449
Cole Faust583dfb42023-09-28 13:56:30 -0700450 if _, ok := ret.environ.Get("BUILD_USERNAME"); !ok {
451 username := "unknown"
452 if u, err := user.Current(); err == nil {
453 username = u.Username
454 } else {
455 ctx.Println("Failed to get current user:", err)
456 }
457 ret.environ.Set("BUILD_USERNAME", username)
458 }
Taylor Santiago8b0bed72024-09-03 13:30:22 -0700459 ret.environ.Set("PWD", ret.sandboxPath(wd, wd))
Cole Faust583dfb42023-09-28 13:56:30 -0700460
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400461 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400462 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400463 ret.environ.Set(k, v)
464 }
465 }
466
Patrice Arruda96850362020-08-11 20:41:11 +0000467 c := Config{ret}
468 storeConfigMetrics(ctx, c)
469 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700470}
471
Patrice Arruda13848222019-04-22 17:12:02 -0700472// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
473// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700474func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
475 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700476}
477
LaMont Jones9a912862023-11-06 22:11:08 +0000478// Prepare for getting make variables. For them to be accurate, we need to have
479// obtained PRODUCT_RELEASE_CONFIG_MAPS.
480//
481// Returns:
482//
483// Whether config should be called again.
484//
485// TODO: when converting product config to a declarative language, make sure
486// that PRODUCT_RELEASE_CONFIG_MAPS is properly handled as a separate step in
487// that process.
488func SetProductReleaseConfigMaps(ctx Context, config Config) bool {
489 ctx.BeginTrace(metrics.RunKati, "SetProductReleaseConfigMaps")
490 defer ctx.EndTrace()
491
492 if config.SkipConfig() {
493 // This duplicates the logic from Build to skip product config
494 // if the user has explicitly said to.
495 return false
496 }
497
498 releaseConfigVars := []string{
499 "PRODUCT_RELEASE_CONFIG_MAPS",
500 }
501
502 origValue, _ := config.environ.Get("PRODUCT_RELEASE_CONFIG_MAPS")
503 // Get the PRODUCT_RELEASE_CONFIG_MAPS for this product, to avoid polluting the environment
504 // when we run product config to get the rest of the make vars.
505 releaseMapVars, err := dumpMakeVars(ctx, config, nil, releaseConfigVars, false, "")
506 if err != nil {
507 ctx.Fatalln("Error getting PRODUCT_RELEASE_CONFIG_MAPS:", err)
508 }
509 productReleaseConfigMaps := releaseMapVars["PRODUCT_RELEASE_CONFIG_MAPS"]
510 os.Setenv("PRODUCT_RELEASE_CONFIG_MAPS", productReleaseConfigMaps)
511 return origValue != productReleaseConfigMaps
512}
513
Patrice Arruda96850362020-08-11 20:41:11 +0000514// storeConfigMetrics selects a set of configuration information and store in
515// the metrics system for further analysis.
516func storeConfigMetrics(ctx Context, config Config) {
517 if ctx.Metrics == nil {
518 return
519 }
520
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400521 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000522
523 s := &smpb.SystemResourceInfo{
524 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
525 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
526 }
527 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000528}
529
Jeongik Cha8d63d562023-03-17 03:52:13 +0900530func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource {
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900531 switch s {
532 case NINJA_LOG:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900533 return smpb.BuildConfig_NINJA_LOG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900534 case EVENLY_DISTRIBUTED:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900535 return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum()
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900536 case EXTERNAL_FILE:
537 return smpb.BuildConfig_EXTERNAL_FILE.Enum()
Jeongik Chae114e602023-03-19 00:12:39 +0900538 case HINT_FROM_SOONG:
539 return smpb.BuildConfig_HINT_FROM_SOONG.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900540 default:
Jeongik Cha8d63d562023-03-17 03:52:13 +0900541 return smpb.BuildConfig_NOT_USED.Enum()
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900542 }
543}
544
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400545func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700546 c := &smpb.BuildConfig{
Colin Cross8d411ff2023-12-07 10:31:24 -0800547 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
548 UseGoma: proto.Bool(config.UseGoma()),
549 UseRbe: proto.Bool(config.UseRBE()),
550 NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400551 }
Yu Liue737a992021-10-04 13:21:41 -0700552 c.Targets = append(c.Targets, config.arguments...)
553
554 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400555}
556
Patrice Arruda13848222019-04-22 17:12:02 -0700557// getConfigArgs processes the command arguments based on the build action and creates a set of new
558// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700559func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700560 // The next block of code verifies that the current directory is the root directory of the source
561 // tree. It then finds the relative path of dir based on the root directory of the source tree
562 // and verify that dir is inside of the source tree.
563 checkTopDir(ctx)
564 topDir, err := os.Getwd()
565 if err != nil {
566 ctx.Fatalf("Error retrieving top directory: %v", err)
567 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700568 dir, err = filepath.EvalSymlinks(dir)
569 if err != nil {
570 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
571 }
Patrice Arruda13848222019-04-22 17:12:02 -0700572 dir, err = filepath.Abs(dir)
573 if err != nil {
574 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
575 }
576 relDir, err := filepath.Rel(topDir, dir)
577 if err != nil {
578 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
579 }
580 // If there are ".." in the path, it's not in the source tree.
581 if strings.Contains(relDir, "..") {
582 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
583 }
584
585 configArgs := args[:]
586
587 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
588 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
589 targetNamePrefix := "MODULES-IN-"
590 if inList("GET-INSTALL-PATH", configArgs) {
591 targetNamePrefix = "GET-INSTALL-PATH-IN-"
592 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
593 }
594
Patrice Arruda13848222019-04-22 17:12:02 -0700595 var targets []string
596
597 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700598 case BUILD_MODULES:
599 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700600 case BUILD_MODULES_IN_A_DIRECTORY:
601 // If dir is the root source tree, all the modules are built of the source tree are built so
602 // no need to find the build file.
603 if topDir == dir {
604 break
605 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700606
Patrice Arruda13848222019-04-22 17:12:02 -0700607 buildFile := findBuildFile(ctx, relDir)
608 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700609 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700610 }
Patrice Arruda13848222019-04-22 17:12:02 -0700611 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
612 case BUILD_MODULES_IN_DIRECTORIES:
613 newConfigArgs, dirs := splitArgs(configArgs)
614 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700615 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700616 }
617
618 // Tidy only override all other specified targets.
619 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
620 if tidyOnly == "true" || tidyOnly == "1" {
621 configArgs = append(configArgs, "tidy_only")
622 } else {
623 configArgs = append(configArgs, targets...)
624 }
625
626 return configArgs
627}
628
629// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
630func convertToTarget(dir string, targetNamePrefix string) string {
631 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
632}
633
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700634// hasBuildFile returns true if dir contains an Android build file.
635func hasBuildFile(ctx Context, dir string) bool {
636 for _, buildFile := range buildFiles {
637 _, err := os.Stat(filepath.Join(dir, buildFile))
638 if err == nil {
639 return true
640 }
641 if !os.IsNotExist(err) {
642 ctx.Fatalf("Error retrieving the build file stats: %v", err)
643 }
644 }
645 return false
646}
647
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700648// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
649// in the current and any sub directory of dir. If a build file is not found, traverse the path
650// up by one directory and repeat again until either a build file is found or reached to the root
651// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
652// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700653func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700654 // If the string is empty or ".", assume it is top directory of the source tree.
655 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700656 return ""
657 }
658
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700659 found := false
660 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
661 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
662 if err != nil {
663 return err
664 }
665 if found {
666 return filepath.SkipDir
667 }
668 if info.IsDir() {
669 return nil
670 }
671 for _, buildFile := range buildFiles {
672 if info.Name() == buildFile {
673 found = true
674 return filepath.SkipDir
675 }
676 }
677 return nil
678 })
679 if err != nil {
680 ctx.Fatalf("Error finding Android build file: %v", err)
681 }
682
683 if found {
684 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700685 }
686 }
687
688 return ""
689}
690
691// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
692func splitArgs(args []string) (newArgs []string, dirs []string) {
693 specialArgs := map[string]bool{
694 "showcommands": true,
695 "snod": true,
696 "dist": true,
697 "checkbuild": true,
698 }
699
700 newArgs = []string{}
701 dirs = []string{}
702
703 for _, arg := range args {
704 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
705 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
706 newArgs = append(newArgs, arg)
707 continue
708 }
709
710 if _, ok := specialArgs[arg]; ok {
711 newArgs = append(newArgs, arg)
712 continue
713 }
714
715 dirs = append(dirs, arg)
716 }
717
718 return newArgs, dirs
719}
720
721// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
722// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
723// source root tree where the build action command was invoked. Each directory is validated if the
724// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700725func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700726 for _, dir := range dirs {
727 // The directory may have specified specific modules to build. ":" is the separator to separate
728 // the directory and the list of modules.
729 s := strings.Split(dir, ":")
730 l := len(s)
731 if l > 2 { // more than one ":" was specified.
732 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
733 }
734
735 dir = filepath.Join(relDir, s[0])
736 if _, err := os.Stat(dir); err != nil {
737 ctx.Fatalf("couldn't find directory %s", dir)
738 }
739
740 // Verify that if there are any targets specified after ":". Each target is separated by ",".
741 var newTargets []string
742 if l == 2 && s[1] != "" {
743 newTargets = strings.Split(s[1], ",")
744 if inList("", newTargets) {
745 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
746 }
747 }
748
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700749 // If there are specified targets to build in dir, an android build file must exist for the one
750 // shot build. For the non-targets case, find the appropriate build file and build all the
751 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700752 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700753 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700754 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
755 }
756 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700757 buildFile := findBuildFile(ctx, dir)
758 if buildFile == "" {
759 ctx.Fatalf("Build file not found for %s directory", dir)
760 }
761 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700762 }
763
Patrice Arruda13848222019-04-22 17:12:02 -0700764 targets = append(targets, newTargets...)
765 }
766
Dan Willemsence41e942019-07-29 23:39:30 -0700767 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700768}
769
Dan Willemsen9b587492017-07-10 22:13:00 -0700770func (c *configImpl) parseArgs(ctx Context, args []string) {
771 for i := 0; i < len(args); i++ {
772 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100773 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700774 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200775 } else if arg == "--empty-ninja-file" {
776 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100777 } else if arg == "--skip-ninja" {
778 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700779 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700780 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
781 // but it does run a Kati ninja file if the .kati_enabled marker file was created
782 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000783 c.skipConfig = true
784 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100785 } else if arg == "--soong-only" {
786 c.skipKati = true
787 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200788 } else if arg == "--config-only" {
789 c.skipKati = true
790 c.skipKatiNinja = true
791 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700792 } else if arg == "--skip-config" {
793 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700794 } else if arg == "--skip-soong-tests" {
795 c.skipSoongTests = true
Colin Cross106d6ef2023-10-24 10:34:56 -0700796 } else if arg == "--no-skip-soong-tests" {
797 c.skipSoongTests = false
MarkDacekd0e7cd32022-12-02 22:22:40 +0000798 } else if arg == "--skip-metrics-upload" {
799 c.skipMetricsUpload = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500800 } else if arg == "--mk-metrics" {
801 c.reportMkMetrics = true
Spandan Das394aa322022-11-03 17:02:10 +0000802 } else if arg == "--search-api-dir" {
803 c.searchApiDir = true
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900804 } else if strings.HasPrefix(arg, "--ninja_weight_source=") {
805 source := strings.TrimPrefix(arg, "--ninja_weight_source=")
806 if source == "ninja_log" {
807 c.ninjaWeightListSource = NINJA_LOG
808 } else if source == "evenly_distributed" {
809 c.ninjaWeightListSource = EVENLY_DISTRIBUTED
810 } else if source == "not_used" {
811 c.ninjaWeightListSource = NOT_USED
Jeongik Chae114e602023-03-19 00:12:39 +0900812 } else if source == "soong" {
813 c.ninjaWeightListSource = HINT_FROM_SOONG
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900814 } else if strings.HasPrefix(source, "file,") {
815 c.ninjaWeightListSource = EXTERNAL_FILE
816 filePath := strings.TrimPrefix(source, "file,")
817 err := validateNinjaWeightList(filePath)
818 if err != nil {
819 ctx.Fatalf("Malformed weight list from %s: %s", filePath, err)
820 }
821 _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list"))
822 if err != nil {
823 ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err)
824 }
Jeongik Cha0cf44d52023-03-15 00:10:45 +0900825 } else {
826 ctx.Fatalf("unknown option for ninja_weight_source: %s", source)
827 }
Jihoon Kang2a929ad2023-06-08 19:02:07 +0000828 } else if arg == "--build-from-source-stub" {
829 c.buildFromSourceStub = true
Yu Liufa297642024-06-11 00:13:02 +0000830 } else if arg == "--incremental-build-actions" {
831 c.incrementalBuildActions = true
MarkDacekb96561e2022-12-02 04:34:43 +0000832 } else if strings.HasPrefix(arg, "--build-command=") {
833 buildCmd := strings.TrimPrefix(arg, "--build-command=")
834 // remove quotations
835 buildCmd = strings.TrimPrefix(buildCmd, "\"")
836 buildCmd = strings.TrimSuffix(buildCmd, "\"")
837 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacek6614d9c2022-12-07 21:57:38 +0000838 } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") {
839 buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=")
840 val, err := strconv.ParseInt(buildTimeStr, 10, 64)
841 if err == nil {
842 c.buildStartedTime = val
843 } else {
844 ctx.Fatalf("Error parsing build-time-started-unix-millis", err)
845 }
MarkDacekf47e1422023-04-19 16:47:36 +0000846 } else if arg == "--ensure-allowlist-integrity" {
847 c.ensureAllowlistIntegrity = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700848 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700849 parseArgNum := func(def int) int {
850 if len(arg) > 2 {
851 p, err := strconv.ParseUint(arg[2:], 10, 31)
852 if err != nil {
853 ctx.Fatalf("Failed to parse %q: %v", arg, err)
854 }
855 return int(p)
856 } else if i+1 < len(args) {
857 p, err := strconv.ParseUint(args[i+1], 10, 31)
858 if err == nil {
859 i++
860 return int(p)
861 }
862 }
863 return def
864 }
865
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700866 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700867 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700868 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700869 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700870 } else {
871 ctx.Fatalln("Unknown option:", arg)
872 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700873 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700874 if k == "OUT_DIR" {
875 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
876 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700877 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700878 } else if arg == "dist" {
879 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200880 } else if arg == "json-module-graph" {
881 c.jsonModuleGraph = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200882 } else if arg == "queryview" {
883 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200884 } else if arg == "soong_docs" {
885 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700886 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700887 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800888 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700889 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700890 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700891 }
892 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700893}
894
Jeongik Cha518f3ea2023-03-19 00:12:39 +0900895func validateNinjaWeightList(weightListFilePath string) (err error) {
896 data, err := os.ReadFile(weightListFilePath)
897 if err != nil {
898 return
899 }
900 lines := strings.Split(strings.TrimSpace(string(data)), "\n")
901 for _, line := range lines {
902 fields := strings.Split(line, ",")
903 if len(fields) != 2 {
904 return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line)
905 }
906 _, err = strconv.Atoi(fields[1])
907 if err != nil {
908 return
909 }
910 }
911 return
912}
913
Dan Willemsened869522018-01-08 14:58:46 -0800914func (c *configImpl) configureLocale(ctx Context) {
915 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
916 output, err := cmd.Output()
917
918 var locales []string
919 if err == nil {
920 locales = strings.Split(string(output), "\n")
921 } else {
922 // If we're unable to list the locales, let's assume en_US.UTF-8
923 locales = []string{"en_US.UTF-8"}
924 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
925 }
926
927 // gettext uses LANGUAGE, which is passed directly through
928
929 // For LANG and LC_*, only preserve the evaluated version of
930 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800931 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800932 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800933 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800934 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800935 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800936 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800937 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800938 }
939
940 c.environ.UnsetWithPrefix("LC_")
941
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800942 if userLang != "" {
943 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800944 }
945
946 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
947 // for others)
948 if inList("C.UTF-8", locales) {
949 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500950 } else if inList("C.utf8", locales) {
951 // These normalize to the same thing
952 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800953 } else if inList("en_US.UTF-8", locales) {
954 c.environ.Set("LANG", "en_US.UTF-8")
955 } else if inList("en_US.utf8", locales) {
956 // These normalize to the same thing
957 c.environ.Set("LANG", "en_US.UTF-8")
958 } else {
959 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
960 }
961}
962
Dan Willemsen1e704462016-08-21 15:17:17 -0700963func (c *configImpl) Environment() *Environment {
964 return c.environ
965}
966
967func (c *configImpl) Arguments() []string {
968 return c.arguments
969}
970
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200971func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200972 if len(c.Arguments()) > 0 {
973 // Explicit targets requested that are not special targets like b2pbuild
974 // or the JSON module graph
975 return true
976 }
977
Colin Cross8d411ff2023-12-07 10:31:24 -0800978 if !c.JsonModuleGraph() && !c.Queryview() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200979 // Command line was empty, the default Ninja target is built
980 return true
981 }
982
Colin Cross8d411ff2023-12-07 10:31:24 -0800983 if c.Dist() {
Liz Kammer88677422021-12-15 15:03:19 -0500984 return true
985 }
986
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200987 // build.ninja doesn't need to be generated
988 return false
989}
990
Dan Willemsen1e704462016-08-21 15:17:17 -0700991func (c *configImpl) OutDir() string {
992 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700993 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700994 }
995 return "out"
996}
997
Dan Willemsen8a073a82017-02-04 17:30:44 -0800998func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -0400999 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001000}
1001
1002func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -07001003 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -08001004}
1005
Dan Willemsen1e704462016-08-21 15:17:17 -07001006func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001007 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001008 return c.arguments
1009 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001010 return c.ninjaArgs
1011}
1012
1013func (c *configImpl) SoongOutDir() string {
1014 return filepath.Join(c.OutDir(), "soong")
1015}
1016
Spandan Das394aa322022-11-03 17:02:10 +00001017func (c *configImpl) ApiSurfacesOutDir() string {
1018 return filepath.Join(c.OutDir(), "api_surfaces")
1019}
1020
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001021func (c *configImpl) PrebuiltOS() string {
1022 switch runtime.GOOS {
1023 case "linux":
1024 return "linux-x86"
1025 case "darwin":
1026 return "darwin-x86"
1027 default:
1028 panic("Unknown GOOS")
1029 }
1030}
Lukacs T. Berki90b43342021-11-02 14:42:04 +01001031
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001032func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -07001033 if c.SkipKatiNinja() {
1034 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
1035 } else {
1036 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
1037 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +02001038}
1039
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001040func (c *configImpl) UsedEnvFile(tag string) string {
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001041 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
Qing Shen713c5422024-08-23 04:09:18 +00001042 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+c.CoverageSuffix()+"."+tag)
Kiyoung Kimeaa55a82023-06-05 16:56:49 +09001043 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +02001044 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
1045}
1046
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001047func (c *configImpl) SoongDocsHtml() string {
1048 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
1049}
1050
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001051func (c *configImpl) QueryviewMarkerFile() string {
1052 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
1053}
1054
Lukacs T. Berkie571dc32021-08-25 14:14:13 +02001055func (c *configImpl) ModuleGraphFile() string {
1056 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
1057}
1058
kgui67007242022-01-25 13:50:25 +08001059func (c *configImpl) ModuleActionsFile() string {
1060 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
1061}
1062
Jeff Gastonefc1b412017-03-29 17:29:06 -07001063func (c *configImpl) TempDir() string {
1064 return shared.TempDirForOutDir(c.SoongOutDir())
1065}
1066
Jeff Gastonb64fc1c2017-08-04 12:30:12 -07001067func (c *configImpl) FileListDir() string {
1068 return filepath.Join(c.OutDir(), ".module_paths")
1069}
1070
Dan Willemsen1e704462016-08-21 15:17:17 -07001071func (c *configImpl) KatiSuffix() string {
1072 if c.katiSuffix != "" {
1073 return c.katiSuffix
1074 }
1075 panic("SetKatiSuffix has not been called")
1076}
1077
Colin Cross37193492017-11-16 17:55:00 -08001078// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
1079// user is interested in additional checks at the expense of build time.
1080func (c *configImpl) Checkbuild() bool {
1081 return c.checkbuild
1082}
1083
Dan Willemsen8a073a82017-02-04 17:30:44 -08001084func (c *configImpl) Dist() bool {
1085 return c.dist
1086}
1087
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001088func (c *configImpl) JsonModuleGraph() bool {
1089 return c.jsonModuleGraph
1090}
1091
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001092func (c *configImpl) Queryview() bool {
1093 return c.queryview
1094}
1095
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001096func (c *configImpl) SoongDocs() bool {
1097 return c.soongDocs
1098}
1099
Dan Willemsen1e704462016-08-21 15:17:17 -07001100func (c *configImpl) IsVerbose() bool {
1101 return c.verbose
1102}
1103
Jeongik Cha0cf44d52023-03-15 00:10:45 +09001104func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource {
1105 return c.ninjaWeightListSource
1106}
1107
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001108func (c *configImpl) SkipKati() bool {
1109 return c.skipKati
1110}
1111
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001112func (c *configImpl) SkipKatiNinja() bool {
1113 return c.skipKatiNinja
1114}
1115
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001116func (c *configImpl) SkipSoong() bool {
1117 return c.skipSoong
1118}
1119
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001120func (c *configImpl) SkipNinja() bool {
1121 return c.skipNinja
1122}
1123
Anton Hansson5a7861a2021-06-04 10:09:01 +01001124func (c *configImpl) SetSkipNinja(v bool) {
1125 c.skipNinja = v
1126}
1127
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001128func (c *configImpl) SkipConfig() bool {
1129 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001130}
1131
Jihoon Kang1bff0342023-01-17 20:40:22 +00001132func (c *configImpl) BuildFromTextStub() bool {
Jihoon Kang2a929ad2023-06-08 19:02:07 +00001133 return !c.buildFromSourceStub
Jihoon Kang1bff0342023-01-17 20:40:22 +00001134}
1135
Dan Willemsen1e704462016-08-21 15:17:17 -07001136func (c *configImpl) TargetProduct() string {
1137 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1138 return v
1139 }
1140 panic("TARGET_PRODUCT is not defined")
1141}
1142
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001143func (c *configImpl) TargetProductOrErr() (string, error) {
1144 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1145 return v, nil
1146 }
1147 return "", fmt.Errorf("TARGET_PRODUCT is not defined")
1148}
1149
Qing Shen713c5422024-08-23 04:09:18 +00001150func (c *configImpl) CoverageSuffix() string {
1151 if v := c.environ.IsEnvTrue("EMMA_INSTRUMENT"); v {
1152 return ".coverage"
1153 }
1154 return ""
1155}
1156
Dan Willemsen02781d52017-05-12 19:28:13 -07001157func (c *configImpl) TargetDevice() string {
1158 return c.targetDevice
1159}
1160
1161func (c *configImpl) SetTargetDevice(device string) {
1162 c.targetDevice = device
1163}
1164
1165func (c *configImpl) TargetBuildVariant() string {
1166 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1167 return v
1168 }
1169 panic("TARGET_BUILD_VARIANT is not defined")
1170}
1171
Dan Willemsen1e704462016-08-21 15:17:17 -07001172func (c *configImpl) KatiArgs() []string {
1173 return c.katiArgs
1174}
1175
1176func (c *configImpl) Parallel() int {
1177 return c.parallel
1178}
1179
Sam Delmerico98a73292023-02-21 11:50:29 -05001180func (c *configImpl) GetSourceRootDirs() []string {
1181 return c.sourceRootDirs
1182}
1183
1184func (c *configImpl) SetSourceRootDirs(i []string) {
1185 c.sourceRootDirs = i
1186}
1187
MarkDacek6614d9c2022-12-07 21:57:38 +00001188func (c *configImpl) GetLogsPrefix() string {
1189 return c.logsPrefix
1190}
1191
1192func (c *configImpl) SetLogsPrefix(prefix string) {
1193 c.logsPrefix = prefix
1194}
1195
Colin Cross8b8bec32019-11-15 13:18:43 -08001196func (c *configImpl) HighmemParallel() int {
1197 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1198 return i
1199 }
1200
1201 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1202 parallel := c.Parallel()
1203 if c.UseRemoteBuild() {
1204 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1205 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1206 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1207 // Return 1/16th of the size of the local pool, rounding up.
1208 return (parallel + 15) / 16
1209 } else if c.totalRAM == 0 {
1210 // Couldn't detect the total RAM, don't restrict highmem processes.
1211 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001212 } else if c.totalRAM <= 16*1024*1024*1024 {
1213 // Less than 16GB of ram, restrict to 1 highmem processes
1214 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001215 } else if c.totalRAM <= 32*1024*1024*1024 {
1216 // Less than 32GB of ram, restrict to 2 highmem processes
1217 return 2
1218 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1219 // If less than 8GB total RAM per process, reduce the number of highmem processes
1220 return p
1221 }
1222 // No restriction on highmem processes
1223 return parallel
1224}
1225
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001226func (c *configImpl) TotalRAM() uint64 {
1227 return c.totalRAM
1228}
1229
Kousik Kumarec478642020-09-21 13:39:24 -04001230// ForceUseGoma determines whether we should override Goma deprecation
1231// and use Goma for the current build or not.
1232func (c *configImpl) ForceUseGoma() bool {
1233 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1234 v = strings.TrimSpace(v)
1235 if v != "" && v != "false" {
1236 return true
1237 }
1238 }
1239 return false
1240}
1241
Dan Willemsen1e704462016-08-21 15:17:17 -07001242func (c *configImpl) UseGoma() bool {
1243 if v, ok := c.environ.Get("USE_GOMA"); ok {
1244 v = strings.TrimSpace(v)
1245 if v != "" && v != "false" {
1246 return true
1247 }
1248 }
1249 return false
1250}
1251
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001252func (c *configImpl) StartGoma() bool {
1253 if !c.UseGoma() {
1254 return false
1255 }
1256
1257 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1258 v = strings.TrimSpace(v)
1259 if v != "" && v != "false" {
1260 return false
1261 }
1262 }
1263 return true
1264}
1265
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001266func (c *configImpl) canSupportRBE() bool {
Joe Onorato86f50e72024-06-24 14:28:25 -07001267 // Only supported on linux
1268 if runtime.GOOS != "linux" {
1269 return false
1270 }
1271
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001272 // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since
1273 // its unlikely that we will be able to obtain necessary creds without stubby.
1274 authType, _ := c.rbeAuth()
1275 if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") {
1276 return false
1277 }
Taylor Santiago3c16e612024-05-30 14:41:31 -07001278 if c.UseABFS() {
1279 return false
1280 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001281 return true
1282}
1283
Taylor Santiago3c16e612024-05-30 14:41:31 -07001284func (c *configImpl) UseABFS() bool {
1285 if v, ok := c.environ.Get("NO_ABFS"); ok {
1286 v = strings.ToLower(strings.TrimSpace(v))
1287 if v == "true" || v == "1" {
1288 return false
1289 }
1290 }
1291
1292 abfsBox := c.PrebuiltBuildTool("abfsbox")
1293 err := exec.Command(abfsBox, "hash", srcDirFileCheck).Run()
1294 return err == nil
1295}
1296
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001297func (c *configImpl) sandboxPath(base, in string) string {
1298 if !c.UseABFS() {
1299 return in
1300 }
1301
1302 rel, err := filepath.Rel(base, in)
1303 if err != nil {
1304 return in
1305 }
1306
1307 return filepath.Join(abfsSrcDir, rel)
1308}
1309
Ramy Medhatbbf25672019-07-17 12:30:04 +00001310func (c *configImpl) UseRBE() bool {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001311 // These alternate modes of running Soong do not use RBE / reclient.
Colin Cross8d411ff2023-12-07 10:31:24 -08001312 if c.Queryview() || c.JsonModuleGraph() {
Jingwen Chend7ccde12023-06-28 07:19:26 +00001313 return false
1314 }
1315
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001316 if !c.canSupportRBE() {
Kousik Kumar67ad4342023-06-06 15:09:27 -04001317 return false
1318 }
Kousik Kumar6d1e3482023-07-24 03:44:16 +00001319
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001320 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001321 v = strings.TrimSpace(v)
1322 if v != "" && v != "false" {
1323 return true
1324 }
1325 }
1326 return false
1327}
1328
1329func (c *configImpl) StartRBE() bool {
1330 if !c.UseRBE() {
1331 return false
1332 }
1333
1334 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1335 v = strings.TrimSpace(v)
1336 if v != "" && v != "false" {
1337 return false
1338 }
1339 }
1340 return true
1341}
1342
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001343func (c *configImpl) rbeProxyLogsDir() string {
1344 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001345 if v, ok := c.environ.Get(f); ok {
1346 return v
1347 }
1348 }
Ramy Medhatbc061762023-10-10 18:36:59 +00001349 return c.rbeTmpDir()
1350}
1351
1352func (c *configImpl) rbeDownloadTmpDir() string {
Cole Faust06ea5312023-10-18 17:38:40 -07001353 for _, f := range []string{"RBE_download_tmp_dir", "FLAG_download_tmp_dir"} {
Ramy Medhatbc061762023-10-10 18:36:59 +00001354 if v, ok := c.environ.Get(f); ok {
1355 return v
1356 }
1357 }
1358 return c.rbeTmpDir()
1359}
1360
1361func (c *configImpl) rbeTmpDir() string {
Yaowen Meid4da2662024-07-24 08:25:12 +00001362 return filepath.Join(c.SoongOutDir(), "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001363}
1364
Ramy Medhatc8f6cc22023-03-31 09:50:34 -04001365func (c *configImpl) rbeCacheDir() string {
1366 for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} {
1367 if v, ok := c.environ.Get(f); ok {
1368 return v
1369 }
1370 }
1371 return shared.JoinPath(c.SoongOutDir(), "rbe")
1372}
1373
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001374func (c *configImpl) shouldCleanupRBELogsDir() bool {
1375 // Perform a log directory cleanup only when the log directory
1376 // is auto created by the build rather than user-specified.
1377 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Yaowen Meid9108d22024-08-29 16:56:32 +00001378 if v, ok := c.environ.Get(f); ok {
1379 if v != c.rbeTmpDir() {
1380 return false
1381 }
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001382 }
1383 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001384 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001385}
1386
1387func (c *configImpl) rbeExecRoot() string {
1388 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1389 if v, ok := c.environ.Get(f); ok {
1390 return v
1391 }
1392 }
1393 wd, err := os.Getwd()
1394 if err != nil {
1395 return ""
1396 }
1397 return wd
1398}
1399
1400func (c *configImpl) rbeDir() string {
1401 if v, ok := c.environ.Get("RBE_DIR"); ok {
1402 return v
1403 }
1404 return "prebuilts/remoteexecution-client/live/"
1405}
1406
1407func (c *configImpl) rbeReproxy() string {
1408 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1409 if v, ok := c.environ.Get(f); ok {
1410 return v
1411 }
1412 }
1413 return filepath.Join(c.rbeDir(), "reproxy")
1414}
1415
1416func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001417 credFlags := []string{
1418 "use_application_default_credentials",
1419 "use_gce_credentials",
1420 "credential_file",
1421 "use_google_prod_creds",
1422 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001423 for _, cf := range credFlags {
1424 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1425 if v, ok := c.environ.Get(f); ok {
1426 v = strings.TrimSpace(v)
1427 if v != "" && v != "false" && v != "0" {
1428 return "RBE_" + cf, v
1429 }
1430 }
1431 }
1432 }
1433 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001434}
1435
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001436func (c *configImpl) rbeSockAddr(dir string) (string, error) {
Andus Yuc917eb82024-03-06 21:54:15 +00001437 // Absolute path socket addresses have a prefix of //. This should
1438 // be included in the length limit.
1439 maxNameLen := len(syscall.RawSockaddrUnix{}.Path) - 2
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001440 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1441
1442 name := filepath.Join(dir, base)
1443 if len(name) < maxNameLen {
1444 return name, nil
1445 }
1446
1447 name = filepath.Join("/tmp", base)
1448 if len(name) < maxNameLen {
1449 return name, nil
1450 }
1451
1452 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1453}
1454
Kousik Kumar7bc78192022-04-27 14:52:56 -04001455// IsGooglerEnvironment returns true if the current build is running
1456// on a Google developer machine and false otherwise.
1457func (c *configImpl) IsGooglerEnvironment() bool {
1458 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1459 if v, ok := c.environ.Get(cf); ok {
1460 return v == "googler"
1461 }
1462 return false
1463}
1464
1465// GoogleProdCredsExist determine whether credentials exist on the
1466// Googler machine to use remote execution.
1467func (c *configImpl) GoogleProdCredsExist() bool {
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001468 if googleProdCredsExistCache {
1469 return googleProdCredsExistCache
1470 }
andusyu0b3dc032023-06-21 17:29:32 -04001471 if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil {
Kousik Kumar7bc78192022-04-27 14:52:56 -04001472 return false
1473 }
Kevin Dagostino096ab2f2023-03-03 19:47:17 +00001474 googleProdCredsExistCache = true
Kousik Kumar7bc78192022-04-27 14:52:56 -04001475 return true
1476}
1477
1478// UseRemoteBuild indicates whether to use a remote build acceleration system
1479// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001480func (c *configImpl) UseRemoteBuild() bool {
1481 return c.UseGoma() || c.UseRBE()
1482}
1483
Kousik Kumar7bc78192022-04-27 14:52:56 -04001484// StubbyExists checks whether the stubby binary exists on the machine running
1485// the build.
1486func (c *configImpl) StubbyExists() bool {
1487 if _, err := exec.LookPath("stubby"); err != nil {
1488 return false
1489 }
1490 return true
1491}
1492
Dan Willemsen1e704462016-08-21 15:17:17 -07001493// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001494// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001495// still limited by Parallel()
1496func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001497 if !c.UseRemoteBuild() {
1498 return 0
1499 }
1500 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1501 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001502 }
1503 return 500
1504}
1505
1506func (c *configImpl) SetKatiArgs(args []string) {
1507 c.katiArgs = args
1508}
1509
1510func (c *configImpl) SetNinjaArgs(args []string) {
1511 c.ninjaArgs = args
1512}
1513
1514func (c *configImpl) SetKatiSuffix(suffix string) {
1515 c.katiSuffix = suffix
1516}
1517
Dan Willemsene0879fc2017-08-04 15:06:27 -07001518func (c *configImpl) LastKatiSuffixFile() string {
1519 return filepath.Join(c.OutDir(), "last_kati_suffix")
1520}
1521
1522func (c *configImpl) HasKatiSuffix() bool {
1523 return c.katiSuffix != ""
1524}
1525
Dan Willemsen1e704462016-08-21 15:17:17 -07001526func (c *configImpl) KatiEnvFile() string {
1527 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1528}
1529
Dan Willemsen29971232018-09-26 14:58:30 -07001530func (c *configImpl) KatiBuildNinjaFile() string {
1531 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001532}
1533
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001534func (c *configImpl) KatiPackageNinjaFile() string {
1535 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1536}
1537
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001538func (c *configImpl) SoongVarsFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001539 targetProduct, err := c.TargetProductOrErr()
1540 if err != nil {
1541 return filepath.Join(c.SoongOutDir(), "soong.variables")
1542 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001543 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".variables")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001544 }
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001545}
1546
Inseob Kim58c802f2024-06-11 10:59:00 +09001547func (c *configImpl) SoongExtraVarsFile() string {
1548 targetProduct, err := c.TargetProductOrErr()
1549 if err != nil {
1550 return filepath.Join(c.SoongOutDir(), "soong.extra.variables")
1551 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001552 return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+c.CoverageSuffix()+".extra.variables")
Inseob Kim58c802f2024-06-11 10:59:00 +09001553 }
1554}
1555
Dan Willemsen1e704462016-08-21 15:17:17 -07001556func (c *configImpl) SoongNinjaFile() string {
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001557 targetProduct, err := c.TargetProductOrErr()
1558 if err != nil {
1559 return filepath.Join(c.SoongOutDir(), "build.ninja")
1560 } else {
Qing Shen713c5422024-08-23 04:09:18 +00001561 return filepath.Join(c.SoongOutDir(), "build."+targetProduct+c.CoverageSuffix()+".ninja")
Kiyoung Kima37d9ba2023-04-19 13:13:45 +09001562 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001563}
1564
1565func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001566 if c.katiSuffix == "" {
1567 return filepath.Join(c.OutDir(), "combined.ninja")
1568 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001569 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1570}
1571
1572func (c *configImpl) SoongAndroidMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001573 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001574}
1575
1576func (c *configImpl) SoongMakeVarsMk() string {
Qing Shen713c5422024-08-23 04:09:18 +00001577 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+c.CoverageSuffix()+".mk")
Dan Willemsen1e704462016-08-21 15:17:17 -07001578}
1579
Colin Crossaa9a2732023-10-27 10:54:27 -07001580func (c *configImpl) SoongBuildMetrics() string {
Colin Crossb67b0612023-10-31 10:02:45 -07001581 return filepath.Join(c.LogsDir(), "soong_build_metrics.pb")
Colin Crossaa9a2732023-10-27 10:54:27 -07001582}
1583
Dan Willemsenf052f782017-05-18 15:29:04 -07001584func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001585 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001586}
1587
Dan Willemsen02781d52017-05-12 19:28:13 -07001588func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001589 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1590}
1591
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001592func (c *configImpl) KatiPackageMkDir() string {
1593 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1594}
1595
Dan Willemsenf052f782017-05-18 15:29:04 -07001596func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001597 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001598}
1599
1600func (c *configImpl) HostOut() string {
1601 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1602}
1603
1604// This probably needs to be multi-valued, so not exporting it for now
1605func (c *configImpl) hostCrossOut() string {
1606 if runtime.GOOS == "linux" {
1607 return filepath.Join(c.hostOutRoot(), "windows-x86")
1608 } else {
1609 return ""
1610 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001611}
1612
Dan Willemsen1e704462016-08-21 15:17:17 -07001613func (c *configImpl) HostPrebuiltTag() string {
1614 if runtime.GOOS == "linux" {
1615 return "linux-x86"
1616 } else if runtime.GOOS == "darwin" {
1617 return "darwin-x86"
1618 } else {
1619 panic("Unsupported OS")
1620 }
1621}
Dan Willemsenf173d592017-04-27 14:28:00 -07001622
Taylor Santiago3c16e612024-05-30 14:41:31 -07001623func (c *configImpl) KatiBin() string {
1624 binName := "ckati"
1625 if c.UseABFS() {
1626 binName = "ckati-wrap"
1627 }
1628
1629 return c.PrebuiltBuildTool(binName)
1630}
1631
1632func (c *configImpl) NinjaBin() string {
1633 binName := "ninja"
1634 if c.UseABFS() {
1635 binName = "ninjago"
1636 }
1637 return c.PrebuiltBuildTool(binName)
1638}
1639
Cole Faust4e58bba2024-08-22 14:27:03 -07001640func (c *configImpl) N2Bin() string {
1641 path := c.PrebuiltBuildTool("n2")
1642 // Use musl instead of glibc because glibc on the build server is old and has bugs
1643 return strings.ReplaceAll(path, "/linux-x86/", "/linux_musl-x86/")
1644}
1645
Dan Willemsen8122bd52017-10-12 20:20:41 -07001646func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001647 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1648 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001649 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1650 if _, err := os.Stat(asan); err == nil {
1651 return asan
1652 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001653 }
1654 }
1655 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1656}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001657
1658func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1659 c.brokenDupRules = val
1660}
1661
1662func (c *configImpl) BuildBrokenDupRules() bool {
1663 return c.brokenDupRules
1664}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001665
Dan Willemsen25e6f092019-04-09 10:22:43 -07001666func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1667 c.brokenUsesNetwork = val
1668}
1669
1670func (c *configImpl) BuildBrokenUsesNetwork() bool {
1671 return c.brokenUsesNetwork
1672}
1673
Dan Willemsene3336352020-01-02 19:10:38 -08001674func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1675 c.brokenNinjaEnvVars = val
1676}
1677
1678func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1679 return c.brokenNinjaEnvVars
1680}
1681
Spandan Das28a6f192024-07-01 21:00:25 +00001682func (c *configImpl) SetBuildBrokenMissingOutputs(val bool) {
1683 c.brokenMissingOutputs = val
1684}
1685
1686func (c *configImpl) BuildBrokenMissingOutputs() bool {
1687 return c.brokenMissingOutputs
1688}
1689
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001690func (c *configImpl) SetTargetDeviceDir(dir string) {
1691 c.targetDeviceDir = dir
1692}
1693
1694func (c *configImpl) TargetDeviceDir() string {
1695 return c.targetDeviceDir
1696}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001697
Patrice Arruda219eef32020-06-01 17:29:30 +00001698func (c *configImpl) BuildDateTime() string {
1699 return c.buildDateTime
1700}
1701
1702func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001703 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001704}
Patrice Arruda83842d72020-12-08 19:42:08 +00001705
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001706// LogsDir returns the absolute path to the logs directory where build log and
1707// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001708// directory. If the argument dist is specified, the logs directory
1709// is <dist_dir>/logs.
1710func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001711 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001712 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001713 // 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 -05001714 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001715 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001716 absDir, err := filepath.Abs(dir)
1717 if err != nil {
1718 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1719 os.Exit(1)
1720 }
1721 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001722}
1723
Chris Parsons53f68ae2022-03-03 12:01:40 -05001724// MkFileMetrics returns the file path for make-related metrics.
1725func (c *configImpl) MkMetrics() string {
1726 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1727}
1728
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001729func (c *configImpl) SetEmptyNinjaFile(v bool) {
1730 c.emptyNinjaFile = v
1731}
1732
1733func (c *configImpl) EmptyNinjaFile() bool {
1734 return c.emptyNinjaFile
1735}
Yu Liu6e13b402021-07-27 14:29:06 -07001736
MarkDacekd0e7cd32022-12-02 22:22:40 +00001737func (c *configImpl) SkipMetricsUpload() bool {
Taylor Santiago8b0bed72024-09-03 13:30:22 -07001738 // b/362625275 - Metrics upload sometimes prevents abfs unmount
1739 if c.UseABFS() {
1740 return true
1741 }
1742
MarkDacekd0e7cd32022-12-02 22:22:40 +00001743 return c.skipMetricsUpload
1744}
1745
MarkDacekf47e1422023-04-19 16:47:36 +00001746func (c *configImpl) EnsureAllowlistIntegrity() bool {
1747 return c.ensureAllowlistIntegrity
1748}
1749
MarkDacek6614d9c2022-12-07 21:57:38 +00001750// Returns a Time object if one was passed via a command-line flag.
1751// Otherwise returns the passed default.
1752func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time {
1753 if c.buildStartedTime == 0 {
1754 return defaultTime
1755 }
1756 return time.UnixMilli(c.buildStartedTime)
1757}
1758
Yu Liu6e13b402021-07-27 14:29:06 -07001759func GetMetricsUploader(topDir string, env *Environment) string {
1760 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1761 metricsUploader := filepath.Join(topDir, p)
1762 if _, err := os.Stat(metricsUploader); err == nil {
1763 return metricsUploader
1764 }
1765 }
1766
1767 return ""
1768}