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