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