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