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