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