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