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