blob: c8d729213ae5b968a64468ce4aeecd35d7163693 [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
Jeff Gastonb64fc1c2017-08-04 12:30:12 -0700283func (c *configImpl) FileListDir() string {
284 return filepath.Join(c.OutDir(), ".module_paths")
285}
286
Dan Willemsen1e704462016-08-21 15:17:17 -0700287func (c *configImpl) KatiSuffix() string {
288 if c.katiSuffix != "" {
289 return c.katiSuffix
290 }
291 panic("SetKatiSuffix has not been called")
292}
293
Dan Willemsen8a073a82017-02-04 17:30:44 -0800294func (c *configImpl) Dist() bool {
295 return c.dist
296}
297
Dan Willemsen1e704462016-08-21 15:17:17 -0700298func (c *configImpl) IsVerbose() bool {
299 return c.verbose
300}
301
Dan Willemsene0879fc2017-08-04 15:06:27 -0700302func (c *configImpl) SkipMake() bool {
303 return c.skipMake
304}
305
Dan Willemsen1e704462016-08-21 15:17:17 -0700306func (c *configImpl) TargetProduct() string {
307 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
308 return v
309 }
310 panic("TARGET_PRODUCT is not defined")
311}
312
Dan Willemsen02781d52017-05-12 19:28:13 -0700313func (c *configImpl) TargetDevice() string {
314 return c.targetDevice
315}
316
317func (c *configImpl) SetTargetDevice(device string) {
318 c.targetDevice = device
319}
320
321func (c *configImpl) TargetBuildVariant() string {
322 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
323 return v
324 }
325 panic("TARGET_BUILD_VARIANT is not defined")
326}
327
Dan Willemsen1e704462016-08-21 15:17:17 -0700328func (c *configImpl) KatiArgs() []string {
329 return c.katiArgs
330}
331
332func (c *configImpl) Parallel() int {
333 return c.parallel
334}
335
336func (c *configImpl) UseGoma() bool {
337 if v, ok := c.environ.Get("USE_GOMA"); ok {
338 v = strings.TrimSpace(v)
339 if v != "" && v != "false" {
340 return true
341 }
342 }
343 return false
344}
345
346// RemoteParallel controls how many remote jobs (i.e., commands which contain
Jeff Gastonefc1b412017-03-29 17:29:06 -0700347// gomacc) are run in parallel. Note the parallelism of all other jobs is
Dan Willemsen1e704462016-08-21 15:17:17 -0700348// still limited by Parallel()
349func (c *configImpl) RemoteParallel() int {
350 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
351 if i, err := strconv.Atoi(v); err == nil {
352 return i
353 }
354 }
355 return 500
356}
357
358func (c *configImpl) SetKatiArgs(args []string) {
359 c.katiArgs = args
360}
361
362func (c *configImpl) SetNinjaArgs(args []string) {
363 c.ninjaArgs = args
364}
365
366func (c *configImpl) SetKatiSuffix(suffix string) {
367 c.katiSuffix = suffix
368}
369
Dan Willemsene0879fc2017-08-04 15:06:27 -0700370func (c *configImpl) LastKatiSuffixFile() string {
371 return filepath.Join(c.OutDir(), "last_kati_suffix")
372}
373
374func (c *configImpl) HasKatiSuffix() bool {
375 return c.katiSuffix != ""
376}
377
Dan Willemsen1e704462016-08-21 15:17:17 -0700378func (c *configImpl) KatiEnvFile() string {
379 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
380}
381
382func (c *configImpl) KatiNinjaFile() string {
383 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
384}
385
386func (c *configImpl) SoongNinjaFile() string {
387 return filepath.Join(c.SoongOutDir(), "build.ninja")
388}
389
390func (c *configImpl) CombinedNinjaFile() string {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700391 if c.katiSuffix == "" {
392 return filepath.Join(c.OutDir(), "combined.ninja")
393 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700394 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
395}
396
397func (c *configImpl) SoongAndroidMk() string {
398 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
399}
400
401func (c *configImpl) SoongMakeVarsMk() string {
402 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
403}
404
Dan Willemsenf052f782017-05-18 15:29:04 -0700405func (c *configImpl) ProductOut() string {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700406 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice())
Dan Willemsenf052f782017-05-18 15:29:04 -0700407}
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 {
Dan Willemsen4dc4e142017-09-08 14:35:43 -0700414 return filepath.Join(c.OutDir(), "host")
Dan Willemsenf052f782017-05-18 15:29:04 -0700415}
416
417func (c *configImpl) HostOut() string {
418 return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag())
419}
420
421// This probably needs to be multi-valued, so not exporting it for now
422func (c *configImpl) hostCrossOut() string {
423 if runtime.GOOS == "linux" {
424 return filepath.Join(c.hostOutRoot(), "windows-x86")
425 } else {
426 return ""
427 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700428}
429
Dan Willemsen1e704462016-08-21 15:17:17 -0700430func (c *configImpl) HostPrebuiltTag() string {
431 if runtime.GOOS == "linux" {
432 return "linux-x86"
433 } else if runtime.GOOS == "darwin" {
434 return "darwin-x86"
435 } else {
436 panic("Unsupported OS")
437 }
438}
Dan Willemsenf173d592017-04-27 14:28:00 -0700439
Dan Willemsen8122bd52017-10-12 20:20:41 -0700440func (c *configImpl) PrebuiltBuildTool(name string) string {
Dan Willemsenf173d592017-04-27 14:28:00 -0700441 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
442 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsen8122bd52017-10-12 20:20:41 -0700443 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
444 if _, err := os.Stat(asan); err == nil {
445 return asan
446 }
Dan Willemsenf173d592017-04-27 14:28:00 -0700447 }
448 }
449 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
450}