blob: 940bb2fb2dacb7f1d41604e42b8122b3e36d4fb5 [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
37 parallel int
38 keepGoing int
39 verbose bool
Dan Willemsen8a073a82017-02-04 17:30:44 -080040 dist bool
Dan Willemsene0879fc2017-08-04 15:06:27 -070041 skipMake bool
Dan Willemsen1e704462016-08-21 15:17:17 -070042
43 // From the product config
Dan Willemsen02781d52017-05-12 19:28:13 -070044 katiArgs []string
45 ninjaArgs []string
46 katiSuffix string
47 targetDevice string
Dan Willemsen1e704462016-08-21 15:17:17 -070048}
49
Dan Willemsenc2af0be2017-01-20 14:10:01 -080050const srcDirFileCheck = "build/soong/root.bp"
51
Dan Willemsen1e704462016-08-21 15:17:17 -070052func NewConfig(ctx Context, args ...string) Config {
53 ret := &configImpl{
54 environ: OsEnvironment(),
55 }
56
Dan Willemsen9b587492017-07-10 22:13:00 -070057 // Sane default matching ninja
58 ret.parallel = runtime.NumCPU() + 2
59 ret.keepGoing = 1
60
61 ret.parseArgs(ctx, args)
62
Dan Willemsen0c3919e2017-03-02 15:49:10 -080063 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070064 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
65 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
66 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -080067 outDir := "out"
68 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
69 if wd, err := os.Getwd(); err != nil {
70 ctx.Fatalln("Failed to get working directory:", err)
71 } else {
72 outDir = filepath.Join(baseDir, filepath.Base(wd))
73 }
74 }
75 ret.environ.Set("OUT_DIR", outDir)
76 }
77
Dan Willemsen1e704462016-08-21 15:17:17 -070078 ret.environ.Unset(
79 // We're already using it
80 "USE_SOONG_UI",
81
82 // We should never use GOROOT/GOPATH from the shell environment
83 "GOROOT",
84 "GOPATH",
85
86 // These should only come from Soong, not the environment.
87 "CLANG",
88 "CLANG_CXX",
89 "CCC_CC",
90 "CCC_CXX",
91
92 // Used by the goma compiler wrapper, but should only be set by
93 // gomacc
94 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -080095
96 // We handle this above
97 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -070098
99 // Variables that have caused problems in the past
100 "DISPLAY",
101 "GREP_OPTIONS",
Dan Willemsenc40e10b2017-07-11 14:30:00 -0700102
103 // Drop make flags
104 "MAKEFLAGS",
105 "MAKELEVEL",
106 "MFLAGS",
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700107
108 // Set in envsetup.sh, reset in makefiles
109 "ANDROID_JAVA_TOOLCHAIN",
Dan Willemsen1e704462016-08-21 15:17:17 -0700110 )
111
112 // Tell python not to spam the source tree with .pyc files.
113 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
114
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800115 // Precondition: the current directory is the top of the source tree
116 if _, err := os.Stat(srcDirFileCheck); err != nil {
117 if os.IsNotExist(err) {
118 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
119 }
120 log.Fatalln("Error verifying tree state:", err)
121 }
122
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700123 if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') {
124 log.Println("You are building in a directory whose absolute path contains a space character:")
125 log.Println()
126 log.Printf("%q\n", srcDir)
127 log.Println()
128 log.Fatalln("Directory names containing spaces are not supported")
Dan Willemsendb8457c2017-05-12 16:38:17 -0700129 }
130
131 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
132 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
133 log.Println()
134 log.Printf("%q\n", outDir)
135 log.Println()
136 log.Fatalln("Directory names containing spaces are not supported")
137 }
138
139 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
140 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
141 log.Println()
142 log.Printf("%q\n", distDir)
143 log.Println()
144 log.Fatalln("Directory names containing spaces are not supported")
145 }
146
Dan Willemsend9e8f0a2017-10-30 13:42:06 -0700147 // Configure Java-related variables, including adding it to $PATH
148 javaHome := func() string {
149 if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok {
150 return override
151 }
152 if v, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK9"); ok && v != "" {
153 return filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag())
154 }
155 return filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag())
156 }()
157 absJavaHome := absPath(ctx, javaHome)
158
159 newPath := []string{filepath.Join(absJavaHome, "bin")}
160 if path, ok := ret.environ.Get("PATH"); ok && path != "" {
161 newPath = append(newPath, path)
162 }
163 ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME")
164 ret.environ.Set("JAVA_HOME", absJavaHome)
165 ret.environ.Set("ANDROID_JAVA_HOME", javaHome)
166 ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator)))
167
Dan Willemsen9b587492017-07-10 22:13:00 -0700168 return Config{ret}
169}
170
171func (c *configImpl) parseArgs(ctx Context, args []string) {
172 for i := 0; i < len(args); i++ {
173 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700174 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700175 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700176 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700177 } else if arg == "--skip-make" {
178 c.skipMake = true
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700179 } else if len(arg) > 0 && arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700180 parseArgNum := func(def int) int {
181 if len(arg) > 2 {
182 p, err := strconv.ParseUint(arg[2:], 10, 31)
183 if err != nil {
184 ctx.Fatalf("Failed to parse %q: %v", arg, err)
185 }
186 return int(p)
187 } else if i+1 < len(args) {
188 p, err := strconv.ParseUint(args[i+1], 10, 31)
189 if err == nil {
190 i++
191 return int(p)
192 }
193 }
194 return def
195 }
196
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700197 if len(arg) > 1 && arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700198 c.parallel = parseArgNum(c.parallel)
Dan Willemsen6ac63ef2017-10-17 20:35:34 -0700199 } else if len(arg) > 1 && arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700200 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700201 } else {
202 ctx.Fatalln("Unknown option:", arg)
203 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700204 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
205 c.environ.Set(k, v)
Dan Willemsen1e704462016-08-21 15:17:17 -0700206 } else {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700207 if arg == "dist" {
208 c.dist = true
209 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700210 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700211 }
212 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700213}
214
215// Lunch configures the environment for a specific product similarly to the
216// `lunch` bash function.
217func (c *configImpl) Lunch(ctx Context, product, variant string) {
218 if variant != "eng" && variant != "userdebug" && variant != "user" {
219 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
220 }
221
222 c.environ.Set("TARGET_PRODUCT", product)
223 c.environ.Set("TARGET_BUILD_VARIANT", variant)
224 c.environ.Set("TARGET_BUILD_TYPE", "release")
225 c.environ.Unset("TARGET_BUILD_APPS")
226}
227
228// Tapas configures the environment to build one or more unbundled apps,
229// similarly to the `tapas` bash function.
230func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
231 if len(apps) == 0 {
232 apps = []string{"all"}
233 }
234 if variant == "" {
235 variant = "eng"
236 }
237
238 if variant != "eng" && variant != "userdebug" && variant != "user" {
239 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
240 }
241
242 var product string
243 switch arch {
244 case "armv5":
245 product = "generic_armv5"
246 case "arm", "":
247 product = "aosp_arm"
248 case "arm64":
249 product = "aosm_arm64"
250 case "mips":
251 product = "aosp_mips"
252 case "mips64":
253 product = "aosp_mips64"
254 case "x86":
255 product = "aosp_x86"
256 case "x86_64":
257 product = "aosp_x86_64"
258 default:
259 ctx.Fatalf("Invalid architecture: %q", arch)
260 }
261
262 c.environ.Set("TARGET_PRODUCT", product)
263 c.environ.Set("TARGET_BUILD_VARIANT", variant)
264 c.environ.Set("TARGET_BUILD_TYPE", "release")
265 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
266}
267
268func (c *configImpl) Environment() *Environment {
269 return c.environ
270}
271
272func (c *configImpl) Arguments() []string {
273 return c.arguments
274}
275
276func (c *configImpl) OutDir() string {
277 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
278 return outDir
279 }
280 return "out"
281}
282
Dan Willemsen8a073a82017-02-04 17:30:44 -0800283func (c *configImpl) DistDir() string {
284 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
285 return distDir
286 }
287 return filepath.Join(c.OutDir(), "dist")
288}
289
Dan Willemsen1e704462016-08-21 15:17:17 -0700290func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700291 if c.skipMake {
292 return c.arguments
293 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700294 return c.ninjaArgs
295}
296
297func (c *configImpl) SoongOutDir() string {
298 return filepath.Join(c.OutDir(), "soong")
299}
300
Jeff Gastonefc1b412017-03-29 17:29:06 -0700301func (c *configImpl) TempDir() string {
302 return shared.TempDirForOutDir(c.SoongOutDir())
303}
304
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700305func (c *configImpl) FileListDir() string {
306 return filepath.Join(c.OutDir(), ".module_paths")
307}
308
Dan Willemsen1e704462016-08-21 15:17:17 -0700309func (c *configImpl) KatiSuffix() string {
310 if c.katiSuffix != "" {
311 return c.katiSuffix
312 }
313 panic("SetKatiSuffix has not been called")
314}
315
Dan Willemsen8a073a82017-02-04 17:30:44 -0800316func (c *configImpl) Dist() bool {
317 return c.dist
318}
319
Dan Willemsen1e704462016-08-21 15:17:17 -0700320func (c *configImpl) IsVerbose() bool {
321 return c.verbose
322}
323
Dan Willemsene0879fc2017-08-04 15:06:27 -0700324func (c *configImpl) SkipMake() bool {
325 return c.skipMake
326}
327
Dan Willemsen1e704462016-08-21 15:17:17 -0700328func (c *configImpl) TargetProduct() string {
329 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
330 return v
331 }
332 panic("TARGET_PRODUCT is not defined")
333}
334
Dan Willemsen02781d52017-05-12 19:28:13 -0700335func (c *configImpl) TargetDevice() string {
336 return c.targetDevice
337}
338
339func (c *configImpl) SetTargetDevice(device string) {
340 c.targetDevice = device
341}
342
343func (c *configImpl) TargetBuildVariant() string {
344 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
345 return v
346 }
347 panic("TARGET_BUILD_VARIANT is not defined")
348}
349
Dan Willemsen1e704462016-08-21 15:17:17 -0700350func (c *configImpl) KatiArgs() []string {
351 return c.katiArgs
352}
353
354func (c *configImpl) Parallel() int {
355 return c.parallel
356}
357
358func (c *configImpl) UseGoma() bool {
359 if v, ok := c.environ.Get("USE_GOMA"); ok {
360 v = strings.TrimSpace(v)
361 if v != "" && v != "false" {
362 return true
363 }
364 }
365 return false
366}
367
368// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700369// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700370// still limited by Parallel()
371func (c *configImpl) RemoteParallel() int {
372 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
373 if i, err := strconv.Atoi(v); err == nil {
374 return i
375 }
376 }
377 return 500
378}
379
380func (c *configImpl) SetKatiArgs(args []string) {
381 c.katiArgs = args
382}
383
384func (c *configImpl) SetNinjaArgs(args []string) {
385 c.ninjaArgs = args
386}
387
388func (c *configImpl) SetKatiSuffix(suffix string) {
389 c.katiSuffix = suffix
390}
391
Dan Willemsene0879fc2017-08-04 15:06:27 -0700392func (c *configImpl) LastKatiSuffixFile() string {
393 return filepath.Join(c.OutDir(), "last_kati_suffix")
394}
395
396func (c *configImpl) HasKatiSuffix() bool {
397 return c.katiSuffix != ""
398}
399
Dan Willemsen1e704462016-08-21 15:17:17 -0700400func (c *configImpl) KatiEnvFile() string {
401 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
402}
403
404func (c *configImpl) KatiNinjaFile() string {
405 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
406}
407
408func (c *configImpl) SoongNinjaFile() string {
409 return filepath.Join(c.SoongOutDir(), "build.ninja")
410}
411
412func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700413 if c.katiSuffix == "" {
414 return filepath.Join(c.OutDir(), "combined.ninja")
415 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700416 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
417}
418
419func (c *configImpl) SoongAndroidMk() string {
420 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
421}
422
423func (c *configImpl) SoongMakeVarsMk() string {
424 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
425}
426
Dan Willemsenf052f782017-05-18 15:29:04 -0700427func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700428 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700429}
430
Dan Willemsen02781d52017-05-12 19:28:13 -0700431func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700432 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
433}
434
435func (c *configImpl) hostOutRoot() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700436 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700437}
438
439func (c *configImpl) HostOut() string {
440 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
441}
442
443// This probably needs to be multi-valued, so not exporting it for now
444func (c *configImpl) hostCrossOut() string {
445 if runtime.GOOS == "linux" {
446 return filepath.Join(c.hostOutRoot(), "windows-x86")
447 } else {
448 return ""
449 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700450}
451
Dan Willemsen1e704462016-08-21 15:17:17 -0700452func (c *configImpl) HostPrebuiltTag() string {
453 if runtime.GOOS == "linux" {
454 return "linux-x86"
455 } else if runtime.GOOS == "darwin" {
456 return "darwin-x86"
457 } else {
458 panic("Unsupported OS")
459 }
460}
Dan Willemsenf173d592017-04-27 14:28:00 -0700461
Dan Willemsen8122bd52017-10-12 20:20:41 -0700462func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700463 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
464 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700465 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
466 if _, err := os.Stat(asan); err == nil {
467 return asan
468 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700469 }
470 }
471 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
472}