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 ( |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 19 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
| 21 | "runtime" |
| 22 | "strconv" |
| 23 | "strings" |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 24 | "time" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 25 | |
| 26 | "android/soong/shared" |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 27 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 28 | "github.com/golang/protobuf/proto" |
| 29 | |
| 30 | smpb "android/soong/ui/metrics/metrics_proto" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 31 | ) |
| 32 | |
| 33 | type Config struct{ *configImpl } |
| 34 | |
| 35 | type configImpl struct { |
| 36 | // From the environment |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 37 | arguments []string |
| 38 | goma bool |
| 39 | environ *Environment |
| 40 | distDir string |
| 41 | buildDateTime string |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 42 | |
| 43 | // From the arguments |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 44 | parallel int |
| 45 | keepGoing int |
| 46 | verbose bool |
| 47 | checkbuild bool |
| 48 | dist bool |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 49 | skipConfig bool |
| 50 | skipKati bool |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 51 | skipNinja bool |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 52 | skipSoongTests bool |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 53 | |
| 54 | // From the product config |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 55 | katiArgs []string |
| 56 | ninjaArgs []string |
| 57 | katiSuffix string |
| 58 | targetDevice string |
| 59 | targetDeviceDir string |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 60 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 61 | // Autodetected |
| 62 | totalRAM uint64 |
| 63 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 64 | brokenDupRules bool |
| 65 | brokenUsesNetwork bool |
| 66 | brokenNinjaEnvVars []string |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 67 | |
| 68 | pathReplaced bool |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 69 | |
| 70 | useBazel bool |
| 71 | |
| 72 | // During Bazel execution, Bazel cannot write outside OUT_DIR. |
| 73 | // So if DIST_DIR is set to an external dir (outside of OUT_DIR), we need to rig it temporarily and then migrate files at the end of the build. |
| 74 | riggedDistDirForBazel string |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 75 | } |
| 76 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 77 | const srcDirFileCheck = "build/soong/root.bp" |
| 78 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 79 | var buildFiles = []string{"Android.mk", "Android.bp"} |
| 80 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 81 | type BuildAction uint |
| 82 | |
| 83 | const ( |
| 84 | // Builds all of the modules and their dependencies of a specified directory, relative to the root |
| 85 | // directory of the source tree. |
| 86 | BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota |
| 87 | |
| 88 | // Builds all of the modules and their dependencies of a list of specified directories. All specified |
| 89 | // directories are relative to the root directory of the source tree. |
| 90 | BUILD_MODULES_IN_DIRECTORIES |
Patrice Arruda | 3928206 | 2019-06-20 16:35:12 -0700 | [diff] [blame] | 91 | |
| 92 | // Build a list of specified modules. If none was specified, simply build the whole source tree. |
| 93 | BUILD_MODULES |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 94 | ) |
| 95 | |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 96 | type bazelBuildMode int |
| 97 | |
| 98 | // Bazel-related build modes. |
| 99 | const ( |
| 100 | // Don't use bazel at all. |
| 101 | noBazel bazelBuildMode = iota |
| 102 | |
| 103 | // Only generate build files (in a subdirectory of the out directory) and exit. |
| 104 | generateBuildFiles |
| 105 | |
| 106 | // Generate synthetic build files and incorporate these files into a build which |
| 107 | // partially uses Bazel. Build metadata may come from Android.bp or BUILD files. |
| 108 | mixedBuild |
| 109 | ) |
| 110 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 111 | // checkTopDir validates that the current directory is at the root directory of the source tree. |
| 112 | func checkTopDir(ctx Context) { |
| 113 | if _, err := os.Stat(srcDirFileCheck); err != nil { |
| 114 | if os.IsNotExist(err) { |
| 115 | ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck) |
| 116 | } |
| 117 | ctx.Fatalln("Error verifying tree state:", err) |
| 118 | } |
| 119 | } |
| 120 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 121 | func NewConfig(ctx Context, args ...string) Config { |
| 122 | ret := &configImpl{ |
| 123 | environ: OsEnvironment(), |
| 124 | } |
| 125 | |
Patrice Arruda | 9010917 | 2020-07-28 18:07:27 +0000 | [diff] [blame] | 126 | // Default matching ninja |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 127 | ret.parallel = runtime.NumCPU() + 2 |
| 128 | ret.keepGoing = 1 |
| 129 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 130 | ret.totalRAM = detectTotalRAM(ctx) |
| 131 | |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 132 | ret.parseArgs(ctx, args) |
| 133 | |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 134 | // Make sure OUT_DIR is set appropriately |
Dan Willemsen | 02f3add | 2017-05-12 13:50:19 -0700 | [diff] [blame] | 135 | if outDir, ok := ret.environ.Get("OUT_DIR"); ok { |
| 136 | ret.environ.Set("OUT_DIR", filepath.Clean(outDir)) |
| 137 | } else { |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 138 | outDir := "out" |
| 139 | if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok { |
| 140 | if wd, err := os.Getwd(); err != nil { |
| 141 | ctx.Fatalln("Failed to get working directory:", err) |
| 142 | } else { |
| 143 | outDir = filepath.Join(baseDir, filepath.Base(wd)) |
| 144 | } |
| 145 | } |
| 146 | ret.environ.Set("OUT_DIR", outDir) |
| 147 | } |
| 148 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 149 | if distDir, ok := ret.environ.Get("DIST_DIR"); ok { |
| 150 | ret.distDir = filepath.Clean(distDir) |
| 151 | } else { |
| 152 | ret.distDir = filepath.Join(ret.OutDir(), "dist") |
| 153 | } |
Dan Willemsen | d50e89f | 2018-10-16 17:49:25 -0700 | [diff] [blame] | 154 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 155 | ret.environ.Unset( |
| 156 | // We're already using it |
| 157 | "USE_SOONG_UI", |
| 158 | |
| 159 | // We should never use GOROOT/GOPATH from the shell environment |
| 160 | "GOROOT", |
| 161 | "GOPATH", |
| 162 | |
| 163 | // These should only come from Soong, not the environment. |
| 164 | "CLANG", |
| 165 | "CLANG_CXX", |
| 166 | "CCC_CC", |
| 167 | "CCC_CXX", |
| 168 | |
| 169 | // Used by the goma compiler wrapper, but should only be set by |
| 170 | // gomacc |
| 171 | "GOMACC_PATH", |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 172 | |
| 173 | // We handle this above |
| 174 | "OUT_DIR_COMMON_BASE", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 175 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 176 | // This is handled above too, and set for individual commands later |
| 177 | "DIST_DIR", |
| 178 | |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 179 | // Variables that have caused problems in the past |
Dan Willemsen | 1c504d9 | 2019-11-18 19:13:53 +0000 | [diff] [blame] | 180 | "BASH_ENV", |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 181 | "CDPATH", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 182 | "DISPLAY", |
| 183 | "GREP_OPTIONS", |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 184 | "NDK_ROOT", |
Dan Willemsen | 00fcb26 | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 185 | "POSIXLY_CORRECT", |
Dan Willemsen | c40e10b | 2017-07-11 14:30:00 -0700 | [diff] [blame] | 186 | |
| 187 | // Drop make flags |
| 188 | "MAKEFLAGS", |
| 189 | "MAKELEVEL", |
| 190 | "MFLAGS", |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 191 | |
| 192 | // Set in envsetup.sh, reset in makefiles |
| 193 | "ANDROID_JAVA_TOOLCHAIN", |
Colin Cross | 7f09c40 | 2018-07-11 14:49:31 -0700 | [diff] [blame] | 194 | |
| 195 | // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional |
| 196 | "ANDROID_BUILD_TOP", |
| 197 | "ANDROID_HOST_OUT", |
| 198 | "ANDROID_PRODUCT_OUT", |
| 199 | "ANDROID_HOST_OUT_TESTCASES", |
| 200 | "ANDROID_TARGET_OUT_TESTCASES", |
| 201 | "ANDROID_TOOLCHAIN", |
| 202 | "ANDROID_TOOLCHAIN_2ND_ARCH", |
| 203 | "ANDROID_DEV_SCRIPTS", |
| 204 | "ANDROID_EMULATOR_PREBUILTS", |
| 205 | "ANDROID_PRE_BUILD_PATHS", |
Dan Willemsen | f99915f | 2018-10-25 22:04:42 -0700 | [diff] [blame] | 206 | |
| 207 | // Only set in multiproduct_kati after config generation |
| 208 | "EMPTY_NINJA_FILE", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 209 | ) |
| 210 | |
Kousik Kumar | b328f6d | 2020-10-19 01:45:46 -0400 | [diff] [blame] | 211 | if ret.UseGoma() || ret.ForceUseGoma() { |
| 212 | ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.") |
| 213 | ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.") |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 214 | } |
| 215 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 216 | // Tell python not to spam the source tree with .pyc files. |
| 217 | ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1") |
| 218 | |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 219 | tmpDir := absPath(ctx, ret.TempDir()) |
| 220 | ret.environ.Set("TMPDIR", tmpDir) |
Dan Willemsen | 32a669b | 2018-03-08 19:42:00 -0800 | [diff] [blame] | 221 | |
Dan Willemsen | 70c1ff8 | 2019-08-21 14:56:13 -0700 | [diff] [blame] | 222 | // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes |
| 223 | symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(), |
| 224 | "llvm-binutils-stable/llvm-symbolizer") |
| 225 | ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath)) |
| 226 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 227 | // Precondition: the current directory is the top of the source tree |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 228 | checkTopDir(ctx) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 229 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 230 | if srcDir := absPath(ctx, "."); strings.ContainsRune(srcDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 231 | ctx.Println("You are building in a directory whose absolute path contains a space character:") |
| 232 | ctx.Println() |
| 233 | ctx.Printf("%q\n", srcDir) |
| 234 | ctx.Println() |
| 235 | ctx.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 239 | ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:") |
| 240 | ctx.Println() |
| 241 | ctx.Printf("%q\n", outDir) |
| 242 | ctx.Println() |
| 243 | ctx.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 246 | if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 247 | ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:") |
| 248 | ctx.Println() |
| 249 | ctx.Printf("%q\n", distDir) |
| 250 | ctx.Println() |
| 251 | ctx.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 252 | } |
| 253 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 254 | // Configure Java-related variables, including adding it to $PATH |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 255 | java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag()) |
| 256 | java9Home := filepath.Join("prebuilts/jdk/jdk9", ret.HostPrebuiltTag()) |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 257 | java11Home := filepath.Join("prebuilts/jdk/jdk11", ret.HostPrebuiltTag()) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 258 | javaHome := func() string { |
| 259 | if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok { |
| 260 | return override |
| 261 | } |
Pete Gillin | a7a3d64 | 2019-11-07 18:58:42 +0000 | [diff] [blame] | 262 | if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" { |
| 263 | ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN is no longer supported. An OpenJDK 11 toolchain is now the global default.") |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 264 | } |
Pete Gillin | abbcdda | 2019-10-28 16:15:33 +0000 | [diff] [blame] | 265 | return java11Home |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 266 | }() |
| 267 | absJavaHome := absPath(ctx, javaHome) |
| 268 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 269 | ret.configureLocale(ctx) |
| 270 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 271 | newPath := []string{filepath.Join(absJavaHome, "bin")} |
| 272 | if path, ok := ret.environ.Get("PATH"); ok && path != "" { |
| 273 | newPath = append(newPath, path) |
| 274 | } |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 275 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 276 | ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME") |
| 277 | ret.environ.Set("JAVA_HOME", absJavaHome) |
| 278 | ret.environ.Set("ANDROID_JAVA_HOME", javaHome) |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 279 | ret.environ.Set("ANDROID_JAVA8_HOME", java8Home) |
| 280 | ret.environ.Set("ANDROID_JAVA9_HOME", java9Home) |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 281 | ret.environ.Set("ANDROID_JAVA11_HOME", java11Home) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 282 | ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator))) |
| 283 | |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 284 | outDir := ret.OutDir() |
| 285 | buildDateTimeFile := filepath.Join(outDir, "build_date.txt") |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 286 | if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" { |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 287 | ret.buildDateTime = buildDateTime |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 288 | } else { |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 289 | ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10) |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 290 | } |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 291 | |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 292 | ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile) |
| 293 | |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 294 | if ret.UseRBE() { |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 295 | for k, v := range getRBEVars(ctx, Config{ret}) { |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 296 | ret.environ.Set(k, v) |
| 297 | } |
| 298 | } |
| 299 | |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 300 | bpd := ret.BazelMetricsDir() |
Patrice Arruda | af880da | 2020-11-13 08:41:26 -0800 | [diff] [blame] | 301 | if err := os.RemoveAll(bpd); err != nil { |
| 302 | ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err) |
| 303 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 304 | |
| 305 | ret.useBazel = ret.environ.IsEnvTrue("USE_BAZEL") |
| 306 | |
Patrice Arruda | af880da | 2020-11-13 08:41:26 -0800 | [diff] [blame] | 307 | if ret.UseBazel() { |
| 308 | if err := os.MkdirAll(bpd, 0777); err != nil { |
| 309 | ctx.Fatalf("Failed to create bazel profile directory %q: %v", bpd, err) |
| 310 | } |
| 311 | } |
| 312 | |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 313 | if ret.UseBazel() { |
| 314 | ret.riggedDistDirForBazel = filepath.Join(ret.OutDir(), "dist") |
| 315 | } else { |
| 316 | // Not rigged |
| 317 | ret.riggedDistDirForBazel = ret.distDir |
| 318 | } |
| 319 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 320 | c := Config{ret} |
| 321 | storeConfigMetrics(ctx, c) |
| 322 | return c |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 325 | // NewBuildActionConfig returns a build configuration based on the build action. The arguments are |
| 326 | // processed based on the build action and extracts any arguments that belongs to the build action. |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 327 | func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config { |
| 328 | return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 331 | // storeConfigMetrics selects a set of configuration information and store in |
| 332 | // the metrics system for further analysis. |
| 333 | func storeConfigMetrics(ctx Context, config Config) { |
| 334 | if ctx.Metrics == nil { |
| 335 | return |
| 336 | } |
| 337 | |
| 338 | b := &smpb.BuildConfig{ |
Patrice Arruda | c97d6dc | 2020-09-28 18:22:07 +0000 | [diff] [blame] | 339 | ForceUseGoma: proto.Bool(config.ForceUseGoma()), |
| 340 | UseGoma: proto.Bool(config.UseGoma()), |
| 341 | UseRbe: proto.Bool(config.UseRBE()), |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 342 | } |
| 343 | ctx.Metrics.BuildConfig(b) |
Patrice Arruda | 3edfd48 | 2020-10-13 23:58:41 +0000 | [diff] [blame] | 344 | |
| 345 | s := &smpb.SystemResourceInfo{ |
| 346 | TotalPhysicalMemory: proto.Uint64(config.TotalRAM()), |
| 347 | AvailableCpus: proto.Int32(int32(runtime.NumCPU())), |
| 348 | } |
| 349 | ctx.Metrics.SystemResourceInfo(s) |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 352 | // getConfigArgs processes the command arguments based on the build action and creates a set of new |
| 353 | // arguments to be accepted by Config. |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 354 | func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 355 | // The next block of code verifies that the current directory is the root directory of the source |
| 356 | // tree. It then finds the relative path of dir based on the root directory of the source tree |
| 357 | // and verify that dir is inside of the source tree. |
| 358 | checkTopDir(ctx) |
| 359 | topDir, err := os.Getwd() |
| 360 | if err != nil { |
| 361 | ctx.Fatalf("Error retrieving top directory: %v", err) |
| 362 | } |
Patrice Arruda | baba9a9 | 2019-07-03 10:47:34 -0700 | [diff] [blame] | 363 | dir, err = filepath.EvalSymlinks(dir) |
| 364 | if err != nil { |
| 365 | ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err) |
| 366 | } |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 367 | dir, err = filepath.Abs(dir) |
| 368 | if err != nil { |
| 369 | ctx.Fatalf("Unable to find absolute path %s: %v", dir, err) |
| 370 | } |
| 371 | relDir, err := filepath.Rel(topDir, dir) |
| 372 | if err != nil { |
| 373 | ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err) |
| 374 | } |
| 375 | // If there are ".." in the path, it's not in the source tree. |
| 376 | if strings.Contains(relDir, "..") { |
| 377 | ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir) |
| 378 | } |
| 379 | |
| 380 | configArgs := args[:] |
| 381 | |
| 382 | // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to |
| 383 | // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules. |
| 384 | targetNamePrefix := "MODULES-IN-" |
| 385 | if inList("GET-INSTALL-PATH", configArgs) { |
| 386 | targetNamePrefix = "GET-INSTALL-PATH-IN-" |
| 387 | configArgs = removeFromList("GET-INSTALL-PATH", configArgs) |
| 388 | } |
| 389 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 390 | var targets []string |
| 391 | |
| 392 | switch action { |
Patrice Arruda | 3928206 | 2019-06-20 16:35:12 -0700 | [diff] [blame] | 393 | case BUILD_MODULES: |
| 394 | // No additional processing is required when building a list of specific modules or all modules. |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 395 | case BUILD_MODULES_IN_A_DIRECTORY: |
| 396 | // If dir is the root source tree, all the modules are built of the source tree are built so |
| 397 | // no need to find the build file. |
| 398 | if topDir == dir { |
| 399 | break |
| 400 | } |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 401 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 402 | buildFile := findBuildFile(ctx, relDir) |
| 403 | if buildFile == "" { |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 404 | ctx.Fatalf("Build file not found for %s directory", relDir) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 405 | } |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 406 | targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)} |
| 407 | case BUILD_MODULES_IN_DIRECTORIES: |
| 408 | newConfigArgs, dirs := splitArgs(configArgs) |
| 409 | configArgs = newConfigArgs |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 410 | targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | // Tidy only override all other specified targets. |
| 414 | tidyOnly := os.Getenv("WITH_TIDY_ONLY") |
| 415 | if tidyOnly == "true" || tidyOnly == "1" { |
| 416 | configArgs = append(configArgs, "tidy_only") |
| 417 | } else { |
| 418 | configArgs = append(configArgs, targets...) |
| 419 | } |
| 420 | |
| 421 | return configArgs |
| 422 | } |
| 423 | |
| 424 | // convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name. |
| 425 | func convertToTarget(dir string, targetNamePrefix string) string { |
| 426 | return targetNamePrefix + strings.ReplaceAll(dir, "/", "-") |
| 427 | } |
| 428 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 429 | // hasBuildFile returns true if dir contains an Android build file. |
| 430 | func hasBuildFile(ctx Context, dir string) bool { |
| 431 | for _, buildFile := range buildFiles { |
| 432 | _, err := os.Stat(filepath.Join(dir, buildFile)) |
| 433 | if err == nil { |
| 434 | return true |
| 435 | } |
| 436 | if !os.IsNotExist(err) { |
| 437 | ctx.Fatalf("Error retrieving the build file stats: %v", err) |
| 438 | } |
| 439 | } |
| 440 | return false |
| 441 | } |
| 442 | |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 443 | // findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file |
| 444 | // in the current and any sub directory of dir. If a build file is not found, traverse the path |
| 445 | // up by one directory and repeat again until either a build file is found or reached to the root |
| 446 | // source tree. The returned filename of build file is "Android.mk". If one was not found, a blank |
| 447 | // string is returned. |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 448 | func findBuildFile(ctx Context, dir string) string { |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 449 | // If the string is empty or ".", assume it is top directory of the source tree. |
| 450 | if dir == "" || dir == "." { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 451 | return "" |
| 452 | } |
| 453 | |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 454 | found := false |
| 455 | for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) { |
| 456 | err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error { |
| 457 | if err != nil { |
| 458 | return err |
| 459 | } |
| 460 | if found { |
| 461 | return filepath.SkipDir |
| 462 | } |
| 463 | if info.IsDir() { |
| 464 | return nil |
| 465 | } |
| 466 | for _, buildFile := range buildFiles { |
| 467 | if info.Name() == buildFile { |
| 468 | found = true |
| 469 | return filepath.SkipDir |
| 470 | } |
| 471 | } |
| 472 | return nil |
| 473 | }) |
| 474 | if err != nil { |
| 475 | ctx.Fatalf("Error finding Android build file: %v", err) |
| 476 | } |
| 477 | |
| 478 | if found { |
| 479 | return filepath.Join(buildDir, "Android.mk") |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 480 | } |
| 481 | } |
| 482 | |
| 483 | return "" |
| 484 | } |
| 485 | |
| 486 | // splitArgs iterates over the arguments list and splits into two lists: arguments and directories. |
| 487 | func splitArgs(args []string) (newArgs []string, dirs []string) { |
| 488 | specialArgs := map[string]bool{ |
| 489 | "showcommands": true, |
| 490 | "snod": true, |
| 491 | "dist": true, |
| 492 | "checkbuild": true, |
| 493 | } |
| 494 | |
| 495 | newArgs = []string{} |
| 496 | dirs = []string{} |
| 497 | |
| 498 | for _, arg := range args { |
| 499 | // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory. |
| 500 | if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 { |
| 501 | newArgs = append(newArgs, arg) |
| 502 | continue |
| 503 | } |
| 504 | |
| 505 | if _, ok := specialArgs[arg]; ok { |
| 506 | newArgs = append(newArgs, arg) |
| 507 | continue |
| 508 | } |
| 509 | |
| 510 | dirs = append(dirs, arg) |
| 511 | } |
| 512 | |
| 513 | return newArgs, dirs |
| 514 | } |
| 515 | |
| 516 | // getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a |
| 517 | // directory from the dirs list does not exist, a fatal error is raised. relDir is related to the |
| 518 | // source root tree where the build action command was invoked. Each directory is validated if the |
| 519 | // build file can be found and follows the format "dir1:target1,target2,...". Target is optional. |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 520 | func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 521 | for _, dir := range dirs { |
| 522 | // The directory may have specified specific modules to build. ":" is the separator to separate |
| 523 | // the directory and the list of modules. |
| 524 | s := strings.Split(dir, ":") |
| 525 | l := len(s) |
| 526 | if l > 2 { // more than one ":" was specified. |
| 527 | ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir) |
| 528 | } |
| 529 | |
| 530 | dir = filepath.Join(relDir, s[0]) |
| 531 | if _, err := os.Stat(dir); err != nil { |
| 532 | ctx.Fatalf("couldn't find directory %s", dir) |
| 533 | } |
| 534 | |
| 535 | // Verify that if there are any targets specified after ":". Each target is separated by ",". |
| 536 | var newTargets []string |
| 537 | if l == 2 && s[1] != "" { |
| 538 | newTargets = strings.Split(s[1], ",") |
| 539 | if inList("", newTargets) { |
| 540 | ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir) |
| 541 | } |
| 542 | } |
| 543 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 544 | // If there are specified targets to build in dir, an android build file must exist for the one |
| 545 | // shot build. For the non-targets case, find the appropriate build file and build all the |
| 546 | // modules in dir (or the closest one in the dir path). |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 547 | if len(newTargets) > 0 { |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 548 | if !hasBuildFile(ctx, dir) { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 549 | ctx.Fatalf("Couldn't locate a build file from %s directory", dir) |
| 550 | } |
| 551 | } else { |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 552 | buildFile := findBuildFile(ctx, dir) |
| 553 | if buildFile == "" { |
| 554 | ctx.Fatalf("Build file not found for %s directory", dir) |
| 555 | } |
| 556 | newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)} |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 557 | } |
| 558 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 559 | targets = append(targets, newTargets...) |
| 560 | } |
| 561 | |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 562 | return targets |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 565 | func (c *configImpl) parseArgs(ctx Context, args []string) { |
| 566 | for i := 0; i < len(args); i++ { |
| 567 | arg := strings.TrimSpace(args[i]) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 568 | if arg == "--make-mode" { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 569 | } else if arg == "showcommands" { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 570 | c.verbose = true |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 571 | } else if arg == "--skip-ninja" { |
| 572 | c.skipNinja = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 573 | } else if arg == "--skip-make" { |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 574 | c.skipConfig = true |
| 575 | c.skipKati = true |
| 576 | } else if arg == "--skip-kati" { |
| 577 | c.skipKati = true |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 578 | } else if arg == "--skip-soong-tests" { |
| 579 | c.skipSoongTests = true |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 580 | } else if len(arg) > 0 && arg[0] == '-' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 581 | parseArgNum := func(def int) int { |
| 582 | if len(arg) > 2 { |
| 583 | p, err := strconv.ParseUint(arg[2:], 10, 31) |
| 584 | if err != nil { |
| 585 | ctx.Fatalf("Failed to parse %q: %v", arg, err) |
| 586 | } |
| 587 | return int(p) |
| 588 | } else if i+1 < len(args) { |
| 589 | p, err := strconv.ParseUint(args[i+1], 10, 31) |
| 590 | if err == nil { |
| 591 | i++ |
| 592 | return int(p) |
| 593 | } |
| 594 | } |
| 595 | return def |
| 596 | } |
| 597 | |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 598 | if len(arg) > 1 && arg[1] == 'j' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 599 | c.parallel = parseArgNum(c.parallel) |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 600 | } else if len(arg) > 1 && arg[1] == 'k' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 601 | c.keepGoing = parseArgNum(0) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 602 | } else { |
| 603 | ctx.Fatalln("Unknown option:", arg) |
| 604 | } |
Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 605 | } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 { |
Dan Willemsen | 6dfe30a | 2018-09-10 12:41:10 -0700 | [diff] [blame] | 606 | if k == "OUT_DIR" { |
| 607 | ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.") |
| 608 | } |
Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 609 | c.environ.Set(k, v) |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 610 | } else if arg == "dist" { |
| 611 | c.dist = true |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 612 | } else { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 613 | if arg == "checkbuild" { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 614 | c.checkbuild = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 615 | } |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 616 | c.arguments = append(c.arguments, arg) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 617 | } |
| 618 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 621 | func (c *configImpl) configureLocale(ctx Context) { |
| 622 | cmd := Command(ctx, Config{c}, "locale", "locale", "-a") |
| 623 | output, err := cmd.Output() |
| 624 | |
| 625 | var locales []string |
| 626 | if err == nil { |
| 627 | locales = strings.Split(string(output), "\n") |
| 628 | } else { |
| 629 | // If we're unable to list the locales, let's assume en_US.UTF-8 |
| 630 | locales = []string{"en_US.UTF-8"} |
| 631 | ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales) |
| 632 | } |
| 633 | |
| 634 | // gettext uses LANGUAGE, which is passed directly through |
| 635 | |
| 636 | // For LANG and LC_*, only preserve the evaluated version of |
| 637 | // LC_MESSAGES |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 638 | userLang := "" |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 639 | if lc_all, ok := c.environ.Get("LC_ALL"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 640 | userLang = lc_all |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 641 | } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 642 | userLang = lc_messages |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 643 | } else if lang, ok := c.environ.Get("LANG"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 644 | userLang = lang |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | c.environ.UnsetWithPrefix("LC_") |
| 648 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 649 | if userLang != "" { |
| 650 | c.environ.Set("LC_MESSAGES", userLang) |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed |
| 654 | // for others) |
| 655 | if inList("C.UTF-8", locales) { |
| 656 | c.environ.Set("LANG", "C.UTF-8") |
Aaron Kling | d236e0e | 2018-08-07 19:21:36 -0500 | [diff] [blame] | 657 | } else if inList("C.utf8", locales) { |
| 658 | // These normalize to the same thing |
| 659 | c.environ.Set("LANG", "C.UTF-8") |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 660 | } else if inList("en_US.UTF-8", locales) { |
| 661 | c.environ.Set("LANG", "en_US.UTF-8") |
| 662 | } else if inList("en_US.utf8", locales) { |
| 663 | // These normalize to the same thing |
| 664 | c.environ.Set("LANG", "en_US.UTF-8") |
| 665 | } else { |
| 666 | ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8") |
| 667 | } |
| 668 | } |
| 669 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 670 | // Lunch configures the environment for a specific product similarly to the |
| 671 | // `lunch` bash function. |
| 672 | func (c *configImpl) Lunch(ctx Context, product, variant string) { |
| 673 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 674 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 675 | } |
| 676 | |
| 677 | c.environ.Set("TARGET_PRODUCT", product) |
| 678 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 679 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 680 | c.environ.Unset("TARGET_BUILD_APPS") |
Martin Stjernholm | 0880233 | 2020-06-04 17:00:01 +0100 | [diff] [blame] | 681 | c.environ.Unset("TARGET_BUILD_UNBUNDLED") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | // Tapas configures the environment to build one or more unbundled apps, |
| 685 | // similarly to the `tapas` bash function. |
| 686 | func (c *configImpl) Tapas(ctx Context, apps []string, arch, variant string) { |
| 687 | if len(apps) == 0 { |
| 688 | apps = []string{"all"} |
| 689 | } |
| 690 | if variant == "" { |
| 691 | variant = "eng" |
| 692 | } |
| 693 | |
| 694 | if variant != "eng" && variant != "userdebug" && variant != "user" { |
| 695 | ctx.Fatalf("Invalid variant %q. Must be one of 'user', 'userdebug' or 'eng'", variant) |
| 696 | } |
| 697 | |
| 698 | var product string |
| 699 | switch arch { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 700 | case "arm", "": |
| 701 | product = "aosp_arm" |
| 702 | case "arm64": |
| 703 | product = "aosm_arm64" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 704 | case "x86": |
| 705 | product = "aosp_x86" |
| 706 | case "x86_64": |
| 707 | product = "aosp_x86_64" |
| 708 | default: |
| 709 | ctx.Fatalf("Invalid architecture: %q", arch) |
| 710 | } |
| 711 | |
| 712 | c.environ.Set("TARGET_PRODUCT", product) |
| 713 | c.environ.Set("TARGET_BUILD_VARIANT", variant) |
| 714 | c.environ.Set("TARGET_BUILD_TYPE", "release") |
| 715 | c.environ.Set("TARGET_BUILD_APPS", strings.Join(apps, " ")) |
| 716 | } |
| 717 | |
| 718 | func (c *configImpl) Environment() *Environment { |
| 719 | return c.environ |
| 720 | } |
| 721 | |
| 722 | func (c *configImpl) Arguments() []string { |
| 723 | return c.arguments |
| 724 | } |
| 725 | |
| 726 | func (c *configImpl) OutDir() string { |
| 727 | if outDir, ok := c.environ.Get("OUT_DIR"); ok { |
Patrice Arruda | 19bd53e | 2019-07-08 17:26:47 -0700 | [diff] [blame] | 728 | return outDir |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 729 | } |
| 730 | return "out" |
| 731 | } |
| 732 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 733 | func (c *configImpl) DistDir() string { |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 734 | if c.UseBazel() { |
| 735 | return c.riggedDistDirForBazel |
| 736 | } else { |
| 737 | return c.distDir |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | func (c *configImpl) RealDistDir() string { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 742 | return c.distDir |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 743 | } |
| 744 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 745 | func (c *configImpl) NinjaArgs() []string { |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 746 | if c.skipKati { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 747 | return c.arguments |
| 748 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 749 | return c.ninjaArgs |
| 750 | } |
| 751 | |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 752 | func (c *configImpl) BazelOutDir() string { |
| 753 | return filepath.Join(c.OutDir(), "bazel") |
| 754 | } |
| 755 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 756 | func (c *configImpl) SoongOutDir() string { |
| 757 | return filepath.Join(c.OutDir(), "soong") |
| 758 | } |
| 759 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 760 | func (c *configImpl) TempDir() string { |
| 761 | return shared.TempDirForOutDir(c.SoongOutDir()) |
| 762 | } |
| 763 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 764 | func (c *configImpl) FileListDir() string { |
| 765 | return filepath.Join(c.OutDir(), ".module_paths") |
| 766 | } |
| 767 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 768 | func (c *configImpl) KatiSuffix() string { |
| 769 | if c.katiSuffix != "" { |
| 770 | return c.katiSuffix |
| 771 | } |
| 772 | panic("SetKatiSuffix has not been called") |
| 773 | } |
| 774 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 775 | // Checkbuild returns true if "checkbuild" was one of the build goals, which means that the |
| 776 | // user is interested in additional checks at the expense of build time. |
| 777 | func (c *configImpl) Checkbuild() bool { |
| 778 | return c.checkbuild |
| 779 | } |
| 780 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 781 | func (c *configImpl) Dist() bool { |
| 782 | return c.dist |
| 783 | } |
| 784 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 785 | func (c *configImpl) IsVerbose() bool { |
| 786 | return c.verbose |
| 787 | } |
| 788 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 789 | func (c *configImpl) SkipKati() bool { |
| 790 | return c.skipKati |
| 791 | } |
| 792 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 793 | func (c *configImpl) SkipNinja() bool { |
| 794 | return c.skipNinja |
| 795 | } |
| 796 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 797 | func (c *configImpl) SkipConfig() bool { |
| 798 | return c.skipConfig |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 801 | func (c *configImpl) TargetProduct() string { |
| 802 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 803 | return v |
| 804 | } |
| 805 | panic("TARGET_PRODUCT is not defined") |
| 806 | } |
| 807 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 808 | func (c *configImpl) TargetDevice() string { |
| 809 | return c.targetDevice |
| 810 | } |
| 811 | |
| 812 | func (c *configImpl) SetTargetDevice(device string) { |
| 813 | c.targetDevice = device |
| 814 | } |
| 815 | |
| 816 | func (c *configImpl) TargetBuildVariant() string { |
| 817 | if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok { |
| 818 | return v |
| 819 | } |
| 820 | panic("TARGET_BUILD_VARIANT is not defined") |
| 821 | } |
| 822 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 823 | func (c *configImpl) KatiArgs() []string { |
| 824 | return c.katiArgs |
| 825 | } |
| 826 | |
| 827 | func (c *configImpl) Parallel() int { |
| 828 | return c.parallel |
| 829 | } |
| 830 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 831 | func (c *configImpl) HighmemParallel() int { |
| 832 | if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok { |
| 833 | return i |
| 834 | } |
| 835 | |
| 836 | const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024 |
| 837 | parallel := c.Parallel() |
| 838 | if c.UseRemoteBuild() { |
| 839 | // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism |
| 840 | // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs |
| 841 | // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention. |
| 842 | // Return 1/16th of the size of the local pool, rounding up. |
| 843 | return (parallel + 15) / 16 |
| 844 | } else if c.totalRAM == 0 { |
| 845 | // Couldn't detect the total RAM, don't restrict highmem processes. |
| 846 | return parallel |
Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 847 | } else if c.totalRAM <= 16*1024*1024*1024 { |
| 848 | // Less than 16GB of ram, restrict to 1 highmem processes |
| 849 | return 1 |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 850 | } else if c.totalRAM <= 32*1024*1024*1024 { |
| 851 | // Less than 32GB of ram, restrict to 2 highmem processes |
| 852 | return 2 |
| 853 | } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel { |
| 854 | // If less than 8GB total RAM per process, reduce the number of highmem processes |
| 855 | return p |
| 856 | } |
| 857 | // No restriction on highmem processes |
| 858 | return parallel |
| 859 | } |
| 860 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 861 | func (c *configImpl) TotalRAM() uint64 { |
| 862 | return c.totalRAM |
| 863 | } |
| 864 | |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 865 | // ForceUseGoma determines whether we should override Goma deprecation |
| 866 | // and use Goma for the current build or not. |
| 867 | func (c *configImpl) ForceUseGoma() bool { |
| 868 | if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok { |
| 869 | v = strings.TrimSpace(v) |
| 870 | if v != "" && v != "false" { |
| 871 | return true |
| 872 | } |
| 873 | } |
| 874 | return false |
| 875 | } |
| 876 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 877 | func (c *configImpl) UseGoma() bool { |
| 878 | if v, ok := c.environ.Get("USE_GOMA"); ok { |
| 879 | v = strings.TrimSpace(v) |
| 880 | if v != "" && v != "false" { |
| 881 | return true |
| 882 | } |
| 883 | } |
| 884 | return false |
| 885 | } |
| 886 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 887 | func (c *configImpl) StartGoma() bool { |
| 888 | if !c.UseGoma() { |
| 889 | return false |
| 890 | } |
| 891 | |
| 892 | if v, ok := c.environ.Get("NOSTART_GOMA"); ok { |
| 893 | v = strings.TrimSpace(v) |
| 894 | if v != "" && v != "false" { |
| 895 | return false |
| 896 | } |
| 897 | } |
| 898 | return true |
| 899 | } |
| 900 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 901 | func (c *configImpl) UseRBE() bool { |
| 902 | if v, ok := c.environ.Get("USE_RBE"); ok { |
| 903 | v = strings.TrimSpace(v) |
| 904 | if v != "" && v != "false" { |
| 905 | return true |
| 906 | } |
| 907 | } |
| 908 | return false |
| 909 | } |
| 910 | |
Patrice Arruda | 0c1c456 | 2020-11-11 13:01:25 -0800 | [diff] [blame] | 911 | func (c *configImpl) UseBazel() bool { |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 912 | return c.useBazel |
Patrice Arruda | 0c1c456 | 2020-11-11 13:01:25 -0800 | [diff] [blame] | 913 | } |
| 914 | |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 915 | func (c *configImpl) bazelBuildMode() bazelBuildMode { |
| 916 | if c.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") { |
| 917 | return mixedBuild |
| 918 | } else if c.Environment().IsEnvTrue("GENERATE_BAZEL_FILES") { |
| 919 | return generateBuildFiles |
| 920 | } else { |
| 921 | return noBazel |
| 922 | } |
| 923 | } |
| 924 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 925 | func (c *configImpl) StartRBE() bool { |
| 926 | if !c.UseRBE() { |
| 927 | return false |
| 928 | } |
| 929 | |
| 930 | if v, ok := c.environ.Get("NOSTART_RBE"); ok { |
| 931 | v = strings.TrimSpace(v) |
| 932 | if v != "" && v != "false" { |
| 933 | return false |
| 934 | } |
| 935 | } |
| 936 | return true |
| 937 | } |
| 938 | |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 939 | func (c *configImpl) rbeLogDir() string { |
Kousik Kumar | 0d15a72 | 2020-09-23 02:54:11 -0400 | [diff] [blame] | 940 | for _, f := range []string{"RBE_log_dir", "FLAG_log_dir"} { |
| 941 | if v, ok := c.environ.Get(f); ok { |
| 942 | return v |
| 943 | } |
| 944 | } |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 945 | if c.Dist() { |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 946 | return c.LogsDir() |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 947 | } |
| 948 | return c.OutDir() |
| 949 | } |
| 950 | |
| 951 | func (c *configImpl) rbeStatsOutputDir() string { |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 952 | for _, f := range []string{"RBE_output_dir", "FLAG_output_dir"} { |
| 953 | if v, ok := c.environ.Get(f); ok { |
| 954 | return v |
| 955 | } |
| 956 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 957 | return c.rbeLogDir() |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | func (c *configImpl) rbeLogPath() string { |
| 961 | for _, f := range []string{"RBE_log_path", "FLAG_log_path"} { |
| 962 | if v, ok := c.environ.Get(f); ok { |
| 963 | return v |
| 964 | } |
| 965 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 966 | return fmt.Sprintf("text://%v/reproxy_log.txt", c.rbeLogDir()) |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | func (c *configImpl) rbeExecRoot() string { |
| 970 | for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} { |
| 971 | if v, ok := c.environ.Get(f); ok { |
| 972 | return v |
| 973 | } |
| 974 | } |
| 975 | wd, err := os.Getwd() |
| 976 | if err != nil { |
| 977 | return "" |
| 978 | } |
| 979 | return wd |
| 980 | } |
| 981 | |
| 982 | func (c *configImpl) rbeDir() string { |
| 983 | if v, ok := c.environ.Get("RBE_DIR"); ok { |
| 984 | return v |
| 985 | } |
| 986 | return "prebuilts/remoteexecution-client/live/" |
| 987 | } |
| 988 | |
| 989 | func (c *configImpl) rbeReproxy() string { |
| 990 | for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} { |
| 991 | if v, ok := c.environ.Get(f); ok { |
| 992 | return v |
| 993 | } |
| 994 | } |
| 995 | return filepath.Join(c.rbeDir(), "reproxy") |
| 996 | } |
| 997 | |
| 998 | func (c *configImpl) rbeAuth() (string, string) { |
| 999 | credFlags := []string{"use_application_default_credentials", "use_gce_credentials", "credential_file"} |
| 1000 | for _, cf := range credFlags { |
| 1001 | for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} { |
| 1002 | if v, ok := c.environ.Get(f); ok { |
| 1003 | v = strings.TrimSpace(v) |
| 1004 | if v != "" && v != "false" && v != "0" { |
| 1005 | return "RBE_" + cf, v |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | return "RBE_use_application_default_credentials", "true" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Colin Cross | 9016b91 | 2019-11-11 14:57:42 -0800 | [diff] [blame] | 1013 | func (c *configImpl) UseRemoteBuild() bool { |
| 1014 | return c.UseGoma() || c.UseRBE() |
| 1015 | } |
| 1016 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1017 | // RemoteParallel controls how many remote jobs (i.e., commands which contain |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 1018 | // 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] | 1019 | // still limited by Parallel() |
| 1020 | func (c *configImpl) RemoteParallel() int { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 1021 | if !c.UseRemoteBuild() { |
| 1022 | return 0 |
| 1023 | } |
| 1024 | if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok { |
| 1025 | return i |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1026 | } |
| 1027 | return 500 |
| 1028 | } |
| 1029 | |
| 1030 | func (c *configImpl) SetKatiArgs(args []string) { |
| 1031 | c.katiArgs = args |
| 1032 | } |
| 1033 | |
| 1034 | func (c *configImpl) SetNinjaArgs(args []string) { |
| 1035 | c.ninjaArgs = args |
| 1036 | } |
| 1037 | |
| 1038 | func (c *configImpl) SetKatiSuffix(suffix string) { |
| 1039 | c.katiSuffix = suffix |
| 1040 | } |
| 1041 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1042 | func (c *configImpl) LastKatiSuffixFile() string { |
| 1043 | return filepath.Join(c.OutDir(), "last_kati_suffix") |
| 1044 | } |
| 1045 | |
| 1046 | func (c *configImpl) HasKatiSuffix() bool { |
| 1047 | return c.katiSuffix != "" |
| 1048 | } |
| 1049 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1050 | func (c *configImpl) KatiEnvFile() string { |
| 1051 | return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh") |
| 1052 | } |
| 1053 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 1054 | func (c *configImpl) KatiBuildNinjaFile() string { |
| 1055 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1056 | } |
| 1057 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 1058 | func (c *configImpl) KatiPackageNinjaFile() string { |
| 1059 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja") |
| 1060 | } |
| 1061 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1062 | func (c *configImpl) SoongNinjaFile() string { |
| 1063 | return filepath.Join(c.SoongOutDir(), "build.ninja") |
| 1064 | } |
| 1065 | |
| 1066 | func (c *configImpl) CombinedNinjaFile() string { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1067 | if c.katiSuffix == "" { |
| 1068 | return filepath.Join(c.OutDir(), "combined.ninja") |
| 1069 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1070 | return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja") |
| 1071 | } |
| 1072 | |
| 1073 | func (c *configImpl) SoongAndroidMk() string { |
| 1074 | return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk") |
| 1075 | } |
| 1076 | |
| 1077 | func (c *configImpl) SoongMakeVarsMk() string { |
| 1078 | return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk") |
| 1079 | } |
| 1080 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1081 | func (c *configImpl) ProductOut() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 1082 | return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice()) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 1085 | func (c *configImpl) DevicePreviousProductConfig() string { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1086 | return filepath.Join(c.ProductOut(), "previous_build_config.mk") |
| 1087 | } |
| 1088 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 1089 | func (c *configImpl) KatiPackageMkDir() string { |
| 1090 | return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging") |
| 1091 | } |
| 1092 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1093 | func (c *configImpl) hostOutRoot() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 1094 | return filepath.Join(c.OutDir(), "host") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | func (c *configImpl) HostOut() string { |
| 1098 | return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag()) |
| 1099 | } |
| 1100 | |
| 1101 | // This probably needs to be multi-valued, so not exporting it for now |
| 1102 | func (c *configImpl) hostCrossOut() string { |
| 1103 | if runtime.GOOS == "linux" { |
| 1104 | return filepath.Join(c.hostOutRoot(), "windows-x86") |
| 1105 | } else { |
| 1106 | return "" |
| 1107 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1110 | func (c *configImpl) HostPrebuiltTag() string { |
| 1111 | if runtime.GOOS == "linux" { |
| 1112 | return "linux-x86" |
| 1113 | } else if runtime.GOOS == "darwin" { |
| 1114 | return "darwin-x86" |
| 1115 | } else { |
| 1116 | panic("Unsupported OS") |
| 1117 | } |
| 1118 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1119 | |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 1120 | func (c *configImpl) PrebuiltBuildTool(name string) string { |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1121 | if v, ok := c.environ.Get("SANITIZE_HOST"); ok { |
| 1122 | if sanitize := strings.Fields(v); inList("address", sanitize) { |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 1123 | asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name) |
| 1124 | if _, err := os.Stat(asan); err == nil { |
| 1125 | return asan |
| 1126 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name) |
| 1130 | } |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 1131 | |
| 1132 | func (c *configImpl) SetBuildBrokenDupRules(val bool) { |
| 1133 | c.brokenDupRules = val |
| 1134 | } |
| 1135 | |
| 1136 | func (c *configImpl) BuildBrokenDupRules() bool { |
| 1137 | return c.brokenDupRules |
| 1138 | } |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 1139 | |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 1140 | func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) { |
| 1141 | c.brokenUsesNetwork = val |
| 1142 | } |
| 1143 | |
| 1144 | func (c *configImpl) BuildBrokenUsesNetwork() bool { |
| 1145 | return c.brokenUsesNetwork |
| 1146 | } |
| 1147 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 1148 | func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) { |
| 1149 | c.brokenNinjaEnvVars = val |
| 1150 | } |
| 1151 | |
| 1152 | func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string { |
| 1153 | return c.brokenNinjaEnvVars |
| 1154 | } |
| 1155 | |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 1156 | func (c *configImpl) SetTargetDeviceDir(dir string) { |
| 1157 | c.targetDeviceDir = dir |
| 1158 | } |
| 1159 | |
| 1160 | func (c *configImpl) TargetDeviceDir() string { |
| 1161 | return c.targetDeviceDir |
| 1162 | } |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 1163 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 1164 | func (c *configImpl) BuildDateTime() string { |
| 1165 | return c.buildDateTime |
| 1166 | } |
| 1167 | |
| 1168 | func (c *configImpl) MetricsUploaderApp() string { |
| 1169 | if p, ok := c.environ.Get("ANDROID_ENABLE_METRICS_UPLOAD"); ok { |
| 1170 | return p |
| 1171 | } |
| 1172 | return "" |
| 1173 | } |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1174 | |
| 1175 | // LogsDir returns the logs directory where build log and metrics |
| 1176 | // files are located. By default, the logs directory is the out |
| 1177 | // directory. If the argument dist is specified, the logs directory |
| 1178 | // is <dist_dir>/logs. |
| 1179 | func (c *configImpl) LogsDir() string { |
| 1180 | if c.Dist() { |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 1181 | // Always write logs to the real dist dir, even if Bazel is using a rigged dist dir for other files |
| 1182 | return filepath.Join(c.RealDistDir(), "logs") |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1183 | } |
| 1184 | return c.OutDir() |
| 1185 | } |
| 1186 | |
| 1187 | // BazelMetricsDir returns the <logs dir>/bazel_metrics directory |
| 1188 | // where the bazel profiles are located. |
| 1189 | func (c *configImpl) BazelMetricsDir() string { |
| 1190 | return filepath.Join(c.LogsDir(), "bazel_metrics") |
| 1191 | } |