blob: 925b15385b81900fd08418b26d767c2e37e4ca2a [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 Willemsen1e704462016-08-21 15:17:17 -070041
42 // From the product config
Dan Willemsen02781d52017-05-12 19:28:13 -070043 katiArgs []string
44 ninjaArgs []string
45 katiSuffix string
46 targetDevice string
Dan Willemsen1e704462016-08-21 15:17:17 -070047}
48
Dan Willemsenc2af0be2017-01-20 14:10:01 -080049const srcDirFileCheck = "build/soong/root.bp"
50
Dan Willemsen1e704462016-08-21 15:17:17 -070051func NewConfig(ctx Context, args ...string) Config {
52 ret := &configImpl{
53 environ: OsEnvironment(),
54 }
55
Dan Willemsen9b587492017-07-10 22:13:00 -070056 // Sane default matching ninja
57 ret.parallel = runtime.NumCPU() + 2
58 ret.keepGoing = 1
59
60 ret.parseArgs(ctx, args)
61
Dan Willemsen0c3919e2017-03-02 15:49:10 -080062 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070063 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
64 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
65 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -080066 outDir := "out"
67 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
68 if wd, err := os.Getwd(); err != nil {
69 ctx.Fatalln("Failed to get working directory:", err)
70 } else {
71 outDir = filepath.Join(baseDir, filepath.Base(wd))
72 }
73 }
74 ret.environ.Set("OUT_DIR", outDir)
75 }
76
Dan Willemsen1e704462016-08-21 15:17:17 -070077 ret.environ.Unset(
78 // We're already using it
79 "USE_SOONG_UI",
80
81 // We should never use GOROOT/GOPATH from the shell environment
82 "GOROOT",
83 "GOPATH",
84
85 // These should only come from Soong, not the environment.
86 "CLANG",
87 "CLANG_CXX",
88 "CCC_CC",
89 "CCC_CXX",
90
91 // Used by the goma compiler wrapper, but should only be set by
92 // gomacc
93 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -080094
95 // We handle this above
96 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -070097
98 // Variables that have caused problems in the past
99 "DISPLAY",
100 "GREP_OPTIONS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700101 )
102
103 // Tell python not to spam the source tree with .pyc files.
104 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
105
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800106 // Precondition: the current directory is the top of the source tree
107 if _, err := os.Stat(srcDirFileCheck); err != nil {
108 if os.IsNotExist(err) {
109 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
110 }
111 log.Fatalln("Error verifying tree state:", err)
112 }
113
Dan Willemsendb8457c2017-05-12 16:38:17 -0700114 if srcDir, err := filepath.Abs("."); err == nil {
115 if strings.ContainsRune(srcDir, ' ') {
116 log.Println("You are building in a directory whose absolute path contains a space character:")
117 log.Println()
118 log.Printf("%q\n", srcDir)
119 log.Println()
120 log.Fatalln("Directory names containing spaces are not supported")
121 }
122 }
123
124 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
125 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
126 log.Println()
127 log.Printf("%q\n", outDir)
128 log.Println()
129 log.Fatalln("Directory names containing spaces are not supported")
130 }
131
132 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
133 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
134 log.Println()
135 log.Printf("%q\n", distDir)
136 log.Println()
137 log.Fatalln("Directory names containing spaces are not supported")
138 }
139
Dan Willemsen9b587492017-07-10 22:13:00 -0700140 return Config{ret}
141}
142
143func (c *configImpl) parseArgs(ctx Context, args []string) {
144 for i := 0; i < len(args); i++ {
145 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700146 if arg == "--make-mode" {
147 continue
148 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700149 c.verbose = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700150 continue
Dan Willemsen8a073a82017-02-04 17:30:44 -0800151 } else if arg == "dist" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700152 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700153 }
154 if arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700155 parseArgNum := func(def int) int {
156 if len(arg) > 2 {
157 p, err := strconv.ParseUint(arg[2:], 10, 31)
158 if err != nil {
159 ctx.Fatalf("Failed to parse %q: %v", arg, err)
160 }
161 return int(p)
162 } else if i+1 < len(args) {
163 p, err := strconv.ParseUint(args[i+1], 10, 31)
164 if err == nil {
165 i++
166 return int(p)
167 }
168 }
169 return def
170 }
171
Dan Willemsen1e704462016-08-21 15:17:17 -0700172 if arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700173 c.parallel = parseArgNum(c.parallel)
Dan Willemsen1e704462016-08-21 15:17:17 -0700174 } else if arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700175 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700176 } else {
177 ctx.Fatalln("Unknown option:", arg)
178 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700179 } else {
Dan Willemsen9b587492017-07-10 22:13:00 -0700180 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700181 }
182 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700183}
184
185// Lunch configures the environment for a specific product similarly to the
186// `lunch` bash function.
187func (c *configImpl) Lunch(ctx Context, product, variant string) {
188 if variant != "eng" && variant != "userdebug" && variant != "user" {
189 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
190 }
191
192 c.environ.Set("TARGET_PRODUCT", product)
193 c.environ.Set("TARGET_BUILD_VARIANT", variant)
194 c.environ.Set("TARGET_BUILD_TYPE", "release")
195 c.environ.Unset("TARGET_BUILD_APPS")
196}
197
198// Tapas configures the environment to build one or more unbundled apps,
199// similarly to the `tapas` bash function.
200func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
201 if len(apps) == 0 {
202 apps = []string{"all"}
203 }
204 if variant == "" {
205 variant = "eng"
206 }
207
208 if variant != "eng" && variant != "userdebug" && variant != "user" {
209 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
210 }
211
212 var product string
213 switch arch {
214 case "armv5":
215 product = "generic_armv5"
216 case "arm", "":
217 product = "aosp_arm"
218 case "arm64":
219 product = "aosm_arm64"
220 case "mips":
221 product = "aosp_mips"
222 case "mips64":
223 product = "aosp_mips64"
224 case "x86":
225 product = "aosp_x86"
226 case "x86_64":
227 product = "aosp_x86_64"
228 default:
229 ctx.Fatalf("Invalid architecture: %q", arch)
230 }
231
232 c.environ.Set("TARGET_PRODUCT", product)
233 c.environ.Set("TARGET_BUILD_VARIANT", variant)
234 c.environ.Set("TARGET_BUILD_TYPE", "release")
235 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
236}
237
238func (c *configImpl) Environment() *Environment {
239 return c.environ
240}
241
242func (c *configImpl) Arguments() []string {
243 return c.arguments
244}
245
246func (c *configImpl) OutDir() string {
247 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
248 return outDir
249 }
250 return "out"
251}
252
Dan Willemsen8a073a82017-02-04 17:30:44 -0800253func (c *configImpl) DistDir() string {
254 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
255 return distDir
256 }
257 return filepath.Join(c.OutDir(), "dist")
258}
259
Dan Willemsen1e704462016-08-21 15:17:17 -0700260func (c *configImpl) NinjaArgs() []string {
261 return c.ninjaArgs
262}
263
264func (c *configImpl) SoongOutDir() string {
265 return filepath.Join(c.OutDir(), "soong")
266}
267
Jeff Gastonefc1b412017-03-29 17:29:06 -0700268func (c *configImpl) TempDir() string {
269 return shared.TempDirForOutDir(c.SoongOutDir())
270}
271
Dan Willemsen1e704462016-08-21 15:17:17 -0700272func (c *configImpl) KatiSuffix() string {
273 if c.katiSuffix != "" {
274 return c.katiSuffix
275 }
276 panic("SetKatiSuffix has not been called")
277}
278
Dan Willemsen8a073a82017-02-04 17:30:44 -0800279func (c *configImpl) Dist() bool {
280 return c.dist
281}
282
Dan Willemsen1e704462016-08-21 15:17:17 -0700283func (c *configImpl) IsVerbose() bool {
284 return c.verbose
285}
286
287func (c *configImpl) TargetProduct() string {
288 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
289 return v
290 }
291 panic("TARGET_PRODUCT is not defined")
292}
293
Dan Willemsen02781d52017-05-12 19:28:13 -0700294func (c *configImpl) TargetDevice() string {
295 return c.targetDevice
296}
297
298func (c *configImpl) SetTargetDevice(device string) {
299 c.targetDevice = device
300}
301
302func (c *configImpl) TargetBuildVariant() string {
303 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
304 return v
305 }
306 panic("TARGET_BUILD_VARIANT is not defined")
307}
308
Dan Willemsen1e704462016-08-21 15:17:17 -0700309func (c *configImpl) KatiArgs() []string {
310 return c.katiArgs
311}
312
313func (c *configImpl) Parallel() int {
314 return c.parallel
315}
316
317func (c *configImpl) UseGoma() bool {
318 if v, ok := c.environ.Get("USE_GOMA"); ok {
319 v = strings.TrimSpace(v)
320 if v != "" && v != "false" {
321 return true
322 }
323 }
324 return false
325}
326
327// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700328// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700329// still limited by Parallel()
330func (c *configImpl) RemoteParallel() int {
331 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
332 if i, err := strconv.Atoi(v); err == nil {
333 return i
334 }
335 }
336 return 500
337}
338
339func (c *configImpl) SetKatiArgs(args []string) {
340 c.katiArgs = args
341}
342
343func (c *configImpl) SetNinjaArgs(args []string) {
344 c.ninjaArgs = args
345}
346
347func (c *configImpl) SetKatiSuffix(suffix string) {
348 c.katiSuffix = suffix
349}
350
351func (c *configImpl) KatiEnvFile() string {
352 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
353}
354
355func (c *configImpl) KatiNinjaFile() string {
356 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
357}
358
359func (c *configImpl) SoongNinjaFile() string {
360 return filepath.Join(c.SoongOutDir(), "build.ninja")
361}
362
363func (c *configImpl) CombinedNinjaFile() string {
364 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
365}
366
367func (c *configImpl) SoongAndroidMk() string {
368 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
369}
370
371func (c *configImpl) SoongMakeVarsMk() string {
372 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
373}
374
Dan Willemsenf052f782017-05-18 15:29:04 -0700375func (c *configImpl) ProductOut() string {
376 if buildType, ok := c.environ.Get("TARGET_BUILD_TYPE"); ok && buildType == "debug" {
377 return filepath.Join(c.OutDir(), "debug", "target", "product", c.TargetDevice())
378 } else {
379 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
380 }
381}
382
Dan Willemsen02781d52017-05-12 19:28:13 -0700383func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700384 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
385}
386
387func (c *configImpl) hostOutRoot() string {
388 if buildType, ok := c.environ.Get("HOST_BUILD_TYPE"); ok && buildType == "debug" {
389 return filepath.Join(c.OutDir(), "debug", "host")
390 } else {
391 return filepath.Join(c.OutDir(), "host")
392 }
393}
394
395func (c *configImpl) HostOut() string {
396 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
397}
398
399// This probably needs to be multi-valued, so not exporting it for now
400func (c *configImpl) hostCrossOut() string {
401 if runtime.GOOS == "linux" {
402 return filepath.Join(c.hostOutRoot(), "windows-x86")
403 } else {
404 return ""
405 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700406}
407
Dan Willemsen1e704462016-08-21 15:17:17 -0700408func (c *configImpl) HostPrebuiltTag() string {
409 if runtime.GOOS == "linux" {
410 return "linux-x86"
411 } else if runtime.GOOS == "darwin" {
412 return "darwin-x86"
413 } else {
414 panic("Unsupported OS")
415 }
416}
Dan Willemsenf173d592017-04-27 14:28:00 -0700417
Dan Willemsena3e6c522017-05-05 15:29:20 -0700418func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700419 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
420 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700421 return true
422 }
423 }
424 return false
425}
426
427func (c *configImpl) PrebuiltBuildTool(name string) string {
428 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
429 if c.HostAsan() || (c.Dist() && name == "ckati") {
430 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
431 if _, err := os.Stat(asan); err == nil {
432 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700433 }
434 }
435 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
436}