blob: 363121f9e828df0c25bd89066914970bc56dfc54 [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 (
Dan Willemsenc2af0be2017-01-20 14:10:01 -080018 "log"
19 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "path/filepath"
21 "runtime"
22 "strconv"
23 "strings"
Jeff Gastonefc1b412017-03-29 17:29:06 -070024
25 "android/soong/shared"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
28type Config struct{ *configImpl }
29
30type configImpl struct {
31 // From the environment
32 arguments []string
33 goma bool
34 environ *Environment
35
36 // From the arguments
Colin Cross37193492017-11-16 17:55:00 -080037 parallel int
38 keepGoing int
39 verbose bool
40 checkbuild bool
41 dist bool
42 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070043
44 // From the product config
Dan Willemsen02781d52017-05-12 19:28:13 -070045 katiArgs []string
46 ninjaArgs []string
47 katiSuffix string
48 targetDevice string
Dan Willemsen1e704462016-08-21 15:17:17 -070049}
50
Dan Willemsenc2af0be2017-01-20 14:10:01 -080051const srcDirFileCheck = "build/soong/root.bp"
52
Dan Willemsen1e704462016-08-21 15:17:17 -070053func NewConfig(ctx Context, args ...string) Config {
54 ret := &configImpl{
55 environ: OsEnvironment(),
56 }
57
Dan Willemsen9b587492017-07-10 22:13:00 -070058 // Sane default matching ninja
59 ret.parallel = runtime.NumCPU() + 2
60 ret.keepGoing = 1
61
62 ret.parseArgs(ctx, args)
63
Dan Willemsen0c3919e2017-03-02 15:49:10 -080064 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070065 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
66 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
67 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -080068 outDir := "out"
69 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
70 if wd, err := os.Getwd(); err != nil {
71 ctx.Fatalln("Failed to get working directory:", err)
72 } else {
73 outDir = filepath.Join(baseDir, filepath.Base(wd))
74 }
75 }
76 ret.environ.Set("OUT_DIR", outDir)
77 }
78
Dan Willemsen1e704462016-08-21 15:17:17 -070079 ret.environ.Unset(
80 // We're already using it
81 "USE_SOONG_UI",
82
83 // We should never use GOROOT/GOPATH from the shell environment
84 "GOROOT",
85 "GOPATH",
86
87 // These should only come from Soong, not the environment.
88 "CLANG",
89 "CLANG_CXX",
90 "CCC_CC",
91 "CCC_CXX",
92
93 // Used by the goma compiler wrapper, but should only be set by
94 // gomacc
95 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -080096
97 // We handle this above
98 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -070099
100 // Variables that have caused problems in the past
101 "DISPLAY",
102 "GREP_OPTIONS",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700103
104 // Drop make flags
105 "MAKEFLAGS",
106 "MAKELEVEL",
107 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700108
109 // Set in envsetup.sh, reset in makefiles
110 "ANDROID_JAVA_TOOLCHAIN",
Dan Willemsen1e704462016-08-21 15:17:17 -0700111 )
112
113 // Tell python not to spam the source tree with .pyc files.
114 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
115
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800116 // Precondition: the current directory is the top of the source tree
117 if _, err := os.Stat(srcDirFileCheck); err != nil {
118 if os.IsNotExist(err) {
119 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
120 }
121 log.Fatalln("Error verifying tree state:", err)
122 }
123
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700124 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
125 log.Println("You are building in a directory whose absolute path contains a space character:")
126 log.Println()
127 log.Printf("%q\n", srcDir)
128 log.Println()
129 log.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700130 }
131
132 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
133 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
134 log.Println()
135 log.Printf("%q\n", outDir)
136 log.Println()
137 log.Fatalln("Directory names containing spaces are not supported")
138 }
139
140 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
141 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
142 log.Println()
143 log.Printf("%q\n", distDir)
144 log.Println()
145 log.Fatalln("Directory names containing spaces are not supported")
146 }
147
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700148 // Configure Java-related variables, including adding it to $PATH
Tobias Thierere59aeff2017-12-20 22:40:39 +0000149 java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
150 java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700151 javaHome := func() string {
152 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
153 return override
154 }
Tobias Thierer18099fd2017-11-17 14:14:29 +0000155 v, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK9")
156 if !ok {
157 v2, ok2 := ret.environ.Get("RUN_ERROR_PRONE")
158 if ok2 && (v2 == "true") {
159 v = "false"
160 } else {
161 v = "1.8"
162 }
163 }
164 if v != "false" {
Tobias Thierere59aeff2017-12-20 22:40:39 +0000165 return java9Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700166 }
Tobias Thierere59aeff2017-12-20 22:40:39 +0000167 return java8Home
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700168 }()
169 absJavaHome := absPath(ctx, javaHome)
170
Dan Willemsened869522018-01-08 14:58:46 -0800171 ret.configureLocale(ctx)
172
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700173 newPath := []string{filepath.Join(absJavaHome, "bin")}
174 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
175 newPath = append(newPath, path)
176 }
177 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
178 ret.environ.Set("JAVA_HOME", absJavaHome)
179 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
Tobias Thierere59aeff2017-12-20 22:40:39 +0000180 ret.environ.Set("ANDROID_JAVA8_HOME", java8Home)
181 ret.environ.Set("ANDROID_JAVA9_HOME", java9Home)
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700182 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
183
Dan Willemsen9b587492017-07-10 22:13:00 -0700184 return Config{ret}
185}
186
187func (c *configImpl) parseArgs(ctx Context, args []string) {
188 for i := 0; i < len(args); i++ {
189 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700190 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700191 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700192 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700193 } else if arg == "--skip-make" {
194 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700195 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700196 parseArgNum := func(def int) int {
197 if len(arg) > 2 {
198 p, err := strconv.ParseUint(arg[2:], 10, 31)
199 if err != nil {
200 ctx.Fatalf("Failed to parse %q: %v", arg, err)
201 }
202 return int(p)
203 } else if i+1 < len(args) {
204 p, err := strconv.ParseUint(args[i+1], 10, 31)
205 if err == nil {
206 i++
207 return int(p)
208 }
209 }
210 return def
211 }
212
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700213 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700214 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700215 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700216 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700217 } else {
218 ctx.Fatalln("Unknown option:", arg)
219 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700220 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
221 c.environ.Set(k, v)
Dan Willemsen1e704462016-08-21 15:17:17 -0700222 } else {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700223 if arg == "dist" {
224 c.dist = true
Colin Cross37193492017-11-16 17:55:00 -0800225 } else if arg == "checkbuild" {
226 c.checkbuild = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700227 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700228 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700229 }
230 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700231}
232
Dan Willemsened869522018-01-08 14:58:46 -0800233func (c *configImpl) configureLocale(ctx Context) {
234 cmd := Command(ctx, Config{c}, "locale", "locale", "-a")
235 output, err := cmd.Output()
236
237 var locales []string
238 if err == nil {
239 locales = strings.Split(string(output), "\n")
240 } else {
241 // If we're unable to list the locales, let's assume en_US.UTF-8
242 locales = []string{"en_US.UTF-8"}
243 ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales)
244 }
245
246 // gettext uses LANGUAGE, which is passed directly through
247
248 // For LANG and LC_*, only preserve the evaluated version of
249 // LC_MESSAGES
250 user_lang := ""
251 if lc_all, ok := c.environ.Get("LC_ALL"); ok {
252 user_lang = lc_all
253 } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok {
254 user_lang = lc_messages
255 } else if lang, ok := c.environ.Get("LANG"); ok {
256 user_lang = lang
257 }
258
259 c.environ.UnsetWithPrefix("LC_")
260
261 if user_lang != "" {
262 c.environ.Set("LC_MESSAGES", user_lang)
263 }
264
265 // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed
266 // for others)
267 if inList("C.UTF-8", locales) {
268 c.environ.Set("LANG", "C.UTF-8")
269 } else if inList("en_US.UTF-8", locales) {
270 c.environ.Set("LANG", "en_US.UTF-8")
271 } else if inList("en_US.utf8", locales) {
272 // These normalize to the same thing
273 c.environ.Set("LANG", "en_US.UTF-8")
274 } else {
275 ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8")
276 }
277}
278
Dan Willemsen1e704462016-08-21 15:17:17 -0700279// Lunch configures the environment for a specific product similarly to the
280// `lunch` bash function.
281func (c *configImpl) Lunch(ctx Context, product, variant string) {
282 if variant != "eng" && variant != "userdebug" && variant != "user" {
283 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
284 }
285
286 c.environ.Set("TARGET_PRODUCT", product)
287 c.environ.Set("TARGET_BUILD_VARIANT", variant)
288 c.environ.Set("TARGET_BUILD_TYPE", "release")
289 c.environ.Unset("TARGET_BUILD_APPS")
290}
291
292// Tapas configures the environment to build one or more unbundled apps,
293// similarly to the `tapas` bash function.
294func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
295 if len(apps) == 0 {
296 apps = []string{"all"}
297 }
298 if variant == "" {
299 variant = "eng"
300 }
301
302 if variant != "eng" && variant != "userdebug" && variant != "user" {
303 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
304 }
305
306 var product string
307 switch arch {
Dan Willemsen1e704462016-08-21 15:17:17 -0700308 case "arm", "":
309 product = "aosp_arm"
310 case "arm64":
311 product = "aosm_arm64"
312 case "mips":
313 product = "aosp_mips"
314 case "mips64":
315 product = "aosp_mips64"
316 case "x86":
317 product = "aosp_x86"
318 case "x86_64":
319 product = "aosp_x86_64"
320 default:
321 ctx.Fatalf("Invalid architecture: %q", arch)
322 }
323
324 c.environ.Set("TARGET_PRODUCT", product)
325 c.environ.Set("TARGET_BUILD_VARIANT", variant)
326 c.environ.Set("TARGET_BUILD_TYPE", "release")
327 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
328}
329
330func (c *configImpl) Environment() *Environment {
331 return c.environ
332}
333
334func (c *configImpl) Arguments() []string {
335 return c.arguments
336}
337
338func (c *configImpl) OutDir() string {
339 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
340 return outDir
341 }
342 return "out"
343}
344
Dan Willemsen8a073a82017-02-04 17:30:44 -0800345func (c *configImpl) DistDir() string {
346 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
347 return distDir
348 }
349 return filepath.Join(c.OutDir(), "dist")
350}
351
Dan Willemsen1e704462016-08-21 15:17:17 -0700352func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700353 if c.skipMake {
354 return c.arguments
355 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700356 return c.ninjaArgs
357}
358
359func (c *configImpl) SoongOutDir() string {
360 return filepath.Join(c.OutDir(), "soong")
361}
362
Jeff Gastonefc1b412017-03-29 17:29:06 -0700363func (c *configImpl) TempDir() string {
364 return shared.TempDirForOutDir(c.SoongOutDir())
365}
366
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700367func (c *configImpl) FileListDir() string {
368 return filepath.Join(c.OutDir(), ".module_paths")
369}
370
Dan Willemsen1e704462016-08-21 15:17:17 -0700371func (c *configImpl) KatiSuffix() string {
372 if c.katiSuffix != "" {
373 return c.katiSuffix
374 }
375 panic("SetKatiSuffix has not been called")
376}
377
Colin Cross37193492017-11-16 17:55:00 -0800378// Checkbuild returns true if "checkbuild" was one of the build goals, which means that the
379// user is interested in additional checks at the expense of build time.
380func (c *configImpl) Checkbuild() bool {
381 return c.checkbuild
382}
383
Dan Willemsen8a073a82017-02-04 17:30:44 -0800384func (c *configImpl) Dist() bool {
385 return c.dist
386}
387
Dan Willemsen1e704462016-08-21 15:17:17 -0700388func (c *configImpl) IsVerbose() bool {
389 return c.verbose
390}
391
Dan Willemsene0879fc2017-08-04 15:06:27 -0700392func (c *configImpl) SkipMake() bool {
393 return c.skipMake
394}
395
Dan Willemsen1e704462016-08-21 15:17:17 -0700396func (c *configImpl) TargetProduct() string {
397 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
398 return v
399 }
400 panic("TARGET_PRODUCT is not defined")
401}
402
Dan Willemsen02781d52017-05-12 19:28:13 -0700403func (c *configImpl) TargetDevice() string {
404 return c.targetDevice
405}
406
407func (c *configImpl) SetTargetDevice(device string) {
408 c.targetDevice = device
409}
410
411func (c *configImpl) TargetBuildVariant() string {
412 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
413 return v
414 }
415 panic("TARGET_BUILD_VARIANT is not defined")
416}
417
Dan Willemsen1e704462016-08-21 15:17:17 -0700418func (c *configImpl) KatiArgs() []string {
419 return c.katiArgs
420}
421
422func (c *configImpl) Parallel() int {
423 return c.parallel
424}
425
426func (c *configImpl) UseGoma() bool {
427 if v, ok := c.environ.Get("USE_GOMA"); ok {
428 v = strings.TrimSpace(v)
429 if v != "" && v != "false" {
430 return true
431 }
432 }
433 return false
434}
435
436// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700437// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700438// still limited by Parallel()
439func (c *configImpl) RemoteParallel() int {
440 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
441 if i, err := strconv.Atoi(v); err == nil {
442 return i
443 }
444 }
445 return 500
446}
447
448func (c *configImpl) SetKatiArgs(args []string) {
449 c.katiArgs = args
450}
451
452func (c *configImpl) SetNinjaArgs(args []string) {
453 c.ninjaArgs = args
454}
455
456func (c *configImpl) SetKatiSuffix(suffix string) {
457 c.katiSuffix = suffix
458}
459
Dan Willemsene0879fc2017-08-04 15:06:27 -0700460func (c *configImpl) LastKatiSuffixFile() string {
461 return filepath.Join(c.OutDir(), "last_kati_suffix")
462}
463
464func (c *configImpl) HasKatiSuffix() bool {
465 return c.katiSuffix != ""
466}
467
Dan Willemsen1e704462016-08-21 15:17:17 -0700468func (c *configImpl) KatiEnvFile() string {
469 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
470}
471
472func (c *configImpl) KatiNinjaFile() string {
473 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
474}
475
476func (c *configImpl) SoongNinjaFile() string {
477 return filepath.Join(c.SoongOutDir(), "build.ninja")
478}
479
480func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700481 if c.katiSuffix == "" {
482 return filepath.Join(c.OutDir(), "combined.ninja")
483 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700484 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
485}
486
487func (c *configImpl) SoongAndroidMk() string {
488 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
489}
490
491func (c *configImpl) SoongMakeVarsMk() string {
492 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
493}
494
Dan Willemsenf052f782017-05-18 15:29:04 -0700495func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700496 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700497}
498
Dan Willemsen02781d52017-05-12 19:28:13 -0700499func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700500 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
501}
502
503func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700504 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700505}
506
507func (c *configImpl) HostOut() string {
508 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
509}
510
511// This probably needs to be multi-valued, so not exporting it for now
512func (c *configImpl) hostCrossOut() string {
513 if runtime.GOOS == "linux" {
514 return filepath.Join(c.hostOutRoot(), "windows-x86")
515 } else {
516 return ""
517 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700518}
519
Dan Willemsen1e704462016-08-21 15:17:17 -0700520func (c *configImpl) HostPrebuiltTag() string {
521 if runtime.GOOS == "linux" {
522 return "linux-x86"
523 } else if runtime.GOOS == "darwin" {
524 return "darwin-x86"
525 } else {
526 panic("Unsupported OS")
527 }
528}
Dan Willemsenf173d592017-04-27 14:28:00 -0700529
Dan Willemsen8122bd52017-10-12 20:20:41 -0700530func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700531 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
532 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700533 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
534 if _, err := os.Stat(asan); err == nil {
535 return asan
536 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700537 }
538 }
539 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
540}