blob: 6df9529fe368383169df73891a48e91cac7af4c7 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// Copyright 2017 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package build
16
17import (
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080018 "io/ioutil"
Dan Willemsenc2af0be2017-01-20 14:10:01 -080019 "log"
20 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070021 "path/filepath"
22 "runtime"
23 "strconv"
24 "strings"
Nan Zhang2e6a4ff2018-02-14 13:27:26 -080025 "time"
Jeff Gastonefc1b412017-03-29 17:29:06 -070026
27 "android/soong/shared"
Dan Willemsen1e704462016-08-21 15:17:17 -070028)
29
30type Config struct{ *configImpl }
31
32type configImpl struct {
33 // From the environment
34 arguments []string
35 goma bool
36 environ *Environment
Dan Willemsen2d31a442018-10-20 21:33:41 -070037 distDir string
Dan Willemsen1e704462016-08-21 15:17:17 -070038
39 // From the arguments
Colin Cross37193492017-11-16 17:55:00 -080040 parallel int
41 keepGoing int
42 verbose bool
43 checkbuild bool
44 dist bool
45 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070046
47 // From the product config
Dan Willemsen6ab79db2018-05-02 00:06:28 -070048 katiArgs []string
49 ninjaArgs []string
50 katiSuffix string
51 targetDevice string
52 targetDeviceDir string
Dan Willemsen3d60b112018-04-04 22:25:56 -070053
Dan Willemsend8aa39d2018-08-27 15:01:03 -070054 pdkBuild bool
55
Dan Willemsen60977462019-04-18 09:40:15 -070056 brokenDupRules bool
57 brokenUsesNetwork bool
Dan Willemsen18490112018-05-25 16:30:04 -070058
59 pathReplaced bool
Dan Willemsen1e704462016-08-21 15:17:17 -070060}
61
Dan Willemsenc2af0be2017-01-20 14:10:01 -080062const srcDirFileCheck = "build/soong/root.bp"
63
Patrice Arruda13848222019-04-22 17:12:02 -070064type BuildAction uint
65
66const (
67 // Builds all of the modules and their dependencies of a specified directory, relative to the root
68 // directory of the source tree.
69 BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota
70
71 // Builds all of the modules and their dependencies of a list of specified directories. All specified
72 // directories are relative to the root directory of the source tree.
73 BUILD_MODULES_IN_DIRECTORIES
74)
75
76// checkTopDir validates that the current directory is at the root directory of the source tree.
77func checkTopDir(ctx Context) {
78 if _, err := os.Stat(srcDirFileCheck); err != nil {
79 if os.IsNotExist(err) {
80 ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck)
81 }
82 ctx.Fatalln("Error verifying tree state:", err)
83 }
84}
85
Dan Willemsen1e704462016-08-21 15:17:17 -070086func NewConfig(ctx Context, args ...string) Config {
87 ret := &configImpl{
88 environ: OsEnvironment(),
89 }
90
Dan Willemsen9b587492017-07-10 22:13:00 -070091 // Sane default matching ninja
92 ret.parallel = runtime.NumCPU() + 2
93 ret.keepGoing = 1
94
95 ret.parseArgs(ctx, args)
96
Dan Willemsen0c3919e2017-03-02 15:49:10 -080097 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070098 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
99 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
100 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800101 outDir := "out"
102 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
103 if wd, err := os.Getwd(); err != nil {
104 ctx.Fatalln("Failed to get working directory:", err)
105 } else {
106 outDir = filepath.Join(baseDir, filepath.Base(wd))
107 }
108 }
109 ret.environ.Set("OUT_DIR", outDir)
110 }
111
Dan Willemsen2d31a442018-10-20 21:33:41 -0700112 if distDir, ok := ret.environ.Get("DIST_DIR"); ok {
113 ret.distDir = filepath.Clean(distDir)
114 } else {
115 ret.distDir = filepath.Join(ret.OutDir(), "dist")
116 }
Dan Willemsend50e89f2018-10-16 17:49:25 -0700117
Dan Willemsen1e704462016-08-21 15:17:17 -0700118 ret.environ.Unset(
119 // We're already using it
120 "USE_SOONG_UI",
121
122 // We should never use GOROOT/GOPATH from the shell environment
123 "GOROOT",
124 "GOPATH",
125
126 // These should only come from Soong, not the environment.
127 "CLANG",
128 "CLANG_CXX",
129 "CCC_CC",
130 "CCC_CXX",
131
132 // Used by the goma compiler wrapper, but should only be set by
133 // gomacc
134 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -0800135
136 // We handle this above
137 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -0700138
Dan Willemsen2d31a442018-10-20 21:33:41 -0700139 // This is handled above too, and set for individual commands later
140 "DIST_DIR",
141
Dan Willemsen68a09852017-04-18 13:56:57 -0700142 // Variables that have caused problems in the past
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700143 "CDPATH",
Dan Willemsen68a09852017-04-18 13:56:57 -0700144 "DISPLAY",
145 "GREP_OPTIONS",
Dan Willemsenebfe33a2018-05-01 10:07:50 -0700146 "NDK_ROOT",
Dan Willemsen00fcb262018-08-15 15:35:38 -0700147 "POSIXLY_CORRECT",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700148
149 // Drop make flags
150 "MAKEFLAGS",
151 "MAKELEVEL",
152 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700153
154 // Set in envsetup.sh, reset in makefiles
155 "ANDROID_JAVA_TOOLCHAIN",
Colin Cross7f09c402018-07-11 14:49:31 -0700156
157 // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional
158 "ANDROID_BUILD_TOP",
159 "ANDROID_HOST_OUT",
160 "ANDROID_PRODUCT_OUT",
161 "ANDROID_HOST_OUT_TESTCASES",
162 "ANDROID_TARGET_OUT_TESTCASES",
163 "ANDROID_TOOLCHAIN",
164 "ANDROID_TOOLCHAIN_2ND_ARCH",
165 "ANDROID_DEV_SCRIPTS",
166 "ANDROID_EMULATOR_PREBUILTS",
167 "ANDROID_PRE_BUILD_PATHS",
Dan Willemsenf99915f2018-10-25 22:04:42 -0700168
169 // Only set in multiproduct_kati after config generation
170 "EMPTY_NINJA_FILE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700171 )
172
173 // Tell python not to spam the source tree with .pyc files.
174 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
175
Dan Willemsen32a669b2018-03-08 19:42:00 -0800176 ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir()))
177
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800178 // Precondition: the current directory is the top of the source tree
Patrice Arruda13848222019-04-22 17:12:02 -0700179 checkTopDir(ctx)
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800180
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700181 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
182 log.Println("You are building in a directory whose absolute path contains a space character:")
183 log.Println()
184 log.Printf("%q\n", srcDir)
185 log.Println()
186 log.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700187 }
188
189 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
190 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
191 log.Println()
192 log.Printf("%q\n", outDir)
193 log.Println()
194 log.Fatalln("Directory names containing spaces are not supported")
195 }
196
197 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
198 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
199 log.Println()
200 log.Printf("%q\n", distDir)
201 log.Println()
202 log.Fatalln("Directory names containing spaces are not supported")
203 }
204
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700205 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000206 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
207 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700208 javaHome := func() string {
209 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
210 return override
211 }
Colin Cross997262f2018-06-19 22:49:39 -0700212 return java9Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700213 }()
214 absJavaHome := absPath(ctx, javaHome)
215
Dan Willemsened869522018-01-08 14:58:46 -0800216 ret.configureLocale(ctx)
217
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700218 newPath := []string{filepath.Join(absJavaHome, "bin")}
219 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
220 newPath = append(newPath, path)
221 }
222 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
223 ret.environ.Set("JAVA_HOME", absJavaHome)
224 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000225 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
226 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700227 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
228
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800229 outDir := ret.OutDir()
230 buildDateTimeFile := filepath.Join(outDir, "build_date.txt")
231 var content string
232 if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" {
233 content = buildDateTime
234 } else {
235 content = strconv.FormatInt(time.Now().Unix(), 10)
236 }
Nan Zhang17f27672018-12-12 16:01:49 -0800237 if ctx.Metrics != nil {
238 ctx.Metrics.SetBuildDateTime(content)
239 }
Nan Zhang2e6a4ff2018-02-14 13:27:26 -0800240 err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777)
241 if err != nil {
242 ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err)
243 }
244 ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile)
245
Dan Willemsen9b587492017-07-10 22:13:00 -0700246 return Config{ret}
247}
248
Patrice Arruda13848222019-04-22 17:12:02 -0700249// NewBuildActionConfig returns a build configuration based on the build action. The arguments are
250// processed based on the build action and extracts any arguments that belongs to the build action.
251func NewBuildActionConfig(action BuildAction, dir string, buildDependencies bool, ctx Context, args ...string) Config {
252 return NewConfig(ctx, getConfigArgs(action, dir, buildDependencies, ctx, args)...)
253}
254
255// getConfigArgs processes the command arguments based on the build action and creates a set of new
256// arguments to be accepted by Config.
257func getConfigArgs(action BuildAction, dir string, buildDependencies bool, ctx Context, args []string) []string {
258 // The next block of code verifies that the current directory is the root directory of the source
259 // tree. It then finds the relative path of dir based on the root directory of the source tree
260 // and verify that dir is inside of the source tree.
261 checkTopDir(ctx)
262 topDir, err := os.Getwd()
263 if err != nil {
264 ctx.Fatalf("Error retrieving top directory: %v", err)
265 }
266 dir, err = filepath.Abs(dir)
267 if err != nil {
268 ctx.Fatalf("Unable to find absolute path %s: %v", dir, err)
269 }
270 relDir, err := filepath.Rel(topDir, dir)
271 if err != nil {
272 ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err)
273 }
274 // If there are ".." in the path, it's not in the source tree.
275 if strings.Contains(relDir, "..") {
276 ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir)
277 }
278
279 configArgs := args[:]
280
281 // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to
282 // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules.
283 targetNamePrefix := "MODULES-IN-"
284 if inList("GET-INSTALL-PATH", configArgs) {
285 targetNamePrefix = "GET-INSTALL-PATH-IN-"
286 configArgs = removeFromList("GET-INSTALL-PATH", configArgs)
287 }
288
289 var buildFiles []string
290 var targets []string
291
292 switch action {
293 case BUILD_MODULES_IN_A_DIRECTORY:
294 // If dir is the root source tree, all the modules are built of the source tree are built so
295 // no need to find the build file.
296 if topDir == dir {
297 break
298 }
299 // Find the build file from the directory where the build action was triggered by traversing up
300 // the source tree. If a blank build filename is returned, simply use the directory where the build
301 // action was invoked.
302 buildFile := findBuildFile(ctx, relDir)
303 if buildFile == "" {
304 buildFile = filepath.Join(relDir, "Android.mk")
305 }
306 buildFiles = []string{buildFile}
307 targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)}
308 case BUILD_MODULES_IN_DIRECTORIES:
309 newConfigArgs, dirs := splitArgs(configArgs)
310 configArgs = newConfigArgs
311 targets, buildFiles = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix)
312 }
313
314 // This is to support building modules without building their dependencies. Soon, this will be
315 // deprecated.
316 if !buildDependencies && len(buildFiles) > 0 {
317 if err := os.Setenv("ONE_SHOT_MAKEFILE", strings.Join(buildFiles, " ")); err != nil {
318 ctx.Fatalf("Unable to set ONE_SHOT_MAKEFILE environment variable: %v", err)
319 }
320 }
321
322 // Tidy only override all other specified targets.
323 tidyOnly := os.Getenv("WITH_TIDY_ONLY")
324 if tidyOnly == "true" || tidyOnly == "1" {
325 configArgs = append(configArgs, "tidy_only")
326 } else {
327 configArgs = append(configArgs, targets...)
328 }
329
330 return configArgs
331}
332
333// convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name.
334func convertToTarget(dir string, targetNamePrefix string) string {
335 return targetNamePrefix + strings.ReplaceAll(dir, "/", "-")
336}
337
338// findBuildFile finds a build file (makefile or blueprint file) by looking at dir first. If not
339// found, go up one level and repeat again until one is found and the path of that build file
340// relative to the root directory of the source tree is returned. The returned filename of build
341// file is "Android.mk". If one was not found, a blank string is returned.
342func findBuildFile(ctx Context, dir string) string {
343 // If the string is empty, assume it is top directory of the source tree.
344 if dir == "" {
345 return ""
346 }
347
348 for ; dir != "."; dir = filepath.Dir(dir) {
349 for _, buildFile := range []string{"Android.bp", "Android.mk"} {
350 _, err := os.Stat(filepath.Join(dir, buildFile))
351 if err == nil {
352 // Returning the filename Android.mk as it might be used for ONE_SHOT_MAKEFILE variable.
353 return filepath.Join(dir, "Android.mk")
354 }
355 if !os.IsNotExist(err) {
356 ctx.Fatalf("Error retrieving the build file stats: %v", err)
357 }
358 }
359 }
360
361 return ""
362}
363
364// splitArgs iterates over the arguments list and splits into two lists: arguments and directories.
365func splitArgs(args []string) (newArgs []string, dirs []string) {
366 specialArgs := map[string]bool{
367 "showcommands": true,
368 "snod": true,
369 "dist": true,
370 "checkbuild": true,
371 }
372
373 newArgs = []string{}
374 dirs = []string{}
375
376 for _, arg := range args {
377 // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory.
378 if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 {
379 newArgs = append(newArgs, arg)
380 continue
381 }
382
383 if _, ok := specialArgs[arg]; ok {
384 newArgs = append(newArgs, arg)
385 continue
386 }
387
388 dirs = append(dirs, arg)
389 }
390
391 return newArgs, dirs
392}
393
394// getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a
395// directory from the dirs list does not exist, a fatal error is raised. relDir is related to the
396// source root tree where the build action command was invoked. Each directory is validated if the
397// build file can be found and follows the format "dir1:target1,target2,...". Target is optional.
398func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string, buildFiles []string) {
399 for _, dir := range dirs {
400 // The directory may have specified specific modules to build. ":" is the separator to separate
401 // the directory and the list of modules.
402 s := strings.Split(dir, ":")
403 l := len(s)
404 if l > 2 { // more than one ":" was specified.
405 ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir)
406 }
407
408 dir = filepath.Join(relDir, s[0])
409 if _, err := os.Stat(dir); err != nil {
410 ctx.Fatalf("couldn't find directory %s", dir)
411 }
412
413 // Verify that if there are any targets specified after ":". Each target is separated by ",".
414 var newTargets []string
415 if l == 2 && s[1] != "" {
416 newTargets = strings.Split(s[1], ",")
417 if inList("", newTargets) {
418 ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir)
419 }
420 }
421
422 buildFile := findBuildFile(ctx, dir)
423 if buildFile == "" {
424 ctx.Fatalf("Build file not found for %s directory", dir)
425 }
426 buildFileDir := filepath.Dir(buildFile)
427
428 // If there are specified targets, find the build file in the directory. If dir does not
429 // contain the build file, bail out as it is required for one shot build. If there are no
430 // target specified, build all the modules in dir (or the closest one in the dir path).
431 if len(newTargets) > 0 {
432 if buildFileDir != dir {
433 ctx.Fatalf("Couldn't locate a build file from %s directory", dir)
434 }
435 } else {
436 newTargets = []string{convertToTarget(buildFileDir, targetNamePrefix)}
437 }
438
439 buildFiles = append(buildFiles, buildFile)
440 targets = append(targets, newTargets...)
441 }
442
443 return targets, buildFiles
444}
445
Dan Willemsen9b587492017-07-10 22:13:00 -0700446func (c *configImpl) parseArgs(ctx Context, args []string) {
447 for i := 0; i < len(args); i++ {
448 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700449 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700450 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700451 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700452 } else if arg == "--skip-make" {
453 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700454 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700455 parseArgNum := func(def int) int {
456 if len(arg) > 2 {
457 p, err := strconv.ParseUint(arg[2:], 10, 31)
458 if err != nil {
459 ctx.Fatalf("Failed to parse %q: %v", arg, err)
460 }
461 return int(p)
462 } else if i+1 < len(args) {
463 p, err := strconv.ParseUint(args[i+1], 10, 31)
464 if err == nil {
465 i++
466 return int(p)
467 }
468 }
469 return def
470 }
471
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700472 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700473 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700474 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700475 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700476 } else {
477 ctx.Fatalln("Unknown option:", arg)
478 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700479 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
480 c.environ.Set(k, v)
Dan Willemsen2d31a442018-10-20 21:33:41 -0700481 } else if arg == "dist" {
482 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700483 } else {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700484 if arg == "checkbuild" {
Colin Cross37193492017-11-16 17:55:00 -0800485 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700486 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700487 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700488 }
489 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700490}
491
Dan Willemsened869522018-01-08 14:58:46 -0800492func (c *configImpl) configureLocale(ctx Context) {
493 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
494 output, err := cmd.Output()
495
496 var locales []string
497 if err == nil {
498 locales = strings.Split(string(output), "\n")
499 } else {
500 // If we're unable to list the locales, let's assume en_US.UTF-8
501 locales = []string{"en_US.UTF-8"}
502 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
503 }
504
505 // gettext uses LANGUAGE, which is passed directly through
506
507 // For LANG and LC_*, only preserve the evaluated version of
508 // LC_MESSAGES
509 user_lang := ""
510 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
511 user_lang = lc_all
512 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
513 user_lang = lc_messages
514 } else if lang, ok := c.environ.Get("LANG"); ok {
515 user_lang = lang
516 }
517
518 c.environ.UnsetWithPrefix("LC_")
519
520 if user_lang != "" {
521 c.environ.Set("LC_MESSAGES", user_lang)
522 }
523
524 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
525 // for others)
526 if inList("C.UTF-8", locales) {
527 c.environ.Set("LANG", "C.UTF-8")
Aaron Klingd236e0e2018-08-07 19:21:36 -0500528 } else if inList("C.utf8", locales) {
529 // These normalize to the same thing
530 c.environ.Set("LANG", "C.UTF-8")
Dan Willemsened869522018-01-08 14:58:46 -0800531 } else if inList("en_US.UTF-8", locales) {
532 c.environ.Set("LANG", "en_US.UTF-8")
533 } else if inList("en_US.utf8", locales) {
534 // These normalize to the same thing
535 c.environ.Set("LANG", "en_US.UTF-8")
536 } else {
537 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
538 }
539}
540
Dan Willemsen1e704462016-08-21 15:17:17 -0700541// Lunch configures the environment for a specific product similarly to the
542// `lunch` bash function.
543func (c *configImpl) Lunch(ctx Context, product, variant string) {
544 if variant != "eng" && variant != "userdebug" && variant != "user" {
545 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
546 }
547
548 c.environ.Set("TARGET_PRODUCT", product)
549 c.environ.Set("TARGET_BUILD_VARIANT", variant)
550 c.environ.Set("TARGET_BUILD_TYPE", "release")
551 c.environ.Unset("TARGET_BUILD_APPS")
552}
553
554// Tapas configures the environment to build one or more unbundled apps,
555// similarly to the `tapas` bash function.
556func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
557 if len(apps) == 0 {
558 apps = []string{"all"}
559 }
560 if variant == "" {
561 variant = "eng"
562 }
563
564 if variant != "eng" && variant != "userdebug" && variant != "user" {
565 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
566 }
567
568 var product string
569 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700570 case "arm", "":
571 product = "aosp_arm"
572 case "arm64":
573 product = "aosm_arm64"
574 case "mips":
575 product = "aosp_mips"
576 case "mips64":
577 product = "aosp_mips64"
578 case "x86":
579 product = "aosp_x86"
580 case "x86_64":
581 product = "aosp_x86_64"
582 default:
583 ctx.Fatalf("Invalid architecture: %q", arch)
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.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
590}
591
592func (c *configImpl) Environment() *Environment {
593 return c.environ
594}
595
596func (c *configImpl) Arguments() []string {
597 return c.arguments
598}
599
600func (c *configImpl) OutDir() string {
601 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
Dan Willemsen25a56182018-08-31 20:25:32 -0700602 return filepath.Clean(outDir)
Dan Willemsen1e704462016-08-21 15:17:17 -0700603 }
604 return "out"
605}
606
Dan Willemsen8a073a82017-02-04 17:30:44 -0800607func (c *configImpl) DistDir() string {
Dan Willemsen2d31a442018-10-20 21:33:41 -0700608 return c.distDir
Dan Willemsen8a073a82017-02-04 17:30:44 -0800609}
610
Dan Willemsen1e704462016-08-21 15:17:17 -0700611func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700612 if c.skipMake {
613 return c.arguments
614 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700615 return c.ninjaArgs
616}
617
618func (c *configImpl) SoongOutDir() string {
619 return filepath.Join(c.OutDir(), "soong")
620}
621
Jeff Gastonefc1b412017-03-29 17:29:06 -0700622func (c *configImpl) TempDir() string {
623 return shared.TempDirForOutDir(c.SoongOutDir())
624}
625
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700626func (c *configImpl) FileListDir() string {
627 return filepath.Join(c.OutDir(), ".module_paths")
628}
629
Dan Willemsen1e704462016-08-21 15:17:17 -0700630func (c *configImpl) KatiSuffix() string {
631 if c.katiSuffix != "" {
632 return c.katiSuffix
633 }
634 panic("SetKatiSuffix has not been called")
635}
636
Colin Cross37193492017-11-16 17:55:00 -0800637// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
638// user is interested in additional checks at the expense of build time.
639func (c *configImpl) Checkbuild() bool {
640 return c.checkbuild
641}
642
Dan Willemsen8a073a82017-02-04 17:30:44 -0800643func (c *configImpl) Dist() bool {
644 return c.dist
645}
646
Dan Willemsen1e704462016-08-21 15:17:17 -0700647func (c *configImpl) IsVerbose() bool {
648 return c.verbose
649}
650
Dan Willemsene0879fc2017-08-04 15:06:27 -0700651func (c *configImpl) SkipMake() bool {
652 return c.skipMake
653}
654
Dan Willemsen1e704462016-08-21 15:17:17 -0700655func (c *configImpl) TargetProduct() string {
656 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
657 return v
658 }
659 panic("TARGET_PRODUCT is not defined")
660}
661
Dan Willemsen02781d52017-05-12 19:28:13 -0700662func (c *configImpl) TargetDevice() string {
663 return c.targetDevice
664}
665
666func (c *configImpl) SetTargetDevice(device string) {
667 c.targetDevice = device
668}
669
670func (c *configImpl) TargetBuildVariant() string {
671 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
672 return v
673 }
674 panic("TARGET_BUILD_VARIANT is not defined")
675}
676
Dan Willemsen1e704462016-08-21 15:17:17 -0700677func (c *configImpl) KatiArgs() []string {
678 return c.katiArgs
679}
680
681func (c *configImpl) Parallel() int {
682 return c.parallel
683}
684
685func (c *configImpl) UseGoma() bool {
686 if v, ok := c.environ.Get("USE_GOMA"); ok {
687 v = strings.TrimSpace(v)
688 if v != "" && v != "false" {
689 return true
690 }
691 }
692 return false
693}
694
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900695func (c *configImpl) StartGoma() bool {
696 if !c.UseGoma() {
697 return false
698 }
699
700 if v, ok := c.environ.Get("NOSTART_GOMA"); ok {
701 v = strings.TrimSpace(v)
702 if v != "" && v != "false" {
703 return false
704 }
705 }
706 return true
707}
708
Dan Willemsen1e704462016-08-21 15:17:17 -0700709// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700710// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700711// still limited by Parallel()
712func (c *configImpl) RemoteParallel() int {
713 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
714 if i, err := strconv.Atoi(v); err == nil {
715 return i
716 }
717 }
718 return 500
719}
720
721func (c *configImpl) SetKatiArgs(args []string) {
722 c.katiArgs = args
723}
724
725func (c *configImpl) SetNinjaArgs(args []string) {
726 c.ninjaArgs = args
727}
728
729func (c *configImpl) SetKatiSuffix(suffix string) {
730 c.katiSuffix = suffix
731}
732
Dan Willemsene0879fc2017-08-04 15:06:27 -0700733func (c *configImpl) LastKatiSuffixFile() string {
734 return filepath.Join(c.OutDir(), "last_kati_suffix")
735}
736
737func (c *configImpl) HasKatiSuffix() bool {
738 return c.katiSuffix != ""
739}
740
Dan Willemsen1e704462016-08-21 15:17:17 -0700741func (c *configImpl) KatiEnvFile() string {
742 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
743}
744
Dan Willemsen29971232018-09-26 14:58:30 -0700745func (c *configImpl) KatiBuildNinjaFile() string {
746 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja")
Dan Willemsen1e704462016-08-21 15:17:17 -0700747}
748
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700749func (c *configImpl) KatiPackageNinjaFile() string {
750 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja")
751}
752
Dan Willemsen1e704462016-08-21 15:17:17 -0700753func (c *configImpl) SoongNinjaFile() string {
754 return filepath.Join(c.SoongOutDir(), "build.ninja")
755}
756
757func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700758 if c.katiSuffix == "" {
759 return filepath.Join(c.OutDir(), "combined.ninja")
760 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700761 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
762}
763
764func (c *configImpl) SoongAndroidMk() string {
765 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
766}
767
768func (c *configImpl) SoongMakeVarsMk() string {
769 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
770}
771
Dan Willemsenf052f782017-05-18 15:29:04 -0700772func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700773 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700774}
775
Dan Willemsen02781d52017-05-12 19:28:13 -0700776func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700777 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
778}
779
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700780func (c *configImpl) KatiPackageMkDir() string {
781 return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging")
782}
783
Dan Willemsenf052f782017-05-18 15:29:04 -0700784func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700785 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700786}
787
788func (c *configImpl) HostOut() string {
789 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
790}
791
792// This probably needs to be multi-valued, so not exporting it for now
793func (c *configImpl) hostCrossOut() string {
794 if runtime.GOOS == "linux" {
795 return filepath.Join(c.hostOutRoot(), "windows-x86")
796 } else {
797 return ""
798 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700799}
800
Dan Willemsen1e704462016-08-21 15:17:17 -0700801func (c *configImpl) HostPrebuiltTag() string {
802 if runtime.GOOS == "linux" {
803 return "linux-x86"
804 } else if runtime.GOOS == "darwin" {
805 return "darwin-x86"
806 } else {
807 panic("Unsupported OS")
808 }
809}
Dan Willemsenf173d592017-04-27 14:28:00 -0700810
Dan Willemsen8122bd52017-10-12 20:20:41 -0700811func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700812 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
813 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700814 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
815 if _, err := os.Stat(asan); err == nil {
816 return asan
817 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700818 }
819 }
820 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
821}
Dan Willemsen3d60b112018-04-04 22:25:56 -0700822
823func (c *configImpl) SetBuildBrokenDupRules(val bool) {
824 c.brokenDupRules = val
825}
826
827func (c *configImpl) BuildBrokenDupRules() bool {
828 return c.brokenDupRules
829}
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700830
Dan Willemsen25e6f092019-04-09 10:22:43 -0700831func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) {
832 c.brokenUsesNetwork = val
833}
834
835func (c *configImpl) BuildBrokenUsesNetwork() bool {
836 return c.brokenUsesNetwork
837}
838
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700839func (c *configImpl) SetTargetDeviceDir(dir string) {
840 c.targetDeviceDir = dir
841}
842
843func (c *configImpl) TargetDeviceDir() string {
844 return c.targetDeviceDir
845}
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700846
847func (c *configImpl) SetPdkBuild(pdk bool) {
848 c.pdkBuild = pdk
849}
850
851func (c *configImpl) IsPdkBuild() bool {
852 return c.pdkBuild
853}