blob: e677d930113e4bdaaf86e727bbdfb6ec64279be8 [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 Willemsen1e704462016-08-21 15:17:17 -0700109 for _, arg := range args {
110 arg = strings.TrimSpace(arg)
111 if arg == "--make-mode" {
112 continue
113 } else if arg == "showcommands" {
114 ret.verbose = true
115 continue
Dan Willemsen8a073a82017-02-04 17:30:44 -0800116 } else if arg == "dist" {
117 ret.dist = true
Dan Willemsen1e704462016-08-21 15:17:17 -0700118 }
119 if arg[0] == '-' {
120 var err error
121 if arg[1] == 'j' {
122 // TODO: handle space between j and number
123 // Unnecessary if used with makeparallel
124 ret.parallel, err = strconv.Atoi(arg[2:])
125 } else if arg[1] == 'k' {
126 // TODO: handle space between k and number
127 // Unnecessary if used with makeparallel
128 ret.keepGoing, err = strconv.Atoi(arg[2:])
129 } else {
130 ctx.Fatalln("Unknown option:", arg)
131 }
132 if err != nil {
133 ctx.Fatalln("Argument error:", err, arg)
134 }
135 } else {
136 ret.arguments = append(ret.arguments, arg)
137 }
138 }
139
140 return Config{ret}
141}
142
143// Lunch configures the environment for a specific product similarly to the
144// `lunch` bash function.
145func (c *configImpl) Lunch(ctx Context, product, variant string) {
146 if variant != "eng" && variant != "userdebug" && variant != "user" {
147 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
148 }
149
150 c.environ.Set("TARGET_PRODUCT", product)
151 c.environ.Set("TARGET_BUILD_VARIANT", variant)
152 c.environ.Set("TARGET_BUILD_TYPE", "release")
153 c.environ.Unset("TARGET_BUILD_APPS")
154}
155
156// Tapas configures the environment to build one or more unbundled apps,
157// similarly to the `tapas` bash function.
158func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) {
159 if len(apps) == 0 {
160 apps = []string{"all"}
161 }
162 if variant == "" {
163 variant = "eng"
164 }
165
166 if variant != "eng" && variant != "userdebug" && variant != "user" {
167 ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant)
168 }
169
170 var product string
171 switch arch {
172 case "armv5":
173 product = "generic_armv5"
174 case "arm", "":
175 product = "aosp_arm"
176 case "arm64":
177 product = "aosm_arm64"
178 case "mips":
179 product = "aosp_mips"
180 case "mips64":
181 product = "aosp_mips64"
182 case "x86":
183 product = "aosp_x86"
184 case "x86_64":
185 product = "aosp_x86_64"
186 default:
187 ctx.Fatalf("Invalid architecture: %q", arch)
188 }
189
190 c.environ.Set("TARGET_PRODUCT", product)
191 c.environ.Set("TARGET_BUILD_VARIANT", variant)
192 c.environ.Set("TARGET_BUILD_TYPE", "release")
193 c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " "))
194}
195
196func (c *configImpl) Environment() *Environment {
197 return c.environ
198}
199
200func (c *configImpl) Arguments() []string {
201 return c.arguments
202}
203
204func (c *configImpl) OutDir() string {
205 if outDir, ok := c.environ.Get("OUT_DIR"); ok {
206 return outDir
207 }
208 return "out"
209}
210
Dan Willemsen8a073a82017-02-04 17:30:44 -0800211func (c *configImpl) DistDir() string {
212 if distDir, ok := c.environ.Get("DIST_DIR"); ok {
213 return distDir
214 }
215 return filepath.Join(c.OutDir(), "dist")
216}
217
Dan Willemsen1e704462016-08-21 15:17:17 -0700218func (c *configImpl) NinjaArgs() []string {
219 return c.ninjaArgs
220}
221
222func (c *configImpl) SoongOutDir() string {
223 return filepath.Join(c.OutDir(), "soong")
224}
225
226func (c *configImpl) KatiSuffix() string {
227 if c.katiSuffix != "" {
228 return c.katiSuffix
229 }
230 panic("SetKatiSuffix has not been called")
231}
232
Dan Willemsen8a073a82017-02-04 17:30:44 -0800233func (c *configImpl) Dist() bool {
234 return c.dist
235}
236
Dan Willemsen1e704462016-08-21 15:17:17 -0700237func (c *configImpl) IsVerbose() bool {
238 return c.verbose
239}
240
241func (c *configImpl) TargetProduct() string {
242 if v, ok := c.environ.Get("TARGET_PRODUCT"); ok {
243 return v
244 }
245 panic("TARGET_PRODUCT is not defined")
246}
247
248func (c *configImpl) KatiArgs() []string {
249 return c.katiArgs
250}
251
252func (c *configImpl) Parallel() int {
253 return c.parallel
254}
255
256func (c *configImpl) UseGoma() bool {
257 if v, ok := c.environ.Get("USE_GOMA"); ok {
258 v = strings.TrimSpace(v)
259 if v != "" && v != "false" {
260 return true
261 }
262 }
263 return false
264}
265
266// RemoteParallel controls how many remote jobs (i.e., commands which contain
267// gomacc) are run in parallel. Note the paralleism of all other jobs is
268// still limited by Parallel()
269func (c *configImpl) RemoteParallel() int {
270 if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok {
271 if i, err := strconv.Atoi(v); err == nil {
272 return i
273 }
274 }
275 return 500
276}
277
278func (c *configImpl) SetKatiArgs(args []string) {
279 c.katiArgs = args
280}
281
282func (c *configImpl) SetNinjaArgs(args []string) {
283 c.ninjaArgs = args
284}
285
286func (c *configImpl) SetKatiSuffix(suffix string) {
287 c.katiSuffix = suffix
288}
289
290func (c *configImpl) KatiEnvFile() string {
291 return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh")
292}
293
294func (c *configImpl) KatiNinjaFile() string {
295 return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja")
296}
297
298func (c *configImpl) SoongNinjaFile() string {
299 return filepath.Join(c.SoongOutDir(), "build.ninja")
300}
301
302func (c *configImpl) CombinedNinjaFile() string {
303 return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja")
304}
305
306func (c *configImpl) SoongAndroidMk() string {
307 return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk")
308}
309
310func (c *configImpl) SoongMakeVarsMk() string {
311 return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk")
312}
313
314func (c *configImpl) HostPrebuiltTag() string {
315 if runtime.GOOS == "linux" {
316 return "linux-x86"
317 } else if runtime.GOOS == "darwin" {
318 return "darwin-x86"
319 } else {
320 panic("Unsupported OS")
321 }
322}
Dan Willemsenf173d592017-04-27 14:28:00 -0700323
Dan Willemsena3e6c522017-05-05 15:29:20 -0700324func (c *configImpl) HostAsan() bool {
Dan Willemsenf173d592017-04-27 14:28:00 -0700325 if v, ok := c.environ.Get("SANITIZE_HOST"); ok {
326 if sanitize := strings.Fields(v); inList("address", sanitize) {
Dan Willemsena3e6c522017-05-05 15:29:20 -0700327 return true
328 }
329 }
330 return false
331}
332
333func (c *configImpl) PrebuiltBuildTool(name string) string {
334 // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers.
335 if c.HostAsan() || (c.Dist() && name == "ckati") {
336 asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name)
337 if _, err := os.Stat(asan); err == nil {
338 return asan
Dan Willemsenf173d592017-04-27 14:28:00 -0700339 }
340 }
341 return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name)
342}