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