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