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