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