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