blob: e271bfca2c767ff5412227ba9d89843dad7241ae [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"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080022 "os"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050023 "os/exec"
Dan Willemsen1e704462016-08-21 15:17:17 -070024 "path/filepath"
25 "runtime"
26 "strconv"
27 "strings"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080028 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070029
30 "android/soong/shared"
Kousik Kumarec478642020-09-21 13:39:24 -040031
Dan Willemsen4591b642021-05-24 14:24:12 -070032 "google.golang.org/protobuf/proto"
Patrice Arruda96850362020-08-11 20:41:11 +000033
34 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070035)
36
Kousik Kumar3ff037e2022-01-25 22:11:01 -050037const (
Chris Parsons53f68ae2022-03-03 12:01:40 -050038 envConfigDir = "vendor/google/tools/soong_config"
39 jsonSuffix = "json"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050040
Chris Parsons53f68ae2022-03-03 12:01:40 -050041 configFetcher = "vendor/google/tools/soong/expconfigfetcher"
Kousik Kumar84bd5bf2022-01-26 23:32:22 -050042 envConfigFetchTimeout = 10 * time.Second
Kousik Kumar3ff037e2022-01-25 22:11:01 -050043)
44
Dan Willemsen1e704462016-08-21 15:17:17 -070045type Config struct{ *configImpl }
46
47type configImpl struct {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020048 // Some targets that are implemented in soong_build
49 // (bp2build, json-module-graph) are not here and have their own bits below.
Colin Cross28f527c2019-11-26 16:19:04 -080050 arguments []string
51 goma bool
52 environ *Environment
53 distDir string
54 buildDateTime string
Dan Willemsen1e704462016-08-21 15:17:17 -070055
56 // From the arguments
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020057 parallel int
58 keepGoing int
59 verbose bool
60 checkbuild bool
61 dist bool
62 jsonModuleGraph bool
63 bp2build bool
Lukacs T. Berki3a821692021-09-06 17:08:02 +020064 queryview bool
Chris Parsons53f68ae2022-03-03 12:01:40 -050065 reportMkMetrics bool // Collect and report mk2bp migration progress metrics.
Lukacs T. Berkic6012f32021-09-06 18:31:46 +020066 soongDocs bool
Lukacs T. Berkia1b93722021-09-02 17:23:06 +020067 skipConfig bool
68 skipKati bool
69 skipKatiNinja bool
70 skipSoong bool
71 skipNinja bool
72 skipSoongTests bool
Dan Willemsen1e704462016-08-21 15:17:17 -070073
74 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070075 katiArgs []string
76 ninjaArgs []string
77 katiSuffix string
78 targetDevice string
79 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +000080 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -070081
Dan Willemsen2bb82d02019-12-27 09:35:42 -080082 // Autodetected
83 totalRAM uint64
84
Dan Willemsene3336352020-01-02 19:10:38 -080085 brokenDupRules bool
86 brokenUsesNetwork bool
87 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -070088
89 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +000090
91 useBazel bool
92
93 // During Bazel execution, Bazel cannot write outside OUT_DIR.
94 // So if DIST_DIR is set to an external dir (outside of OUT_DIR), we need to rig it temporarily and then migrate files at the end of the build.
95 riggedDistDirForBazel string
Colin Crossf3bdbcb2021-06-01 11:43:55 -070096
97 // Set by multiproduct_kati
98 emptyNinjaFile bool
Yu Liu6e13b402021-07-27 14:29:06 -070099
100 metricsUploader string
Dan Willemsen1e704462016-08-21 15:17:17 -0700101}
102
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800103const srcDirFileCheck = "build/soong/root.bp"
104
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700105var buildFiles = []string{"Android.mk", "Android.bp"}
106
Patrice Arruda13848222019-04-22 17:12:02 -0700107type BuildAction uint
108
109const (
110 // Builds all of the modules and their dependencies of a specified directory, relative to the root
111 // directory of the source tree.
112 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
113
114 // Builds all of the modules and their dependencies of a list of specified directories. All specified
115 // directories are relative to the root directory of the source tree.
116 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -0700117
118 // Build a list of specified modules. If none was specified, simply build the whole source tree.
119 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -0700120)
121
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400122type bazelBuildMode int
123
124// Bazel-related build modes.
125const (
126 // Don't use bazel at all.
127 noBazel bazelBuildMode = iota
128
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400129 // Generate synthetic build files and incorporate these files into a build which
130 // partially uses Bazel. Build metadata may come from Android.bp or BUILD files.
131 mixedBuild
132)
133
Patrice Arruda13848222019-04-22 17:12:02 -0700134// checkTopDir validates that the current directory is at the root directory of the source tree.
135func checkTopDir(ctx Context) {
136 if _, err := os.Stat(srcDirFileCheck); err != nil {
137 if os.IsNotExist(err) {
138 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
139 }
140 ctx.Fatalln("Error verifying tree state:", err)
141 }
142}
143
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500144// fetchEnvConfig optionally fetches environment config from an
145// experiments system to control Soong features dynamically.
146func fetchEnvConfig(ctx Context, config *configImpl, envConfigName string) error {
David Goldsmith62243a32022-04-08 13:42:04 +0000147 configName := envConfigName + "." + jsonSuffix
148 expConfigFetcher := &smpb.ExpConfigFetcher{}
149 defer func() {
150 ctx.Metrics.ExpConfigFetcher(expConfigFetcher)
151 }()
152
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500153 s, err := os.Stat(configFetcher)
154 if err != nil {
155 if os.IsNotExist(err) {
156 return nil
157 }
158 return err
159 }
160 if s.Mode()&0111 == 0 {
David Goldsmith62243a32022-04-08 13:42:04 +0000161 status := smpb.ExpConfigFetcher_ERROR
162 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500163 return fmt.Errorf("configuration fetcher binary %v is not executable: %v", configFetcher, s.Mode())
164 }
165
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500166 tCtx, cancel := context.WithTimeout(ctx, envConfigFetchTimeout)
167 defer cancel()
David Goldsmith62243a32022-04-08 13:42:04 +0000168 fetchStart := time.Now()
169 cmd := exec.CommandContext(tCtx, configFetcher, "-output_config_dir", config.OutDir(),
170 "-output_config_name", configName)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500171 if err := cmd.Start(); err != nil {
David Goldsmith62243a32022-04-08 13:42:04 +0000172 status := smpb.ExpConfigFetcher_ERROR
173 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500174 return err
175 }
176
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500177 if err := cmd.Wait(); err != nil {
David Goldsmith62243a32022-04-08 13:42:04 +0000178 status := smpb.ExpConfigFetcher_ERROR
179 expConfigFetcher.Status = &status
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500180 return err
181 }
David Goldsmith62243a32022-04-08 13:42:04 +0000182 fetchEnd := time.Now()
183 expConfigFetcher.Micros = proto.Uint64(uint64(fetchEnd.Sub(fetchStart).Microseconds()))
184 outConfigFilePath := filepath.Join(config.OutDir(), configName)
185 expConfigFetcher.Filename = proto.String(outConfigFilePath)
186 if _, err := os.Stat(outConfigFilePath); err == nil {
187 status := smpb.ExpConfigFetcher_CONFIG
188 expConfigFetcher.Status = &status
189 } else {
190 status := smpb.ExpConfigFetcher_NO_CONFIG
191 expConfigFetcher.Status = &status
192 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500193 return nil
194}
195
196func loadEnvConfig(ctx Context, config *configImpl) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500197 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
198 if bc == "" {
199 return nil
200 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500201
202 if err := fetchEnvConfig(ctx, config, bc); err != nil {
Kousik Kumar93d192c2022-03-18 01:39:56 -0400203 fmt.Fprintf(os.Stderr, "Failed to fetch config file: %v\n", err)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500204 }
205
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500206 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500207 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500208 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500209 envConfigDir,
210 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500211 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500212 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
213 envVarsJSON, err := ioutil.ReadFile(cfgFile)
214 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500215 continue
216 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500217 ctx.Verbosef("Loading config file %v\n", cfgFile)
218 var envVars map[string]map[string]string
219 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
220 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
221 continue
222 }
223 for k, v := range envVars["env"] {
224 if os.Getenv(k) != "" {
225 continue
226 }
227 config.environ.Set(k, v)
228 }
229 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
230 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500231 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500232
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500233 return nil
234}
235
Dan Willemsen1e704462016-08-21 15:17:17 -0700236func NewConfig(ctx Context, args ...string) Config {
237 ret := &configImpl{
Spandan Dasa3639e62021-05-25 19:14:02 +0000238 environ: OsEnvironment(),
239 sandboxConfig: &SandboxConfig{},
Dan Willemsen1e704462016-08-21 15:17:17 -0700240 }
241
Patrice Arruda90109172020-07-28 18:07:27 +0000242 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700243 ret.parallel = runtime.NumCPU() + 2
244 ret.keepGoing = 1
245
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800246 ret.totalRAM = detectTotalRAM(ctx)
247
Dan Willemsen9b587492017-07-10 22:13:00 -0700248 ret.parseArgs(ctx, args)
249
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800250 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700251 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
252 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
253 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800254 outDir := "out"
255 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
256 if wd, err := os.Getwd(); err != nil {
257 ctx.Fatalln("Failed to get working directory:", err)
258 } else {
259 outDir = filepath.Join(baseDir, filepath.Base(wd))
260 }
261 }
262 ret.environ.Set("OUT_DIR", outDir)
263 }
264
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500265 // loadEnvConfig needs to know what the OUT_DIR is, so it should
266 // be called after we determine the appropriate out directory.
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500267 if err := loadEnvConfig(ctx, ret); err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500268 ctx.Fatalln("Failed to parse env config files: %v", err)
269 }
270
Dan Willemsen2d31a442018-10-20 21:33:41 -0700271 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
272 ret.distDir = filepath.Clean(distDir)
273 } else {
274 ret.distDir = filepath.Join(ret.OutDir(), "dist")
275 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700276
Spandan Das05063612021-06-25 01:39:04 +0000277 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
278 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
279 }
280
Dan Willemsen1e704462016-08-21 15:17:17 -0700281 ret.environ.Unset(
282 // We're already using it
283 "USE_SOONG_UI",
284
285 // We should never use GOROOT/GOPATH from the shell environment
286 "GOROOT",
287 "GOPATH",
288
289 // These should only come from Soong, not the environment.
290 "CLANG",
291 "CLANG_CXX",
292 "CCC_CC",
293 "CCC_CXX",
294
295 // Used by the goma compiler wrapper, but should only be set by
296 // gomacc
297 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800298
299 // We handle this above
300 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700301
Dan Willemsen2d31a442018-10-20 21:33:41 -0700302 // This is handled above too, and set for individual commands later
303 "DIST_DIR",
304
Dan Willemsen68a09852017-04-18 13:56:57 -0700305 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000306 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700307 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700308 "DISPLAY",
309 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700310 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700311 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700312
313 // Drop make flags
314 "MAKEFLAGS",
315 "MAKELEVEL",
316 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700317
318 // Set in envsetup.sh, reset in makefiles
319 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700320
321 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
322 "ANDROID_BUILD_TOP",
323 "ANDROID_HOST_OUT",
324 "ANDROID_PRODUCT_OUT",
325 "ANDROID_HOST_OUT_TESTCASES",
326 "ANDROID_TARGET_OUT_TESTCASES",
327 "ANDROID_TOOLCHAIN",
328 "ANDROID_TOOLCHAIN_2ND_ARCH",
329 "ANDROID_DEV_SCRIPTS",
330 "ANDROID_EMULATOR_PREBUILTS",
331 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700332 )
333
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400334 if ret.UseGoma() || ret.ForceUseGoma() {
335 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
336 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400337 }
338
Dan Willemsen1e704462016-08-21 15:17:17 -0700339 // Tell python not to spam the source tree with .pyc files.
340 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
341
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400342 tmpDir := absPath(ctx, ret.TempDir())
343 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800344
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700345 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
346 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
347 "llvm-binutils-stable/llvm-symbolizer")
348 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
349
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800350 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700351 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800352
Yu Liu6e13b402021-07-27 14:29:06 -0700353 srcDir := absPath(ctx, ".")
354 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700355 ctx.Println("You are building in a directory whose absolute path contains a space character:")
356 ctx.Println()
357 ctx.Printf("%q\n", srcDir)
358 ctx.Println()
359 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700360 }
361
Yu Liu6e13b402021-07-27 14:29:06 -0700362 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
363
Dan Willemsendb8457c2017-05-12 16:38:17 -0700364 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700365 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
366 ctx.Println()
367 ctx.Printf("%q\n", outDir)
368 ctx.Println()
369 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700370 }
371
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000372 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700373 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
374 ctx.Println()
375 ctx.Printf("%q\n", distDir)
376 ctx.Println()
377 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700378 }
379
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700380 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000381 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
382 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100383 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Colin Cross59c1e6a2022-03-04 13:37:19 -0800384 java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700385 javaHome := func() string {
386 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
387 return override
388 }
Colin Cross59c1e6a2022-03-04 13:37:19 -0800389 if ret.environ.IsEnvTrue("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN") {
390 return java17Home
391 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000392 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
393 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 +0100394 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000395 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700396 }()
397 absJavaHome := absPath(ctx, javaHome)
398
Dan Willemsened869522018-01-08 14:58:46 -0800399 ret.configureLocale(ctx)
400
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700401 newPath := []string{filepath.Join(absJavaHome, "bin")}
402 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
403 newPath = append(newPath, path)
404 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100405
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700406 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
407 ret.environ.Set("JAVA_HOME", absJavaHome)
408 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000409 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
410 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100411 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700412 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
413
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800414 outDir := ret.OutDir()
415 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800416 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800417 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800418 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800419 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800420 }
Colin Cross28f527c2019-11-26 16:19:04 -0800421
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800422 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
423
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400424 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400425 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400426 ret.environ.Set(k, v)
427 }
428 }
429
Patrice Arruda83842d72020-12-08 19:42:08 +0000430 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800431 if err := os.RemoveAll(bpd); err != nil {
432 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
433 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000434
435 ret.useBazel = ret.environ.IsEnvTrue("USE_BAZEL")
436
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800437 if ret.UseBazel() {
438 if err := os.MkdirAll(bpd, 0777); err != nil {
439 ctx.Fatalf("Failed to create bazel profile directory %q: %v", bpd, err)
440 }
441 }
442
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000443 if ret.UseBazel() {
444 ret.riggedDistDirForBazel = filepath.Join(ret.OutDir(), "dist")
445 } else {
446 // Not rigged
447 ret.riggedDistDirForBazel = ret.distDir
448 }
449
Patrice Arruda96850362020-08-11 20:41:11 +0000450 c := Config{ret}
451 storeConfigMetrics(ctx, c)
452 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700453}
454
Patrice Arruda13848222019-04-22 17:12:02 -0700455// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
456// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700457func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
458 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700459}
460
Patrice Arruda96850362020-08-11 20:41:11 +0000461// storeConfigMetrics selects a set of configuration information and store in
462// the metrics system for further analysis.
463func storeConfigMetrics(ctx Context, config Config) {
464 if ctx.Metrics == nil {
465 return
466 }
467
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400468 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000469
470 s := &smpb.SystemResourceInfo{
471 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
472 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
473 }
474 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000475}
476
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400477func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700478 c := &smpb.BuildConfig{
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400479 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
480 UseGoma: proto.Bool(config.UseGoma()),
481 UseRbe: proto.Bool(config.UseRBE()),
482 BazelAsNinja: proto.Bool(config.UseBazel()),
483 BazelMixedBuild: proto.Bool(config.bazelBuildMode() == mixedBuild),
484 }
Yu Liue737a992021-10-04 13:21:41 -0700485 c.Targets = append(c.Targets, config.arguments...)
486
487 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400488}
489
Patrice Arruda13848222019-04-22 17:12:02 -0700490// getConfigArgs processes the command arguments based on the build action and creates a set of new
491// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700492func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700493 // The next block of code verifies that the current directory is the root directory of the source
494 // tree. It then finds the relative path of dir based on the root directory of the source tree
495 // and verify that dir is inside of the source tree.
496 checkTopDir(ctx)
497 topDir, err := os.Getwd()
498 if err != nil {
499 ctx.Fatalf("Error retrieving top directory: %v", err)
500 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700501 dir, err = filepath.EvalSymlinks(dir)
502 if err != nil {
503 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
504 }
Patrice Arruda13848222019-04-22 17:12:02 -0700505 dir, err = filepath.Abs(dir)
506 if err != nil {
507 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
508 }
509 relDir, err := filepath.Rel(topDir, dir)
510 if err != nil {
511 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
512 }
513 // If there are ".." in the path, it's not in the source tree.
514 if strings.Contains(relDir, "..") {
515 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
516 }
517
518 configArgs := args[:]
519
520 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
521 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
522 targetNamePrefix := "MODULES-IN-"
523 if inList("GET-INSTALL-PATH", configArgs) {
524 targetNamePrefix = "GET-INSTALL-PATH-IN-"
525 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
526 }
527
Patrice Arruda13848222019-04-22 17:12:02 -0700528 var targets []string
529
530 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700531 case BUILD_MODULES:
532 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700533 case BUILD_MODULES_IN_A_DIRECTORY:
534 // If dir is the root source tree, all the modules are built of the source tree are built so
535 // no need to find the build file.
536 if topDir == dir {
537 break
538 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700539
Patrice Arruda13848222019-04-22 17:12:02 -0700540 buildFile := findBuildFile(ctx, relDir)
541 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700542 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700543 }
Patrice Arruda13848222019-04-22 17:12:02 -0700544 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
545 case BUILD_MODULES_IN_DIRECTORIES:
546 newConfigArgs, dirs := splitArgs(configArgs)
547 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700548 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700549 }
550
551 // Tidy only override all other specified targets.
552 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
553 if tidyOnly == "true" || tidyOnly == "1" {
554 configArgs = append(configArgs, "tidy_only")
555 } else {
556 configArgs = append(configArgs, targets...)
557 }
558
559 return configArgs
560}
561
562// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
563func convertToTarget(dir string, targetNamePrefix string) string {
564 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
565}
566
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700567// hasBuildFile returns true if dir contains an Android build file.
568func hasBuildFile(ctx Context, dir string) bool {
569 for _, buildFile := range buildFiles {
570 _, err := os.Stat(filepath.Join(dir, buildFile))
571 if err == nil {
572 return true
573 }
574 if !os.IsNotExist(err) {
575 ctx.Fatalf("Error retrieving the build file stats: %v", err)
576 }
577 }
578 return false
579}
580
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700581// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
582// in the current and any sub directory of dir. If a build file is not found, traverse the path
583// up by one directory and repeat again until either a build file is found or reached to the root
584// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
585// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700586func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700587 // If the string is empty or ".", assume it is top directory of the source tree.
588 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700589 return ""
590 }
591
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700592 found := false
593 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
594 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
595 if err != nil {
596 return err
597 }
598 if found {
599 return filepath.SkipDir
600 }
601 if info.IsDir() {
602 return nil
603 }
604 for _, buildFile := range buildFiles {
605 if info.Name() == buildFile {
606 found = true
607 return filepath.SkipDir
608 }
609 }
610 return nil
611 })
612 if err != nil {
613 ctx.Fatalf("Error finding Android build file: %v", err)
614 }
615
616 if found {
617 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700618 }
619 }
620
621 return ""
622}
623
624// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
625func splitArgs(args []string) (newArgs []string, dirs []string) {
626 specialArgs := map[string]bool{
627 "showcommands": true,
628 "snod": true,
629 "dist": true,
630 "checkbuild": true,
631 }
632
633 newArgs = []string{}
634 dirs = []string{}
635
636 for _, arg := range args {
637 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
638 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
639 newArgs = append(newArgs, arg)
640 continue
641 }
642
643 if _, ok := specialArgs[arg]; ok {
644 newArgs = append(newArgs, arg)
645 continue
646 }
647
648 dirs = append(dirs, arg)
649 }
650
651 return newArgs, dirs
652}
653
654// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
655// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
656// source root tree where the build action command was invoked. Each directory is validated if the
657// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700658func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700659 for _, dir := range dirs {
660 // The directory may have specified specific modules to build. ":" is the separator to separate
661 // the directory and the list of modules.
662 s := strings.Split(dir, ":")
663 l := len(s)
664 if l > 2 { // more than one ":" was specified.
665 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
666 }
667
668 dir = filepath.Join(relDir, s[0])
669 if _, err := os.Stat(dir); err != nil {
670 ctx.Fatalf("couldn't find directory %s", dir)
671 }
672
673 // Verify that if there are any targets specified after ":". Each target is separated by ",".
674 var newTargets []string
675 if l == 2 && s[1] != "" {
676 newTargets = strings.Split(s[1], ",")
677 if inList("", newTargets) {
678 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
679 }
680 }
681
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700682 // If there are specified targets to build in dir, an android build file must exist for the one
683 // shot build. For the non-targets case, find the appropriate build file and build all the
684 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700685 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700686 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700687 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
688 }
689 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700690 buildFile := findBuildFile(ctx, dir)
691 if buildFile == "" {
692 ctx.Fatalf("Build file not found for %s directory", dir)
693 }
694 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700695 }
696
Patrice Arruda13848222019-04-22 17:12:02 -0700697 targets = append(targets, newTargets...)
698 }
699
Dan Willemsence41e942019-07-29 23:39:30 -0700700 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700701}
702
Dan Willemsen9b587492017-07-10 22:13:00 -0700703func (c *configImpl) parseArgs(ctx Context, args []string) {
704 for i := 0; i < len(args); i++ {
705 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100706 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700707 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200708 } else if arg == "--empty-ninja-file" {
709 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100710 } else if arg == "--skip-ninja" {
711 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700712 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700713 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
714 // but it does run a Kati ninja file if the .kati_enabled marker file was created
715 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000716 c.skipConfig = true
717 c.skipKati = true
718 } else if arg == "--skip-kati" {
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100719 // TODO: remove --skip-kati once module builds have been migrated to --song-only
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000720 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100721 } else if arg == "--soong-only" {
722 c.skipKati = true
723 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200724 } else if arg == "--config-only" {
725 c.skipKati = true
726 c.skipKatiNinja = true
727 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700728 } else if arg == "--skip-config" {
729 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700730 } else if arg == "--skip-soong-tests" {
731 c.skipSoongTests = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500732 } else if arg == "--mk-metrics" {
733 c.reportMkMetrics = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700734 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700735 parseArgNum := func(def int) int {
736 if len(arg) > 2 {
737 p, err := strconv.ParseUint(arg[2:], 10, 31)
738 if err != nil {
739 ctx.Fatalf("Failed to parse %q: %v", arg, err)
740 }
741 return int(p)
742 } else if i+1 < len(args) {
743 p, err := strconv.ParseUint(args[i+1], 10, 31)
744 if err == nil {
745 i++
746 return int(p)
747 }
748 }
749 return def
750 }
751
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700752 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700753 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700754 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700755 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700756 } else {
757 ctx.Fatalln("Unknown option:", arg)
758 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700759 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700760 if k == "OUT_DIR" {
761 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
762 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700763 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700764 } else if arg == "dist" {
765 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200766 } else if arg == "json-module-graph" {
767 c.jsonModuleGraph = true
768 } else if arg == "bp2build" {
769 c.bp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200770 } else if arg == "queryview" {
771 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200772 } else if arg == "soong_docs" {
773 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700774 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700775 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800776 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700777 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700778 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700779 }
780 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700781}
782
Dan Willemsened869522018-01-08 14:58:46 -0800783func (c *configImpl) configureLocale(ctx Context) {
784 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
785 output, err := cmd.Output()
786
787 var locales []string
788 if err == nil {
789 locales = strings.Split(string(output), "\n")
790 } else {
791 // If we're unable to list the locales, let's assume en_US.UTF-8
792 locales = []string{"en_US.UTF-8"}
793 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
794 }
795
796 // gettext uses LANGUAGE, which is passed directly through
797
798 // For LANG and LC_*, only preserve the evaluated version of
799 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800800 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800801 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800802 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800803 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800804 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800805 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800806 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800807 }
808
809 c.environ.UnsetWithPrefix("LC_")
810
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800811 if userLang != "" {
812 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800813 }
814
815 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
816 // for others)
817 if inList("C.UTF-8", locales) {
818 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500819 } else if inList("C.utf8", locales) {
820 // These normalize to the same thing
821 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800822 } else if inList("en_US.UTF-8", locales) {
823 c.environ.Set("LANG", "en_US.UTF-8")
824 } else if inList("en_US.utf8", locales) {
825 // These normalize to the same thing
826 c.environ.Set("LANG", "en_US.UTF-8")
827 } else {
828 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
829 }
830}
831
Dan Willemsen1e704462016-08-21 15:17:17 -0700832func (c *configImpl) Environment() *Environment {
833 return c.environ
834}
835
836func (c *configImpl) Arguments() []string {
837 return c.arguments
838}
839
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200840func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200841 if len(c.Arguments()) > 0 {
842 // Explicit targets requested that are not special targets like b2pbuild
843 // or the JSON module graph
844 return true
845 }
846
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200847 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200848 // Command line was empty, the default Ninja target is built
849 return true
850 }
851
Liz Kammer88677422021-12-15 15:03:19 -0500852 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
853 if c.Dist() && !c.Bp2Build() {
854 return true
855 }
856
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200857 // build.ninja doesn't need to be generated
858 return false
859}
860
Dan Willemsen1e704462016-08-21 15:17:17 -0700861func (c *configImpl) OutDir() string {
862 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700863 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700864 }
865 return "out"
866}
867
Dan Willemsen8a073a82017-02-04 17:30:44 -0800868func (c *configImpl) DistDir() string {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000869 if c.UseBazel() {
870 return c.riggedDistDirForBazel
871 } else {
872 return c.distDir
873 }
874}
875
876func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700877 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800878}
879
Dan Willemsen1e704462016-08-21 15:17:17 -0700880func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000881 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700882 return c.arguments
883 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700884 return c.ninjaArgs
885}
886
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500887func (c *configImpl) BazelOutDir() string {
888 return filepath.Join(c.OutDir(), "bazel")
889}
890
Dan Willemsen1e704462016-08-21 15:17:17 -0700891func (c *configImpl) SoongOutDir() string {
892 return filepath.Join(c.OutDir(), "soong")
893}
894
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200895func (c *configImpl) PrebuiltOS() string {
896 switch runtime.GOOS {
897 case "linux":
898 return "linux-x86"
899 case "darwin":
900 return "darwin-x86"
901 default:
902 panic("Unknown GOOS")
903 }
904}
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100905
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200906func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -0700907 if c.SkipKatiNinja() {
908 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
909 } else {
910 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
911 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200912}
913
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200914func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100915 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200916}
917
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200918func (c *configImpl) UsedEnvFile(tag string) string {
919 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
920}
921
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200922func (c *configImpl) Bp2BuildMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100923 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200924}
925
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200926func (c *configImpl) SoongDocsHtml() string {
927 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
928}
929
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200930func (c *configImpl) QueryviewMarkerFile() string {
931 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
932}
933
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200934func (c *configImpl) ModuleGraphFile() string {
935 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
936}
937
kgui67007242022-01-25 13:50:25 +0800938func (c *configImpl) ModuleActionsFile() string {
939 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
940}
941
Jeff Gastonefc1b412017-03-29 17:29:06 -0700942func (c *configImpl) TempDir() string {
943 return shared.TempDirForOutDir(c.SoongOutDir())
944}
945
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700946func (c *configImpl) FileListDir() string {
947 return filepath.Join(c.OutDir(), ".module_paths")
948}
949
Dan Willemsen1e704462016-08-21 15:17:17 -0700950func (c *configImpl) KatiSuffix() string {
951 if c.katiSuffix != "" {
952 return c.katiSuffix
953 }
954 panic("SetKatiSuffix has not been called")
955}
956
Colin Cross37193492017-11-16 17:55:00 -0800957// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
958// user is interested in additional checks at the expense of build time.
959func (c *configImpl) Checkbuild() bool {
960 return c.checkbuild
961}
962
Dan Willemsen8a073a82017-02-04 17:30:44 -0800963func (c *configImpl) Dist() bool {
964 return c.dist
965}
966
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200967func (c *configImpl) JsonModuleGraph() bool {
968 return c.jsonModuleGraph
969}
970
971func (c *configImpl) Bp2Build() bool {
972 return c.bp2build
973}
974
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200975func (c *configImpl) Queryview() bool {
976 return c.queryview
977}
978
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200979func (c *configImpl) SoongDocs() bool {
980 return c.soongDocs
981}
982
Dan Willemsen1e704462016-08-21 15:17:17 -0700983func (c *configImpl) IsVerbose() bool {
984 return c.verbose
985}
986
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000987func (c *configImpl) SkipKati() bool {
988 return c.skipKati
989}
990
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100991func (c *configImpl) SkipKatiNinja() bool {
992 return c.skipKatiNinja
993}
994
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200995func (c *configImpl) SkipSoong() bool {
996 return c.skipSoong
997}
998
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100999func (c *configImpl) SkipNinja() bool {
1000 return c.skipNinja
1001}
1002
Anton Hansson5a7861a2021-06-04 10:09:01 +01001003func (c *configImpl) SetSkipNinja(v bool) {
1004 c.skipNinja = v
1005}
1006
Anton Hansson5e5c48b2020-11-27 12:35:20 +00001007func (c *configImpl) SkipConfig() bool {
1008 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -07001009}
1010
Dan Willemsen1e704462016-08-21 15:17:17 -07001011func (c *configImpl) TargetProduct() string {
1012 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
1013 return v
1014 }
1015 panic("TARGET_PRODUCT is not defined")
1016}
1017
Dan Willemsen02781d52017-05-12 19:28:13 -07001018func (c *configImpl) TargetDevice() string {
1019 return c.targetDevice
1020}
1021
1022func (c *configImpl) SetTargetDevice(device string) {
1023 c.targetDevice = device
1024}
1025
1026func (c *configImpl) TargetBuildVariant() string {
1027 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1028 return v
1029 }
1030 panic("TARGET_BUILD_VARIANT is not defined")
1031}
1032
Dan Willemsen1e704462016-08-21 15:17:17 -07001033func (c *configImpl) KatiArgs() []string {
1034 return c.katiArgs
1035}
1036
1037func (c *configImpl) Parallel() int {
1038 return c.parallel
1039}
1040
Colin Cross8b8bec32019-11-15 13:18:43 -08001041func (c *configImpl) HighmemParallel() int {
1042 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1043 return i
1044 }
1045
1046 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1047 parallel := c.Parallel()
1048 if c.UseRemoteBuild() {
1049 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1050 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1051 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1052 // Return 1/16th of the size of the local pool, rounding up.
1053 return (parallel + 15) / 16
1054 } else if c.totalRAM == 0 {
1055 // Couldn't detect the total RAM, don't restrict highmem processes.
1056 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001057 } else if c.totalRAM <= 16*1024*1024*1024 {
1058 // Less than 16GB of ram, restrict to 1 highmem processes
1059 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001060 } else if c.totalRAM <= 32*1024*1024*1024 {
1061 // Less than 32GB of ram, restrict to 2 highmem processes
1062 return 2
1063 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1064 // If less than 8GB total RAM per process, reduce the number of highmem processes
1065 return p
1066 }
1067 // No restriction on highmem processes
1068 return parallel
1069}
1070
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001071func (c *configImpl) TotalRAM() uint64 {
1072 return c.totalRAM
1073}
1074
Kousik Kumarec478642020-09-21 13:39:24 -04001075// ForceUseGoma determines whether we should override Goma deprecation
1076// and use Goma for the current build or not.
1077func (c *configImpl) ForceUseGoma() bool {
1078 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1079 v = strings.TrimSpace(v)
1080 if v != "" && v != "false" {
1081 return true
1082 }
1083 }
1084 return false
1085}
1086
Dan Willemsen1e704462016-08-21 15:17:17 -07001087func (c *configImpl) UseGoma() bool {
1088 if v, ok := c.environ.Get("USE_GOMA"); ok {
1089 v = strings.TrimSpace(v)
1090 if v != "" && v != "false" {
1091 return true
1092 }
1093 }
1094 return false
1095}
1096
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001097func (c *configImpl) StartGoma() bool {
1098 if !c.UseGoma() {
1099 return false
1100 }
1101
1102 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1103 v = strings.TrimSpace(v)
1104 if v != "" && v != "false" {
1105 return false
1106 }
1107 }
1108 return true
1109}
1110
Ramy Medhatbbf25672019-07-17 12:30:04 +00001111func (c *configImpl) UseRBE() bool {
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001112 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001113 v = strings.TrimSpace(v)
1114 if v != "" && v != "false" {
1115 return true
1116 }
1117 }
1118 return false
1119}
1120
Patrice Arruda0c1c4562020-11-11 13:01:25 -08001121func (c *configImpl) UseBazel() bool {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001122 return c.useBazel
Patrice Arruda0c1c4562020-11-11 13:01:25 -08001123}
1124
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001125func (c *configImpl) bazelBuildMode() bazelBuildMode {
1126 if c.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") {
1127 return mixedBuild
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001128 } else {
1129 return noBazel
1130 }
1131}
1132
Ramy Medhatbbf25672019-07-17 12:30:04 +00001133func (c *configImpl) StartRBE() bool {
1134 if !c.UseRBE() {
1135 return false
1136 }
1137
1138 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1139 v = strings.TrimSpace(v)
1140 if v != "" && v != "false" {
1141 return false
1142 }
1143 }
1144 return true
1145}
1146
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001147func (c *configImpl) rbeLogDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001148 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
1149 if v, ok := c.environ.Get(f); ok {
1150 return v
1151 }
1152 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001153 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001154 return c.LogsDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001155 }
1156 return c.OutDir()
1157}
1158
1159func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001160 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
1161 if v, ok := c.environ.Get(f); ok {
1162 return v
1163 }
1164 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001165 return c.rbeLogDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001166}
1167
1168func (c *configImpl) rbeLogPath() string {
1169 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
1170 if v, ok := c.environ.Get(f); ok {
1171 return v
1172 }
1173 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001174 return fmt.Sprintf("text://%v/reproxy_log.txt", c.rbeLogDir())
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001175}
1176
1177func (c *configImpl) rbeExecRoot() string {
1178 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1179 if v, ok := c.environ.Get(f); ok {
1180 return v
1181 }
1182 }
1183 wd, err := os.Getwd()
1184 if err != nil {
1185 return ""
1186 }
1187 return wd
1188}
1189
1190func (c *configImpl) rbeDir() string {
1191 if v, ok := c.environ.Get("RBE_DIR"); ok {
1192 return v
1193 }
1194 return "prebuilts/remoteexecution-client/live/"
1195}
1196
1197func (c *configImpl) rbeReproxy() string {
1198 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1199 if v, ok := c.environ.Get(f); ok {
1200 return v
1201 }
1202 }
1203 return filepath.Join(c.rbeDir(), "reproxy")
1204}
1205
1206func (c *configImpl) rbeAuth() (string, string) {
Kousik Kumar93d192c2022-03-18 01:39:56 -04001207 credFlags := []string{
1208 "use_application_default_credentials",
1209 "use_gce_credentials",
1210 "credential_file",
1211 "use_google_prod_creds",
1212 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001213 for _, cf := range credFlags {
1214 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1215 if v, ok := c.environ.Get(f); ok {
1216 v = strings.TrimSpace(v)
1217 if v != "" && v != "false" && v != "0" {
1218 return "RBE_" + cf, v
1219 }
1220 }
1221 }
1222 }
1223 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001224}
1225
Colin Cross9016b912019-11-11 14:57:42 -08001226func (c *configImpl) UseRemoteBuild() bool {
1227 return c.UseGoma() || c.UseRBE()
1228}
1229
Dan Willemsen1e704462016-08-21 15:17:17 -07001230// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001231// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001232// still limited by Parallel()
1233func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001234 if !c.UseRemoteBuild() {
1235 return 0
1236 }
1237 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1238 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001239 }
1240 return 500
1241}
1242
1243func (c *configImpl) SetKatiArgs(args []string) {
1244 c.katiArgs = args
1245}
1246
1247func (c *configImpl) SetNinjaArgs(args []string) {
1248 c.ninjaArgs = args
1249}
1250
1251func (c *configImpl) SetKatiSuffix(suffix string) {
1252 c.katiSuffix = suffix
1253}
1254
Dan Willemsene0879fc2017-08-04 15:06:27 -07001255func (c *configImpl) LastKatiSuffixFile() string {
1256 return filepath.Join(c.OutDir(), "last_kati_suffix")
1257}
1258
1259func (c *configImpl) HasKatiSuffix() bool {
1260 return c.katiSuffix != ""
1261}
1262
Dan Willemsen1e704462016-08-21 15:17:17 -07001263func (c *configImpl) KatiEnvFile() string {
1264 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1265}
1266
Dan Willemsen29971232018-09-26 14:58:30 -07001267func (c *configImpl) KatiBuildNinjaFile() string {
1268 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001269}
1270
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001271func (c *configImpl) KatiPackageNinjaFile() string {
1272 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1273}
1274
Dan Willemsen1e704462016-08-21 15:17:17 -07001275func (c *configImpl) SoongNinjaFile() string {
1276 return filepath.Join(c.SoongOutDir(), "build.ninja")
1277}
1278
1279func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001280 if c.katiSuffix == "" {
1281 return filepath.Join(c.OutDir(), "combined.ninja")
1282 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001283 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1284}
1285
1286func (c *configImpl) SoongAndroidMk() string {
1287 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1288}
1289
1290func (c *configImpl) SoongMakeVarsMk() string {
1291 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1292}
1293
Dan Willemsenf052f782017-05-18 15:29:04 -07001294func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001295 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001296}
1297
Dan Willemsen02781d52017-05-12 19:28:13 -07001298func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001299 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1300}
1301
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001302func (c *configImpl) KatiPackageMkDir() string {
1303 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1304}
1305
Dan Willemsenf052f782017-05-18 15:29:04 -07001306func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001307 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001308}
1309
1310func (c *configImpl) HostOut() string {
1311 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1312}
1313
1314// This probably needs to be multi-valued, so not exporting it for now
1315func (c *configImpl) hostCrossOut() string {
1316 if runtime.GOOS == "linux" {
1317 return filepath.Join(c.hostOutRoot(), "windows-x86")
1318 } else {
1319 return ""
1320 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001321}
1322
Dan Willemsen1e704462016-08-21 15:17:17 -07001323func (c *configImpl) HostPrebuiltTag() string {
1324 if runtime.GOOS == "linux" {
1325 return "linux-x86"
1326 } else if runtime.GOOS == "darwin" {
1327 return "darwin-x86"
1328 } else {
1329 panic("Unsupported OS")
1330 }
1331}
Dan Willemsenf173d592017-04-27 14:28:00 -07001332
Dan Willemsen8122bd52017-10-12 20:20:41 -07001333func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001334 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1335 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001336 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1337 if _, err := os.Stat(asan); err == nil {
1338 return asan
1339 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001340 }
1341 }
1342 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1343}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001344
1345func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1346 c.brokenDupRules = val
1347}
1348
1349func (c *configImpl) BuildBrokenDupRules() bool {
1350 return c.brokenDupRules
1351}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001352
Dan Willemsen25e6f092019-04-09 10:22:43 -07001353func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1354 c.brokenUsesNetwork = val
1355}
1356
1357func (c *configImpl) BuildBrokenUsesNetwork() bool {
1358 return c.brokenUsesNetwork
1359}
1360
Dan Willemsene3336352020-01-02 19:10:38 -08001361func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1362 c.brokenNinjaEnvVars = val
1363}
1364
1365func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1366 return c.brokenNinjaEnvVars
1367}
1368
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001369func (c *configImpl) SetTargetDeviceDir(dir string) {
1370 c.targetDeviceDir = dir
1371}
1372
1373func (c *configImpl) TargetDeviceDir() string {
1374 return c.targetDeviceDir
1375}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001376
Patrice Arruda219eef32020-06-01 17:29:30 +00001377func (c *configImpl) BuildDateTime() string {
1378 return c.buildDateTime
1379}
1380
1381func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001382 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001383}
Patrice Arruda83842d72020-12-08 19:42:08 +00001384
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001385// LogsDir returns the absolute path to the logs directory where build log and
1386// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001387// directory. If the argument dist is specified, the logs directory
1388// is <dist_dir>/logs.
1389func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001390 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001391 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001392 // 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 -05001393 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001394 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001395 absDir, err := filepath.Abs(dir)
1396 if err != nil {
1397 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1398 os.Exit(1)
1399 }
1400 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001401}
1402
1403// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1404// where the bazel profiles are located.
1405func (c *configImpl) BazelMetricsDir() string {
1406 return filepath.Join(c.LogsDir(), "bazel_metrics")
1407}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001408
Chris Parsons53f68ae2022-03-03 12:01:40 -05001409// MkFileMetrics returns the file path for make-related metrics.
1410func (c *configImpl) MkMetrics() string {
1411 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1412}
1413
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001414func (c *configImpl) SetEmptyNinjaFile(v bool) {
1415 c.emptyNinjaFile = v
1416}
1417
1418func (c *configImpl) EmptyNinjaFile() bool {
1419 return c.emptyNinjaFile
1420}
Yu Liu6e13b402021-07-27 14:29:06 -07001421
1422func GetMetricsUploader(topDir string, env *Environment) string {
1423 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1424 metricsUploader := filepath.Join(topDir, p)
1425 if _, err := os.Stat(metricsUploader); err == nil {
1426 return metricsUploader
1427 }
1428 }
1429
1430 return ""
1431}