blob: 045f6748c9c5dd5d4d69a5f22246af2b89059253 [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 Willemsen1e704462016-08-21 15:17:17 -0700107 )
108
109 // Tell python not to spam the source tree with .pyc files.
110 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
111
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800112 // Precondition: the current directory is the top of the source tree
113 if _, err := os.Stat(srcDirFileCheck); err != nil {
114 if os.IsNotExist(err) {
115 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
116 }
117 log.Fatalln("Error verifying tree state:", err)
118 }
119
Dan Willemsendb8457c2017-05-12 16:38:17 -0700120 if srcDir, err := filepath.Abs("."); err == nil {
121 if strings.ContainsRune(srcDir, ' ') {
122 log.Println("You are building in a directory whose absolute path contains a space character:")
123 log.Println()
124 log.Printf("%q\n", srcDir)
125 log.Println()
126 log.Fatalln("Directory names containing spaces are not supported")
127 }
128 }
129
130 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
131 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
132 log.Println()
133 log.Printf("%q\n", outDir)
134 log.Println()
135 log.Fatalln("Directory names containing spaces are not supported")
136 }
137
138 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
139 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
140 log.Println()
141 log.Printf("%q\n", distDir)
142 log.Println()
143 log.Fatalln("Directory names containing spaces are not supported")
144 }
145
Dan Willemsen9b587492017-07-10 22:13:00 -0700146 return Config{ret}
147}
148
149func (c *configImpl) parseArgs(ctx Context, args []string) {
150 for i := 0; i < len(args); i++ {
151 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700152 if arg == "--make-mode" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700153 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700154 c.verbose = true
Dan Willemsene0879fc2017-08-04 15:06:27 -0700155 } else if arg == "--skip-make" {
156 c.skipMake = true
157 } else if arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700158 parseArgNum := func(def int) int {
159 if len(arg) > 2 {
160 p, err := strconv.ParseUint(arg[2:], 10, 31)
161 if err != nil {
162 ctx.Fatalf("Failed to parse %q: %v", arg, err)
163 }
164 return int(p)
165 } else if i+1 < len(args) {
166 p, err := strconv.ParseUint(args[i+1], 10, 31)
167 if err == nil {
168 i++
169 return int(p)
170 }
171 }
172 return def
173 }
174
Dan Willemsen1e704462016-08-21 15:17:17 -0700175 if arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700176 c.parallel = parseArgNum(c.parallel)
Dan Willemsen1e704462016-08-21 15:17:17 -0700177 } else if arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700178 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700179 } else {
180 ctx.Fatalln("Unknown option:", arg)
181 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700182 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
183 c.environ.Set(k, v)
Dan Willemsen1e704462016-08-21 15:17:17 -0700184 } else {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700185 if arg == "dist" {
186 c.dist = true
187 }
Dan Willemsen9b587492017-07-10 22:13:00 -0700188 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700189 }
190 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700191}
192
193// Lunch configures the environment for a specific product similarly to the
194// `lunch` bash function.
195func (c *configImpl) Lunch(ctx Context, product, variant string) {
196 if variant != "eng" && variant != "userdebug" && variant != "user" {
197 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
198 }
199
200 c.environ.Set("TARGET_PRODUCT", product)
201 c.environ.Set("TARGET_BUILD_VARIANT", variant)
202 c.environ.Set("TARGET_BUILD_TYPE", "release")
203 c.environ.Unset("TARGET_BUILD_APPS")
204}
205
206// Tapas configures the environment to build one or more unbundled apps,
207// similarly to the `tapas` bash function.
208func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
209 if len(apps) == 0 {
210 apps = []string{"all"}
211 }
212 if variant == "" {
213 variant = "eng"
214 }
215
216 if variant != "eng" && variant != "userdebug" && variant != "user" {
217 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
218 }
219
220 var product string
221 switch arch {
222 case "armv5":
223 product = "generic_armv5"
224 case "arm", "":
225 product = "aosp_arm"
226 case "arm64":
227 product = "aosm_arm64"
228 case "mips":
229 product = "aosp_mips"
230 case "mips64":
231 product = "aosp_mips64"
232 case "x86":
233 product = "aosp_x86"
234 case "x86_64":
235 product = "aosp_x86_64"
236 default:
237 ctx.Fatalf("Invalid architecture: %q", arch)
238 }
239
240 c.environ.Set("TARGET_PRODUCT", product)
241 c.environ.Set("TARGET_BUILD_VARIANT", variant)
242 c.environ.Set("TARGET_BUILD_TYPE", "release")
243 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
244}
245
246func (c *configImpl) Environment() *Environment {
247 return c.environ
248}
249
250func (c *configImpl) Arguments() []string {
251 return c.arguments
252}
253
254func (c *configImpl) OutDir() string {
255 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
256 return outDir
257 }
258 return "out"
259}
260
Dan Willemsen8a073a82017-02-04 17:30:44 -0800261func (c *configImpl) DistDir() string {
262 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
263 return distDir
264 }
265 return filepath.Join(c.OutDir(), "dist")
266}
267
Dan Willemsen1e704462016-08-21 15:17:17 -0700268func (c *configImpl) NinjaArgs() []string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700269 if c.skipMake {
270 return c.arguments
271 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700272 return c.ninjaArgs
273}
274
275func (c *configImpl) SoongOutDir() string {
276 return filepath.Join(c.OutDir(), "soong")
277}
278
Jeff Gastonefc1b412017-03-29 17:29:06 -0700279func (c *configImpl) TempDir() string {
280 return shared.TempDirForOutDir(c.SoongOutDir())
281}
282
Dan Willemsen1e704462016-08-21 15:17:17 -0700283func (c *configImpl) KatiSuffix() string {
284 if c.katiSuffix != "" {
285 return c.katiSuffix
286 }
287 panic("SetKatiSuffix has not been called")
288}
289
Dan Willemsen8a073a82017-02-04 17:30:44 -0800290func (c *configImpl) Dist() bool {
291 return c.dist
292}
293
Dan Willemsen1e704462016-08-21 15:17:17 -0700294func (c *configImpl) IsVerbose() bool {
295 return c.verbose
296}
297
Dan Willemsene0879fc2017-08-04 15:06:27 -0700298func (c *configImpl) SkipMake() bool {
299 return c.skipMake
300}
301
Dan Willemsen1e704462016-08-21 15:17:17 -0700302func (c *configImpl) TargetProduct() string {
303 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
304 return v
305 }
306 panic("TARGET_PRODUCT is not defined")
307}
308
Dan Willemsen02781d52017-05-12 19:28:13 -0700309func (c *configImpl) TargetDevice() string {
310 return c.targetDevice
311}
312
313func (c *configImpl) SetTargetDevice(device string) {
314 c.targetDevice = device
315}
316
317func (c *configImpl) TargetBuildVariant() string {
318 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
319 return v
320 }
321 panic("TARGET_BUILD_VARIANT is not defined")
322}
323
Dan Willemsen1e704462016-08-21 15:17:17 -0700324func (c *configImpl) KatiArgs() []string {
325 return c.katiArgs
326}
327
328func (c *configImpl) Parallel() int {
329 return c.parallel
330}
331
332func (c *configImpl) UseGoma() bool {
333 if v, ok := c.environ.Get("USE_GOMA"); ok {
334 v = strings.TrimSpace(v)
335 if v != "" && v != "false" {
336 return true
337 }
338 }
339 return false
340}
341
342// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700343// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700344// still limited by Parallel()
345func (c *configImpl) RemoteParallel() int {
346 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
347 if i, err := strconv.Atoi(v); err == nil {
348 return i
349 }
350 }
351 return 500
352}
353
354func (c *configImpl) SetKatiArgs(args []string) {
355 c.katiArgs = args
356}
357
358func (c *configImpl) SetNinjaArgs(args []string) {
359 c.ninjaArgs = args
360}
361
362func (c *configImpl) SetKatiSuffix(suffix string) {
363 c.katiSuffix = suffix
364}
365
Dan Willemsene0879fc2017-08-04 15:06:27 -0700366func (c *configImpl) LastKatiSuffixFile() string {
367 return filepath.Join(c.OutDir(), "last_kati_suffix")
368}
369
370func (c *configImpl) HasKatiSuffix() bool {
371 return c.katiSuffix != ""
372}
373
Dan Willemsen1e704462016-08-21 15:17:17 -0700374func (c *configImpl) KatiEnvFile() string {
375 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
376}
377
378func (c *configImpl) KatiNinjaFile() string {
379 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
380}
381
382func (c *configImpl) SoongNinjaFile() string {
383 return filepath.Join(c.SoongOutDir(), "build.ninja")
384}
385
386func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700387 if c.katiSuffix == "" {
388 return filepath.Join(c.OutDir(), "combined.ninja")
389 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700390 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
391}
392
393func (c *configImpl) SoongAndroidMk() string {
394 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
395}
396
397func (c *configImpl) SoongMakeVarsMk() string {
398 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
399}
400
Dan Willemsenf052f782017-05-18 15:29:04 -0700401func (c *configImpl) ProductOut() string {
402 if buildType, ok := c.environ.Get("TARGET_BUILD_TYPE"); ok && buildType == "debug" {
403 return filepath.Join(c.OutDir(), "debug", "target", "product", c.TargetDevice())
404 } else {
405 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
406 }
407}
408
Dan Willemsen02781d52017-05-12 19:28:13 -0700409func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700410 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
411}
412
413func (c *configImpl) hostOutRoot() string {
414 if buildType, ok := c.environ.Get("HOST_BUILD_TYPE"); ok && buildType == "debug" {
415 return filepath.Join(c.OutDir(), "debug", "host")
416 } else {
417 return filepath.Join(c.OutDir(), "host")
418 }
419}
420
421func (c *configImpl) HostOut() string {
422 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
423}
424
425// This probably needs to be multi-valued, so not exporting it for now
426func (c *configImpl) hostCrossOut() string {
427 if runtime.GOOS == "linux" {
428 return filepath.Join(c.hostOutRoot(), "windows-x86")
429 } else {
430 return ""
431 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700432}
433
Dan Willemsen1e704462016-08-21 15:17:17 -0700434func (c *configImpl) HostPrebuiltTag() string {
435 if runtime.GOOS == "linux" {
436 return "linux-x86"
437 } else if runtime.GOOS == "darwin" {
438 return "darwin-x86"
439 } else {
440 panic("Unsupported OS")
441 }
442}
Dan Willemsenf173d592017-04-27 14:28:00 -0700443
Dan Willemsena3e6c522017-05-05 15:29:20 -0700444func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700445 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
446 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700447 return true
448 }
449 }
450 return false
451}
452
453func (c *configImpl) PrebuiltBuildTool(name string) string {
454 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
455 if c.HostAsan() || (c.Dist() && name == "ckati") {
456 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
457 if _, err := os.Stat(asan); err == nil {
458 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700459 }
460 }
461 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
462}