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