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