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