blob: e57c7303c5cc663b9eabf5f21c5506c8e5d0e5e5 [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
49 skipMake bool
50 skipSoongTests bool
Dan Willemsen1e704462016-08-21 15:17:17 -070051
52 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070053 katiArgs []string
54 ninjaArgs []string
55 katiSuffix string
56 targetDevice string
57 targetDeviceDir string
Dan Willemsen3d60b112018-04-04 22:25:56 -070058
Dan Willemsen2bb82d02019-12-27 09:35:42 -080059 // Autodetected
60 totalRAM uint64
61
Dan Willemsene3336352020-01-02 19:10:38 -080062 brokenDupRules bool
63 brokenUsesNetwork bool
64 brokenNinjaEnvVars []string
Dan Willemsen18490112018-05-25 16:30:04 -070065
66 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070067}
68
Dan Willemsenc2af0be2017-01-20 14:10:01 -080069const srcDirFileCheck = "build/soong/root.bp"
70
Patrice Arruda9450d0b2019-07-08 11:06:46 -070071var buildFiles = []string{"Android.mk", "Android.bp"}
72
Patrice Arruda13848222019-04-22 17:12:02 -070073type BuildAction uint
74
75const (
76 // Builds all of the modules and their dependencies of a specified directory, relative to the root
77 // directory of the source tree.
78 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
79
80 // Builds all of the modules and their dependencies of a list of specified directories. All specified
81 // directories are relative to the root directory of the source tree.
82 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070083
84 // Build a list of specified modules. If none was specified, simply build the whole source tree.
85 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070086)
87
88// checkTopDir validates that the current directory is at the root directory of the source tree.
89func checkTopDir(ctx Context) {
90 if _, err := os.Stat(srcDirFileCheck); err != nil {
91 if os.IsNotExist(err) {
92 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
93 }
94 ctx.Fatalln("Error verifying tree state:", err)
95 }
96}
97
Dan Willemsen1e704462016-08-21 15:17:17 -070098func NewConfig(ctx Context, args ...string) Config {
99 ret := &configImpl{
100 environ: OsEnvironment(),
101 }
102
Patrice Arruda90109172020-07-28 18:07:27 +0000103 // Default matching ninja
Dan Willemsen9b587492017-07-10 22:13:00 -0700104 ret.parallel = runtime.NumCPU() + 2
105 ret.keepGoing = 1
106
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800107 ret.totalRAM = detectTotalRAM(ctx)
108
Dan Willemsen9b587492017-07-10 22:13:00 -0700109 ret.parseArgs(ctx, args)
110
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800111 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700112 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
113 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
114 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800115 outDir := "out"
116 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
117 if wd, err := os.Getwd(); err != nil {
118 ctx.Fatalln("Failed to get working directory:", err)
119 } else {
120 outDir = filepath.Join(baseDir, filepath.Base(wd))
121 }
122 }
123 ret.environ.Set("OUT_DIR", outDir)
124 }
125
Dan Willemsen2d31a442018-10-20 21:33:41 -0700126 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
127 ret.distDir = filepath.Clean(distDir)
128 } else {
129 ret.distDir = filepath.Join(ret.OutDir(), "dist")
130 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700131
Dan Willemsen1e704462016-08-21 15:17:17 -0700132 ret.environ.Unset(
133 // We're already using it
134 "USE_SOONG_UI",
135
136 // We should never use GOROOT/GOPATH from the shell environment
137 "GOROOT",
138 "GOPATH",
139
140 // These should only come from Soong, not the environment.
141 "CLANG",
142 "CLANG_CXX",
143 "CCC_CC",
144 "CCC_CXX",
145
146 // Used by the goma compiler wrapper, but should only be set by
147 // gomacc
148 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800149
150 // We handle this above
151 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700152
Dan Willemsen2d31a442018-10-20 21:33:41 -0700153 // This is handled above too, and set for individual commands later
154 "DIST_DIR",
155
Dan Willemsen68a09852017-04-18 13:56:57 -0700156 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000157 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700158 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700159 "DISPLAY",
160 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700161 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700162 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700163
164 // Drop make flags
165 "MAKEFLAGS",
166 "MAKELEVEL",
167 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700168
169 // Set in envsetup.sh, reset in makefiles
170 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700171
172 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
173 "ANDROID_BUILD_TOP",
174 "ANDROID_HOST_OUT",
175 "ANDROID_PRODUCT_OUT",
176 "ANDROID_HOST_OUT_TESTCASES",
177 "ANDROID_TARGET_OUT_TESTCASES",
178 "ANDROID_TOOLCHAIN",
179 "ANDROID_TOOLCHAIN_2ND_ARCH",
180 "ANDROID_DEV_SCRIPTS",
181 "ANDROID_EMULATOR_PREBUILTS",
182 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700183
184 // Only set in multiproduct_kati after config generation
185 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700186 )
187
Kousik Kumarb328f6d2020-10-19 01:45:46 -0400188 if ret.UseGoma() || ret.ForceUseGoma() {
189 ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.")
190 ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.")
Kousik Kumarec478642020-09-21 13:39:24 -0400191 }
192
Dan Willemsen1e704462016-08-21 15:17:17 -0700193 // Tell python not to spam the source tree with .pyc files.
194 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
195
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400196 tmpDir := absPath(ctx, ret.TempDir())
197 ret.environ.Set("TMPDIR", tmpDir)
Dan Willemsen32a669b2018-03-08 19:42:00 -0800198
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700199 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
200 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
201 "llvm-binutils-stable/llvm-symbolizer")
202 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
203
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800204 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700205 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800206
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700207 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700208 ctx.Println("You are building in a directory whose absolute path contains a space character:")
209 ctx.Println()
210 ctx.Printf("%q\n", srcDir)
211 ctx.Println()
212 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700213 }
214
215 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700216 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
217 ctx.Println()
218 ctx.Printf("%q\n", outDir)
219 ctx.Println()
220 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700221 }
222
223 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700224 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
225 ctx.Println()
226 ctx.Printf("%q\n", distDir)
227 ctx.Println()
228 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700229 }
230
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700231 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000232 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
233 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100234 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700235 javaHome := func() string {
236 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
237 return override
238 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000239 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
240 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 +0100241 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000242 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700243 }()
244 absJavaHome := absPath(ctx, javaHome)
245
Dan Willemsened869522018-01-08 14:58:46 -0800246 ret.configureLocale(ctx)
247
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700248 newPath := []string{filepath.Join(absJavaHome, "bin")}
249 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
250 newPath = append(newPath, path)
251 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100252
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700253 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
254 ret.environ.Set("JAVA_HOME", absJavaHome)
255 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000256 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
257 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100258 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700259 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
260
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800261 outDir := ret.OutDir()
262 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800263 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800264 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800265 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800266 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800267 }
Colin Cross28f527c2019-11-26 16:19:04 -0800268
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800269 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
270
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400271 if ret.UseRBE() {
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400272 for k, v := range getRBEVars(ctx, Config{ret}) {
Ramy Medhatca1e44c2020-07-16 12:18:37 -0400273 ret.environ.Set(k, v)
274 }
275 }
276
Patrice Arruda96850362020-08-11 20:41:11 +0000277 c := Config{ret}
278 storeConfigMetrics(ctx, c)
279 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700280}
281
Patrice Arruda13848222019-04-22 17:12:02 -0700282// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
283// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700284func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
285 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700286}
287
Patrice Arruda96850362020-08-11 20:41:11 +0000288// storeConfigMetrics selects a set of configuration information and store in
289// the metrics system for further analysis.
290func storeConfigMetrics(ctx Context, config Config) {
291 if ctx.Metrics == nil {
292 return
293 }
294
295 b := &smpb.BuildConfig{
Patrice Arrudac97d6dc2020-09-28 18:22:07 +0000296 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
297 UseGoma: proto.Bool(config.UseGoma()),
298 UseRbe: proto.Bool(config.UseRBE()),
Patrice Arruda96850362020-08-11 20:41:11 +0000299 }
300 ctx.Metrics.BuildConfig(b)
Patrice Arruda3edfd482020-10-13 23:58:41 +0000301
302 s := &smpb.SystemResourceInfo{
303 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
304 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
305 }
306 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000307}
308
Patrice Arruda13848222019-04-22 17:12:02 -0700309// getConfigArgs processes the command arguments based on the build action and creates a set of new
310// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700311func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700312 // The next block of code verifies that the current directory is the root directory of the source
313 // tree. It then finds the relative path of dir based on the root directory of the source tree
314 // and verify that dir is inside of the source tree.
315 checkTopDir(ctx)
316 topDir, err := os.Getwd()
317 if err != nil {
318 ctx.Fatalf("Error retrieving top directory: %v", err)
319 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700320 dir, err = filepath.EvalSymlinks(dir)
321 if err != nil {
322 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
323 }
Patrice Arruda13848222019-04-22 17:12:02 -0700324 dir, err = filepath.Abs(dir)
325 if err != nil {
326 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
327 }
328 relDir, err := filepath.Rel(topDir, dir)
329 if err != nil {
330 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
331 }
332 // If there are ".." in the path, it's not in the source tree.
333 if strings.Contains(relDir, "..") {
334 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
335 }
336
337 configArgs := args[:]
338
339 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
340 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
341 targetNamePrefix := "MODULES-IN-"
342 if inList("GET-INSTALL-PATH", configArgs) {
343 targetNamePrefix = "GET-INSTALL-PATH-IN-"
344 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
345 }
346
Patrice Arruda13848222019-04-22 17:12:02 -0700347 var targets []string
348
349 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700350 case BUILD_MODULES:
351 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700352 case BUILD_MODULES_IN_A_DIRECTORY:
353 // If dir is the root source tree, all the modules are built of the source tree are built so
354 // no need to find the build file.
355 if topDir == dir {
356 break
357 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700358
Patrice Arruda13848222019-04-22 17:12:02 -0700359 buildFile := findBuildFile(ctx, relDir)
360 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700361 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700362 }
Patrice Arruda13848222019-04-22 17:12:02 -0700363 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
364 case BUILD_MODULES_IN_DIRECTORIES:
365 newConfigArgs, dirs := splitArgs(configArgs)
366 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700367 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700368 }
369
370 // Tidy only override all other specified targets.
371 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
372 if tidyOnly == "true" || tidyOnly == "1" {
373 configArgs = append(configArgs, "tidy_only")
374 } else {
375 configArgs = append(configArgs, targets...)
376 }
377
378 return configArgs
379}
380
381// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
382func convertToTarget(dir string, targetNamePrefix string) string {
383 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
384}
385
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700386// hasBuildFile returns true if dir contains an Android build file.
387func hasBuildFile(ctx Context, dir string) bool {
388 for _, buildFile := range buildFiles {
389 _, err := os.Stat(filepath.Join(dir, buildFile))
390 if err == nil {
391 return true
392 }
393 if !os.IsNotExist(err) {
394 ctx.Fatalf("Error retrieving the build file stats: %v", err)
395 }
396 }
397 return false
398}
399
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700400// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
401// in the current and any sub directory of dir. If a build file is not found, traverse the path
402// up by one directory and repeat again until either a build file is found or reached to the root
403// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
404// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700405func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700406 // If the string is empty or ".", assume it is top directory of the source tree.
407 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700408 return ""
409 }
410
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700411 found := false
412 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
413 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
414 if err != nil {
415 return err
416 }
417 if found {
418 return filepath.SkipDir
419 }
420 if info.IsDir() {
421 return nil
422 }
423 for _, buildFile := range buildFiles {
424 if info.Name() == buildFile {
425 found = true
426 return filepath.SkipDir
427 }
428 }
429 return nil
430 })
431 if err != nil {
432 ctx.Fatalf("Error finding Android build file: %v", err)
433 }
434
435 if found {
436 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700437 }
438 }
439
440 return ""
441}
442
443// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
444func splitArgs(args []string) (newArgs []string, dirs []string) {
445 specialArgs := map[string]bool{
446 "showcommands": true,
447 "snod": true,
448 "dist": true,
449 "checkbuild": true,
450 }
451
452 newArgs = []string{}
453 dirs = []string{}
454
455 for _, arg := range args {
456 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
457 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
458 newArgs = append(newArgs, arg)
459 continue
460 }
461
462 if _, ok := specialArgs[arg]; ok {
463 newArgs = append(newArgs, arg)
464 continue
465 }
466
467 dirs = append(dirs, arg)
468 }
469
470 return newArgs, dirs
471}
472
473// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
474// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
475// source root tree where the build action command was invoked. Each directory is validated if the
476// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700477func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700478 for _, dir := range dirs {
479 // The directory may have specified specific modules to build. ":" is the separator to separate
480 // the directory and the list of modules.
481 s := strings.Split(dir, ":")
482 l := len(s)
483 if l > 2 { // more than one ":" was specified.
484 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
485 }
486
487 dir = filepath.Join(relDir, s[0])
488 if _, err := os.Stat(dir); err != nil {
489 ctx.Fatalf("couldn't find directory %s", dir)
490 }
491
492 // Verify that if there are any targets specified after ":". Each target is separated by ",".
493 var newTargets []string
494 if l == 2 && s[1] != "" {
495 newTargets = strings.Split(s[1], ",")
496 if inList("", newTargets) {
497 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
498 }
499 }
500
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700501 // If there are specified targets to build in dir, an android build file must exist for the one
502 // shot build. For the non-targets case, find the appropriate build file and build all the
503 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700504 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700505 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700506 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
507 }
508 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700509 buildFile := findBuildFile(ctx, dir)
510 if buildFile == "" {
511 ctx.Fatalf("Build file not found for %s directory", dir)
512 }
513 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700514 }
515
Patrice Arruda13848222019-04-22 17:12:02 -0700516 targets = append(targets, newTargets...)
517 }
518
Dan Willemsence41e942019-07-29 23:39:30 -0700519 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700520}
521
Dan Willemsen9b587492017-07-10 22:13:00 -0700522func (c *configImpl) parseArgs(ctx Context, args []string) {
523 for i := 0; i < len(args); i++ {
524 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700525 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700526 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700527 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700528 } else if arg == "--skip-make" {
529 c.skipMake = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700530 } else if arg == "--skip-soong-tests" {
531 c.skipSoongTests = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700532 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700533 parseArgNum := func(def int) int {
534 if len(arg) > 2 {
535 p, err := strconv.ParseUint(arg[2:], 10, 31)
536 if err != nil {
537 ctx.Fatalf("Failed to parse %q: %v", arg, err)
538 }
539 return int(p)
540 } else if i+1 < len(args) {
541 p, err := strconv.ParseUint(args[i+1], 10, 31)
542 if err == nil {
543 i++
544 return int(p)
545 }
546 }
547 return def
548 }
549
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700550 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700551 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700552 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700553 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700554 } else {
555 ctx.Fatalln("Unknown option:", arg)
556 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700557 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700558 if k == "OUT_DIR" {
559 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
560 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700561 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700562 } else if arg == "dist" {
563 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700564 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700565 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800566 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700567 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700568 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700569 }
570 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700571}
572
Dan Willemsened869522018-01-08 14:58:46 -0800573func (c *configImpl) configureLocale(ctx Context) {
574 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
575 output, err := cmd.Output()
576
577 var locales []string
578 if err == nil {
579 locales = strings.Split(string(output), "\n")
580 } else {
581 // If we're unable to list the locales, let's assume en_US.UTF-8
582 locales = []string{"en_US.UTF-8"}
583 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
584 }
585
586 // gettext uses LANGUAGE, which is passed directly through
587
588 // For LANG and LC_*, only preserve the evaluated version of
589 // LC_MESSAGES
590 user_lang := ""
591 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
592 user_lang = lc_all
593 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
594 user_lang = lc_messages
595 } else if lang, ok := c.environ.Get("LANG"); ok {
596 user_lang = lang
597 }
598
599 c.environ.UnsetWithPrefix("LC_")
600
601 if user_lang != "" {
602 c.environ.Set("LC_MESSAGES", user_lang)
603 }
604
605 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
606 // for others)
607 if inList("C.UTF-8", locales) {
608 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500609 } else if inList("C.utf8", locales) {
610 // These normalize to the same thing
611 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800612 } else if inList("en_US.UTF-8", locales) {
613 c.environ.Set("LANG", "en_US.UTF-8")
614 } else if inList("en_US.utf8", locales) {
615 // These normalize to the same thing
616 c.environ.Set("LANG", "en_US.UTF-8")
617 } else {
618 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
619 }
620}
621
Dan Willemsen1e704462016-08-21 15:17:17 -0700622// Lunch configures the environment for a specific product similarly to the
623// `lunch` bash function.
624func (c *configImpl) Lunch(ctx Context, product, variant string) {
625 if variant != "eng" && variant != "userdebug" && variant != "user" {
626 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
627 }
628
629 c.environ.Set("TARGET_PRODUCT", product)
630 c.environ.Set("TARGET_BUILD_VARIANT", variant)
631 c.environ.Set("TARGET_BUILD_TYPE", "release")
632 c.environ.Unset("TARGET_BUILD_APPS")
Martin Stjernholm08802332020-06-04 17:00:01 +0100633 c.environ.Unset("TARGET_BUILD_UNBUNDLED")
Dan Willemsen1e704462016-08-21 15:17:17 -0700634}
635
636// Tapas configures the environment to build one or more unbundled apps,
637// similarly to the `tapas` bash function.
638func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
639 if len(apps) == 0 {
640 apps = []string{"all"}
641 }
642 if variant == "" {
643 variant = "eng"
644 }
645
646 if variant != "eng" && variant != "userdebug" && variant != "user" {
647 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
648 }
649
650 var product string
651 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700652 case "arm", "":
653 product = "aosp_arm"
654 case "arm64":
655 product = "aosm_arm64"
Dan Willemsen1e704462016-08-21 15:17:17 -0700656 case "x86":
657 product = "aosp_x86"
658 case "x86_64":
659 product = "aosp_x86_64"
660 default:
661 ctx.Fatalf("Invalid architecture: %q", arch)
662 }
663
664 c.environ.Set("TARGET_PRODUCT", product)
665 c.environ.Set("TARGET_BUILD_VARIANT", variant)
666 c.environ.Set("TARGET_BUILD_TYPE", "release")
667 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
668}
669
670func (c *configImpl) Environment() *Environment {
671 return c.environ
672}
673
674func (c *configImpl) Arguments() []string {
675 return c.arguments
676}
677
678func (c *configImpl) OutDir() string {
679 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700680 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700681 }
682 return "out"
683}
684
Dan Willemsen8a073a82017-02-04 17:30:44 -0800685func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700686 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800687}
688
Dan Willemsen1e704462016-08-21 15:17:17 -0700689func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700690 if c.skipMake {
691 return c.arguments
692 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700693 return c.ninjaArgs
694}
695
696func (c *configImpl) SoongOutDir() string {
697 return filepath.Join(c.OutDir(), "soong")
698}
699
Jeff Gastonefc1b412017-03-29 17:29:06 -0700700func (c *configImpl) TempDir() string {
701 return shared.TempDirForOutDir(c.SoongOutDir())
702}
703
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700704func (c *configImpl) FileListDir() string {
705 return filepath.Join(c.OutDir(), ".module_paths")
706}
707
Dan Willemsen1e704462016-08-21 15:17:17 -0700708func (c *configImpl) KatiSuffix() string {
709 if c.katiSuffix != "" {
710 return c.katiSuffix
711 }
712 panic("SetKatiSuffix has not been called")
713}
714
Colin Cross37193492017-11-16 17:55:00 -0800715// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
716// user is interested in additional checks at the expense of build time.
717func (c *configImpl) Checkbuild() bool {
718 return c.checkbuild
719}
720
Dan Willemsen8a073a82017-02-04 17:30:44 -0800721func (c *configImpl) Dist() bool {
722 return c.dist
723}
724
Dan Willemsen1e704462016-08-21 15:17:17 -0700725func (c *configImpl) IsVerbose() bool {
726 return c.verbose
727}
728
Dan Willemsene0879fc2017-08-04 15:06:27 -0700729func (c *configImpl) SkipMake() bool {
730 return c.skipMake
731}
732
Dan Willemsen1e704462016-08-21 15:17:17 -0700733func (c *configImpl) TargetProduct() string {
734 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
735 return v
736 }
737 panic("TARGET_PRODUCT is not defined")
738}
739
Dan Willemsen02781d52017-05-12 19:28:13 -0700740func (c *configImpl) TargetDevice() string {
741 return c.targetDevice
742}
743
744func (c *configImpl) SetTargetDevice(device string) {
745 c.targetDevice = device
746}
747
748func (c *configImpl) TargetBuildVariant() string {
749 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
750 return v
751 }
752 panic("TARGET_BUILD_VARIANT is not defined")
753}
754
Dan Willemsen1e704462016-08-21 15:17:17 -0700755func (c *configImpl) KatiArgs() []string {
756 return c.katiArgs
757}
758
759func (c *configImpl) Parallel() int {
760 return c.parallel
761}
762
Colin Cross8b8bec32019-11-15 13:18:43 -0800763func (c *configImpl) HighmemParallel() int {
764 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
765 return i
766 }
767
768 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
769 parallel := c.Parallel()
770 if c.UseRemoteBuild() {
771 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
772 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
773 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
774 // Return 1/16th of the size of the local pool, rounding up.
775 return (parallel + 15) / 16
776 } else if c.totalRAM == 0 {
777 // Couldn't detect the total RAM, don't restrict highmem processes.
778 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -0700779 } else if c.totalRAM <= 16*1024*1024*1024 {
780 // Less than 16GB of ram, restrict to 1 highmem processes
781 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -0800782 } else if c.totalRAM <= 32*1024*1024*1024 {
783 // Less than 32GB of ram, restrict to 2 highmem processes
784 return 2
785 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
786 // If less than 8GB total RAM per process, reduce the number of highmem processes
787 return p
788 }
789 // No restriction on highmem processes
790 return parallel
791}
792
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800793func (c *configImpl) TotalRAM() uint64 {
794 return c.totalRAM
795}
796
Kousik Kumarec478642020-09-21 13:39:24 -0400797// ForceUseGoma determines whether we should override Goma deprecation
798// and use Goma for the current build or not.
799func (c *configImpl) ForceUseGoma() bool {
800 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
801 v = strings.TrimSpace(v)
802 if v != "" && v != "false" {
803 return true
804 }
805 }
806 return false
807}
808
Dan Willemsen1e704462016-08-21 15:17:17 -0700809func (c *configImpl) UseGoma() bool {
810 if v, ok := c.environ.Get("USE_GOMA"); ok {
811 v = strings.TrimSpace(v)
812 if v != "" && v != "false" {
813 return true
814 }
815 }
816 return false
817}
818
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900819func (c *configImpl) StartGoma() bool {
820 if !c.UseGoma() {
821 return false
822 }
823
824 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
825 v = strings.TrimSpace(v)
826 if v != "" && v != "false" {
827 return false
828 }
829 }
830 return true
831}
832
Ramy Medhatbbf25672019-07-17 12:30:04 +0000833func (c *configImpl) UseRBE() bool {
834 if v, ok := c.environ.Get("USE_RBE"); ok {
835 v = strings.TrimSpace(v)
836 if v != "" && v != "false" {
837 return true
838 }
839 }
840 return false
841}
842
843func (c *configImpl) StartRBE() bool {
844 if !c.UseRBE() {
845 return false
846 }
847
848 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
849 v = strings.TrimSpace(v)
850 if v != "" && v != "false" {
851 return false
852 }
853 }
854 return true
855}
856
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400857func (c *configImpl) logDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -0400858 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
859 if v, ok := c.environ.Get(f); ok {
860 return v
861 }
862 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400863 if c.Dist() {
864 return filepath.Join(c.DistDir(), "logs")
865 }
866 return c.OutDir()
867}
868
869func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000870 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
871 if v, ok := c.environ.Get(f); ok {
872 return v
873 }
874 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400875 return c.logDir()
876}
877
878func (c *configImpl) rbeLogPath() string {
879 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
880 if v, ok := c.environ.Get(f); ok {
881 return v
882 }
883 }
884 return fmt.Sprintf("text://%v/reproxy_log.txt", c.logDir())
885}
886
887func (c *configImpl) rbeExecRoot() string {
888 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
889 if v, ok := c.environ.Get(f); ok {
890 return v
891 }
892 }
893 wd, err := os.Getwd()
894 if err != nil {
895 return ""
896 }
897 return wd
898}
899
900func (c *configImpl) rbeDir() string {
901 if v, ok := c.environ.Get("RBE_DIR"); ok {
902 return v
903 }
904 return "prebuilts/remoteexecution-client/live/"
905}
906
907func (c *configImpl) rbeReproxy() string {
908 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
909 if v, ok := c.environ.Get(f); ok {
910 return v
911 }
912 }
913 return filepath.Join(c.rbeDir(), "reproxy")
914}
915
916func (c *configImpl) rbeAuth() (string, string) {
917 credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"}
918 for _, cf := range credFlags {
919 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
920 if v, ok := c.environ.Get(f); ok {
921 v = strings.TrimSpace(v)
922 if v != "" && v != "false" && v != "0" {
923 return "RBE_" + cf, v
924 }
925 }
926 }
927 }
928 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000929}
930
Colin Cross9016b912019-11-11 14:57:42 -0800931func (c *configImpl) UseRemoteBuild() bool {
932 return c.UseGoma() || c.UseRBE()
933}
934
Dan Willemsen1e704462016-08-21 15:17:17 -0700935// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700936// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700937// still limited by Parallel()
938func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -0800939 if !c.UseRemoteBuild() {
940 return 0
941 }
942 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
943 return i
Dan Willemsen1e704462016-08-21 15:17:17 -0700944 }
945 return 500
946}
947
948func (c *configImpl) SetKatiArgs(args []string) {
949 c.katiArgs = args
950}
951
952func (c *configImpl) SetNinjaArgs(args []string) {
953 c.ninjaArgs = args
954}
955
956func (c *configImpl) SetKatiSuffix(suffix string) {
957 c.katiSuffix = suffix
958}
959
Dan Willemsene0879fc2017-08-04 15:06:27 -0700960func (c *configImpl) LastKatiSuffixFile() string {
961 return filepath.Join(c.OutDir(), "last_kati_suffix")
962}
963
964func (c *configImpl) HasKatiSuffix() bool {
965 return c.katiSuffix != ""
966}
967
Dan Willemsen1e704462016-08-21 15:17:17 -0700968func (c *configImpl) KatiEnvFile() string {
969 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
970}
971
Dan Willemsen29971232018-09-26 14:58:30 -0700972func (c *configImpl) KatiBuildNinjaFile() string {
973 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700974}
975
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700976func (c *configImpl) KatiPackageNinjaFile() string {
977 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
978}
979
Dan Willemsen1e704462016-08-21 15:17:17 -0700980func (c *configImpl) SoongNinjaFile() string {
981 return filepath.Join(c.SoongOutDir(), "build.ninja")
982}
983
984func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700985 if c.katiSuffix == "" {
986 return filepath.Join(c.OutDir(), "combined.ninja")
987 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700988 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
989}
990
991func (c *configImpl) SoongAndroidMk() string {
992 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
993}
994
995func (c *configImpl) SoongMakeVarsMk() string {
996 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
997}
998
Dan Willemsenf052f782017-05-18 15:29:04 -0700999func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001000 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001001}
1002
Dan Willemsen02781d52017-05-12 19:28:13 -07001003func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001004 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1005}
1006
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001007func (c *configImpl) KatiPackageMkDir() string {
1008 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1009}
1010
Dan Willemsenf052f782017-05-18 15:29:04 -07001011func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001012 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001013}
1014
1015func (c *configImpl) HostOut() string {
1016 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1017}
1018
1019// This probably needs to be multi-valued, so not exporting it for now
1020func (c *configImpl) hostCrossOut() string {
1021 if runtime.GOOS == "linux" {
1022 return filepath.Join(c.hostOutRoot(), "windows-x86")
1023 } else {
1024 return ""
1025 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001026}
1027
Dan Willemsen1e704462016-08-21 15:17:17 -07001028func (c *configImpl) HostPrebuiltTag() string {
1029 if runtime.GOOS == "linux" {
1030 return "linux-x86"
1031 } else if runtime.GOOS == "darwin" {
1032 return "darwin-x86"
1033 } else {
1034 panic("Unsupported OS")
1035 }
1036}
Dan Willemsenf173d592017-04-27 14:28:00 -07001037
Dan Willemsen8122bd52017-10-12 20:20:41 -07001038func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001039 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1040 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001041 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1042 if _, err := os.Stat(asan); err == nil {
1043 return asan
1044 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001045 }
1046 }
1047 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1048}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001049
1050func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1051 c.brokenDupRules = val
1052}
1053
1054func (c *configImpl) BuildBrokenDupRules() bool {
1055 return c.brokenDupRules
1056}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001057
Dan Willemsen25e6f092019-04-09 10:22:43 -07001058func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1059 c.brokenUsesNetwork = val
1060}
1061
1062func (c *configImpl) BuildBrokenUsesNetwork() bool {
1063 return c.brokenUsesNetwork
1064}
1065
Dan Willemsene3336352020-01-02 19:10:38 -08001066func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1067 c.brokenNinjaEnvVars = val
1068}
1069
1070func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1071 return c.brokenNinjaEnvVars
1072}
1073
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001074func (c *configImpl) SetTargetDeviceDir(dir string) {
1075 c.targetDeviceDir = dir
1076}
1077
1078func (c *configImpl) TargetDeviceDir() string {
1079 return c.targetDeviceDir
1080}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001081
Patrice Arruda219eef32020-06-01 17:29:30 +00001082func (c *configImpl) BuildDateTime() string {
1083 return c.buildDateTime
1084}
1085
1086func (c *configImpl) MetricsUploaderApp() string {
1087 if p, ok := c.environ.Get("ANDROID_ENABLE_METRICS_UPLOAD"); ok {
1088 return p
1089 }
1090 return ""
1091}