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