blob: 0d29924373bfd841b16e1083cb3de482208a1fbf [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
41 katiArgs []string
42 ninjaArgs []string
43 katiSuffix string
44}
45
Dan Willemsenc2af0be2017-01-20 14:10:01 -080046const srcDirFileCheck = "build/soong/root.bp"
47
Dan Willemsen1e704462016-08-21 15:17:17 -070048func NewConfig(ctx Context, args ...string) Config {
49 ret := &configImpl{
50 environ: OsEnvironment(),
51 }
52
Dan Willemsen0c3919e2017-03-02 15:49:10 -080053 // Make sure OUT_DIR is set appropriately
Dan Willemsen02f3add2017-05-12 13:50:19 -070054 if outDir, ok := ret.environ.Get("OUT_DIR"); ok {
55 ret.environ.Set("OUT_DIR", filepath.Clean(outDir))
56 } else {
Dan Willemsen0c3919e2017-03-02 15:49:10 -080057 outDir := "out"
58 if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok {
59 if wd, err := os.Getwd(); err != nil {
60 ctx.Fatalln("Failed to get working directory:", err)
61 } else {
62 outDir = filepath.Join(baseDir, filepath.Base(wd))
63 }
64 }
65 ret.environ.Set("OUT_DIR", outDir)
66 }
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068 ret.environ.Unset(
69 // We're already using it
70 "USE_SOONG_UI",
71
72 // We should never use GOROOT/GOPATH from the shell environment
73 "GOROOT",
74 "GOPATH",
75
76 // These should only come from Soong, not the environment.
77 "CLANG",
78 "CLANG_CXX",
79 "CCC_CC",
80 "CCC_CXX",
81
82 // Used by the goma compiler wrapper, but should only be set by
83 // gomacc
84 "GOMACC_PATH",
Dan Willemsen0c3919e2017-03-02 15:49:10 -080085
86 // We handle this above
87 "OUT_DIR_COMMON_BASE",
Dan Willemsen68a09852017-04-18 13:56:57 -070088
89 // Variables that have caused problems in the past
90 "DISPLAY",
91 "GREP_OPTIONS",
Dan Willemsen1e704462016-08-21 15:17:17 -070092 )
93
94 // Tell python not to spam the source tree with .pyc files.
95 ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1")
96
97 // Sane default matching ninja
98 ret.parallel = runtime.NumCPU() + 2
99 ret.keepGoing = 1
100
Dan Willemsenc2af0be2017-01-20 14:10:01 -0800101 // Precondition: the current directory is the top of the source tree
102 if _, err := os.Stat(srcDirFileCheck); err != nil {
103 if os.IsNotExist(err) {
104 log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck)
105 }
106 log.Fatalln("Error verifying tree state:", err)
107 }
108
Dan Willemsendb8457c2017-05-12 16:38:17 -0700109 if srcDir, err := filepath.Abs("."); err == nil {
110 if strings.ContainsRune(srcDir, ' ') {
111 log.Println("You are building in a directory whose absolute path contains a space character:")
112 log.Println()
113 log.Printf("%q\n", srcDir)
114 log.Println()
115 log.Fatalln("Directory names containing spaces are not supported")
116 }
117 }
118
119 if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') {
120 log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:")
121 log.Println()
122 log.Printf("%q\n", outDir)
123 log.Println()
124 log.Fatalln("Directory names containing spaces are not supported")
125 }
126
127 if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') {
128 log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:")
129 log.Println()
130 log.Printf("%q\n", distDir)
131 log.Println()
132 log.Fatalln("Directory names containing spaces are not supported")
133 }
134
Dan Willemsen1e704462016-08-21 15:17:17 -0700135 for _, arg := range args {
136 arg = strings.TrimSpace(arg)
137 if arg == "--make-mode" {
138 continue
139 } else if arg == "showcommands" {
140 ret.verbose = true
141 continue
Dan Willemsen8a073a82017-02-04 17:30:44 -0800142 } else if arg == "dist" {
143 ret.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700144 }
145 if arg[0] == '-' {
146 var err error
147 if arg[1] == 'j' {
148 // TODO: handle space between j and number
149 // Unnecessary if used with makeparallel
150 ret.parallel, err = strconv.Atoi(arg[2:])
151 } else if arg[1] == 'k' {
152 // TODO: handle space between k and number
153 // Unnecessary if used with makeparallel
154 ret.keepGoing, err = strconv.Atoi(arg[2:])
155 } else {
156 ctx.Fatalln("Unknown option:", arg)
157 }
158 if err != nil {
159 ctx.Fatalln("Argument error:", err, arg)
160 }
161 } else {
162 ret.arguments = append(ret.arguments, arg)
163 }
164 }
165
166 return Config{ret}
167}
168
169// Lunch configures the environment for a specific product similarly to the
170// `lunch` bash function.
171func (c *configImpl) Lunch(ctx Context, product, variant string) {
172 if variant != "eng" && variant != "userdebug" && variant != "user" {
173 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
174 }
175
176 c.environ.Set("TARGET_PRODUCT", product)
177 c.environ.Set("TARGET_BUILD_VARIANT", variant)
178 c.environ.Set("TARGET_BUILD_TYPE", "release")
179 c.environ.Unset("TARGET_BUILD_APPS")
180}
181
182// Tapas configures the environment to build one or more unbundled apps,
183// similarly to the `tapas` bash function.
184func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
185 if len(apps) == 0 {
186 apps = []string{"all"}
187 }
188 if variant == "" {
189 variant = "eng"
190 }
191
192 if variant != "eng" && variant != "userdebug" && variant != "user" {
193 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
194 }
195
196 var product string
197 switch arch {
198 case "armv5":
199 product = "generic_armv5"
200 case "arm", "":
201 product = "aosp_arm"
202 case "arm64":
203 product = "aosm_arm64"
204 case "mips":
205 product = "aosp_mips"
206 case "mips64":
207 product = "aosp_mips64"
208 case "x86":
209 product = "aosp_x86"
210 case "x86_64":
211 product = "aosp_x86_64"
212 default:
213 ctx.Fatalf("Invalid architecture: %q", arch)
214 }
215
216 c.environ.Set("TARGET_PRODUCT", product)
217 c.environ.Set("TARGET_BUILD_VARIANT", variant)
218 c.environ.Set("TARGET_BUILD_TYPE", "release")
219 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
220}
221
222func (c *configImpl) Environment() *Environment {
223 return c.environ
224}
225
226func (c *configImpl) Arguments() []string {
227 return c.arguments
228}
229
230func (c *configImpl) OutDir() string {
231 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
232 return outDir
233 }
234 return "out"
235}
236
Dan Willemsen8a073a82017-02-04 17:30:44 -0800237func (c *configImpl) DistDir() string {
238 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
239 return distDir
240 }
241 return filepath.Join(c.OutDir(), "dist")
242}
243
Dan Willemsen1e704462016-08-21 15:17:17 -0700244func (c *configImpl) NinjaArgs() []string {
245 return c.ninjaArgs
246}
247
248func (c *configImpl) SoongOutDir() string {
249 return filepath.Join(c.OutDir(), "soong")
250}
251
252func (c *configImpl) KatiSuffix() string {
253 if c.katiSuffix != "" {
254 return c.katiSuffix
255 }
256 panic("SetKatiSuffix has not been called")
257}
258
Dan Willemsen8a073a82017-02-04 17:30:44 -0800259func (c *configImpl) Dist() bool {
260 return c.dist
261}
262
Dan Willemsen1e704462016-08-21 15:17:17 -0700263func (c *configImpl) IsVerbose() bool {
264 return c.verbose
265}
266
267func (c *configImpl) TargetProduct() string {
268 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
269 return v
270 }
271 panic("TARGET_PRODUCT is not defined")
272}
273
274func (c *configImpl) KatiArgs() []string {
275 return c.katiArgs
276}
277
278func (c *configImpl) Parallel() int {
279 return c.parallel
280}
281
282func (c *configImpl) UseGoma() bool {
283 if v, ok := c.environ.Get("USE_GOMA"); ok {
284 v = strings.TrimSpace(v)
285 if v != "" && v != "false" {
286 return true
287 }
288 }
289 return false
290}
291
292// RemoteParallel controls how many remote jobs (i.e., commands which contain
293// gomacc) are run in parallel. Note the paralleism of all other jobs is
294// still limited by Parallel()
295func (c *configImpl) RemoteParallel() int {
296 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
297 if i, err := strconv.Atoi(v); err == nil {
298 return i
299 }
300 }
301 return 500
302}
303
304func (c *configImpl) SetKatiArgs(args []string) {
305 c.katiArgs = args
306}
307
308func (c *configImpl) SetNinjaArgs(args []string) {
309 c.ninjaArgs = args
310}
311
312func (c *configImpl) SetKatiSuffix(suffix string) {
313 c.katiSuffix = suffix
314}
315
316func (c *configImpl) KatiEnvFile() string {
317 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
318}
319
320func (c *configImpl) KatiNinjaFile() string {
321 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
322}
323
324func (c *configImpl) SoongNinjaFile() string {
325 return filepath.Join(c.SoongOutDir(), "build.ninja")
326}
327
328func (c *configImpl) CombinedNinjaFile() string {
329 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
330}
331
332func (c *configImpl) SoongAndroidMk() string {
333 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
334}
335
336func (c *configImpl) SoongMakeVarsMk() string {
337 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
338}
339
340func (c *configImpl) HostPrebuiltTag() string {
341 if runtime.GOOS == "linux" {
342 return "linux-x86"
343 } else if runtime.GOOS == "darwin" {
344 return "darwin-x86"
345 } else {
346 panic("Unsupported OS")
347 }
348}
Dan Willemsenf173d592017-04-27 14:28:00 -0700349
Dan Willemsena3e6c522017-05-05 15:29:20 -0700350func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700351 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
352 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700353 return true
354 }
355 }
356 return false
357}
358
359func (c *configImpl) PrebuiltBuildTool(name string) string {
360 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
361 if c.HostAsan() || (c.Dist() && name == "ckati") {
362 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
363 if _, err := os.Stat(asan); err == nil {
364 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700365 }
366 }
367 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
368}