blob: ef2e87e0d50c8eef4fd9253963ec113fe81a213d [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050018 "context"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050019 "encoding/json"
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040020 "fmt"
Kousik Kumar3ff037e2022-01-25 22:11:01 -050021 "io/ioutil"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040022 "math/rand"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080023 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050024 "os/exec"
Dan Willemsen1e704462016-08-21 15:17:17 -070025 "path/filepath"
26 "runtime"
27 "strconv"
28 "strings"
Kousik Kumar4c180ad2022-05-27 07:48:37 -040029 "syscall"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080030 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070031
32 "android/soong/shared"
Kousik Kumarec478642020-09-21 13:39:24 -040033
Dan Willemsen4591b642021-05-24 14:24:12 -070034 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000035
36 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070037)
38
Kousik Kumar3ff037e2022-01-25 22:11:01 -050039const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050040 envConfigDir = "vendor/google/tools/soong_config"
41 jsonSuffix = "json"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050042
Chris Parsons53f68ae2022-03-03 12:01:40 -050043 configFetcher = "vendor/google/tools/soong/expconfigfetcher"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050044 envConfigFetchTimeout = 10 * time.Second
Kousik Kumar3ff037e2022-01-25 22:11:01 -050045)
46
Kousik Kumar4c180ad2022-05-27 07:48:37 -040047var (
48 rbeRandPrefix int
49)
50
51func init() {
52 rand.Seed(time.Now().UnixNano())
53 rbeRandPrefix = rand.Intn(1000)
54}
55
Dan Willemsen1e704462016-08-21 15:17:17 -070056type Config struct{ *configImpl }
57
58type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020059 // Some targets that are implemented in soong_build
60 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080061 arguments []string
62 goma bool
63 environ *Environment
64 distDir string
65 buildDateTime string
Dan Willemsen1e704462016-08-21 15:17:17 -070066
67 // From the arguments
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020068 parallel int
69 keepGoing int
70 verbose bool
71 checkbuild bool
72 dist bool
73 jsonModuleGraph bool
Spandan Das5af0bd32022-09-28 20:43:08 +000074 apiBp2build bool // Generate BUILD files for Soong modules that contribute APIs
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020075 bp2build bool
Lukacs T. Berki3a821692021-09-06 17:08:02 +020076 queryview bool
Chris Parsons53f68ae2022-03-03 12:01:40 -050077 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
Lukacs T. Berkic6012f32021-09-06 18:31:46 +020078 soongDocs bool
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020079 skipConfig bool
80 skipKati bool
81 skipKatiNinja bool
82 skipSoong bool
83 skipNinja bool
84 skipSoongTests bool
Spandan Das394aa322022-11-03 17:02:10 +000085 searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces
Dan Willemsen1e704462016-08-21 15:17:17 -070086
87 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070088 katiArgs []string
89 ninjaArgs []string
90 katiSuffix string
91 targetDevice string
92 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +000093 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -070094
Dan Willemsen2bb82d02019-12-27 09:35:42 -080095 // Autodetected
96 totalRAM uint64
97
Dan Willemsene3336352020-01-02 19:10:38 -080098 brokenDupRules bool
99 brokenUsesNetwork bool
100 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -0700101
102 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000103
MarkDacekb78465d2022-10-18 20:10:16 +0000104 bazelProdMode bool
105 bazelDevMode bool
106 bazelStagingMode bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000107
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700108 // Set by multiproduct_kati
109 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -0700110
111 metricsUploader string
MarkDacekd06db5d2022-11-29 00:47:59 +0000112
113 bazelForceEnabledModules string
Spandan Dasc5763832022-11-08 18:42:16 +0000114
115 includeTags []string
Dan Willemsen1e704462016-08-21 15:17:17 -0700116}
117
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800118const srcDirFileCheck = "build/soong/root.bp"
119
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700120var buildFiles = []string{"Android.mk", "Android.bp"}
121
Patrice Arruda13848222019-04-22 17:12:02 -0700122type BuildAction uint
123
124const (
125 // Builds all of the modules and their dependencies of a specified directory, relative to the root
126 // directory of the source tree.
127 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
128
129 // Builds all of the modules and their dependencies of a list of specified directories. All specified
130 // directories are relative to the root directory of the source tree.
131 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700132
133 // Build a list of specified modules. If none was specified, simply build the whole source tree.
134 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700135)
136
137// checkTopDir validates that the current directory is at the root directory of the source tree.
138func checkTopDir(ctx Context) {
139 if _, err := os.Stat(srcDirFileCheck); err != nil {
140 if os.IsNotExist(err) {
141 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
142 }
143 ctx.Fatalln("Error verifying tree state:", err)
144 }
145}
146
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500147// fetchEnvConfig optionally fetches environment config from an
148// experiments system to control Soong features dynamically.
149func fetchEnvConfig(ctx Context, config *configImpl, envConfigName string) error {
David Goldsmith62243a32022-04-08 13:42:04 +0000150 configName := envConfigName + "." + jsonSuffix
Kousik Kumarc75e1292022-07-07 02:20:51 +0000151 expConfigFetcher := &smpb.ExpConfigFetcher{Filename: &configName}
David Goldsmith62243a32022-04-08 13:42:04 +0000152 defer func() {
153 ctx.Metrics.ExpConfigFetcher(expConfigFetcher)
154 }()
Kousik Kumarc75e1292022-07-07 02:20:51 +0000155 if !config.GoogleProdCredsExist() {
156 status := smpb.ExpConfigFetcher_MISSING_GCERT
157 expConfigFetcher.Status = &status
158 return nil
159 }
David Goldsmith62243a32022-04-08 13:42:04 +0000160
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500161 s, err := os.Stat(configFetcher)
162 if err != nil {
163 if os.IsNotExist(err) {
164 return nil
165 }
166 return err
167 }
168 if s.Mode()&0111 == 0 {
David Goldsmith62243a32022-04-08 13:42:04 +0000169 status := smpb.ExpConfigFetcher_ERROR
170 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500171 return fmt.Errorf("configuration fetcher binary %v is not executable: %v", configFetcher, s.Mode())
172 }
173
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500174 tCtx, cancel := context.WithTimeout(ctx, envConfigFetchTimeout)
175 defer cancel()
David Goldsmith62243a32022-04-08 13:42:04 +0000176 fetchStart := time.Now()
177 cmd := exec.CommandContext(tCtx, configFetcher, "-output_config_dir", config.OutDir(),
178 "-output_config_name", configName)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500179 if err := cmd.Start(); err != nil {
David Goldsmith62243a32022-04-08 13:42:04 +0000180 status := smpb.ExpConfigFetcher_ERROR
181 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500182 return err
183 }
184
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500185 if err := cmd.Wait(); err != nil {
David Goldsmith62243a32022-04-08 13:42:04 +0000186 status := smpb.ExpConfigFetcher_ERROR
187 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500188 return err
189 }
David Goldsmith62243a32022-04-08 13:42:04 +0000190 fetchEnd := time.Now()
191 expConfigFetcher.Micros = proto.Uint64(uint64(fetchEnd.Sub(fetchStart).Microseconds()))
192 outConfigFilePath := filepath.Join(config.OutDir(), configName)
193 expConfigFetcher.Filename = proto.String(outConfigFilePath)
194 if _, err := os.Stat(outConfigFilePath); err == nil {
195 status := smpb.ExpConfigFetcher_CONFIG
196 expConfigFetcher.Status = &status
197 } else {
198 status := smpb.ExpConfigFetcher_NO_CONFIG
199 expConfigFetcher.Status = &status
200 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500201 return nil
202}
203
204func loadEnvConfig(ctx Context, config *configImpl) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500205 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
206 if bc == "" {
207 return nil
208 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500209
210 if err := fetchEnvConfig(ctx, config, bc); err != nil {
Kousik Kumar595fb1c2022-06-24 16:49:52 +0000211 ctx.Verbosef("Failed to fetch config file: %v\n", err)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500212 }
213
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500214 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500215 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500216 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500217 envConfigDir,
218 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500219 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500220 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
221 envVarsJSON, err := ioutil.ReadFile(cfgFile)
222 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500223 continue
224 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500225 ctx.Verbosef("Loading config file %v\n", cfgFile)
226 var envVars map[string]map[string]string
227 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
228 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
229 continue
230 }
231 for k, v := range envVars["env"] {
232 if os.Getenv(k) != "" {
233 continue
234 }
235 config.environ.Set(k, v)
236 }
237 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
238 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500239 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500240
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500241 return nil
242}
243
Chris Parsonsb6e96902022-10-31 20:08:45 -0400244func defaultBazelProdMode(cfg *configImpl) bool {
MarkDacekd06db5d2022-11-29 00:47:59 +0000245 // Environment flag to disable Bazel for users which experience
Chris Parsonsb6e96902022-10-31 20:08:45 -0400246 // broken bazel-handled builds, or significant performance regressions.
247 if cfg.IsBazelMixedBuildForceDisabled() {
248 return false
249 }
250 // Darwin-host builds are currently untested with Bazel.
251 if runtime.GOOS == "darwin" {
252 return false
253 }
Chris Parsons035e03a2022-11-01 14:25:45 -0400254 return true
Chris Parsonsb6e96902022-10-31 20:08:45 -0400255}
256
Dan Willemsen1e704462016-08-21 15:17:17 -0700257func NewConfig(ctx Context, args ...string) Config {
258 ret := &configImpl{
Spandan Dasa3639e62021-05-25 19:14:02 +0000259 environ: OsEnvironment(),
260 sandboxConfig: &SandboxConfig{},
Dan Willemsen1e704462016-08-21 15:17:17 -0700261 }
262
Patrice Arruda90109172020-07-28 18:07:27 +0000263 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700264 ret.parallel = runtime.NumCPU() + 2
265 ret.keepGoing = 1
266
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800267 ret.totalRAM = detectTotalRAM(ctx)
268
Dan Willemsen9b587492017-07-10 22:13:00 -0700269 ret.parseArgs(ctx, args)
270
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800271 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700272 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
273 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
274 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800275 outDir := "out"
276 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
277 if wd, err := os.Getwd(); err != nil {
278 ctx.Fatalln("Failed to get working directory:", err)
279 } else {
280 outDir = filepath.Join(baseDir, filepath.Base(wd))
281 }
282 }
283 ret.environ.Set("OUT_DIR", outDir)
284 }
285
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500286 // loadEnvConfig needs to know what the OUT_DIR is, so it should
287 // be called after we determine the appropriate out directory.
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500288 if err := loadEnvConfig(ctx, ret); err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500289 ctx.Fatalln("Failed to parse env config files: %v", err)
290 }
291
Dan Willemsen2d31a442018-10-20 21:33:41 -0700292 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
293 ret.distDir = filepath.Clean(distDir)
294 } else {
295 ret.distDir = filepath.Join(ret.OutDir(), "dist")
296 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700297
Spandan Das05063612021-06-25 01:39:04 +0000298 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
299 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
300 }
301
Dan Willemsen1e704462016-08-21 15:17:17 -0700302 ret.environ.Unset(
303 // We're already using it
304 "USE_SOONG_UI",
305
306 // We should never use GOROOT/GOPATH from the shell environment
307 "GOROOT",
308 "GOPATH",
309
310 // These should only come from Soong, not the environment.
311 "CLANG",
312 "CLANG_CXX",
313 "CCC_CC",
314 "CCC_CXX",
315
316 // Used by the goma compiler wrapper, but should only be set by
317 // gomacc
318 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800319
320 // We handle this above
321 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700322
Dan Willemsen2d31a442018-10-20 21:33:41 -0700323 // This is handled above too, and set for individual commands later
324 "DIST_DIR",
325
Dan Willemsen68a09852017-04-18 13:56:57 -0700326 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000327 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700328 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700329 "DISPLAY",
330 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700331 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700332 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700333
334 // Drop make flags
335 "MAKEFLAGS",
336 "MAKELEVEL",
337 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700338
339 // Set in envsetup.sh, reset in makefiles
340 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700341
342 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
343 "ANDROID_BUILD_TOP",
344 "ANDROID_HOST_OUT",
345 "ANDROID_PRODUCT_OUT",
346 "ANDROID_HOST_OUT_TESTCASES",
347 "ANDROID_TARGET_OUT_TESTCASES",
348 "ANDROID_TOOLCHAIN",
349 "ANDROID_TOOLCHAIN_2ND_ARCH",
350 "ANDROID_DEV_SCRIPTS",
351 "ANDROID_EMULATOR_PREBUILTS",
352 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700353 )
354
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400355 if ret.UseGoma() || ret.ForceUseGoma() {
356 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
357 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400358 }
359
Dan Willemsen1e704462016-08-21 15:17:17 -0700360 // Tell python not to spam the source tree with .pyc files.
361 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
362
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400363 tmpDir := absPath(ctx, ret.TempDir())
364 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800365
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700366 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
367 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
368 "llvm-binutils-stable/llvm-symbolizer")
369 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
370
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800371 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700372 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800373
Yu Liu6e13b402021-07-27 14:29:06 -0700374 srcDir := absPath(ctx, ".")
375 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700376 ctx.Println("You are building in a directory whose absolute path contains a space character:")
377 ctx.Println()
378 ctx.Printf("%q\n", srcDir)
379 ctx.Println()
380 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700381 }
382
Yu Liu6e13b402021-07-27 14:29:06 -0700383 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
384
Dan Willemsendb8457c2017-05-12 16:38:17 -0700385 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700386 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
387 ctx.Println()
388 ctx.Printf("%q\n", outDir)
389 ctx.Println()
390 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700391 }
392
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000393 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700394 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
395 ctx.Println()
396 ctx.Printf("%q\n", distDir)
397 ctx.Println()
398 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700399 }
400
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700401 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000402 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
403 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100404 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Colin Cross59c1e6a2022-03-04 13:37:19 -0800405 java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700406 javaHome := func() string {
407 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
408 return override
409 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000410 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
411 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 +0100412 }
Sorin Basca7e094b32022-10-05 08:20:12 +0000413 if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" {
414 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 17 toolchain is now the global default.")
415 }
416 return java17Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700417 }()
418 absJavaHome := absPath(ctx, javaHome)
419
Dan Willemsened869522018-01-08 14:58:46 -0800420 ret.configureLocale(ctx)
421
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700422 newPath := []string{filepath.Join(absJavaHome, "bin")}
423 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
424 newPath = append(newPath, path)
425 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100426
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700427 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
428 ret.environ.Set("JAVA_HOME", absJavaHome)
429 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000430 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
431 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100432 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700433 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
434
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800435 outDir := ret.OutDir()
436 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800437 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800438 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800439 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800440 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800441 }
Colin Cross28f527c2019-11-26 16:19:04 -0800442
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800443 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
444
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400445 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400446 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400447 ret.environ.Set(k, v)
448 }
449 }
450
Patrice Arruda83842d72020-12-08 19:42:08 +0000451 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800452 if err := os.RemoveAll(bpd); err != nil {
453 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
454 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000455
Patrice Arruda96850362020-08-11 20:41:11 +0000456 c := Config{ret}
457 storeConfigMetrics(ctx, c)
458 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700459}
460
Patrice Arruda13848222019-04-22 17:12:02 -0700461// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
462// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700463func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
464 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700465}
466
Patrice Arruda96850362020-08-11 20:41:11 +0000467// storeConfigMetrics selects a set of configuration information and store in
468// the metrics system for further analysis.
469func storeConfigMetrics(ctx Context, config Config) {
470 if ctx.Metrics == nil {
471 return
472 }
473
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400474 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000475
476 s := &smpb.SystemResourceInfo{
477 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
478 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
479 }
480 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000481}
482
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400483func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700484 c := &smpb.BuildConfig{
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -0400485 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
486 UseGoma: proto.Bool(config.UseGoma()),
487 UseRbe: proto.Bool(config.UseRBE()),
488 BazelMixedBuild: proto.Bool(config.BazelBuildEnabled()),
489 ForceDisableBazelMixedBuild: proto.Bool(config.IsBazelMixedBuildForceDisabled()),
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400490 }
Yu Liue737a992021-10-04 13:21:41 -0700491 c.Targets = append(c.Targets, config.arguments...)
492
493 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400494}
495
Patrice Arruda13848222019-04-22 17:12:02 -0700496// getConfigArgs processes the command arguments based on the build action and creates a set of new
497// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700498func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700499 // The next block of code verifies that the current directory is the root directory of the source
500 // tree. It then finds the relative path of dir based on the root directory of the source tree
501 // and verify that dir is inside of the source tree.
502 checkTopDir(ctx)
503 topDir, err := os.Getwd()
504 if err != nil {
505 ctx.Fatalf("Error retrieving top directory: %v", err)
506 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700507 dir, err = filepath.EvalSymlinks(dir)
508 if err != nil {
509 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
510 }
Patrice Arruda13848222019-04-22 17:12:02 -0700511 dir, err = filepath.Abs(dir)
512 if err != nil {
513 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
514 }
515 relDir, err := filepath.Rel(topDir, dir)
516 if err != nil {
517 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
518 }
519 // If there are ".." in the path, it's not in the source tree.
520 if strings.Contains(relDir, "..") {
521 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
522 }
523
524 configArgs := args[:]
525
526 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
527 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
528 targetNamePrefix := "MODULES-IN-"
529 if inList("GET-INSTALL-PATH", configArgs) {
530 targetNamePrefix = "GET-INSTALL-PATH-IN-"
531 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
532 }
533
Patrice Arruda13848222019-04-22 17:12:02 -0700534 var targets []string
535
536 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700537 case BUILD_MODULES:
538 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700539 case BUILD_MODULES_IN_A_DIRECTORY:
540 // If dir is the root source tree, all the modules are built of the source tree are built so
541 // no need to find the build file.
542 if topDir == dir {
543 break
544 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700545
Patrice Arruda13848222019-04-22 17:12:02 -0700546 buildFile := findBuildFile(ctx, relDir)
547 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700548 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700549 }
Patrice Arruda13848222019-04-22 17:12:02 -0700550 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
551 case BUILD_MODULES_IN_DIRECTORIES:
552 newConfigArgs, dirs := splitArgs(configArgs)
553 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700554 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700555 }
556
557 // Tidy only override all other specified targets.
558 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
559 if tidyOnly == "true" || tidyOnly == "1" {
560 configArgs = append(configArgs, "tidy_only")
561 } else {
562 configArgs = append(configArgs, targets...)
563 }
564
565 return configArgs
566}
567
568// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
569func convertToTarget(dir string, targetNamePrefix string) string {
570 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
571}
572
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700573// hasBuildFile returns true if dir contains an Android build file.
574func hasBuildFile(ctx Context, dir string) bool {
575 for _, buildFile := range buildFiles {
576 _, err := os.Stat(filepath.Join(dir, buildFile))
577 if err == nil {
578 return true
579 }
580 if !os.IsNotExist(err) {
581 ctx.Fatalf("Error retrieving the build file stats: %v", err)
582 }
583 }
584 return false
585}
586
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700587// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
588// in the current and any sub directory of dir. If a build file is not found, traverse the path
589// up by one directory and repeat again until either a build file is found or reached to the root
590// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
591// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700592func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700593 // If the string is empty or ".", assume it is top directory of the source tree.
594 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700595 return ""
596 }
597
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700598 found := false
599 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
600 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
601 if err != nil {
602 return err
603 }
604 if found {
605 return filepath.SkipDir
606 }
607 if info.IsDir() {
608 return nil
609 }
610 for _, buildFile := range buildFiles {
611 if info.Name() == buildFile {
612 found = true
613 return filepath.SkipDir
614 }
615 }
616 return nil
617 })
618 if err != nil {
619 ctx.Fatalf("Error finding Android build file: %v", err)
620 }
621
622 if found {
623 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700624 }
625 }
626
627 return ""
628}
629
630// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
631func splitArgs(args []string) (newArgs []string, dirs []string) {
632 specialArgs := map[string]bool{
633 "showcommands": true,
634 "snod": true,
635 "dist": true,
636 "checkbuild": true,
637 }
638
639 newArgs = []string{}
640 dirs = []string{}
641
642 for _, arg := range args {
643 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
644 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
645 newArgs = append(newArgs, arg)
646 continue
647 }
648
649 if _, ok := specialArgs[arg]; ok {
650 newArgs = append(newArgs, arg)
651 continue
652 }
653
654 dirs = append(dirs, arg)
655 }
656
657 return newArgs, dirs
658}
659
660// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
661// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
662// source root tree where the build action command was invoked. Each directory is validated if the
663// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700664func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700665 for _, dir := range dirs {
666 // The directory may have specified specific modules to build. ":" is the separator to separate
667 // the directory and the list of modules.
668 s := strings.Split(dir, ":")
669 l := len(s)
670 if l > 2 { // more than one ":" was specified.
671 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
672 }
673
674 dir = filepath.Join(relDir, s[0])
675 if _, err := os.Stat(dir); err != nil {
676 ctx.Fatalf("couldn't find directory %s", dir)
677 }
678
679 // Verify that if there are any targets specified after ":". Each target is separated by ",".
680 var newTargets []string
681 if l == 2 && s[1] != "" {
682 newTargets = strings.Split(s[1], ",")
683 if inList("", newTargets) {
684 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
685 }
686 }
687
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700688 // If there are specified targets to build in dir, an android build file must exist for the one
689 // shot build. For the non-targets case, find the appropriate build file and build all the
690 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700691 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700692 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700693 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
694 }
695 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700696 buildFile := findBuildFile(ctx, dir)
697 if buildFile == "" {
698 ctx.Fatalf("Build file not found for %s directory", dir)
699 }
700 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700701 }
702
Patrice Arruda13848222019-04-22 17:12:02 -0700703 targets = append(targets, newTargets...)
704 }
705
Dan Willemsence41e942019-07-29 23:39:30 -0700706 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700707}
708
Dan Willemsen9b587492017-07-10 22:13:00 -0700709func (c *configImpl) parseArgs(ctx Context, args []string) {
710 for i := 0; i < len(args); i++ {
711 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100712 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700713 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200714 } else if arg == "--empty-ninja-file" {
715 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100716 } else if arg == "--skip-ninja" {
717 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700718 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700719 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
720 // but it does run a Kati ninja file if the .kati_enabled marker file was created
721 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000722 c.skipConfig = true
723 c.skipKati = true
724 } else if arg == "--skip-kati" {
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100725 // TODO: remove --skip-kati once module builds have been migrated to --song-only
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000726 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100727 } else if arg == "--soong-only" {
728 c.skipKati = true
729 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200730 } else if arg == "--config-only" {
731 c.skipKati = true
732 c.skipKatiNinja = true
733 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700734 } else if arg == "--skip-config" {
735 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700736 } else if arg == "--skip-soong-tests" {
737 c.skipSoongTests = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500738 } else if arg == "--mk-metrics" {
739 c.reportMkMetrics = true
Chris Parsonsef615e52022-08-18 22:04:11 -0400740 } else if arg == "--bazel-mode" {
741 c.bazelProdMode = true
742 } else if arg == "--bazel-mode-dev" {
743 c.bazelDevMode = true
MarkDacekb78465d2022-10-18 20:10:16 +0000744 } else if arg == "--bazel-mode-staging" {
745 c.bazelStagingMode = true
Spandan Das394aa322022-11-03 17:02:10 +0000746 } else if arg == "--search-api-dir" {
747 c.searchApiDir = true
MarkDacekb96561e2022-12-02 04:34:43 +0000748 } else if strings.HasPrefix(arg, "--build-command=") {
749 buildCmd := strings.TrimPrefix(arg, "--build-command=")
750 // remove quotations
751 buildCmd = strings.TrimPrefix(buildCmd, "\"")
752 buildCmd = strings.TrimSuffix(buildCmd, "\"")
753 ctx.Metrics.SetBuildCommand([]string{buildCmd})
MarkDacekd06db5d2022-11-29 00:47:59 +0000754 } else if strings.HasPrefix(arg, "--bazel-force-enabled-modules=") {
755 c.bazelForceEnabledModules = strings.TrimPrefix(arg, "--bazel-force-enabled-modules=")
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700756 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700757 parseArgNum := func(def int) int {
758 if len(arg) > 2 {
759 p, err := strconv.ParseUint(arg[2:], 10, 31)
760 if err != nil {
761 ctx.Fatalf("Failed to parse %q: %v", arg, err)
762 }
763 return int(p)
764 } else if i+1 < len(args) {
765 p, err := strconv.ParseUint(args[i+1], 10, 31)
766 if err == nil {
767 i++
768 return int(p)
769 }
770 }
771 return def
772 }
773
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700774 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700775 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700776 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700777 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700778 } else {
779 ctx.Fatalln("Unknown option:", arg)
780 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700781 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700782 if k == "OUT_DIR" {
783 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
784 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700785 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700786 } else if arg == "dist" {
787 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200788 } else if arg == "json-module-graph" {
789 c.jsonModuleGraph = true
790 } else if arg == "bp2build" {
791 c.bp2build = true
Spandan Das5af0bd32022-09-28 20:43:08 +0000792 } else if arg == "api_bp2build" {
793 c.apiBp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200794 } else if arg == "queryview" {
795 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200796 } else if arg == "soong_docs" {
797 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700798 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700799 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800800 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700801 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700802 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700803 }
804 }
Chris Parsonsb6e96902022-10-31 20:08:45 -0400805 if (!c.bazelProdMode) && (!c.bazelDevMode) && (!c.bazelStagingMode) {
806 c.bazelProdMode = defaultBazelProdMode(c)
807 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700808}
809
Dan Willemsened869522018-01-08 14:58:46 -0800810func (c *configImpl) configureLocale(ctx Context) {
811 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
812 output, err := cmd.Output()
813
814 var locales []string
815 if err == nil {
816 locales = strings.Split(string(output), "\n")
817 } else {
818 // If we're unable to list the locales, let's assume en_US.UTF-8
819 locales = []string{"en_US.UTF-8"}
820 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
821 }
822
823 // gettext uses LANGUAGE, which is passed directly through
824
825 // For LANG and LC_*, only preserve the evaluated version of
826 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800827 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800828 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800829 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800830 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800831 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800832 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800833 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800834 }
835
836 c.environ.UnsetWithPrefix("LC_")
837
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800838 if userLang != "" {
839 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800840 }
841
842 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
843 // for others)
844 if inList("C.UTF-8", locales) {
845 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500846 } else if inList("C.utf8", locales) {
847 // These normalize to the same thing
848 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800849 } else if inList("en_US.UTF-8", locales) {
850 c.environ.Set("LANG", "en_US.UTF-8")
851 } else if inList("en_US.utf8", locales) {
852 // These normalize to the same thing
853 c.environ.Set("LANG", "en_US.UTF-8")
854 } else {
855 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
856 }
857}
858
Dan Willemsen1e704462016-08-21 15:17:17 -0700859func (c *configImpl) Environment() *Environment {
860 return c.environ
861}
862
863func (c *configImpl) Arguments() []string {
864 return c.arguments
865}
866
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200867func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200868 if len(c.Arguments()) > 0 {
869 // Explicit targets requested that are not special targets like b2pbuild
870 // or the JSON module graph
871 return true
872 }
873
Spandan Das5af0bd32022-09-28 20:43:08 +0000874 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() && !c.ApiBp2build() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200875 // Command line was empty, the default Ninja target is built
876 return true
877 }
878
Liz Kammer88677422021-12-15 15:03:19 -0500879 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
880 if c.Dist() && !c.Bp2Build() {
881 return true
882 }
883
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200884 // build.ninja doesn't need to be generated
885 return false
886}
887
Dan Willemsen1e704462016-08-21 15:17:17 -0700888func (c *configImpl) OutDir() string {
889 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700890 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700891 }
892 return "out"
893}
894
Dan Willemsen8a073a82017-02-04 17:30:44 -0800895func (c *configImpl) DistDir() string {
Chris Parsons19ab9a42022-08-30 13:15:04 -0400896 return c.distDir
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000897}
898
899func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700900 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800901}
902
Dan Willemsen1e704462016-08-21 15:17:17 -0700903func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000904 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700905 return c.arguments
906 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700907 return c.ninjaArgs
908}
909
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500910func (c *configImpl) BazelOutDir() string {
911 return filepath.Join(c.OutDir(), "bazel")
912}
913
Liz Kammer2af5ea82022-11-11 14:21:03 -0500914func (c *configImpl) bazelOutputBase() string {
915 return filepath.Join(c.BazelOutDir(), "output")
916}
917
Dan Willemsen1e704462016-08-21 15:17:17 -0700918func (c *configImpl) SoongOutDir() string {
919 return filepath.Join(c.OutDir(), "soong")
920}
921
Spandan Das394aa322022-11-03 17:02:10 +0000922func (c *configImpl) ApiSurfacesOutDir() string {
923 return filepath.Join(c.OutDir(), "api_surfaces")
924}
925
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200926func (c *configImpl) PrebuiltOS() string {
927 switch runtime.GOOS {
928 case "linux":
929 return "linux-x86"
930 case "darwin":
931 return "darwin-x86"
932 default:
933 panic("Unknown GOOS")
934 }
935}
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100936
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200937func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -0700938 if c.SkipKatiNinja() {
939 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
940 } else {
941 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
942 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200943}
944
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200945func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100946 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200947}
948
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200949func (c *configImpl) UsedEnvFile(tag string) string {
950 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
951}
952
Lukacs T. Berkic541cd22022-10-26 07:26:50 +0000953func (c *configImpl) Bp2BuildFilesMarkerFile() string {
954 return shared.JoinPath(c.SoongOutDir(), "bp2build_files_marker")
955}
956
957func (c *configImpl) Bp2BuildWorkspaceMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100958 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200959}
960
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200961func (c *configImpl) SoongDocsHtml() string {
962 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
963}
964
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200965func (c *configImpl) QueryviewMarkerFile() string {
966 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
967}
968
Spandan Das5af0bd32022-09-28 20:43:08 +0000969func (c *configImpl) ApiBp2buildMarkerFile() string {
970 return shared.JoinPath(c.SoongOutDir(), "api_bp2build.marker")
971}
972
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200973func (c *configImpl) ModuleGraphFile() string {
974 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
975}
976
kgui67007242022-01-25 13:50:25 +0800977func (c *configImpl) ModuleActionsFile() string {
978 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
979}
980
Jeff Gastonefc1b412017-03-29 17:29:06 -0700981func (c *configImpl) TempDir() string {
982 return shared.TempDirForOutDir(c.SoongOutDir())
983}
984
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700985func (c *configImpl) FileListDir() string {
986 return filepath.Join(c.OutDir(), ".module_paths")
987}
988
Dan Willemsen1e704462016-08-21 15:17:17 -0700989func (c *configImpl) KatiSuffix() string {
990 if c.katiSuffix != "" {
991 return c.katiSuffix
992 }
993 panic("SetKatiSuffix has not been called")
994}
995
Colin Cross37193492017-11-16 17:55:00 -0800996// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
997// user is interested in additional checks at the expense of build time.
998func (c *configImpl) Checkbuild() bool {
999 return c.checkbuild
1000}
1001
Dan Willemsen8a073a82017-02-04 17:30:44 -08001002func (c *configImpl) Dist() bool {
1003 return c.dist
1004}
1005
Lukacs T. Berkia1b93722021-09-02 17:23:06 +02001006func (c *configImpl) JsonModuleGraph() bool {
1007 return c.jsonModuleGraph
1008}
1009
1010func (c *configImpl) Bp2Build() bool {
1011 return c.bp2build
1012}
1013
Spandan Das5af0bd32022-09-28 20:43:08 +00001014func (c *configImpl) ApiBp2build() bool {
1015 return c.apiBp2build
1016}
1017
Lukacs T. Berki3a821692021-09-06 17:08:02 +02001018func (c *configImpl) Queryview() bool {
1019 return c.queryview
1020}
1021
Lukacs T. Berkic6012f32021-09-06 18:31:46 +02001022func (c *configImpl) SoongDocs() bool {
1023 return c.soongDocs
1024}
1025
Dan Willemsen1e704462016-08-21 15:17:17 -07001026func (c *configImpl) IsVerbose() bool {
1027 return c.verbose
1028}
1029
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001030func (c *configImpl) SkipKati() bool {
1031 return c.skipKati
1032}
1033
Anton Hansson0b55bdb2021-06-04 10:08:08 +01001034func (c *configImpl) SkipKatiNinja() bool {
1035 return c.skipKatiNinja
1036}
1037
Lukacs T. Berkicef87b62021-08-10 15:01:13 +02001038func (c *configImpl) SkipSoong() bool {
1039 return c.skipSoong
1040}
1041
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +01001042func (c *configImpl) SkipNinja() bool {
1043 return c.skipNinja
1044}
1045
Anton Hansson5a7861a2021-06-04 10:09:01 +01001046func (c *configImpl) SetSkipNinja(v bool) {
1047 c.skipNinja = v
1048}
1049
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001050func (c *configImpl) SkipConfig() bool {
1051 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001052}
1053
Dan Willemsen1e704462016-08-21 15:17:17 -07001054func (c *configImpl) TargetProduct() string {
1055 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1056 return v
1057 }
1058 panic("TARGET_PRODUCT is not defined")
1059}
1060
Dan Willemsen02781d52017-05-12 19:28:13 -07001061func (c *configImpl) TargetDevice() string {
1062 return c.targetDevice
1063}
1064
1065func (c *configImpl) SetTargetDevice(device string) {
1066 c.targetDevice = device
1067}
1068
1069func (c *configImpl) TargetBuildVariant() string {
1070 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1071 return v
1072 }
1073 panic("TARGET_BUILD_VARIANT is not defined")
1074}
1075
Dan Willemsen1e704462016-08-21 15:17:17 -07001076func (c *configImpl) KatiArgs() []string {
1077 return c.katiArgs
1078}
1079
1080func (c *configImpl) Parallel() int {
1081 return c.parallel
1082}
1083
Spandan Dasc5763832022-11-08 18:42:16 +00001084func (c *configImpl) GetIncludeTags() []string {
1085 return c.includeTags
1086}
1087
1088func (c *configImpl) SetIncludeTags(i []string) {
1089 c.includeTags = i
1090}
1091
Colin Cross8b8bec32019-11-15 13:18:43 -08001092func (c *configImpl) HighmemParallel() int {
1093 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1094 return i
1095 }
1096
1097 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1098 parallel := c.Parallel()
1099 if c.UseRemoteBuild() {
1100 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1101 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1102 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1103 // Return 1/16th of the size of the local pool, rounding up.
1104 return (parallel + 15) / 16
1105 } else if c.totalRAM == 0 {
1106 // Couldn't detect the total RAM, don't restrict highmem processes.
1107 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001108 } else if c.totalRAM <= 16*1024*1024*1024 {
1109 // Less than 16GB of ram, restrict to 1 highmem processes
1110 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001111 } else if c.totalRAM <= 32*1024*1024*1024 {
1112 // Less than 32GB of ram, restrict to 2 highmem processes
1113 return 2
1114 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1115 // If less than 8GB total RAM per process, reduce the number of highmem processes
1116 return p
1117 }
1118 // No restriction on highmem processes
1119 return parallel
1120}
1121
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001122func (c *configImpl) TotalRAM() uint64 {
1123 return c.totalRAM
1124}
1125
Kousik Kumarec478642020-09-21 13:39:24 -04001126// ForceUseGoma determines whether we should override Goma deprecation
1127// and use Goma for the current build or not.
1128func (c *configImpl) ForceUseGoma() bool {
1129 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1130 v = strings.TrimSpace(v)
1131 if v != "" && v != "false" {
1132 return true
1133 }
1134 }
1135 return false
1136}
1137
Dan Willemsen1e704462016-08-21 15:17:17 -07001138func (c *configImpl) UseGoma() bool {
1139 if v, ok := c.environ.Get("USE_GOMA"); ok {
1140 v = strings.TrimSpace(v)
1141 if v != "" && v != "false" {
1142 return true
1143 }
1144 }
1145 return false
1146}
1147
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001148func (c *configImpl) StartGoma() bool {
1149 if !c.UseGoma() {
1150 return false
1151 }
1152
1153 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1154 v = strings.TrimSpace(v)
1155 if v != "" && v != "false" {
1156 return false
1157 }
1158 }
1159 return true
1160}
1161
Ramy Medhatbbf25672019-07-17 12:30:04 +00001162func (c *configImpl) UseRBE() bool {
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001163 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001164 v = strings.TrimSpace(v)
1165 if v != "" && v != "false" {
1166 return true
1167 }
1168 }
1169 return false
1170}
1171
Chris Parsonsef615e52022-08-18 22:04:11 -04001172func (c *configImpl) BazelBuildEnabled() bool {
MarkDacekb78465d2022-10-18 20:10:16 +00001173 return c.bazelProdMode || c.bazelDevMode || c.bazelStagingMode
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001174}
1175
Ramy Medhatbbf25672019-07-17 12:30:04 +00001176func (c *configImpl) StartRBE() bool {
1177 if !c.UseRBE() {
1178 return false
1179 }
1180
1181 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1182 v = strings.TrimSpace(v)
1183 if v != "" && v != "false" {
1184 return false
1185 }
1186 }
1187 return true
1188}
1189
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001190func (c *configImpl) rbeProxyLogsDir() string {
1191 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001192 if v, ok := c.environ.Get(f); ok {
1193 return v
1194 }
1195 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001196 buildTmpDir := shared.TempDirForOutDir(c.SoongOutDir())
1197 return filepath.Join(buildTmpDir, "rbe")
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001198}
1199
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001200func (c *configImpl) shouldCleanupRBELogsDir() bool {
1201 // Perform a log directory cleanup only when the log directory
1202 // is auto created by the build rather than user-specified.
1203 for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} {
1204 if _, ok := c.environ.Get(f); ok {
1205 return false
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001206 }
1207 }
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001208 return true
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001209}
1210
1211func (c *configImpl) rbeExecRoot() string {
1212 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1213 if v, ok := c.environ.Get(f); ok {
1214 return v
1215 }
1216 }
1217 wd, err := os.Getwd()
1218 if err != nil {
1219 return ""
1220 }
1221 return wd
1222}
1223
1224func (c *configImpl) rbeDir() string {
1225 if v, ok := c.environ.Get("RBE_DIR"); ok {
1226 return v
1227 }
1228 return "prebuilts/remoteexecution-client/live/"
1229}
1230
1231func (c *configImpl) rbeReproxy() string {
1232 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1233 if v, ok := c.environ.Get(f); ok {
1234 return v
1235 }
1236 }
1237 return filepath.Join(c.rbeDir(), "reproxy")
1238}
1239
1240func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001241 credFlags := []string{
1242 "use_application_default_credentials",
1243 "use_gce_credentials",
1244 "credential_file",
1245 "use_google_prod_creds",
1246 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001247 for _, cf := range credFlags {
1248 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1249 if v, ok := c.environ.Get(f); ok {
1250 v = strings.TrimSpace(v)
1251 if v != "" && v != "false" && v != "0" {
1252 return "RBE_" + cf, v
1253 }
1254 }
1255 }
1256 }
1257 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001258}
1259
Kousik Kumar4c180ad2022-05-27 07:48:37 -04001260func (c *configImpl) rbeSockAddr(dir string) (string, error) {
1261 maxNameLen := len(syscall.RawSockaddrUnix{}.Path)
1262 base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix)
1263
1264 name := filepath.Join(dir, base)
1265 if len(name) < maxNameLen {
1266 return name, nil
1267 }
1268
1269 name = filepath.Join("/tmp", base)
1270 if len(name) < maxNameLen {
1271 return name, nil
1272 }
1273
1274 return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen)
1275}
1276
Kousik Kumar7bc78192022-04-27 14:52:56 -04001277// IsGooglerEnvironment returns true if the current build is running
1278// on a Google developer machine and false otherwise.
1279func (c *configImpl) IsGooglerEnvironment() bool {
1280 cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG"
1281 if v, ok := c.environ.Get(cf); ok {
1282 return v == "googler"
1283 }
1284 return false
1285}
1286
1287// GoogleProdCredsExist determine whether credentials exist on the
1288// Googler machine to use remote execution.
1289func (c *configImpl) GoogleProdCredsExist() bool {
1290 if _, err := exec.Command("/usr/bin/prodcertstatus", "--simple_output", "--nocheck_loas").Output(); err != nil {
1291 return false
1292 }
1293 return true
1294}
1295
1296// UseRemoteBuild indicates whether to use a remote build acceleration system
1297// to speed up the build.
Colin Cross9016b912019-11-11 14:57:42 -08001298func (c *configImpl) UseRemoteBuild() bool {
1299 return c.UseGoma() || c.UseRBE()
1300}
1301
Kousik Kumar7bc78192022-04-27 14:52:56 -04001302// StubbyExists checks whether the stubby binary exists on the machine running
1303// the build.
1304func (c *configImpl) StubbyExists() bool {
1305 if _, err := exec.LookPath("stubby"); err != nil {
1306 return false
1307 }
1308 return true
1309}
1310
Dan Willemsen1e704462016-08-21 15:17:17 -07001311// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001312// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001313// still limited by Parallel()
1314func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001315 if !c.UseRemoteBuild() {
1316 return 0
1317 }
1318 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1319 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001320 }
1321 return 500
1322}
1323
1324func (c *configImpl) SetKatiArgs(args []string) {
1325 c.katiArgs = args
1326}
1327
1328func (c *configImpl) SetNinjaArgs(args []string) {
1329 c.ninjaArgs = args
1330}
1331
1332func (c *configImpl) SetKatiSuffix(suffix string) {
1333 c.katiSuffix = suffix
1334}
1335
Dan Willemsene0879fc2017-08-04 15:06:27 -07001336func (c *configImpl) LastKatiSuffixFile() string {
1337 return filepath.Join(c.OutDir(), "last_kati_suffix")
1338}
1339
1340func (c *configImpl) HasKatiSuffix() bool {
1341 return c.katiSuffix != ""
1342}
1343
Dan Willemsen1e704462016-08-21 15:17:17 -07001344func (c *configImpl) KatiEnvFile() string {
1345 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1346}
1347
Dan Willemsen29971232018-09-26 14:58:30 -07001348func (c *configImpl) KatiBuildNinjaFile() string {
1349 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001350}
1351
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001352func (c *configImpl) KatiPackageNinjaFile() string {
1353 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1354}
1355
Jihoon Kang9f4f8a32022-08-16 00:57:30 +00001356func (c *configImpl) SoongVarsFile() string {
1357 return filepath.Join(c.SoongOutDir(), "soong.variables")
1358}
1359
Dan Willemsen1e704462016-08-21 15:17:17 -07001360func (c *configImpl) SoongNinjaFile() string {
1361 return filepath.Join(c.SoongOutDir(), "build.ninja")
1362}
1363
1364func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001365 if c.katiSuffix == "" {
1366 return filepath.Join(c.OutDir(), "combined.ninja")
1367 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001368 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1369}
1370
1371func (c *configImpl) SoongAndroidMk() string {
1372 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1373}
1374
1375func (c *configImpl) SoongMakeVarsMk() string {
1376 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1377}
1378
Dan Willemsenf052f782017-05-18 15:29:04 -07001379func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001380 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001381}
1382
Dan Willemsen02781d52017-05-12 19:28:13 -07001383func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001384 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1385}
1386
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001387func (c *configImpl) KatiPackageMkDir() string {
1388 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1389}
1390
Dan Willemsenf052f782017-05-18 15:29:04 -07001391func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001392 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001393}
1394
1395func (c *configImpl) HostOut() string {
1396 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1397}
1398
1399// This probably needs to be multi-valued, so not exporting it for now
1400func (c *configImpl) hostCrossOut() string {
1401 if runtime.GOOS == "linux" {
1402 return filepath.Join(c.hostOutRoot(), "windows-x86")
1403 } else {
1404 return ""
1405 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001406}
1407
Dan Willemsen1e704462016-08-21 15:17:17 -07001408func (c *configImpl) HostPrebuiltTag() string {
1409 if runtime.GOOS == "linux" {
1410 return "linux-x86"
1411 } else if runtime.GOOS == "darwin" {
1412 return "darwin-x86"
1413 } else {
1414 panic("Unsupported OS")
1415 }
1416}
Dan Willemsenf173d592017-04-27 14:28:00 -07001417
Dan Willemsen8122bd52017-10-12 20:20:41 -07001418func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001419 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1420 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001421 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1422 if _, err := os.Stat(asan); err == nil {
1423 return asan
1424 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001425 }
1426 }
1427 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1428}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001429
1430func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1431 c.brokenDupRules = val
1432}
1433
1434func (c *configImpl) BuildBrokenDupRules() bool {
1435 return c.brokenDupRules
1436}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001437
Dan Willemsen25e6f092019-04-09 10:22:43 -07001438func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1439 c.brokenUsesNetwork = val
1440}
1441
1442func (c *configImpl) BuildBrokenUsesNetwork() bool {
1443 return c.brokenUsesNetwork
1444}
1445
Dan Willemsene3336352020-01-02 19:10:38 -08001446func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1447 c.brokenNinjaEnvVars = val
1448}
1449
1450func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1451 return c.brokenNinjaEnvVars
1452}
1453
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001454func (c *configImpl) SetTargetDeviceDir(dir string) {
1455 c.targetDeviceDir = dir
1456}
1457
1458func (c *configImpl) TargetDeviceDir() string {
1459 return c.targetDeviceDir
1460}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001461
Patrice Arruda219eef32020-06-01 17:29:30 +00001462func (c *configImpl) BuildDateTime() string {
1463 return c.buildDateTime
1464}
1465
1466func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001467 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001468}
Patrice Arruda83842d72020-12-08 19:42:08 +00001469
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001470// LogsDir returns the absolute path to the logs directory where build log and
1471// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001472// directory. If the argument dist is specified, the logs directory
1473// is <dist_dir>/logs.
1474func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001475 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001476 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001477 // 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 -05001478 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001479 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001480 absDir, err := filepath.Abs(dir)
1481 if err != nil {
1482 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1483 os.Exit(1)
1484 }
1485 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001486}
1487
1488// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1489// where the bazel profiles are located.
1490func (c *configImpl) BazelMetricsDir() string {
1491 return filepath.Join(c.LogsDir(), "bazel_metrics")
1492}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001493
Chris Parsons53f68ae2022-03-03 12:01:40 -05001494// MkFileMetrics returns the file path for make-related metrics.
1495func (c *configImpl) MkMetrics() string {
1496 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1497}
1498
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001499func (c *configImpl) SetEmptyNinjaFile(v bool) {
1500 c.emptyNinjaFile = v
1501}
1502
1503func (c *configImpl) EmptyNinjaFile() bool {
1504 return c.emptyNinjaFile
1505}
Yu Liu6e13b402021-07-27 14:29:06 -07001506
Romain Jobredeaux0a7529b2022-10-26 12:56:41 -04001507func (c *configImpl) IsBazelMixedBuildForceDisabled() bool {
1508 return c.Environment().IsEnvTrue("BUILD_BROKEN_DISABLE_BAZEL")
1509}
1510
MarkDacekd06db5d2022-11-29 00:47:59 +00001511func (c *configImpl) BazelModulesForceEnabledByFlag() string {
1512 return c.bazelForceEnabledModules
1513}
1514
Yu Liu6e13b402021-07-27 14:29:06 -07001515func GetMetricsUploader(topDir string, env *Environment) string {
1516 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1517 metricsUploader := filepath.Join(topDir, p)
1518 if _, err := os.Stat(metricsUploader); err == nil {
1519 return metricsUploader
1520 }
1521 }
1522
1523 return ""
1524}