blob: def3345e9612f3d7328a5f875bd32ed8699024da [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 "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"
Dan Willemsen1e704462016-08-21 15:17:17 -070027)
28
29type Config struct{ *configImpl }
30
31type configImpl struct {
32 // From the environment
33 arguments []string
34 goma bool
35 environ *Environment
Dan Willemsen2d31a442018-10-20 21:33:41 -070036 distDir 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 Willemsend8aa39d2018-08-27 15:01:03 -070053 pdkBuild bool
54
Dan Willemsen60977462019-04-18 09:40:15 -070055 brokenDupRules bool
56 brokenUsesNetwork bool
Dan Willemsen18490112018-05-25 16:30:04 -070057
58 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070059}
60
Dan Willemsenc2af0be2017-01-20 14:10:01 -080061const srcDirFileCheck = "build/soong/root.bp"
62
Patrice Arruda9450d0b2019-07-08 11:06:46 -070063var buildFiles = []string{"Android.mk", "Android.bp"}
64
Patrice Arruda13848222019-04-22 17:12:02 -070065type BuildAction uint
66
67const (
68 // Builds all of the modules and their dependencies of a specified directory, relative to the root
69 // directory of the source tree.
70 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
71
72 // Builds all of the modules and their dependencies of a list of specified directories. All specified
73 // directories are relative to the root directory of the source tree.
74 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda39282062019-06-20 16:35:12 -070075
76 // Build a list of specified modules. If none was specified, simply build the whole source tree.
77 BUILD_MODULES
Patrice Arruda13848222019-04-22 17:12:02 -070078)
79
80// checkTopDir validates that the current directory is at the root directory of the source tree.
81func checkTopDir(ctx Context) {
82 if _, err := os.Stat(srcDirFileCheck); err != nil {
83 if os.IsNotExist(err) {
84 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
85 }
86 ctx.Fatalln("Error verifying tree state:", err)
87 }
88}
89
Dan Willemsen1e704462016-08-21 15:17:17 -070090func NewConfig(ctx Context, args ...string) Config {
91 ret := &configImpl{
92 environ: OsEnvironment(),
93 }
94
Dan Willemsen9b587492017-07-10 22:13:00 -070095 // Sane default matching ninja
96 ret.parallel = runtime.NumCPU() + 2
97 ret.keepGoing = 1
98
99 ret.parseArgs(ctx, args)
100
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800101 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700102 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
103 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
104 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800105 outDir := "out"
106 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
107 if wd, err := os.Getwd(); err != nil {
108 ctx.Fatalln("Failed to get working directory:", err)
109 } else {
110 outDir = filepath.Join(baseDir, filepath.Base(wd))
111 }
112 }
113 ret.environ.Set("OUT_DIR", outDir)
114 }
115
Dan Willemsen2d31a442018-10-20 21:33:41 -0700116 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
117 ret.distDir = filepath.Clean(distDir)
118 } else {
119 ret.distDir = filepath.Join(ret.OutDir(), "dist")
120 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700121
Dan Willemsen1e704462016-08-21 15:17:17 -0700122 ret.environ.Unset(
123 // We're already using it
124 "USE_SOONG_UI",
125
126 // We should never use GOROOT/GOPATH from the shell environment
127 "GOROOT",
128 "GOPATH",
129
130 // These should only come from Soong, not the environment.
131 "CLANG",
132 "CLANG_CXX",
133 "CCC_CC",
134 "CCC_CXX",
135
136 // Used by the goma compiler wrapper, but should only be set by
137 // gomacc
138 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800139
140 // We handle this above
141 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700142
Dan Willemsen2d31a442018-10-20 21:33:41 -0700143 // This is handled above too, and set for individual commands later
144 "DIST_DIR",
145
Dan Willemsen68a09852017-04-18 13:56:57 -0700146 // Variables that have caused problems in the past
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700147 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700148 "DISPLAY",
149 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700150 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700151 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700152
153 // Drop make flags
154 "MAKEFLAGS",
155 "MAKELEVEL",
156 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700157
158 // Set in envsetup.sh, reset in makefiles
159 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700160
161 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
162 "ANDROID_BUILD_TOP",
163 "ANDROID_HOST_OUT",
164 "ANDROID_PRODUCT_OUT",
165 "ANDROID_HOST_OUT_TESTCASES",
166 "ANDROID_TARGET_OUT_TESTCASES",
167 "ANDROID_TOOLCHAIN",
168 "ANDROID_TOOLCHAIN_2ND_ARCH",
169 "ANDROID_DEV_SCRIPTS",
170 "ANDROID_EMULATOR_PREBUILTS",
171 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700172
173 // Only set in multiproduct_kati after config generation
174 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700175 )
176
177 // Tell python not to spam the source tree with .pyc files.
178 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
179
Dan Willemsen32a669b2018-03-08 19:42:00 -0800180 ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
181
Dan Willemsen70c1ff82019-08-21 14:56:13 -0700182 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
183 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
184 "llvm-binutils-stable/llvm-symbolizer")
185 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
186
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800187 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700188 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800189
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700190 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700191 ctx.Println("You are building in a directory whose absolute path contains a space character:")
192 ctx.Println()
193 ctx.Printf("%q\n", srcDir)
194 ctx.Println()
195 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700196 }
197
198 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700199 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
200 ctx.Println()
201 ctx.Printf("%q\n", outDir)
202 ctx.Println()
203 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700204 }
205
206 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700207 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
208 ctx.Println()
209 ctx.Printf("%q\n", distDir)
210 ctx.Println()
211 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700212 }
213
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700214 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000215 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
216 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700217 javaHome := func() string {
218 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
219 return override
220 }
Colin Cross997262f2018-06-19 22:49:39 -0700221 return java9Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700222 }()
223 absJavaHome := absPath(ctx, javaHome)
224
Dan Willemsened869522018-01-08 14:58:46 -0800225 ret.configureLocale(ctx)
226
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700227 newPath := []string{filepath.Join(absJavaHome, "bin")}
228 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
229 newPath = append(newPath, path)
230 }
231 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
232 ret.environ.Set("JAVA_HOME", absJavaHome)
233 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000234 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
235 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700236 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
237
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800238 outDir := ret.OutDir()
239 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
240 var content string
241 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
242 content = buildDateTime
243 } else {
244 content = strconv.FormatInt(time.Now().Unix(), 10)
245 }
Nan Zhang17f27672018-12-12 16:01:49 -0800246 if ctx.Metrics != nil {
247 ctx.Metrics.SetBuildDateTime(content)
248 }
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800249 err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
250 if err != nil {
251 ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
252 }
253 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
254
Dan Willemsen9b587492017-07-10 22:13:00 -0700255 return Config{ret}
256}
257
Patrice Arruda13848222019-04-22 17:12:02 -0700258// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
259// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700260func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
261 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700262}
263
264// getConfigArgs processes the command arguments based on the build action and creates a set of new
265// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700266func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700267 // The next block of code verifies that the current directory is the root directory of the source
268 // tree. It then finds the relative path of dir based on the root directory of the source tree
269 // and verify that dir is inside of the source tree.
270 checkTopDir(ctx)
271 topDir, err := os.Getwd()
272 if err != nil {
273 ctx.Fatalf("Error retrieving top directory: %v", err)
274 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700275 dir, err = filepath.EvalSymlinks(dir)
276 if err != nil {
277 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
278 }
Patrice Arruda13848222019-04-22 17:12:02 -0700279 dir, err = filepath.Abs(dir)
280 if err != nil {
281 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
282 }
283 relDir, err := filepath.Rel(topDir, dir)
284 if err != nil {
285 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
286 }
287 // If there are ".." in the path, it's not in the source tree.
288 if strings.Contains(relDir, "..") {
289 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
290 }
291
292 configArgs := args[:]
293
294 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
295 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
296 targetNamePrefix := "MODULES-IN-"
297 if inList("GET-INSTALL-PATH", configArgs) {
298 targetNamePrefix = "GET-INSTALL-PATH-IN-"
299 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
300 }
301
Patrice Arruda13848222019-04-22 17:12:02 -0700302 var targets []string
303
304 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700305 case BUILD_MODULES:
306 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700307 case BUILD_MODULES_IN_A_DIRECTORY:
308 // If dir is the root source tree, all the modules are built of the source tree are built so
309 // no need to find the build file.
310 if topDir == dir {
311 break
312 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700313
Patrice Arruda13848222019-04-22 17:12:02 -0700314 buildFile := findBuildFile(ctx, relDir)
315 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700316 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700317 }
Patrice Arruda13848222019-04-22 17:12:02 -0700318 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
319 case BUILD_MODULES_IN_DIRECTORIES:
320 newConfigArgs, dirs := splitArgs(configArgs)
321 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700322 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700323 }
324
325 // Tidy only override all other specified targets.
326 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
327 if tidyOnly == "true" || tidyOnly == "1" {
328 configArgs = append(configArgs, "tidy_only")
329 } else {
330 configArgs = append(configArgs, targets...)
331 }
332
333 return configArgs
334}
335
336// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
337func convertToTarget(dir string, targetNamePrefix string) string {
338 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
339}
340
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700341// hasBuildFile returns true if dir contains an Android build file.
342func hasBuildFile(ctx Context, dir string) bool {
343 for _, buildFile := range buildFiles {
344 _, err := os.Stat(filepath.Join(dir, buildFile))
345 if err == nil {
346 return true
347 }
348 if !os.IsNotExist(err) {
349 ctx.Fatalf("Error retrieving the build file stats: %v", err)
350 }
351 }
352 return false
353}
354
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700355// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
356// in the current and any sub directory of dir. If a build file is not found, traverse the path
357// up by one directory and repeat again until either a build file is found or reached to the root
358// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
359// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700360func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700361 // If the string is empty or ".", assume it is top directory of the source tree.
362 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700363 return ""
364 }
365
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700366 found := false
367 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
368 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
369 if err != nil {
370 return err
371 }
372 if found {
373 return filepath.SkipDir
374 }
375 if info.IsDir() {
376 return nil
377 }
378 for _, buildFile := range buildFiles {
379 if info.Name() == buildFile {
380 found = true
381 return filepath.SkipDir
382 }
383 }
384 return nil
385 })
386 if err != nil {
387 ctx.Fatalf("Error finding Android build file: %v", err)
388 }
389
390 if found {
391 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700392 }
393 }
394
395 return ""
396}
397
398// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
399func splitArgs(args []string) (newArgs []string, dirs []string) {
400 specialArgs := map[string]bool{
401 "showcommands": true,
402 "snod": true,
403 "dist": true,
404 "checkbuild": true,
405 }
406
407 newArgs = []string{}
408 dirs = []string{}
409
410 for _, arg := range args {
411 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
412 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
413 newArgs = append(newArgs, arg)
414 continue
415 }
416
417 if _, ok := specialArgs[arg]; ok {
418 newArgs = append(newArgs, arg)
419 continue
420 }
421
422 dirs = append(dirs, arg)
423 }
424
425 return newArgs, dirs
426}
427
428// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
429// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
430// source root tree where the build action command was invoked. Each directory is validated if the
431// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700432func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700433 for _, dir := range dirs {
434 // The directory may have specified specific modules to build. ":" is the separator to separate
435 // the directory and the list of modules.
436 s := strings.Split(dir, ":")
437 l := len(s)
438 if l > 2 { // more than one ":" was specified.
439 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
440 }
441
442 dir = filepath.Join(relDir, s[0])
443 if _, err := os.Stat(dir); err != nil {
444 ctx.Fatalf("couldn't find directory %s", dir)
445 }
446
447 // Verify that if there are any targets specified after ":". Each target is separated by ",".
448 var newTargets []string
449 if l == 2 && s[1] != "" {
450 newTargets = strings.Split(s[1], ",")
451 if inList("", newTargets) {
452 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
453 }
454 }
455
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700456 // If there are specified targets to build in dir, an android build file must exist for the one
457 // shot build. For the non-targets case, find the appropriate build file and build all the
458 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700459 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700460 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700461 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
462 }
463 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700464 buildFile := findBuildFile(ctx, dir)
465 if buildFile == "" {
466 ctx.Fatalf("Build file not found for %s directory", dir)
467 }
468 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700469 }
470
Patrice Arruda13848222019-04-22 17:12:02 -0700471 targets = append(targets, newTargets...)
472 }
473
Dan Willemsence41e942019-07-29 23:39:30 -0700474 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700475}
476
Dan Willemsen9b587492017-07-10 22:13:00 -0700477func (c *configImpl) parseArgs(ctx Context, args []string) {
478 for i := 0; i < len(args); i++ {
479 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700480 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700481 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700482 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700483 } else if arg == "--skip-make" {
484 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700485 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700486 parseArgNum := func(def int) int {
487 if len(arg) > 2 {
488 p, err := strconv.ParseUint(arg[2:], 10, 31)
489 if err != nil {
490 ctx.Fatalf("Failed to parse %q: %v", arg, err)
491 }
492 return int(p)
493 } else if i+1 < len(args) {
494 p, err := strconv.ParseUint(args[i+1], 10, 31)
495 if err == nil {
496 i++
497 return int(p)
498 }
499 }
500 return def
501 }
502
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700503 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700504 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700505 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700506 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700507 } else {
508 ctx.Fatalln("Unknown option:", arg)
509 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700510 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
511 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700512 } else if arg == "dist" {
513 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700514 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700515 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800516 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700517 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700518 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700519 }
520 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700521}
522
Dan Willemsened869522018-01-08 14:58:46 -0800523func (c *configImpl) configureLocale(ctx Context) {
524 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
525 output, err := cmd.Output()
526
527 var locales []string
528 if err == nil {
529 locales = strings.Split(string(output), "\n")
530 } else {
531 // If we're unable to list the locales, let's assume en_US.UTF-8
532 locales = []string{"en_US.UTF-8"}
533 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
534 }
535
536 // gettext uses LANGUAGE, which is passed directly through
537
538 // For LANG and LC_*, only preserve the evaluated version of
539 // LC_MESSAGES
540 user_lang := ""
541 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
542 user_lang = lc_all
543 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
544 user_lang = lc_messages
545 } else if lang, ok := c.environ.Get("LANG"); ok {
546 user_lang = lang
547 }
548
549 c.environ.UnsetWithPrefix("LC_")
550
551 if user_lang != "" {
552 c.environ.Set("LC_MESSAGES", user_lang)
553 }
554
555 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
556 // for others)
557 if inList("C.UTF-8", locales) {
558 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500559 } else if inList("C.utf8", locales) {
560 // These normalize to the same thing
561 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800562 } else if inList("en_US.UTF-8", locales) {
563 c.environ.Set("LANG", "en_US.UTF-8")
564 } else if inList("en_US.utf8", locales) {
565 // These normalize to the same thing
566 c.environ.Set("LANG", "en_US.UTF-8")
567 } else {
568 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
569 }
570}
571
Dan Willemsen1e704462016-08-21 15:17:17 -0700572// Lunch configures the environment for a specific product similarly to the
573// `lunch` bash function.
574func (c *configImpl) Lunch(ctx Context, product, variant string) {
575 if variant != "eng" && variant != "userdebug" && variant != "user" {
576 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
577 }
578
579 c.environ.Set("TARGET_PRODUCT", product)
580 c.environ.Set("TARGET_BUILD_VARIANT", variant)
581 c.environ.Set("TARGET_BUILD_TYPE", "release")
582 c.environ.Unset("TARGET_BUILD_APPS")
583}
584
585// Tapas configures the environment to build one or more unbundled apps,
586// similarly to the `tapas` bash function.
587func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
588 if len(apps) == 0 {
589 apps = []string{"all"}
590 }
591 if variant == "" {
592 variant = "eng"
593 }
594
595 if variant != "eng" && variant != "userdebug" && variant != "user" {
596 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
597 }
598
599 var product string
600 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700601 case "arm", "":
602 product = "aosp_arm"
603 case "arm64":
604 product = "aosm_arm64"
605 case "mips":
606 product = "aosp_mips"
607 case "mips64":
608 product = "aosp_mips64"
609 case "x86":
610 product = "aosp_x86"
611 case "x86_64":
612 product = "aosp_x86_64"
613 default:
614 ctx.Fatalf("Invalid architecture: %q", arch)
615 }
616
617 c.environ.Set("TARGET_PRODUCT", product)
618 c.environ.Set("TARGET_BUILD_VARIANT", variant)
619 c.environ.Set("TARGET_BUILD_TYPE", "release")
620 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
621}
622
623func (c *configImpl) Environment() *Environment {
624 return c.environ
625}
626
627func (c *configImpl) Arguments() []string {
628 return c.arguments
629}
630
631func (c *configImpl) OutDir() string {
632 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700633 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700634 }
635 return "out"
636}
637
Dan Willemsen8a073a82017-02-04 17:30:44 -0800638func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700639 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800640}
641
Dan Willemsen1e704462016-08-21 15:17:17 -0700642func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700643 if c.skipMake {
644 return c.arguments
645 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700646 return c.ninjaArgs
647}
648
649func (c *configImpl) SoongOutDir() string {
650 return filepath.Join(c.OutDir(), "soong")
651}
652
Jeff Gastonefc1b412017-03-29 17:29:06 -0700653func (c *configImpl) TempDir() string {
654 return shared.TempDirForOutDir(c.SoongOutDir())
655}
656
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700657func (c *configImpl) FileListDir() string {
658 return filepath.Join(c.OutDir(), ".module_paths")
659}
660
Dan Willemsen1e704462016-08-21 15:17:17 -0700661func (c *configImpl) KatiSuffix() string {
662 if c.katiSuffix != "" {
663 return c.katiSuffix
664 }
665 panic("SetKatiSuffix has not been called")
666}
667
Colin Cross37193492017-11-16 17:55:00 -0800668// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
669// user is interested in additional checks at the expense of build time.
670func (c *configImpl) Checkbuild() bool {
671 return c.checkbuild
672}
673
Dan Willemsen8a073a82017-02-04 17:30:44 -0800674func (c *configImpl) Dist() bool {
675 return c.dist
676}
677
Dan Willemsen1e704462016-08-21 15:17:17 -0700678func (c *configImpl) IsVerbose() bool {
679 return c.verbose
680}
681
Dan Willemsene0879fc2017-08-04 15:06:27 -0700682func (c *configImpl) SkipMake() bool {
683 return c.skipMake
684}
685
Dan Willemsen1e704462016-08-21 15:17:17 -0700686func (c *configImpl) TargetProduct() string {
687 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
688 return v
689 }
690 panic("TARGET_PRODUCT is not defined")
691}
692
Dan Willemsen02781d52017-05-12 19:28:13 -0700693func (c *configImpl) TargetDevice() string {
694 return c.targetDevice
695}
696
697func (c *configImpl) SetTargetDevice(device string) {
698 c.targetDevice = device
699}
700
701func (c *configImpl) TargetBuildVariant() string {
702 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
703 return v
704 }
705 panic("TARGET_BUILD_VARIANT is not defined")
706}
707
Dan Willemsen1e704462016-08-21 15:17:17 -0700708func (c *configImpl) KatiArgs() []string {
709 return c.katiArgs
710}
711
712func (c *configImpl) Parallel() int {
713 return c.parallel
714}
715
716func (c *configImpl) UseGoma() bool {
717 if v, ok := c.environ.Get("USE_GOMA"); ok {
718 v = strings.TrimSpace(v)
719 if v != "" && v != "false" {
720 return true
721 }
722 }
723 return false
724}
725
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900726func (c *configImpl) StartGoma() bool {
727 if !c.UseGoma() {
728 return false
729 }
730
731 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
732 v = strings.TrimSpace(v)
733 if v != "" && v != "false" {
734 return false
735 }
736 }
737 return true
738}
739
Ramy Medhatbbf25672019-07-17 12:30:04 +0000740func (c *configImpl) UseRBE() bool {
741 if v, ok := c.environ.Get("USE_RBE"); ok {
742 v = strings.TrimSpace(v)
743 if v != "" && v != "false" {
744 return true
745 }
746 }
747 return false
748}
749
750func (c *configImpl) StartRBE() bool {
751 if !c.UseRBE() {
752 return false
753 }
754
755 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
756 v = strings.TrimSpace(v)
757 if v != "" && v != "false" {
758 return false
759 }
760 }
761 return true
762}
763
Dan Willemsen1e704462016-08-21 15:17:17 -0700764// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700765// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700766// still limited by Parallel()
767func (c *configImpl) RemoteParallel() int {
768 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
769 if i, err := strconv.Atoi(v); err == nil {
770 return i
771 }
772 }
773 return 500
774}
775
776func (c *configImpl) SetKatiArgs(args []string) {
777 c.katiArgs = args
778}
779
780func (c *configImpl) SetNinjaArgs(args []string) {
781 c.ninjaArgs = args
782}
783
784func (c *configImpl) SetKatiSuffix(suffix string) {
785 c.katiSuffix = suffix
786}
787
Dan Willemsene0879fc2017-08-04 15:06:27 -0700788func (c *configImpl) LastKatiSuffixFile() string {
789 return filepath.Join(c.OutDir(), "last_kati_suffix")
790}
791
792func (c *configImpl) HasKatiSuffix() bool {
793 return c.katiSuffix != ""
794}
795
Dan Willemsen1e704462016-08-21 15:17:17 -0700796func (c *configImpl) KatiEnvFile() string {
797 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
798}
799
Dan Willemsen29971232018-09-26 14:58:30 -0700800func (c *configImpl) KatiBuildNinjaFile() string {
801 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700802}
803
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700804func (c *configImpl) KatiPackageNinjaFile() string {
805 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
806}
807
Dan Willemsen1e704462016-08-21 15:17:17 -0700808func (c *configImpl) SoongNinjaFile() string {
809 return filepath.Join(c.SoongOutDir(), "build.ninja")
810}
811
812func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700813 if c.katiSuffix == "" {
814 return filepath.Join(c.OutDir(), "combined.ninja")
815 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700816 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
817}
818
819func (c *configImpl) SoongAndroidMk() string {
820 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
821}
822
823func (c *configImpl) SoongMakeVarsMk() string {
824 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
825}
826
Dan Willemsenf052f782017-05-18 15:29:04 -0700827func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700828 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700829}
830
Dan Willemsen02781d52017-05-12 19:28:13 -0700831func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700832 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
833}
834
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700835func (c *configImpl) KatiPackageMkDir() string {
836 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
837}
838
Dan Willemsenf052f782017-05-18 15:29:04 -0700839func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700840 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700841}
842
843func (c *configImpl) HostOut() string {
844 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
845}
846
847// This probably needs to be multi-valued, so not exporting it for now
848func (c *configImpl) hostCrossOut() string {
849 if runtime.GOOS == "linux" {
850 return filepath.Join(c.hostOutRoot(), "windows-x86")
851 } else {
852 return ""
853 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700854}
855
Dan Willemsen1e704462016-08-21 15:17:17 -0700856func (c *configImpl) HostPrebuiltTag() string {
857 if runtime.GOOS == "linux" {
858 return "linux-x86"
859 } else if runtime.GOOS == "darwin" {
860 return "darwin-x86"
861 } else {
862 panic("Unsupported OS")
863 }
864}
Dan Willemsenf173d592017-04-27 14:28:00 -0700865
Dan Willemsen8122bd52017-10-12 20:20:41 -0700866func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700867 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
868 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700869 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
870 if _, err := os.Stat(asan); err == nil {
871 return asan
872 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700873 }
874 }
875 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
876}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700877
878func (c *configImpl) SetBuildBrokenDupRules(val bool) {
879 c.brokenDupRules = val
880}
881
882func (c *configImpl) BuildBrokenDupRules() bool {
883 return c.brokenDupRules
884}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700885
Dan Willemsen25e6f092019-04-09 10:22:43 -0700886func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
887 c.brokenUsesNetwork = val
888}
889
890func (c *configImpl) BuildBrokenUsesNetwork() bool {
891 return c.brokenUsesNetwork
892}
893
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700894func (c *configImpl) SetTargetDeviceDir(dir string) {
895 c.targetDeviceDir = dir
896}
897
898func (c *configImpl) TargetDeviceDir() string {
899 return c.targetDeviceDir
900}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700901
902func (c *configImpl) SetPdkBuild(pdk bool) {
903 c.pdkBuild = pdk
904}
905
906func (c *configImpl) IsPdkBuild() bool {
907 return c.pdkBuild
908}