blob: 215b8d3b3decc5740c41a354b6540c742460f0fb [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
56 brokenDupRules bool
57 brokenPhonyTargets bool
Dan Willemsen47946562019-04-09 10:22:43 -070058 brokenUsesNetwork bool
Dan Willemsen18490112018-05-25 16:30:04 -070059
60 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070061}
62
Dan Willemsenc2af0be2017-01-20 14:10:01 -080063const srcDirFileCheck = "build/soong/root.bp"
64
Patrice Arruda9752df12019-07-08 11:06:46 -070065var buildFiles = []string{"Android.mk", "Android.bp"}
66
Patrice Arruda4a6e0482019-04-22 17:12:02 -070067type BuildAction uint
68
69const (
70 // Builds all of the modules and their dependencies of a specified directory, relative to the root
71 // directory of the source tree.
72 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
73
74 // Builds all of the modules and their dependencies of a list of specified directories. All specified
75 // directories are relative to the root directory of the source tree.
76 BUILD_MODULES_IN_DIRECTORIES
Patrice Arruda80ef1d52019-06-20 16:35:12 -070077
78 // Build a list of specified modules. If none was specified, simply build the whole source tree.
79 BUILD_MODULES
Patrice Arruda4a6e0482019-04-22 17:12:02 -070080)
81
82// checkTopDir validates that the current directory is at the root directory of the source tree.
83func checkTopDir(ctx Context) {
84 if _, err := os.Stat(srcDirFileCheck); err != nil {
85 if os.IsNotExist(err) {
86 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
87 }
88 ctx.Fatalln("Error verifying tree state:", err)
89 }
90}
91
Dan Willemsen1e704462016-08-21 15:17:17 -070092func NewConfig(ctx Context, args ...string) Config {
93 ret := &configImpl{
94 environ: OsEnvironment(),
95 }
96
Dan Willemsen9b587492017-07-10 22:13:00 -070097 // Sane default matching ninja
mydongistinye8bb87c2019-01-23 18:17:46 -080098 ret.parallel = runtime.NumCPU()
Dan Willemsen9b587492017-07-10 22:13:00 -070099 ret.keepGoing = 1
100
101 ret.parseArgs(ctx, args)
102
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800103 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -0700104 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
Adrian DCb0db3072018-08-10 17:07:47 +0200105 outDir := filepath.Clean(outDir)
106 if (!filepath.IsAbs(outDir)) {
107 outDir = filepath.Join(os.Getenv("TOP"), outDir)
108 }
109 ret.environ.Set("OUT_DIR", outDir)
Dan Willemsen02f3add2017-05-12 13:50:19 -0700110 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800111 outDir := "out"
112 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
113 if wd, err := os.Getwd(); err != nil {
114 ctx.Fatalln("Failed to get working directory:", err)
115 } else {
116 outDir = filepath.Join(baseDir, filepath.Base(wd))
117 }
Dan Pasanen46c3b9e2017-08-23 08:32:09 -0500118 } else {
119 outDir = filepath.Join(os.Getenv("TOP"), outDir)
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800120 }
121 ret.environ.Set("OUT_DIR", outDir)
122 }
123
Dan Willemsen2d31a442018-10-20 21:33:41 -0700124 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
125 ret.distDir = filepath.Clean(distDir)
126 } else {
127 ret.distDir = filepath.Join(ret.OutDir(), "dist")
128 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700129
Dan Willemsen1e704462016-08-21 15:17:17 -0700130 ret.environ.Unset(
131 // We're already using it
132 "USE_SOONG_UI",
133
134 // We should never use GOROOT/GOPATH from the shell environment
135 "GOROOT",
136 "GOPATH",
137
138 // These should only come from Soong, not the environment.
139 "CLANG",
140 "CLANG_CXX",
141 "CCC_CC",
142 "CCC_CXX",
143
144 // Used by the goma compiler wrapper, but should only be set by
145 // gomacc
146 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800147
148 // We handle this above
149 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700150
Dan Willemsen2d31a442018-10-20 21:33:41 -0700151 // This is handled above too, and set for individual commands later
152 "DIST_DIR",
153
Dan Willemsen68a09852017-04-18 13:56:57 -0700154 // Variables that have caused problems in the past
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700155 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700156 "DISPLAY",
157 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700158 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700159 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700160
161 // Drop make flags
162 "MAKEFLAGS",
163 "MAKELEVEL",
164 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700165
166 // Set in envsetup.sh, reset in makefiles
167 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700168
169 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
170 "ANDROID_BUILD_TOP",
171 "ANDROID_HOST_OUT",
172 "ANDROID_PRODUCT_OUT",
173 "ANDROID_HOST_OUT_TESTCASES",
174 "ANDROID_TARGET_OUT_TESTCASES",
175 "ANDROID_TOOLCHAIN",
176 "ANDROID_TOOLCHAIN_2ND_ARCH",
177 "ANDROID_DEV_SCRIPTS",
178 "ANDROID_EMULATOR_PREBUILTS",
179 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700180
181 // Only set in multiproduct_kati after config generation
182 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700183 )
184
185 // Tell python not to spam the source tree with .pyc files.
186 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
187
Dan Willemsen32a669b2018-03-08 19:42:00 -0800188 ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
189
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800190 // Precondition: the current directory is the top of the source tree
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700191 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800192
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700193 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
194 log.Println("You are building in a directory whose absolute path contains a space character:")
195 log.Println()
196 log.Printf("%q\n", srcDir)
197 log.Println()
198 log.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700199 }
200
201 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
202 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
203 log.Println()
204 log.Printf("%q\n", outDir)
205 log.Println()
206 log.Fatalln("Directory names containing spaces are not supported")
207 }
208
209 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
210 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
211 log.Println()
212 log.Printf("%q\n", distDir)
213 log.Println()
214 log.Fatalln("Directory names containing spaces are not supported")
215 }
216
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700217 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000218 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
219 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700220 javaHome := func() string {
221 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
222 return override
223 }
Colin Cross997262f2018-06-19 22:49:39 -0700224 return java9Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700225 }()
226 absJavaHome := absPath(ctx, javaHome)
227
Dan Willemsened869522018-01-08 14:58:46 -0800228 ret.configureLocale(ctx)
229
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700230 newPath := []string{filepath.Join(absJavaHome, "bin")}
231 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
232 newPath = append(newPath, path)
233 }
234 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
235 ret.environ.Set("JAVA_HOME", absJavaHome)
236 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000237 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
238 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700239 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
240
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800241 outDir := ret.OutDir()
242 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
243 var content string
244 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
245 content = buildDateTime
246 } else {
247 content = strconv.FormatInt(time.Now().Unix(), 10)
248 }
Nan Zhang17f27672018-12-12 16:01:49 -0800249 if ctx.Metrics != nil {
250 ctx.Metrics.SetBuildDateTime(content)
251 }
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800252 err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
253 if err != nil {
254 ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
255 }
256 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
257
Dan Willemsen9b587492017-07-10 22:13:00 -0700258 return Config{ret}
259}
260
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700261// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
262// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsened988012019-07-29 23:39:30 -0700263func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
264 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700265}
266
267// getConfigArgs processes the command arguments based on the build action and creates a set of new
268// arguments to be accepted by Config.
Dan Willemsened988012019-07-29 23:39:30 -0700269func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700270 // The next block of code verifies that the current directory is the root directory of the source
271 // tree. It then finds the relative path of dir based on the root directory of the source tree
272 // and verify that dir is inside of the source tree.
273 checkTopDir(ctx)
274 topDir, err := os.Getwd()
275 if err != nil {
276 ctx.Fatalf("Error retrieving top directory: %v", err)
277 }
Patrice Arruda6c76de92019-07-03 10:47:34 -0700278 dir, err = filepath.EvalSymlinks(dir)
279 if err != nil {
280 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
281 }
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700282 dir, err = filepath.Abs(dir)
283 if err != nil {
284 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
285 }
286 relDir, err := filepath.Rel(topDir, dir)
287 if err != nil {
288 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
289 }
290 // If there are ".." in the path, it's not in the source tree.
291 if strings.Contains(relDir, "..") {
292 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
293 }
294
295 configArgs := args[:]
296
297 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
298 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
299 targetNamePrefix := "MODULES-IN-"
300 if inList("GET-INSTALL-PATH", configArgs) {
301 targetNamePrefix = "GET-INSTALL-PATH-IN-"
302 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
303 }
304
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700305 var targets []string
306
307 switch action {
Patrice Arruda80ef1d52019-06-20 16:35:12 -0700308 case BUILD_MODULES:
309 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700310 case BUILD_MODULES_IN_A_DIRECTORY:
311 // If dir is the root source tree, all the modules are built of the source tree are built so
312 // no need to find the build file.
313 if topDir == dir {
314 break
315 }
Patrice Arrudaf6af71c2019-07-08 17:03:33 -0700316
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700317 buildFile := findBuildFile(ctx, relDir)
318 if buildFile == "" {
Patrice Arrudaf6af71c2019-07-08 17:03:33 -0700319 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700320 }
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700321 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
322 case BUILD_MODULES_IN_DIRECTORIES:
323 newConfigArgs, dirs := splitArgs(configArgs)
324 configArgs = newConfigArgs
Dan Willemsened988012019-07-29 23:39:30 -0700325 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700326 }
327
328 // Tidy only override all other specified targets.
329 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
330 if tidyOnly == "true" || tidyOnly == "1" {
331 configArgs = append(configArgs, "tidy_only")
332 } else {
333 configArgs = append(configArgs, targets...)
334 }
335
336 return configArgs
337}
338
339// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
340func convertToTarget(dir string, targetNamePrefix string) string {
341 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
342}
343
Patrice Arruda9752df12019-07-08 11:06:46 -0700344// hasBuildFile returns true if dir contains an Android build file.
345func hasBuildFile(ctx Context, dir string) bool {
346 for _, buildFile := range buildFiles {
347 _, err := os.Stat(filepath.Join(dir, buildFile))
348 if err == nil {
349 return true
350 }
351 if !os.IsNotExist(err) {
352 ctx.Fatalf("Error retrieving the build file stats: %v", err)
353 }
354 }
355 return false
356}
357
Patrice Arrudaf6af71c2019-07-08 17:03:33 -0700358// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
359// in the current and any sub directory of dir. If a build file is not found, traverse the path
360// up by one directory and repeat again until either a build file is found or reached to the root
361// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
362// string is returned.
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700363func findBuildFile(ctx Context, dir string) string {
Patrice Arrudaf6af71c2019-07-08 17:03:33 -0700364 // If the string is empty or ".", assume it is top directory of the source tree.
365 if dir == "" || dir == "." {
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700366 return ""
367 }
368
Patrice Arrudaf6af71c2019-07-08 17:03:33 -0700369 found := false
370 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
371 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
372 if err != nil {
373 return err
374 }
375 if found {
376 return filepath.SkipDir
377 }
378 if info.IsDir() {
379 return nil
380 }
381 for _, buildFile := range buildFiles {
382 if info.Name() == buildFile {
383 found = true
384 return filepath.SkipDir
385 }
386 }
387 return nil
388 })
389 if err != nil {
390 ctx.Fatalf("Error finding Android build file: %v", err)
391 }
392
393 if found {
394 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700395 }
396 }
397
398 return ""
399}
400
401// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
402func splitArgs(args []string) (newArgs []string, dirs []string) {
403 specialArgs := map[string]bool{
404 "showcommands": true,
405 "snod": true,
406 "dist": true,
407 "checkbuild": true,
408 }
409
410 newArgs = []string{}
411 dirs = []string{}
412
413 for _, arg := range args {
414 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
415 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
416 newArgs = append(newArgs, arg)
417 continue
418 }
419
420 if _, ok := specialArgs[arg]; ok {
421 newArgs = append(newArgs, arg)
422 continue
423 }
424
425 dirs = append(dirs, arg)
426 }
427
428 return newArgs, dirs
429}
430
431// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
432// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
433// source root tree where the build action command was invoked. Each directory is validated if the
434// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsened988012019-07-29 23:39:30 -0700435func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700436 for _, dir := range dirs {
437 // The directory may have specified specific modules to build. ":" is the separator to separate
438 // the directory and the list of modules.
439 s := strings.Split(dir, ":")
440 l := len(s)
441 if l > 2 { // more than one ":" was specified.
442 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
443 }
444
445 dir = filepath.Join(relDir, s[0])
446 if _, err := os.Stat(dir); err != nil {
447 ctx.Fatalf("couldn't find directory %s", dir)
448 }
449
450 // Verify that if there are any targets specified after ":". Each target is separated by ",".
451 var newTargets []string
452 if l == 2 && s[1] != "" {
453 newTargets = strings.Split(s[1], ",")
454 if inList("", newTargets) {
455 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
456 }
457 }
458
Patrice Arruda9752df12019-07-08 11:06:46 -0700459 // If there are specified targets to build in dir, an android build file must exist for the one
460 // shot build. For the non-targets case, find the appropriate build file and build all the
461 // modules in dir (or the closest one in the dir path).
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700462 if len(newTargets) > 0 {
Patrice Arruda9752df12019-07-08 11:06:46 -0700463 if !hasBuildFile(ctx, dir) {
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700464 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
465 }
466 } else {
Patrice Arruda9752df12019-07-08 11:06:46 -0700467 buildFile := findBuildFile(ctx, dir)
468 if buildFile == "" {
469 ctx.Fatalf("Build file not found for %s directory", dir)
470 }
471 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700472 }
473
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700474 targets = append(targets, newTargets...)
475 }
476
Dan Willemsened988012019-07-29 23:39:30 -0700477 return targets
Patrice Arruda4a6e0482019-04-22 17:12:02 -0700478}
479
Dan Willemsen9b587492017-07-10 22:13:00 -0700480func (c *configImpl) parseArgs(ctx Context, args []string) {
481 for i := 0; i < len(args); i++ {
482 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700483 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700484 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700485 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700486 } else if arg == "--skip-make" {
487 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700488 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700489 parseArgNum := func(def int) int {
490 if len(arg) > 2 {
491 p, err := strconv.ParseUint(arg[2:], 10, 31)
492 if err != nil {
493 ctx.Fatalf("Failed to parse %q: %v", arg, err)
494 }
495 return int(p)
496 } else if i+1 < len(args) {
497 p, err := strconv.ParseUint(args[i+1], 10, 31)
498 if err == nil {
499 i++
500 return int(p)
501 }
502 }
503 return def
504 }
505
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700506 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700507 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700508 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700509 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700510 } else {
511 ctx.Fatalln("Unknown option:", arg)
512 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700513 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
514 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700515 } else if arg == "dist" {
516 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700517 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700518 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800519 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700520 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700521 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700522 }
523 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700524}
525
Dan Willemsened869522018-01-08 14:58:46 -0800526func (c *configImpl) configureLocale(ctx Context) {
527 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
528 output, err := cmd.Output()
529
530 var locales []string
531 if err == nil {
532 locales = strings.Split(string(output), "\n")
533 } else {
534 // If we're unable to list the locales, let's assume en_US.UTF-8
535 locales = []string{"en_US.UTF-8"}
536 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
537 }
538
539 // gettext uses LANGUAGE, which is passed directly through
540
541 // For LANG and LC_*, only preserve the evaluated version of
542 // LC_MESSAGES
543 user_lang := ""
544 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
545 user_lang = lc_all
546 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
547 user_lang = lc_messages
548 } else if lang, ok := c.environ.Get("LANG"); ok {
549 user_lang = lang
550 }
551
552 c.environ.UnsetWithPrefix("LC_")
553
554 if user_lang != "" {
555 c.environ.Set("LC_MESSAGES", user_lang)
556 }
557
558 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
559 // for others)
560 if inList("C.UTF-8", locales) {
561 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500562 } else if inList("C.utf8", locales) {
563 // These normalize to the same thing
564 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800565 } else if inList("en_US.UTF-8", locales) {
566 c.environ.Set("LANG", "en_US.UTF-8")
567 } else if inList("en_US.utf8", locales) {
568 // These normalize to the same thing
569 c.environ.Set("LANG", "en_US.UTF-8")
570 } else {
571 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
572 }
573}
574
Dan Willemsen1e704462016-08-21 15:17:17 -0700575// Lunch configures the environment for a specific product similarly to the
576// `lunch` bash function.
577func (c *configImpl) Lunch(ctx Context, product, variant string) {
578 if variant != "eng" && variant != "userdebug" && variant != "user" {
579 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
580 }
581
582 c.environ.Set("TARGET_PRODUCT", product)
583 c.environ.Set("TARGET_BUILD_VARIANT", variant)
584 c.environ.Set("TARGET_BUILD_TYPE", "release")
585 c.environ.Unset("TARGET_BUILD_APPS")
586}
587
588// Tapas configures the environment to build one or more unbundled apps,
589// similarly to the `tapas` bash function.
590func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
591 if len(apps) == 0 {
592 apps = []string{"all"}
593 }
594 if variant == "" {
595 variant = "eng"
596 }
597
598 if variant != "eng" && variant != "userdebug" && variant != "user" {
599 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
600 }
601
602 var product string
603 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700604 case "arm", "":
605 product = "aosp_arm"
606 case "arm64":
607 product = "aosm_arm64"
608 case "mips":
609 product = "aosp_mips"
610 case "mips64":
611 product = "aosp_mips64"
612 case "x86":
613 product = "aosp_x86"
614 case "x86_64":
615 product = "aosp_x86_64"
616 default:
617 ctx.Fatalf("Invalid architecture: %q", arch)
618 }
619
620 c.environ.Set("TARGET_PRODUCT", product)
621 c.environ.Set("TARGET_BUILD_VARIANT", variant)
622 c.environ.Set("TARGET_BUILD_TYPE", "release")
623 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
624}
625
626func (c *configImpl) Environment() *Environment {
627 return c.environ
628}
629
630func (c *configImpl) Arguments() []string {
631 return c.arguments
632}
633
634func (c *configImpl) OutDir() string {
635 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arrudafac30932019-07-08 17:26:47 -0700636 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700637 }
638 return "out"
639}
640
Dan Willemsen8a073a82017-02-04 17:30:44 -0800641func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700642 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800643}
644
Dan Willemsen1e704462016-08-21 15:17:17 -0700645func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700646 if c.skipMake {
647 return c.arguments
648 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700649 return c.ninjaArgs
650}
651
652func (c *configImpl) SoongOutDir() string {
653 return filepath.Join(c.OutDir(), "soong")
654}
655
Jeff Gastonefc1b412017-03-29 17:29:06 -0700656func (c *configImpl) TempDir() string {
657 return shared.TempDirForOutDir(c.SoongOutDir())
658}
659
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700660func (c *configImpl) FileListDir() string {
661 return filepath.Join(c.OutDir(), ".module_paths")
662}
663
Dan Willemsen1e704462016-08-21 15:17:17 -0700664func (c *configImpl) KatiSuffix() string {
665 if c.katiSuffix != "" {
666 return c.katiSuffix
667 }
668 panic("SetKatiSuffix has not been called")
669}
670
Colin Cross37193492017-11-16 17:55:00 -0800671// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
672// user is interested in additional checks at the expense of build time.
673func (c *configImpl) Checkbuild() bool {
674 return c.checkbuild
675}
676
Dan Willemsen8a073a82017-02-04 17:30:44 -0800677func (c *configImpl) Dist() bool {
678 return c.dist
679}
680
Dan Willemsen1e704462016-08-21 15:17:17 -0700681func (c *configImpl) IsVerbose() bool {
682 return c.verbose
683}
684
Dan Willemsene0879fc2017-08-04 15:06:27 -0700685func (c *configImpl) SkipMake() bool {
686 return c.skipMake
687}
688
Dan Willemsen1e704462016-08-21 15:17:17 -0700689func (c *configImpl) TargetProduct() string {
690 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
691 return v
692 }
693 panic("TARGET_PRODUCT is not defined")
694}
695
Dan Willemsen02781d52017-05-12 19:28:13 -0700696func (c *configImpl) TargetDevice() string {
697 return c.targetDevice
698}
699
700func (c *configImpl) SetTargetDevice(device string) {
701 c.targetDevice = device
702}
703
704func (c *configImpl) TargetBuildVariant() string {
705 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
706 return v
707 }
708 panic("TARGET_BUILD_VARIANT is not defined")
709}
710
Dan Willemsen1e704462016-08-21 15:17:17 -0700711func (c *configImpl) KatiArgs() []string {
712 return c.katiArgs
713}
714
715func (c *configImpl) Parallel() int {
716 return c.parallel
717}
718
719func (c *configImpl) UseGoma() bool {
720 if v, ok := c.environ.Get("USE_GOMA"); ok {
721 v = strings.TrimSpace(v)
722 if v != "" && v != "false" {
723 return true
724 }
725 }
726 return false
727}
728
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900729func (c *configImpl) StartGoma() bool {
730 if !c.UseGoma() {
731 return false
732 }
733
734 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
735 v = strings.TrimSpace(v)
736 if v != "" && v != "false" {
737 return false
738 }
739 }
740 return true
741}
742
Dan Willemsen1e704462016-08-21 15:17:17 -0700743// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700744// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700745// still limited by Parallel()
746func (c *configImpl) RemoteParallel() int {
747 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
748 if i, err := strconv.Atoi(v); err == nil {
749 return i
750 }
751 }
752 return 500
753}
754
755func (c *configImpl) SetKatiArgs(args []string) {
756 c.katiArgs = args
757}
758
759func (c *configImpl) SetNinjaArgs(args []string) {
760 c.ninjaArgs = args
761}
762
763func (c *configImpl) SetKatiSuffix(suffix string) {
764 c.katiSuffix = suffix
765}
766
Dan Willemsene0879fc2017-08-04 15:06:27 -0700767func (c *configImpl) LastKatiSuffixFile() string {
768 return filepath.Join(c.OutDir(), "last_kati_suffix")
769}
770
771func (c *configImpl) HasKatiSuffix() bool {
772 return c.katiSuffix != ""
773}
774
Dan Willemsen1e704462016-08-21 15:17:17 -0700775func (c *configImpl) KatiEnvFile() string {
776 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
777}
778
Dan Willemsen29971232018-09-26 14:58:30 -0700779func (c *configImpl) KatiBuildNinjaFile() string {
780 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700781}
782
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700783func (c *configImpl) KatiPackageNinjaFile() string {
784 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
785}
786
Dan Willemsen1e704462016-08-21 15:17:17 -0700787func (c *configImpl) SoongNinjaFile() string {
788 return filepath.Join(c.SoongOutDir(), "build.ninja")
789}
790
791func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700792 if c.katiSuffix == "" {
793 return filepath.Join(c.OutDir(), "combined.ninja")
794 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700795 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
796}
797
798func (c *configImpl) SoongAndroidMk() string {
799 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
800}
801
802func (c *configImpl) SoongMakeVarsMk() string {
803 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
804}
805
Dan Willemsenf052f782017-05-18 15:29:04 -0700806func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700807 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700808}
809
Dan Willemsen02781d52017-05-12 19:28:13 -0700810func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700811 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
812}
813
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700814func (c *configImpl) KatiPackageMkDir() string {
815 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
816}
817
Dan Willemsenf052f782017-05-18 15:29:04 -0700818func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700819 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700820}
821
822func (c *configImpl) HostOut() string {
823 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
824}
825
826// This probably needs to be multi-valued, so not exporting it for now
827func (c *configImpl) hostCrossOut() string {
828 if runtime.GOOS == "linux" {
829 return filepath.Join(c.hostOutRoot(), "windows-x86")
830 } else {
831 return ""
832 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700833}
834
Dan Willemsen1e704462016-08-21 15:17:17 -0700835func (c *configImpl) HostPrebuiltTag() string {
836 if runtime.GOOS == "linux" {
837 return "linux-x86"
838 } else if runtime.GOOS == "darwin" {
839 return "darwin-x86"
840 } else {
841 panic("Unsupported OS")
842 }
843}
Dan Willemsenf173d592017-04-27 14:28:00 -0700844
Dan Willemsen8122bd52017-10-12 20:20:41 -0700845func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700846 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
847 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700848 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
849 if _, err := os.Stat(asan); err == nil {
850 return asan
851 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700852 }
853 }
854 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
855}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700856
857func (c *configImpl) SetBuildBrokenDupRules(val bool) {
858 c.brokenDupRules = val
859}
860
861func (c *configImpl) BuildBrokenDupRules() bool {
862 return c.brokenDupRules
863}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700864
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700865func (c *configImpl) SetBuildBrokenPhonyTargets(val bool) {
866 c.brokenPhonyTargets = val
867}
868
869func (c *configImpl) BuildBrokenPhonyTargets() bool {
870 return c.brokenPhonyTargets
871}
872
Dan Willemsen47946562019-04-09 10:22:43 -0700873func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
874 c.brokenUsesNetwork = val
875}
876
877func (c *configImpl) BuildBrokenUsesNetwork() bool {
878 return c.brokenUsesNetwork
879}
880
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700881func (c *configImpl) SetTargetDeviceDir(dir string) {
882 c.targetDeviceDir = dir
883}
884
885func (c *configImpl) TargetDeviceDir() string {
886 return c.targetDeviceDir
887}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700888
889func (c *configImpl) SetPdkBuild(pdk bool) {
890 c.pdkBuild = pdk
891}
892
893func (c *configImpl) IsPdkBuild() bool {
894 return c.pdkBuild
895}