blob: fbe5cd24177104205645ea08935cda26f40bf834 [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 Cross37193492017-11-16 17:55:00 -080044 parallel int
45 keepGoing int
46 verbose bool
47 checkbuild bool
48 dist bool
49 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070050
51 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070052 katiArgs []string
53 ninjaArgs []string
54 katiSuffix string
55 targetDevice string
56 targetDeviceDir string
Dan Willemsen3d60b112018-04-04 22:25:56 -070057
Dan Willemsen2bb82d02019-12-27 09:35:42 -080058 // Autodetected
59 totalRAM uint64
60
Dan Willemsene3336352020-01-02 19:10:38 -080061 brokenDupRules bool
62 brokenUsesNetwork bool
63 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -070064
65 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070066}
67
Dan Willemsenc2af0be2017-01-20 14:10:01 -080068const srcDirFileCheck = "build/soong/root.bp"
69
Patrice Arruda9450d0b2019-07-08 11:06:46 -070070var buildFiles = []string{"Android.mk", "Android.bp"}
71
Patrice Arruda13848222019-04-22 17:12:02 -070072type BuildAction uint
73
74const (
75 // Builds all of the modules and their dependencies of a specified directory, relative to the root
76 // directory of the source tree.
77 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
78
79 // Builds all of the modules and their dependencies of a list of specified directories. All specified
80 // directories are relative to the root directory of the source tree.
81 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070082
83 // Build a list of specified modules. If none was specified, simply build the whole source tree.
84 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070085)
86
87// checkTopDir validates that the current directory is at the root directory of the source tree.
88func checkTopDir(ctx Context) {
89 if _, err := os.Stat(srcDirFileCheck); err != nil {
90 if os.IsNotExist(err) {
91 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
92 }
93 ctx.Fatalln("Error verifying tree state:", err)
94 }
95}
96
Dan Willemsen1e704462016-08-21 15:17:17 -070097func NewConfig(ctx Context, args ...string) Config {
98 ret := &configImpl{
99 environ: OsEnvironment(),
100 }
101
Patrice Arruda90109172020-07-28 18:07:27 +0000102 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700103 ret.parallel = runtime.NumCPU() + 2
104 ret.keepGoing = 1
105
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800106 ret.totalRAM = detectTotalRAM(ctx)
107
Dan Willemsen9b587492017-07-10 22:13:00 -0700108 ret.parseArgs(ctx, args)
109
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800110 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700111 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
112 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
113 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800114 outDir := "out"
115 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
116 if wd, err := os.Getwd(); err != nil {
117 ctx.Fatalln("Failed to get working directory:", err)
118 } else {
119 outDir = filepath.Join(baseDir, filepath.Base(wd))
120 }
121 }
122 ret.environ.Set("OUT_DIR", outDir)
123 }
124
Dan Willemsen2d31a442018-10-20 21:33:41 -0700125 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
126 ret.distDir = filepath.Clean(distDir)
127 } else {
128 ret.distDir = filepath.Join(ret.OutDir(), "dist")
129 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700130
Dan Willemsen1e704462016-08-21 15:17:17 -0700131 ret.environ.Unset(
132 // We're already using it
133 "USE_SOONG_UI",
134
135 // We should never use GOROOT/GOPATH from the shell environment
136 "GOROOT",
137 "GOPATH",
138
139 // These should only come from Soong, not the environment.
140 "CLANG",
141 "CLANG_CXX",
142 "CCC_CC",
143 "CCC_CXX",
144
145 // Used by the goma compiler wrapper, but should only be set by
146 // gomacc
147 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800148
149 // We handle this above
150 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700151
Dan Willemsen2d31a442018-10-20 21:33:41 -0700152 // This is handled above too, and set for individual commands later
153 "DIST_DIR",
154
Dan Willemsen68a09852017-04-18 13:56:57 -0700155 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000156 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700157 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700158 "DISPLAY",
159 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700160 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700161 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700162
163 // Drop make flags
164 "MAKEFLAGS",
165 "MAKELEVEL",
166 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700167
168 // Set in envsetup.sh, reset in makefiles
169 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700170
171 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
172 "ANDROID_BUILD_TOP",
173 "ANDROID_HOST_OUT",
174 "ANDROID_PRODUCT_OUT",
175 "ANDROID_HOST_OUT_TESTCASES",
176 "ANDROID_TARGET_OUT_TESTCASES",
177 "ANDROID_TOOLCHAIN",
178 "ANDROID_TOOLCHAIN_2ND_ARCH",
179 "ANDROID_DEV_SCRIPTS",
180 "ANDROID_EMULATOR_PREBUILTS",
181 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700182
183 // Only set in multiproduct_kati after config generation
184 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700185 )
186
Kousik Kumarec478642020-09-21 13:39:24 -0400187 if ret.UseGoma() {
188 ctx.Println("Goma for Android is being deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
189 ctx.Println()
190 ctx.Println("See go/goma_android_exceptions for exceptions.")
191 ctx.Fatalln("USE_GOMA flag is no longer supported.")
192 }
193
194 if ret.ForceUseGoma() {
195 ret.environ.Set("USE_GOMA", "true")
196 }
197
Dan Willemsen1e704462016-08-21 15:17:17 -0700198 // Tell python not to spam the source tree with .pyc files.
199 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
200
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400201 tmpDir := absPath(ctx, ret.TempDir())
202 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800203
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700204 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
205 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
206 "llvm-binutils-stable/llvm-symbolizer")
207 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
208
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800209 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700210 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800211
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700212 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700213 ctx.Println("You are building in a directory whose absolute path contains a space character:")
214 ctx.Println()
215 ctx.Printf("%q\n", srcDir)
216 ctx.Println()
217 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700218 }
219
220 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700221 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
222 ctx.Println()
223 ctx.Printf("%q\n", outDir)
224 ctx.Println()
225 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700226 }
227
228 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700229 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
230 ctx.Println()
231 ctx.Printf("%q\n", distDir)
232 ctx.Println()
233 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700234 }
235
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700236 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000237 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
238 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100239 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700240 javaHome := func() string {
241 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
242 return override
243 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000244 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
245 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 +0100246 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000247 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700248 }()
249 absJavaHome := absPath(ctx, javaHome)
250
Dan Willemsened869522018-01-08 14:58:46 -0800251 ret.configureLocale(ctx)
252
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700253 newPath := []string{filepath.Join(absJavaHome, "bin")}
254 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
255 newPath = append(newPath, path)
256 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100257
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700258 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
259 ret.environ.Set("JAVA_HOME", absJavaHome)
260 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000261 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
262 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100263 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700264 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
265
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800266 outDir := ret.OutDir()
267 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800268 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800269 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800270 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800271 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800272 }
Colin Cross28f527c2019-11-26 16:19:04 -0800273
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800274 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
275
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400276 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400277 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400278 ret.environ.Set(k, v)
279 }
280 }
281
Patrice Arruda96850362020-08-11 20:41:11 +0000282 c := Config{ret}
283 storeConfigMetrics(ctx, c)
284 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700285}
286
Patrice Arruda13848222019-04-22 17:12:02 -0700287// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
288// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700289func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
290 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700291}
292
Patrice Arruda96850362020-08-11 20:41:11 +0000293// storeConfigMetrics selects a set of configuration information and store in
294// the metrics system for further analysis.
295func storeConfigMetrics(ctx Context, config Config) {
296 if ctx.Metrics == nil {
297 return
298 }
299
300 b := &smpb.BuildConfig{
Patrice Arrudac97d6dc2020-09-28 18:22:07 +0000301 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
302 UseGoma: proto.Bool(config.UseGoma()),
303 UseRbe: proto.Bool(config.UseRBE()),
Patrice Arruda96850362020-08-11 20:41:11 +0000304 }
305 ctx.Metrics.BuildConfig(b)
Patrice Arruda3edfd482020-10-13 23:58:41 +0000306
307 s := &smpb.SystemResourceInfo{
308 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
309 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
310 }
311 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000312}
313
Patrice Arruda13848222019-04-22 17:12:02 -0700314// getConfigArgs processes the command arguments based on the build action and creates a set of new
315// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700316func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700317 // The next block of code verifies that the current directory is the root directory of the source
318 // tree. It then finds the relative path of dir based on the root directory of the source tree
319 // and verify that dir is inside of the source tree.
320 checkTopDir(ctx)
321 topDir, err := os.Getwd()
322 if err != nil {
323 ctx.Fatalf("Error retrieving top directory: %v", err)
324 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700325 dir, err = filepath.EvalSymlinks(dir)
326 if err != nil {
327 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
328 }
Patrice Arruda13848222019-04-22 17:12:02 -0700329 dir, err = filepath.Abs(dir)
330 if err != nil {
331 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
332 }
333 relDir, err := filepath.Rel(topDir, dir)
334 if err != nil {
335 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
336 }
337 // If there are ".." in the path, it's not in the source tree.
338 if strings.Contains(relDir, "..") {
339 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
340 }
341
342 configArgs := args[:]
343
344 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
345 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
346 targetNamePrefix := "MODULES-IN-"
347 if inList("GET-INSTALL-PATH", configArgs) {
348 targetNamePrefix = "GET-INSTALL-PATH-IN-"
349 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
350 }
351
Patrice Arruda13848222019-04-22 17:12:02 -0700352 var targets []string
353
354 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700355 case BUILD_MODULES:
356 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700357 case BUILD_MODULES_IN_A_DIRECTORY:
358 // If dir is the root source tree, all the modules are built of the source tree are built so
359 // no need to find the build file.
360 if topDir == dir {
361 break
362 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700363
Patrice Arruda13848222019-04-22 17:12:02 -0700364 buildFile := findBuildFile(ctx, relDir)
365 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700366 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700367 }
Patrice Arruda13848222019-04-22 17:12:02 -0700368 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
369 case BUILD_MODULES_IN_DIRECTORIES:
370 newConfigArgs, dirs := splitArgs(configArgs)
371 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700372 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700373 }
374
375 // Tidy only override all other specified targets.
376 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
377 if tidyOnly == "true" || tidyOnly == "1" {
378 configArgs = append(configArgs, "tidy_only")
379 } else {
380 configArgs = append(configArgs, targets...)
381 }
382
383 return configArgs
384}
385
386// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
387func convertToTarget(dir string, targetNamePrefix string) string {
388 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
389}
390
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700391// hasBuildFile returns true if dir contains an Android build file.
392func hasBuildFile(ctx Context, dir string) bool {
393 for _, buildFile := range buildFiles {
394 _, err := os.Stat(filepath.Join(dir, buildFile))
395 if err == nil {
396 return true
397 }
398 if !os.IsNotExist(err) {
399 ctx.Fatalf("Error retrieving the build file stats: %v", err)
400 }
401 }
402 return false
403}
404
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700405// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
406// in the current and any sub directory of dir. If a build file is not found, traverse the path
407// up by one directory and repeat again until either a build file is found or reached to the root
408// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
409// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700410func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700411 // If the string is empty or ".", assume it is top directory of the source tree.
412 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700413 return ""
414 }
415
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700416 found := false
417 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
418 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
419 if err != nil {
420 return err
421 }
422 if found {
423 return filepath.SkipDir
424 }
425 if info.IsDir() {
426 return nil
427 }
428 for _, buildFile := range buildFiles {
429 if info.Name() == buildFile {
430 found = true
431 return filepath.SkipDir
432 }
433 }
434 return nil
435 })
436 if err != nil {
437 ctx.Fatalf("Error finding Android build file: %v", err)
438 }
439
440 if found {
441 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700442 }
443 }
444
445 return ""
446}
447
448// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
449func splitArgs(args []string) (newArgs []string, dirs []string) {
450 specialArgs := map[string]bool{
451 "showcommands": true,
452 "snod": true,
453 "dist": true,
454 "checkbuild": true,
455 }
456
457 newArgs = []string{}
458 dirs = []string{}
459
460 for _, arg := range args {
461 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
462 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
463 newArgs = append(newArgs, arg)
464 continue
465 }
466
467 if _, ok := specialArgs[arg]; ok {
468 newArgs = append(newArgs, arg)
469 continue
470 }
471
472 dirs = append(dirs, arg)
473 }
474
475 return newArgs, dirs
476}
477
478// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
479// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
480// source root tree where the build action command was invoked. Each directory is validated if the
481// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700482func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700483 for _, dir := range dirs {
484 // The directory may have specified specific modules to build. ":" is the separator to separate
485 // the directory and the list of modules.
486 s := strings.Split(dir, ":")
487 l := len(s)
488 if l > 2 { // more than one ":" was specified.
489 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
490 }
491
492 dir = filepath.Join(relDir, s[0])
493 if _, err := os.Stat(dir); err != nil {
494 ctx.Fatalf("couldn't find directory %s", dir)
495 }
496
497 // Verify that if there are any targets specified after ":". Each target is separated by ",".
498 var newTargets []string
499 if l == 2 && s[1] != "" {
500 newTargets = strings.Split(s[1], ",")
501 if inList("", newTargets) {
502 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
503 }
504 }
505
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700506 // If there are specified targets to build in dir, an android build file must exist for the one
507 // shot build. For the non-targets case, find the appropriate build file and build all the
508 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700509 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700510 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700511 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
512 }
513 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700514 buildFile := findBuildFile(ctx, dir)
515 if buildFile == "" {
516 ctx.Fatalf("Build file not found for %s directory", dir)
517 }
518 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700519 }
520
Patrice Arruda13848222019-04-22 17:12:02 -0700521 targets = append(targets, newTargets...)
522 }
523
Dan Willemsence41e942019-07-29 23:39:30 -0700524 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700525}
526
Dan Willemsen9b587492017-07-10 22:13:00 -0700527func (c *configImpl) parseArgs(ctx Context, args []string) {
528 for i := 0; i < len(args); i++ {
529 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700530 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700531 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700532 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700533 } else if arg == "--skip-make" {
534 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700535 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700536 parseArgNum := func(def int) int {
537 if len(arg) > 2 {
538 p, err := strconv.ParseUint(arg[2:], 10, 31)
539 if err != nil {
540 ctx.Fatalf("Failed to parse %q: %v", arg, err)
541 }
542 return int(p)
543 } else if i+1 < len(args) {
544 p, err := strconv.ParseUint(args[i+1], 10, 31)
545 if err == nil {
546 i++
547 return int(p)
548 }
549 }
550 return def
551 }
552
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700553 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700554 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700555 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700556 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700557 } else {
558 ctx.Fatalln("Unknown option:", arg)
559 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700560 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700561 if k == "OUT_DIR" {
562 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
563 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700564 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700565 } else if arg == "dist" {
566 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700567 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700568 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800569 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700570 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700571 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700572 }
573 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700574}
575
Dan Willemsened869522018-01-08 14:58:46 -0800576func (c *configImpl) configureLocale(ctx Context) {
577 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
578 output, err := cmd.Output()
579
580 var locales []string
581 if err == nil {
582 locales = strings.Split(string(output), "\n")
583 } else {
584 // If we're unable to list the locales, let's assume en_US.UTF-8
585 locales = []string{"en_US.UTF-8"}
586 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
587 }
588
589 // gettext uses LANGUAGE, which is passed directly through
590
591 // For LANG and LC_*, only preserve the evaluated version of
592 // LC_MESSAGES
593 user_lang := ""
594 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
595 user_lang = lc_all
596 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
597 user_lang = lc_messages
598 } else if lang, ok := c.environ.Get("LANG"); ok {
599 user_lang = lang
600 }
601
602 c.environ.UnsetWithPrefix("LC_")
603
604 if user_lang != "" {
605 c.environ.Set("LC_MESSAGES", user_lang)
606 }
607
608 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
609 // for others)
610 if inList("C.UTF-8", locales) {
611 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500612 } else if inList("C.utf8", locales) {
613 // These normalize to the same thing
614 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800615 } else if inList("en_US.UTF-8", locales) {
616 c.environ.Set("LANG", "en_US.UTF-8")
617 } else if inList("en_US.utf8", locales) {
618 // These normalize to the same thing
619 c.environ.Set("LANG", "en_US.UTF-8")
620 } else {
621 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
622 }
623}
624
Dan Willemsen1e704462016-08-21 15:17:17 -0700625// Lunch configures the environment for a specific product similarly to the
626// `lunch` bash function.
627func (c *configImpl) Lunch(ctx Context, product, variant string) {
628 if variant != "eng" && variant != "userdebug" && variant != "user" {
629 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
630 }
631
632 c.environ.Set("TARGET_PRODUCT", product)
633 c.environ.Set("TARGET_BUILD_VARIANT", variant)
634 c.environ.Set("TARGET_BUILD_TYPE", "release")
635 c.environ.Unset("TARGET_BUILD_APPS")
Martin Stjernholm08802332020-06-04 17:00:01 +0100636 c.environ.Unset("TARGET_BUILD_UNBUNDLED")
Dan Willemsen1e704462016-08-21 15:17:17 -0700637}
638
639// Tapas configures the environment to build one or more unbundled apps,
640// similarly to the `tapas` bash function.
641func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
642 if len(apps) == 0 {
643 apps = []string{"all"}
644 }
645 if variant == "" {
646 variant = "eng"
647 }
648
649 if variant != "eng" && variant != "userdebug" && variant != "user" {
650 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
651 }
652
653 var product string
654 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700655 case "arm", "":
656 product = "aosp_arm"
657 case "arm64":
658 product = "aosm_arm64"
Dan Willemsen1e704462016-08-21 15:17:17 -0700659 case "x86":
660 product = "aosp_x86"
661 case "x86_64":
662 product = "aosp_x86_64"
663 default:
664 ctx.Fatalf("Invalid architecture: %q", arch)
665 }
666
667 c.environ.Set("TARGET_PRODUCT", product)
668 c.environ.Set("TARGET_BUILD_VARIANT", variant)
669 c.environ.Set("TARGET_BUILD_TYPE", "release")
670 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
671}
672
673func (c *configImpl) Environment() *Environment {
674 return c.environ
675}
676
677func (c *configImpl) Arguments() []string {
678 return c.arguments
679}
680
681func (c *configImpl) OutDir() string {
682 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700683 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700684 }
685 return "out"
686}
687
Dan Willemsen8a073a82017-02-04 17:30:44 -0800688func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700689 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800690}
691
Dan Willemsen1e704462016-08-21 15:17:17 -0700692func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700693 if c.skipMake {
694 return c.arguments
695 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700696 return c.ninjaArgs
697}
698
699func (c *configImpl) SoongOutDir() string {
700 return filepath.Join(c.OutDir(), "soong")
701}
702
Jeff Gastonefc1b412017-03-29 17:29:06 -0700703func (c *configImpl) TempDir() string {
704 return shared.TempDirForOutDir(c.SoongOutDir())
705}
706
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700707func (c *configImpl) FileListDir() string {
708 return filepath.Join(c.OutDir(), ".module_paths")
709}
710
Dan Willemsen1e704462016-08-21 15:17:17 -0700711func (c *configImpl) KatiSuffix() string {
712 if c.katiSuffix != "" {
713 return c.katiSuffix
714 }
715 panic("SetKatiSuffix has not been called")
716}
717
Colin Cross37193492017-11-16 17:55:00 -0800718// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
719// user is interested in additional checks at the expense of build time.
720func (c *configImpl) Checkbuild() bool {
721 return c.checkbuild
722}
723
Dan Willemsen8a073a82017-02-04 17:30:44 -0800724func (c *configImpl) Dist() bool {
725 return c.dist
726}
727
Dan Willemsen1e704462016-08-21 15:17:17 -0700728func (c *configImpl) IsVerbose() bool {
729 return c.verbose
730}
731
Dan Willemsene0879fc2017-08-04 15:06:27 -0700732func (c *configImpl) SkipMake() bool {
733 return c.skipMake
734}
735
Dan Willemsen1e704462016-08-21 15:17:17 -0700736func (c *configImpl) TargetProduct() string {
737 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
738 return v
739 }
740 panic("TARGET_PRODUCT is not defined")
741}
742
Dan Willemsen02781d52017-05-12 19:28:13 -0700743func (c *configImpl) TargetDevice() string {
744 return c.targetDevice
745}
746
747func (c *configImpl) SetTargetDevice(device string) {
748 c.targetDevice = device
749}
750
751func (c *configImpl) TargetBuildVariant() string {
752 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
753 return v
754 }
755 panic("TARGET_BUILD_VARIANT is not defined")
756}
757
Dan Willemsen1e704462016-08-21 15:17:17 -0700758func (c *configImpl) KatiArgs() []string {
759 return c.katiArgs
760}
761
762func (c *configImpl) Parallel() int {
763 return c.parallel
764}
765
Colin Cross8b8bec32019-11-15 13:18:43 -0800766func (c *configImpl) HighmemParallel() int {
767 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
768 return i
769 }
770
771 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
772 parallel := c.Parallel()
773 if c.UseRemoteBuild() {
774 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
775 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
776 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
777 // Return 1/16th of the size of the local pool, rounding up.
778 return (parallel + 15) / 16
779 } else if c.totalRAM == 0 {
780 // Couldn't detect the total RAM, don't restrict highmem processes.
781 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -0700782 } else if c.totalRAM <= 16*1024*1024*1024 {
783 // Less than 16GB of ram, restrict to 1 highmem processes
784 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -0800785 } else if c.totalRAM <= 32*1024*1024*1024 {
786 // Less than 32GB of ram, restrict to 2 highmem processes
787 return 2
788 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
789 // If less than 8GB total RAM per process, reduce the number of highmem processes
790 return p
791 }
792 // No restriction on highmem processes
793 return parallel
794}
795
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800796func (c *configImpl) TotalRAM() uint64 {
797 return c.totalRAM
798}
799
Kousik Kumarec478642020-09-21 13:39:24 -0400800// ForceUseGoma determines whether we should override Goma deprecation
801// and use Goma for the current build or not.
802func (c *configImpl) ForceUseGoma() bool {
803 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
804 v = strings.TrimSpace(v)
805 if v != "" && v != "false" {
806 return true
807 }
808 }
809 return false
810}
811
Dan Willemsen1e704462016-08-21 15:17:17 -0700812func (c *configImpl) UseGoma() bool {
813 if v, ok := c.environ.Get("USE_GOMA"); ok {
814 v = strings.TrimSpace(v)
815 if v != "" && v != "false" {
816 return true
817 }
818 }
819 return false
820}
821
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900822func (c *configImpl) StartGoma() bool {
823 if !c.UseGoma() {
824 return false
825 }
826
827 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
828 v = strings.TrimSpace(v)
829 if v != "" && v != "false" {
830 return false
831 }
832 }
833 return true
834}
835
Ramy Medhatbbf25672019-07-17 12:30:04 +0000836func (c *configImpl) UseRBE() bool {
837 if v, ok := c.environ.Get("USE_RBE"); ok {
838 v = strings.TrimSpace(v)
839 if v != "" && v != "false" {
840 return true
841 }
842 }
843 return false
844}
845
846func (c *configImpl) StartRBE() bool {
847 if !c.UseRBE() {
848 return false
849 }
850
851 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
852 v = strings.TrimSpace(v)
853 if v != "" && v != "false" {
854 return false
855 }
856 }
857 return true
858}
859
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400860func (c *configImpl) logDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -0400861 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
862 if v, ok := c.environ.Get(f); ok {
863 return v
864 }
865 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400866 if c.Dist() {
867 return filepath.Join(c.DistDir(), "logs")
868 }
869 return c.OutDir()
870}
871
872func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000873 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
874 if v, ok := c.environ.Get(f); ok {
875 return v
876 }
877 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400878 return c.logDir()
879}
880
881func (c *configImpl) rbeLogPath() string {
882 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
883 if v, ok := c.environ.Get(f); ok {
884 return v
885 }
886 }
887 return fmt.Sprintf("text://%v/reproxy_log.txt", c.logDir())
888}
889
890func (c *configImpl) rbeExecRoot() string {
891 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
892 if v, ok := c.environ.Get(f); ok {
893 return v
894 }
895 }
896 wd, err := os.Getwd()
897 if err != nil {
898 return ""
899 }
900 return wd
901}
902
903func (c *configImpl) rbeDir() string {
904 if v, ok := c.environ.Get("RBE_DIR"); ok {
905 return v
906 }
907 return "prebuilts/remoteexecution-client/live/"
908}
909
910func (c *configImpl) rbeReproxy() string {
911 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
912 if v, ok := c.environ.Get(f); ok {
913 return v
914 }
915 }
916 return filepath.Join(c.rbeDir(), "reproxy")
917}
918
919func (c *configImpl) rbeAuth() (string, string) {
920 credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"}
921 for _, cf := range credFlags {
922 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
923 if v, ok := c.environ.Get(f); ok {
924 v = strings.TrimSpace(v)
925 if v != "" && v != "false" && v != "0" {
926 return "RBE_" + cf, v
927 }
928 }
929 }
930 }
931 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000932}
933
Colin Cross9016b912019-11-11 14:57:42 -0800934func (c *configImpl) UseRemoteBuild() bool {
935 return c.UseGoma() || c.UseRBE()
936}
937
Dan Willemsen1e704462016-08-21 15:17:17 -0700938// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700939// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700940// still limited by Parallel()
941func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -0800942 if !c.UseRemoteBuild() {
943 return 0
944 }
945 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
946 return i
Dan Willemsen1e704462016-08-21 15:17:17 -0700947 }
948 return 500
949}
950
951func (c *configImpl) SetKatiArgs(args []string) {
952 c.katiArgs = args
953}
954
955func (c *configImpl) SetNinjaArgs(args []string) {
956 c.ninjaArgs = args
957}
958
959func (c *configImpl) SetKatiSuffix(suffix string) {
960 c.katiSuffix = suffix
961}
962
Dan Willemsene0879fc2017-08-04 15:06:27 -0700963func (c *configImpl) LastKatiSuffixFile() string {
964 return filepath.Join(c.OutDir(), "last_kati_suffix")
965}
966
967func (c *configImpl) HasKatiSuffix() bool {
968 return c.katiSuffix != ""
969}
970
Dan Willemsen1e704462016-08-21 15:17:17 -0700971func (c *configImpl) KatiEnvFile() string {
972 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
973}
974
Dan Willemsen29971232018-09-26 14:58:30 -0700975func (c *configImpl) KatiBuildNinjaFile() string {
976 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700977}
978
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700979func (c *configImpl) KatiPackageNinjaFile() string {
980 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
981}
982
Dan Willemsen1e704462016-08-21 15:17:17 -0700983func (c *configImpl) SoongNinjaFile() string {
984 return filepath.Join(c.SoongOutDir(), "build.ninja")
985}
986
987func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700988 if c.katiSuffix == "" {
989 return filepath.Join(c.OutDir(), "combined.ninja")
990 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700991 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
992}
993
994func (c *configImpl) SoongAndroidMk() string {
995 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
996}
997
998func (c *configImpl) SoongMakeVarsMk() string {
999 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1000}
1001
Dan Willemsenf052f782017-05-18 15:29:04 -07001002func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001003 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001004}
1005
Dan Willemsen02781d52017-05-12 19:28:13 -07001006func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001007 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1008}
1009
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001010func (c *configImpl) KatiPackageMkDir() string {
1011 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1012}
1013
Dan Willemsenf052f782017-05-18 15:29:04 -07001014func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001015 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001016}
1017
1018func (c *configImpl) HostOut() string {
1019 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1020}
1021
1022// This probably needs to be multi-valued, so not exporting it for now
1023func (c *configImpl) hostCrossOut() string {
1024 if runtime.GOOS == "linux" {
1025 return filepath.Join(c.hostOutRoot(), "windows-x86")
1026 } else {
1027 return ""
1028 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001029}
1030
Dan Willemsen1e704462016-08-21 15:17:17 -07001031func (c *configImpl) HostPrebuiltTag() string {
1032 if runtime.GOOS == "linux" {
1033 return "linux-x86"
1034 } else if runtime.GOOS == "darwin" {
1035 return "darwin-x86"
1036 } else {
1037 panic("Unsupported OS")
1038 }
1039}
Dan Willemsenf173d592017-04-27 14:28:00 -07001040
Dan Willemsen8122bd52017-10-12 20:20:41 -07001041func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001042 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1043 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001044 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1045 if _, err := os.Stat(asan); err == nil {
1046 return asan
1047 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001048 }
1049 }
1050 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1051}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001052
1053func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1054 c.brokenDupRules = val
1055}
1056
1057func (c *configImpl) BuildBrokenDupRules() bool {
1058 return c.brokenDupRules
1059}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001060
Dan Willemsen25e6f092019-04-09 10:22:43 -07001061func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1062 c.brokenUsesNetwork = val
1063}
1064
1065func (c *configImpl) BuildBrokenUsesNetwork() bool {
1066 return c.brokenUsesNetwork
1067}
1068
Dan Willemsene3336352020-01-02 19:10:38 -08001069func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1070 c.brokenNinjaEnvVars = val
1071}
1072
1073func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1074 return c.brokenNinjaEnvVars
1075}
1076
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001077func (c *configImpl) SetTargetDeviceDir(dir string) {
1078 c.targetDeviceDir = dir
1079}
1080
1081func (c *configImpl) TargetDeviceDir() string {
1082 return c.targetDeviceDir
1083}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001084
Patrice Arruda219eef32020-06-01 17:29:30 +00001085func (c *configImpl) BuildDateTime() string {
1086 return c.buildDateTime
1087}
1088
1089func (c *configImpl) MetricsUploaderApp() string {
1090 if p, ok := c.environ.Get("ANDROID_ENABLE_METRICS_UPLOAD"); ok {
1091 return p
1092 }
1093 return ""
1094}