blob: 7e119f361c1a45e1e202ccd6a0ab460fee3f3562 [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 (
Ramy Medhat0fc67eb2020-08-12 01:26:23 -040018 "fmt"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080019 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "path/filepath"
21 "runtime"
22 "strconv"
23 "strings"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080024 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070025
26 "android/soong/shared"
Kousik Kumarec478642020-09-21 13:39:24 -040027
Patrice Arruda96850362020-08-11 20:41:11 +000028 "github.com/golang/protobuf/proto"
29
30 smpb "android/soong/ui/metrics/metrics_proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070031)
32
33type Config struct{ *configImpl }
34
35type configImpl struct {
36 // From the environment
Colin Cross28f527c2019-11-26 16:19:04 -080037 arguments []string
38 goma bool
39 environ *Environment
40 distDir string
41 buildDateTime string
Dan Willemsen1e704462016-08-21 15:17:17 -070042
43 // From the arguments
Colin Cross00a8a3f2020-10-29 14:08:31 -070044 parallel int
45 keepGoing int
46 verbose bool
47 checkbuild bool
48 dist bool
Anton Hansson5e5c48b2020-11-27 12:35:20 +000049 skipConfig bool
50 skipKati bool
Anton Hansson0b55bdb2021-06-04 10:08:08 +010051 skipKatiNinja bool
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010052 skipNinja bool
Colin Cross00a8a3f2020-10-29 14:08:31 -070053 skipSoongTests bool
Dan Willemsen1e704462016-08-21 15:17:17 -070054
55 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070056 katiArgs []string
57 ninjaArgs []string
58 katiSuffix string
59 targetDevice string
60 targetDeviceDir string
Spandan Dasa3639e62021-05-25 19:14:02 +000061 sandboxConfig *SandboxConfig
Dan Willemsen3d60b112018-04-04 22:25:56 -070062
Dan Willemsen2bb82d02019-12-27 09:35:42 -080063 // Autodetected
64 totalRAM uint64
65
Dan Willemsene3336352020-01-02 19:10:38 -080066 brokenDupRules bool
67 brokenUsesNetwork bool
68 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -070069
70 pathReplaced bool
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +000071
72 useBazel bool
73
74 // During Bazel execution, Bazel cannot write outside OUT_DIR.
75 // 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.
76 riggedDistDirForBazel string
Colin Crossf3bdbcb2021-06-01 11:43:55 -070077
78 // Set by multiproduct_kati
79 emptyNinjaFile bool
Dan Willemsen1e704462016-08-21 15:17:17 -070080}
81
Dan Willemsenc2af0be2017-01-20 14:10:01 -080082const srcDirFileCheck = "build/soong/root.bp"
83
Patrice Arruda9450d0b2019-07-08 11:06:46 -070084var buildFiles = []string{"Android.mk", "Android.bp"}
85
Patrice Arruda13848222019-04-22 17:12:02 -070086type BuildAction uint
87
88const (
89 // Builds all of the modules and their dependencies of a specified directory, relative to the root
90 // directory of the source tree.
91 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
92
93 // Builds all of the modules and their dependencies of a list of specified directories. All specified
94 // directories are relative to the root directory of the source tree.
95 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070096
97 // Build a list of specified modules. If none was specified, simply build the whole source tree.
98 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070099)
100
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400101type bazelBuildMode int
102
103// Bazel-related build modes.
104const (
105 // Don't use bazel at all.
106 noBazel bazelBuildMode = iota
107
108 // Only generate build files (in a subdirectory of the out directory) and exit.
109 generateBuildFiles
110
111 // Generate synthetic build files and incorporate these files into a build which
112 // partially uses Bazel. Build metadata may come from Android.bp or BUILD files.
113 mixedBuild
114)
115
Patrice Arruda13848222019-04-22 17:12:02 -0700116// checkTopDir validates that the current directory is at the root directory of the source tree.
117func checkTopDir(ctx Context) {
118 if _, err := os.Stat(srcDirFileCheck); err != nil {
119 if os.IsNotExist(err) {
120 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
121 }
122 ctx.Fatalln("Error verifying tree state:", err)
123 }
124}
125
Dan Willemsen1e704462016-08-21 15:17:17 -0700126func NewConfig(ctx Context, args ...string) Config {
127 ret := &configImpl{
Spandan Dasa3639e62021-05-25 19:14:02 +0000128 environ: OsEnvironment(),
129 sandboxConfig: &SandboxConfig{},
Dan Willemsen1e704462016-08-21 15:17:17 -0700130 }
131
Patrice Arruda90109172020-07-28 18:07:27 +0000132 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700133 ret.parallel = runtime.NumCPU() + 2
134 ret.keepGoing = 1
135
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800136 ret.totalRAM = detectTotalRAM(ctx)
137
Dan Willemsen9b587492017-07-10 22:13:00 -0700138 ret.parseArgs(ctx, args)
139
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800140 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700141 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
142 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
143 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800144 outDir := "out"
145 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
146 if wd, err := os.Getwd(); err != nil {
147 ctx.Fatalln("Failed to get working directory:", err)
148 } else {
149 outDir = filepath.Join(baseDir, filepath.Base(wd))
150 }
151 }
152 ret.environ.Set("OUT_DIR", outDir)
153 }
154
Dan Willemsen2d31a442018-10-20 21:33:41 -0700155 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
156 ret.distDir = filepath.Clean(distDir)
157 } else {
158 ret.distDir = filepath.Join(ret.OutDir(), "dist")
159 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700160
Spandan Das05063612021-06-25 01:39:04 +0000161 if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok {
162 ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false")
163 }
164
Dan Willemsen1e704462016-08-21 15:17:17 -0700165 ret.environ.Unset(
166 // We're already using it
167 "USE_SOONG_UI",
168
169 // We should never use GOROOT/GOPATH from the shell environment
170 "GOROOT",
171 "GOPATH",
172
173 // These should only come from Soong, not the environment.
174 "CLANG",
175 "CLANG_CXX",
176 "CCC_CC",
177 "CCC_CXX",
178
179 // Used by the goma compiler wrapper, but should only be set by
180 // gomacc
181 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800182
183 // We handle this above
184 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700185
Dan Willemsen2d31a442018-10-20 21:33:41 -0700186 // This is handled above too, and set for individual commands later
187 "DIST_DIR",
188
Dan Willemsen68a09852017-04-18 13:56:57 -0700189 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000190 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700191 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700192 "DISPLAY",
193 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700194 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700195 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700196
197 // Drop make flags
198 "MAKEFLAGS",
199 "MAKELEVEL",
200 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700201
202 // Set in envsetup.sh, reset in makefiles
203 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700204
205 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
206 "ANDROID_BUILD_TOP",
207 "ANDROID_HOST_OUT",
208 "ANDROID_PRODUCT_OUT",
209 "ANDROID_HOST_OUT_TESTCASES",
210 "ANDROID_TARGET_OUT_TESTCASES",
211 "ANDROID_TOOLCHAIN",
212 "ANDROID_TOOLCHAIN_2ND_ARCH",
213 "ANDROID_DEV_SCRIPTS",
214 "ANDROID_EMULATOR_PREBUILTS",
215 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700216 )
217
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400218 if ret.UseGoma() || ret.ForceUseGoma() {
219 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
220 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400221 }
222
Dan Willemsen1e704462016-08-21 15:17:17 -0700223 // Tell python not to spam the source tree with .pyc files.
224 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
225
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400226 tmpDir := absPath(ctx, ret.TempDir())
227 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800228
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700229 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
230 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
231 "llvm-binutils-stable/llvm-symbolizer")
232 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
233
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800234 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700235 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800236
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700237 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700238 ctx.Println("You are building in a directory whose absolute path contains a space character:")
239 ctx.Println()
240 ctx.Printf("%q\n", srcDir)
241 ctx.Println()
242 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700243 }
244
245 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700246 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
247 ctx.Println()
248 ctx.Printf("%q\n", outDir)
249 ctx.Println()
250 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700251 }
252
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000253 if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700254 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
255 ctx.Println()
256 ctx.Printf("%q\n", distDir)
257 ctx.Println()
258 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700259 }
260
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700261 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000262 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
263 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100264 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700265 javaHome := func() string {
266 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
267 return override
268 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000269 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
270 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 +0100271 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000272 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700273 }()
274 absJavaHome := absPath(ctx, javaHome)
275
Dan Willemsened869522018-01-08 14:58:46 -0800276 ret.configureLocale(ctx)
277
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700278 newPath := []string{filepath.Join(absJavaHome, "bin")}
279 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
280 newPath = append(newPath, path)
281 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100282
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700283 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
284 ret.environ.Set("JAVA_HOME", absJavaHome)
285 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000286 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
287 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100288 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700289 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
290
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800291 outDir := ret.OutDir()
292 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800293 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800294 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800295 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800296 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800297 }
Colin Cross28f527c2019-11-26 16:19:04 -0800298
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800299 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
300
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400301 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400302 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400303 ret.environ.Set(k, v)
304 }
305 }
306
Patrice Arruda83842d72020-12-08 19:42:08 +0000307 bpd := ret.BazelMetricsDir()
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800308 if err := os.RemoveAll(bpd); err != nil {
309 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
310 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000311
312 ret.useBazel = ret.environ.IsEnvTrue("USE_BAZEL")
313
Patrice Arrudaaf880da2020-11-13 08:41:26 -0800314 if ret.UseBazel() {
315 if err := os.MkdirAll(bpd, 0777); err != nil {
316 ctx.Fatalf("Failed to create bazel profile directory %q: %v", bpd, err)
317 }
318 }
319
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000320 if ret.UseBazel() {
321 ret.riggedDistDirForBazel = filepath.Join(ret.OutDir(), "dist")
322 } else {
323 // Not rigged
324 ret.riggedDistDirForBazel = ret.distDir
325 }
326
Patrice Arruda96850362020-08-11 20:41:11 +0000327 c := Config{ret}
328 storeConfigMetrics(ctx, c)
329 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700330}
331
Patrice Arruda13848222019-04-22 17:12:02 -0700332// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
333// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700334func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
335 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700336}
337
Patrice Arruda96850362020-08-11 20:41:11 +0000338// storeConfigMetrics selects a set of configuration information and store in
339// the metrics system for further analysis.
340func storeConfigMetrics(ctx Context, config Config) {
341 if ctx.Metrics == nil {
342 return
343 }
344
345 b := &smpb.BuildConfig{
Patrice Arrudac97d6dc2020-09-28 18:22:07 +0000346 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
347 UseGoma: proto.Bool(config.UseGoma()),
348 UseRbe: proto.Bool(config.UseRBE()),
Patrice Arruda96850362020-08-11 20:41:11 +0000349 }
350 ctx.Metrics.BuildConfig(b)
Patrice Arruda3edfd482020-10-13 23:58:41 +0000351
352 s := &smpb.SystemResourceInfo{
353 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
354 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
355 }
356 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000357}
358
Patrice Arruda13848222019-04-22 17:12:02 -0700359// getConfigArgs processes the command arguments based on the build action and creates a set of new
360// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700361func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700362 // The next block of code verifies that the current directory is the root directory of the source
363 // tree. It then finds the relative path of dir based on the root directory of the source tree
364 // and verify that dir is inside of the source tree.
365 checkTopDir(ctx)
366 topDir, err := os.Getwd()
367 if err != nil {
368 ctx.Fatalf("Error retrieving top directory: %v", err)
369 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700370 dir, err = filepath.EvalSymlinks(dir)
371 if err != nil {
372 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
373 }
Patrice Arruda13848222019-04-22 17:12:02 -0700374 dir, err = filepath.Abs(dir)
375 if err != nil {
376 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
377 }
378 relDir, err := filepath.Rel(topDir, dir)
379 if err != nil {
380 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
381 }
382 // If there are ".." in the path, it's not in the source tree.
383 if strings.Contains(relDir, "..") {
384 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
385 }
386
387 configArgs := args[:]
388
389 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
390 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
391 targetNamePrefix := "MODULES-IN-"
392 if inList("GET-INSTALL-PATH", configArgs) {
393 targetNamePrefix = "GET-INSTALL-PATH-IN-"
394 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
395 }
396
Patrice Arruda13848222019-04-22 17:12:02 -0700397 var targets []string
398
399 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700400 case BUILD_MODULES:
401 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700402 case BUILD_MODULES_IN_A_DIRECTORY:
403 // If dir is the root source tree, all the modules are built of the source tree are built so
404 // no need to find the build file.
405 if topDir == dir {
406 break
407 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700408
Patrice Arruda13848222019-04-22 17:12:02 -0700409 buildFile := findBuildFile(ctx, relDir)
410 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700411 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700412 }
Patrice Arruda13848222019-04-22 17:12:02 -0700413 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
414 case BUILD_MODULES_IN_DIRECTORIES:
415 newConfigArgs, dirs := splitArgs(configArgs)
416 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700417 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700418 }
419
420 // Tidy only override all other specified targets.
421 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
422 if tidyOnly == "true" || tidyOnly == "1" {
423 configArgs = append(configArgs, "tidy_only")
424 } else {
425 configArgs = append(configArgs, targets...)
426 }
427
428 return configArgs
429}
430
431// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
432func convertToTarget(dir string, targetNamePrefix string) string {
433 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
434}
435
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700436// hasBuildFile returns true if dir contains an Android build file.
437func hasBuildFile(ctx Context, dir string) bool {
438 for _, buildFile := range buildFiles {
439 _, err := os.Stat(filepath.Join(dir, buildFile))
440 if err == nil {
441 return true
442 }
443 if !os.IsNotExist(err) {
444 ctx.Fatalf("Error retrieving the build file stats: %v", err)
445 }
446 }
447 return false
448}
449
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700450// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
451// in the current and any sub directory of dir. If a build file is not found, traverse the path
452// up by one directory and repeat again until either a build file is found or reached to the root
453// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
454// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700455func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700456 // If the string is empty or ".", assume it is top directory of the source tree.
457 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700458 return ""
459 }
460
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700461 found := false
462 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
463 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
464 if err != nil {
465 return err
466 }
467 if found {
468 return filepath.SkipDir
469 }
470 if info.IsDir() {
471 return nil
472 }
473 for _, buildFile := range buildFiles {
474 if info.Name() == buildFile {
475 found = true
476 return filepath.SkipDir
477 }
478 }
479 return nil
480 })
481 if err != nil {
482 ctx.Fatalf("Error finding Android build file: %v", err)
483 }
484
485 if found {
486 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700487 }
488 }
489
490 return ""
491}
492
493// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
494func splitArgs(args []string) (newArgs []string, dirs []string) {
495 specialArgs := map[string]bool{
496 "showcommands": true,
497 "snod": true,
498 "dist": true,
499 "checkbuild": true,
500 }
501
502 newArgs = []string{}
503 dirs = []string{}
504
505 for _, arg := range args {
506 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
507 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
508 newArgs = append(newArgs, arg)
509 continue
510 }
511
512 if _, ok := specialArgs[arg]; ok {
513 newArgs = append(newArgs, arg)
514 continue
515 }
516
517 dirs = append(dirs, arg)
518 }
519
520 return newArgs, dirs
521}
522
523// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
524// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
525// source root tree where the build action command was invoked. Each directory is validated if the
526// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700527func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700528 for _, dir := range dirs {
529 // The directory may have specified specific modules to build. ":" is the separator to separate
530 // the directory and the list of modules.
531 s := strings.Split(dir, ":")
532 l := len(s)
533 if l > 2 { // more than one ":" was specified.
534 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
535 }
536
537 dir = filepath.Join(relDir, s[0])
538 if _, err := os.Stat(dir); err != nil {
539 ctx.Fatalf("couldn't find directory %s", dir)
540 }
541
542 // Verify that if there are any targets specified after ":". Each target is separated by ",".
543 var newTargets []string
544 if l == 2 && s[1] != "" {
545 newTargets = strings.Split(s[1], ",")
546 if inList("", newTargets) {
547 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
548 }
549 }
550
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700551 // If there are specified targets to build in dir, an android build file must exist for the one
552 // shot build. For the non-targets case, find the appropriate build file and build all the
553 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700554 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700555 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700556 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
557 }
558 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700559 buildFile := findBuildFile(ctx, dir)
560 if buildFile == "" {
561 ctx.Fatalf("Build file not found for %s directory", dir)
562 }
563 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700564 }
565
Patrice Arruda13848222019-04-22 17:12:02 -0700566 targets = append(targets, newTargets...)
567 }
568
Dan Willemsence41e942019-07-29 23:39:30 -0700569 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700570}
571
Dan Willemsen9b587492017-07-10 22:13:00 -0700572func (c *configImpl) parseArgs(ctx Context, args []string) {
573 for i := 0; i < len(args); i++ {
574 arg := strings.TrimSpace(args[i])
Anton Hansson5a7861a2021-06-04 10:09:01 +0100575 if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700576 c.verbose = true
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100577 } else if arg == "--skip-ninja" {
578 c.skipNinja = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700579 } else if arg == "--skip-make" {
Colin Cross30e444b2021-06-18 11:26:19 -0700580 // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati,
581 // but it does run a Kati ninja file if the .kati_enabled marker file was created
582 // by a previous build.
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000583 c.skipConfig = true
584 c.skipKati = true
585 } else if arg == "--skip-kati" {
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100586 // TODO: remove --skip-kati once module builds have been migrated to --song-only
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000587 c.skipKati = true
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100588 } else if arg == "--soong-only" {
589 c.skipKati = true
590 c.skipKatiNinja = true
Colin Cross30e444b2021-06-18 11:26:19 -0700591 } else if arg == "--skip-config" {
592 c.skipConfig = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700593 } else if arg == "--skip-soong-tests" {
594 c.skipSoongTests = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700595 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700596 parseArgNum := func(def int) int {
597 if len(arg) > 2 {
598 p, err := strconv.ParseUint(arg[2:], 10, 31)
599 if err != nil {
600 ctx.Fatalf("Failed to parse %q: %v", arg, err)
601 }
602 return int(p)
603 } else if i+1 < len(args) {
604 p, err := strconv.ParseUint(args[i+1], 10, 31)
605 if err == nil {
606 i++
607 return int(p)
608 }
609 }
610 return def
611 }
612
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700613 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700614 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700615 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700616 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700617 } else {
618 ctx.Fatalln("Unknown option:", arg)
619 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700620 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700621 if k == "OUT_DIR" {
622 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
623 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700624 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700625 } else if arg == "dist" {
626 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700627 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700628 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800629 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700630 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700631 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700632 }
633 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700634}
635
Dan Willemsened869522018-01-08 14:58:46 -0800636func (c *configImpl) configureLocale(ctx Context) {
637 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
638 output, err := cmd.Output()
639
640 var locales []string
641 if err == nil {
642 locales = strings.Split(string(output), "\n")
643 } else {
644 // If we're unable to list the locales, let's assume en_US.UTF-8
645 locales = []string{"en_US.UTF-8"}
646 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
647 }
648
649 // gettext uses LANGUAGE, which is passed directly through
650
651 // For LANG and LC_*, only preserve the evaluated version of
652 // LC_MESSAGES
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800653 userLang := ""
Dan Willemsened869522018-01-08 14:58:46 -0800654 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800655 userLang = lc_all
Dan Willemsened869522018-01-08 14:58:46 -0800656 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800657 userLang = lc_messages
Dan Willemsened869522018-01-08 14:58:46 -0800658 } else if lang, ok := c.environ.Get("LANG"); ok {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800659 userLang = lang
Dan Willemsened869522018-01-08 14:58:46 -0800660 }
661
662 c.environ.UnsetWithPrefix("LC_")
663
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800664 if userLang != "" {
665 c.environ.Set("LC_MESSAGES", userLang)
Dan Willemsened869522018-01-08 14:58:46 -0800666 }
667
668 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
669 // for others)
670 if inList("C.UTF-8", locales) {
671 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500672 } else if inList("C.utf8", locales) {
673 // These normalize to the same thing
674 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800675 } else if inList("en_US.UTF-8", locales) {
676 c.environ.Set("LANG", "en_US.UTF-8")
677 } else if inList("en_US.utf8", locales) {
678 // These normalize to the same thing
679 c.environ.Set("LANG", "en_US.UTF-8")
680 } else {
681 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
682 }
683}
684
Dan Willemsen1e704462016-08-21 15:17:17 -0700685// Lunch configures the environment for a specific product similarly to the
686// `lunch` bash function.
687func (c *configImpl) Lunch(ctx Context, product, variant string) {
688 if variant != "eng" && variant != "userdebug" && variant != "user" {
689 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
690 }
691
692 c.environ.Set("TARGET_PRODUCT", product)
693 c.environ.Set("TARGET_BUILD_VARIANT", variant)
694 c.environ.Set("TARGET_BUILD_TYPE", "release")
695 c.environ.Unset("TARGET_BUILD_APPS")
Martin Stjernholm08802332020-06-04 17:00:01 +0100696 c.environ.Unset("TARGET_BUILD_UNBUNDLED")
Dan Willemsen1e704462016-08-21 15:17:17 -0700697}
698
699// Tapas configures the environment to build one or more unbundled apps,
700// similarly to the `tapas` bash function.
701func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
702 if len(apps) == 0 {
703 apps = []string{"all"}
704 }
705 if variant == "" {
706 variant = "eng"
707 }
708
709 if variant != "eng" && variant != "userdebug" && variant != "user" {
710 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
711 }
712
713 var product string
714 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700715 case "arm", "":
716 product = "aosp_arm"
717 case "arm64":
718 product = "aosm_arm64"
Dan Willemsen1e704462016-08-21 15:17:17 -0700719 case "x86":
720 product = "aosp_x86"
721 case "x86_64":
722 product = "aosp_x86_64"
723 default:
724 ctx.Fatalf("Invalid architecture: %q", arch)
725 }
726
727 c.environ.Set("TARGET_PRODUCT", product)
728 c.environ.Set("TARGET_BUILD_VARIANT", variant)
729 c.environ.Set("TARGET_BUILD_TYPE", "release")
730 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
731}
732
733func (c *configImpl) Environment() *Environment {
734 return c.environ
735}
736
737func (c *configImpl) Arguments() []string {
738 return c.arguments
739}
740
741func (c *configImpl) OutDir() string {
742 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700743 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700744 }
745 return "out"
746}
747
Dan Willemsen8a073a82017-02-04 17:30:44 -0800748func (c *configImpl) DistDir() string {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000749 if c.UseBazel() {
750 return c.riggedDistDirForBazel
751 } else {
752 return c.distDir
753 }
754}
755
756func (c *configImpl) RealDistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700757 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800758}
759
Dan Willemsen1e704462016-08-21 15:17:17 -0700760func (c *configImpl) NinjaArgs() []string {
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000761 if c.skipKati {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700762 return c.arguments
763 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700764 return c.ninjaArgs
765}
766
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500767func (c *configImpl) BazelOutDir() string {
768 return filepath.Join(c.OutDir(), "bazel")
769}
770
Dan Willemsen1e704462016-08-21 15:17:17 -0700771func (c *configImpl) SoongOutDir() string {
772 return filepath.Join(c.OutDir(), "soong")
773}
774
Jeff Gastonefc1b412017-03-29 17:29:06 -0700775func (c *configImpl) TempDir() string {
776 return shared.TempDirForOutDir(c.SoongOutDir())
777}
778
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700779func (c *configImpl) FileListDir() string {
780 return filepath.Join(c.OutDir(), ".module_paths")
781}
782
Dan Willemsen1e704462016-08-21 15:17:17 -0700783func (c *configImpl) KatiSuffix() string {
784 if c.katiSuffix != "" {
785 return c.katiSuffix
786 }
787 panic("SetKatiSuffix has not been called")
788}
789
Colin Cross37193492017-11-16 17:55:00 -0800790// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
791// user is interested in additional checks at the expense of build time.
792func (c *configImpl) Checkbuild() bool {
793 return c.checkbuild
794}
795
Dan Willemsen8a073a82017-02-04 17:30:44 -0800796func (c *configImpl) Dist() bool {
797 return c.dist
798}
799
Dan Willemsen1e704462016-08-21 15:17:17 -0700800func (c *configImpl) IsVerbose() bool {
801 return c.verbose
802}
803
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000804func (c *configImpl) SkipKati() bool {
805 return c.skipKati
806}
807
Anton Hansson0b55bdb2021-06-04 10:08:08 +0100808func (c *configImpl) SkipKatiNinja() bool {
809 return c.skipKatiNinja
810}
811
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100812func (c *configImpl) SkipNinja() bool {
813 return c.skipNinja
814}
815
Anton Hansson5a7861a2021-06-04 10:09:01 +0100816func (c *configImpl) SetSkipNinja(v bool) {
817 c.skipNinja = v
818}
819
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000820func (c *configImpl) SkipConfig() bool {
821 return c.skipConfig
Dan Willemsene0879fc2017-08-04 15:06:27 -0700822}
823
Dan Willemsen1e704462016-08-21 15:17:17 -0700824func (c *configImpl) TargetProduct() string {
825 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
826 return v
827 }
828 panic("TARGET_PRODUCT is not defined")
829}
830
Dan Willemsen02781d52017-05-12 19:28:13 -0700831func (c *configImpl) TargetDevice() string {
832 return c.targetDevice
833}
834
835func (c *configImpl) SetTargetDevice(device string) {
836 c.targetDevice = device
837}
838
839func (c *configImpl) TargetBuildVariant() string {
840 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
841 return v
842 }
843 panic("TARGET_BUILD_VARIANT is not defined")
844}
845
Dan Willemsen1e704462016-08-21 15:17:17 -0700846func (c *configImpl) KatiArgs() []string {
847 return c.katiArgs
848}
849
850func (c *configImpl) Parallel() int {
851 return c.parallel
852}
853
Colin Cross8b8bec32019-11-15 13:18:43 -0800854func (c *configImpl) HighmemParallel() int {
855 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
856 return i
857 }
858
859 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
860 parallel := c.Parallel()
861 if c.UseRemoteBuild() {
862 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
863 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
864 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
865 // Return 1/16th of the size of the local pool, rounding up.
866 return (parallel + 15) / 16
867 } else if c.totalRAM == 0 {
868 // Couldn't detect the total RAM, don't restrict highmem processes.
869 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -0700870 } else if c.totalRAM <= 16*1024*1024*1024 {
871 // Less than 16GB of ram, restrict to 1 highmem processes
872 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -0800873 } else if c.totalRAM <= 32*1024*1024*1024 {
874 // Less than 32GB of ram, restrict to 2 highmem processes
875 return 2
876 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
877 // If less than 8GB total RAM per process, reduce the number of highmem processes
878 return p
879 }
880 // No restriction on highmem processes
881 return parallel
882}
883
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800884func (c *configImpl) TotalRAM() uint64 {
885 return c.totalRAM
886}
887
Kousik Kumarec478642020-09-21 13:39:24 -0400888// ForceUseGoma determines whether we should override Goma deprecation
889// and use Goma for the current build or not.
890func (c *configImpl) ForceUseGoma() bool {
891 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
892 v = strings.TrimSpace(v)
893 if v != "" && v != "false" {
894 return true
895 }
896 }
897 return false
898}
899
Dan Willemsen1e704462016-08-21 15:17:17 -0700900func (c *configImpl) UseGoma() bool {
901 if v, ok := c.environ.Get("USE_GOMA"); ok {
902 v = strings.TrimSpace(v)
903 if v != "" && v != "false" {
904 return true
905 }
906 }
907 return false
908}
909
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900910func (c *configImpl) StartGoma() bool {
911 if !c.UseGoma() {
912 return false
913 }
914
915 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
916 v = strings.TrimSpace(v)
917 if v != "" && v != "false" {
918 return false
919 }
920 }
921 return true
922}
923
Ramy Medhatbbf25672019-07-17 12:30:04 +0000924func (c *configImpl) UseRBE() bool {
925 if v, ok := c.environ.Get("USE_RBE"); ok {
926 v = strings.TrimSpace(v)
927 if v != "" && v != "false" {
928 return true
929 }
930 }
931 return false
932}
933
Patrice Arruda0c1c4562020-11-11 13:01:25 -0800934func (c *configImpl) UseBazel() bool {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000935 return c.useBazel
Patrice Arruda0c1c4562020-11-11 13:01:25 -0800936}
937
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400938func (c *configImpl) bazelBuildMode() bazelBuildMode {
939 if c.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") {
940 return mixedBuild
941 } else if c.Environment().IsEnvTrue("GENERATE_BAZEL_FILES") {
942 return generateBuildFiles
943 } else {
944 return noBazel
945 }
946}
947
Ramy Medhatbbf25672019-07-17 12:30:04 +0000948func (c *configImpl) StartRBE() bool {
949 if !c.UseRBE() {
950 return false
951 }
952
953 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
954 v = strings.TrimSpace(v)
955 if v != "" && v != "false" {
956 return false
957 }
958 }
959 return true
960}
961
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000962func (c *configImpl) rbeLogDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -0400963 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
964 if v, ok := c.environ.Get(f); ok {
965 return v
966 }
967 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400968 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000969 return c.LogsDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400970 }
971 return c.OutDir()
972}
973
974func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000975 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
976 if v, ok := c.environ.Get(f); ok {
977 return v
978 }
979 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000980 return c.rbeLogDir()
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400981}
982
983func (c *configImpl) rbeLogPath() string {
984 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
985 if v, ok := c.environ.Get(f); ok {
986 return v
987 }
988 }
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +0000989 return fmt.Sprintf("text://%v/reproxy_log.txt", c.rbeLogDir())
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400990}
991
992func (c *configImpl) rbeExecRoot() string {
993 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
994 if v, ok := c.environ.Get(f); ok {
995 return v
996 }
997 }
998 wd, err := os.Getwd()
999 if err != nil {
1000 return ""
1001 }
1002 return wd
1003}
1004
1005func (c *configImpl) rbeDir() string {
1006 if v, ok := c.environ.Get("RBE_DIR"); ok {
1007 return v
1008 }
1009 return "prebuilts/remoteexecution-client/live/"
1010}
1011
1012func (c *configImpl) rbeReproxy() string {
1013 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
1014 if v, ok := c.environ.Get(f); ok {
1015 return v
1016 }
1017 }
1018 return filepath.Join(c.rbeDir(), "reproxy")
1019}
1020
1021func (c *configImpl) rbeAuth() (string, string) {
1022 credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"}
1023 for _, cf := range credFlags {
1024 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
1025 if v, ok := c.environ.Get(f); ok {
1026 v = strings.TrimSpace(v)
1027 if v != "" && v != "false" && v != "0" {
1028 return "RBE_" + cf, v
1029 }
1030 }
1031 }
1032 }
1033 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +00001034}
1035
Colin Cross9016b912019-11-11 14:57:42 -08001036func (c *configImpl) UseRemoteBuild() bool {
1037 return c.UseGoma() || c.UseRBE()
1038}
1039
Dan Willemsen1e704462016-08-21 15:17:17 -07001040// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -07001041// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -07001042// still limited by Parallel()
1043func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -08001044 if !c.UseRemoteBuild() {
1045 return 0
1046 }
1047 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
1048 return i
Dan Willemsen1e704462016-08-21 15:17:17 -07001049 }
1050 return 500
1051}
1052
1053func (c *configImpl) SetKatiArgs(args []string) {
1054 c.katiArgs = args
1055}
1056
1057func (c *configImpl) SetNinjaArgs(args []string) {
1058 c.ninjaArgs = args
1059}
1060
1061func (c *configImpl) SetKatiSuffix(suffix string) {
1062 c.katiSuffix = suffix
1063}
1064
Dan Willemsene0879fc2017-08-04 15:06:27 -07001065func (c *configImpl) LastKatiSuffixFile() string {
1066 return filepath.Join(c.OutDir(), "last_kati_suffix")
1067}
1068
1069func (c *configImpl) HasKatiSuffix() bool {
1070 return c.katiSuffix != ""
1071}
1072
Dan Willemsen1e704462016-08-21 15:17:17 -07001073func (c *configImpl) KatiEnvFile() string {
1074 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
1075}
1076
Dan Willemsen29971232018-09-26 14:58:30 -07001077func (c *configImpl) KatiBuildNinjaFile() string {
1078 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -07001079}
1080
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001081func (c *configImpl) KatiPackageNinjaFile() string {
1082 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1083}
1084
Dan Willemsen1e704462016-08-21 15:17:17 -07001085func (c *configImpl) SoongNinjaFile() string {
1086 return filepath.Join(c.SoongOutDir(), "build.ninja")
1087}
1088
1089func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001090 if c.katiSuffix == "" {
1091 return filepath.Join(c.OutDir(), "combined.ninja")
1092 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001093 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1094}
1095
1096func (c *configImpl) SoongAndroidMk() string {
1097 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1098}
1099
1100func (c *configImpl) SoongMakeVarsMk() string {
1101 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1102}
1103
Dan Willemsenf052f782017-05-18 15:29:04 -07001104func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001105 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001106}
1107
Dan Willemsen02781d52017-05-12 19:28:13 -07001108func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001109 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1110}
1111
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001112func (c *configImpl) KatiPackageMkDir() string {
1113 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1114}
1115
Dan Willemsenf052f782017-05-18 15:29:04 -07001116func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001117 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001118}
1119
1120func (c *configImpl) HostOut() string {
1121 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1122}
1123
1124// This probably needs to be multi-valued, so not exporting it for now
1125func (c *configImpl) hostCrossOut() string {
1126 if runtime.GOOS == "linux" {
1127 return filepath.Join(c.hostOutRoot(), "windows-x86")
1128 } else {
1129 return ""
1130 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001131}
1132
Dan Willemsen1e704462016-08-21 15:17:17 -07001133func (c *configImpl) HostPrebuiltTag() string {
1134 if runtime.GOOS == "linux" {
1135 return "linux-x86"
1136 } else if runtime.GOOS == "darwin" {
1137 return "darwin-x86"
1138 } else {
1139 panic("Unsupported OS")
1140 }
1141}
Dan Willemsenf173d592017-04-27 14:28:00 -07001142
Dan Willemsen8122bd52017-10-12 20:20:41 -07001143func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001144 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1145 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001146 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1147 if _, err := os.Stat(asan); err == nil {
1148 return asan
1149 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001150 }
1151 }
1152 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1153}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001154
1155func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1156 c.brokenDupRules = val
1157}
1158
1159func (c *configImpl) BuildBrokenDupRules() bool {
1160 return c.brokenDupRules
1161}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001162
Dan Willemsen25e6f092019-04-09 10:22:43 -07001163func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1164 c.brokenUsesNetwork = val
1165}
1166
1167func (c *configImpl) BuildBrokenUsesNetwork() bool {
1168 return c.brokenUsesNetwork
1169}
1170
Dan Willemsene3336352020-01-02 19:10:38 -08001171func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1172 c.brokenNinjaEnvVars = val
1173}
1174
1175func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1176 return c.brokenNinjaEnvVars
1177}
1178
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001179func (c *configImpl) SetTargetDeviceDir(dir string) {
1180 c.targetDeviceDir = dir
1181}
1182
1183func (c *configImpl) TargetDeviceDir() string {
1184 return c.targetDeviceDir
1185}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001186
Patrice Arruda219eef32020-06-01 17:29:30 +00001187func (c *configImpl) BuildDateTime() string {
1188 return c.buildDateTime
1189}
1190
1191func (c *configImpl) MetricsUploaderApp() string {
1192 if p, ok := c.environ.Get("ANDROID_ENABLE_METRICS_UPLOAD"); ok {
1193 return p
1194 }
1195 return ""
1196}
Patrice Arruda83842d72020-12-08 19:42:08 +00001197
1198// LogsDir returns the logs directory where build log and metrics
1199// files are located. By default, the logs directory is the out
1200// directory. If the argument dist is specified, the logs directory
1201// is <dist_dir>/logs.
1202func (c *configImpl) LogsDir() string {
1203 if c.Dist() {
Rupert Shuttleworth3c9f5ac2020-12-10 11:32:38 +00001204 // Always write logs to the real dist dir, even if Bazel is using a rigged dist dir for other files
1205 return filepath.Join(c.RealDistDir(), "logs")
Patrice Arruda83842d72020-12-08 19:42:08 +00001206 }
1207 return c.OutDir()
1208}
1209
1210// BazelMetricsDir returns the <logs dir>/bazel_metrics directory
1211// where the bazel profiles are located.
1212func (c *configImpl) BazelMetricsDir() string {
1213 return filepath.Join(c.LogsDir(), "bazel_metrics")
1214}
Colin Crossf3bdbcb2021-06-01 11:43:55 -07001215
1216func (c *configImpl) SetEmptyNinjaFile(v bool) {
1217 c.emptyNinjaFile = v
1218}
1219
1220func (c *configImpl) EmptyNinjaFile() bool {
1221 return c.emptyNinjaFile
1222}