blob: 434047bc5b5ec7b2aeca4e726e606f5a254737cf [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 (
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080018 "io/ioutil"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080019 "log"
20 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070021 "path/filepath"
22 "runtime"
23 "strconv"
24 "strings"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080025 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070026
27 "android/soong/shared"
Dan Willemsen1e704462016-08-21 15:17:17 -070028)
29
30type Config struct{ *configImpl }
31
32type configImpl struct {
33 // From the environment
34 arguments []string
35 goma bool
36 environ *Environment
Dan Willemsen2d31a442018-10-20 21:33:41 -070037 distDir string
Dan Willemsen1e704462016-08-21 15:17:17 -070038
39 // From the arguments
Colin Cross37193492017-11-16 17:55:00 -080040 parallel int
41 keepGoing int
42 verbose bool
43 checkbuild bool
44 dist bool
45 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070046
47 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070048 katiArgs []string
49 ninjaArgs []string
50 katiSuffix string
51 targetDevice string
52 targetDeviceDir string
Dan Willemsen3d60b112018-04-04 22:25:56 -070053
Dan Willemsend8aa39d2018-08-27 15:01:03 -070054 pdkBuild bool
55
Dan Willemsen60977462019-04-18 09:40:15 -070056 brokenDupRules bool
57 brokenUsesNetwork bool
Dan Willemsen18490112018-05-25 16:30:04 -070058
59 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070060}
61
Dan Willemsenc2af0be2017-01-20 14:10:01 -080062const srcDirFileCheck = "build/soong/root.bp"
63
Patrice Arruda9450d0b2019-07-08 11:06:46 -070064var buildFiles = []string{"Android.mk", "Android.bp"}
65
Patrice Arruda13848222019-04-22 17:12:02 -070066type BuildAction uint
67
68const (
69 // Builds all of the modules and their dependencies of a specified directory, relative to the root
70 // directory of the source tree.
71 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
72
73 // Builds all of the modules and their dependencies of a list of specified directories. All specified
74 // directories are relative to the root directory of the source tree.
75 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070076
77 // Build a list of specified modules. If none was specified, simply build the whole source tree.
78 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070079)
80
81// checkTopDir validates that the current directory is at the root directory of the source tree.
82func checkTopDir(ctx Context) {
83 if _, err := os.Stat(srcDirFileCheck); err != nil {
84 if os.IsNotExist(err) {
85 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
86 }
87 ctx.Fatalln("Error verifying tree state:", err)
88 }
89}
90
Dan Willemsen1e704462016-08-21 15:17:17 -070091func NewConfig(ctx Context, args ...string) Config {
92 ret := &configImpl{
93 environ: OsEnvironment(),
94 }
95
Dan Willemsen9b587492017-07-10 22:13:00 -070096 // Sane default matching ninja
97 ret.parallel = runtime.NumCPU() + 2
98 ret.keepGoing = 1
99
100 ret.parseArgs(ctx, args)
101
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800102 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700103 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
104 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
105 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800106 outDir := "out"
107 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
108 if wd, err := os.Getwd(); err != nil {
109 ctx.Fatalln("Failed to get working directory:", err)
110 } else {
111 outDir = filepath.Join(baseDir, filepath.Base(wd))
112 }
113 }
114 ret.environ.Set("OUT_DIR", outDir)
115 }
116
Dan Willemsen2d31a442018-10-20 21:33:41 -0700117 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
118 ret.distDir = filepath.Clean(distDir)
119 } else {
120 ret.distDir = filepath.Join(ret.OutDir(), "dist")
121 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700122
Dan Willemsen1e704462016-08-21 15:17:17 -0700123 ret.environ.Unset(
124 // We're already using it
125 "USE_SOONG_UI",
126
127 // We should never use GOROOT/GOPATH from the shell environment
128 "GOROOT",
129 "GOPATH",
130
131 // These should only come from Soong, not the environment.
132 "CLANG",
133 "CLANG_CXX",
134 "CCC_CC",
135 "CCC_CXX",
136
137 // Used by the goma compiler wrapper, but should only be set by
138 // gomacc
139 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800140
141 // We handle this above
142 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700143
Dan Willemsen2d31a442018-10-20 21:33:41 -0700144 // This is handled above too, and set for individual commands later
145 "DIST_DIR",
146
Dan Willemsen68a09852017-04-18 13:56:57 -0700147 // Variables that have caused problems in the past
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700148 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700149 "DISPLAY",
150 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700151 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700152 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700153
154 // Drop make flags
155 "MAKEFLAGS",
156 "MAKELEVEL",
157 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700158
159 // Set in envsetup.sh, reset in makefiles
160 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700161
162 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
163 "ANDROID_BUILD_TOP",
164 "ANDROID_HOST_OUT",
165 "ANDROID_PRODUCT_OUT",
166 "ANDROID_HOST_OUT_TESTCASES",
167 "ANDROID_TARGET_OUT_TESTCASES",
168 "ANDROID_TOOLCHAIN",
169 "ANDROID_TOOLCHAIN_2ND_ARCH",
170 "ANDROID_DEV_SCRIPTS",
171 "ANDROID_EMULATOR_PREBUILTS",
172 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700173
174 // Only set in multiproduct_kati after config generation
175 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700176 )
177
178 // Tell python not to spam the source tree with .pyc files.
179 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
180
Dan Willemsen32a669b2018-03-08 19:42:00 -0800181 ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
182
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800183 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700184 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800185
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700186 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
187 log.Println("You are building in a directory whose absolute path contains a space character:")
188 log.Println()
189 log.Printf("%q\n", srcDir)
190 log.Println()
191 log.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700192 }
193
194 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
195 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
196 log.Println()
197 log.Printf("%q\n", outDir)
198 log.Println()
199 log.Fatalln("Directory names containing spaces are not supported")
200 }
201
202 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
203 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
204 log.Println()
205 log.Printf("%q\n", distDir)
206 log.Println()
207 log.Fatalln("Directory names containing spaces are not supported")
208 }
209
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700210 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000211 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
212 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700213 javaHome := func() string {
214 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
215 return override
216 }
Colin Cross997262f2018-06-19 22:49:39 -0700217 return java9Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700218 }()
219 absJavaHome := absPath(ctx, javaHome)
220
Dan Willemsened869522018-01-08 14:58:46 -0800221 ret.configureLocale(ctx)
222
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700223 newPath := []string{filepath.Join(absJavaHome, "bin")}
224 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
225 newPath = append(newPath, path)
226 }
227 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
228 ret.environ.Set("JAVA_HOME", absJavaHome)
229 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000230 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
231 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700232 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
233
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800234 outDir := ret.OutDir()
235 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
236 var content string
237 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
238 content = buildDateTime
239 } else {
240 content = strconv.FormatInt(time.Now().Unix(), 10)
241 }
Nan Zhang17f27672018-12-12 16:01:49 -0800242 if ctx.Metrics != nil {
243 ctx.Metrics.SetBuildDateTime(content)
244 }
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800245 err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
246 if err != nil {
247 ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
248 }
249 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
250
Dan Willemsen9b587492017-07-10 22:13:00 -0700251 return Config{ret}
252}
253
Patrice Arruda13848222019-04-22 17:12:02 -0700254// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
255// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700256func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
257 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700258}
259
260// getConfigArgs processes the command arguments based on the build action and creates a set of new
261// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700262func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700263 // The next block of code verifies that the current directory is the root directory of the source
264 // tree. It then finds the relative path of dir based on the root directory of the source tree
265 // and verify that dir is inside of the source tree.
266 checkTopDir(ctx)
267 topDir, err := os.Getwd()
268 if err != nil {
269 ctx.Fatalf("Error retrieving top directory: %v", err)
270 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700271 dir, err = filepath.EvalSymlinks(dir)
272 if err != nil {
273 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
274 }
Patrice Arruda13848222019-04-22 17:12:02 -0700275 dir, err = filepath.Abs(dir)
276 if err != nil {
277 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
278 }
279 relDir, err := filepath.Rel(topDir, dir)
280 if err != nil {
281 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
282 }
283 // If there are ".." in the path, it's not in the source tree.
284 if strings.Contains(relDir, "..") {
285 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
286 }
287
288 configArgs := args[:]
289
290 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
291 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
292 targetNamePrefix := "MODULES-IN-"
293 if inList("GET-INSTALL-PATH", configArgs) {
294 targetNamePrefix = "GET-INSTALL-PATH-IN-"
295 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
296 }
297
Patrice Arruda13848222019-04-22 17:12:02 -0700298 var targets []string
299
300 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700301 case BUILD_MODULES:
302 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700303 case BUILD_MODULES_IN_A_DIRECTORY:
304 // If dir is the root source tree, all the modules are built of the source tree are built so
305 // no need to find the build file.
306 if topDir == dir {
307 break
308 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700309
Patrice Arruda13848222019-04-22 17:12:02 -0700310 buildFile := findBuildFile(ctx, relDir)
311 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700312 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700313 }
Patrice Arruda13848222019-04-22 17:12:02 -0700314 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
315 case BUILD_MODULES_IN_DIRECTORIES:
316 newConfigArgs, dirs := splitArgs(configArgs)
317 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700318 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700319 }
320
321 // Tidy only override all other specified targets.
322 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
323 if tidyOnly == "true" || tidyOnly == "1" {
324 configArgs = append(configArgs, "tidy_only")
325 } else {
326 configArgs = append(configArgs, targets...)
327 }
328
329 return configArgs
330}
331
332// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
333func convertToTarget(dir string, targetNamePrefix string) string {
334 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
335}
336
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700337// hasBuildFile returns true if dir contains an Android build file.
338func hasBuildFile(ctx Context, dir string) bool {
339 for _, buildFile := range buildFiles {
340 _, err := os.Stat(filepath.Join(dir, buildFile))
341 if err == nil {
342 return true
343 }
344 if !os.IsNotExist(err) {
345 ctx.Fatalf("Error retrieving the build file stats: %v", err)
346 }
347 }
348 return false
349}
350
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700351// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
352// in the current and any sub directory of dir. If a build file is not found, traverse the path
353// up by one directory and repeat again until either a build file is found or reached to the root
354// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
355// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700356func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700357 // If the string is empty or ".", assume it is top directory of the source tree.
358 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700359 return ""
360 }
361
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700362 found := false
363 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
364 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
365 if err != nil {
366 return err
367 }
368 if found {
369 return filepath.SkipDir
370 }
371 if info.IsDir() {
372 return nil
373 }
374 for _, buildFile := range buildFiles {
375 if info.Name() == buildFile {
376 found = true
377 return filepath.SkipDir
378 }
379 }
380 return nil
381 })
382 if err != nil {
383 ctx.Fatalf("Error finding Android build file: %v", err)
384 }
385
386 if found {
387 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700388 }
389 }
390
391 return ""
392}
393
394// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
395func splitArgs(args []string) (newArgs []string, dirs []string) {
396 specialArgs := map[string]bool{
397 "showcommands": true,
398 "snod": true,
399 "dist": true,
400 "checkbuild": true,
401 }
402
403 newArgs = []string{}
404 dirs = []string{}
405
406 for _, arg := range args {
407 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
408 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
409 newArgs = append(newArgs, arg)
410 continue
411 }
412
413 if _, ok := specialArgs[arg]; ok {
414 newArgs = append(newArgs, arg)
415 continue
416 }
417
418 dirs = append(dirs, arg)
419 }
420
421 return newArgs, dirs
422}
423
424// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
425// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
426// source root tree where the build action command was invoked. Each directory is validated if the
427// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700428func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700429 for _, dir := range dirs {
430 // The directory may have specified specific modules to build. ":" is the separator to separate
431 // the directory and the list of modules.
432 s := strings.Split(dir, ":")
433 l := len(s)
434 if l > 2 { // more than one ":" was specified.
435 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
436 }
437
438 dir = filepath.Join(relDir, s[0])
439 if _, err := os.Stat(dir); err != nil {
440 ctx.Fatalf("couldn't find directory %s", dir)
441 }
442
443 // Verify that if there are any targets specified after ":". Each target is separated by ",".
444 var newTargets []string
445 if l == 2 && s[1] != "" {
446 newTargets = strings.Split(s[1], ",")
447 if inList("", newTargets) {
448 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
449 }
450 }
451
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700452 // If there are specified targets to build in dir, an android build file must exist for the one
453 // shot build. For the non-targets case, find the appropriate build file and build all the
454 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700455 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700456 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700457 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
458 }
459 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700460 buildFile := findBuildFile(ctx, dir)
461 if buildFile == "" {
462 ctx.Fatalf("Build file not found for %s directory", dir)
463 }
464 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700465 }
466
Patrice Arruda13848222019-04-22 17:12:02 -0700467 targets = append(targets, newTargets...)
468 }
469
Dan Willemsence41e942019-07-29 23:39:30 -0700470 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700471}
472
Dan Willemsen9b587492017-07-10 22:13:00 -0700473func (c *configImpl) parseArgs(ctx Context, args []string) {
474 for i := 0; i < len(args); i++ {
475 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700476 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700477 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700478 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700479 } else if arg == "--skip-make" {
480 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700481 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700482 parseArgNum := func(def int) int {
483 if len(arg) > 2 {
484 p, err := strconv.ParseUint(arg[2:], 10, 31)
485 if err != nil {
486 ctx.Fatalf("Failed to parse %q: %v", arg, err)
487 }
488 return int(p)
489 } else if i+1 < len(args) {
490 p, err := strconv.ParseUint(args[i+1], 10, 31)
491 if err == nil {
492 i++
493 return int(p)
494 }
495 }
496 return def
497 }
498
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700499 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700500 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700501 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700502 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700503 } else {
504 ctx.Fatalln("Unknown option:", arg)
505 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700506 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
507 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700508 } else if arg == "dist" {
509 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700510 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700511 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800512 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700513 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700514 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700515 }
516 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700517}
518
Dan Willemsened869522018-01-08 14:58:46 -0800519func (c *configImpl) configureLocale(ctx Context) {
520 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
521 output, err := cmd.Output()
522
523 var locales []string
524 if err == nil {
525 locales = strings.Split(string(output), "\n")
526 } else {
527 // If we're unable to list the locales, let's assume en_US.UTF-8
528 locales = []string{"en_US.UTF-8"}
529 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
530 }
531
532 // gettext uses LANGUAGE, which is passed directly through
533
534 // For LANG and LC_*, only preserve the evaluated version of
535 // LC_MESSAGES
536 user_lang := ""
537 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
538 user_lang = lc_all
539 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
540 user_lang = lc_messages
541 } else if lang, ok := c.environ.Get("LANG"); ok {
542 user_lang = lang
543 }
544
545 c.environ.UnsetWithPrefix("LC_")
546
547 if user_lang != "" {
548 c.environ.Set("LC_MESSAGES", user_lang)
549 }
550
551 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
552 // for others)
553 if inList("C.UTF-8", locales) {
554 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500555 } else if inList("C.utf8", locales) {
556 // These normalize to the same thing
557 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800558 } else if inList("en_US.UTF-8", locales) {
559 c.environ.Set("LANG", "en_US.UTF-8")
560 } else if inList("en_US.utf8", locales) {
561 // These normalize to the same thing
562 c.environ.Set("LANG", "en_US.UTF-8")
563 } else {
564 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
565 }
566}
567
Dan Willemsen1e704462016-08-21 15:17:17 -0700568// Lunch configures the environment for a specific product similarly to the
569// `lunch` bash function.
570func (c *configImpl) Lunch(ctx Context, product, variant string) {
571 if variant != "eng" && variant != "userdebug" && variant != "user" {
572 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
573 }
574
575 c.environ.Set("TARGET_PRODUCT", product)
576 c.environ.Set("TARGET_BUILD_VARIANT", variant)
577 c.environ.Set("TARGET_BUILD_TYPE", "release")
578 c.environ.Unset("TARGET_BUILD_APPS")
579}
580
581// Tapas configures the environment to build one or more unbundled apps,
582// similarly to the `tapas` bash function.
583func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
584 if len(apps) == 0 {
585 apps = []string{"all"}
586 }
587 if variant == "" {
588 variant = "eng"
589 }
590
591 if variant != "eng" && variant != "userdebug" && variant != "user" {
592 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
593 }
594
595 var product string
596 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700597 case "arm", "":
598 product = "aosp_arm"
599 case "arm64":
600 product = "aosm_arm64"
601 case "mips":
602 product = "aosp_mips"
603 case "mips64":
604 product = "aosp_mips64"
605 case "x86":
606 product = "aosp_x86"
607 case "x86_64":
608 product = "aosp_x86_64"
609 default:
610 ctx.Fatalf("Invalid architecture: %q", arch)
611 }
612
613 c.environ.Set("TARGET_PRODUCT", product)
614 c.environ.Set("TARGET_BUILD_VARIANT", variant)
615 c.environ.Set("TARGET_BUILD_TYPE", "release")
616 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
617}
618
619func (c *configImpl) Environment() *Environment {
620 return c.environ
621}
622
623func (c *configImpl) Arguments() []string {
624 return c.arguments
625}
626
627func (c *configImpl) OutDir() string {
628 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700629 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700630 }
631 return "out"
632}
633
Dan Willemsen8a073a82017-02-04 17:30:44 -0800634func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700635 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800636}
637
Dan Willemsen1e704462016-08-21 15:17:17 -0700638func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700639 if c.skipMake {
640 return c.arguments
641 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700642 return c.ninjaArgs
643}
644
645func (c *configImpl) SoongOutDir() string {
646 return filepath.Join(c.OutDir(), "soong")
647}
648
Jeff Gastonefc1b412017-03-29 17:29:06 -0700649func (c *configImpl) TempDir() string {
650 return shared.TempDirForOutDir(c.SoongOutDir())
651}
652
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700653func (c *configImpl) FileListDir() string {
654 return filepath.Join(c.OutDir(), ".module_paths")
655}
656
Dan Willemsen1e704462016-08-21 15:17:17 -0700657func (c *configImpl) KatiSuffix() string {
658 if c.katiSuffix != "" {
659 return c.katiSuffix
660 }
661 panic("SetKatiSuffix has not been called")
662}
663
Colin Cross37193492017-11-16 17:55:00 -0800664// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
665// user is interested in additional checks at the expense of build time.
666func (c *configImpl) Checkbuild() bool {
667 return c.checkbuild
668}
669
Dan Willemsen8a073a82017-02-04 17:30:44 -0800670func (c *configImpl) Dist() bool {
671 return c.dist
672}
673
Dan Willemsen1e704462016-08-21 15:17:17 -0700674func (c *configImpl) IsVerbose() bool {
675 return c.verbose
676}
677
Dan Willemsene0879fc2017-08-04 15:06:27 -0700678func (c *configImpl) SkipMake() bool {
679 return c.skipMake
680}
681
Dan Willemsen1e704462016-08-21 15:17:17 -0700682func (c *configImpl) TargetProduct() string {
683 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
684 return v
685 }
686 panic("TARGET_PRODUCT is not defined")
687}
688
Dan Willemsen02781d52017-05-12 19:28:13 -0700689func (c *configImpl) TargetDevice() string {
690 return c.targetDevice
691}
692
693func (c *configImpl) SetTargetDevice(device string) {
694 c.targetDevice = device
695}
696
697func (c *configImpl) TargetBuildVariant() string {
698 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
699 return v
700 }
701 panic("TARGET_BUILD_VARIANT is not defined")
702}
703
Dan Willemsen1e704462016-08-21 15:17:17 -0700704func (c *configImpl) KatiArgs() []string {
705 return c.katiArgs
706}
707
708func (c *configImpl) Parallel() int {
709 return c.parallel
710}
711
712func (c *configImpl) UseGoma() bool {
713 if v, ok := c.environ.Get("USE_GOMA"); ok {
714 v = strings.TrimSpace(v)
715 if v != "" && v != "false" {
716 return true
717 }
718 }
719 return false
720}
721
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900722func (c *configImpl) StartGoma() bool {
723 if !c.UseGoma() {
724 return false
725 }
726
727 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
728 v = strings.TrimSpace(v)
729 if v != "" && v != "false" {
730 return false
731 }
732 }
733 return true
734}
735
Ramy Medhatbbf25672019-07-17 12:30:04 +0000736func (c *configImpl) UseRBE() bool {
737 if v, ok := c.environ.Get("USE_RBE"); ok {
738 v = strings.TrimSpace(v)
739 if v != "" && v != "false" {
740 return true
741 }
742 }
743 return false
744}
745
746func (c *configImpl) StartRBE() bool {
747 if !c.UseRBE() {
748 return false
749 }
750
751 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
752 v = strings.TrimSpace(v)
753 if v != "" && v != "false" {
754 return false
755 }
756 }
757 return true
758}
759
Dan Willemsen1e704462016-08-21 15:17:17 -0700760// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700761// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700762// still limited by Parallel()
763func (c *configImpl) RemoteParallel() int {
764 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
765 if i, err := strconv.Atoi(v); err == nil {
766 return i
767 }
768 }
769 return 500
770}
771
772func (c *configImpl) SetKatiArgs(args []string) {
773 c.katiArgs = args
774}
775
776func (c *configImpl) SetNinjaArgs(args []string) {
777 c.ninjaArgs = args
778}
779
780func (c *configImpl) SetKatiSuffix(suffix string) {
781 c.katiSuffix = suffix
782}
783
Dan Willemsene0879fc2017-08-04 15:06:27 -0700784func (c *configImpl) LastKatiSuffixFile() string {
785 return filepath.Join(c.OutDir(), "last_kati_suffix")
786}
787
788func (c *configImpl) HasKatiSuffix() bool {
789 return c.katiSuffix != ""
790}
791
Dan Willemsen1e704462016-08-21 15:17:17 -0700792func (c *configImpl) KatiEnvFile() string {
793 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
794}
795
Dan Willemsen29971232018-09-26 14:58:30 -0700796func (c *configImpl) KatiBuildNinjaFile() string {
797 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700798}
799
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700800func (c *configImpl) KatiPackageNinjaFile() string {
801 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
802}
803
Dan Willemsen1e704462016-08-21 15:17:17 -0700804func (c *configImpl) SoongNinjaFile() string {
805 return filepath.Join(c.SoongOutDir(), "build.ninja")
806}
807
808func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700809 if c.katiSuffix == "" {
810 return filepath.Join(c.OutDir(), "combined.ninja")
811 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700812 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
813}
814
815func (c *configImpl) SoongAndroidMk() string {
816 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
817}
818
819func (c *configImpl) SoongMakeVarsMk() string {
820 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
821}
822
Dan Willemsenf052f782017-05-18 15:29:04 -0700823func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700824 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700825}
826
Dan Willemsen02781d52017-05-12 19:28:13 -0700827func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700828 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
829}
830
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700831func (c *configImpl) KatiPackageMkDir() string {
832 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
833}
834
Dan Willemsenf052f782017-05-18 15:29:04 -0700835func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700836 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700837}
838
839func (c *configImpl) HostOut() string {
840 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
841}
842
843// This probably needs to be multi-valued, so not exporting it for now
844func (c *configImpl) hostCrossOut() string {
845 if runtime.GOOS == "linux" {
846 return filepath.Join(c.hostOutRoot(), "windows-x86")
847 } else {
848 return ""
849 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700850}
851
Dan Willemsen1e704462016-08-21 15:17:17 -0700852func (c *configImpl) HostPrebuiltTag() string {
853 if runtime.GOOS == "linux" {
854 return "linux-x86"
855 } else if runtime.GOOS == "darwin" {
856 return "darwin-x86"
857 } else {
858 panic("Unsupported OS")
859 }
860}
Dan Willemsenf173d592017-04-27 14:28:00 -0700861
Dan Willemsen8122bd52017-10-12 20:20:41 -0700862func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700863 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
864 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700865 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
866 if _, err := os.Stat(asan); err == nil {
867 return asan
868 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700869 }
870 }
871 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
872}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700873
874func (c *configImpl) SetBuildBrokenDupRules(val bool) {
875 c.brokenDupRules = val
876}
877
878func (c *configImpl) BuildBrokenDupRules() bool {
879 return c.brokenDupRules
880}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700881
Dan Willemsen25e6f092019-04-09 10:22:43 -0700882func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
883 c.brokenUsesNetwork = val
884}
885
886func (c *configImpl) BuildBrokenUsesNetwork() bool {
887 return c.brokenUsesNetwork
888}
889
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700890func (c *configImpl) SetTargetDeviceDir(dir string) {
891 c.targetDeviceDir = dir
892}
893
894func (c *configImpl) TargetDeviceDir() string {
895 return c.targetDeviceDir
896}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700897
898func (c *configImpl) SetPdkBuild(pdk bool) {
899 c.pdkBuild = pdk
900}
901
902func (c *configImpl) IsPdkBuild() bool {
903 return c.pdkBuild
904}