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