blob: 229bd5cea1db43b650db4fc5a4c1ccba56ec3979 [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 Arrudaaf880da2020-11-13 08:41:26 -0800277 bpd := shared.BazelMetricsDir(ret.OutDir())
278 if err := os.RemoveAll(bpd); err != nil {
279 ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err)
280 }
281 if ret.UseBazel() {
282 if err := os.MkdirAll(bpd, 0777); err != nil {
283 ctx.Fatalf("Failed to create bazel profile directory %q: %v", bpd, err)
284 }
285 }
286
Patrice Arruda96850362020-08-11 20:41:11 +0000287 c := Config{ret}
288 storeConfigMetrics(ctx, c)
289 return c
Dan Willemsen9b587492017-07-10 22:13:00 -0700290}
291
Patrice Arruda13848222019-04-22 17:12:02 -0700292// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
293// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700294func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
295 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700296}
297
Patrice Arruda96850362020-08-11 20:41:11 +0000298// storeConfigMetrics selects a set of configuration information and store in
299// the metrics system for further analysis.
300func storeConfigMetrics(ctx Context, config Config) {
301 if ctx.Metrics == nil {
302 return
303 }
304
305 b := &smpb.BuildConfig{
Patrice Arrudac97d6dc2020-09-28 18:22:07 +0000306 ForceUseGoma: proto.Bool(config.ForceUseGoma()),
307 UseGoma: proto.Bool(config.UseGoma()),
308 UseRbe: proto.Bool(config.UseRBE()),
Patrice Arruda96850362020-08-11 20:41:11 +0000309 }
310 ctx.Metrics.BuildConfig(b)
Patrice Arruda3edfd482020-10-13 23:58:41 +0000311
312 s := &smpb.SystemResourceInfo{
313 TotalPhysicalMemory: proto.Uint64(config.TotalRAM()),
314 AvailableCpus: proto.Int32(int32(runtime.NumCPU())),
315 }
316 ctx.Metrics.SystemResourceInfo(s)
Patrice Arruda96850362020-08-11 20:41:11 +0000317}
318
Patrice Arruda13848222019-04-22 17:12:02 -0700319// getConfigArgs processes the command arguments based on the build action and creates a set of new
320// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700321func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700322 // The next block of code verifies that the current directory is the root directory of the source
323 // tree. It then finds the relative path of dir based on the root directory of the source tree
324 // and verify that dir is inside of the source tree.
325 checkTopDir(ctx)
326 topDir, err := os.Getwd()
327 if err != nil {
328 ctx.Fatalf("Error retrieving top directory: %v", err)
329 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700330 dir, err = filepath.EvalSymlinks(dir)
331 if err != nil {
332 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
333 }
Patrice Arruda13848222019-04-22 17:12:02 -0700334 dir, err = filepath.Abs(dir)
335 if err != nil {
336 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
337 }
338 relDir, err := filepath.Rel(topDir, dir)
339 if err != nil {
340 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
341 }
342 // If there are ".." in the path, it's not in the source tree.
343 if strings.Contains(relDir, "..") {
344 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
345 }
346
347 configArgs := args[:]
348
349 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
350 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
351 targetNamePrefix := "MODULES-IN-"
352 if inList("GET-INSTALL-PATH", configArgs) {
353 targetNamePrefix = "GET-INSTALL-PATH-IN-"
354 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
355 }
356
Patrice Arruda13848222019-04-22 17:12:02 -0700357 var targets []string
358
359 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700360 case BUILD_MODULES:
361 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700362 case BUILD_MODULES_IN_A_DIRECTORY:
363 // If dir is the root source tree, all the modules are built of the source tree are built so
364 // no need to find the build file.
365 if topDir == dir {
366 break
367 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700368
Patrice Arruda13848222019-04-22 17:12:02 -0700369 buildFile := findBuildFile(ctx, relDir)
370 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700371 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700372 }
Patrice Arruda13848222019-04-22 17:12:02 -0700373 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
374 case BUILD_MODULES_IN_DIRECTORIES:
375 newConfigArgs, dirs := splitArgs(configArgs)
376 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700377 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700378 }
379
380 // Tidy only override all other specified targets.
381 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
382 if tidyOnly == "true" || tidyOnly == "1" {
383 configArgs = append(configArgs, "tidy_only")
384 } else {
385 configArgs = append(configArgs, targets...)
386 }
387
388 return configArgs
389}
390
391// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
392func convertToTarget(dir string, targetNamePrefix string) string {
393 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
394}
395
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700396// hasBuildFile returns true if dir contains an Android build file.
397func hasBuildFile(ctx Context, dir string) bool {
398 for _, buildFile := range buildFiles {
399 _, err := os.Stat(filepath.Join(dir, buildFile))
400 if err == nil {
401 return true
402 }
403 if !os.IsNotExist(err) {
404 ctx.Fatalf("Error retrieving the build file stats: %v", err)
405 }
406 }
407 return false
408}
409
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700410// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
411// in the current and any sub directory of dir. If a build file is not found, traverse the path
412// up by one directory and repeat again until either a build file is found or reached to the root
413// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
414// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700415func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700416 // If the string is empty or ".", assume it is top directory of the source tree.
417 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700418 return ""
419 }
420
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700421 found := false
422 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
423 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
424 if err != nil {
425 return err
426 }
427 if found {
428 return filepath.SkipDir
429 }
430 if info.IsDir() {
431 return nil
432 }
433 for _, buildFile := range buildFiles {
434 if info.Name() == buildFile {
435 found = true
436 return filepath.SkipDir
437 }
438 }
439 return nil
440 })
441 if err != nil {
442 ctx.Fatalf("Error finding Android build file: %v", err)
443 }
444
445 if found {
446 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700447 }
448 }
449
450 return ""
451}
452
453// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
454func splitArgs(args []string) (newArgs []string, dirs []string) {
455 specialArgs := map[string]bool{
456 "showcommands": true,
457 "snod": true,
458 "dist": true,
459 "checkbuild": true,
460 }
461
462 newArgs = []string{}
463 dirs = []string{}
464
465 for _, arg := range args {
466 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
467 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
468 newArgs = append(newArgs, arg)
469 continue
470 }
471
472 if _, ok := specialArgs[arg]; ok {
473 newArgs = append(newArgs, arg)
474 continue
475 }
476
477 dirs = append(dirs, arg)
478 }
479
480 return newArgs, dirs
481}
482
483// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
484// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
485// source root tree where the build action command was invoked. Each directory is validated if the
486// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700487func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700488 for _, dir := range dirs {
489 // The directory may have specified specific modules to build. ":" is the separator to separate
490 // the directory and the list of modules.
491 s := strings.Split(dir, ":")
492 l := len(s)
493 if l > 2 { // more than one ":" was specified.
494 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
495 }
496
497 dir = filepath.Join(relDir, s[0])
498 if _, err := os.Stat(dir); err != nil {
499 ctx.Fatalf("couldn't find directory %s", dir)
500 }
501
502 // Verify that if there are any targets specified after ":". Each target is separated by ",".
503 var newTargets []string
504 if l == 2 && s[1] != "" {
505 newTargets = strings.Split(s[1], ",")
506 if inList("", newTargets) {
507 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
508 }
509 }
510
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700511 // If there are specified targets to build in dir, an android build file must exist for the one
512 // shot build. For the non-targets case, find the appropriate build file and build all the
513 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700514 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700515 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700516 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
517 }
518 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700519 buildFile := findBuildFile(ctx, dir)
520 if buildFile == "" {
521 ctx.Fatalf("Build file not found for %s directory", dir)
522 }
523 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700524 }
525
Patrice Arruda13848222019-04-22 17:12:02 -0700526 targets = append(targets, newTargets...)
527 }
528
Dan Willemsence41e942019-07-29 23:39:30 -0700529 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700530}
531
Dan Willemsen9b587492017-07-10 22:13:00 -0700532func (c *configImpl) parseArgs(ctx Context, args []string) {
533 for i := 0; i < len(args); i++ {
534 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700535 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700536 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700537 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700538 } else if arg == "--skip-make" {
539 c.skipMake = true
Colin Cross00a8a3f2020-10-29 14:08:31 -0700540 } else if arg == "--skip-soong-tests" {
541 c.skipSoongTests = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700542 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700543 parseArgNum := func(def int) int {
544 if len(arg) > 2 {
545 p, err := strconv.ParseUint(arg[2:], 10, 31)
546 if err != nil {
547 ctx.Fatalf("Failed to parse %q: %v", arg, err)
548 }
549 return int(p)
550 } else if i+1 < len(args) {
551 p, err := strconv.ParseUint(args[i+1], 10, 31)
552 if err == nil {
553 i++
554 return int(p)
555 }
556 }
557 return def
558 }
559
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700560 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700561 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700562 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700563 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700564 } else {
565 ctx.Fatalln("Unknown option:", arg)
566 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700567 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
Dan Willemsen6dfe30a2018-09-10 12:41:10 -0700568 if k == "OUT_DIR" {
569 ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.")
570 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700571 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700572 } else if arg == "dist" {
573 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700574 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700575 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800576 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700577 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700578 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700579 }
580 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700581}
582
Dan Willemsened869522018-01-08 14:58:46 -0800583func (c *configImpl) configureLocale(ctx Context) {
584 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
585 output, err := cmd.Output()
586
587 var locales []string
588 if err == nil {
589 locales = strings.Split(string(output), "\n")
590 } else {
591 // If we're unable to list the locales, let's assume en_US.UTF-8
592 locales = []string{"en_US.UTF-8"}
593 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
594 }
595
596 // gettext uses LANGUAGE, which is passed directly through
597
598 // For LANG and LC_*, only preserve the evaluated version of
599 // LC_MESSAGES
600 user_lang := ""
601 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
602 user_lang = lc_all
603 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
604 user_lang = lc_messages
605 } else if lang, ok := c.environ.Get("LANG"); ok {
606 user_lang = lang
607 }
608
609 c.environ.UnsetWithPrefix("LC_")
610
611 if user_lang != "" {
612 c.environ.Set("LC_MESSAGES", user_lang)
613 }
614
615 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
616 // for others)
617 if inList("C.UTF-8", locales) {
618 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500619 } else if inList("C.utf8", locales) {
620 // These normalize to the same thing
621 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800622 } else if inList("en_US.UTF-8", locales) {
623 c.environ.Set("LANG", "en_US.UTF-8")
624 } else if inList("en_US.utf8", locales) {
625 // These normalize to the same thing
626 c.environ.Set("LANG", "en_US.UTF-8")
627 } else {
628 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
629 }
630}
631
Dan Willemsen1e704462016-08-21 15:17:17 -0700632// Lunch configures the environment for a specific product similarly to the
633// `lunch` bash function.
634func (c *configImpl) Lunch(ctx Context, product, variant string) {
635 if variant != "eng" && variant != "userdebug" && variant != "user" {
636 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
637 }
638
639 c.environ.Set("TARGET_PRODUCT", product)
640 c.environ.Set("TARGET_BUILD_VARIANT", variant)
641 c.environ.Set("TARGET_BUILD_TYPE", "release")
642 c.environ.Unset("TARGET_BUILD_APPS")
Martin Stjernholm08802332020-06-04 17:00:01 +0100643 c.environ.Unset("TARGET_BUILD_UNBUNDLED")
Dan Willemsen1e704462016-08-21 15:17:17 -0700644}
645
646// Tapas configures the environment to build one or more unbundled apps,
647// similarly to the `tapas` bash function.
648func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
649 if len(apps) == 0 {
650 apps = []string{"all"}
651 }
652 if variant == "" {
653 variant = "eng"
654 }
655
656 if variant != "eng" && variant != "userdebug" && variant != "user" {
657 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
658 }
659
660 var product string
661 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700662 case "arm", "":
663 product = "aosp_arm"
664 case "arm64":
665 product = "aosm_arm64"
Dan Willemsen1e704462016-08-21 15:17:17 -0700666 case "x86":
667 product = "aosp_x86"
668 case "x86_64":
669 product = "aosp_x86_64"
670 default:
671 ctx.Fatalf("Invalid architecture: %q", arch)
672 }
673
674 c.environ.Set("TARGET_PRODUCT", product)
675 c.environ.Set("TARGET_BUILD_VARIANT", variant)
676 c.environ.Set("TARGET_BUILD_TYPE", "release")
677 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
678}
679
680func (c *configImpl) Environment() *Environment {
681 return c.environ
682}
683
684func (c *configImpl) Arguments() []string {
685 return c.arguments
686}
687
688func (c *configImpl) OutDir() string {
689 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700690 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700691 }
692 return "out"
693}
694
Dan Willemsen8a073a82017-02-04 17:30:44 -0800695func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700696 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800697}
698
Dan Willemsen1e704462016-08-21 15:17:17 -0700699func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700700 if c.skipMake {
701 return c.arguments
702 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700703 return c.ninjaArgs
704}
705
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500706func (c *configImpl) BazelOutDir() string {
707 return filepath.Join(c.OutDir(), "bazel")
708}
709
Dan Willemsen1e704462016-08-21 15:17:17 -0700710func (c *configImpl) SoongOutDir() string {
711 return filepath.Join(c.OutDir(), "soong")
712}
713
Jeff Gastonefc1b412017-03-29 17:29:06 -0700714func (c *configImpl) TempDir() string {
715 return shared.TempDirForOutDir(c.SoongOutDir())
716}
717
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700718func (c *configImpl) FileListDir() string {
719 return filepath.Join(c.OutDir(), ".module_paths")
720}
721
Dan Willemsen1e704462016-08-21 15:17:17 -0700722func (c *configImpl) KatiSuffix() string {
723 if c.katiSuffix != "" {
724 return c.katiSuffix
725 }
726 panic("SetKatiSuffix has not been called")
727}
728
Colin Cross37193492017-11-16 17:55:00 -0800729// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
730// user is interested in additional checks at the expense of build time.
731func (c *configImpl) Checkbuild() bool {
732 return c.checkbuild
733}
734
Dan Willemsen8a073a82017-02-04 17:30:44 -0800735func (c *configImpl) Dist() bool {
736 return c.dist
737}
738
Dan Willemsen1e704462016-08-21 15:17:17 -0700739func (c *configImpl) IsVerbose() bool {
740 return c.verbose
741}
742
Dan Willemsene0879fc2017-08-04 15:06:27 -0700743func (c *configImpl) SkipMake() bool {
744 return c.skipMake
745}
746
Dan Willemsen1e704462016-08-21 15:17:17 -0700747func (c *configImpl) TargetProduct() string {
748 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
749 return v
750 }
751 panic("TARGET_PRODUCT is not defined")
752}
753
Dan Willemsen02781d52017-05-12 19:28:13 -0700754func (c *configImpl) TargetDevice() string {
755 return c.targetDevice
756}
757
758func (c *configImpl) SetTargetDevice(device string) {
759 c.targetDevice = device
760}
761
762func (c *configImpl) TargetBuildVariant() string {
763 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
764 return v
765 }
766 panic("TARGET_BUILD_VARIANT is not defined")
767}
768
Dan Willemsen1e704462016-08-21 15:17:17 -0700769func (c *configImpl) KatiArgs() []string {
770 return c.katiArgs
771}
772
773func (c *configImpl) Parallel() int {
774 return c.parallel
775}
776
Colin Cross8b8bec32019-11-15 13:18:43 -0800777func (c *configImpl) HighmemParallel() int {
778 if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok {
779 return i
780 }
781
782 const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024
783 parallel := c.Parallel()
784 if c.UseRemoteBuild() {
785 // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism
786 // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs
787 // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention.
788 // Return 1/16th of the size of the local pool, rounding up.
789 return (parallel + 15) / 16
790 } else if c.totalRAM == 0 {
791 // Couldn't detect the total RAM, don't restrict highmem processes.
792 return parallel
Dan Willemsen570a2922020-05-26 23:02:29 -0700793 } else if c.totalRAM <= 16*1024*1024*1024 {
794 // Less than 16GB of ram, restrict to 1 highmem processes
795 return 1
Colin Cross8b8bec32019-11-15 13:18:43 -0800796 } else if c.totalRAM <= 32*1024*1024*1024 {
797 // Less than 32GB of ram, restrict to 2 highmem processes
798 return 2
799 } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel {
800 // If less than 8GB total RAM per process, reduce the number of highmem processes
801 return p
802 }
803 // No restriction on highmem processes
804 return parallel
805}
806
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800807func (c *configImpl) TotalRAM() uint64 {
808 return c.totalRAM
809}
810
Kousik Kumarec478642020-09-21 13:39:24 -0400811// ForceUseGoma determines whether we should override Goma deprecation
812// and use Goma for the current build or not.
813func (c *configImpl) ForceUseGoma() bool {
814 if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok {
815 v = strings.TrimSpace(v)
816 if v != "" && v != "false" {
817 return true
818 }
819 }
820 return false
821}
822
Dan Willemsen1e704462016-08-21 15:17:17 -0700823func (c *configImpl) UseGoma() bool {
824 if v, ok := c.environ.Get("USE_GOMA"); ok {
825 v = strings.TrimSpace(v)
826 if v != "" && v != "false" {
827 return true
828 }
829 }
830 return false
831}
832
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900833func (c *configImpl) StartGoma() bool {
834 if !c.UseGoma() {
835 return false
836 }
837
838 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
839 v = strings.TrimSpace(v)
840 if v != "" && v != "false" {
841 return false
842 }
843 }
844 return true
845}
846
Ramy Medhatbbf25672019-07-17 12:30:04 +0000847func (c *configImpl) UseRBE() bool {
848 if v, ok := c.environ.Get("USE_RBE"); ok {
849 v = strings.TrimSpace(v)
850 if v != "" && v != "false" {
851 return true
852 }
853 }
854 return false
855}
856
Patrice Arruda0c1c4562020-11-11 13:01:25 -0800857func (c *configImpl) UseBazel() bool {
858 if v, ok := c.environ.Get("USE_BAZEL"); ok {
859 v = strings.TrimSpace(v)
860 if v != "" && v != "false" {
861 return true
862 }
863 }
864 return false
865}
866
Ramy Medhatbbf25672019-07-17 12:30:04 +0000867func (c *configImpl) StartRBE() bool {
868 if !c.UseRBE() {
869 return false
870 }
871
872 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
873 v = strings.TrimSpace(v)
874 if v != "" && v != "false" {
875 return false
876 }
877 }
878 return true
879}
880
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400881func (c *configImpl) logDir() string {
Kousik Kumar0d15a722020-09-23 02:54:11 -0400882 for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} {
883 if v, ok := c.environ.Get(f); ok {
884 return v
885 }
886 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400887 if c.Dist() {
888 return filepath.Join(c.DistDir(), "logs")
889 }
890 return c.OutDir()
891}
892
893func (c *configImpl) rbeStatsOutputDir() string {
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000894 for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} {
895 if v, ok := c.environ.Get(f); ok {
896 return v
897 }
898 }
Ramy Medhat0fc67eb2020-08-12 01:26:23 -0400899 return c.logDir()
900}
901
902func (c *configImpl) rbeLogPath() string {
903 for _, f := range []string{"RBE_log_path", "FLAG_log_path"} {
904 if v, ok := c.environ.Get(f); ok {
905 return v
906 }
907 }
908 return fmt.Sprintf("text://%v/reproxy_log.txt", c.logDir())
909}
910
911func (c *configImpl) rbeExecRoot() string {
912 for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} {
913 if v, ok := c.environ.Get(f); ok {
914 return v
915 }
916 }
917 wd, err := os.Getwd()
918 if err != nil {
919 return ""
920 }
921 return wd
922}
923
924func (c *configImpl) rbeDir() string {
925 if v, ok := c.environ.Get("RBE_DIR"); ok {
926 return v
927 }
928 return "prebuilts/remoteexecution-client/live/"
929}
930
931func (c *configImpl) rbeReproxy() string {
932 for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} {
933 if v, ok := c.environ.Get(f); ok {
934 return v
935 }
936 }
937 return filepath.Join(c.rbeDir(), "reproxy")
938}
939
940func (c *configImpl) rbeAuth() (string, string) {
941 credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"}
942 for _, cf := range credFlags {
943 for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} {
944 if v, ok := c.environ.Get(f); ok {
945 v = strings.TrimSpace(v)
946 if v != "" && v != "false" && v != "0" {
947 return "RBE_" + cf, v
948 }
949 }
950 }
951 }
952 return "RBE_use_application_default_credentials", "true"
Patrice Arruda62f1bf22020-07-07 12:48:26 +0000953}
954
Colin Cross9016b912019-11-11 14:57:42 -0800955func (c *configImpl) UseRemoteBuild() bool {
956 return c.UseGoma() || c.UseRBE()
957}
958
Dan Willemsen1e704462016-08-21 15:17:17 -0700959// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700960// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700961// still limited by Parallel()
962func (c *configImpl) RemoteParallel() int {
Colin Cross8b8bec32019-11-15 13:18:43 -0800963 if !c.UseRemoteBuild() {
964 return 0
965 }
966 if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok {
967 return i
Dan Willemsen1e704462016-08-21 15:17:17 -0700968 }
969 return 500
970}
971
972func (c *configImpl) SetKatiArgs(args []string) {
973 c.katiArgs = args
974}
975
976func (c *configImpl) SetNinjaArgs(args []string) {
977 c.ninjaArgs = args
978}
979
980func (c *configImpl) SetKatiSuffix(suffix string) {
981 c.katiSuffix = suffix
982}
983
Dan Willemsene0879fc2017-08-04 15:06:27 -0700984func (c *configImpl) LastKatiSuffixFile() string {
985 return filepath.Join(c.OutDir(), "last_kati_suffix")
986}
987
988func (c *configImpl) HasKatiSuffix() bool {
989 return c.katiSuffix != ""
990}
991
Dan Willemsen1e704462016-08-21 15:17:17 -0700992func (c *configImpl) KatiEnvFile() string {
993 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
994}
995
Dan Willemsen29971232018-09-26 14:58:30 -0700996func (c *configImpl) KatiBuildNinjaFile() string {
997 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700998}
999
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001000func (c *configImpl) KatiPackageNinjaFile() string {
1001 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
1002}
1003
Dan Willemsen1e704462016-08-21 15:17:17 -07001004func (c *configImpl) SoongNinjaFile() string {
1005 return filepath.Join(c.SoongOutDir(), "build.ninja")
1006}
1007
1008func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -07001009 if c.katiSuffix == "" {
1010 return filepath.Join(c.OutDir(), "combined.ninja")
1011 }
Dan Willemsen1e704462016-08-21 15:17:17 -07001012 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
1013}
1014
1015func (c *configImpl) SoongAndroidMk() string {
1016 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
1017}
1018
1019func (c *configImpl) SoongMakeVarsMk() string {
1020 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
1021}
1022
Dan Willemsenf052f782017-05-18 15:29:04 -07001023func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001024 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -07001025}
1026
Dan Willemsen02781d52017-05-12 19:28:13 -07001027func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -07001028 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
1029}
1030
Dan Willemsenfb1271a2018-09-26 15:00:42 -07001031func (c *configImpl) KatiPackageMkDir() string {
1032 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
1033}
1034
Dan Willemsenf052f782017-05-18 15:29:04 -07001035func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -07001036 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -07001037}
1038
1039func (c *configImpl) HostOut() string {
1040 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
1041}
1042
1043// This probably needs to be multi-valued, so not exporting it for now
1044func (c *configImpl) hostCrossOut() string {
1045 if runtime.GOOS == "linux" {
1046 return filepath.Join(c.hostOutRoot(), "windows-x86")
1047 } else {
1048 return ""
1049 }
Dan Willemsen02781d52017-05-12 19:28:13 -07001050}
1051
Dan Willemsen1e704462016-08-21 15:17:17 -07001052func (c *configImpl) HostPrebuiltTag() string {
1053 if runtime.GOOS == "linux" {
1054 return "linux-x86"
1055 } else if runtime.GOOS == "darwin" {
1056 return "darwin-x86"
1057 } else {
1058 panic("Unsupported OS")
1059 }
1060}
Dan Willemsenf173d592017-04-27 14:28:00 -07001061
Dan Willemsen8122bd52017-10-12 20:20:41 -07001062func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -07001063 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
1064 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -07001065 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
1066 if _, err := os.Stat(asan); err == nil {
1067 return asan
1068 }
Dan Willemsenf173d592017-04-27 14:28:00 -07001069 }
1070 }
1071 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
1072}
Dan Willemsen3d60b112018-04-04 22:25:56 -07001073
1074func (c *configImpl) SetBuildBrokenDupRules(val bool) {
1075 c.brokenDupRules = val
1076}
1077
1078func (c *configImpl) BuildBrokenDupRules() bool {
1079 return c.brokenDupRules
1080}
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001081
Dan Willemsen25e6f092019-04-09 10:22:43 -07001082func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
1083 c.brokenUsesNetwork = val
1084}
1085
1086func (c *configImpl) BuildBrokenUsesNetwork() bool {
1087 return c.brokenUsesNetwork
1088}
1089
Dan Willemsene3336352020-01-02 19:10:38 -08001090func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) {
1091 c.brokenNinjaEnvVars = val
1092}
1093
1094func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string {
1095 return c.brokenNinjaEnvVars
1096}
1097
Dan Willemsen6ab79db2018-05-02 00:06:28 -07001098func (c *configImpl) SetTargetDeviceDir(dir string) {
1099 c.targetDeviceDir = dir
1100}
1101
1102func (c *configImpl) TargetDeviceDir() string {
1103 return c.targetDeviceDir
1104}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -07001105
Patrice Arruda219eef32020-06-01 17:29:30 +00001106func (c *configImpl) BuildDateTime() string {
1107 return c.buildDateTime
1108}
1109
1110func (c *configImpl) MetricsUploaderApp() string {
1111 if p, ok := c.environ.Get("ANDROID_ENABLE_METRICS_UPLOAD"); ok {
1112 return p
1113 }
1114 return ""
1115}