blob: 077a4d19920d5981ebcaf09563b1387b46ed5b6b [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 {
147 s, err := os.Stat(configFetcher)
148 if err != nil {
149 if os.IsNotExist(err) {
150 return nil
151 }
152 return err
153 }
154 if s.Mode()&0111 == 0 {
155 return fmt.Errorf("configuration fetcher binary %v is not executable: %v", configFetcher, s.Mode())
156 }
157
158 configExists := false
Chris Parsons53f68ae2022-03-03 12:01:40 -0500159 outConfigFilePath := filepath.Join(config.OutDir(), envConfigName+jsonSuffix)
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500160 if _, err := os.Stat(outConfigFilePath); err == nil {
161 configExists = true
162 }
163
164 tCtx, cancel := context.WithTimeout(ctx, envConfigFetchTimeout)
165 defer cancel()
166 cmd := exec.CommandContext(tCtx, configFetcher, "-output_config_dir", config.OutDir())
167 if err := cmd.Start(); err != nil {
168 return err
169 }
170
171 // If a config file already exists, return immediately and run the config file
172 // fetch in the background. Otherwise, wait for the config file to be fetched.
173 if configExists {
174 go cmd.Wait()
175 return nil
176 }
177 if err := cmd.Wait(); err != nil {
178 return err
179 }
180 return nil
181}
182
183func loadEnvConfig(ctx Context, config *configImpl) error {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500184 bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG")
185 if bc == "" {
186 return nil
187 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500188
189 if err := fetchEnvConfig(ctx, config, bc); err != nil {
190 fmt.Fprintf(os.Stderr, "Failed to fetch config file: %v", err)
191 }
192
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500193 configDirs := []string{
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500194 config.OutDir(),
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500195 os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"),
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500196 envConfigDir,
197 }
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500198 for _, dir := range configDirs {
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500199 cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix))
200 envVarsJSON, err := ioutil.ReadFile(cfgFile)
201 if err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500202 continue
203 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500204 ctx.Verbosef("Loading config file %v\n", cfgFile)
205 var envVars map[string]map[string]string
206 if err := json.Unmarshal(envVarsJSON, &envVars); err != nil {
207 fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error())
208 continue
209 }
210 for k, v := range envVars["env"] {
211 if os.Getenv(k) != "" {
212 continue
213 }
214 config.environ.Set(k, v)
215 }
216 ctx.Verbosef("Finished loading config file %v\n", cfgFile)
217 break
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500218 }
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500219
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500220 return nil
221}
222
Dan Willemsen1e704462016-08-21 15:17:17 -0700223func NewConfig(ctx Context, args ...string) Config {
224 ret := &configImpl{
Spandan Dasa3639e62021-05-25 19:14:02 +0000225 environ: OsEnvironment(),
226 sandboxConfig: &SandboxConfig{},
Dan Willemsen1e704462016-08-21 15:17:17 -0700227 }
228
Patrice Arruda90109172020-07-28 18:07:27 +0000229 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700230 ret.parallel = runtime.NumCPU() + 2
231 ret.keepGoing = 1
232
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800233 ret.totalRAM = detectTotalRAM(ctx)
234
Dan Willemsen9b587492017-07-10 22:13:00 -0700235 ret.parseArgs(ctx, args)
236
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800237 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700238 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
239 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
240 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800241 outDir := "out"
242 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
243 if wd, err := os.Getwd(); err != nil {
244 ctx.Fatalln("Failed to get working directory:", err)
245 } else {
246 outDir = filepath.Join(baseDir, filepath.Base(wd))
247 }
248 }
249 ret.environ.Set("OUT_DIR", outDir)
250 }
251
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500252 // loadEnvConfig needs to know what the OUT_DIR is, so it should
253 // be called after we determine the appropriate out directory.
Kousik Kumar84bd5bf2022-01-26 23:32:22 -0500254 if err := loadEnvConfig(ctx, ret); err != nil {
Kousik Kumar3ff037e2022-01-25 22:11:01 -0500255 ctx.Fatalln("Failed to parse env config files: %v", err)
256 }
257
Dan Willemsen2d31a442018-10-20 21:33:41 -0700258 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
259 ret.distDir = filepath.Clean(distDir)
260 } else {
261 ret.distDir = filepath.Join(ret.OutDir(), "dist")
262 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700263
Spandan Das05063612021-06-25 01:39:04 +0000264 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
265 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
266 }
267
Dan Willemsen1e704462016-08-21 15:17:17 -0700268 ret.environ.Unset(
269 // We're already using it
270 "USE_SOONG_UI",
271
272 // We should never use GOROOT/GOPATH from the shell environment
273 "GOROOT",
274 "GOPATH",
275
276 // These should only come from Soong, not the environment.
277 "CLANG",
278 "CLANG_CXX",
279 "CCC_CC",
280 "CCC_CXX",
281
282 // Used by the goma compiler wrapper, but should only be set by
283 // gomacc
284 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800285
286 // We handle this above
287 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700288
Dan Willemsen2d31a442018-10-20 21:33:41 -0700289 // This is handled above too, and set for individual commands later
290 "DIST_DIR",
291
Dan Willemsen68a09852017-04-18 13:56:57 -0700292 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000293 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700294 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700295 "DISPLAY",
296 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700297 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700298 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700299
300 // Drop make flags
301 "MAKEFLAGS",
302 "MAKELEVEL",
303 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700304
305 // Set in envsetup.sh, reset in makefiles
306 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700307
308 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
309 "ANDROID_BUILD_TOP",
310 "ANDROID_HOST_OUT",
311 "ANDROID_PRODUCT_OUT",
312 "ANDROID_HOST_OUT_TESTCASES",
313 "ANDROID_TARGET_OUT_TESTCASES",
314 "ANDROID_TOOLCHAIN",
315 "ANDROID_TOOLCHAIN_2ND_ARCH",
316 "ANDROID_DEV_SCRIPTS",
317 "ANDROID_EMULATOR_PREBUILTS",
318 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700319 )
320
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400321 if ret.UseGoma() || ret.ForceUseGoma() {
322 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
323 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400324 }
325
Dan Willemsen1e704462016-08-21 15:17:17 -0700326 // Tell python not to spam the source tree with .pyc files.
327 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
328
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400329 tmpDir := absPath(ctx, ret.TempDir())
330 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800331
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700332 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
333 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
334 "llvm-binutils-stable/llvm-symbolizer")
335 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
336
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800337 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700338 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800339
Yu Liu6e13b402021-07-27 14:29:06 -0700340 srcDir := absPath(ctx, ".")
341 if strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700342 ctx.Println("You are building in a directory whose absolute path contains a space character:")
343 ctx.Println()
344 ctx.Printf("%q\n", srcDir)
345 ctx.Println()
346 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700347 }
348
Yu Liu6e13b402021-07-27 14:29:06 -0700349 ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ)
350
Dan Willemsendb8457c2017-05-12 16:38:17 -0700351 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700352 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
353 ctx.Println()
354 ctx.Printf("%q\n", outDir)
355 ctx.Println()
356 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700357 }
358
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000359 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700360 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
361 ctx.Println()
362 ctx.Printf("%q\n", distDir)
363 ctx.Println()
364 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700365 }
366
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700367 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000368 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
369 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100370 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700371 javaHome := func() string {
372 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
373 return override
374 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000375 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
376 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 +0100377 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000378 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700379 }()
380 absJavaHome := absPath(ctx, javaHome)
381
Dan Willemsened869522018-01-08 14:58:46 -0800382 ret.configureLocale(ctx)
383
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700384 newPath := []string{filepath.Join(absJavaHome, "bin")}
385 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
386 newPath = append(newPath, path)
387 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100388
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700389 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
390 ret.environ.Set("JAVA_HOME", absJavaHome)
391 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000392 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
393 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100394 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700395 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
396
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800397 outDir := ret.OutDir()
398 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800399 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800400 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800401 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800402 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800403 }
Colin Cross28f527c2019-11-26 16:19:04 -0800404
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800405 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
406
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400407 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400408 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400409 ret.environ.Set(k, v)
410 }
411 }
412
Patrice Arruda83842d72020-12-08 19:42:08 +0000413 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800414 if err := os.RemoveAll(bpd); err != nil {
415 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
416 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000417
418 ret.useBazel = ret.environ.IsEnvTrue("USE_BAZEL")
419
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800420 if ret.UseBazel() {
421 if err := os.MkdirAll(bpd, 0777); err != nil {
422 ctx.Fatalf("Failed to create bazel profile directory %q: %v", bpd, err)
423 }
424 }
425
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000426 if ret.UseBazel() {
427 ret.riggedDistDirForBazel = filepath.Join(ret.OutDir(), "dist")
428 } else {
429 // Not rigged
430 ret.riggedDistDirForBazel = ret.distDir
431 }
432
Patrice Arruda96850362020-08-11 20:41:11 +0000433 c := Config{ret}
434 storeConfigMetrics(ctx, c)
435 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700436}
437
Patrice Arruda13848222019-04-22 17:12:02 -0700438// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
439// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700440func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
441 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700442}
443
Patrice Arruda96850362020-08-11 20:41:11 +0000444// storeConfigMetrics selects a set of configuration information and store in
445// the metrics system for further analysis.
446func storeConfigMetrics(ctx Context, config Config) {
447 if ctx.Metrics == nil {
448 return
449 }
450
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400451 ctx.Metrics.BuildConfig(buildConfig(config))
Patrice Arruda3edfd482020-10-13 23:58:41 +0000452
453 s := &smpb.SystemResourceInfo{
454 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
455 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
456 }
457 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000458}
459
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400460func buildConfig(config Config) *smpb.BuildConfig {
Yu Liue737a992021-10-04 13:21:41 -0700461 c := &smpb.BuildConfig{
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400462 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
463 UseGoma: proto.Bool(config.UseGoma()),
464 UseRbe: proto.Bool(config.UseRBE()),
465 BazelAsNinja: proto.Bool(config.UseBazel()),
466 BazelMixedBuild: proto.Bool(config.bazelBuildMode() == mixedBuild),
467 }
Yu Liue737a992021-10-04 13:21:41 -0700468 c.Targets = append(c.Targets, config.arguments...)
469
470 return c
Liz Kammerca9cb2e2021-07-14 15:29:57 -0400471}
472
Patrice Arruda13848222019-04-22 17:12:02 -0700473// getConfigArgs processes the command arguments based on the build action and creates a set of new
474// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700475func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700476 // The next block of code verifies that the current directory is the root directory of the source
477 // tree. It then finds the relative path of dir based on the root directory of the source tree
478 // and verify that dir is inside of the source tree.
479 checkTopDir(ctx)
480 topDir, err := os.Getwd()
481 if err != nil {
482 ctx.Fatalf("Error retrieving top directory: %v", err)
483 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700484 dir, err = filepath.EvalSymlinks(dir)
485 if err != nil {
486 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
487 }
Patrice Arruda13848222019-04-22 17:12:02 -0700488 dir, err = filepath.Abs(dir)
489 if err != nil {
490 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
491 }
492 relDir, err := filepath.Rel(topDir, dir)
493 if err != nil {
494 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
495 }
496 // If there are ".." in the path, it's not in the source tree.
497 if strings.Contains(relDir, "..") {
498 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
499 }
500
501 configArgs := args[:]
502
503 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
504 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
505 targetNamePrefix := "MODULES-IN-"
506 if inList("GET-INSTALL-PATH", configArgs) {
507 targetNamePrefix = "GET-INSTALL-PATH-IN-"
508 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
509 }
510
Patrice Arruda13848222019-04-22 17:12:02 -0700511 var targets []string
512
513 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700514 case BUILD_MODULES:
515 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700516 case BUILD_MODULES_IN_A_DIRECTORY:
517 // If dir is the root source tree, all the modules are built of the source tree are built so
518 // no need to find the build file.
519 if topDir == dir {
520 break
521 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700522
Patrice Arruda13848222019-04-22 17:12:02 -0700523 buildFile := findBuildFile(ctx, relDir)
524 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700525 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700526 }
Patrice Arruda13848222019-04-22 17:12:02 -0700527 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
528 case BUILD_MODULES_IN_DIRECTORIES:
529 newConfigArgs, dirs := splitArgs(configArgs)
530 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700531 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700532 }
533
534 // Tidy only override all other specified targets.
535 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
536 if tidyOnly == "true" || tidyOnly == "1" {
537 configArgs = append(configArgs, "tidy_only")
538 } else {
539 configArgs = append(configArgs, targets...)
540 }
541
542 return configArgs
543}
544
545// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
546func convertToTarget(dir string, targetNamePrefix string) string {
547 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
548}
549
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700550// hasBuildFile returns true if dir contains an Android build file.
551func hasBuildFile(ctx Context, dir string) bool {
552 for _, buildFile := range buildFiles {
553 _, err := os.Stat(filepath.Join(dir, buildFile))
554 if err == nil {
555 return true
556 }
557 if !os.IsNotExist(err) {
558 ctx.Fatalf("Error retrieving the build file stats: %v", err)
559 }
560 }
561 return false
562}
563
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700564// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
565// in the current and any sub directory of dir. If a build file is not found, traverse the path
566// up by one directory and repeat again until either a build file is found or reached to the root
567// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
568// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700569func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700570 // If the string is empty or ".", assume it is top directory of the source tree.
571 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700572 return ""
573 }
574
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700575 found := false
576 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
577 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
578 if err != nil {
579 return err
580 }
581 if found {
582 return filepath.SkipDir
583 }
584 if info.IsDir() {
585 return nil
586 }
587 for _, buildFile := range buildFiles {
588 if info.Name() == buildFile {
589 found = true
590 return filepath.SkipDir
591 }
592 }
593 return nil
594 })
595 if err != nil {
596 ctx.Fatalf("Error finding Android build file: %v", err)
597 }
598
599 if found {
600 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700601 }
602 }
603
604 return ""
605}
606
607// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
608func splitArgs(args []string) (newArgs []string, dirs []string) {
609 specialArgs := map[string]bool{
610 "showcommands": true,
611 "snod": true,
612 "dist": true,
613 "checkbuild": true,
614 }
615
616 newArgs = []string{}
617 dirs = []string{}
618
619 for _, arg := range args {
620 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
621 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
622 newArgs = append(newArgs, arg)
623 continue
624 }
625
626 if _, ok := specialArgs[arg]; ok {
627 newArgs = append(newArgs, arg)
628 continue
629 }
630
631 dirs = append(dirs, arg)
632 }
633
634 return newArgs, dirs
635}
636
637// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
638// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
639// source root tree where the build action command was invoked. Each directory is validated if the
640// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700641func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700642 for _, dir := range dirs {
643 // The directory may have specified specific modules to build. ":" is the separator to separate
644 // the directory and the list of modules.
645 s := strings.Split(dir, ":")
646 l := len(s)
647 if l > 2 { // more than one ":" was specified.
648 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
649 }
650
651 dir = filepath.Join(relDir, s[0])
652 if _, err := os.Stat(dir); err != nil {
653 ctx.Fatalf("couldn't find directory %s", dir)
654 }
655
656 // Verify that if there are any targets specified after ":". Each target is separated by ",".
657 var newTargets []string
658 if l == 2 && s[1] != "" {
659 newTargets = strings.Split(s[1], ",")
660 if inList("", newTargets) {
661 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
662 }
663 }
664
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700665 // If there are specified targets to build in dir, an android build file must exist for the one
666 // shot build. For the non-targets case, find the appropriate build file and build all the
667 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700668 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700669 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700670 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
671 }
672 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700673 buildFile := findBuildFile(ctx, dir)
674 if buildFile == "" {
675 ctx.Fatalf("Build file not found for %s directory", dir)
676 }
677 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700678 }
679
Patrice Arruda13848222019-04-22 17:12:02 -0700680 targets = append(targets, newTargets...)
681 }
682
Dan Willemsence41e942019-07-29 23:39:30 -0700683 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700684}
685
Dan Willemsen9b587492017-07-10 22:13:00 -0700686func (c *configImpl) parseArgs(ctx Context, args []string) {
687 for i := 0; i < len(args); i++ {
688 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100689 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700690 c.verbose = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200691 } else if arg == "--empty-ninja-file" {
692 c.emptyNinjaFile = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100693 } else if arg == "--skip-ninja" {
694 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700695 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700696 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
697 // but it does run a Kati ninja file if the .kati_enabled marker file was created
698 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000699 c.skipConfig = true
700 c.skipKati = true
701 } else if arg == "--skip-kati" {
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100702 // TODO: remove --skip-kati once module builds have been migrated to --song-only
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000703 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100704 } else if arg == "--soong-only" {
705 c.skipKati = true
706 c.skipKatiNinja = true
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200707 } else if arg == "--config-only" {
708 c.skipKati = true
709 c.skipKatiNinja = true
710 c.skipSoong = true
Colin Cross30e444b2021-06-18 11:26:19 -0700711 } else if arg == "--skip-config" {
712 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700713 } else if arg == "--skip-soong-tests" {
714 c.skipSoongTests = true
Chris Parsons53f68ae2022-03-03 12:01:40 -0500715 } else if arg == "--mk-metrics" {
716 c.reportMkMetrics = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700717 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700718 parseArgNum := func(def int) int {
719 if len(arg) > 2 {
720 p, err := strconv.ParseUint(arg[2:], 10, 31)
721 if err != nil {
722 ctx.Fatalf("Failed to parse %q: %v", arg, err)
723 }
724 return int(p)
725 } else if i+1 < len(args) {
726 p, err := strconv.ParseUint(args[i+1], 10, 31)
727 if err == nil {
728 i++
729 return int(p)
730 }
731 }
732 return def
733 }
734
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700735 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700736 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700737 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700738 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700739 } else {
740 ctx.Fatalln("Unknown option:", arg)
741 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700742 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700743 if k == "OUT_DIR" {
744 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
745 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700746 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700747 } else if arg == "dist" {
748 c.dist = true
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200749 } else if arg == "json-module-graph" {
750 c.jsonModuleGraph = true
751 } else if arg == "bp2build" {
752 c.bp2build = true
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200753 } else if arg == "queryview" {
754 c.queryview = true
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200755 } else if arg == "soong_docs" {
756 c.soongDocs = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700757 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700758 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800759 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700760 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700761 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700762 }
763 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700764}
765
Dan Willemsened869522018-01-08 14:58:46 -0800766func (c *configImpl) configureLocale(ctx Context) {
767 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
768 output, err := cmd.Output()
769
770 var locales []string
771 if err == nil {
772 locales = strings.Split(string(output), "\n")
773 } else {
774 // If we're unable to list the locales, let's assume en_US.UTF-8
775 locales = []string{"en_US.UTF-8"}
776 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
777 }
778
779 // gettext uses LANGUAGE, which is passed directly through
780
781 // For LANG and LC_*, only preserve the evaluated version of
782 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800783 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800784 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800785 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800786 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800787 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800788 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800789 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800790 }
791
792 c.environ.UnsetWithPrefix("LC_")
793
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800794 if userLang != "" {
795 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800796 }
797
798 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
799 // for others)
800 if inList("C.UTF-8", locales) {
801 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500802 } else if inList("C.utf8", locales) {
803 // These normalize to the same thing
804 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800805 } else if inList("en_US.UTF-8", locales) {
806 c.environ.Set("LANG", "en_US.UTF-8")
807 } else if inList("en_US.utf8", locales) {
808 // These normalize to the same thing
809 c.environ.Set("LANG", "en_US.UTF-8")
810 } else {
811 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
812 }
813}
814
Dan Willemsen1e704462016-08-21 15:17:17 -0700815func (c *configImpl) Environment() *Environment {
816 return c.environ
817}
818
819func (c *configImpl) Arguments() []string {
820 return c.arguments
821}
822
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200823func (c *configImpl) SoongBuildInvocationNeeded() bool {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200824 if len(c.Arguments()) > 0 {
825 // Explicit targets requested that are not special targets like b2pbuild
826 // or the JSON module graph
827 return true
828 }
829
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200830 if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() {
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200831 // Command line was empty, the default Ninja target is built
832 return true
833 }
834
Liz Kammer88677422021-12-15 15:03:19 -0500835 // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation
836 if c.Dist() && !c.Bp2Build() {
837 return true
838 }
839
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200840 // build.ninja doesn't need to be generated
841 return false
842}
843
Dan Willemsen1e704462016-08-21 15:17:17 -0700844func (c *configImpl) OutDir() string {
845 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700846 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700847 }
848 return "out"
849}
850
Dan Willemsen8a073a82017-02-04 17:30:44 -0800851func (c *configImpl) DistDir() string {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000852 if c.UseBazel() {
853 return c.riggedDistDirForBazel
854 } else {
855 return c.distDir
856 }
857}
858
859func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700860 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800861}
862
Dan Willemsen1e704462016-08-21 15:17:17 -0700863func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000864 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700865 return c.arguments
866 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700867 return c.ninjaArgs
868}
869
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500870func (c *configImpl) BazelOutDir() string {
871 return filepath.Join(c.OutDir(), "bazel")
872}
873
Dan Willemsen1e704462016-08-21 15:17:17 -0700874func (c *configImpl) SoongOutDir() string {
875 return filepath.Join(c.OutDir(), "soong")
876}
877
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200878func (c *configImpl) PrebuiltOS() string {
879 switch runtime.GOOS {
880 case "linux":
881 return "linux-x86"
882 case "darwin":
883 return "darwin-x86"
884 default:
885 panic("Unknown GOOS")
886 }
887}
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100888
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200889func (c *configImpl) HostToolDir() string {
Colin Crossacfcc1f2021-10-25 15:40:32 -0700890 if c.SkipKatiNinja() {
891 return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin")
892 } else {
893 return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin")
894 }
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200895}
896
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200897func (c *configImpl) NamedGlobFile(name string) string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100898 return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja")
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200899}
900
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200901func (c *configImpl) UsedEnvFile(tag string) string {
902 return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag)
903}
904
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200905func (c *configImpl) Bp2BuildMarkerFile() string {
Lukacs T. Berki90b43342021-11-02 14:42:04 +0100906 return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200907}
908
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200909func (c *configImpl) SoongDocsHtml() string {
910 return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html")
911}
912
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200913func (c *configImpl) QueryviewMarkerFile() string {
914 return shared.JoinPath(c.SoongOutDir(), "queryview.marker")
915}
916
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200917func (c *configImpl) ModuleGraphFile() string {
918 return shared.JoinPath(c.SoongOutDir(), "module-graph.json")
919}
920
kgui67007242022-01-25 13:50:25 +0800921func (c *configImpl) ModuleActionsFile() string {
922 return shared.JoinPath(c.SoongOutDir(), "module-actions.json")
923}
924
Jeff Gastonefc1b412017-03-29 17:29:06 -0700925func (c *configImpl) TempDir() string {
926 return shared.TempDirForOutDir(c.SoongOutDir())
927}
928
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700929func (c *configImpl) FileListDir() string {
930 return filepath.Join(c.OutDir(), ".module_paths")
931}
932
Dan Willemsen1e704462016-08-21 15:17:17 -0700933func (c *configImpl) KatiSuffix() string {
934 if c.katiSuffix != "" {
935 return c.katiSuffix
936 }
937 panic("SetKatiSuffix has not been called")
938}
939
Colin Cross37193492017-11-16 17:55:00 -0800940// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
941// user is interested in additional checks at the expense of build time.
942func (c *configImpl) Checkbuild() bool {
943 return c.checkbuild
944}
945
Dan Willemsen8a073a82017-02-04 17:30:44 -0800946func (c *configImpl) Dist() bool {
947 return c.dist
948}
949
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200950func (c *configImpl) JsonModuleGraph() bool {
951 return c.jsonModuleGraph
952}
953
954func (c *configImpl) Bp2Build() bool {
955 return c.bp2build
956}
957
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200958func (c *configImpl) Queryview() bool {
959 return c.queryview
960}
961
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200962func (c *configImpl) SoongDocs() bool {
963 return c.soongDocs
964}
965
Dan Willemsen1e704462016-08-21 15:17:17 -0700966func (c *configImpl) IsVerbose() bool {
967 return c.verbose
968}
969
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000970func (c *configImpl) SkipKati() bool {
971 return c.skipKati
972}
973
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100974func (c *configImpl) SkipKatiNinja() bool {
975 return c.skipKatiNinja
976}
977
Lukacs T. Berkicef87b62021-08-10 15:01:13 +0200978func (c *configImpl) SkipSoong() bool {
979 return c.skipSoong
980}
981
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100982func (c *configImpl) SkipNinja() bool {
983 return c.skipNinja
984}
985
Anton Hansson5a7861a2021-06-04 10:09:01 +0100986func (c *configImpl) SetSkipNinja(v bool) {
987 c.skipNinja = v
988}
989
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000990func (c *configImpl) SkipConfig() bool {
991 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -0700992}
993
Dan Willemsen1e704462016-08-21 15:17:17 -0700994func (c *configImpl) TargetProduct() string {
995 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
996 return v
997 }
998 panic("TARGET_PRODUCT is not defined")
999}
1000
Dan Willemsen02781d52017-05-12 19:28:13 -07001001func (c *configImpl) TargetDevice() string {
1002 return c.targetDevice
1003}
1004
1005func (c *configImpl) SetTargetDevice(device string) {
1006 c.targetDevice = device
1007}
1008
1009func (c *configImpl) TargetBuildVariant() string {
1010 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
1011 return v
1012 }
1013 panic("TARGET_BUILD_VARIANT is not defined")
1014}
1015
Dan Willemsen1e704462016-08-21 15:17:17 -07001016func (c *configImpl) KatiArgs() []string {
1017 return c.katiArgs
1018}
1019
1020func (c *configImpl) Parallel() int {
1021 return c.parallel
1022}
1023
Colin Cross8b8bec32019-11-15 13:18:43 -08001024func (c *configImpl) HighmemParallel() int {
1025 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
1026 return i
1027 }
1028
1029 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
1030 parallel := c.Parallel()
1031 if c.UseRemoteBuild() {
1032 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
1033 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
1034 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
1035 // Return 1/16th of the size of the local pool, rounding up.
1036 return (parallel + 15) / 16
1037 } else if c.totalRAM == 0 {
1038 // Couldn't detect the total RAM, don't restrict highmem processes.
1039 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -07001040 } else if c.totalRAM <= 16*1024*1024*1024 {
1041 // Less than 16GB of ram, restrict to 1 highmem processes
1042 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -08001043 } else if c.totalRAM <= 32*1024*1024*1024 {
1044 // Less than 32GB of ram, restrict to 2 highmem processes
1045 return 2
1046 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
1047 // If less than 8GB total RAM per process, reduce the number of highmem processes
1048 return p
1049 }
1050 // No restriction on highmem processes
1051 return parallel
1052}
1053
Dan Willemsen2bb82d02019-12-27 09:35:42 -08001054func (c *configImpl) TotalRAM() uint64 {
1055 return c.totalRAM
1056}
1057
Kousik Kumarec478642020-09-21 13:39:24 -04001058// ForceUseGoma determines whether we should override Goma deprecation
1059// and use Goma for the current build or not.
1060func (c *configImpl) ForceUseGoma() bool {
1061 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
1062 v = strings.TrimSpace(v)
1063 if v != "" && v != "false" {
1064 return true
1065 }
1066 }
1067 return false
1068}
1069
Dan Willemsen1e704462016-08-21 15:17:17 -07001070func (c *configImpl) UseGoma() bool {
1071 if v, ok := c.environ.Get("USE_GOMA"); ok {
1072 v = strings.TrimSpace(v)
1073 if v != "" && v != "false" {
1074 return true
1075 }
1076 }
1077 return false
1078}
1079
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +09001080func (c *configImpl) StartGoma() bool {
1081 if !c.UseGoma() {
1082 return false
1083 }
1084
1085 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
1086 v = strings.TrimSpace(v)
1087 if v != "" && v != "false" {
1088 return false
1089 }
1090 }
1091 return true
1092}
1093
Ramy Medhatbbf25672019-07-17 12:30:04 +00001094func (c *configImpl) UseRBE() bool {
Kousik Kumar3ff037e2022-01-25 22:11:01 -05001095 if v, ok := c.Environment().Get("USE_RBE"); ok {
Ramy Medhatbbf25672019-07-17 12:30:04 +00001096 v = strings.TrimSpace(v)
1097 if v != "" && v != "false" {
1098 return true
1099 }
1100 }
1101 return false
1102}
1103
Patrice Arruda0c1c4562020-11-11 13:01:25 -08001104func (c *configImpl) UseBazel() bool {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001105 return c.useBazel
Patrice Arruda0c1c4562020-11-11 13:01:25 -08001106}
1107
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001108func (c *configImpl) bazelBuildMode() bazelBuildMode {
1109 if c.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") {
1110 return mixedBuild
Chris Parsonsec1a3dc2021-04-20 15:32:07 -04001111 } else {
1112 return noBazel
1113 }
1114}
1115
Ramy Medhatbbf25672019-07-17 12:30:04 +00001116func (c *configImpl) StartRBE() bool {
1117 if !c.UseRBE() {
1118 return false
1119 }
1120
1121 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
1122 v = strings.TrimSpace(v)
1123 if v != "" && v != "false" {
1124 return false
1125 }
1126 }
1127 return true
1128}
1129
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001130func (c *configImpl) rbeLogDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -04001131 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
1132 if v, ok := c.environ.Get(f); ok {
1133 return v
1134 }
1135 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001136 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001137 return c.LogsDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001138 }
1139 return c.OutDir()
1140}
1141
1142func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001143 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
1144 if v, ok := c.environ.Get(f); ok {
1145 return v
1146 }
1147 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001148 return c.rbeLogDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001149}
1150
1151func (c *configImpl) rbeLogPath() string {
1152 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
1153 if v, ok := c.environ.Get(f); ok {
1154 return v
1155 }
1156 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001157 return fmt.Sprintf("text://%v/reproxy_log.txt", c.rbeLogDir())
Ramy Medhat0fc67eb2020-08-12 01:26:23 -04001158}
1159
1160func (c *configImpl) rbeExecRoot() string {
1161 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
1162 if v, ok := c.environ.Get(f); ok {
1163 return v
1164 }
1165 }
1166 wd, err := os.Getwd()
1167 if err != nil {
1168 return ""
1169 }
1170 return wd
1171}
1172
1173func (c *configImpl) rbeDir() string {
1174 if v, ok := c.environ.Get("RBE_DIR"); ok {
1175 return v
1176 }
1177 return "prebuilts/remoteexecution-client/live/"
1178}
1179
1180func (c *configImpl) rbeReproxy() string {
1181 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1182 if v, ok := c.environ.Get(f); ok {
1183 return v
1184 }
1185 }
1186 return filepath.Join(c.rbeDir(), "reproxy")
1187}
1188
1189func (c *configImpl) rbeAuth() (string, string) {
1190 credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"}
1191 for _, cf := range credFlags {
1192 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1193 if v, ok := c.environ.Get(f); ok {
1194 v = strings.TrimSpace(v)
1195 if v != "" && v != "false" && v != "0" {
1196 return "RBE_" + cf, v
1197 }
1198 }
1199 }
1200 }
1201 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001202}
1203
Colin Cross9016b912019-11-11 14:57:42 -08001204func (c *configImpl) UseRemoteBuild() bool {
1205 return c.UseGoma() || c.UseRBE()
1206}
1207
Dan Willemsen1e704462016-08-21 15:17:17 -07001208// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001209// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001210// still limited by Parallel()
1211func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001212 if !c.UseRemoteBuild() {
1213 return 0
1214 }
1215 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1216 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001217 }
1218 return 500
1219}
1220
1221func (c *configImpl) SetKatiArgs(args []string) {
1222 c.katiArgs = args
1223}
1224
1225func (c *configImpl) SetNinjaArgs(args []string) {
1226 c.ninjaArgs = args
1227}
1228
1229func (c *configImpl) SetKatiSuffix(suffix string) {
1230 c.katiSuffix = suffix
1231}
1232
Dan Willemsene0879fc2017-08-04 15:06:27 -07001233func (c *configImpl) LastKatiSuffixFile() string {
1234 return filepath.Join(c.OutDir(), "last_kati_suffix")
1235}
1236
1237func (c *configImpl) HasKatiSuffix() bool {
1238 return c.katiSuffix != ""
1239}
1240
Dan Willemsen1e704462016-08-21 15:17:17 -07001241func (c *configImpl) KatiEnvFile() string {
1242 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1243}
1244
Dan Willemsen29971232018-09-26 14:58:30 -07001245func (c *configImpl) KatiBuildNinjaFile() string {
1246 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001247}
1248
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001249func (c *configImpl) KatiPackageNinjaFile() string {
1250 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1251}
1252
Dan Willemsen1e704462016-08-21 15:17:17 -07001253func (c *configImpl) SoongNinjaFile() string {
1254 return filepath.Join(c.SoongOutDir(), "build.ninja")
1255}
1256
1257func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001258 if c.katiSuffix == "" {
1259 return filepath.Join(c.OutDir(), "combined.ninja")
1260 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001261 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1262}
1263
1264func (c *configImpl) SoongAndroidMk() string {
1265 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1266}
1267
1268func (c *configImpl) SoongMakeVarsMk() string {
1269 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1270}
1271
Dan Willemsenf052f782017-05-18 15:29:04 -07001272func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001273 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001274}
1275
Dan Willemsen02781d52017-05-12 19:28:13 -07001276func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001277 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1278}
1279
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001280func (c *configImpl) KatiPackageMkDir() string {
1281 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1282}
1283
Dan Willemsenf052f782017-05-18 15:29:04 -07001284func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001285 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001286}
1287
1288func (c *configImpl) HostOut() string {
1289 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1290}
1291
1292// This probably needs to be multi-valued, so not exporting it for now
1293func (c *configImpl) hostCrossOut() string {
1294 if runtime.GOOS == "linux" {
1295 return filepath.Join(c.hostOutRoot(), "windows-x86")
1296 } else {
1297 return ""
1298 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001299}
1300
Dan Willemsen1e704462016-08-21 15:17:17 -07001301func (c *configImpl) HostPrebuiltTag() string {
1302 if runtime.GOOS == "linux" {
1303 return "linux-x86"
1304 } else if runtime.GOOS == "darwin" {
1305 return "darwin-x86"
1306 } else {
1307 panic("Unsupported OS")
1308 }
1309}
Dan Willemsenf173d592017-04-27 14:28:00 -07001310
Dan Willemsen8122bd52017-10-12 20:20:41 -07001311func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001312 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1313 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001314 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1315 if _, err := os.Stat(asan); err == nil {
1316 return asan
1317 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001318 }
1319 }
1320 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1321}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001322
1323func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1324 c.brokenDupRules = val
1325}
1326
1327func (c *configImpl) BuildBrokenDupRules() bool {
1328 return c.brokenDupRules
1329}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001330
Dan Willemsen25e6f092019-04-09 10:22:43 -07001331func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1332 c.brokenUsesNetwork = val
1333}
1334
1335func (c *configImpl) BuildBrokenUsesNetwork() bool {
1336 return c.brokenUsesNetwork
1337}
1338
Dan Willemsene3336352020-01-02 19:10:38 -08001339func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1340 c.brokenNinjaEnvVars = val
1341}
1342
1343func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1344 return c.brokenNinjaEnvVars
1345}
1346
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001347func (c *configImpl) SetTargetDeviceDir(dir string) {
1348 c.targetDeviceDir = dir
1349}
1350
1351func (c *configImpl) TargetDeviceDir() string {
1352 return c.targetDeviceDir
1353}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001354
Patrice Arruda219eef32020-06-01 17:29:30 +00001355func (c *configImpl) BuildDateTime() string {
1356 return c.buildDateTime
1357}
1358
1359func (c *configImpl) MetricsUploaderApp() string {
Yu Liu6e13b402021-07-27 14:29:06 -07001360 return c.metricsUploader
Patrice Arruda219eef32020-06-01 17:29:30 +00001361}
Patrice Arruda83842d72020-12-08 19:42:08 +00001362
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001363// LogsDir returns the absolute path to the logs directory where build log and
1364// metrics files are located. By default, the logs directory is the out
Patrice Arruda83842d72020-12-08 19:42:08 +00001365// directory. If the argument dist is specified, the logs directory
1366// is <dist_dir>/logs.
1367func (c *configImpl) LogsDir() string {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001368 dir := c.OutDir()
Patrice Arruda83842d72020-12-08 19:42:08 +00001369 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001370 // 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 -05001371 dir = filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001372 }
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux947fdbf2021-11-10 09:55:20 -05001373 absDir, err := filepath.Abs(dir)
1374 if err != nil {
1375 fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error())
1376 os.Exit(1)
1377 }
1378 return absDir
Patrice Arruda83842d72020-12-08 19:42:08 +00001379}
1380
1381// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1382// where the bazel profiles are located.
1383func (c *configImpl) BazelMetricsDir() string {
1384 return filepath.Join(c.LogsDir(), "bazel_metrics")
1385}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001386
Chris Parsons53f68ae2022-03-03 12:01:40 -05001387// MkFileMetrics returns the file path for make-related metrics.
1388func (c *configImpl) MkMetrics() string {
1389 return filepath.Join(c.LogsDir(), "mk_metrics.pb")
1390}
1391
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001392func (c *configImpl) SetEmptyNinjaFile(v bool) {
1393 c.emptyNinjaFile = v
1394}
1395
1396func (c *configImpl) EmptyNinjaFile() bool {
1397 return c.emptyNinjaFile
1398}
Yu Liu6e13b402021-07-27 14:29:06 -07001399
1400func GetMetricsUploader(topDir string, env *Environment) string {
1401 if p, ok := env.Get("METRICS_UPLOADER"); ok {
1402 metricsUploader := filepath.Join(topDir, p)
1403 if _, err := os.Stat(metricsUploader); err == nil {
1404 return metricsUploader
1405 }
1406 }
1407
1408 return ""
1409}