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 ( |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 18 | "io/ioutil" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 19 | "log" |
| 20 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "path/filepath" |
| 22 | "runtime" |
| 23 | "strconv" |
| 24 | "strings" |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 25 | "time" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 26 | |
| 27 | "android/soong/shared" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
| 30 | type Config struct{ *configImpl } |
| 31 | |
| 32 | type configImpl struct { |
| 33 | // From the environment |
| 34 | arguments []string |
| 35 | goma bool |
| 36 | environ *Environment |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 37 | distDir string |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 38 | |
| 39 | // From the arguments |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 40 | parallel int |
| 41 | keepGoing int |
| 42 | verbose bool |
| 43 | checkbuild bool |
| 44 | dist bool |
| 45 | skipMake bool |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 46 | |
| 47 | // From the product config |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 48 | katiArgs []string |
| 49 | ninjaArgs []string |
| 50 | katiSuffix string |
| 51 | targetDevice string |
| 52 | targetDeviceDir string |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 53 | |
Dan Willemsen | d8aa39d | 2018-08-27 15:01:03 -0700 | [diff] [blame] | 54 | pdkBuild bool |
| 55 | |
| 56 | brokenDupRules bool |
| 57 | brokenPhonyTargets bool |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 58 | |
| 59 | pathReplaced bool |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 62 | const srcDirFileCheck = "build/soong/root.bp" |
| 63 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 64 | func NewConfig(ctx Context, args ...string) Config { |
| 65 | ret := &configImpl{ |
| 66 | environ: OsEnvironment(), |
| 67 | } |
| 68 | |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 69 | // Sane default matching ninja |
| 70 | ret.parallel = runtime.NumCPU() + 2 |
| 71 | ret.keepGoing = 1 |
| 72 | |
| 73 | ret.parseArgs(ctx, args) |
| 74 | |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 75 | // Make sure OUT_DIR is set appropriately |
Dan Willemsen | 02f3add | 2017-05-12 13:50:19 -0700 | [diff] [blame] | 76 | if outDir, ok := ret.environ.Get("OUT_DIR"); ok { |
| 77 | ret.environ.Set("OUT_DIR", filepath.Clean(outDir)) |
| 78 | } else { |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 79 | outDir := "out" |
| 80 | if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok { |
| 81 | if wd, err := os.Getwd(); err != nil { |
| 82 | ctx.Fatalln("Failed to get working directory:", err) |
| 83 | } else { |
| 84 | outDir = filepath.Join(baseDir, filepath.Base(wd)) |
| 85 | } |
| 86 | } |
| 87 | ret.environ.Set("OUT_DIR", outDir) |
| 88 | } |
| 89 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 90 | if distDir, ok := ret.environ.Get("DIST_DIR"); ok { |
| 91 | ret.distDir = filepath.Clean(distDir) |
| 92 | } else { |
| 93 | ret.distDir = filepath.Join(ret.OutDir(), "dist") |
| 94 | } |
Dan Willemsen | d50e89f | 2018-10-16 17:49:25 -0700 | [diff] [blame] | 95 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 96 | ret.environ.Unset( |
| 97 | // We're already using it |
| 98 | "USE_SOONG_UI", |
| 99 | |
| 100 | // We should never use GOROOT/GOPATH from the shell environment |
| 101 | "GOROOT", |
| 102 | "GOPATH", |
| 103 | |
| 104 | // These should only come from Soong, not the environment. |
| 105 | "CLANG", |
| 106 | "CLANG_CXX", |
| 107 | "CCC_CC", |
| 108 | "CCC_CXX", |
| 109 | |
| 110 | // Used by the goma compiler wrapper, but should only be set by |
| 111 | // gomacc |
| 112 | "GOMACC_PATH", |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 113 | |
| 114 | // We handle this above |
| 115 | "OUT_DIR_COMMON_BASE", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 116 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 117 | // This is handled above too, and set for individual commands later |
| 118 | "DIST_DIR", |
| 119 | |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 120 | // Variables that have caused problems in the past |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 121 | "CDPATH", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 122 | "DISPLAY", |
| 123 | "GREP_OPTIONS", |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 124 | "NDK_ROOT", |
Dan Willemsen | 00fcb26 | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 125 | "POSIXLY_CORRECT", |
Dan Willemsen | c40e10b | 2017-07-11 14:30:00 -0700 | [diff] [blame] | 126 | |
| 127 | // Drop make flags |
| 128 | "MAKEFLAGS", |
| 129 | "MAKELEVEL", |
| 130 | "MFLAGS", |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 131 | |
| 132 | // Set in envsetup.sh, reset in makefiles |
| 133 | "ANDROID_JAVA_TOOLCHAIN", |
Colin Cross | 7f09c40 | 2018-07-11 14:49:31 -0700 | [diff] [blame] | 134 | |
| 135 | // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional |
| 136 | "ANDROID_BUILD_TOP", |
| 137 | "ANDROID_HOST_OUT", |
| 138 | "ANDROID_PRODUCT_OUT", |
| 139 | "ANDROID_HOST_OUT_TESTCASES", |
| 140 | "ANDROID_TARGET_OUT_TESTCASES", |
| 141 | "ANDROID_TOOLCHAIN", |
| 142 | "ANDROID_TOOLCHAIN_2ND_ARCH", |
| 143 | "ANDROID_DEV_SCRIPTS", |
| 144 | "ANDROID_EMULATOR_PREBUILTS", |
| 145 | "ANDROID_PRE_BUILD_PATHS", |
Dan Willemsen | f99915f | 2018-10-25 22:04:42 -0700 | [diff] [blame] | 146 | |
| 147 | // Only set in multiproduct_kati after config generation |
| 148 | "EMPTY_NINJA_FILE", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 149 | ) |
| 150 | |
| 151 | // Tell python not to spam the source tree with .pyc files. |
| 152 | ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1") |
| 153 | |
Dan Willemsen | 32a669b | 2018-03-08 19:42:00 -0800 | [diff] [blame] | 154 | ret.environ.Set("TMPDIR", absPath(ctx, ret.TempDir())) |
| 155 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 156 | // Precondition: the current directory is the top of the source tree |
| 157 | if _, err := os.Stat(srcDirFileCheck); err != nil { |
| 158 | if os.IsNotExist(err) { |
| 159 | log.Fatalf("Current working directory must be the source tree. %q not found", srcDirFileCheck) |
| 160 | } |
| 161 | log.Fatalln("Error verifying tree state:", err) |
| 162 | } |
| 163 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 164 | if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') { |
| 165 | log.Println("You are building in a directory whose absolute path contains a space character:") |
| 166 | log.Println() |
| 167 | log.Printf("%q\n", srcDir) |
| 168 | log.Println() |
| 169 | log.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') { |
| 173 | log.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:") |
| 174 | log.Println() |
| 175 | log.Printf("%q\n", outDir) |
| 176 | log.Println() |
| 177 | log.Fatalln("Directory names containing spaces are not supported") |
| 178 | } |
| 179 | |
| 180 | if distDir := ret.DistDir(); strings.ContainsRune(distDir, ' ') { |
| 181 | log.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:") |
| 182 | log.Println() |
| 183 | log.Printf("%q\n", distDir) |
| 184 | log.Println() |
| 185 | log.Fatalln("Directory names containing spaces are not supported") |
| 186 | } |
| 187 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 188 | // Configure Java-related variables, including adding it to $PATH |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 189 | java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag()) |
| 190 | java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag()) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 191 | javaHome := func() string { |
| 192 | if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok { |
| 193 | return override |
| 194 | } |
Colin Cross | 997262f | 2018-06-19 22:49:39 -0700 | [diff] [blame] | 195 | return java9Home |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 196 | }() |
| 197 | absJavaHome := absPath(ctx, javaHome) |
| 198 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 199 | ret.configureLocale(ctx) |
| 200 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 201 | newPath := []string{filepath.Join(absJavaHome, "bin")} |
| 202 | if path, ok := ret.environ.Get("PATH"); ok && path != "" { |
| 203 | newPath = append(newPath, path) |
| 204 | } |
| 205 | ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME") |
| 206 | ret.environ.Set("JAVA_HOME", absJavaHome) |
| 207 | ret.environ.Set("ANDROID_JAVA_HOME", javaHome) |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 208 | ret.environ.Set("ANDROID_JAVA8_HOME", java8Home) |
| 209 | ret.environ.Set("ANDROID_JAVA9_HOME", java9Home) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 210 | ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator))) |
| 211 | |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 212 | outDir := ret.OutDir() |
| 213 | buildDateTimeFile := filepath.Join(outDir, "build_date.txt") |
| 214 | var content string |
| 215 | if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" { |
| 216 | content = buildDateTime |
| 217 | } else { |
| 218 | content = strconv.FormatInt(time.Now().Unix(), 10) |
| 219 | } |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 220 | if ctx.Metrics != nil { |
| 221 | ctx.Metrics.SetBuildDateTime(content) |
| 222 | } |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 223 | err := ioutil.WriteFile(buildDateTimeFile, []byte(content), 0777) |
| 224 | if err != nil { |
| 225 | ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err) |
| 226 | } |
| 227 | ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile) |
| 228 | |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 229 | return Config{ret} |
| 230 | } |
| 231 | |
| 232 | func (c *configImpl) parseArgs(ctx Context, args []string) { |
| 233 | for i := 0; i < len(args); i++ { |
| 234 | arg := strings.TrimSpace(args[i]) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 235 | if arg == "--make-mode" { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 236 | } else if arg == "showcommands" { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 237 | c.verbose = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 238 | } else if arg == "--skip-make" { |
| 239 | c.skipMake = true |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 240 | } else if len(arg) > 0 && arg[0] == '-' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 241 | parseArgNum := func(def int) int { |
| 242 | if len(arg) > 2 { |
| 243 | p, err := strconv.ParseUint(arg[2:], 10, 31) |
| 244 | if err != nil { |
| 245 | ctx.Fatalf("Failed to parse %q: %v", arg, err) |
| 246 | } |
| 247 | return int(p) |
| 248 | } else if i+1 < len(args) { |
| 249 | p, err := strconv.ParseUint(args[i+1], 10, 31) |
| 250 | if err == nil { |
| 251 | i++ |
| 252 | return int(p) |
| 253 | } |
| 254 | } |
| 255 | return def |
| 256 | } |
| 257 | |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 258 | if len(arg) > 1 && arg[1] == 'j' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 259 | c.parallel = parseArgNum(c.parallel) |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 260 | } else if len(arg) > 1 && arg[1] == 'k' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 261 | c.keepGoing = parseArgNum(0) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 262 | } else { |
| 263 | ctx.Fatalln("Unknown option:", arg) |
| 264 | } |
Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 265 | } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 { |
| 266 | c.environ.Set(k, v) |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 267 | } else if arg == "dist" { |
| 268 | c.dist = true |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 269 | } else { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 270 | if arg == "checkbuild" { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 271 | c.checkbuild = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 272 | } |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 273 | c.arguments = append(c.arguments, arg) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 278 | func (c *configImpl) configureLocale(ctx Context) { |
| 279 | cmd := Command(ctx, Config{c}, "locale", "locale", "-a") |
| 280 | output, err := cmd.Output() |
| 281 | |
| 282 | var locales []string |
| 283 | if err == nil { |
| 284 | locales = strings.Split(string(output), "\n") |
| 285 | } else { |
| 286 | // If we're unable to list the locales, let's assume en_US.UTF-8 |
| 287 | locales = []string{"en_US.UTF-8"} |
| 288 | ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales) |
| 289 | } |
| 290 | |
| 291 | // gettext uses LANGUAGE, which is passed directly through |
| 292 | |
| 293 | // For LANG and LC_*, only preserve the evaluated version of |
| 294 | // LC_MESSAGES |
| 295 | user_lang := "" |
| 296 | if lc_all, ok := c.environ.Get("LC_ALL"); ok { |
| 297 | user_lang = lc_all |
| 298 | } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok { |
| 299 | user_lang = lc_messages |
| 300 | } else if lang, ok := c.environ.Get("LANG"); ok { |
| 301 | user_lang = lang |
| 302 | } |
| 303 | |
| 304 | c.environ.UnsetWithPrefix("LC_") |
| 305 | |
| 306 | if user_lang != "" { |
| 307 | c.environ.Set("LC_MESSAGES", user_lang) |
| 308 | } |
| 309 | |
| 310 | // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed |
| 311 | // for others) |
| 312 | if inList("C.UTF-8", locales) { |
| 313 | c.environ.Set("LANG", "C.UTF-8") |
Aaron Kling | d236e0e | 2018-08-07 19:21:36 -0500 | [diff] [blame] | 314 | } else if inList("C.utf8", locales) { |
| 315 | // These normalize to the same thing |
| 316 | c.environ.Set("LANG", "C.UTF-8") |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 317 | } else if inList("en_US.UTF-8", locales) { |
| 318 | c.environ.Set("LANG", "en_US.UTF-8") |
| 319 | } else if inList("en_US.utf8", locales) { |
| 320 | // These normalize to the same thing |
| 321 | c.environ.Set("LANG", "en_US.UTF-8") |
| 322 | } else { |
| 323 | ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8") |
| 324 | } |
| 325 | } |
| 326 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 327 | // Lunch configures the environment for a specific product similarly to the |
| 328 | // `lunch` bash function. |
| 329 | func (c *configImpl) Lunch(ctx Context, product, variant string) { |
| 330 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 331 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 332 | } |
| 333 | |
| 334 | c.environ.Set("TARGET_PRODUCT", product) |
| 335 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 336 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 337 | c.environ.Unset("TARGET_BUILD_APPS") |
| 338 | } |
| 339 | |
| 340 | // Tapas configures the environment to build one or more unbundled apps, |
| 341 | // similarly to the `tapas` bash function. |
| 342 | func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) { |
| 343 | if len(apps) == 0 { |
| 344 | apps = []string{"all"} |
| 345 | } |
| 346 | if variant == "" { |
| 347 | variant = "eng" |
| 348 | } |
| 349 | |
| 350 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 351 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 352 | } |
| 353 | |
| 354 | var product string |
| 355 | switch arch { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 356 | case "arm", "": |
| 357 | product = "aosp_arm" |
| 358 | case "arm64": |
| 359 | product = "aosm_arm64" |
| 360 | case "mips": |
| 361 | product = "aosp_mips" |
| 362 | case "mips64": |
| 363 | product = "aosp_mips64" |
| 364 | case "x86": |
| 365 | product = "aosp_x86" |
| 366 | case "x86_64": |
| 367 | product = "aosp_x86_64" |
| 368 | default: |
| 369 | ctx.Fatalf("Invalid architecture: %q", arch) |
| 370 | } |
| 371 | |
| 372 | c.environ.Set("TARGET_PRODUCT", product) |
| 373 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 374 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 375 | c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " ")) |
| 376 | } |
| 377 | |
| 378 | func (c *configImpl) Environment() *Environment { |
| 379 | return c.environ |
| 380 | } |
| 381 | |
| 382 | func (c *configImpl) Arguments() []string { |
| 383 | return c.arguments |
| 384 | } |
| 385 | |
| 386 | func (c *configImpl) OutDir() string { |
| 387 | if outDir, ok := c.environ.Get("OUT_DIR"); ok { |
Dan Willemsen | 25a5618 | 2018-08-31 20:25:32 -0700 | [diff] [blame] | 388 | return filepath.Clean(outDir) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 389 | } |
| 390 | return "out" |
| 391 | } |
| 392 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 393 | func (c *configImpl) DistDir() string { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 394 | return c.distDir |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 395 | } |
| 396 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 397 | func (c *configImpl) NinjaArgs() []string { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 398 | if c.skipMake { |
| 399 | return c.arguments |
| 400 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 401 | return c.ninjaArgs |
| 402 | } |
| 403 | |
| 404 | func (c *configImpl) SoongOutDir() string { |
| 405 | return filepath.Join(c.OutDir(), "soong") |
| 406 | } |
| 407 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 408 | func (c *configImpl) TempDir() string { |
| 409 | return shared.TempDirForOutDir(c.SoongOutDir()) |
| 410 | } |
| 411 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 412 | func (c *configImpl) FileListDir() string { |
| 413 | return filepath.Join(c.OutDir(), ".module_paths") |
| 414 | } |
| 415 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 416 | func (c *configImpl) KatiSuffix() string { |
| 417 | if c.katiSuffix != "" { |
| 418 | return c.katiSuffix |
| 419 | } |
| 420 | panic("SetKatiSuffix has not been called") |
| 421 | } |
| 422 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 423 | // Checkbuild returns true if "checkbuild" was one of the build goals, which means that the |
| 424 | // user is interested in additional checks at the expense of build time. |
| 425 | func (c *configImpl) Checkbuild() bool { |
| 426 | return c.checkbuild |
| 427 | } |
| 428 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 429 | func (c *configImpl) Dist() bool { |
| 430 | return c.dist |
| 431 | } |
| 432 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 433 | func (c *configImpl) IsVerbose() bool { |
| 434 | return c.verbose |
| 435 | } |
| 436 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 437 | func (c *configImpl) SkipMake() bool { |
| 438 | return c.skipMake |
| 439 | } |
| 440 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 441 | func (c *configImpl) TargetProduct() string { |
| 442 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 443 | return v |
| 444 | } |
| 445 | panic("TARGET_PRODUCT is not defined") |
| 446 | } |
| 447 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 448 | func (c *configImpl) TargetDevice() string { |
| 449 | return c.targetDevice |
| 450 | } |
| 451 | |
| 452 | func (c *configImpl) SetTargetDevice(device string) { |
| 453 | c.targetDevice = device |
| 454 | } |
| 455 | |
| 456 | func (c *configImpl) TargetBuildVariant() string { |
| 457 | if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok { |
| 458 | return v |
| 459 | } |
| 460 | panic("TARGET_BUILD_VARIANT is not defined") |
| 461 | } |
| 462 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 463 | func (c *configImpl) KatiArgs() []string { |
| 464 | return c.katiArgs |
| 465 | } |
| 466 | |
| 467 | func (c *configImpl) Parallel() int { |
| 468 | return c.parallel |
| 469 | } |
| 470 | |
| 471 | func (c *configImpl) UseGoma() bool { |
| 472 | if v, ok := c.environ.Get("USE_GOMA"); ok { |
| 473 | v = strings.TrimSpace(v) |
| 474 | if v != "" && v != "false" { |
| 475 | return true |
| 476 | } |
| 477 | } |
| 478 | return false |
| 479 | } |
| 480 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 481 | func (c *configImpl) StartGoma() bool { |
| 482 | if !c.UseGoma() { |
| 483 | return false |
| 484 | } |
| 485 | |
| 486 | if v, ok := c.environ.Get("NOSTART_GOMA"); ok { |
| 487 | v = strings.TrimSpace(v) |
| 488 | if v != "" && v != "false" { |
| 489 | return false |
| 490 | } |
| 491 | } |
| 492 | return true |
| 493 | } |
| 494 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 495 | // RemoteParallel controls how many remote jobs (i.e., commands which contain |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 496 | // gomacc) are run in parallel. Note the parallelism of all other jobs is |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 497 | // still limited by Parallel() |
| 498 | func (c *configImpl) RemoteParallel() int { |
| 499 | if v, ok := c.environ.Get("NINJA_REMOTE_NUM_JOBS"); ok { |
| 500 | if i, err := strconv.Atoi(v); err == nil { |
| 501 | return i |
| 502 | } |
| 503 | } |
| 504 | return 500 |
| 505 | } |
| 506 | |
| 507 | func (c *configImpl) SetKatiArgs(args []string) { |
| 508 | c.katiArgs = args |
| 509 | } |
| 510 | |
| 511 | func (c *configImpl) SetNinjaArgs(args []string) { |
| 512 | c.ninjaArgs = args |
| 513 | } |
| 514 | |
| 515 | func (c *configImpl) SetKatiSuffix(suffix string) { |
| 516 | c.katiSuffix = suffix |
| 517 | } |
| 518 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 519 | func (c *configImpl) LastKatiSuffixFile() string { |
| 520 | return filepath.Join(c.OutDir(), "last_kati_suffix") |
| 521 | } |
| 522 | |
| 523 | func (c *configImpl) HasKatiSuffix() bool { |
| 524 | return c.katiSuffix != "" |
| 525 | } |
| 526 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 527 | func (c *configImpl) KatiEnvFile() string { |
| 528 | return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh") |
| 529 | } |
| 530 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 531 | func (c *configImpl) KatiBuildNinjaFile() string { |
| 532 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 533 | } |
| 534 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 535 | func (c *configImpl) KatiPackageNinjaFile() string { |
| 536 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja") |
| 537 | } |
| 538 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 539 | func (c *configImpl) SoongNinjaFile() string { |
| 540 | return filepath.Join(c.SoongOutDir(), "build.ninja") |
| 541 | } |
| 542 | |
| 543 | func (c *configImpl) CombinedNinjaFile() string { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 544 | if c.katiSuffix == "" { |
| 545 | return filepath.Join(c.OutDir(), "combined.ninja") |
| 546 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 547 | return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja") |
| 548 | } |
| 549 | |
| 550 | func (c *configImpl) SoongAndroidMk() string { |
| 551 | return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk") |
| 552 | } |
| 553 | |
| 554 | func (c *configImpl) SoongMakeVarsMk() string { |
| 555 | return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk") |
| 556 | } |
| 557 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 558 | func (c *configImpl) ProductOut() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 559 | return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice()) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 562 | func (c *configImpl) DevicePreviousProductConfig() string { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 563 | return filepath.Join(c.ProductOut(), "previous_build_config.mk") |
| 564 | } |
| 565 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 566 | func (c *configImpl) KatiPackageMkDir() string { |
| 567 | return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging") |
| 568 | } |
| 569 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 570 | func (c *configImpl) hostOutRoot() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 571 | return filepath.Join(c.OutDir(), "host") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | func (c *configImpl) HostOut() string { |
| 575 | return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag()) |
| 576 | } |
| 577 | |
| 578 | // This probably needs to be multi-valued, so not exporting it for now |
| 579 | func (c *configImpl) hostCrossOut() string { |
| 580 | if runtime.GOOS == "linux" { |
| 581 | return filepath.Join(c.hostOutRoot(), "windows-x86") |
| 582 | } else { |
| 583 | return "" |
| 584 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 585 | } |
| 586 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 587 | func (c *configImpl) HostPrebuiltTag() string { |
| 588 | if runtime.GOOS == "linux" { |
| 589 | return "linux-x86" |
| 590 | } else if runtime.GOOS == "darwin" { |
| 591 | return "darwin-x86" |
| 592 | } else { |
| 593 | panic("Unsupported OS") |
| 594 | } |
| 595 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 596 | |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 597 | func (c *configImpl) PrebuiltBuildTool(name string) string { |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 598 | if v, ok := c.environ.Get("SANITIZE_HOST"); ok { |
| 599 | if sanitize := strings.Fields(v); inList("address", sanitize) { |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 600 | asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name) |
| 601 | if _, err := os.Stat(asan); err == nil { |
| 602 | return asan |
| 603 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
| 606 | return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name) |
| 607 | } |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 608 | |
| 609 | func (c *configImpl) SetBuildBrokenDupRules(val bool) { |
| 610 | c.brokenDupRules = val |
| 611 | } |
| 612 | |
| 613 | func (c *configImpl) BuildBrokenDupRules() bool { |
| 614 | return c.brokenDupRules |
| 615 | } |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 616 | |
Dan Willemsen | d8aa39d | 2018-08-27 15:01:03 -0700 | [diff] [blame] | 617 | func (c *configImpl) SetBuildBrokenPhonyTargets(val bool) { |
| 618 | c.brokenPhonyTargets = val |
| 619 | } |
| 620 | |
| 621 | func (c *configImpl) BuildBrokenPhonyTargets() bool { |
| 622 | return c.brokenPhonyTargets |
| 623 | } |
| 624 | |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 625 | func (c *configImpl) SetTargetDeviceDir(dir string) { |
| 626 | c.targetDeviceDir = dir |
| 627 | } |
| 628 | |
| 629 | func (c *configImpl) TargetDeviceDir() string { |
| 630 | return c.targetDeviceDir |
| 631 | } |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 632 | |
| 633 | func (c *configImpl) SetPdkBuild(pdk bool) { |
| 634 | c.pdkBuild = pdk |
| 635 | } |
| 636 | |
| 637 | func (c *configImpl) IsPdkBuild() bool { |
| 638 | return c.pdkBuild |
| 639 | } |