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