blob: fae569f099d02527fa20fd23d56c5778bb3280cb [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 (
Dan Willemsenc2af0be2017-01-20 14:10:01 -080018 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "path/filepath"
20 "runtime"
21 "strconv"
22 "strings"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080023 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070024
25 "android/soong/shared"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
28type Config struct{ *configImpl }
29
30type configImpl struct {
31 // From the environment
Colin Cross28f527c2019-11-26 16:19:04 -080032 arguments []string
33 goma bool
34 environ *Environment
35 distDir string
36 buildDateTime string
Dan Willemsen1e704462016-08-21 15:17:17 -070037
38 // From the arguments
Colin Cross37193492017-11-16 17:55:00 -080039 parallel int
40 keepGoing int
41 verbose bool
42 checkbuild bool
43 dist bool
44 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070045
46 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070047 katiArgs []string
48 ninjaArgs []string
49 katiSuffix string
50 targetDevice string
51 targetDeviceDir string
Dan Willemsen3d60b112018-04-04 22:25:56 -070052
Dan Willemsen2bb82d02019-12-27 09:35:42 -080053 // Autodetected
54 totalRAM uint64
55
Dan Willemsend8aa39d2018-08-27 15:01:03 -070056 pdkBuild bool
57
Dan Willemsen60977462019-04-18 09:40:15 -070058 brokenDupRules bool
59 brokenUsesNetwork bool
Dan Willemsen18490112018-05-25 16:30:04 -070060
61 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070062}
63
Dan Willemsenc2af0be2017-01-20 14:10:01 -080064const srcDirFileCheck = "build/soong/root.bp"
65
Patrice Arruda9450d0b2019-07-08 11:06:46 -070066var buildFiles = []string{"Android.mk", "Android.bp"}
67
Patrice Arruda13848222019-04-22 17:12:02 -070068type BuildAction uint
69
70const (
71 // Builds all of the modules and their dependencies of a specified directory, relative to the root
72 // directory of the source tree.
73 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
74
75 // Builds all of the modules and their dependencies of a list of specified directories. All specified
76 // directories are relative to the root directory of the source tree.
77 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070078
79 // Build a list of specified modules. If none was specified, simply build the whole source tree.
80 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070081)
82
83// checkTopDir validates that the current directory is at the root directory of the source tree.
84func checkTopDir(ctx Context) {
85 if _, err := os.Stat(srcDirFileCheck); err != nil {
86 if os.IsNotExist(err) {
87 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
88 }
89 ctx.Fatalln("Error verifying tree state:", err)
90 }
91}
92
Dan Willemsen1e704462016-08-21 15:17:17 -070093func NewConfig(ctx Context, args ...string) Config {
94 ret := &configImpl{
95 environ: OsEnvironment(),
96 }
97
Dan Willemsen9b587492017-07-10 22:13:00 -070098 // Sane default matching ninja
99 ret.parallel = runtime.NumCPU() + 2
100 ret.keepGoing = 1
101
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800102 ret.totalRAM = detectTotalRAM(ctx)
103
Dan Willemsen9b587492017-07-10 22:13:00 -0700104 ret.parseArgs(ctx, args)
105
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800106 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700107 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
108 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
109 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800110 outDir := "out"
111 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
112 if wd, err := os.Getwd(); err != nil {
113 ctx.Fatalln("Failed to get working directory:", err)
114 } else {
115 outDir = filepath.Join(baseDir, filepath.Base(wd))
116 }
117 }
118 ret.environ.Set("OUT_DIR", outDir)
119 }
120
Dan Willemsen2d31a442018-10-20 21:33:41 -0700121 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
122 ret.distDir = filepath.Clean(distDir)
123 } else {
124 ret.distDir = filepath.Join(ret.OutDir(), "dist")
125 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700126
Dan Willemsen1e704462016-08-21 15:17:17 -0700127 ret.environ.Unset(
128 // We're already using it
129 "USE_SOONG_UI",
130
131 // We should never use GOROOT/GOPATH from the shell environment
132 "GOROOT",
133 "GOPATH",
134
135 // These should only come from Soong, not the environment.
136 "CLANG",
137 "CLANG_CXX",
138 "CCC_CC",
139 "CCC_CXX",
140
141 // Used by the goma compiler wrapper, but should only be set by
142 // gomacc
143 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800144
145 // We handle this above
146 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700147
Dan Willemsen2d31a442018-10-20 21:33:41 -0700148 // This is handled above too, and set for individual commands later
149 "DIST_DIR",
150
Dan Willemsen68a09852017-04-18 13:56:57 -0700151 // Variables that have caused problems in the past
Dan Willemsen1c504d92019-11-18 19:13:53 +0000152 "BASH_ENV",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700153 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700154 "DISPLAY",
155 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700156 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700157 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700158
159 // Drop make flags
160 "MAKEFLAGS",
161 "MAKELEVEL",
162 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700163
164 // Set in envsetup.sh, reset in makefiles
165 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700166
167 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
168 "ANDROID_BUILD_TOP",
169 "ANDROID_HOST_OUT",
170 "ANDROID_PRODUCT_OUT",
171 "ANDROID_HOST_OUT_TESTCASES",
172 "ANDROID_TARGET_OUT_TESTCASES",
173 "ANDROID_TOOLCHAIN",
174 "ANDROID_TOOLCHAIN_2ND_ARCH",
175 "ANDROID_DEV_SCRIPTS",
176 "ANDROID_EMULATOR_PREBUILTS",
177 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700178
179 // Only set in multiproduct_kati after config generation
180 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700181 )
182
183 // Tell python not to spam the source tree with .pyc files.
184 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
185
Dan Willemsen32a669b2018-03-08 19:42:00 -0800186 ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
187
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700188 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
189 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
190 "llvm-binutils-stable/llvm-symbolizer")
191 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
192
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800193 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700194 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800195
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700196 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700197 ctx.Println("You are building in a directory whose absolute path contains a space character:")
198 ctx.Println()
199 ctx.Printf("%q\n", srcDir)
200 ctx.Println()
201 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700202 }
203
204 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700205 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
206 ctx.Println()
207 ctx.Printf("%q\n", outDir)
208 ctx.Println()
209 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700210 }
211
212 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700213 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
214 ctx.Println()
215 ctx.Printf("%q\n", distDir)
216 ctx.Println()
217 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700218 }
219
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700220 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000221 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
222 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100223 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700224 javaHome := func() string {
225 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
226 return override
227 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000228 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
229 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 +0100230 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000231 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700232 }()
233 absJavaHome := absPath(ctx, javaHome)
234
Dan Willemsened869522018-01-08 14:58:46 -0800235 ret.configureLocale(ctx)
236
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700237 newPath := []string{filepath.Join(absJavaHome, "bin")}
238 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
239 newPath = append(newPath, path)
240 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100241
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700242 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
243 ret.environ.Set("JAVA_HOME", absJavaHome)
244 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000245 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
246 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100247 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700248 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
249
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800250 outDir := ret.OutDir()
251 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800252 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
Colin Cross28f527c2019-11-26 16:19:04 -0800253 ret.buildDateTime = buildDateTime
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800254 } else {
Colin Cross28f527c2019-11-26 16:19:04 -0800255 ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800256 }
Colin Cross28f527c2019-11-26 16:19:04 -0800257
Nan Zhang17f27672018-12-12 16:01:49 -0800258 if ctx.Metrics != nil {
Colin Cross28f527c2019-11-26 16:19:04 -0800259 ctx.Metrics.SetBuildDateTime(ret.buildDateTime)
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800260 }
261 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
262
Dan Willemsen9b587492017-07-10 22:13:00 -0700263 return Config{ret}
264}
265
Patrice Arruda13848222019-04-22 17:12:02 -0700266// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
267// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700268func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
269 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700270}
271
272// getConfigArgs processes the command arguments based on the build action and creates a set of new
273// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700274func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700275 // The next block of code verifies that the current directory is the root directory of the source
276 // tree. It then finds the relative path of dir based on the root directory of the source tree
277 // and verify that dir is inside of the source tree.
278 checkTopDir(ctx)
279 topDir, err := os.Getwd()
280 if err != nil {
281 ctx.Fatalf("Error retrieving top directory: %v", err)
282 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700283 dir, err = filepath.EvalSymlinks(dir)
284 if err != nil {
285 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
286 }
Patrice Arruda13848222019-04-22 17:12:02 -0700287 dir, err = filepath.Abs(dir)
288 if err != nil {
289 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
290 }
291 relDir, err := filepath.Rel(topDir, dir)
292 if err != nil {
293 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
294 }
295 // If there are ".." in the path, it's not in the source tree.
296 if strings.Contains(relDir, "..") {
297 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
298 }
299
300 configArgs := args[:]
301
302 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
303 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
304 targetNamePrefix := "MODULES-IN-"
305 if inList("GET-INSTALL-PATH", configArgs) {
306 targetNamePrefix = "GET-INSTALL-PATH-IN-"
307 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
308 }
309
Patrice Arruda13848222019-04-22 17:12:02 -0700310 var targets []string
311
312 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700313 case BUILD_MODULES:
314 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700315 case BUILD_MODULES_IN_A_DIRECTORY:
316 // If dir is the root source tree, all the modules are built of the source tree are built so
317 // no need to find the build file.
318 if topDir == dir {
319 break
320 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700321
Patrice Arruda13848222019-04-22 17:12:02 -0700322 buildFile := findBuildFile(ctx, relDir)
323 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700324 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700325 }
Patrice Arruda13848222019-04-22 17:12:02 -0700326 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
327 case BUILD_MODULES_IN_DIRECTORIES:
328 newConfigArgs, dirs := splitArgs(configArgs)
329 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700330 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700331 }
332
333 // Tidy only override all other specified targets.
334 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
335 if tidyOnly == "true" || tidyOnly == "1" {
336 configArgs = append(configArgs, "tidy_only")
337 } else {
338 configArgs = append(configArgs, targets...)
339 }
340
341 return configArgs
342}
343
344// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
345func convertToTarget(dir string, targetNamePrefix string) string {
346 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
347}
348
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700349// hasBuildFile returns true if dir contains an Android build file.
350func hasBuildFile(ctx Context, dir string) bool {
351 for _, buildFile := range buildFiles {
352 _, err := os.Stat(filepath.Join(dir, buildFile))
353 if err == nil {
354 return true
355 }
356 if !os.IsNotExist(err) {
357 ctx.Fatalf("Error retrieving the build file stats: %v", err)
358 }
359 }
360 return false
361}
362
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700363// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
364// in the current and any sub directory of dir. If a build file is not found, traverse the path
365// up by one directory and repeat again until either a build file is found or reached to the root
366// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
367// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700368func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700369 // If the string is empty or ".", assume it is top directory of the source tree.
370 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700371 return ""
372 }
373
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700374 found := false
375 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
376 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
377 if err != nil {
378 return err
379 }
380 if found {
381 return filepath.SkipDir
382 }
383 if info.IsDir() {
384 return nil
385 }
386 for _, buildFile := range buildFiles {
387 if info.Name() == buildFile {
388 found = true
389 return filepath.SkipDir
390 }
391 }
392 return nil
393 })
394 if err != nil {
395 ctx.Fatalf("Error finding Android build file: %v", err)
396 }
397
398 if found {
399 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700400 }
401 }
402
403 return ""
404}
405
406// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
407func splitArgs(args []string) (newArgs []string, dirs []string) {
408 specialArgs := map[string]bool{
409 "showcommands": true,
410 "snod": true,
411 "dist": true,
412 "checkbuild": true,
413 }
414
415 newArgs = []string{}
416 dirs = []string{}
417
418 for _, arg := range args {
419 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
420 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
421 newArgs = append(newArgs, arg)
422 continue
423 }
424
425 if _, ok := specialArgs[arg]; ok {
426 newArgs = append(newArgs, arg)
427 continue
428 }
429
430 dirs = append(dirs, arg)
431 }
432
433 return newArgs, dirs
434}
435
436// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
437// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
438// source root tree where the build action command was invoked. Each directory is validated if the
439// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700440func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700441 for _, dir := range dirs {
442 // The directory may have specified specific modules to build. ":" is the separator to separate
443 // the directory and the list of modules.
444 s := strings.Split(dir, ":")
445 l := len(s)
446 if l > 2 { // more than one ":" was specified.
447 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
448 }
449
450 dir = filepath.Join(relDir, s[0])
451 if _, err := os.Stat(dir); err != nil {
452 ctx.Fatalf("couldn't find directory %s", dir)
453 }
454
455 // Verify that if there are any targets specified after ":". Each target is separated by ",".
456 var newTargets []string
457 if l == 2 && s[1] != "" {
458 newTargets = strings.Split(s[1], ",")
459 if inList("", newTargets) {
460 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
461 }
462 }
463
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700464 // If there are specified targets to build in dir, an android build file must exist for the one
465 // shot build. For the non-targets case, find the appropriate build file and build all the
466 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700467 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700468 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700469 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
470 }
471 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700472 buildFile := findBuildFile(ctx, dir)
473 if buildFile == "" {
474 ctx.Fatalf("Build file not found for %s directory", dir)
475 }
476 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700477 }
478
Patrice Arruda13848222019-04-22 17:12:02 -0700479 targets = append(targets, newTargets...)
480 }
481
Dan Willemsence41e942019-07-29 23:39:30 -0700482 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700483}
484
Dan Willemsen9b587492017-07-10 22:13:00 -0700485func (c *configImpl) parseArgs(ctx Context, args []string) {
486 for i := 0; i < len(args); i++ {
487 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700488 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700489 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700490 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700491 } else if arg == "--skip-make" {
492 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700493 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700494 parseArgNum := func(def int) int {
495 if len(arg) > 2 {
496 p, err := strconv.ParseUint(arg[2:], 10, 31)
497 if err != nil {
498 ctx.Fatalf("Failed to parse %q: %v", arg, err)
499 }
500 return int(p)
501 } else if i+1 < len(args) {
502 p, err := strconv.ParseUint(args[i+1], 10, 31)
503 if err == nil {
504 i++
505 return int(p)
506 }
507 }
508 return def
509 }
510
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700511 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700512 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700513 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700514 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700515 } else {
516 ctx.Fatalln("Unknown option:", arg)
517 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700518 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
519 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700520 } else if arg == "dist" {
521 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700522 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700523 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800524 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700525 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700526 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700527 }
528 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700529}
530
Dan Willemsened869522018-01-08 14:58:46 -0800531func (c *configImpl) configureLocale(ctx Context) {
532 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
533 output, err := cmd.Output()
534
535 var locales []string
536 if err == nil {
537 locales = strings.Split(string(output), "\n")
538 } else {
539 // If we're unable to list the locales, let's assume en_US.UTF-8
540 locales = []string{"en_US.UTF-8"}
541 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
542 }
543
544 // gettext uses LANGUAGE, which is passed directly through
545
546 // For LANG and LC_*, only preserve the evaluated version of
547 // LC_MESSAGES
548 user_lang := ""
549 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
550 user_lang = lc_all
551 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
552 user_lang = lc_messages
553 } else if lang, ok := c.environ.Get("LANG"); ok {
554 user_lang = lang
555 }
556
557 c.environ.UnsetWithPrefix("LC_")
558
559 if user_lang != "" {
560 c.environ.Set("LC_MESSAGES", user_lang)
561 }
562
563 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
564 // for others)
565 if inList("C.UTF-8", locales) {
566 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500567 } else if inList("C.utf8", locales) {
568 // These normalize to the same thing
569 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800570 } else if inList("en_US.UTF-8", locales) {
571 c.environ.Set("LANG", "en_US.UTF-8")
572 } else if inList("en_US.utf8", locales) {
573 // These normalize to the same thing
574 c.environ.Set("LANG", "en_US.UTF-8")
575 } else {
576 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
577 }
578}
579
Dan Willemsen1e704462016-08-21 15:17:17 -0700580// Lunch configures the environment for a specific product similarly to the
581// `lunch` bash function.
582func (c *configImpl) Lunch(ctx Context, product, variant string) {
583 if variant != "eng" && variant != "userdebug" && variant != "user" {
584 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
585 }
586
587 c.environ.Set("TARGET_PRODUCT", product)
588 c.environ.Set("TARGET_BUILD_VARIANT", variant)
589 c.environ.Set("TARGET_BUILD_TYPE", "release")
590 c.environ.Unset("TARGET_BUILD_APPS")
591}
592
593// Tapas configures the environment to build one or more unbundled apps,
594// similarly to the `tapas` bash function.
595func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
596 if len(apps) == 0 {
597 apps = []string{"all"}
598 }
599 if variant == "" {
600 variant = "eng"
601 }
602
603 if variant != "eng" && variant != "userdebug" && variant != "user" {
604 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
605 }
606
607 var product string
608 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700609 case "arm", "":
610 product = "aosp_arm"
611 case "arm64":
612 product = "aosm_arm64"
613 case "mips":
614 product = "aosp_mips"
615 case "mips64":
616 product = "aosp_mips64"
617 case "x86":
618 product = "aosp_x86"
619 case "x86_64":
620 product = "aosp_x86_64"
621 default:
622 ctx.Fatalf("Invalid architecture: %q", arch)
623 }
624
625 c.environ.Set("TARGET_PRODUCT", product)
626 c.environ.Set("TARGET_BUILD_VARIANT", variant)
627 c.environ.Set("TARGET_BUILD_TYPE", "release")
628 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
629}
630
631func (c *configImpl) Environment() *Environment {
632 return c.environ
633}
634
635func (c *configImpl) Arguments() []string {
636 return c.arguments
637}
638
639func (c *configImpl) OutDir() string {
640 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700641 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700642 }
643 return "out"
644}
645
Dan Willemsen8a073a82017-02-04 17:30:44 -0800646func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700647 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800648}
649
Dan Willemsen1e704462016-08-21 15:17:17 -0700650func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700651 if c.skipMake {
652 return c.arguments
653 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700654 return c.ninjaArgs
655}
656
657func (c *configImpl) SoongOutDir() string {
658 return filepath.Join(c.OutDir(), "soong")
659}
660
Jeff Gastonefc1b412017-03-29 17:29:06 -0700661func (c *configImpl) TempDir() string {
662 return shared.TempDirForOutDir(c.SoongOutDir())
663}
664
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700665func (c *configImpl) FileListDir() string {
666 return filepath.Join(c.OutDir(), ".module_paths")
667}
668
Dan Willemsen1e704462016-08-21 15:17:17 -0700669func (c *configImpl) KatiSuffix() string {
670 if c.katiSuffix != "" {
671 return c.katiSuffix
672 }
673 panic("SetKatiSuffix has not been called")
674}
675
Colin Cross37193492017-11-16 17:55:00 -0800676// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
677// user is interested in additional checks at the expense of build time.
678func (c *configImpl) Checkbuild() bool {
679 return c.checkbuild
680}
681
Dan Willemsen8a073a82017-02-04 17:30:44 -0800682func (c *configImpl) Dist() bool {
683 return c.dist
684}
685
Dan Willemsen1e704462016-08-21 15:17:17 -0700686func (c *configImpl) IsVerbose() bool {
687 return c.verbose
688}
689
Dan Willemsene0879fc2017-08-04 15:06:27 -0700690func (c *configImpl) SkipMake() bool {
691 return c.skipMake
692}
693
Dan Willemsen1e704462016-08-21 15:17:17 -0700694func (c *configImpl) TargetProduct() string {
695 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
696 return v
697 }
698 panic("TARGET_PRODUCT is not defined")
699}
700
Dan Willemsen02781d52017-05-12 19:28:13 -0700701func (c *configImpl) TargetDevice() string {
702 return c.targetDevice
703}
704
705func (c *configImpl) SetTargetDevice(device string) {
706 c.targetDevice = device
707}
708
709func (c *configImpl) TargetBuildVariant() string {
710 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
711 return v
712 }
713 panic("TARGET_BUILD_VARIANT is not defined")
714}
715
Dan Willemsen1e704462016-08-21 15:17:17 -0700716func (c *configImpl) KatiArgs() []string {
717 return c.katiArgs
718}
719
720func (c *configImpl) Parallel() int {
721 return c.parallel
722}
723
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800724func (c *configImpl) TotalRAM() uint64 {
725 return c.totalRAM
726}
727
Dan Willemsen1e704462016-08-21 15:17:17 -0700728func (c *configImpl) UseGoma() bool {
729 if v, ok := c.environ.Get("USE_GOMA"); ok {
730 v = strings.TrimSpace(v)
731 if v != "" && v != "false" {
732 return true
733 }
734 }
735 return false
736}
737
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900738func (c *configImpl) StartGoma() bool {
739 if !c.UseGoma() {
740 return false
741 }
742
743 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
744 v = strings.TrimSpace(v)
745 if v != "" && v != "false" {
746 return false
747 }
748 }
749 return true
750}
751
Ramy Medhatbbf25672019-07-17 12:30:04 +0000752func (c *configImpl) UseRBE() bool {
753 if v, ok := c.environ.Get("USE_RBE"); ok {
754 v = strings.TrimSpace(v)
755 if v != "" && v != "false" {
756 return true
757 }
758 }
759 return false
760}
761
762func (c *configImpl) StartRBE() bool {
763 if !c.UseRBE() {
764 return false
765 }
766
767 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
768 v = strings.TrimSpace(v)
769 if v != "" && v != "false" {
770 return false
771 }
772 }
773 return true
774}
775
Colin Cross9016b912019-11-11 14:57:42 -0800776func (c *configImpl) UseRemoteBuild() bool {
777 return c.UseGoma() || c.UseRBE()
778}
779
Dan Willemsen1e704462016-08-21 15:17:17 -0700780// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700781// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700782// still limited by Parallel()
783func (c *configImpl) RemoteParallel() int {
784 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
785 if i, err := strconv.Atoi(v); err == nil {
786 return i
787 }
788 }
789 return 500
790}
791
792func (c *configImpl) SetKatiArgs(args []string) {
793 c.katiArgs = args
794}
795
796func (c *configImpl) SetNinjaArgs(args []string) {
797 c.ninjaArgs = args
798}
799
800func (c *configImpl) SetKatiSuffix(suffix string) {
801 c.katiSuffix = suffix
802}
803
Dan Willemsene0879fc2017-08-04 15:06:27 -0700804func (c *configImpl) LastKatiSuffixFile() string {
805 return filepath.Join(c.OutDir(), "last_kati_suffix")
806}
807
808func (c *configImpl) HasKatiSuffix() bool {
809 return c.katiSuffix != ""
810}
811
Dan Willemsen1e704462016-08-21 15:17:17 -0700812func (c *configImpl) KatiEnvFile() string {
813 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
814}
815
Dan Willemsen29971232018-09-26 14:58:30 -0700816func (c *configImpl) KatiBuildNinjaFile() string {
817 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700818}
819
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700820func (c *configImpl) KatiPackageNinjaFile() string {
821 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
822}
823
Dan Willemsen1e704462016-08-21 15:17:17 -0700824func (c *configImpl) SoongNinjaFile() string {
825 return filepath.Join(c.SoongOutDir(), "build.ninja")
826}
827
828func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700829 if c.katiSuffix == "" {
830 return filepath.Join(c.OutDir(), "combined.ninja")
831 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700832 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
833}
834
835func (c *configImpl) SoongAndroidMk() string {
836 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
837}
838
839func (c *configImpl) SoongMakeVarsMk() string {
840 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
841}
842
Dan Willemsenf052f782017-05-18 15:29:04 -0700843func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700844 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700845}
846
Dan Willemsen02781d52017-05-12 19:28:13 -0700847func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700848 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
849}
850
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700851func (c *configImpl) KatiPackageMkDir() string {
852 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
853}
854
Dan Willemsenf052f782017-05-18 15:29:04 -0700855func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700856 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700857}
858
859func (c *configImpl) HostOut() string {
860 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
861}
862
863// This probably needs to be multi-valued, so not exporting it for now
864func (c *configImpl) hostCrossOut() string {
865 if runtime.GOOS == "linux" {
866 return filepath.Join(c.hostOutRoot(), "windows-x86")
867 } else {
868 return ""
869 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700870}
871
Dan Willemsen1e704462016-08-21 15:17:17 -0700872func (c *configImpl) HostPrebuiltTag() string {
873 if runtime.GOOS == "linux" {
874 return "linux-x86"
875 } else if runtime.GOOS == "darwin" {
876 return "darwin-x86"
877 } else {
878 panic("Unsupported OS")
879 }
880}
Dan Willemsenf173d592017-04-27 14:28:00 -0700881
Dan Willemsen8122bd52017-10-12 20:20:41 -0700882func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700883 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
884 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700885 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
886 if _, err := os.Stat(asan); err == nil {
887 return asan
888 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700889 }
890 }
891 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
892}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700893
894func (c *configImpl) SetBuildBrokenDupRules(val bool) {
895 c.brokenDupRules = val
896}
897
898func (c *configImpl) BuildBrokenDupRules() bool {
899 return c.brokenDupRules
900}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700901
Dan Willemsen25e6f092019-04-09 10:22:43 -0700902func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
903 c.brokenUsesNetwork = val
904}
905
906func (c *configImpl) BuildBrokenUsesNetwork() bool {
907 return c.brokenUsesNetwork
908}
909
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700910func (c *configImpl) SetTargetDeviceDir(dir string) {
911 c.targetDeviceDir = dir
912}
913
914func (c *configImpl) TargetDeviceDir() string {
915 return c.targetDeviceDir
916}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700917
918func (c *configImpl) SetPdkBuild(pdk bool) {
919 c.pdkBuild = pdk
920}
921
922func (c *configImpl) IsPdkBuild() bool {
923 return c.pdkBuild
924}