Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 18 | "encoding/json" |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 19 | "errors" |
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" |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 22 | "math/rand" |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 23 | "os" |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 24 | "os/exec" |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 25 | "os/user" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 26 | "path/filepath" |
| 27 | "runtime" |
| 28 | "strconv" |
| 29 | "strings" |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 30 | "syscall" |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 31 | "time" |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 32 | |
| 33 | "android/soong/shared" |
LaMont Jones | 9a91286 | 2023-11-06 22:11:08 +0000 | [diff] [blame] | 34 | "android/soong/ui/metrics" |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 35 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 36 | "google.golang.org/protobuf/proto" |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 37 | |
| 38 | smpb "android/soong/ui/metrics/metrics_proto" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 39 | ) |
| 40 | |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 41 | const ( |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame] | 42 | envConfigDir = "vendor/google/tools/soong_config" |
| 43 | jsonSuffix = "json" |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 44 | ) |
| 45 | |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 46 | var ( |
Kevin Dagostino | 096ab2f | 2023-03-03 19:47:17 +0000 | [diff] [blame] | 47 | rbeRandPrefix int |
| 48 | googleProdCredsExistCache bool |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 49 | ) |
| 50 | |
| 51 | func init() { |
| 52 | rand.Seed(time.Now().UnixNano()) |
| 53 | rbeRandPrefix = rand.Intn(1000) |
| 54 | } |
| 55 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 56 | type Config struct{ *configImpl } |
| 57 | |
| 58 | type configImpl struct { |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 59 | // Some targets that are implemented in soong_build |
| 60 | // (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] | 61 | arguments []string |
| 62 | goma bool |
| 63 | environ *Environment |
| 64 | distDir string |
| 65 | buildDateTime string |
MarkDacek | 6614d9c | 2022-12-07 21:57:38 +0000 | [diff] [blame] | 66 | logsPrefix string |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 67 | |
| 68 | // From the arguments |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 69 | parallel int |
| 70 | keepGoing int |
| 71 | verbose bool |
| 72 | checkbuild bool |
| 73 | dist bool |
| 74 | jsonModuleGraph bool |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 75 | bp2build bool |
| 76 | queryview bool |
| 77 | reportMkMetrics bool // Collect and report mk2bp migration progress metrics. |
| 78 | soongDocs bool |
| 79 | multitreeBuild bool // This is a multitree build. |
| 80 | skipConfig bool |
| 81 | skipKati bool |
| 82 | skipKatiNinja bool |
| 83 | skipSoong bool |
| 84 | skipNinja bool |
| 85 | skipSoongTests bool |
| 86 | searchApiDir bool // Scan the Android.bp files generated in out/api_surfaces |
| 87 | skipMetricsUpload bool |
| 88 | buildStartedTime int64 // For metrics-upload-only - manually specify a build-started time |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 89 | buildFromSourceStub bool |
MarkDacek | 396491e | 2023-06-14 19:41:18 +0000 | [diff] [blame] | 90 | ensureAllowlistIntegrity bool // For CI builds - make sure modules are mixed-built |
| 91 | bazelExitCode int32 // For b runs - necessary for updating NonZeroExit |
| 92 | besId string // For b runs, to identify the BuildEventService logs |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 93 | |
| 94 | // From the product config |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 95 | katiArgs []string |
| 96 | ninjaArgs []string |
| 97 | katiSuffix string |
| 98 | targetDevice string |
| 99 | targetDeviceDir string |
Spandan Das | a3639e6 | 2021-05-25 19:14:02 +0000 | [diff] [blame] | 100 | sandboxConfig *SandboxConfig |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 101 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 102 | // Autodetected |
| 103 | totalRAM uint64 |
| 104 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 105 | brokenDupRules bool |
| 106 | brokenUsesNetwork bool |
| 107 | brokenNinjaEnvVars []string |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 108 | |
| 109 | pathReplaced bool |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 110 | |
MarkDacek | b78465d | 2022-10-18 20:10:16 +0000 | [diff] [blame] | 111 | bazelProdMode bool |
MarkDacek | b78465d | 2022-10-18 20:10:16 +0000 | [diff] [blame] | 112 | bazelStagingMode bool |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 113 | |
Colin Cross | f3bdbcb | 2021-06-01 11:43:55 -0700 | [diff] [blame] | 114 | // Set by multiproduct_kati |
| 115 | emptyNinjaFile bool |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 116 | |
| 117 | metricsUploader string |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 118 | |
| 119 | bazelForceEnabledModules string |
Spandan Das | c576383 | 2022-11-08 18:42:16 +0000 | [diff] [blame] | 120 | |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 121 | includeTags []string |
| 122 | sourceRootDirs []string |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 123 | |
| 124 | // Data source to write ninja weight list |
| 125 | ninjaWeightListSource NinjaWeightListSource |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 128 | type NinjaWeightListSource uint |
| 129 | |
| 130 | const ( |
| 131 | // ninja doesn't use weight list. |
| 132 | NOT_USED NinjaWeightListSource = iota |
| 133 | // ninja uses weight list based on previous builds by ninja log |
| 134 | NINJA_LOG |
| 135 | // ninja thinks every task has the same weight. |
| 136 | EVENLY_DISTRIBUTED |
Jeongik Cha | 518f3ea | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 137 | // ninja uses an external custom weight list |
| 138 | EXTERNAL_FILE |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 139 | // ninja uses a prioritized module list from Soong |
| 140 | HINT_FROM_SOONG |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 141 | // If ninja log exists, use NINJA_LOG, if not, use HINT_FROM_SOONG instead. |
| 142 | // We can assume it is an incremental build if ninja log exists. |
| 143 | DEFAULT |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 144 | ) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 145 | const srcDirFileCheck = "build/soong/root.bp" |
| 146 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 147 | var buildFiles = []string{"Android.mk", "Android.bp"} |
| 148 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 149 | type BuildAction uint |
| 150 | |
| 151 | const ( |
| 152 | // Builds all of the modules and their dependencies of a specified directory, relative to the root |
| 153 | // directory of the source tree. |
| 154 | BUILD_MODULES_IN_A_DIRECTORY BuildAction = iota |
| 155 | |
| 156 | // Builds all of the modules and their dependencies of a list of specified directories. All specified |
| 157 | // directories are relative to the root directory of the source tree. |
| 158 | BUILD_MODULES_IN_DIRECTORIES |
Patrice Arruda | 3928206 | 2019-06-20 16:35:12 -0700 | [diff] [blame] | 159 | |
| 160 | // Build a list of specified modules. If none was specified, simply build the whole source tree. |
| 161 | BUILD_MODULES |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 162 | ) |
| 163 | |
| 164 | // checkTopDir validates that the current directory is at the root directory of the source tree. |
| 165 | func checkTopDir(ctx Context) { |
| 166 | if _, err := os.Stat(srcDirFileCheck); err != nil { |
| 167 | if os.IsNotExist(err) { |
| 168 | ctx.Fatalf("Current working directory must be the source tree. %q not found.", srcDirFileCheck) |
| 169 | } |
| 170 | ctx.Fatalln("Error verifying tree state:", err) |
| 171 | } |
| 172 | } |
| 173 | |
MarkDacek | 7901e58 | 2023-01-09 19:48:01 +0000 | [diff] [blame] | 174 | func loadEnvConfig(ctx Context, config *configImpl, bc string) error { |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 175 | if bc == "" { |
| 176 | return nil |
| 177 | } |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 178 | |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 179 | configDirs := []string{ |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 180 | config.OutDir(), |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 181 | os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG_DIR"), |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 182 | envConfigDir, |
| 183 | } |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 184 | for _, dir := range configDirs { |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 185 | cfgFile := filepath.Join(os.Getenv("TOP"), dir, fmt.Sprintf("%s.%s", bc, jsonSuffix)) |
| 186 | envVarsJSON, err := ioutil.ReadFile(cfgFile) |
| 187 | if err != nil { |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 188 | continue |
| 189 | } |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 190 | ctx.Verbosef("Loading config file %v\n", cfgFile) |
| 191 | var envVars map[string]map[string]string |
| 192 | if err := json.Unmarshal(envVarsJSON, &envVars); err != nil { |
| 193 | fmt.Fprintf(os.Stderr, "Env vars config file %s did not parse correctly: %s", cfgFile, err.Error()) |
| 194 | continue |
| 195 | } |
| 196 | for k, v := range envVars["env"] { |
| 197 | if os.Getenv(k) != "" { |
| 198 | continue |
| 199 | } |
| 200 | config.environ.Set(k, v) |
| 201 | } |
| 202 | ctx.Verbosef("Finished loading config file %v\n", cfgFile) |
| 203 | break |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 204 | } |
Kousik Kumar | 84bd5bf | 2022-01-26 23:32:22 -0500 | [diff] [blame] | 205 | |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 206 | return nil |
| 207 | } |
| 208 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 209 | func NewConfig(ctx Context, args ...string) Config { |
| 210 | ret := &configImpl{ |
Jeongik Cha | f2ecf76 | 2023-05-19 14:03:45 +0900 | [diff] [blame] | 211 | environ: OsEnvironment(), |
| 212 | sandboxConfig: &SandboxConfig{}, |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 213 | ninjaWeightListSource: DEFAULT, |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Colin Cross | 106d6ef | 2023-10-24 10:34:56 -0700 | [diff] [blame] | 216 | // Skip soong tests by default on Linux |
| 217 | if runtime.GOOS == "linux" { |
| 218 | ret.skipSoongTests = true |
| 219 | } |
| 220 | |
Patrice Arruda | 9010917 | 2020-07-28 18:07:27 +0000 | [diff] [blame] | 221 | // Default matching ninja |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 222 | ret.parallel = runtime.NumCPU() + 2 |
| 223 | ret.keepGoing = 1 |
| 224 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 225 | ret.totalRAM = detectTotalRAM(ctx) |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 226 | ret.parseArgs(ctx, args) |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 227 | |
| 228 | if ret.ninjaWeightListSource == HINT_FROM_SOONG { |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 229 | ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "always") |
| 230 | } else if ret.ninjaWeightListSource == DEFAULT { |
| 231 | defaultNinjaWeightListSource := NINJA_LOG |
| 232 | if _, err := os.Stat(filepath.Join(ret.OutDir(), ninjaLogFileName)); errors.Is(err, os.ErrNotExist) { |
| 233 | ctx.Verboseln("$OUT/.ninja_log doesn't exist, use HINT_FROM_SOONG instead") |
| 234 | defaultNinjaWeightListSource = HINT_FROM_SOONG |
| 235 | } else { |
| 236 | ctx.Verboseln("$OUT/.ninja_log exist, use NINJA_LOG") |
| 237 | } |
| 238 | ret.ninjaWeightListSource = defaultNinjaWeightListSource |
| 239 | // soong_build generates ninja hint depending on ninja log existence. |
| 240 | // Set it "depend" to avoid soong re-run due to env variable change. |
| 241 | ret.environ.Set("SOONG_GENERATES_NINJA_HINT", "depend") |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 242 | } |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 243 | |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 244 | // Make sure OUT_DIR is set appropriately |
Dan Willemsen | 02f3add | 2017-05-12 13:50:19 -0700 | [diff] [blame] | 245 | if outDir, ok := ret.environ.Get("OUT_DIR"); ok { |
| 246 | ret.environ.Set("OUT_DIR", filepath.Clean(outDir)) |
| 247 | } else { |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 248 | outDir := "out" |
| 249 | if baseDir, ok := ret.environ.Get("OUT_DIR_COMMON_BASE"); ok { |
| 250 | if wd, err := os.Getwd(); err != nil { |
| 251 | ctx.Fatalln("Failed to get working directory:", err) |
| 252 | } else { |
| 253 | outDir = filepath.Join(baseDir, filepath.Base(wd)) |
| 254 | } |
| 255 | } |
| 256 | ret.environ.Set("OUT_DIR", outDir) |
| 257 | } |
| 258 | |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 259 | // loadEnvConfig needs to know what the OUT_DIR is, so it should |
| 260 | // be called after we determine the appropriate out directory. |
MarkDacek | 7901e58 | 2023-01-09 19:48:01 +0000 | [diff] [blame] | 261 | bc := os.Getenv("ANDROID_BUILD_ENVIRONMENT_CONFIG") |
| 262 | |
| 263 | if bc != "" { |
Kousik Kumar | c881833 | 2023-01-16 16:33:05 +0000 | [diff] [blame] | 264 | if err := loadEnvConfig(ctx, ret, bc); err != nil { |
MarkDacek | 7901e58 | 2023-01-09 19:48:01 +0000 | [diff] [blame] | 265 | ctx.Fatalln("Failed to parse env config files: %v", err) |
| 266 | } |
Kousik Kumar | 6d1e348 | 2023-07-24 03:44:16 +0000 | [diff] [blame] | 267 | if !ret.canSupportRBE() { |
| 268 | // Explicitly set USE_RBE env variable to false when we cannot run |
| 269 | // an RBE build to avoid ninja local execution pool issues. |
| 270 | ret.environ.Set("USE_RBE", "false") |
| 271 | } |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 272 | } |
| 273 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 274 | if distDir, ok := ret.environ.Get("DIST_DIR"); ok { |
| 275 | ret.distDir = filepath.Clean(distDir) |
| 276 | } else { |
| 277 | ret.distDir = filepath.Join(ret.OutDir(), "dist") |
| 278 | } |
Dan Willemsen | d50e89f | 2018-10-16 17:49:25 -0700 | [diff] [blame] | 279 | |
Spandan Das | 0506361 | 2021-06-25 01:39:04 +0000 | [diff] [blame] | 280 | if srcDirIsWritable, ok := ret.environ.Get("BUILD_BROKEN_SRC_DIR_IS_WRITABLE"); ok { |
| 281 | ret.sandboxConfig.SetSrcDirIsRO(srcDirIsWritable == "false") |
| 282 | } |
| 283 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 284 | ret.environ.Unset( |
| 285 | // We're already using it |
| 286 | "USE_SOONG_UI", |
| 287 | |
| 288 | // We should never use GOROOT/GOPATH from the shell environment |
| 289 | "GOROOT", |
| 290 | "GOPATH", |
| 291 | |
| 292 | // These should only come from Soong, not the environment. |
| 293 | "CLANG", |
| 294 | "CLANG_CXX", |
| 295 | "CCC_CC", |
| 296 | "CCC_CXX", |
| 297 | |
| 298 | // Used by the goma compiler wrapper, but should only be set by |
| 299 | // gomacc |
| 300 | "GOMACC_PATH", |
Dan Willemsen | 0c3919e | 2017-03-02 15:49:10 -0800 | [diff] [blame] | 301 | |
| 302 | // We handle this above |
| 303 | "OUT_DIR_COMMON_BASE", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 304 | |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 305 | // This is handled above too, and set for individual commands later |
| 306 | "DIST_DIR", |
| 307 | |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 308 | // Variables that have caused problems in the past |
Dan Willemsen | 1c504d9 | 2019-11-18 19:13:53 +0000 | [diff] [blame] | 309 | "BASH_ENV", |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 310 | "CDPATH", |
Dan Willemsen | 68a0985 | 2017-04-18 13:56:57 -0700 | [diff] [blame] | 311 | "DISPLAY", |
| 312 | "GREP_OPTIONS", |
Nathan Egge | 7b067fb | 2023-02-17 17:54:31 +0000 | [diff] [blame] | 313 | "JAVAC", |
Dan Willemsen | ebfe33a | 2018-05-01 10:07:50 -0700 | [diff] [blame] | 314 | "NDK_ROOT", |
Dan Willemsen | 00fcb26 | 2018-08-15 15:35:38 -0700 | [diff] [blame] | 315 | "POSIXLY_CORRECT", |
Dan Willemsen | c40e10b | 2017-07-11 14:30:00 -0700 | [diff] [blame] | 316 | |
| 317 | // Drop make flags |
| 318 | "MAKEFLAGS", |
| 319 | "MAKELEVEL", |
| 320 | "MFLAGS", |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 321 | |
| 322 | // Set in envsetup.sh, reset in makefiles |
| 323 | "ANDROID_JAVA_TOOLCHAIN", |
Colin Cross | 7f09c40 | 2018-07-11 14:49:31 -0700 | [diff] [blame] | 324 | |
| 325 | // Set by envsetup.sh, but shouldn't be used inside the build because envsetup.sh is optional |
| 326 | "ANDROID_BUILD_TOP", |
| 327 | "ANDROID_HOST_OUT", |
| 328 | "ANDROID_PRODUCT_OUT", |
| 329 | "ANDROID_HOST_OUT_TESTCASES", |
| 330 | "ANDROID_TARGET_OUT_TESTCASES", |
| 331 | "ANDROID_TOOLCHAIN", |
| 332 | "ANDROID_TOOLCHAIN_2ND_ARCH", |
| 333 | "ANDROID_DEV_SCRIPTS", |
| 334 | "ANDROID_EMULATOR_PREBUILTS", |
| 335 | "ANDROID_PRE_BUILD_PATHS", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 336 | ) |
| 337 | |
Kousik Kumar | b328f6d | 2020-10-19 01:45:46 -0400 | [diff] [blame] | 338 | if ret.UseGoma() || ret.ForceUseGoma() { |
| 339 | ctx.Println("Goma for Android has been deprecated and replaced with RBE. See go/rbe_for_android for instructions on how to use RBE.") |
| 340 | ctx.Fatalln("USE_GOMA / FORCE_USE_GOMA flag is no longer supported.") |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 341 | } |
| 342 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 343 | // Tell python not to spam the source tree with .pyc files. |
| 344 | ret.environ.Set("PYTHONDONTWRITEBYTECODE", "1") |
| 345 | |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 346 | tmpDir := absPath(ctx, ret.TempDir()) |
| 347 | ret.environ.Set("TMPDIR", tmpDir) |
Dan Willemsen | 32a669b | 2018-03-08 19:42:00 -0800 | [diff] [blame] | 348 | |
Dan Willemsen | 70c1ff8 | 2019-08-21 14:56:13 -0700 | [diff] [blame] | 349 | // Always set ASAN_SYMBOLIZER_PATH so that ASAN-based tools can symbolize any crashes |
| 350 | symbolizerPath := filepath.Join("prebuilts/clang/host", ret.HostPrebuiltTag(), |
| 351 | "llvm-binutils-stable/llvm-symbolizer") |
| 352 | ret.environ.Set("ASAN_SYMBOLIZER_PATH", absPath(ctx, symbolizerPath)) |
| 353 | |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 354 | // Precondition: the current directory is the top of the source tree |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 355 | checkTopDir(ctx) |
Dan Willemsen | c2af0be | 2017-01-20 14:10:01 -0800 | [diff] [blame] | 356 | |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 357 | srcDir := absPath(ctx, ".") |
| 358 | if strings.ContainsRune(srcDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 359 | ctx.Println("You are building in a directory whose absolute path contains a space character:") |
| 360 | ctx.Println() |
| 361 | ctx.Printf("%q\n", srcDir) |
| 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 | |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 366 | ret.metricsUploader = GetMetricsUploader(srcDir, ret.environ) |
| 367 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 368 | if outDir := ret.OutDir(); strings.ContainsRune(outDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 369 | ctx.Println("The absolute path of your output directory ($OUT_DIR) contains a space character:") |
| 370 | ctx.Println() |
| 371 | ctx.Printf("%q\n", outDir) |
| 372 | ctx.Println() |
| 373 | ctx.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 376 | if distDir := ret.RealDistDir(); strings.ContainsRune(distDir, ' ') { |
Colin Cross | 1f6faeb | 2019-09-23 15:52:40 -0700 | [diff] [blame] | 377 | ctx.Println("The absolute path of your dist directory ($DIST_DIR) contains a space character:") |
| 378 | ctx.Println() |
| 379 | ctx.Printf("%q\n", distDir) |
| 380 | ctx.Println() |
| 381 | ctx.Fatalln("Directory names containing spaces are not supported") |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 384 | // Configure Java-related variables, including adding it to $PATH |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 385 | java8Home := filepath.Join("prebuilts/jdk/jdk8", ret.HostPrebuiltTag()) |
Colin Cross | 59c1e6a | 2022-03-04 13:37:19 -0800 | [diff] [blame] | 386 | java17Home := filepath.Join("prebuilts/jdk/jdk17", ret.HostPrebuiltTag()) |
Sorin Basca | 0760c89 | 2023-11-29 19:13:55 +0000 | [diff] [blame^] | 387 | java21Home := filepath.Join("prebuilts/jdk/jdk21", ret.HostPrebuiltTag()) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 388 | javaHome := func() string { |
| 389 | if override, ok := ret.environ.Get("OVERRIDE_ANDROID_JAVA_HOME"); ok { |
| 390 | return override |
| 391 | } |
Sorin Basca | 0760c89 | 2023-11-29 19:13:55 +0000 | [diff] [blame^] | 392 | if ret.environ.IsEnvTrue("EXPERIMENTAL_USE_OPENJDK21_TOOLCHAIN") { |
| 393 | return java21Home |
| 394 | } |
Pete Gillin | a7a3d64 | 2019-11-07 18:58:42 +0000 | [diff] [blame] | 395 | if toolchain11, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK11_TOOLCHAIN"); ok && toolchain11 != "true" { |
| 396 | 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] | 397 | } |
Sorin Basca | 7e094b3 | 2022-10-05 08:20:12 +0000 | [diff] [blame] | 398 | if toolchain17, ok := ret.environ.Get("EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN"); ok && toolchain17 != "true" { |
| 399 | ctx.Fatalln("The environment variable EXPERIMENTAL_USE_OPENJDK17_TOOLCHAIN is no longer supported. An OpenJDK 17 toolchain is now the global default.") |
| 400 | } |
| 401 | return java17Home |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 402 | }() |
| 403 | absJavaHome := absPath(ctx, javaHome) |
| 404 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 405 | ret.configureLocale(ctx) |
| 406 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 407 | newPath := []string{filepath.Join(absJavaHome, "bin")} |
| 408 | if path, ok := ret.environ.Get("PATH"); ok && path != "" { |
| 409 | newPath = append(newPath, path) |
| 410 | } |
Pete Gillin | 1f52e93 | 2019-10-09 17:10:08 +0100 | [diff] [blame] | 411 | |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 412 | ret.environ.Unset("OVERRIDE_ANDROID_JAVA_HOME") |
| 413 | ret.environ.Set("JAVA_HOME", absJavaHome) |
| 414 | ret.environ.Set("ANDROID_JAVA_HOME", javaHome) |
Tobias Thierer | e59aeff | 2017-12-20 22:40:39 +0000 | [diff] [blame] | 415 | ret.environ.Set("ANDROID_JAVA8_HOME", java8Home) |
Dan Willemsen | d9e8f0a | 2017-10-30 13:42:06 -0700 | [diff] [blame] | 416 | ret.environ.Set("PATH", strings.Join(newPath, string(filepath.ListSeparator))) |
| 417 | |
Colin Cross | fe5ed4d | 2023-07-28 09:27:23 -0700 | [diff] [blame] | 418 | // b/286885495, https://bugzilla.redhat.com/show_bug.cgi?id=2227130: some versions of Fedora include patches |
| 419 | // to unzip to enable zipbomb detection that incorrectly handle zip64 and data descriptors and fail on large |
| 420 | // zip files produced by soong_zip. Disable zipbomb detection. |
| 421 | ret.environ.Set("UNZIP_DISABLE_ZIPBOMB_DETECTION", "TRUE") |
| 422 | |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 423 | if ret.MultitreeBuild() { |
| 424 | ret.environ.Set("MULTITREE_BUILD", "true") |
| 425 | } |
| 426 | |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 427 | outDir := ret.OutDir() |
| 428 | buildDateTimeFile := filepath.Join(outDir, "build_date.txt") |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 429 | if buildDateTime, ok := ret.environ.Get("BUILD_DATETIME"); ok && buildDateTime != "" { |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 430 | ret.buildDateTime = buildDateTime |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 431 | } else { |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 432 | ret.buildDateTime = strconv.FormatInt(time.Now().Unix(), 10) |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 433 | } |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 434 | |
Nan Zhang | 2e6a4ff | 2018-02-14 13:27:26 -0800 | [diff] [blame] | 435 | ret.environ.Set("BUILD_DATETIME_FILE", buildDateTimeFile) |
| 436 | |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 437 | if _, ok := ret.environ.Get("BUILD_USERNAME"); !ok { |
| 438 | username := "unknown" |
| 439 | if u, err := user.Current(); err == nil { |
| 440 | username = u.Username |
| 441 | } else { |
| 442 | ctx.Println("Failed to get current user:", err) |
| 443 | } |
| 444 | ret.environ.Set("BUILD_USERNAME", username) |
| 445 | } |
| 446 | |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 447 | if ret.UseRBE() { |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 448 | for k, v := range getRBEVars(ctx, Config{ret}) { |
Ramy Medhat | ca1e44c | 2020-07-16 12:18:37 -0400 | [diff] [blame] | 449 | ret.environ.Set(k, v) |
| 450 | } |
| 451 | } |
| 452 | |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 453 | bpd := ret.BazelMetricsDir() |
Patrice Arruda | af880da | 2020-11-13 08:41:26 -0800 | [diff] [blame] | 454 | if err := os.RemoveAll(bpd); err != nil { |
| 455 | ctx.Fatalf("Unable to remove bazel profile directory %q: %v", bpd, err) |
| 456 | } |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 457 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 458 | c := Config{ret} |
| 459 | storeConfigMetrics(ctx, c) |
| 460 | return c |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 461 | } |
| 462 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 463 | // NewBuildActionConfig returns a build configuration based on the build action. The arguments are |
| 464 | // 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] | 465 | func NewBuildActionConfig(action BuildAction, dir string, ctx Context, args ...string) Config { |
| 466 | return NewConfig(ctx, getConfigArgs(action, dir, ctx, args)...) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 467 | } |
| 468 | |
LaMont Jones | 9a91286 | 2023-11-06 22:11:08 +0000 | [diff] [blame] | 469 | // Prepare for getting make variables. For them to be accurate, we need to have |
| 470 | // obtained PRODUCT_RELEASE_CONFIG_MAPS. |
| 471 | // |
| 472 | // Returns: |
| 473 | // |
| 474 | // Whether config should be called again. |
| 475 | // |
| 476 | // TODO: when converting product config to a declarative language, make sure |
| 477 | // that PRODUCT_RELEASE_CONFIG_MAPS is properly handled as a separate step in |
| 478 | // that process. |
| 479 | func SetProductReleaseConfigMaps(ctx Context, config Config) bool { |
| 480 | ctx.BeginTrace(metrics.RunKati, "SetProductReleaseConfigMaps") |
| 481 | defer ctx.EndTrace() |
| 482 | |
| 483 | if config.SkipConfig() { |
| 484 | // This duplicates the logic from Build to skip product config |
| 485 | // if the user has explicitly said to. |
| 486 | return false |
| 487 | } |
| 488 | |
| 489 | releaseConfigVars := []string{ |
| 490 | "PRODUCT_RELEASE_CONFIG_MAPS", |
| 491 | } |
| 492 | |
| 493 | origValue, _ := config.environ.Get("PRODUCT_RELEASE_CONFIG_MAPS") |
| 494 | // Get the PRODUCT_RELEASE_CONFIG_MAPS for this product, to avoid polluting the environment |
| 495 | // when we run product config to get the rest of the make vars. |
| 496 | releaseMapVars, err := dumpMakeVars(ctx, config, nil, releaseConfigVars, false, "") |
| 497 | if err != nil { |
| 498 | ctx.Fatalln("Error getting PRODUCT_RELEASE_CONFIG_MAPS:", err) |
| 499 | } |
| 500 | productReleaseConfigMaps := releaseMapVars["PRODUCT_RELEASE_CONFIG_MAPS"] |
| 501 | os.Setenv("PRODUCT_RELEASE_CONFIG_MAPS", productReleaseConfigMaps) |
| 502 | return origValue != productReleaseConfigMaps |
| 503 | } |
| 504 | |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 505 | // storeConfigMetrics selects a set of configuration information and store in |
| 506 | // the metrics system for further analysis. |
| 507 | func storeConfigMetrics(ctx Context, config Config) { |
| 508 | if ctx.Metrics == nil { |
| 509 | return |
| 510 | } |
| 511 | |
Liz Kammer | ca9cb2e | 2021-07-14 15:29:57 -0400 | [diff] [blame] | 512 | ctx.Metrics.BuildConfig(buildConfig(config)) |
Patrice Arruda | 3edfd48 | 2020-10-13 23:58:41 +0000 | [diff] [blame] | 513 | |
| 514 | s := &smpb.SystemResourceInfo{ |
| 515 | TotalPhysicalMemory: proto.Uint64(config.TotalRAM()), |
| 516 | AvailableCpus: proto.Int32(int32(runtime.NumCPU())), |
| 517 | } |
| 518 | ctx.Metrics.SystemResourceInfo(s) |
Patrice Arruda | 9685036 | 2020-08-11 20:41:11 +0000 | [diff] [blame] | 519 | } |
| 520 | |
Jeongik Cha | 8d63d56 | 2023-03-17 03:52:13 +0900 | [diff] [blame] | 521 | func getNinjaWeightListSourceInMetric(s NinjaWeightListSource) *smpb.BuildConfig_NinjaWeightListSource { |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 522 | switch s { |
| 523 | case NINJA_LOG: |
Jeongik Cha | 8d63d56 | 2023-03-17 03:52:13 +0900 | [diff] [blame] | 524 | return smpb.BuildConfig_NINJA_LOG.Enum() |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 525 | case EVENLY_DISTRIBUTED: |
Jeongik Cha | 8d63d56 | 2023-03-17 03:52:13 +0900 | [diff] [blame] | 526 | return smpb.BuildConfig_EVENLY_DISTRIBUTED.Enum() |
Jeongik Cha | 518f3ea | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 527 | case EXTERNAL_FILE: |
| 528 | return smpb.BuildConfig_EXTERNAL_FILE.Enum() |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 529 | case HINT_FROM_SOONG: |
| 530 | return smpb.BuildConfig_HINT_FROM_SOONG.Enum() |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 531 | default: |
Jeongik Cha | 8d63d56 | 2023-03-17 03:52:13 +0900 | [diff] [blame] | 532 | return smpb.BuildConfig_NOT_USED.Enum() |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Liz Kammer | ca9cb2e | 2021-07-14 15:29:57 -0400 | [diff] [blame] | 536 | func buildConfig(config Config) *smpb.BuildConfig { |
Yu Liu | e737a99 | 2021-10-04 13:21:41 -0700 | [diff] [blame] | 537 | c := &smpb.BuildConfig{ |
Romain Jobredeaux | 0a7529b | 2022-10-26 12:56:41 -0400 | [diff] [blame] | 538 | ForceUseGoma: proto.Bool(config.ForceUseGoma()), |
| 539 | UseGoma: proto.Bool(config.UseGoma()), |
| 540 | UseRbe: proto.Bool(config.UseRBE()), |
| 541 | BazelMixedBuild: proto.Bool(config.BazelBuildEnabled()), |
| 542 | ForceDisableBazelMixedBuild: proto.Bool(config.IsBazelMixedBuildForceDisabled()), |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 543 | NinjaWeightListSource: getNinjaWeightListSourceInMetric(config.NinjaWeightListSource()), |
Liz Kammer | ca9cb2e | 2021-07-14 15:29:57 -0400 | [diff] [blame] | 544 | } |
Yu Liu | e737a99 | 2021-10-04 13:21:41 -0700 | [diff] [blame] | 545 | c.Targets = append(c.Targets, config.arguments...) |
| 546 | |
| 547 | return c |
Liz Kammer | ca9cb2e | 2021-07-14 15:29:57 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 550 | // getConfigArgs processes the command arguments based on the build action and creates a set of new |
| 551 | // arguments to be accepted by Config. |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 552 | func getConfigArgs(action BuildAction, dir string, ctx Context, args []string) []string { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 553 | // The next block of code verifies that the current directory is the root directory of the source |
| 554 | // tree. It then finds the relative path of dir based on the root directory of the source tree |
| 555 | // and verify that dir is inside of the source tree. |
| 556 | checkTopDir(ctx) |
| 557 | topDir, err := os.Getwd() |
| 558 | if err != nil { |
| 559 | ctx.Fatalf("Error retrieving top directory: %v", err) |
| 560 | } |
Patrice Arruda | baba9a9 | 2019-07-03 10:47:34 -0700 | [diff] [blame] | 561 | dir, err = filepath.EvalSymlinks(dir) |
| 562 | if err != nil { |
| 563 | ctx.Fatalf("Unable to evaluate symlink of %s: %v", dir, err) |
| 564 | } |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 565 | dir, err = filepath.Abs(dir) |
| 566 | if err != nil { |
| 567 | ctx.Fatalf("Unable to find absolute path %s: %v", dir, err) |
| 568 | } |
| 569 | relDir, err := filepath.Rel(topDir, dir) |
| 570 | if err != nil { |
| 571 | ctx.Fatalf("Unable to find relative path %s of %s: %v", relDir, topDir, err) |
| 572 | } |
| 573 | // If there are ".." in the path, it's not in the source tree. |
| 574 | if strings.Contains(relDir, "..") { |
| 575 | ctx.Fatalf("Directory %s is not under the source tree %s", dir, topDir) |
| 576 | } |
| 577 | |
| 578 | configArgs := args[:] |
| 579 | |
| 580 | // If the arguments contains GET-INSTALL-PATH, change the target name prefix from MODULES-IN- to |
| 581 | // GET-INSTALL-PATH-IN- to extract the installation path instead of building the modules. |
| 582 | targetNamePrefix := "MODULES-IN-" |
| 583 | if inList("GET-INSTALL-PATH", configArgs) { |
| 584 | targetNamePrefix = "GET-INSTALL-PATH-IN-" |
| 585 | configArgs = removeFromList("GET-INSTALL-PATH", configArgs) |
| 586 | } |
| 587 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 588 | var targets []string |
| 589 | |
| 590 | switch action { |
Patrice Arruda | 3928206 | 2019-06-20 16:35:12 -0700 | [diff] [blame] | 591 | case BUILD_MODULES: |
| 592 | // 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] | 593 | case BUILD_MODULES_IN_A_DIRECTORY: |
| 594 | // If dir is the root source tree, all the modules are built of the source tree are built so |
| 595 | // no need to find the build file. |
| 596 | if topDir == dir { |
| 597 | break |
| 598 | } |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 599 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 600 | buildFile := findBuildFile(ctx, relDir) |
| 601 | if buildFile == "" { |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 602 | ctx.Fatalf("Build file not found for %s directory", relDir) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 603 | } |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 604 | targets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)} |
| 605 | case BUILD_MODULES_IN_DIRECTORIES: |
| 606 | newConfigArgs, dirs := splitArgs(configArgs) |
| 607 | configArgs = newConfigArgs |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 608 | targets = getTargetsFromDirs(ctx, relDir, dirs, targetNamePrefix) |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | // Tidy only override all other specified targets. |
| 612 | tidyOnly := os.Getenv("WITH_TIDY_ONLY") |
| 613 | if tidyOnly == "true" || tidyOnly == "1" { |
| 614 | configArgs = append(configArgs, "tidy_only") |
| 615 | } else { |
| 616 | configArgs = append(configArgs, targets...) |
| 617 | } |
| 618 | |
| 619 | return configArgs |
| 620 | } |
| 621 | |
| 622 | // convertToTarget replaces "/" to "-" in dir and pre-append the targetNamePrefix to the target name. |
| 623 | func convertToTarget(dir string, targetNamePrefix string) string { |
| 624 | return targetNamePrefix + strings.ReplaceAll(dir, "/", "-") |
| 625 | } |
| 626 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 627 | // hasBuildFile returns true if dir contains an Android build file. |
| 628 | func hasBuildFile(ctx Context, dir string) bool { |
| 629 | for _, buildFile := range buildFiles { |
| 630 | _, err := os.Stat(filepath.Join(dir, buildFile)) |
| 631 | if err == nil { |
| 632 | return true |
| 633 | } |
| 634 | if !os.IsNotExist(err) { |
| 635 | ctx.Fatalf("Error retrieving the build file stats: %v", err) |
| 636 | } |
| 637 | } |
| 638 | return false |
| 639 | } |
| 640 | |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 641 | // findBuildFile finds a build file (makefile or blueprint file) by looking if there is a build file |
| 642 | // in the current and any sub directory of dir. If a build file is not found, traverse the path |
| 643 | // up by one directory and repeat again until either a build file is found or reached to the root |
| 644 | // source tree. The returned filename of build file is "Android.mk". If one was not found, a blank |
| 645 | // string is returned. |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 646 | func findBuildFile(ctx Context, dir string) string { |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 647 | // If the string is empty or ".", assume it is top directory of the source tree. |
| 648 | if dir == "" || dir == "." { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 649 | return "" |
| 650 | } |
| 651 | |
Patrice Arruda | 0dcf27f | 2019-07-08 17:03:33 -0700 | [diff] [blame] | 652 | found := false |
| 653 | for buildDir := dir; buildDir != "."; buildDir = filepath.Dir(buildDir) { |
| 654 | err := filepath.Walk(buildDir, func(path string, info os.FileInfo, err error) error { |
| 655 | if err != nil { |
| 656 | return err |
| 657 | } |
| 658 | if found { |
| 659 | return filepath.SkipDir |
| 660 | } |
| 661 | if info.IsDir() { |
| 662 | return nil |
| 663 | } |
| 664 | for _, buildFile := range buildFiles { |
| 665 | if info.Name() == buildFile { |
| 666 | found = true |
| 667 | return filepath.SkipDir |
| 668 | } |
| 669 | } |
| 670 | return nil |
| 671 | }) |
| 672 | if err != nil { |
| 673 | ctx.Fatalf("Error finding Android build file: %v", err) |
| 674 | } |
| 675 | |
| 676 | if found { |
| 677 | return filepath.Join(buildDir, "Android.mk") |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 678 | } |
| 679 | } |
| 680 | |
| 681 | return "" |
| 682 | } |
| 683 | |
| 684 | // splitArgs iterates over the arguments list and splits into two lists: arguments and directories. |
| 685 | func splitArgs(args []string) (newArgs []string, dirs []string) { |
| 686 | specialArgs := map[string]bool{ |
| 687 | "showcommands": true, |
| 688 | "snod": true, |
| 689 | "dist": true, |
| 690 | "checkbuild": true, |
| 691 | } |
| 692 | |
| 693 | newArgs = []string{} |
| 694 | dirs = []string{} |
| 695 | |
| 696 | for _, arg := range args { |
| 697 | // It's a dash argument if it starts with "-" or it's a key=value pair, it's not a directory. |
| 698 | if strings.IndexRune(arg, '-') == 0 || strings.IndexRune(arg, '=') != -1 { |
| 699 | newArgs = append(newArgs, arg) |
| 700 | continue |
| 701 | } |
| 702 | |
| 703 | if _, ok := specialArgs[arg]; ok { |
| 704 | newArgs = append(newArgs, arg) |
| 705 | continue |
| 706 | } |
| 707 | |
| 708 | dirs = append(dirs, arg) |
| 709 | } |
| 710 | |
| 711 | return newArgs, dirs |
| 712 | } |
| 713 | |
| 714 | // getTargetsFromDirs iterates over the dirs list and creates a list of targets to build. If a |
| 715 | // directory from the dirs list does not exist, a fatal error is raised. relDir is related to the |
| 716 | // source root tree where the build action command was invoked. Each directory is validated if the |
| 717 | // 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] | 718 | func getTargetsFromDirs(ctx Context, relDir string, dirs []string, targetNamePrefix string) (targets []string) { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 719 | for _, dir := range dirs { |
| 720 | // The directory may have specified specific modules to build. ":" is the separator to separate |
| 721 | // the directory and the list of modules. |
| 722 | s := strings.Split(dir, ":") |
| 723 | l := len(s) |
| 724 | if l > 2 { // more than one ":" was specified. |
| 725 | ctx.Fatalf("%s not in proper directory:target1,target2,... format (\":\" was specified more than once)", dir) |
| 726 | } |
| 727 | |
| 728 | dir = filepath.Join(relDir, s[0]) |
| 729 | if _, err := os.Stat(dir); err != nil { |
| 730 | ctx.Fatalf("couldn't find directory %s", dir) |
| 731 | } |
| 732 | |
| 733 | // Verify that if there are any targets specified after ":". Each target is separated by ",". |
| 734 | var newTargets []string |
| 735 | if l == 2 && s[1] != "" { |
| 736 | newTargets = strings.Split(s[1], ",") |
| 737 | if inList("", newTargets) { |
| 738 | ctx.Fatalf("%s not in proper directory:target1,target2,... format", dir) |
| 739 | } |
| 740 | } |
| 741 | |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 742 | // If there are specified targets to build in dir, an android build file must exist for the one |
| 743 | // shot build. For the non-targets case, find the appropriate build file and build all the |
| 744 | // modules in dir (or the closest one in the dir path). |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 745 | if len(newTargets) > 0 { |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 746 | if !hasBuildFile(ctx, dir) { |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 747 | ctx.Fatalf("Couldn't locate a build file from %s directory", dir) |
| 748 | } |
| 749 | } else { |
Patrice Arruda | 9450d0b | 2019-07-08 11:06:46 -0700 | [diff] [blame] | 750 | buildFile := findBuildFile(ctx, dir) |
| 751 | if buildFile == "" { |
| 752 | ctx.Fatalf("Build file not found for %s directory", dir) |
| 753 | } |
| 754 | newTargets = []string{convertToTarget(filepath.Dir(buildFile), targetNamePrefix)} |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 755 | } |
| 756 | |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 757 | targets = append(targets, newTargets...) |
| 758 | } |
| 759 | |
Dan Willemsen | ce41e94 | 2019-07-29 23:39:30 -0700 | [diff] [blame] | 760 | return targets |
Patrice Arruda | 1384822 | 2019-04-22 17:12:02 -0700 | [diff] [blame] | 761 | } |
| 762 | |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 763 | func (c *configImpl) parseArgs(ctx Context, args []string) { |
| 764 | for i := 0; i < len(args); i++ { |
| 765 | arg := strings.TrimSpace(args[i]) |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 766 | if arg == "showcommands" { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 767 | c.verbose = true |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 768 | } else if arg == "--empty-ninja-file" { |
| 769 | c.emptyNinjaFile = true |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 770 | } else if arg == "--skip-ninja" { |
| 771 | c.skipNinja = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 772 | } else if arg == "--skip-make" { |
Colin Cross | 30e444b | 2021-06-18 11:26:19 -0700 | [diff] [blame] | 773 | // TODO(ccross): deprecate this, it has confusing behaviors. It doesn't run kati, |
| 774 | // but it does run a Kati ninja file if the .kati_enabled marker file was created |
| 775 | // by a previous build. |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 776 | c.skipConfig = true |
| 777 | c.skipKati = true |
Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 778 | } else if arg == "--soong-only" { |
| 779 | c.skipKati = true |
| 780 | c.skipKatiNinja = true |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 781 | } else if arg == "--config-only" { |
| 782 | c.skipKati = true |
| 783 | c.skipKatiNinja = true |
| 784 | c.skipSoong = true |
Colin Cross | 30e444b | 2021-06-18 11:26:19 -0700 | [diff] [blame] | 785 | } else if arg == "--skip-config" { |
| 786 | c.skipConfig = true |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 787 | } else if arg == "--skip-soong-tests" { |
| 788 | c.skipSoongTests = true |
Colin Cross | 106d6ef | 2023-10-24 10:34:56 -0700 | [diff] [blame] | 789 | } else if arg == "--no-skip-soong-tests" { |
| 790 | c.skipSoongTests = false |
MarkDacek | d0e7cd3 | 2022-12-02 22:22:40 +0000 | [diff] [blame] | 791 | } else if arg == "--skip-metrics-upload" { |
| 792 | c.skipMetricsUpload = true |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame] | 793 | } else if arg == "--mk-metrics" { |
| 794 | c.reportMkMetrics = true |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 795 | } else if arg == "--multitree-build" { |
| 796 | c.multitreeBuild = true |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 797 | } else if arg == "--bazel-mode" { |
| 798 | c.bazelProdMode = true |
MarkDacek | b78465d | 2022-10-18 20:10:16 +0000 | [diff] [blame] | 799 | } else if arg == "--bazel-mode-staging" { |
| 800 | c.bazelStagingMode = true |
Spandan Das | 394aa32 | 2022-11-03 17:02:10 +0000 | [diff] [blame] | 801 | } else if arg == "--search-api-dir" { |
| 802 | c.searchApiDir = true |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 803 | } else if strings.HasPrefix(arg, "--ninja_weight_source=") { |
| 804 | source := strings.TrimPrefix(arg, "--ninja_weight_source=") |
| 805 | if source == "ninja_log" { |
| 806 | c.ninjaWeightListSource = NINJA_LOG |
| 807 | } else if source == "evenly_distributed" { |
| 808 | c.ninjaWeightListSource = EVENLY_DISTRIBUTED |
| 809 | } else if source == "not_used" { |
| 810 | c.ninjaWeightListSource = NOT_USED |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 811 | } else if source == "soong" { |
| 812 | c.ninjaWeightListSource = HINT_FROM_SOONG |
Jeongik Cha | 518f3ea | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 813 | } else if strings.HasPrefix(source, "file,") { |
| 814 | c.ninjaWeightListSource = EXTERNAL_FILE |
| 815 | filePath := strings.TrimPrefix(source, "file,") |
| 816 | err := validateNinjaWeightList(filePath) |
| 817 | if err != nil { |
| 818 | ctx.Fatalf("Malformed weight list from %s: %s", filePath, err) |
| 819 | } |
| 820 | _, err = copyFile(filePath, filepath.Join(c.OutDir(), ".ninja_weight_list")) |
| 821 | if err != nil { |
| 822 | ctx.Fatalf("Error to copy ninja weight list from %s: %s", filePath, err) |
| 823 | } |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 824 | } else { |
| 825 | ctx.Fatalf("unknown option for ninja_weight_source: %s", source) |
| 826 | } |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 827 | } else if arg == "--build-from-source-stub" { |
| 828 | c.buildFromSourceStub = true |
MarkDacek | b96561e | 2022-12-02 04:34:43 +0000 | [diff] [blame] | 829 | } else if strings.HasPrefix(arg, "--build-command=") { |
| 830 | buildCmd := strings.TrimPrefix(arg, "--build-command=") |
| 831 | // remove quotations |
| 832 | buildCmd = strings.TrimPrefix(buildCmd, "\"") |
| 833 | buildCmd = strings.TrimSuffix(buildCmd, "\"") |
| 834 | ctx.Metrics.SetBuildCommand([]string{buildCmd}) |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 835 | } else if strings.HasPrefix(arg, "--bazel-force-enabled-modules=") { |
| 836 | c.bazelForceEnabledModules = strings.TrimPrefix(arg, "--bazel-force-enabled-modules=") |
MarkDacek | 6614d9c | 2022-12-07 21:57:38 +0000 | [diff] [blame] | 837 | } else if strings.HasPrefix(arg, "--build-started-time-unix-millis=") { |
| 838 | buildTimeStr := strings.TrimPrefix(arg, "--build-started-time-unix-millis=") |
| 839 | val, err := strconv.ParseInt(buildTimeStr, 10, 64) |
| 840 | if err == nil { |
| 841 | c.buildStartedTime = val |
| 842 | } else { |
| 843 | ctx.Fatalf("Error parsing build-time-started-unix-millis", err) |
| 844 | } |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 845 | } else if arg == "--ensure-allowlist-integrity" { |
| 846 | c.ensureAllowlistIntegrity = true |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 847 | } else if len(arg) > 0 && arg[0] == '-' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 848 | parseArgNum := func(def int) int { |
| 849 | if len(arg) > 2 { |
| 850 | p, err := strconv.ParseUint(arg[2:], 10, 31) |
| 851 | if err != nil { |
| 852 | ctx.Fatalf("Failed to parse %q: %v", arg, err) |
| 853 | } |
| 854 | return int(p) |
| 855 | } else if i+1 < len(args) { |
| 856 | p, err := strconv.ParseUint(args[i+1], 10, 31) |
| 857 | if err == nil { |
| 858 | i++ |
| 859 | return int(p) |
| 860 | } |
| 861 | } |
| 862 | return def |
| 863 | } |
| 864 | |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 865 | if len(arg) > 1 && arg[1] == 'j' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 866 | c.parallel = parseArgNum(c.parallel) |
Dan Willemsen | 6ac63ef | 2017-10-17 20:35:34 -0700 | [diff] [blame] | 867 | } else if len(arg) > 1 && arg[1] == 'k' { |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 868 | c.keepGoing = parseArgNum(0) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 869 | } else { |
| 870 | ctx.Fatalln("Unknown option:", arg) |
| 871 | } |
Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 872 | } else if k, v, ok := decodeKeyValue(arg); ok && len(k) > 0 { |
Dan Willemsen | 6dfe30a | 2018-09-10 12:41:10 -0700 | [diff] [blame] | 873 | if k == "OUT_DIR" { |
| 874 | ctx.Fatalln("OUT_DIR may only be set in the environment, not as a command line option.") |
| 875 | } |
Dan Willemsen | 091525e | 2017-07-11 14:17:50 -0700 | [diff] [blame] | 876 | c.environ.Set(k, v) |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 877 | } else if arg == "dist" { |
| 878 | c.dist = true |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 879 | } else if arg == "json-module-graph" { |
| 880 | c.jsonModuleGraph = true |
| 881 | } else if arg == "bp2build" { |
| 882 | c.bp2build = true |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 883 | } else if arg == "queryview" { |
| 884 | c.queryview = true |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 885 | } else if arg == "soong_docs" { |
| 886 | c.soongDocs = true |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 887 | } else { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 888 | if arg == "checkbuild" { |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 889 | c.checkbuild = true |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 890 | } |
Dan Willemsen | 9b58749 | 2017-07-10 22:13:00 -0700 | [diff] [blame] | 891 | c.arguments = append(c.arguments, arg) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 892 | } |
| 893 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 894 | } |
| 895 | |
Jeongik Cha | 518f3ea | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 896 | func validateNinjaWeightList(weightListFilePath string) (err error) { |
| 897 | data, err := os.ReadFile(weightListFilePath) |
| 898 | if err != nil { |
| 899 | return |
| 900 | } |
| 901 | lines := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 902 | for _, line := range lines { |
| 903 | fields := strings.Split(line, ",") |
| 904 | if len(fields) != 2 { |
| 905 | return fmt.Errorf("wrong format, each line should have two fields, but '%s'", line) |
| 906 | } |
| 907 | _, err = strconv.Atoi(fields[1]) |
| 908 | if err != nil { |
| 909 | return |
| 910 | } |
| 911 | } |
| 912 | return |
| 913 | } |
| 914 | |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 915 | func (c *configImpl) configureLocale(ctx Context) { |
| 916 | cmd := Command(ctx, Config{c}, "locale", "locale", "-a") |
| 917 | output, err := cmd.Output() |
| 918 | |
| 919 | var locales []string |
| 920 | if err == nil { |
| 921 | locales = strings.Split(string(output), "\n") |
| 922 | } else { |
| 923 | // If we're unable to list the locales, let's assume en_US.UTF-8 |
| 924 | locales = []string{"en_US.UTF-8"} |
| 925 | ctx.Verbosef("Failed to list locales (%q), falling back to %q", err, locales) |
| 926 | } |
| 927 | |
| 928 | // gettext uses LANGUAGE, which is passed directly through |
| 929 | |
| 930 | // For LANG and LC_*, only preserve the evaluated version of |
| 931 | // LC_MESSAGES |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 932 | userLang := "" |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 933 | if lc_all, ok := c.environ.Get("LC_ALL"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 934 | userLang = lc_all |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 935 | } else if lc_messages, ok := c.environ.Get("LC_MESSAGES"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 936 | userLang = lc_messages |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 937 | } else if lang, ok := c.environ.Get("LANG"); ok { |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 938 | userLang = lang |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | c.environ.UnsetWithPrefix("LC_") |
| 942 | |
Jaewoong Jung | 18aefc1 | 2020-12-21 09:11:10 -0800 | [diff] [blame] | 943 | if userLang != "" { |
| 944 | c.environ.Set("LC_MESSAGES", userLang) |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | // The for LANG, use C.UTF-8 if it exists (Debian currently, proposed |
| 948 | // for others) |
| 949 | if inList("C.UTF-8", locales) { |
| 950 | c.environ.Set("LANG", "C.UTF-8") |
Aaron Kling | d236e0e | 2018-08-07 19:21:36 -0500 | [diff] [blame] | 951 | } else if inList("C.utf8", locales) { |
| 952 | // These normalize to the same thing |
| 953 | c.environ.Set("LANG", "C.UTF-8") |
Dan Willemsen | ed86952 | 2018-01-08 14:58:46 -0800 | [diff] [blame] | 954 | } else if inList("en_US.UTF-8", locales) { |
| 955 | c.environ.Set("LANG", "en_US.UTF-8") |
| 956 | } else if inList("en_US.utf8", locales) { |
| 957 | // These normalize to the same thing |
| 958 | c.environ.Set("LANG", "en_US.UTF-8") |
| 959 | } else { |
| 960 | ctx.Fatalln("System doesn't support either C.UTF-8 or en_US.UTF-8") |
| 961 | } |
| 962 | } |
| 963 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 964 | func (c *configImpl) Environment() *Environment { |
| 965 | return c.environ |
| 966 | } |
| 967 | |
| 968 | func (c *configImpl) Arguments() []string { |
| 969 | return c.arguments |
| 970 | } |
| 971 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 972 | func (c *configImpl) SoongBuildInvocationNeeded() bool { |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 973 | if len(c.Arguments()) > 0 { |
| 974 | // Explicit targets requested that are not special targets like b2pbuild |
| 975 | // or the JSON module graph |
| 976 | return true |
| 977 | } |
| 978 | |
Chris Parsons | 73f411b | 2023-06-20 21:46:57 +0000 | [diff] [blame] | 979 | if !c.JsonModuleGraph() && !c.Bp2Build() && !c.Queryview() && !c.SoongDocs() { |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 980 | // Command line was empty, the default Ninja target is built |
| 981 | return true |
| 982 | } |
| 983 | |
Liz Kammer | 8867742 | 2021-12-15 15:03:19 -0500 | [diff] [blame] | 984 | // bp2build + dist may be used to dist bp2build logs but does not require SoongBuildInvocation |
| 985 | if c.Dist() && !c.Bp2Build() { |
| 986 | return true |
| 987 | } |
| 988 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 989 | // build.ninja doesn't need to be generated |
| 990 | return false |
| 991 | } |
| 992 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 993 | func (c *configImpl) OutDir() string { |
| 994 | if outDir, ok := c.environ.Get("OUT_DIR"); ok { |
Patrice Arruda | 19bd53e | 2019-07-08 17:26:47 -0700 | [diff] [blame] | 995 | return outDir |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 996 | } |
| 997 | return "out" |
| 998 | } |
| 999 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 1000 | func (c *configImpl) DistDir() string { |
Chris Parsons | 19ab9a4 | 2022-08-30 13:15:04 -0400 | [diff] [blame] | 1001 | return c.distDir |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | func (c *configImpl) RealDistDir() string { |
Dan Willemsen | 2d31a44 | 2018-10-20 21:33:41 -0700 | [diff] [blame] | 1005 | return c.distDir |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 1006 | } |
| 1007 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1008 | func (c *configImpl) NinjaArgs() []string { |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 1009 | if c.skipKati { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1010 | return c.arguments |
| 1011 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1012 | return c.ninjaArgs |
| 1013 | } |
| 1014 | |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 1015 | func (c *configImpl) BazelOutDir() string { |
| 1016 | return filepath.Join(c.OutDir(), "bazel") |
| 1017 | } |
| 1018 | |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame] | 1019 | func (c *configImpl) bazelOutputBase() string { |
| 1020 | return filepath.Join(c.BazelOutDir(), "output") |
| 1021 | } |
| 1022 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1023 | func (c *configImpl) SoongOutDir() string { |
| 1024 | return filepath.Join(c.OutDir(), "soong") |
| 1025 | } |
| 1026 | |
Spandan Das | 394aa32 | 2022-11-03 17:02:10 +0000 | [diff] [blame] | 1027 | func (c *configImpl) ApiSurfacesOutDir() string { |
| 1028 | return filepath.Join(c.OutDir(), "api_surfaces") |
| 1029 | } |
| 1030 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 1031 | func (c *configImpl) PrebuiltOS() string { |
| 1032 | switch runtime.GOOS { |
| 1033 | case "linux": |
| 1034 | return "linux-x86" |
| 1035 | case "darwin": |
| 1036 | return "darwin-x86" |
| 1037 | default: |
| 1038 | panic("Unknown GOOS") |
| 1039 | } |
| 1040 | } |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 1041 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 1042 | func (c *configImpl) HostToolDir() string { |
Colin Cross | acfcc1f | 2021-10-25 15:40:32 -0700 | [diff] [blame] | 1043 | if c.SkipKatiNinja() { |
| 1044 | return filepath.Join(c.SoongOutDir(), "host", c.PrebuiltOS(), "bin") |
| 1045 | } else { |
| 1046 | return filepath.Join(c.OutDir(), "host", c.PrebuiltOS(), "bin") |
| 1047 | } |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 1050 | func (c *configImpl) NamedGlobFile(name string) string { |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 1051 | return shared.JoinPath(c.SoongOutDir(), "globs-"+name+".ninja") |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 1052 | } |
| 1053 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 1054 | func (c *configImpl) UsedEnvFile(tag string) string { |
Kiyoung Kim | eaa55a8 | 2023-06-05 16:56:49 +0900 | [diff] [blame] | 1055 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 1056 | return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+v+"."+tag) |
| 1057 | } |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 1058 | return shared.JoinPath(c.SoongOutDir(), usedEnvFile+"."+tag) |
| 1059 | } |
| 1060 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 1061 | func (c *configImpl) Bp2BuildFilesMarkerFile() string { |
| 1062 | return shared.JoinPath(c.SoongOutDir(), "bp2build_files_marker") |
| 1063 | } |
| 1064 | |
| 1065 | func (c *configImpl) Bp2BuildWorkspaceMarkerFile() string { |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 1066 | return shared.JoinPath(c.SoongOutDir(), "bp2build_workspace_marker") |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 1069 | func (c *configImpl) SoongDocsHtml() string { |
| 1070 | return shared.JoinPath(c.SoongOutDir(), "docs/soong_build.html") |
| 1071 | } |
| 1072 | |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 1073 | func (c *configImpl) QueryviewMarkerFile() string { |
| 1074 | return shared.JoinPath(c.SoongOutDir(), "queryview.marker") |
| 1075 | } |
| 1076 | |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 1077 | func (c *configImpl) ModuleGraphFile() string { |
| 1078 | return shared.JoinPath(c.SoongOutDir(), "module-graph.json") |
| 1079 | } |
| 1080 | |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 1081 | func (c *configImpl) ModuleActionsFile() string { |
| 1082 | return shared.JoinPath(c.SoongOutDir(), "module-actions.json") |
| 1083 | } |
| 1084 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 1085 | func (c *configImpl) TempDir() string { |
| 1086 | return shared.TempDirForOutDir(c.SoongOutDir()) |
| 1087 | } |
| 1088 | |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 1089 | func (c *configImpl) FileListDir() string { |
| 1090 | return filepath.Join(c.OutDir(), ".module_paths") |
| 1091 | } |
| 1092 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1093 | func (c *configImpl) KatiSuffix() string { |
| 1094 | if c.katiSuffix != "" { |
| 1095 | return c.katiSuffix |
| 1096 | } |
| 1097 | panic("SetKatiSuffix has not been called") |
| 1098 | } |
| 1099 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 1100 | // Checkbuild returns true if "checkbuild" was one of the build goals, which means that the |
| 1101 | // user is interested in additional checks at the expense of build time. |
| 1102 | func (c *configImpl) Checkbuild() bool { |
| 1103 | return c.checkbuild |
| 1104 | } |
| 1105 | |
Dan Willemsen | 8a073a8 | 2017-02-04 17:30:44 -0800 | [diff] [blame] | 1106 | func (c *configImpl) Dist() bool { |
| 1107 | return c.dist |
| 1108 | } |
| 1109 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 1110 | func (c *configImpl) JsonModuleGraph() bool { |
| 1111 | return c.jsonModuleGraph |
| 1112 | } |
| 1113 | |
| 1114 | func (c *configImpl) Bp2Build() bool { |
| 1115 | return c.bp2build |
| 1116 | } |
| 1117 | |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 1118 | func (c *configImpl) Queryview() bool { |
| 1119 | return c.queryview |
| 1120 | } |
| 1121 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 1122 | func (c *configImpl) SoongDocs() bool { |
| 1123 | return c.soongDocs |
| 1124 | } |
| 1125 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1126 | func (c *configImpl) IsVerbose() bool { |
| 1127 | return c.verbose |
| 1128 | } |
| 1129 | |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 1130 | func (c *configImpl) MultitreeBuild() bool { |
| 1131 | return c.multitreeBuild |
| 1132 | } |
| 1133 | |
Jeongik Cha | 0cf44d5 | 2023-03-15 00:10:45 +0900 | [diff] [blame] | 1134 | func (c *configImpl) NinjaWeightListSource() NinjaWeightListSource { |
| 1135 | return c.ninjaWeightListSource |
| 1136 | } |
| 1137 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 1138 | func (c *configImpl) SkipKati() bool { |
| 1139 | return c.skipKati |
| 1140 | } |
| 1141 | |
Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 1142 | func (c *configImpl) SkipKatiNinja() bool { |
| 1143 | return c.skipKatiNinja |
| 1144 | } |
| 1145 | |
Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 1146 | func (c *configImpl) SkipSoong() bool { |
| 1147 | return c.skipSoong |
| 1148 | } |
| 1149 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 1150 | func (c *configImpl) SkipNinja() bool { |
| 1151 | return c.skipNinja |
| 1152 | } |
| 1153 | |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 1154 | func (c *configImpl) SetSkipNinja(v bool) { |
| 1155 | c.skipNinja = v |
| 1156 | } |
| 1157 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 1158 | func (c *configImpl) SkipConfig() bool { |
| 1159 | return c.skipConfig |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 1162 | func (c *configImpl) BuildFromTextStub() bool { |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 1163 | return !c.buildFromSourceStub |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1166 | func (c *configImpl) TargetProduct() string { |
| 1167 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 1168 | return v |
| 1169 | } |
| 1170 | panic("TARGET_PRODUCT is not defined") |
| 1171 | } |
| 1172 | |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 1173 | func (c *configImpl) TargetProductOrErr() (string, error) { |
| 1174 | if v, ok := c.environ.Get("TARGET_PRODUCT"); ok { |
| 1175 | return v, nil |
| 1176 | } |
| 1177 | return "", fmt.Errorf("TARGET_PRODUCT is not defined") |
| 1178 | } |
| 1179 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 1180 | func (c *configImpl) TargetDevice() string { |
| 1181 | return c.targetDevice |
| 1182 | } |
| 1183 | |
| 1184 | func (c *configImpl) SetTargetDevice(device string) { |
| 1185 | c.targetDevice = device |
| 1186 | } |
| 1187 | |
| 1188 | func (c *configImpl) TargetBuildVariant() string { |
| 1189 | if v, ok := c.environ.Get("TARGET_BUILD_VARIANT"); ok { |
| 1190 | return v |
| 1191 | } |
| 1192 | panic("TARGET_BUILD_VARIANT is not defined") |
| 1193 | } |
| 1194 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1195 | func (c *configImpl) KatiArgs() []string { |
| 1196 | return c.katiArgs |
| 1197 | } |
| 1198 | |
| 1199 | func (c *configImpl) Parallel() int { |
| 1200 | return c.parallel |
| 1201 | } |
| 1202 | |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 1203 | func (c *configImpl) GetSourceRootDirs() []string { |
| 1204 | return c.sourceRootDirs |
| 1205 | } |
| 1206 | |
| 1207 | func (c *configImpl) SetSourceRootDirs(i []string) { |
| 1208 | c.sourceRootDirs = i |
| 1209 | } |
| 1210 | |
Spandan Das | c576383 | 2022-11-08 18:42:16 +0000 | [diff] [blame] | 1211 | func (c *configImpl) GetIncludeTags() []string { |
| 1212 | return c.includeTags |
| 1213 | } |
| 1214 | |
| 1215 | func (c *configImpl) SetIncludeTags(i []string) { |
| 1216 | c.includeTags = i |
| 1217 | } |
| 1218 | |
MarkDacek | 6614d9c | 2022-12-07 21:57:38 +0000 | [diff] [blame] | 1219 | func (c *configImpl) GetLogsPrefix() string { |
| 1220 | return c.logsPrefix |
| 1221 | } |
| 1222 | |
| 1223 | func (c *configImpl) SetLogsPrefix(prefix string) { |
| 1224 | c.logsPrefix = prefix |
| 1225 | } |
| 1226 | |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 1227 | func (c *configImpl) HighmemParallel() int { |
| 1228 | if i, ok := c.environ.GetInt("NINJA_HIGHMEM_NUM_JOBS"); ok { |
| 1229 | return i |
| 1230 | } |
| 1231 | |
| 1232 | const minMemPerHighmemProcess = 8 * 1024 * 1024 * 1024 |
| 1233 | parallel := c.Parallel() |
| 1234 | if c.UseRemoteBuild() { |
| 1235 | // Ninja doesn't support nested pools, and when remote builds are enabled the total ninja parallelism |
| 1236 | // is set very high (i.e. 500). Using a large value here would cause the total number of running jobs |
| 1237 | // to be the sum of the sizes of the local and highmem pools, which will cause extra CPU contention. |
| 1238 | // Return 1/16th of the size of the local pool, rounding up. |
| 1239 | return (parallel + 15) / 16 |
| 1240 | } else if c.totalRAM == 0 { |
| 1241 | // Couldn't detect the total RAM, don't restrict highmem processes. |
| 1242 | return parallel |
Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 1243 | } else if c.totalRAM <= 16*1024*1024*1024 { |
| 1244 | // Less than 16GB of ram, restrict to 1 highmem processes |
| 1245 | return 1 |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 1246 | } else if c.totalRAM <= 32*1024*1024*1024 { |
| 1247 | // Less than 32GB of ram, restrict to 2 highmem processes |
| 1248 | return 2 |
| 1249 | } else if p := int(c.totalRAM / minMemPerHighmemProcess); p < parallel { |
| 1250 | // If less than 8GB total RAM per process, reduce the number of highmem processes |
| 1251 | return p |
| 1252 | } |
| 1253 | // No restriction on highmem processes |
| 1254 | return parallel |
| 1255 | } |
| 1256 | |
Dan Willemsen | 2bb82d0 | 2019-12-27 09:35:42 -0800 | [diff] [blame] | 1257 | func (c *configImpl) TotalRAM() uint64 { |
| 1258 | return c.totalRAM |
| 1259 | } |
| 1260 | |
Kousik Kumar | ec47864 | 2020-09-21 13:39:24 -0400 | [diff] [blame] | 1261 | // ForceUseGoma determines whether we should override Goma deprecation |
| 1262 | // and use Goma for the current build or not. |
| 1263 | func (c *configImpl) ForceUseGoma() bool { |
| 1264 | if v, ok := c.environ.Get("FORCE_USE_GOMA"); ok { |
| 1265 | v = strings.TrimSpace(v) |
| 1266 | if v != "" && v != "false" { |
| 1267 | return true |
| 1268 | } |
| 1269 | } |
| 1270 | return false |
| 1271 | } |
| 1272 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1273 | func (c *configImpl) UseGoma() bool { |
| 1274 | if v, ok := c.environ.Get("USE_GOMA"); ok { |
| 1275 | v = strings.TrimSpace(v) |
| 1276 | if v != "" && v != "false" { |
| 1277 | return true |
| 1278 | } |
| 1279 | } |
| 1280 | return false |
| 1281 | } |
| 1282 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 1283 | func (c *configImpl) StartGoma() bool { |
| 1284 | if !c.UseGoma() { |
| 1285 | return false |
| 1286 | } |
| 1287 | |
| 1288 | if v, ok := c.environ.Get("NOSTART_GOMA"); ok { |
| 1289 | v = strings.TrimSpace(v) |
| 1290 | if v != "" && v != "false" { |
| 1291 | return false |
| 1292 | } |
| 1293 | } |
| 1294 | return true |
| 1295 | } |
| 1296 | |
Kousik Kumar | 6d1e348 | 2023-07-24 03:44:16 +0000 | [diff] [blame] | 1297 | func (c *configImpl) canSupportRBE() bool { |
| 1298 | // Do not use RBE with prod credentials in scenarios when stubby doesn't exist, since |
| 1299 | // its unlikely that we will be able to obtain necessary creds without stubby. |
| 1300 | authType, _ := c.rbeAuth() |
| 1301 | if !c.StubbyExists() && strings.Contains(authType, "use_google_prod_creds") { |
| 1302 | return false |
| 1303 | } |
| 1304 | return true |
| 1305 | } |
| 1306 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 1307 | func (c *configImpl) UseRBE() bool { |
Jingwen Chen | d7ccde1 | 2023-06-28 07:19:26 +0000 | [diff] [blame] | 1308 | // These alternate modes of running Soong do not use RBE / reclient. |
Chris Parsons | 73f411b | 2023-06-20 21:46:57 +0000 | [diff] [blame] | 1309 | if c.Bp2Build() || c.Queryview() || c.JsonModuleGraph() { |
Jingwen Chen | d7ccde1 | 2023-06-28 07:19:26 +0000 | [diff] [blame] | 1310 | return false |
| 1311 | } |
| 1312 | |
Kousik Kumar | 6d1e348 | 2023-07-24 03:44:16 +0000 | [diff] [blame] | 1313 | if !c.canSupportRBE() { |
Kousik Kumar | 67ad434 | 2023-06-06 15:09:27 -0400 | [diff] [blame] | 1314 | return false |
| 1315 | } |
Kousik Kumar | 6d1e348 | 2023-07-24 03:44:16 +0000 | [diff] [blame] | 1316 | |
Kousik Kumar | 3ff037e | 2022-01-25 22:11:01 -0500 | [diff] [blame] | 1317 | if v, ok := c.Environment().Get("USE_RBE"); ok { |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 1318 | v = strings.TrimSpace(v) |
| 1319 | if v != "" && v != "false" { |
| 1320 | return true |
| 1321 | } |
| 1322 | } |
| 1323 | return false |
| 1324 | } |
| 1325 | |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 1326 | func (c *configImpl) BazelBuildEnabled() bool { |
Chris Parsons | 21f8027 | 2023-06-15 04:02:28 +0000 | [diff] [blame] | 1327 | return c.bazelProdMode || c.bazelStagingMode |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 1328 | } |
| 1329 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 1330 | func (c *configImpl) StartRBE() bool { |
| 1331 | if !c.UseRBE() { |
| 1332 | return false |
| 1333 | } |
| 1334 | |
| 1335 | if v, ok := c.environ.Get("NOSTART_RBE"); ok { |
| 1336 | v = strings.TrimSpace(v) |
| 1337 | if v != "" && v != "false" { |
| 1338 | return false |
| 1339 | } |
| 1340 | } |
| 1341 | return true |
| 1342 | } |
| 1343 | |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 1344 | func (c *configImpl) rbeProxyLogsDir() string { |
| 1345 | for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} { |
Kousik Kumar | 0d15a72 | 2020-09-23 02:54:11 -0400 | [diff] [blame] | 1346 | if v, ok := c.environ.Get(f); ok { |
| 1347 | return v |
| 1348 | } |
| 1349 | } |
Ramy Medhat | bc06176 | 2023-10-10 18:36:59 +0000 | [diff] [blame] | 1350 | return c.rbeTmpDir() |
| 1351 | } |
| 1352 | |
| 1353 | func (c *configImpl) rbeDownloadTmpDir() string { |
Cole Faust | 06ea531 | 2023-10-18 17:38:40 -0700 | [diff] [blame] | 1354 | for _, f := range []string{"RBE_download_tmp_dir", "FLAG_download_tmp_dir"} { |
Ramy Medhat | bc06176 | 2023-10-10 18:36:59 +0000 | [diff] [blame] | 1355 | if v, ok := c.environ.Get(f); ok { |
| 1356 | return v |
| 1357 | } |
| 1358 | } |
| 1359 | return c.rbeTmpDir() |
| 1360 | } |
| 1361 | |
| 1362 | func (c *configImpl) rbeTmpDir() string { |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 1363 | buildTmpDir := shared.TempDirForOutDir(c.SoongOutDir()) |
| 1364 | return filepath.Join(buildTmpDir, "rbe") |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 1365 | } |
| 1366 | |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 1367 | func (c *configImpl) rbeCacheDir() string { |
| 1368 | for _, f := range []string{"RBE_cache_dir", "FLAG_cache_dir"} { |
| 1369 | if v, ok := c.environ.Get(f); ok { |
| 1370 | return v |
| 1371 | } |
| 1372 | } |
| 1373 | return shared.JoinPath(c.SoongOutDir(), "rbe") |
| 1374 | } |
| 1375 | |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 1376 | func (c *configImpl) shouldCleanupRBELogsDir() bool { |
| 1377 | // Perform a log directory cleanup only when the log directory |
| 1378 | // is auto created by the build rather than user-specified. |
| 1379 | for _, f := range []string{"RBE_proxy_log_dir", "FLAG_output_dir"} { |
| 1380 | if _, ok := c.environ.Get(f); ok { |
| 1381 | return false |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 1382 | } |
| 1383 | } |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 1384 | return true |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | func (c *configImpl) rbeExecRoot() string { |
| 1388 | for _, f := range []string{"RBE_exec_root", "FLAG_exec_root"} { |
| 1389 | if v, ok := c.environ.Get(f); ok { |
| 1390 | return v |
| 1391 | } |
| 1392 | } |
| 1393 | wd, err := os.Getwd() |
| 1394 | if err != nil { |
| 1395 | return "" |
| 1396 | } |
| 1397 | return wd |
| 1398 | } |
| 1399 | |
| 1400 | func (c *configImpl) rbeDir() string { |
| 1401 | if v, ok := c.environ.Get("RBE_DIR"); ok { |
| 1402 | return v |
| 1403 | } |
| 1404 | return "prebuilts/remoteexecution-client/live/" |
| 1405 | } |
| 1406 | |
| 1407 | func (c *configImpl) rbeReproxy() string { |
| 1408 | for _, f := range []string{"RBE_re_proxy", "FLAG_re_proxy"} { |
| 1409 | if v, ok := c.environ.Get(f); ok { |
| 1410 | return v |
| 1411 | } |
| 1412 | } |
| 1413 | return filepath.Join(c.rbeDir(), "reproxy") |
| 1414 | } |
| 1415 | |
| 1416 | func (c *configImpl) rbeAuth() (string, string) { |
Kousik Kumar | 93d192c | 2022-03-18 01:39:56 -0400 | [diff] [blame] | 1417 | credFlags := []string{ |
| 1418 | "use_application_default_credentials", |
| 1419 | "use_gce_credentials", |
| 1420 | "credential_file", |
| 1421 | "use_google_prod_creds", |
| 1422 | } |
Ramy Medhat | 0fc67eb | 2020-08-12 01:26:23 -0400 | [diff] [blame] | 1423 | for _, cf := range credFlags { |
| 1424 | for _, f := range []string{"RBE_" + cf, "FLAG_" + cf} { |
| 1425 | if v, ok := c.environ.Get(f); ok { |
| 1426 | v = strings.TrimSpace(v) |
| 1427 | if v != "" && v != "false" && v != "0" { |
| 1428 | return "RBE_" + cf, v |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | return "RBE_use_application_default_credentials", "true" |
Patrice Arruda | 62f1bf2 | 2020-07-07 12:48:26 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 1436 | func (c *configImpl) rbeSockAddr(dir string) (string, error) { |
| 1437 | maxNameLen := len(syscall.RawSockaddrUnix{}.Path) |
| 1438 | base := fmt.Sprintf("reproxy_%v.sock", rbeRandPrefix) |
| 1439 | |
| 1440 | name := filepath.Join(dir, base) |
| 1441 | if len(name) < maxNameLen { |
| 1442 | return name, nil |
| 1443 | } |
| 1444 | |
| 1445 | name = filepath.Join("/tmp", base) |
| 1446 | if len(name) < maxNameLen { |
| 1447 | return name, nil |
| 1448 | } |
| 1449 | |
| 1450 | return "", fmt.Errorf("cannot generate a proxy socket address shorter than the limit of %v", maxNameLen) |
| 1451 | } |
| 1452 | |
Kousik Kumar | 7bc7819 | 2022-04-27 14:52:56 -0400 | [diff] [blame] | 1453 | // IsGooglerEnvironment returns true if the current build is running |
| 1454 | // on a Google developer machine and false otherwise. |
| 1455 | func (c *configImpl) IsGooglerEnvironment() bool { |
| 1456 | cf := "ANDROID_BUILD_ENVIRONMENT_CONFIG" |
| 1457 | if v, ok := c.environ.Get(cf); ok { |
| 1458 | return v == "googler" |
| 1459 | } |
| 1460 | return false |
| 1461 | } |
| 1462 | |
| 1463 | // GoogleProdCredsExist determine whether credentials exist on the |
| 1464 | // Googler machine to use remote execution. |
| 1465 | func (c *configImpl) GoogleProdCredsExist() bool { |
Kevin Dagostino | 096ab2f | 2023-03-03 19:47:17 +0000 | [diff] [blame] | 1466 | if googleProdCredsExistCache { |
| 1467 | return googleProdCredsExistCache |
| 1468 | } |
andusyu | 0b3dc03 | 2023-06-21 17:29:32 -0400 | [diff] [blame] | 1469 | if _, err := exec.Command("/usr/bin/gcertstatus", "-nocheck_ssh").Output(); err != nil { |
Kousik Kumar | 7bc7819 | 2022-04-27 14:52:56 -0400 | [diff] [blame] | 1470 | return false |
| 1471 | } |
Kevin Dagostino | 096ab2f | 2023-03-03 19:47:17 +0000 | [diff] [blame] | 1472 | googleProdCredsExistCache = true |
Kousik Kumar | 7bc7819 | 2022-04-27 14:52:56 -0400 | [diff] [blame] | 1473 | return true |
| 1474 | } |
| 1475 | |
| 1476 | // UseRemoteBuild indicates whether to use a remote build acceleration system |
| 1477 | // to speed up the build. |
Colin Cross | 9016b91 | 2019-11-11 14:57:42 -0800 | [diff] [blame] | 1478 | func (c *configImpl) UseRemoteBuild() bool { |
| 1479 | return c.UseGoma() || c.UseRBE() |
| 1480 | } |
| 1481 | |
Kousik Kumar | 7bc7819 | 2022-04-27 14:52:56 -0400 | [diff] [blame] | 1482 | // StubbyExists checks whether the stubby binary exists on the machine running |
| 1483 | // the build. |
| 1484 | func (c *configImpl) StubbyExists() bool { |
| 1485 | if _, err := exec.LookPath("stubby"); err != nil { |
| 1486 | return false |
| 1487 | } |
| 1488 | return true |
| 1489 | } |
| 1490 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1491 | // RemoteParallel controls how many remote jobs (i.e., commands which contain |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 1492 | // 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] | 1493 | // still limited by Parallel() |
| 1494 | func (c *configImpl) RemoteParallel() int { |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 1495 | if !c.UseRemoteBuild() { |
| 1496 | return 0 |
| 1497 | } |
| 1498 | if i, ok := c.environ.GetInt("NINJA_REMOTE_NUM_JOBS"); ok { |
| 1499 | return i |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1500 | } |
| 1501 | return 500 |
| 1502 | } |
| 1503 | |
| 1504 | func (c *configImpl) SetKatiArgs(args []string) { |
| 1505 | c.katiArgs = args |
| 1506 | } |
| 1507 | |
| 1508 | func (c *configImpl) SetNinjaArgs(args []string) { |
| 1509 | c.ninjaArgs = args |
| 1510 | } |
| 1511 | |
| 1512 | func (c *configImpl) SetKatiSuffix(suffix string) { |
| 1513 | c.katiSuffix = suffix |
| 1514 | } |
| 1515 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1516 | func (c *configImpl) LastKatiSuffixFile() string { |
| 1517 | return filepath.Join(c.OutDir(), "last_kati_suffix") |
| 1518 | } |
| 1519 | |
| 1520 | func (c *configImpl) HasKatiSuffix() bool { |
| 1521 | return c.katiSuffix != "" |
| 1522 | } |
| 1523 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1524 | func (c *configImpl) KatiEnvFile() string { |
| 1525 | return filepath.Join(c.OutDir(), "env"+c.KatiSuffix()+".sh") |
| 1526 | } |
| 1527 | |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 1528 | func (c *configImpl) KatiBuildNinjaFile() string { |
| 1529 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiBuildSuffix+".ninja") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 1532 | func (c *configImpl) KatiPackageNinjaFile() string { |
| 1533 | return filepath.Join(c.OutDir(), "build"+c.KatiSuffix()+katiPackageSuffix+".ninja") |
| 1534 | } |
| 1535 | |
Jihoon Kang | 9f4f8a3 | 2022-08-16 00:57:30 +0000 | [diff] [blame] | 1536 | func (c *configImpl) SoongVarsFile() string { |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 1537 | targetProduct, err := c.TargetProductOrErr() |
| 1538 | if err != nil { |
| 1539 | return filepath.Join(c.SoongOutDir(), "soong.variables") |
| 1540 | } else { |
| 1541 | return filepath.Join(c.SoongOutDir(), "soong."+targetProduct+".variables") |
| 1542 | } |
Jihoon Kang | 9f4f8a3 | 2022-08-16 00:57:30 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1545 | func (c *configImpl) SoongNinjaFile() string { |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 1546 | targetProduct, err := c.TargetProductOrErr() |
| 1547 | if err != nil { |
| 1548 | return filepath.Join(c.SoongOutDir(), "build.ninja") |
| 1549 | } else { |
| 1550 | return filepath.Join(c.SoongOutDir(), "build."+targetProduct+".ninja") |
| 1551 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
| 1554 | func (c *configImpl) CombinedNinjaFile() string { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 1555 | if c.katiSuffix == "" { |
| 1556 | return filepath.Join(c.OutDir(), "combined.ninja") |
| 1557 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1558 | return filepath.Join(c.OutDir(), "combined"+c.KatiSuffix()+".ninja") |
| 1559 | } |
| 1560 | |
| 1561 | func (c *configImpl) SoongAndroidMk() string { |
| 1562 | return filepath.Join(c.SoongOutDir(), "Android-"+c.TargetProduct()+".mk") |
| 1563 | } |
| 1564 | |
| 1565 | func (c *configImpl) SoongMakeVarsMk() string { |
| 1566 | return filepath.Join(c.SoongOutDir(), "make_vars-"+c.TargetProduct()+".mk") |
| 1567 | } |
| 1568 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 1569 | func (c *configImpl) SoongBuildMetrics() string { |
Colin Cross | b67b061 | 2023-10-31 10:02:45 -0700 | [diff] [blame] | 1570 | return filepath.Join(c.LogsDir(), "soong_build_metrics.pb") |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1573 | func (c *configImpl) ProductOut() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 1574 | return filepath.Join(c.OutDir(), "target", "product", c.TargetDevice()) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1575 | } |
| 1576 | |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 1577 | func (c *configImpl) DevicePreviousProductConfig() string { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1578 | return filepath.Join(c.ProductOut(), "previous_build_config.mk") |
| 1579 | } |
| 1580 | |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 1581 | func (c *configImpl) KatiPackageMkDir() string { |
| 1582 | return filepath.Join(c.ProductOut(), "obj", "CONFIG", "kati_packaging") |
| 1583 | } |
| 1584 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1585 | func (c *configImpl) hostOutRoot() string { |
Dan Willemsen | 4dc4e14 | 2017-09-08 14:35:43 -0700 | [diff] [blame] | 1586 | return filepath.Join(c.OutDir(), "host") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1587 | } |
| 1588 | |
| 1589 | func (c *configImpl) HostOut() string { |
| 1590 | return filepath.Join(c.hostOutRoot(), c.HostPrebuiltTag()) |
| 1591 | } |
| 1592 | |
| 1593 | // This probably needs to be multi-valued, so not exporting it for now |
| 1594 | func (c *configImpl) hostCrossOut() string { |
| 1595 | if runtime.GOOS == "linux" { |
| 1596 | return filepath.Join(c.hostOutRoot(), "windows-x86") |
| 1597 | } else { |
| 1598 | return "" |
| 1599 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 1600 | } |
| 1601 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1602 | func (c *configImpl) HostPrebuiltTag() string { |
| 1603 | if runtime.GOOS == "linux" { |
| 1604 | return "linux-x86" |
| 1605 | } else if runtime.GOOS == "darwin" { |
| 1606 | return "darwin-x86" |
| 1607 | } else { |
| 1608 | panic("Unsupported OS") |
| 1609 | } |
| 1610 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1611 | |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 1612 | func (c *configImpl) PrebuiltBuildTool(name string) string { |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1613 | if v, ok := c.environ.Get("SANITIZE_HOST"); ok { |
| 1614 | if sanitize := strings.Fields(v); inList("address", sanitize) { |
Dan Willemsen | 8122bd5 | 2017-10-12 20:20:41 -0700 | [diff] [blame] | 1615 | asan := filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "asan/bin", name) |
| 1616 | if _, err := os.Stat(asan); err == nil { |
| 1617 | return asan |
| 1618 | } |
Dan Willemsen | f173d59 | 2017-04-27 14:28:00 -0700 | [diff] [blame] | 1619 | } |
| 1620 | } |
| 1621 | return filepath.Join("prebuilts/build-tools", c.HostPrebuiltTag(), "bin", name) |
| 1622 | } |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 1623 | |
| 1624 | func (c *configImpl) SetBuildBrokenDupRules(val bool) { |
| 1625 | c.brokenDupRules = val |
| 1626 | } |
| 1627 | |
| 1628 | func (c *configImpl) BuildBrokenDupRules() bool { |
| 1629 | return c.brokenDupRules |
| 1630 | } |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 1631 | |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 1632 | func (c *configImpl) SetBuildBrokenUsesNetwork(val bool) { |
| 1633 | c.brokenUsesNetwork = val |
| 1634 | } |
| 1635 | |
| 1636 | func (c *configImpl) BuildBrokenUsesNetwork() bool { |
| 1637 | return c.brokenUsesNetwork |
| 1638 | } |
| 1639 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 1640 | func (c *configImpl) SetBuildBrokenNinjaUsesEnvVars(val []string) { |
| 1641 | c.brokenNinjaEnvVars = val |
| 1642 | } |
| 1643 | |
| 1644 | func (c *configImpl) BuildBrokenNinjaUsesEnvVars() []string { |
| 1645 | return c.brokenNinjaEnvVars |
| 1646 | } |
| 1647 | |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 1648 | func (c *configImpl) SetTargetDeviceDir(dir string) { |
| 1649 | c.targetDeviceDir = dir |
| 1650 | } |
| 1651 | |
| 1652 | func (c *configImpl) TargetDeviceDir() string { |
| 1653 | return c.targetDeviceDir |
| 1654 | } |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 1655 | |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 1656 | func (c *configImpl) BuildDateTime() string { |
| 1657 | return c.buildDateTime |
| 1658 | } |
| 1659 | |
| 1660 | func (c *configImpl) MetricsUploaderApp() string { |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 1661 | return c.metricsUploader |
Patrice Arruda | 219eef3 | 2020-06-01 17:29:30 +0000 | [diff] [blame] | 1662 | } |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1663 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 1664 | // LogsDir returns the absolute path to the logs directory where build log and |
| 1665 | // metrics files are located. By default, the logs directory is the out |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1666 | // directory. If the argument dist is specified, the logs directory |
| 1667 | // is <dist_dir>/logs. |
| 1668 | 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] | 1669 | dir := c.OutDir() |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1670 | if c.Dist() { |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 1671 | // 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] | 1672 | dir = filepath.Join(c.RealDistDir(), "logs") |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1673 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 1674 | absDir, err := filepath.Abs(dir) |
| 1675 | if err != nil { |
| 1676 | fmt.Fprintf(os.Stderr, "\nError making log dir '%s' absolute: %s\n", dir, err.Error()) |
| 1677 | os.Exit(1) |
| 1678 | } |
| 1679 | return absDir |
Patrice Arruda | 83842d7 | 2020-12-08 19:42:08 +0000 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | // BazelMetricsDir returns the <logs dir>/bazel_metrics directory |
| 1683 | // where the bazel profiles are located. |
| 1684 | func (c *configImpl) BazelMetricsDir() string { |
| 1685 | return filepath.Join(c.LogsDir(), "bazel_metrics") |
| 1686 | } |
Colin Cross | f3bdbcb | 2021-06-01 11:43:55 -0700 | [diff] [blame] | 1687 | |
Chris Parsons | 53f68ae | 2022-03-03 12:01:40 -0500 | [diff] [blame] | 1688 | // MkFileMetrics returns the file path for make-related metrics. |
| 1689 | func (c *configImpl) MkMetrics() string { |
| 1690 | return filepath.Join(c.LogsDir(), "mk_metrics.pb") |
| 1691 | } |
| 1692 | |
Colin Cross | f3bdbcb | 2021-06-01 11:43:55 -0700 | [diff] [blame] | 1693 | func (c *configImpl) SetEmptyNinjaFile(v bool) { |
| 1694 | c.emptyNinjaFile = v |
| 1695 | } |
| 1696 | |
| 1697 | func (c *configImpl) EmptyNinjaFile() bool { |
| 1698 | return c.emptyNinjaFile |
| 1699 | } |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 1700 | |
Romain Jobredeaux | 0a7529b | 2022-10-26 12:56:41 -0400 | [diff] [blame] | 1701 | func (c *configImpl) IsBazelMixedBuildForceDisabled() bool { |
| 1702 | return c.Environment().IsEnvTrue("BUILD_BROKEN_DISABLE_BAZEL") |
| 1703 | } |
| 1704 | |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 1705 | func (c *configImpl) IsPersistentBazelEnabled() bool { |
| 1706 | return c.Environment().IsEnvTrue("USE_PERSISTENT_BAZEL") |
| 1707 | } |
| 1708 | |
Chris Parsons | c83398f | 2023-05-31 18:41:41 +0000 | [diff] [blame] | 1709 | // GetBazeliskBazelVersion returns the Bazel version to use for this build, |
| 1710 | // or the empty string if the current canonical prod Bazel should be used. |
| 1711 | // This environment variable should only be set to debug the build system. |
| 1712 | // The Bazel version, if set, will be passed to Bazelisk, and Bazelisk will |
| 1713 | // handle downloading and invoking the correct Bazel binary. |
| 1714 | func (c *configImpl) GetBazeliskBazelVersion() string { |
| 1715 | value, _ := c.Environment().Get("USE_BAZEL_VERSION") |
| 1716 | return value |
| 1717 | } |
| 1718 | |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 1719 | func (c *configImpl) BazelModulesForceEnabledByFlag() string { |
| 1720 | return c.bazelForceEnabledModules |
| 1721 | } |
| 1722 | |
MarkDacek | d0e7cd3 | 2022-12-02 22:22:40 +0000 | [diff] [blame] | 1723 | func (c *configImpl) SkipMetricsUpload() bool { |
| 1724 | return c.skipMetricsUpload |
| 1725 | } |
| 1726 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 1727 | func (c *configImpl) EnsureAllowlistIntegrity() bool { |
| 1728 | return c.ensureAllowlistIntegrity |
| 1729 | } |
| 1730 | |
MarkDacek | 6614d9c | 2022-12-07 21:57:38 +0000 | [diff] [blame] | 1731 | // Returns a Time object if one was passed via a command-line flag. |
| 1732 | // Otherwise returns the passed default. |
| 1733 | func (c *configImpl) BuildStartedTimeOrDefault(defaultTime time.Time) time.Time { |
| 1734 | if c.buildStartedTime == 0 { |
| 1735 | return defaultTime |
| 1736 | } |
| 1737 | return time.UnixMilli(c.buildStartedTime) |
| 1738 | } |
| 1739 | |
MarkDacek | d33c2fd | 2023-05-04 20:40:04 +0000 | [diff] [blame] | 1740 | func (c *configImpl) BazelExitCode() int32 { |
| 1741 | return c.bazelExitCode |
| 1742 | } |
| 1743 | |
Yu Liu | 6e13b40 | 2021-07-27 14:29:06 -0700 | [diff] [blame] | 1744 | func GetMetricsUploader(topDir string, env *Environment) string { |
| 1745 | if p, ok := env.Get("METRICS_UPLOADER"); ok { |
| 1746 | metricsUploader := filepath.Join(topDir, p) |
| 1747 | if _, err := os.Stat(metricsUploader); err == nil { |
| 1748 | return metricsUploader |
| 1749 | } |
| 1750 | } |
| 1751 | |
| 1752 | return "" |
| 1753 | } |