blob: 51cff5069e0a64df6a0e94c1981d863cbc114c02 [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"
24)
25
26type Config struct{ *configImpl }
27
28type configImpl struct {
29 // From the environment
30 arguments []string
31 goma bool
32 environ *Environment
33
34 // From the arguments
35 parallel int
36 keepGoing int
37 verbose bool
Dan Willemsen8a073a82017-02-04 17:30:44 -080038 dist bool
Dan Willemsen1e704462016-08-21 15:17:17 -070039
40 // From the product config
Dan Willemsen02781d52017-05-12 19:28:13 -070041 katiArgs []string
42 ninjaArgs []string
43 katiSuffix string
44 targetDevice string
Dan Willemsen1e704462016-08-21 15:17:17 -070045}
46
Dan Willemsenc2af0be2017-01-20 14:10:01 -080047const srcDirFileCheck = "build/soong/root.bp"
48
Dan Willemsen1e704462016-08-21 15:17:17 -070049func NewConfig(ctx Context, args ...string) Config {
50 ret := &configImpl{
51 environ: OsEnvironment(),
52 }
53
Dan Willemsen0c3919e2017-03-02 15:49:10 -080054 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070055 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
56 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
57 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -080058 outDir := "out"
59 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
60 if wd, err := os.Getwd(); err != nil {
61 ctx.Fatalln("Failed to get working directory:", err)
62 } else {
63 outDir = filepath.Join(baseDir, filepath.Base(wd))
64 }
65 }
66 ret.environ.Set("OUT_DIR", outDir)
67 }
68
Dan Willemsen1e704462016-08-21 15:17:17 -070069 ret.environ.Unset(
70 // We're already using it
71 "USE_SOONG_UI",
72
73 // We should never use GOROOT/GOPATH from the shell environment
74 "GOROOT",
75 "GOPATH",
76
77 // These should only come from Soong, not the environment.
78 "CLANG",
79 "CLANG_CXX",
80 "CCC_CC",
81 "CCC_CXX",
82
83 // Used by the goma compiler wrapper, but should only be set by
84 // gomacc
85 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -080086
87 // We handle this above
88 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -070089
90 // Variables that have caused problems in the past
91 "DISPLAY",
92 "GREP_OPTIONS",
Dan Willemsen1e704462016-08-21 15:17:17 -070093 )
94
95 // Tell python not to spam the source tree with .pyc files.
96 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
97
98 // Sane default matching ninja
99 ret.parallel = runtime.NumCPU() + 2
100 ret.keepGoing = 1
101
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800102 // Precondition: the current directory is the top of the source tree
103 if _, err := os.Stat(srcDirFileCheck); err != nil {
104 if os.IsNotExist(err) {
105 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
106 }
107 log.Fatalln("Error verifying tree state:", err)
108 }
109
Dan Willemsendb8457c2017-05-12 16:38:17 -0700110 if srcDir, err := filepath.Abs("."); err == nil {
111 if strings.ContainsRune(srcDir, ' ') {
112 log.Println("You are building in a directory whose absolute path contains a space character:")
113 log.Println()
114 log.Printf("%q\n", srcDir)
115 log.Println()
116 log.Fatalln("Directory names containing spaces are not supported")
117 }
118 }
119
120 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
121 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
122 log.Println()
123 log.Printf("%q\n", outDir)
124 log.Println()
125 log.Fatalln("Directory names containing spaces are not supported")
126 }
127
128 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
129 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
130 log.Println()
131 log.Printf("%q\n", distDir)
132 log.Println()
133 log.Fatalln("Directory names containing spaces are not supported")
134 }
135
Dan Willemsen1e704462016-08-21 15:17:17 -0700136 for _, arg := range args {
137 arg = strings.TrimSpace(arg)
138 if arg == "--make-mode" {
139 continue
140 } else if arg == "showcommands" {
141 ret.verbose = true
142 continue
Dan Willemsen8a073a82017-02-04 17:30:44 -0800143 } else if arg == "dist" {
144 ret.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700145 }
146 if arg[0] == '-' {
147 var err error
148 if arg[1] == 'j' {
149 // TODO: handle space between j and number
150 // Unnecessary if used with makeparallel
151 ret.parallel, err = strconv.Atoi(arg[2:])
152 } else if arg[1] == 'k' {
153 // TODO: handle space between k and number
154 // Unnecessary if used with makeparallel
155 ret.keepGoing, err = strconv.Atoi(arg[2:])
156 } else {
157 ctx.Fatalln("Unknown option:", arg)
158 }
159 if err != nil {
160 ctx.Fatalln("Argument error:", err, arg)
161 }
162 } else {
163 ret.arguments = append(ret.arguments, arg)
164 }
165 }
166
167 return Config{ret}
168}
169
Dan Willemsen02781d52017-05-12 19:28:13 -0700170// CopyConfig copies the configuration from an existing configuration, but replaces
171// the Arguments() list with a new set. Useful if you need to run a different build
172// with the same state as an existing build config.
173func CopyConfig(ctx Context, config Config, args ...string) Config {
174 return Config{&configImpl{
175 arguments: args,
176 goma: config.goma,
177 environ: config.environ.Copy(),
178
179 parallel: config.parallel,
180 keepGoing: config.keepGoing,
181 verbose: config.verbose,
182 dist: config.dist,
183 }}
184}
185
Dan Willemsen1e704462016-08-21 15:17:17 -0700186// Lunch configures the environment for a specific product similarly to the
187// `lunch` bash function.
188func (c *configImpl) Lunch(ctx Context, product, variant string) {
189 if variant != "eng" && variant != "userdebug" && variant != "user" {
190 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
191 }
192
193 c.environ.Set("TARGET_PRODUCT", product)
194 c.environ.Set("TARGET_BUILD_VARIANT", variant)
195 c.environ.Set("TARGET_BUILD_TYPE", "release")
196 c.environ.Unset("TARGET_BUILD_APPS")
197}
198
199// Tapas configures the environment to build one or more unbundled apps,
200// similarly to the `tapas` bash function.
201func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
202 if len(apps) == 0 {
203 apps = []string{"all"}
204 }
205 if variant == "" {
206 variant = "eng"
207 }
208
209 if variant != "eng" && variant != "userdebug" && variant != "user" {
210 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
211 }
212
213 var product string
214 switch arch {
215 case "armv5":
216 product = "generic_armv5"
217 case "arm", "":
218 product = "aosp_arm"
219 case "arm64":
220 product = "aosm_arm64"
221 case "mips":
222 product = "aosp_mips"
223 case "mips64":
224 product = "aosp_mips64"
225 case "x86":
226 product = "aosp_x86"
227 case "x86_64":
228 product = "aosp_x86_64"
229 default:
230 ctx.Fatalf("Invalid architecture: %q", arch)
231 }
232
233 c.environ.Set("TARGET_PRODUCT", product)
234 c.environ.Set("TARGET_BUILD_VARIANT", variant)
235 c.environ.Set("TARGET_BUILD_TYPE", "release")
236 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
237}
238
239func (c *configImpl) Environment() *Environment {
240 return c.environ
241}
242
243func (c *configImpl) Arguments() []string {
244 return c.arguments
245}
246
247func (c *configImpl) OutDir() string {
248 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
249 return outDir
250 }
251 return "out"
252}
253
Dan Willemsen8a073a82017-02-04 17:30:44 -0800254func (c *configImpl) DistDir() string {
255 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
256 return distDir
257 }
258 return filepath.Join(c.OutDir(), "dist")
259}
260
Dan Willemsen1e704462016-08-21 15:17:17 -0700261func (c *configImpl) NinjaArgs() []string {
262 return c.ninjaArgs
263}
264
265func (c *configImpl) SoongOutDir() string {
266 return filepath.Join(c.OutDir(), "soong")
267}
268
269func (c *configImpl) KatiSuffix() string {
270 if c.katiSuffix != "" {
271 return c.katiSuffix
272 }
273 panic("SetKatiSuffix has not been called")
274}
275
Dan Willemsen8a073a82017-02-04 17:30:44 -0800276func (c *configImpl) Dist() bool {
277 return c.dist
278}
279
Dan Willemsen1e704462016-08-21 15:17:17 -0700280func (c *configImpl) IsVerbose() bool {
281 return c.verbose
282}
283
284func (c *configImpl) TargetProduct() string {
285 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
286 return v
287 }
288 panic("TARGET_PRODUCT is not defined")
289}
290
Dan Willemsen02781d52017-05-12 19:28:13 -0700291func (c *configImpl) TargetDevice() string {
292 return c.targetDevice
293}
294
295func (c *configImpl) SetTargetDevice(device string) {
296 c.targetDevice = device
297}
298
299func (c *configImpl) TargetBuildVariant() string {
300 if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok {
301 return v
302 }
303 panic("TARGET_BUILD_VARIANT is not defined")
304}
305
Dan Willemsen1e704462016-08-21 15:17:17 -0700306func (c *configImpl) KatiArgs() []string {
307 return c.katiArgs
308}
309
310func (c *configImpl) Parallel() int {
311 return c.parallel
312}
313
314func (c *configImpl) UseGoma() bool {
315 if v, ok := c.environ.Get("USE_GOMA"); ok {
316 v = strings.TrimSpace(v)
317 if v != "" && v != "false" {
318 return true
319 }
320 }
321 return false
322}
323
324// RemoteParallel controls how many remote jobs (i.e., commands which contain
325// gomacc) are run in parallel. Note the paralleism of all other jobs is
326// still limited by Parallel()
327func (c *configImpl) RemoteParallel() int {
328 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
329 if i, err := strconv.Atoi(v); err == nil {
330 return i
331 }
332 }
333 return 500
334}
335
336func (c *configImpl) SetKatiArgs(args []string) {
337 c.katiArgs = args
338}
339
340func (c *configImpl) SetNinjaArgs(args []string) {
341 c.ninjaArgs = args
342}
343
344func (c *configImpl) SetKatiSuffix(suffix string) {
345 c.katiSuffix = suffix
346}
347
348func (c *configImpl) KatiEnvFile() string {
349 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
350}
351
352func (c *configImpl) KatiNinjaFile() string {
353 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
354}
355
356func (c *configImpl) SoongNinjaFile() string {
357 return filepath.Join(c.SoongOutDir(), "build.ninja")
358}
359
360func (c *configImpl) CombinedNinjaFile() string {
361 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
362}
363
364func (c *configImpl) SoongAndroidMk() string {
365 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
366}
367
368func (c *configImpl) SoongMakeVarsMk() string {
369 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
370}
371
Dan Willemsen02781d52017-05-12 19:28:13 -0700372func (c *configImpl) DevicePreviousProductConfig() string {
373 return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice(), "previous_build_config.mk")
374}
375
Dan Willemsen1e704462016-08-21 15:17:17 -0700376func (c *configImpl) HostPrebuiltTag() string {
377 if runtime.GOOS == "linux" {
378 return "linux-x86"
379 } else if runtime.GOOS == "darwin" {
380 return "darwin-x86"
381 } else {
382 panic("Unsupported OS")
383 }
384}
Dan Willemsenf173d592017-04-27 14:28:00 -0700385
Dan Willemsena3e6c522017-05-05 15:29:20 -0700386func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700387 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
388 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700389 return true
390 }
391 }
392 return false
393}
394
395func (c *configImpl) PrebuiltBuildTool(name string) string {
396 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
397 if c.HostAsan() || (c.Dist() && name == "ckati") {
398 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
399 if _, err := os.Stat(asan); err == nil {
400 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700401 }
402 }
403 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
404}