blob: ef0615768c8e1f805035b5c3f8c58a4587d2e460 [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 Willemsenc40e10b2017-07-11 14:30:00 -0700101
102 // Drop make flags
103 "MAKEFLAGS",
104 "MAKELEVEL",
105 "MFLAGS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700106 )
107
108 // Tell python not to spam the source tree with .pyc files.
109 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
110
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800111 // Precondition: the current directory is the top of the source tree
112 if _, err := os.Stat(srcDirFileCheck); err != nil {
113 if os.IsNotExist(err) {
114 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
115 }
116 log.Fatalln("Error verifying tree state:", err)
117 }
118
Dan Willemsendb8457c2017-05-12 16:38:17 -0700119 if srcDir, err := filepath.Abs("."); err == nil {
120 if strings.ContainsRune(srcDir, ' ') {
121 log.Println("You are building in a directory whose absolute path contains a space character:")
122 log.Println()
123 log.Printf("%q\n", srcDir)
124 log.Println()
125 log.Fatalln("Directory names containing spaces are not supported")
126 }
127 }
128
129 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
130 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
131 log.Println()
132 log.Printf("%q\n", outDir)
133 log.Println()
134 log.Fatalln("Directory names containing spaces are not supported")
135 }
136
137 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
138 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
139 log.Println()
140 log.Printf("%q\n", distDir)
141 log.Println()
142 log.Fatalln("Directory names containing spaces are not supported")
143 }
144
Dan Willemsen9b587492017-07-10 22:13:00 -0700145 return Config{ret}
146}
147
148func (c *configImpl) parseArgs(ctx Context, args []string) {
149 for i := 0; i < len(args); i++ {
150 arg := strings.TrimSpace(args[i])
Dan Willemsen1e704462016-08-21 15:17:17 -0700151 if arg == "--make-mode" {
152 continue
153 } else if arg == "showcommands" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700154 c.verbose = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700155 continue
Dan Willemsen8a073a82017-02-04 17:30:44 -0800156 } else if arg == "dist" {
Dan Willemsen9b587492017-07-10 22:13:00 -0700157 c.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700158 }
159 if arg[0] == '-' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700160 parseArgNum := func(def int) int {
161 if len(arg) > 2 {
162 p, err := strconv.ParseUint(arg[2:], 10, 31)
163 if err != nil {
164 ctx.Fatalf("Failed to parse %q: %v", arg, err)
165 }
166 return int(p)
167 } else if i+1 < len(args) {
168 p, err := strconv.ParseUint(args[i+1], 10, 31)
169 if err == nil {
170 i++
171 return int(p)
172 }
173 }
174 return def
175 }
176
Dan Willemsen1e704462016-08-21 15:17:17 -0700177 if arg[1] == 'j' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700178 c.parallel = parseArgNum(c.parallel)
Dan Willemsen1e704462016-08-21 15:17:17 -0700179 } else if arg[1] == 'k' {
Dan Willemsen9b587492017-07-10 22:13:00 -0700180 c.keepGoing = parseArgNum(0)
Dan Willemsen1e704462016-08-21 15:17:17 -0700181 } else {
182 ctx.Fatalln("Unknown option:", arg)
183 }
Dan Willemsen091525e2017-07-11 14:17:50 -0700184 } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 {
185 c.environ.Set(k, v)
Dan Willemsen1e704462016-08-21 15:17:17 -0700186 } else {
Dan Willemsen9b587492017-07-10 22:13:00 -0700187 c.arguments = append(c.arguments, arg)
Dan Willemsen1e704462016-08-21 15:17:17 -0700188 }
189 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700190}
191
192// Lunch configures the environment for a specific product similarly to the
193// `lunch` bash function.
194func (c *configImpl) Lunch(ctx Context, product, variant string) {
195 if variant != "eng" && variant != "userdebug" && variant != "user" {
196 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
197 }
198
199 c.environ.Set("TARGET_PRODUCT", product)
200 c.environ.Set("TARGET_BUILD_VARIANT", variant)
201 c.environ.Set("TARGET_BUILD_TYPE", "release")
202 c.environ.Unset("TARGET_BUILD_APPS")
203}
204
205// Tapas configures the environment to build one or more unbundled apps,
206// similarly to the `tapas` bash function.
207func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
208 if len(apps) == 0 {
209 apps = []string{"all"}
210 }
211 if variant == "" {
212 variant = "eng"
213 }
214
215 if variant != "eng" && variant != "userdebug" && variant != "user" {
216 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
217 }
218
219 var product string
220 switch arch {
221 case "armv5":
222 product = "generic_armv5"
223 case "arm", "":
224 product = "aosp_arm"
225 case "arm64":
226 product = "aosm_arm64"
227 case "mips":
228 product = "aosp_mips"
229 case "mips64":
230 product = "aosp_mips64"
231 case "x86":
232 product = "aosp_x86"
233 case "x86_64":
234 product = "aosp_x86_64"
235 default:
236 ctx.Fatalf("Invalid architecture: %q", arch)
237 }
238
239 c.environ.Set("TARGET_PRODUCT", product)
240 c.environ.Set("TARGET_BUILD_VARIANT", variant)
241 c.environ.Set("TARGET_BUILD_TYPE", "release")
242 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
243}
244
245func (c *configImpl) Environment() *Environment {
246 return c.environ
247}
248
249func (c *configImpl) Arguments() []string {
250 return c.arguments
251}
252
253func (c *configImpl) OutDir() string {
254 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
255 return outDir
256 }
257 return "out"
258}
259
Dan Willemsen8a073a82017-02-04 17:30:44 -0800260func (c *configImpl) DistDir() string {
261 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
262 return distDir
263 }
264 return filepath.Join(c.OutDir(), "dist")
265}
266
Dan Willemsen1e704462016-08-21 15:17:17 -0700267func (c *configImpl) NinjaArgs() []string {
268 return c.ninjaArgs
269}
270
271func (c *configImpl) SoongOutDir() string {
272 return filepath.Join(c.OutDir(), "soong")
273}
274
Jeff Gastonefc1b412017-03-29 17:29:06 -0700275func (c *configImpl) TempDir() string {
276 return shared.TempDirForOutDir(c.SoongOutDir())
277}
278
Dan Willemsen1e704462016-08-21 15:17:17 -0700279func (c *configImpl) KatiSuffix() string {
280 if c.katiSuffix != "" {
281 return c.katiSuffix
282 }
283 panic("SetKatiSuffix has not been called")
284}
285
Dan Willemsen8a073a82017-02-04 17:30:44 -0800286func (c *configImpl) Dist() bool {
287 return c.dist
288}
289
Dan Willemsen1e704462016-08-21 15:17:17 -0700290func (c *configImpl) IsVerbose() bool {
291 return c.verbose
292}
293
294func (c *configImpl) TargetProduct() string {
295 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
296 return v
297 }
298 panic("TARGET_PRODUCT is not defined")
299}
300
Dan Willemsen02781d52017-05-12 19:28:13 -0700301func (c *configImpl) TargetDevice() string {
302 return c.targetDevice
303}
304
305func (c *configImpl) SetTargetDevice(device string) {
306 c.targetDevice = device
307}
308
309func (c *configImpl) TargetBuildVariant() string {
310 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
311 return v
312 }
313 panic("TARGET_BUILD_VARIANT is not defined")
314}
315
Dan Willemsen1e704462016-08-21 15:17:17 -0700316func (c *configImpl) KatiArgs() []string {
317 return c.katiArgs
318}
319
320func (c *configImpl) Parallel() int {
321 return c.parallel
322}
323
324func (c *configImpl) UseGoma() bool {
325 if v, ok := c.environ.Get("USE_GOMA"); ok {
326 v = strings.TrimSpace(v)
327 if v != "" && v != "false" {
328 return true
329 }
330 }
331 return false
332}
333
334// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700335// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700336// still limited by Parallel()
337func (c *configImpl) RemoteParallel() int {
338 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
339 if i, err := strconv.Atoi(v); err == nil {
340 return i
341 }
342 }
343 return 500
344}
345
346func (c *configImpl) SetKatiArgs(args []string) {
347 c.katiArgs = args
348}
349
350func (c *configImpl) SetNinjaArgs(args []string) {
351 c.ninjaArgs = args
352}
353
354func (c *configImpl) SetKatiSuffix(suffix string) {
355 c.katiSuffix = suffix
356}
357
358func (c *configImpl) KatiEnvFile() string {
359 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
360}
361
362func (c *configImpl) KatiNinjaFile() string {
363 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
364}
365
366func (c *configImpl) SoongNinjaFile() string {
367 return filepath.Join(c.SoongOutDir(), "build.ninja")
368}
369
370func (c *configImpl) CombinedNinjaFile() string {
371 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
372}
373
374func (c *configImpl) SoongAndroidMk() string {
375 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
376}
377
378func (c *configImpl) SoongMakeVarsMk() string {
379 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
380}
381
Dan Willemsenf052f782017-05-18 15:29:04 -0700382func (c *configImpl) ProductOut() string {
383 if buildType, ok := c.environ.Get("TARGET_BUILD_TYPE"); ok && buildType == "debug" {
384 return filepath.Join(c.OutDir(), "debug", "target", "product", c.TargetDevice())
385 } else {
386 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
387 }
388}
389
Dan Willemsen02781d52017-05-12 19:28:13 -0700390func (c *configImpl) DevicePreviousProductConfig() string {
Dan Willemsenf052f782017-05-18 15:29:04 -0700391 return filepath.Join(c.ProductOut(), "previous_build_config.mk")
392}
393
394func (c *configImpl) hostOutRoot() string {
395 if buildType, ok := c.environ.Get("HOST_BUILD_TYPE"); ok && buildType == "debug" {
396 return filepath.Join(c.OutDir(), "debug", "host")
397 } else {
398 return filepath.Join(c.OutDir(), "host")
399 }
400}
401
402func (c *configImpl) HostOut() string {
403 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
404}
405
406// This probably needs to be multi-valued, so not exporting it for now
407func (c *configImpl) hostCrossOut() string {
408 if runtime.GOOS == "linux" {
409 return filepath.Join(c.hostOutRoot(), "windows-x86")
410 } else {
411 return ""
412 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700413}
414
Dan Willemsen1e704462016-08-21 15:17:17 -0700415func (c *configImpl) HostPrebuiltTag() string {
416 if runtime.GOOS == "linux" {
417 return "linux-x86"
418 } else if runtime.GOOS == "darwin" {
419 return "darwin-x86"
420 } else {
421 panic("Unsupported OS")
422 }
423}
Dan Willemsenf173d592017-04-27 14:28:00 -0700424
Dan Willemsena3e6c522017-05-05 15:29:20 -0700425func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700426 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
427 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700428 return true
429 }
430 }
431 return false
432}
433
434func (c *configImpl) PrebuiltBuildTool(name string) string {
435 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
436 if c.HostAsan() || (c.Dist() && name == "ckati") {
437 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
438 if _, err := os.Stat(asan); err == nil {
439 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700440 }
441 }
442 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
443}