Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 18 | "log" |
| 19 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "runtime" |
| 22 | "strconv" |
| 23 | "strings" |
| 24 | ) |
| 25 | |
| 26 | type Config struct{ *configImpl } |
| 27 | |
| 28 | type 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 Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 38 | dist bool |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 39 | |
| 40 | // From the product config |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 41 | katiArgs []string |
| 42 | ninjaArgs []string |
| 43 | katiSuffix string |
| 44 | targetDevice string |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 47 | const srcDirFileCheck = "build/soong/root.bp" |
| 48 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 49 | func NewConfig(ctx Context, args ...string) Config { |
| 50 | ret := &configImpl{ |
| 51 | environ: OsEnvironment(), |
| 52 | } |
| 53 | |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 54 | // Make sure OUT_DIR is set appropriately |
Dan Willemsen | 02f3add | 2017-05-12 13:50:19 -0700 | [diff] [blame] | 55 | if outDir, ok := ret.environ.Get("OUT_DIR"); ok { |
| 56 | ret.environ.Set("OUT_DIR", filepath.Clean(outDir)) |
| 57 | } else { |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 58 | 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 69 | 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 Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 86 | |
| 87 | // We handle this above |
| 88 | "OUT_DIR_COMMON_BASE", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 89 | |
| 90 | // Variables that have caused problems in the past |
| 91 | "DISPLAY", |
| 92 | "GREP_OPTIONS", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 93 | ) |
| 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 Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 102 | // 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 Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 110 | 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 136 | 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 Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 143 | } else if arg == "dist" { |
| 144 | ret.dist = true |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 145 | } |
| 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 | |
| 170 | // Lunch configures the environment for a specific product similarly to the |
| 171 | // `lunch` bash function. |
| 172 | func (c *configImpl) Lunch(ctx Context, product, variant string) { |
| 173 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 174 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 175 | } |
| 176 | |
| 177 | c.environ.Set("TARGET_PRODUCT", product) |
| 178 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 179 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 180 | c.environ.Unset("TARGET_BUILD_APPS") |
| 181 | } |
| 182 | |
| 183 | // Tapas configures the environment to build one or more unbundled apps, |
| 184 | // similarly to the `tapas` bash function. |
| 185 | func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) { |
| 186 | if len(apps) == 0 { |
| 187 | apps = []string{"all"} |
| 188 | } |
| 189 | if variant == "" { |
| 190 | variant = "eng" |
| 191 | } |
| 192 | |
| 193 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 194 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 195 | } |
| 196 | |
| 197 | var product string |
| 198 | switch arch { |
| 199 | case "armv5": |
| 200 | product = "generic_armv5" |
| 201 | case "arm", "": |
| 202 | product = "aosp_arm" |
| 203 | case "arm64": |
| 204 | product = "aosm_arm64" |
| 205 | case "mips": |
| 206 | product = "aosp_mips" |
| 207 | case "mips64": |
| 208 | product = "aosp_mips64" |
| 209 | case "x86": |
| 210 | product = "aosp_x86" |
| 211 | case "x86_64": |
| 212 | product = "aosp_x86_64" |
| 213 | default: |
| 214 | ctx.Fatalf("Invalid architecture: %q", arch) |
| 215 | } |
| 216 | |
| 217 | c.environ.Set("TARGET_PRODUCT", product) |
| 218 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 219 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 220 | c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " ")) |
| 221 | } |
| 222 | |
| 223 | func (c *configImpl) Environment() *Environment { |
| 224 | return c.environ |
| 225 | } |
| 226 | |
| 227 | func (c *configImpl) Arguments() []string { |
| 228 | return c.arguments |
| 229 | } |
| 230 | |
| 231 | func (c *configImpl) OutDir() string { |
| 232 | if outDir, ok := c.environ.Get("OUT_DIR"); ok { |
| 233 | return outDir |
| 234 | } |
| 235 | return "out" |
| 236 | } |
| 237 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 238 | func (c *configImpl) DistDir() string { |
| 239 | if distDir, ok := c.environ.Get("DIST_DIR"); ok { |
| 240 | return distDir |
| 241 | } |
| 242 | return filepath.Join(c.OutDir(), "dist") |
| 243 | } |
| 244 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 245 | func (c *configImpl) NinjaArgs() []string { |
| 246 | return c.ninjaArgs |
| 247 | } |
| 248 | |
| 249 | func (c *configImpl) SoongOutDir() string { |
| 250 | return filepath.Join(c.OutDir(), "soong") |
| 251 | } |
| 252 | |
| 253 | func (c *configImpl) KatiSuffix() string { |
| 254 | if c.katiSuffix != "" { |
| 255 | return c.katiSuffix |
| 256 | } |
| 257 | panic("SetKatiSuffix has not been called") |
| 258 | } |
| 259 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 260 | func (c *configImpl) Dist() bool { |
| 261 | return c.dist |
| 262 | } |
| 263 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 264 | func (c *configImpl) IsVerbose() bool { |
| 265 | return c.verbose |
| 266 | } |
| 267 | |
| 268 | func (c *configImpl) TargetProduct() string { |
| 269 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 270 | return v |
| 271 | } |
| 272 | panic("TARGET_PRODUCT is not defined") |
| 273 | } |
| 274 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 275 | func (c *configImpl) TargetDevice() string { |
| 276 | return c.targetDevice |
| 277 | } |
| 278 | |
| 279 | func (c *configImpl) SetTargetDevice(device string) { |
| 280 | c.targetDevice = device |
| 281 | } |
| 282 | |
| 283 | func (c *configImpl) TargetBuildVariant() string { |
| 284 | if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok { |
| 285 | return v |
| 286 | } |
| 287 | panic("TARGET_BUILD_VARIANT is not defined") |
| 288 | } |
| 289 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 290 | func (c *configImpl) KatiArgs() []string { |
| 291 | return c.katiArgs |
| 292 | } |
| 293 | |
| 294 | func (c *configImpl) Parallel() int { |
| 295 | return c.parallel |
| 296 | } |
| 297 | |
| 298 | func (c *configImpl) UseGoma() bool { |
| 299 | if v, ok := c.environ.Get("USE_GOMA"); ok { |
| 300 | v = strings.TrimSpace(v) |
| 301 | if v != "" && v != "false" { |
| 302 | return true |
| 303 | } |
| 304 | } |
| 305 | return false |
| 306 | } |
| 307 | |
| 308 | // RemoteParallel controls how many remote jobs (i.e., commands which contain |
| 309 | // gomacc) are run in parallel. Note the paralleism of all other jobs is |
| 310 | // still limited by Parallel() |
| 311 | func (c *configImpl) RemoteParallel() int { |
| 312 | if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok { |
| 313 | if i, err := strconv.Atoi(v); err == nil { |
| 314 | return i |
| 315 | } |
| 316 | } |
| 317 | return 500 |
| 318 | } |
| 319 | |
| 320 | func (c *configImpl) SetKatiArgs(args []string) { |
| 321 | c.katiArgs = args |
| 322 | } |
| 323 | |
| 324 | func (c *configImpl) SetNinjaArgs(args []string) { |
| 325 | c.ninjaArgs = args |
| 326 | } |
| 327 | |
| 328 | func (c *configImpl) SetKatiSuffix(suffix string) { |
| 329 | c.katiSuffix = suffix |
| 330 | } |
| 331 | |
| 332 | func (c *configImpl) KatiEnvFile() string { |
| 333 | return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh") |
| 334 | } |
| 335 | |
| 336 | func (c *configImpl) KatiNinjaFile() string { |
| 337 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+".ninja") |
| 338 | } |
| 339 | |
| 340 | func (c *configImpl) SoongNinjaFile() string { |
| 341 | return filepath.Join(c.SoongOutDir(), "build.ninja") |
| 342 | } |
| 343 | |
| 344 | func (c *configImpl) CombinedNinjaFile() string { |
| 345 | return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja") |
| 346 | } |
| 347 | |
| 348 | func (c *configImpl) SoongAndroidMk() string { |
| 349 | return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk") |
| 350 | } |
| 351 | |
| 352 | func (c *configImpl) SoongMakeVarsMk() string { |
| 353 | return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk") |
| 354 | } |
| 355 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame^] | 356 | func (c *configImpl) ProductOut() string { |
| 357 | if buildType, ok := c.environ.Get("TARGET_BUILD_TYPE"); ok && buildType == "debug" { |
| 358 | return filepath.Join(c.OutDir(), "debug", "target", "product", c.TargetDevice()) |
| 359 | } else { |
| 360 | return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice()) |
| 361 | } |
| 362 | } |
| 363 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 364 | func (c *configImpl) DevicePreviousProductConfig() string { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame^] | 365 | return filepath.Join(c.ProductOut(), "previous_build_config.mk") |
| 366 | } |
| 367 | |
| 368 | func (c *configImpl) hostOutRoot() string { |
| 369 | if buildType, ok := c.environ.Get("HOST_BUILD_TYPE"); ok && buildType == "debug" { |
| 370 | return filepath.Join(c.OutDir(), "debug", "host") |
| 371 | } else { |
| 372 | return filepath.Join(c.OutDir(), "host") |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | func (c *configImpl) HostOut() string { |
| 377 | return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag()) |
| 378 | } |
| 379 | |
| 380 | // This probably needs to be multi-valued, so not exporting it for now |
| 381 | func (c *configImpl) hostCrossOut() string { |
| 382 | if runtime.GOOS == "linux" { |
| 383 | return filepath.Join(c.hostOutRoot(), "windows-x86") |
| 384 | } else { |
| 385 | return "" |
| 386 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 387 | } |
| 388 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 389 | func (c *configImpl) HostPrebuiltTag() string { |
| 390 | if runtime.GOOS == "linux" { |
| 391 | return "linux-x86" |
| 392 | } else if runtime.GOOS == "darwin" { |
| 393 | return "darwin-x86" |
| 394 | } else { |
| 395 | panic("Unsupported OS") |
| 396 | } |
| 397 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 398 | |
Dan Willemsen | a3e6c52 | 2017-05-05 15:29:20 -0700 | [diff] [blame] | 399 | func (c *configImpl) HostAsan() bool { |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 400 | if v, ok := c.environ.Get("SANITIZE_HOST"); ok { |
| 401 | if sanitize := strings.Fields(v); inList("address", sanitize) { |
Dan Willemsen | a3e6c52 | 2017-05-05 15:29:20 -0700 | [diff] [blame] | 402 | return true |
| 403 | } |
| 404 | } |
| 405 | return false |
| 406 | } |
| 407 | |
| 408 | func (c *configImpl) PrebuiltBuildTool(name string) string { |
| 409 | // (b/36182021) We're seeing rare ckati crashes, so always enable asan kati on the build servers. |
| 410 | if c.HostAsan() || (c.Dist() && name == "ckati") { |
| 411 | asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name) |
| 412 | if _, err := os.Stat(asan); err == nil { |
| 413 | return asan |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name) |
| 417 | } |