blob: 876bfe0247e314681addb03afe123c575a61ea17 [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 Willemsen1c504d92019-11-18 19:13:53 +0000147 "BASH_ENV",
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 Willemsen70c1ff82019-08-21 14:56:13 -0700183 // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes
184 symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(),
185 "llvm-binutils-stable/llvm-symbolizer")
186 ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath))
187
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800188 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700189 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800190
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700191 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700192 ctx.Println("You are building in a directory whose absolute path contains a space character:")
193 ctx.Println()
194 ctx.Printf("%q\n", srcDir)
195 ctx.Println()
196 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700197 }
198
199 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700200 ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
201 ctx.Println()
202 ctx.Printf("%q\n", outDir)
203 ctx.Println()
204 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700205 }
206
207 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
Colin Cross1f6faeb2019-09-23 15:52:40 -0700208 ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
209 ctx.Println()
210 ctx.Printf("%q\n", distDir)
211 ctx.Println()
212 ctx.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700213 }
214
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700215 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000216 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
217 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Pete Gillin1f52e932019-10-09 17:10:08 +0100218 java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700219 javaHome := func() string {
220 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
221 return override
222 }
Pete Gillina7a3d642019-11-07 18:58:42 +0000223 if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" {
224 ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 11 toolchain is now the global default.")
Pete Gillin1f52e932019-10-09 17:10:08 +0100225 }
Pete Gillinabbcdda2019-10-28 16:15:33 +0000226 return java11Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700227 }()
228 absJavaHome := absPath(ctx, javaHome)
229
Dan Willemsened869522018-01-08 14:58:46 -0800230 ret.configureLocale(ctx)
231
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700232 newPath := []string{filepath.Join(absJavaHome, "bin")}
233 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
234 newPath = append(newPath, path)
235 }
Pete Gillin1f52e932019-10-09 17:10:08 +0100236
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700237 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
238 ret.environ.Set("JAVA_HOME", absJavaHome)
239 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000240 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
241 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Pete Gillin1f52e932019-10-09 17:10:08 +0100242 ret.environ.Set("ANDROID_JAVA11_HOME", java11Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700243 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
244
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800245 outDir := ret.OutDir()
246 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
247 var content string
248 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
249 content = buildDateTime
250 } else {
251 content = strconv.FormatInt(time.Now().Unix(), 10)
252 }
Nan Zhang17f27672018-12-12 16:01:49 -0800253 if ctx.Metrics != nil {
254 ctx.Metrics.SetBuildDateTime(content)
255 }
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800256 err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
257 if err != nil {
258 ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
259 }
260 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
261
Dan Willemsen9b587492017-07-10 22:13:00 -0700262 return Config{ret}
263}
264
Patrice Arruda13848222019-04-22 17:12:02 -0700265// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
266// processed based on the build action and extracts any arguments that belongs to the build action.
Dan Willemsence41e942019-07-29 23:39:30 -0700267func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config {
268 return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...)
Patrice Arruda13848222019-04-22 17:12:02 -0700269}
270
271// getConfigArgs processes the command arguments based on the build action and creates a set of new
272// arguments to be accepted by Config.
Dan Willemsence41e942019-07-29 23:39:30 -0700273func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string {
Patrice Arruda13848222019-04-22 17:12:02 -0700274 // The next block of code verifies that the current directory is the root directory of the source
275 // tree. It then finds the relative path of dir based on the root directory of the source tree
276 // and verify that dir is inside of the source tree.
277 checkTopDir(ctx)
278 topDir, err := os.Getwd()
279 if err != nil {
280 ctx.Fatalf("Error retrieving top directory: %v", err)
281 }
Patrice Arrudababa9a92019-07-03 10:47:34 -0700282 dir, err = filepath.EvalSymlinks(dir)
283 if err != nil {
284 ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err)
285 }
Patrice Arruda13848222019-04-22 17:12:02 -0700286 dir, err = filepath.Abs(dir)
287 if err != nil {
288 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
289 }
290 relDir, err := filepath.Rel(topDir, dir)
291 if err != nil {
292 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
293 }
294 // If there are ".." in the path, it's not in the source tree.
295 if strings.Contains(relDir, "..") {
296 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
297 }
298
299 configArgs := args[:]
300
301 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
302 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
303 targetNamePrefix := "MODULES-IN-"
304 if inList("GET-INSTALL-PATH", configArgs) {
305 targetNamePrefix = "GET-INSTALL-PATH-IN-"
306 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
307 }
308
Patrice Arruda13848222019-04-22 17:12:02 -0700309 var targets []string
310
311 switch action {
Patrice Arruda39282062019-06-20 16:35:12 -0700312 case BUILD_MODULES:
313 // No additional processing is required when building a list of specific modules or all modules.
Patrice Arruda13848222019-04-22 17:12:02 -0700314 case BUILD_MODULES_IN_A_DIRECTORY:
315 // If dir is the root source tree, all the modules are built of the source tree are built so
316 // no need to find the build file.
317 if topDir == dir {
318 break
319 }
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700320
Patrice Arruda13848222019-04-22 17:12:02 -0700321 buildFile := findBuildFile(ctx, relDir)
322 if buildFile == "" {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700323 ctx.Fatalf("Build file not found for %s directory", relDir)
Patrice Arruda13848222019-04-22 17:12:02 -0700324 }
Patrice Arruda13848222019-04-22 17:12:02 -0700325 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
326 case BUILD_MODULES_IN_DIRECTORIES:
327 newConfigArgs, dirs := splitArgs(configArgs)
328 configArgs = newConfigArgs
Dan Willemsence41e942019-07-29 23:39:30 -0700329 targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
Patrice Arruda13848222019-04-22 17:12:02 -0700330 }
331
332 // Tidy only override all other specified targets.
333 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
334 if tidyOnly == "true" || tidyOnly == "1" {
335 configArgs = append(configArgs, "tidy_only")
336 } else {
337 configArgs = append(configArgs, targets...)
338 }
339
340 return configArgs
341}
342
343// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
344func convertToTarget(dir string, targetNamePrefix string) string {
345 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
346}
347
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700348// hasBuildFile returns true if dir contains an Android build file.
349func hasBuildFile(ctx Context, dir string) bool {
350 for _, buildFile := range buildFiles {
351 _, err := os.Stat(filepath.Join(dir, buildFile))
352 if err == nil {
353 return true
354 }
355 if !os.IsNotExist(err) {
356 ctx.Fatalf("Error retrieving the build file stats: %v", err)
357 }
358 }
359 return false
360}
361
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700362// findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file
363// in the current and any sub directory of dir. If a build file is not found, traverse the path
364// up by one directory and repeat again until either a build file is found or reached to the root
365// source tree. The returned filename of build file is "Android.mk". If one was not found, a blank
366// string is returned.
Patrice Arruda13848222019-04-22 17:12:02 -0700367func findBuildFile(ctx Context, dir string) string {
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700368 // If the string is empty or ".", assume it is top directory of the source tree.
369 if dir == "" || dir == "." {
Patrice Arruda13848222019-04-22 17:12:02 -0700370 return ""
371 }
372
Patrice Arruda0dcf27f2019-07-08 17:03:33 -0700373 found := false
374 for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) {
375 err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error {
376 if err != nil {
377 return err
378 }
379 if found {
380 return filepath.SkipDir
381 }
382 if info.IsDir() {
383 return nil
384 }
385 for _, buildFile := range buildFiles {
386 if info.Name() == buildFile {
387 found = true
388 return filepath.SkipDir
389 }
390 }
391 return nil
392 })
393 if err != nil {
394 ctx.Fatalf("Error finding Android build file: %v", err)
395 }
396
397 if found {
398 return filepath.Join(buildDir, "Android.mk")
Patrice Arruda13848222019-04-22 17:12:02 -0700399 }
400 }
401
402 return ""
403}
404
405// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
406func splitArgs(args []string) (newArgs []string, dirs []string) {
407 specialArgs := map[string]bool{
408 "showcommands": true,
409 "snod": true,
410 "dist": true,
411 "checkbuild": true,
412 }
413
414 newArgs = []string{}
415 dirs = []string{}
416
417 for _, arg := range args {
418 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
419 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
420 newArgs = append(newArgs, arg)
421 continue
422 }
423
424 if _, ok := specialArgs[arg]; ok {
425 newArgs = append(newArgs, arg)
426 continue
427 }
428
429 dirs = append(dirs, arg)
430 }
431
432 return newArgs, dirs
433}
434
435// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
436// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
437// source root tree where the build action command was invoked. Each directory is validated if the
438// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
Dan Willemsence41e942019-07-29 23:39:30 -0700439func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) {
Patrice Arruda13848222019-04-22 17:12:02 -0700440 for _, dir := range dirs {
441 // The directory may have specified specific modules to build. ":" is the separator to separate
442 // the directory and the list of modules.
443 s := strings.Split(dir, ":")
444 l := len(s)
445 if l > 2 { // more than one ":" was specified.
446 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
447 }
448
449 dir = filepath.Join(relDir, s[0])
450 if _, err := os.Stat(dir); err != nil {
451 ctx.Fatalf("couldn't find directory %s", dir)
452 }
453
454 // Verify that if there are any targets specified after ":". Each target is separated by ",".
455 var newTargets []string
456 if l == 2 && s[1] != "" {
457 newTargets = strings.Split(s[1], ",")
458 if inList("", newTargets) {
459 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
460 }
461 }
462
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700463 // If there are specified targets to build in dir, an android build file must exist for the one
464 // shot build. For the non-targets case, find the appropriate build file and build all the
465 // modules in dir (or the closest one in the dir path).
Patrice Arruda13848222019-04-22 17:12:02 -0700466 if len(newTargets) > 0 {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700467 if !hasBuildFile(ctx, dir) {
Patrice Arruda13848222019-04-22 17:12:02 -0700468 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
469 }
470 } else {
Patrice Arruda9450d0b2019-07-08 11:06:46 -0700471 buildFile := findBuildFile(ctx, dir)
472 if buildFile == "" {
473 ctx.Fatalf("Build file not found for %s directory", dir)
474 }
475 newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
Patrice Arruda13848222019-04-22 17:12:02 -0700476 }
477
Patrice Arruda13848222019-04-22 17:12:02 -0700478 targets = append(targets, newTargets...)
479 }
480
Dan Willemsence41e942019-07-29 23:39:30 -0700481 return targets
Patrice Arruda13848222019-04-22 17:12:02 -0700482}
483
Dan Willemsen9b587492017-07-10 22:13:00 -0700484func (c *configImpl) parseArgs(ctx Context, args []string) {
485 for i := 0; i < len(args); i++ {
486 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700487 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700488 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700489 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700490 } else if arg == "--skip-make" {
491 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700492 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700493 parseArgNum := func(def int) int {
494 if len(arg) > 2 {
495 p, err := strconv.ParseUint(arg[2:], 10, 31)
496 if err != nil {
497 ctx.Fatalf("Failed to parse %q: %v", arg, err)
498 }
499 return int(p)
500 } else if i+1 < len(args) {
501 p, err := strconv.ParseUint(args[i+1], 10, 31)
502 if err == nil {
503 i++
504 return int(p)
505 }
506 }
507 return def
508 }
509
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700510 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700511 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700512 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700513 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700514 } else {
515 ctx.Fatalln("Unknown option:", arg)
516 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700517 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
518 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700519 } else if arg == "dist" {
520 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700521 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700522 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800523 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700524 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700525 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700526 }
527 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700528}
529
Dan Willemsened869522018-01-08 14:58:46 -0800530func (c *configImpl) configureLocale(ctx Context) {
531 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
532 output, err := cmd.Output()
533
534 var locales []string
535 if err == nil {
536 locales = strings.Split(string(output), "\n")
537 } else {
538 // If we're unable to list the locales, let's assume en_US.UTF-8
539 locales = []string{"en_US.UTF-8"}
540 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
541 }
542
543 // gettext uses LANGUAGE, which is passed directly through
544
545 // For LANG and LC_*, only preserve the evaluated version of
546 // LC_MESSAGES
547 user_lang := ""
548 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
549 user_lang = lc_all
550 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
551 user_lang = lc_messages
552 } else if lang, ok := c.environ.Get("LANG"); ok {
553 user_lang = lang
554 }
555
556 c.environ.UnsetWithPrefix("LC_")
557
558 if user_lang != "" {
559 c.environ.Set("LC_MESSAGES", user_lang)
560 }
561
562 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
563 // for others)
564 if inList("C.UTF-8", locales) {
565 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500566 } else if inList("C.utf8", locales) {
567 // These normalize to the same thing
568 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800569 } else if inList("en_US.UTF-8", locales) {
570 c.environ.Set("LANG", "en_US.UTF-8")
571 } else if inList("en_US.utf8", locales) {
572 // These normalize to the same thing
573 c.environ.Set("LANG", "en_US.UTF-8")
574 } else {
575 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
576 }
577}
578
Dan Willemsen1e704462016-08-21 15:17:17 -0700579// Lunch configures the environment for a specific product similarly to the
580// `lunch` bash function.
581func (c *configImpl) Lunch(ctx Context, product, variant string) {
582 if variant != "eng" && variant != "userdebug" && variant != "user" {
583 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
584 }
585
586 c.environ.Set("TARGET_PRODUCT", product)
587 c.environ.Set("TARGET_BUILD_VARIANT", variant)
588 c.environ.Set("TARGET_BUILD_TYPE", "release")
589 c.environ.Unset("TARGET_BUILD_APPS")
590}
591
592// Tapas configures the environment to build one or more unbundled apps,
593// similarly to the `tapas` bash function.
594func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
595 if len(apps) == 0 {
596 apps = []string{"all"}
597 }
598 if variant == "" {
599 variant = "eng"
600 }
601
602 if variant != "eng" && variant != "userdebug" && variant != "user" {
603 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
604 }
605
606 var product string
607 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700608 case "arm", "":
609 product = "aosp_arm"
610 case "arm64":
611 product = "aosm_arm64"
612 case "mips":
613 product = "aosp_mips"
614 case "mips64":
615 product = "aosp_mips64"
616 case "x86":
617 product = "aosp_x86"
618 case "x86_64":
619 product = "aosp_x86_64"
620 default:
621 ctx.Fatalf("Invalid architecture: %q", arch)
622 }
623
624 c.environ.Set("TARGET_PRODUCT", product)
625 c.environ.Set("TARGET_BUILD_VARIANT", variant)
626 c.environ.Set("TARGET_BUILD_TYPE", "release")
627 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
628}
629
630func (c *configImpl) Environment() *Environment {
631 return c.environ
632}
633
634func (c *configImpl) Arguments() []string {
635 return c.arguments
636}
637
638func (c *configImpl) OutDir() string {
639 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Patrice Arruda19bd53e2019-07-08 17:26:47 -0700640 return outDir
Dan Willemsen1e704462016-08-21 15:17:17 -0700641 }
642 return "out"
643}
644
Dan Willemsen8a073a82017-02-04 17:30:44 -0800645func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700646 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800647}
648
Dan Willemsen1e704462016-08-21 15:17:17 -0700649func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700650 if c.skipMake {
651 return c.arguments
652 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700653 return c.ninjaArgs
654}
655
656func (c *configImpl) SoongOutDir() string {
657 return filepath.Join(c.OutDir(), "soong")
658}
659
Jeff Gastonefc1b412017-03-29 17:29:06 -0700660func (c *configImpl) TempDir() string {
661 return shared.TempDirForOutDir(c.SoongOutDir())
662}
663
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700664func (c *configImpl) FileListDir() string {
665 return filepath.Join(c.OutDir(), ".module_paths")
666}
667
Dan Willemsen1e704462016-08-21 15:17:17 -0700668func (c *configImpl) KatiSuffix() string {
669 if c.katiSuffix != "" {
670 return c.katiSuffix
671 }
672 panic("SetKatiSuffix has not been called")
673}
674
Colin Cross37193492017-11-16 17:55:00 -0800675// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
676// user is interested in additional checks at the expense of build time.
677func (c *configImpl) Checkbuild() bool {
678 return c.checkbuild
679}
680
Dan Willemsen8a073a82017-02-04 17:30:44 -0800681func (c *configImpl) Dist() bool {
682 return c.dist
683}
684
Dan Willemsen1e704462016-08-21 15:17:17 -0700685func (c *configImpl) IsVerbose() bool {
686 return c.verbose
687}
688
Dan Willemsene0879fc2017-08-04 15:06:27 -0700689func (c *configImpl) SkipMake() bool {
690 return c.skipMake
691}
692
Dan Willemsen1e704462016-08-21 15:17:17 -0700693func (c *configImpl) TargetProduct() string {
694 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
695 return v
696 }
697 panic("TARGET_PRODUCT is not defined")
698}
699
Dan Willemsen02781d52017-05-12 19:28:13 -0700700func (c *configImpl) TargetDevice() string {
701 return c.targetDevice
702}
703
704func (c *configImpl) SetTargetDevice(device string) {
705 c.targetDevice = device
706}
707
708func (c *configImpl) TargetBuildVariant() string {
709 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
710 return v
711 }
712 panic("TARGET_BUILD_VARIANT is not defined")
713}
714
Dan Willemsen1e704462016-08-21 15:17:17 -0700715func (c *configImpl) KatiArgs() []string {
716 return c.katiArgs
717}
718
719func (c *configImpl) Parallel() int {
720 return c.parallel
721}
722
723func (c *configImpl) UseGoma() bool {
724 if v, ok := c.environ.Get("USE_GOMA"); ok {
725 v = strings.TrimSpace(v)
726 if v != "" && v != "false" {
727 return true
728 }
729 }
730 return false
731}
732
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900733func (c *configImpl) StartGoma() bool {
734 if !c.UseGoma() {
735 return false
736 }
737
738 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
739 v = strings.TrimSpace(v)
740 if v != "" && v != "false" {
741 return false
742 }
743 }
744 return true
745}
746
Ramy Medhatbbf25672019-07-17 12:30:04 +0000747func (c *configImpl) UseRBE() bool {
748 if v, ok := c.environ.Get("USE_RBE"); ok {
749 v = strings.TrimSpace(v)
750 if v != "" && v != "false" {
751 return true
752 }
753 }
754 return false
755}
756
757func (c *configImpl) StartRBE() bool {
758 if !c.UseRBE() {
759 return false
760 }
761
762 if v, ok := c.environ.Get("NOSTART_RBE"); ok {
763 v = strings.TrimSpace(v)
764 if v != "" && v != "false" {
765 return false
766 }
767 }
768 return true
769}
770
Colin Cross9016b912019-11-11 14:57:42 -0800771func (c *configImpl) UseRemoteBuild() bool {
772 return c.UseGoma() || c.UseRBE()
773}
774
Dan Willemsen1e704462016-08-21 15:17:17 -0700775// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700776// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700777// still limited by Parallel()
778func (c *configImpl) RemoteParallel() int {
779 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
780 if i, err := strconv.Atoi(v); err == nil {
781 return i
782 }
783 }
784 return 500
785}
786
787func (c *configImpl) SetKatiArgs(args []string) {
788 c.katiArgs = args
789}
790
791func (c *configImpl) SetNinjaArgs(args []string) {
792 c.ninjaArgs = args
793}
794
795func (c *configImpl) SetKatiSuffix(suffix string) {
796 c.katiSuffix = suffix
797}
798
Dan Willemsene0879fc2017-08-04 15:06:27 -0700799func (c *configImpl) LastKatiSuffixFile() string {
800 return filepath.Join(c.OutDir(), "last_kati_suffix")
801}
802
803func (c *configImpl) HasKatiSuffix() bool {
804 return c.katiSuffix != ""
805}
806
Dan Willemsen1e704462016-08-21 15:17:17 -0700807func (c *configImpl) KatiEnvFile() string {
808 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
809}
810
Dan Willemsen29971232018-09-26 14:58:30 -0700811func (c *configImpl) KatiBuildNinjaFile() string {
812 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700813}
814
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700815func (c *configImpl) KatiPackageNinjaFile() string {
816 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
817}
818
Dan Willemsen1e704462016-08-21 15:17:17 -0700819func (c *configImpl) SoongNinjaFile() string {
820 return filepath.Join(c.SoongOutDir(), "build.ninja")
821}
822
823func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700824 if c.katiSuffix == "" {
825 return filepath.Join(c.OutDir(), "combined.ninja")
826 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700827 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
828}
829
830func (c *configImpl) SoongAndroidMk() string {
831 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
832}
833
834func (c *configImpl) SoongMakeVarsMk() string {
835 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
836}
837
Dan Willemsenf052f782017-05-18 15:29:04 -0700838func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700839 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700840}
841
Dan Willemsen02781d52017-05-12 19:28:13 -0700842func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700843 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
844}
845
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700846func (c *configImpl) KatiPackageMkDir() string {
847 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
848}
849
Dan Willemsenf052f782017-05-18 15:29:04 -0700850func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700851 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700852}
853
854func (c *configImpl) HostOut() string {
855 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
856}
857
858// This probably needs to be multi-valued, so not exporting it for now
859func (c *configImpl) hostCrossOut() string {
860 if runtime.GOOS == "linux" {
861 return filepath.Join(c.hostOutRoot(), "windows-x86")
862 } else {
863 return ""
864 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700865}
866
Dan Willemsen1e704462016-08-21 15:17:17 -0700867func (c *configImpl) HostPrebuiltTag() string {
868 if runtime.GOOS == "linux" {
869 return "linux-x86"
870 } else if runtime.GOOS == "darwin" {
871 return "darwin-x86"
872 } else {
873 panic("Unsupported OS")
874 }
875}
Dan Willemsenf173d592017-04-27 14:28:00 -0700876
Dan Willemsen8122bd52017-10-12 20:20:41 -0700877func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700878 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
879 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700880 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
881 if _, err := os.Stat(asan); err == nil {
882 return asan
883 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700884 }
885 }
886 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
887}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700888
889func (c *configImpl) SetBuildBrokenDupRules(val bool) {
890 c.brokenDupRules = val
891}
892
893func (c *configImpl) BuildBrokenDupRules() bool {
894 return c.brokenDupRules
895}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700896
Dan Willemsen25e6f092019-04-09 10:22:43 -0700897func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
898 c.brokenUsesNetwork = val
899}
900
901func (c *configImpl) BuildBrokenUsesNetwork() bool {
902 return c.brokenUsesNetwork
903}
904
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700905func (c *configImpl) SetTargetDeviceDir(dir string) {
906 c.targetDeviceDir = dir
907}
908
909func (c *configImpl) TargetDeviceDir() string {
910 return c.targetDeviceDir
911}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700912
913func (c *configImpl) SetPdkBuild(pdk bool) {
914 c.pdkBuild = pdk
915}
916
917func (c *configImpl) IsPdkBuild() bool {
918 return c.pdkBuild
919}