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 ( |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 18 | "encoding/json" |
| 19 | "errors" |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 20 | "fmt" |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 21 | "io/fs" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 22 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "path/filepath" |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 24 | "runtime" |
| 25 | "slices" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 26 | "strconv" |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 27 | "strings" |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 28 | "sync" |
| 29 | "sync/atomic" |
Colin Cross | 117af42 | 2024-09-25 09:14:21 -0700 | [diff] [blame] | 30 | "syscall" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 31 | "time" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 32 | |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 33 | "android/soong/ui/tracer" |
| 34 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 35 | "android/soong/ui/metrics" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 36 | "android/soong/ui/metrics/metrics_proto" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 37 | "android/soong/ui/status" |
| 38 | |
| 39 | "android/soong/shared" |
| 40 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 41 | "github.com/google/blueprint" |
| 42 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 43 | "github.com/google/blueprint/microfactory" |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 44 | "github.com/google/blueprint/pathtools" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 45 | |
| 46 | "google.golang.org/protobuf/proto" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 47 | ) |
| 48 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 49 | const ( |
| 50 | availableEnvFile = "soong.environment.available" |
| 51 | usedEnvFile = "soong.environment.used" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 52 | |
Colin Cross | 8d411ff | 2023-12-07 10:31:24 -0800 | [diff] [blame] | 53 | soongBuildTag = "build" |
| 54 | jsonModuleGraphTag = "modulegraph" |
Colin Cross | 8d411ff | 2023-12-07 10:31:24 -0800 | [diff] [blame] | 55 | soongDocsTag = "soong_docs" |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 56 | |
| 57 | // bootstrapEpoch is used to determine if an incremental build is incompatible with the current |
| 58 | // version of bootstrap and needs cleaning before continuing the build. Increment this for |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 59 | // incompatible changes, for example when moving the location of a microfactory binary that is |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 60 | // executed during bootstrap before the primary builder has had a chance to update the path. |
Lukacs T. Berki | 9985d9a | 2021-11-04 11:47:42 +0100 | [diff] [blame] | 61 | bootstrapEpoch = 1 |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 62 | ) |
| 63 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 64 | var ( |
| 65 | // Used during parallel update of symlinks in out directory to reflect new |
| 66 | // TOP dir. |
| 67 | symlinkWg sync.WaitGroup |
| 68 | numFound, numUpdated uint32 |
| 69 | ) |
| 70 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 71 | func writeEnvironmentFile(_ Context, envFile string, envDeps map[string]string) error { |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 72 | data, err := shared.EnvFileContents(envDeps) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 77 | return os.WriteFile(envFile, data, 0644) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 80 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 81 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 82 | // However, the execution of <builddir>/build.ninja happens later in |
| 83 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 84 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 85 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 86 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 87 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 88 | // 1. We use "Microfactory", a simple tool to compile Go code, to build |
| 89 | // first itself, then soong_ui from soong_ui.bash. This binary contains |
| 90 | // parts of soong_build that are needed to build itself. |
| 91 | // 2. This simplified version of soong_build then reads the Blueprint files |
| 92 | // that describe itself and emits .bootstrap/build.ninja that describes |
| 93 | // how to build its full version and use that to produce the final Ninja |
| 94 | // file Soong emits. |
| 95 | // 3. soong_ui executes .bootstrap/build.ninja |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 96 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 97 | // (After this, Kati is executed to parse the Makefiles, but that's not part of |
| 98 | // bootstrapping Soong) |
| 99 | |
| 100 | // A tiny struct used to tell Blueprint that it's in bootstrap mode. It would |
| 101 | // probably be nicer to use a flag in bootstrap.Args instead. |
| 102 | type BlueprintConfig struct { |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 103 | toolDir string |
| 104 | soongOutDir string |
| 105 | outDir string |
| 106 | runGoTests bool |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 107 | debugCompilation bool |
| 108 | subninjas []string |
| 109 | primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 112 | func (c BlueprintConfig) HostToolDir() string { |
| 113 | return c.toolDir |
| 114 | } |
| 115 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 116 | func (c BlueprintConfig) SoongOutDir() string { |
| 117 | return c.soongOutDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 120 | func (c BlueprintConfig) OutDir() string { |
| 121 | return c.outDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 122 | } |
| 123 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 124 | func (c BlueprintConfig) RunGoTests() bool { |
| 125 | return c.runGoTests |
| 126 | } |
| 127 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 128 | func (c BlueprintConfig) DebugCompilation() bool { |
| 129 | return c.debugCompilation |
| 130 | } |
| 131 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 132 | func (c BlueprintConfig) Subninjas() []string { |
| 133 | return c.subninjas |
| 134 | } |
| 135 | |
| 136 | func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation { |
| 137 | return c.primaryBuilderInvocations |
| 138 | } |
| 139 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 140 | func environmentArgs(config Config, tag string) []string { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 141 | return []string{ |
| 142 | "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile), |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 143 | "--used_env", config.UsedEnvFile(tag), |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 144 | } |
| 145 | } |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 146 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 147 | func writeEmptyFile(ctx Context, path string) { |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 148 | err := os.MkdirAll(filepath.Dir(path), 0777) |
| 149 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 150 | ctx.Fatalf("Failed to create parent directories of empty file '%s': %s", path, err) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 153 | if exists, err := fileExists(path); err != nil { |
| 154 | ctx.Fatalf("Failed to check if file '%s' exists: %s", path, err) |
| 155 | } else if !exists { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 156 | err = os.WriteFile(path, nil, 0666) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 157 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 158 | ctx.Fatalf("Failed to create empty file '%s': %s", path, err) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 163 | func fileExists(path string) (bool, error) { |
| 164 | if _, err := os.Stat(path); os.IsNotExist(err) { |
| 165 | return false, nil |
| 166 | } else if err != nil { |
| 167 | return false, err |
| 168 | } |
| 169 | return true, nil |
| 170 | } |
| 171 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 172 | type PrimaryBuilderFactory struct { |
| 173 | name string |
| 174 | description string |
| 175 | config Config |
| 176 | output string |
| 177 | specificArgs []string |
| 178 | debugPort string |
| 179 | } |
| 180 | |
Jeongik Cha | ccf3700 | 2023-08-04 01:46:32 +0900 | [diff] [blame] | 181 | func getGlobPathName(config Config) string { |
| 182 | globPathName, ok := config.TargetProductOrErr() |
| 183 | if ok != nil { |
| 184 | globPathName = soongBuildTag |
| 185 | } |
| 186 | return globPathName |
| 187 | } |
| 188 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 189 | func getGlobPathNameFromPrimaryBuilderFactory(config Config, pb PrimaryBuilderFactory) string { |
| 190 | if pb.name == soongBuildTag { |
| 191 | // Glob path for soong build would be separated per product target |
| 192 | return getGlobPathName(config) |
| 193 | } |
| 194 | return pb.name |
| 195 | } |
| 196 | |
| 197 | func (pb PrimaryBuilderFactory) primaryBuilderInvocation(config Config) bootstrap.PrimaryBuilderInvocation { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 198 | commonArgs := make([]string, 0, 0) |
| 199 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 200 | if !pb.config.skipSoongTests { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 201 | commonArgs = append(commonArgs, "-t") |
| 202 | } |
| 203 | |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 204 | if pb.config.buildFromSourceStub { |
| 205 | commonArgs = append(commonArgs, "--build-from-source-stub") |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 206 | } |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 207 | |
Joe Onorato | e5ed347 | 2024-02-02 14:52:05 -0800 | [diff] [blame] | 208 | if pb.config.moduleDebugFile != "" { |
| 209 | commonArgs = append(commonArgs, "--soong_module_debug") |
| 210 | commonArgs = append(commonArgs, pb.config.moduleDebugFile) |
| 211 | } |
| 212 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 213 | commonArgs = append(commonArgs, "-l", filepath.Join(pb.config.FileListDir(), "Android.bp.list")) |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 214 | invocationEnv := make(map[string]string) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 215 | if pb.debugPort != "" { |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 216 | //debug mode |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 217 | commonArgs = append(commonArgs, "--delve_listen", pb.debugPort, |
| 218 | "--delve_path", shared.ResolveDelveBinary()) |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 219 | // GODEBUG=asyncpreemptoff=1 disables the preemption of goroutines. This |
| 220 | // is useful because the preemption happens by sending SIGURG to the OS |
| 221 | // thread hosting the goroutine in question and each signal results in |
| 222 | // work that needs to be done by Delve; it uses ptrace to debug the Go |
| 223 | // process and the tracer process must deal with every signal (it is not |
| 224 | // possible to selectively ignore SIGURG). This makes debugging slower, |
| 225 | // sometimes by an order of magnitude depending on luck. |
| 226 | // The original reason for adding async preemption to Go is here: |
| 227 | // https://github.com/golang/proposal/blob/master/design/24543-non-cooperative-preemption.md |
| 228 | invocationEnv["GODEBUG"] = "asyncpreemptoff=1" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 229 | } |
| 230 | |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 231 | var allArgs []string |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 232 | allArgs = append(allArgs, pb.specificArgs...) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 233 | |
| 234 | allArgs = append(allArgs, commonArgs...) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 235 | allArgs = append(allArgs, environmentArgs(pb.config, pb.name)...) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 236 | if profileCpu := os.Getenv("SOONG_PROFILE_CPU"); profileCpu != "" { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 237 | allArgs = append(allArgs, "--cpuprofile", profileCpu+"."+pb.name) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 238 | } |
| 239 | if profileMem := os.Getenv("SOONG_PROFILE_MEM"); profileMem != "" { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 240 | allArgs = append(allArgs, "--memprofile", profileMem+"."+pb.name) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 241 | } |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 242 | allArgs = append(allArgs, "Android.bp") |
| 243 | |
| 244 | return bootstrap.PrimaryBuilderInvocation{ |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 245 | Implicits: []string{pb.output + ".glob_results"}, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 246 | Outputs: []string{pb.output}, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 247 | Args: allArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 248 | Description: pb.description, |
Lukacs T. Berki | 2fad341 | 2022-01-04 14:40:13 +0100 | [diff] [blame] | 249 | // NB: Changing the value of this environment variable will not result in a |
| 250 | // rebuild. The bootstrap Ninja file will change, but apparently Ninja does |
| 251 | // not consider changing the pool specified in a statement a change that's |
| 252 | // worth rebuilding for. |
| 253 | Console: os.Getenv("SOONG_UNBUFFERED_OUTPUT") == "1", |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 254 | Env: invocationEnv, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 258 | // bootstrapEpochCleanup deletes files used by bootstrap during incremental builds across |
| 259 | // incompatible changes. Incompatible changes are marked by incrementing the bootstrapEpoch |
| 260 | // constant. A tree is considered out of date for the current epoch of the |
| 261 | // .soong.bootstrap.epoch.<epoch> file doesn't exist. |
| 262 | func bootstrapEpochCleanup(ctx Context, config Config) { |
| 263 | epochFile := fmt.Sprintf(".soong.bootstrap.epoch.%d", bootstrapEpoch) |
| 264 | epochPath := filepath.Join(config.SoongOutDir(), epochFile) |
| 265 | if exists, err := fileExists(epochPath); err != nil { |
| 266 | ctx.Fatalf("failed to check if bootstrap epoch file %q exists: %q", epochPath, err) |
| 267 | } else if !exists { |
| 268 | // The tree is out of date for the current epoch, delete files used by bootstrap |
| 269 | // and force the primary builder to rerun. |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 270 | soongNinjaFile := config.SoongNinjaFile() |
| 271 | os.Remove(soongNinjaFile) |
| 272 | for _, file := range blueprint.GetNinjaShardFiles(soongNinjaFile) { |
| 273 | if ok, _ := fileExists(file); ok { |
| 274 | os.Remove(file) |
| 275 | } |
| 276 | } |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 277 | os.Remove(soongNinjaFile + ".globs") |
| 278 | os.Remove(soongNinjaFile + ".globs_time") |
| 279 | os.Remove(soongNinjaFile + ".glob_results") |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 280 | |
| 281 | // Mark the tree as up to date with the current epoch by writing the epoch marker file. |
| 282 | writeEmptyFile(ctx, epochPath) |
| 283 | } |
| 284 | } |
| 285 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 286 | func bootstrapBlueprint(ctx Context, config Config) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 287 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 288 | defer ctx.EndTrace() |
| 289 | |
Colin Cross | 98bb3b8 | 2024-10-25 14:45:07 -0700 | [diff] [blame] | 290 | st := ctx.Status.StartTool() |
| 291 | defer st.Finish() |
| 292 | st.SetTotalActions(1) |
| 293 | action := &status.Action{ |
| 294 | Description: "bootstrap blueprint", |
| 295 | Outputs: []string{"bootstrap blueprint"}, |
| 296 | } |
| 297 | st.StartAction(action) |
| 298 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 299 | // Clean up some files for incremental builds across incompatible changes. |
| 300 | bootstrapEpochCleanup(ctx, config) |
| 301 | |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 302 | baseArgs := []string{"--soong_variables", config.SoongVarsFile()} |
| 303 | |
| 304 | mainSoongBuildExtraArgs := append(baseArgs, "-o", config.SoongNinjaFile()) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 305 | if config.EmptyNinjaFile() { |
| 306 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file") |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 307 | } |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 308 | if config.buildFromSourceStub { |
| 309 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--build-from-source-stub") |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 310 | } |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 311 | if config.ensureAllowlistIntegrity { |
| 312 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--ensure-allowlist-integrity") |
| 313 | } |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame] | 314 | if config.incrementalBuildActions { |
| 315 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--incremental-build-actions") |
| 316 | } |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 317 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 318 | pbfs := []PrimaryBuilderFactory{ |
| 319 | { |
| 320 | name: soongBuildTag, |
| 321 | description: fmt.Sprintf("analyzing Android.bp files and generating ninja file at %s", config.SoongNinjaFile()), |
| 322 | config: config, |
| 323 | output: config.SoongNinjaFile(), |
| 324 | specificArgs: mainSoongBuildExtraArgs, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 325 | }, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 326 | { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 327 | name: jsonModuleGraphTag, |
| 328 | description: fmt.Sprintf("generating the Soong module graph at %s", config.ModuleGraphFile()), |
| 329 | config: config, |
| 330 | output: config.ModuleGraphFile(), |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 331 | specificArgs: append(baseArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 332 | "--module_graph_file", config.ModuleGraphFile(), |
| 333 | "--module_actions_file", config.ModuleActionsFile(), |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 334 | ), |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 335 | }, |
| 336 | { |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 337 | name: soongDocsTag, |
| 338 | description: fmt.Sprintf("generating Soong docs at %s", config.SoongDocsHtml()), |
| 339 | config: config, |
| 340 | output: config.SoongDocsHtml(), |
| 341 | specificArgs: append(baseArgs, |
| 342 | "--soong_docs", config.SoongDocsHtml(), |
| 343 | ), |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 344 | }, |
| 345 | } |
| 346 | |
| 347 | // Figure out which invocations will be run under the debugger: |
| 348 | // * SOONG_DELVE if set specifies listening port |
| 349 | // * SOONG_DELVE_STEPS if set specifies specific invocations to be debugged, otherwise all are |
| 350 | debuggedInvocations := make(map[string]bool) |
| 351 | delvePort := os.Getenv("SOONG_DELVE") |
| 352 | if delvePort != "" { |
| 353 | if steps := os.Getenv("SOONG_DELVE_STEPS"); steps != "" { |
| 354 | var validSteps []string |
| 355 | for _, pbf := range pbfs { |
| 356 | debuggedInvocations[pbf.name] = false |
| 357 | validSteps = append(validSteps, pbf.name) |
| 358 | |
| 359 | } |
| 360 | for _, step := range strings.Split(steps, ",") { |
| 361 | if _, ok := debuggedInvocations[step]; ok { |
| 362 | debuggedInvocations[step] = true |
| 363 | } else { |
| 364 | ctx.Fatalf("SOONG_DELVE_STEPS contains unknown soong_build step %s\n"+ |
| 365 | "Valid steps are %v", step, validSteps) |
| 366 | } |
| 367 | } |
| 368 | } else { |
| 369 | // SOONG_DELVE_STEPS is not set, run all steps in the debugger |
| 370 | for _, pbf := range pbfs { |
| 371 | debuggedInvocations[pbf.name] = true |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | var invocations []bootstrap.PrimaryBuilderInvocation |
| 377 | for _, pbf := range pbfs { |
| 378 | if debuggedInvocations[pbf.name] { |
| 379 | pbf.debugPort = delvePort |
| 380 | } |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 381 | pbi := pbf.primaryBuilderInvocation(config) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 382 | invocations = append(invocations, pbi) |
| 383 | } |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 384 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 385 | blueprintArgs := bootstrap.Args{ |
| 386 | ModuleListFile: filepath.Join(config.FileListDir(), "Android.bp.list"), |
| 387 | OutFile: shared.JoinPath(config.SoongOutDir(), "bootstrap.ninja"), |
| 388 | EmptyNinjaFile: false, |
| 389 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 390 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 391 | blueprintCtx := blueprint.NewContext() |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 392 | blueprintCtx.AddSourceRootDirs(config.GetSourceRootDirs()...) |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 393 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 394 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 395 | soongOutDir: config.SoongOutDir(), |
| 396 | toolDir: config.HostToolDir(), |
| 397 | outDir: config.OutDir(), |
| 398 | runGoTests: !config.skipSoongTests, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 399 | // If we want to debug soong_build, we need to compile it for debugging |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 400 | debugCompilation: delvePort != "", |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 401 | primaryBuilderInvocations: invocations, |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 402 | } |
| 403 | |
usta | 5bb4a5d | 2022-08-24 12:53:46 -0400 | [diff] [blame] | 404 | // since `bootstrap.ninja` is regenerated unconditionally, we ignore the deps, i.e. little |
| 405 | // reason to write a `bootstrap.ninja.d` file |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 406 | _, err := bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig) |
Colin Cross | 98bb3b8 | 2024-10-25 14:45:07 -0700 | [diff] [blame] | 407 | |
| 408 | result := status.ActionResult{ |
| 409 | Action: action, |
| 410 | } |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 411 | if err != nil { |
Colin Cross | 98bb3b8 | 2024-10-25 14:45:07 -0700 | [diff] [blame] | 412 | result.Error = err |
| 413 | result.Output = err.Error() |
| 414 | } |
| 415 | st.FinishAction(result) |
| 416 | if err != nil { |
| 417 | ctx.Fatalf("bootstrap failed") |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 418 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 421 | func checkEnvironmentFile(ctx Context, currentEnv *Environment, envFile string) { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 422 | getenv := func(k string) string { |
| 423 | v, _ := currentEnv.Get(k) |
| 424 | return v |
| 425 | } |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 426 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 427 | // Log the changed environment variables to ChangedEnvironmentVariable field |
| 428 | if stale, changedEnvironmentVariableList, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 429 | for _, changedEnvironmentVariable := range changedEnvironmentVariableList { |
| 430 | ctx.Metrics.AddChangedEnvironmentVariable(changedEnvironmentVariable) |
| 431 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 432 | os.Remove(envFile) |
| 433 | } |
| 434 | } |
| 435 | |
George Burgess IV | 1f46a91 | 2024-11-14 09:53:50 -0700 | [diff] [blame] | 436 | func updateSymlinks(ctx Context, dir, prevCWD, cwd string, updateSemaphore chan struct{}) error { |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 437 | defer symlinkWg.Done() |
| 438 | |
| 439 | visit := func(path string, d fs.DirEntry, err error) error { |
| 440 | if d.IsDir() && path != dir { |
| 441 | symlinkWg.Add(1) |
George Burgess IV | 1f46a91 | 2024-11-14 09:53:50 -0700 | [diff] [blame] | 442 | go updateSymlinks(ctx, path, prevCWD, cwd, updateSemaphore) |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 443 | return filepath.SkipDir |
| 444 | } |
| 445 | f, err := d.Info() |
| 446 | if err != nil { |
| 447 | return err |
| 448 | } |
| 449 | // If the file is not a symlink, we don't have to update it. |
| 450 | if f.Mode()&os.ModeSymlink != os.ModeSymlink { |
| 451 | return nil |
| 452 | } |
| 453 | |
| 454 | atomic.AddUint32(&numFound, 1) |
| 455 | target, err := os.Readlink(path) |
| 456 | if err != nil { |
| 457 | return err |
| 458 | } |
| 459 | if strings.HasPrefix(target, prevCWD) && |
| 460 | (len(target) == len(prevCWD) || target[len(prevCWD)] == '/') { |
| 461 | target = filepath.Join(cwd, target[len(prevCWD):]) |
| 462 | if err := os.Remove(path); err != nil { |
| 463 | return err |
| 464 | } |
| 465 | if err := os.Symlink(target, path); err != nil { |
| 466 | return err |
| 467 | } |
| 468 | atomic.AddUint32(&numUpdated, 1) |
| 469 | } |
| 470 | return nil |
| 471 | } |
| 472 | |
George Burgess IV | 1f46a91 | 2024-11-14 09:53:50 -0700 | [diff] [blame] | 473 | <-updateSemaphore |
| 474 | defer func() { updateSemaphore <- struct{}{} }() |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 475 | if err := filepath.WalkDir(dir, visit); err != nil { |
| 476 | return err |
| 477 | } |
| 478 | return nil |
| 479 | } |
| 480 | |
George Burgess IV | 1f46a91 | 2024-11-14 09:53:50 -0700 | [diff] [blame] | 481 | // b/376466642: If the concurrency of updateSymlinks is unbounded, Go's runtime spawns a |
| 482 | // theoretically unbounded number of threads to handle blocking syscalls. This causes the runtime to |
| 483 | // panic due to hitting thread limits in rare cases. Limiting to GOMAXPROCS concurrent symlink |
| 484 | // updates should make this a non-issue. |
| 485 | func newUpdateSemaphore() chan struct{} { |
| 486 | numPermits := runtime.GOMAXPROCS(0) |
| 487 | c := make(chan struct{}, numPermits) |
| 488 | for i := 0; i < numPermits; i++ { |
| 489 | c <- struct{}{} |
| 490 | } |
| 491 | return c |
| 492 | } |
| 493 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 494 | func fixOutDirSymlinks(ctx Context, config Config, outDir string) error { |
| 495 | cwd, err := os.Getwd() |
| 496 | if err != nil { |
| 497 | return err |
| 498 | } |
| 499 | |
| 500 | // Record the .top as the very last thing in the function. |
| 501 | tf := filepath.Join(outDir, ".top") |
| 502 | defer func() { |
| 503 | if err := os.WriteFile(tf, []byte(cwd), 0644); err != nil { |
| 504 | fmt.Fprintf(os.Stderr, fmt.Sprintf("Unable to log CWD: %v", err)) |
| 505 | } |
| 506 | }() |
| 507 | |
| 508 | // Find the previous working directory if it was recorded. |
| 509 | var prevCWD string |
| 510 | pcwd, err := os.ReadFile(tf) |
| 511 | if err != nil { |
| 512 | if os.IsNotExist(err) { |
| 513 | // No previous working directory recorded, nothing to do. |
| 514 | return nil |
| 515 | } |
| 516 | return err |
| 517 | } |
| 518 | prevCWD = strings.Trim(string(pcwd), "\n") |
| 519 | |
| 520 | if prevCWD == cwd { |
| 521 | // We are in the same source dir, nothing to update. |
| 522 | return nil |
| 523 | } |
| 524 | |
| 525 | symlinkWg.Add(1) |
George Burgess IV | 1f46a91 | 2024-11-14 09:53:50 -0700 | [diff] [blame] | 526 | if err := updateSymlinks(ctx, outDir, prevCWD, cwd, newUpdateSemaphore()); err != nil { |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 527 | return err |
| 528 | } |
| 529 | symlinkWg.Wait() |
| 530 | ctx.Println(fmt.Sprintf("Updated %d/%d symlinks in dir %v", numUpdated, numFound, outDir)) |
| 531 | return nil |
| 532 | } |
| 533 | |
| 534 | func migrateOutputSymlinks(ctx Context, config Config) error { |
| 535 | // Figure out the real out directory ("out" could be a symlink). |
| 536 | outDir := config.OutDir() |
| 537 | s, err := os.Lstat(outDir) |
| 538 | if err != nil { |
| 539 | if os.IsNotExist(err) { |
| 540 | // No out dir exists, no symlinks to migrate. |
| 541 | return nil |
| 542 | } |
| 543 | return err |
| 544 | } |
| 545 | if s.Mode()&os.ModeSymlink == os.ModeSymlink { |
| 546 | target, err := filepath.EvalSymlinks(outDir) |
| 547 | if err != nil { |
| 548 | return err |
| 549 | } |
| 550 | outDir = target |
| 551 | } |
| 552 | return fixOutDirSymlinks(ctx, config, outDir) |
| 553 | } |
| 554 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 555 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 556 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 557 | defer ctx.EndTrace() |
| 558 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 559 | if err := migrateOutputSymlinks(ctx, config); err != nil { |
| 560 | ctx.Fatalf("failed to migrate output directory to current TOP dir: %v", err) |
| 561 | } |
| 562 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 563 | // We have two environment files: .available is the one with every variable, |
| 564 | // .used with the ones that were actually used. The latter is used to |
| 565 | // determine whether Soong needs to be re-run since why re-run it if only |
| 566 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 567 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 568 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 569 | // This is done unconditionally, but does not take a measurable amount of time |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 570 | bootstrapBlueprint(ctx, config) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 571 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 572 | soongBuildEnv := config.Environment().Copy() |
| 573 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 574 | soongBuildEnv.Set("LOG_DIR", config.LogsDir()) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 575 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 576 | // For Soong bootstrapping tests |
| 577 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 578 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 579 | } |
| 580 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 581 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 582 | if err != nil { |
| 583 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 584 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 585 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 586 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 587 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 588 | defer ctx.EndTrace() |
| 589 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 590 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongBuildTag)) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 591 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 592 | if config.JsonModuleGraph() { |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 593 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 594 | } |
| 595 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 596 | if config.SoongDocs() { |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 597 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongDocsTag)) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 598 | } |
| 599 | }() |
| 600 | |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 601 | ninja := func(targets ...string) { |
| 602 | ctx.BeginTrace(metrics.RunSoong, "bootstrap") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 603 | defer ctx.EndTrace() |
| 604 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 605 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 606 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 607 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 608 | |
LaMont Jones | ece626c | 2024-09-03 11:19:31 -0700 | [diff] [blame] | 609 | var ninjaCmd string |
| 610 | var ninjaArgs []string |
| 611 | switch config.ninjaCommand { |
| 612 | case NINJA_N2: |
| 613 | ninjaCmd = config.N2Bin() |
Cole Faust | bee030d | 2024-01-03 13:45:48 -0800 | [diff] [blame] | 614 | ninjaArgs = []string{ |
| 615 | // TODO: implement these features, or remove them. |
| 616 | //"-d", "keepdepfile", |
| 617 | //"-d", "stats", |
| 618 | //"-o", "usesphonyoutputs=yes", |
| 619 | //"-o", "preremoveoutputs=yes", |
| 620 | //"-w", "dupbuild=err", |
| 621 | //"-w", "outputdir=err", |
| 622 | //"-w", "missingoutfile=err", |
| 623 | "-v", |
| 624 | "-j", strconv.Itoa(config.Parallel()), |
| 625 | "--frontend-file", fifo, |
| 626 | "-f", filepath.Join(config.SoongOutDir(), "bootstrap.ninja"), |
| 627 | } |
LaMont Jones | ece626c | 2024-09-03 11:19:31 -0700 | [diff] [blame] | 628 | case NINJA_SISO: |
| 629 | ninjaCmd = config.SisoBin() |
| 630 | ninjaArgs = []string{ |
| 631 | "ninja", |
| 632 | // TODO: implement these features, or remove them. |
| 633 | //"-d", "keepdepfile", |
| 634 | //"-d", "stats", |
| 635 | //"-o", "usesphonyoutputs=yes", |
| 636 | //"-o", "preremoveoutputs=yes", |
| 637 | //"-w", "dupbuild=err", |
| 638 | //"-w", "outputdir=err", |
| 639 | //"-w", "missingoutfile=err", |
| 640 | "-v", |
| 641 | "-j", strconv.Itoa(config.Parallel()), |
| 642 | //"--frontend-file", fifo, |
| 643 | "--log_dir", config.SoongOutDir(), |
| 644 | "-f", filepath.Join(config.SoongOutDir(), "bootstrap.ninja"), |
| 645 | } |
| 646 | default: |
| 647 | // NINJA_NINJA is the default. |
| 648 | ninjaCmd = config.NinjaBin() |
| 649 | ninjaArgs = []string{ |
| 650 | "-d", "keepdepfile", |
| 651 | "-d", "stats", |
| 652 | "-o", "usesphonyoutputs=yes", |
| 653 | "-o", "preremoveoutputs=yes", |
| 654 | "-w", "dupbuild=err", |
| 655 | "-w", "outputdir=err", |
| 656 | "-w", "missingoutfile=err", |
| 657 | "-j", strconv.Itoa(config.Parallel()), |
| 658 | "--frontend_file", fifo, |
| 659 | "-f", filepath.Join(config.SoongOutDir(), "bootstrap.ninja"), |
| 660 | } |
Cole Faust | bee030d | 2024-01-03 13:45:48 -0800 | [diff] [blame] | 661 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 662 | |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 663 | if extra, ok := config.Environment().Get("SOONG_UI_NINJA_ARGS"); ok { |
| 664 | ctx.Printf(`CAUTION: arguments in $SOONG_UI_NINJA_ARGS=%q, e.g. "-n", can make soong_build FAIL or INCORRECT`, extra) |
| 665 | ninjaArgs = append(ninjaArgs, strings.Fields(extra)...) |
| 666 | } |
| 667 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 668 | ninjaArgs = append(ninjaArgs, targets...) |
Cole Faust | bee030d | 2024-01-03 13:45:48 -0800 | [diff] [blame] | 669 | |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 670 | cmd := Command(ctx, config, "soong bootstrap", |
Cole Faust | bee030d | 2024-01-03 13:45:48 -0800 | [diff] [blame] | 671 | ninjaCmd, ninjaArgs...) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 672 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 673 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 674 | |
| 675 | // This is currently how the command line to invoke soong_build finds the |
| 676 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 677 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 678 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 679 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 680 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 681 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 682 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 683 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 684 | targets := make([]string, 0, 0) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 685 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 686 | if config.JsonModuleGraph() { |
| 687 | targets = append(targets, config.ModuleGraphFile()) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 688 | } |
| 689 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 690 | if config.SoongDocs() { |
| 691 | targets = append(targets, config.SoongDocsHtml()) |
| 692 | } |
| 693 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 694 | if config.SoongBuildInvocationNeeded() { |
| 695 | // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build(). |
Cole Faust | 65f298c | 2021-10-28 16:05:13 -0700 | [diff] [blame] | 696 | targets = append(targets, config.SoongNinjaFile()) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 697 | } |
| 698 | |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 699 | for _, target := range targets { |
| 700 | if err := checkGlobs(ctx, target); err != nil { |
| 701 | ctx.Fatalf("Error checking globs: %s", err.Error()) |
| 702 | } |
| 703 | } |
| 704 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 705 | beforeSoongTimestamp := time.Now() |
| 706 | |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 707 | ninja(targets...) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 708 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 709 | loadSoongBuildMetrics(ctx, config, beforeSoongTimestamp) |
| 710 | |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 711 | soongNinjaFile := config.SoongNinjaFile() |
| 712 | distGzipFile(ctx, config, soongNinjaFile, "soong") |
| 713 | for _, file := range blueprint.GetNinjaShardFiles(soongNinjaFile) { |
| 714 | if ok, _ := fileExists(file); ok { |
| 715 | distGzipFile(ctx, config, file, "soong") |
| 716 | } |
| 717 | } |
Jihoon Kang | 9f4f8a3 | 2022-08-16 00:57:30 +0000 | [diff] [blame] | 718 | distFile(ctx, config, config.SoongVarsFile(), "soong") |
Inseob Kim | 58c802f | 2024-06-11 10:59:00 +0900 | [diff] [blame] | 719 | distFile(ctx, config, config.SoongExtraVarsFile(), "soong") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 720 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 721 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 722 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 723 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 724 | } |
| 725 | |
Rob Seymour | 33cd10d | 2022-03-02 23:10:25 +0000 | [diff] [blame] | 726 | if config.JsonModuleGraph() { |
| 727 | distGzipFile(ctx, config, config.ModuleGraphFile(), "soong") |
| 728 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 729 | } |
| 730 | |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 731 | // checkGlobs manages the globs that cause soong to rerun. |
| 732 | // |
| 733 | // When soong_build runs, it will run globs. It will write all the globs |
| 734 | // it ran into the "{finalOutFile}.globs" file. Then every build, |
| 735 | // soong_ui will check that file, rerun the globs, and if they changed |
| 736 | // from the results that soong_build got, update the ".glob_results" |
| 737 | // file, causing soong_build to rerun. The ".glob_results" file will |
| 738 | // be empty on the first run of soong_build, because we don't know |
| 739 | // what the globs are yet, but also remain empty until the globs change |
| 740 | // so that we don't run soong_build a second time unnecessarily. |
| 741 | // Both soong_build and soong_ui will also update a ".globs_time" file |
| 742 | // with the time that they ran at every build. When soong_ui checks |
| 743 | // globs, it only reruns globs whose dependencies are newer than the |
| 744 | // time in the ".globs_time" file. |
| 745 | func checkGlobs(ctx Context, finalOutFile string) error { |
| 746 | ctx.BeginTrace(metrics.RunSoong, "check_globs") |
| 747 | defer ctx.EndTrace() |
| 748 | st := ctx.Status.StartTool() |
| 749 | st.Status("Running globs...") |
| 750 | defer st.Finish() |
| 751 | |
| 752 | globsFile, err := os.Open(finalOutFile + ".globs") |
| 753 | if errors.Is(err, fs.ErrNotExist) { |
| 754 | // if the glob file doesn't exist, make sure the glob_results file exists and is empty. |
| 755 | if err := os.MkdirAll(filepath.Dir(finalOutFile), 0777); err != nil { |
| 756 | return err |
| 757 | } |
| 758 | f, err := os.Create(finalOutFile + ".glob_results") |
| 759 | if err != nil { |
| 760 | return err |
| 761 | } |
| 762 | return f.Close() |
| 763 | } else if err != nil { |
| 764 | return err |
| 765 | } |
| 766 | defer globsFile.Close() |
| 767 | globsFileDecoder := json.NewDecoder(globsFile) |
| 768 | |
| 769 | globsTimeBytes, err := os.ReadFile(finalOutFile + ".globs_time") |
| 770 | if err != nil { |
| 771 | return err |
| 772 | } |
| 773 | globsTimeMicros, err := strconv.ParseInt(strings.TrimSpace(string(globsTimeBytes)), 10, 64) |
| 774 | if err != nil { |
| 775 | return err |
| 776 | } |
| 777 | globCheckStartTime := time.Now().UnixMicro() |
| 778 | |
| 779 | globsChan := make(chan pathtools.GlobResult) |
| 780 | errorsChan := make(chan error) |
| 781 | wg := sync.WaitGroup{} |
Steven Moreland | 8e47abc | 2024-10-01 23:38:15 +0000 | [diff] [blame] | 782 | |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 783 | hasChangedGlobs := false |
Steven Moreland | 8e47abc | 2024-10-01 23:38:15 +0000 | [diff] [blame] | 784 | var changedGlobNameMutex sync.Mutex |
| 785 | var changedGlobName string |
| 786 | |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 787 | for i := 0; i < runtime.NumCPU()*2; i++ { |
| 788 | wg.Add(1) |
| 789 | go func() { |
| 790 | for cachedGlob := range globsChan { |
| 791 | // If we've already determined we have changed globs, just finish consuming |
| 792 | // the channel without doing any more checks. |
| 793 | if hasChangedGlobs { |
| 794 | continue |
| 795 | } |
| 796 | // First, check if any of the deps are newer than the last time globs were checked. |
| 797 | // If not, we don't need to rerun the glob. |
| 798 | hasNewDep := false |
| 799 | for _, dep := range cachedGlob.Deps { |
| 800 | info, err := os.Stat(dep) |
Colin Cross | 117af42 | 2024-09-25 09:14:21 -0700 | [diff] [blame] | 801 | if errors.Is(err, fs.ErrNotExist) || errors.Is(err, syscall.ENOTDIR) { |
Cole Faust | 69c78e9 | 2024-09-10 16:46:06 -0700 | [diff] [blame] | 802 | hasNewDep = true |
| 803 | break |
| 804 | } else if err != nil { |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 805 | errorsChan <- err |
| 806 | continue |
| 807 | } |
| 808 | if info.ModTime().UnixMicro() > globsTimeMicros { |
| 809 | hasNewDep = true |
| 810 | break |
| 811 | } |
| 812 | } |
| 813 | if !hasNewDep { |
| 814 | continue |
| 815 | } |
| 816 | |
| 817 | // Then rerun the glob and check if we got the same result as before. |
| 818 | result, err := pathtools.Glob(cachedGlob.Pattern, cachedGlob.Excludes, pathtools.FollowSymlinks) |
| 819 | if err != nil { |
| 820 | errorsChan <- err |
| 821 | } else { |
| 822 | if !slices.Equal(result.Matches, cachedGlob.Matches) { |
| 823 | hasChangedGlobs = true |
Steven Moreland | 8e47abc | 2024-10-01 23:38:15 +0000 | [diff] [blame] | 824 | |
| 825 | changedGlobNameMutex.Lock() |
| 826 | defer changedGlobNameMutex.Unlock() |
| 827 | changedGlobName = result.Pattern |
Cole Faust | 976890e | 2024-10-24 10:29:08 -0700 | [diff] [blame] | 828 | if len(result.Excludes) > 2 { |
| 829 | changedGlobName += fmt.Sprintf(" (excluding %d other patterns)", len(result.Excludes)) |
| 830 | } else if len(result.Excludes) > 0 { |
| 831 | changedGlobName += " (excluding " + strings.Join(result.Excludes, " and ") + ")" |
Steven Moreland | 8e47abc | 2024-10-01 23:38:15 +0000 | [diff] [blame] | 832 | } |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | } |
| 836 | wg.Done() |
| 837 | }() |
| 838 | } |
| 839 | go func() { |
| 840 | wg.Wait() |
| 841 | close(errorsChan) |
| 842 | }() |
| 843 | |
| 844 | errorsWg := sync.WaitGroup{} |
| 845 | errorsWg.Add(1) |
| 846 | var errFromGoRoutines error |
| 847 | go func() { |
| 848 | for result := range errorsChan { |
| 849 | if errFromGoRoutines == nil { |
| 850 | errFromGoRoutines = result |
| 851 | } |
| 852 | } |
| 853 | errorsWg.Done() |
| 854 | }() |
| 855 | |
| 856 | var cachedGlob pathtools.GlobResult |
| 857 | for globsFileDecoder.More() { |
| 858 | if err := globsFileDecoder.Decode(&cachedGlob); err != nil { |
| 859 | return err |
| 860 | } |
| 861 | // Need to clone the GlobResult because the json decoder will |
| 862 | // reuse the same slice allocations. |
| 863 | globsChan <- cachedGlob.Clone() |
| 864 | } |
| 865 | close(globsChan) |
| 866 | errorsWg.Wait() |
| 867 | if errFromGoRoutines != nil { |
| 868 | return errFromGoRoutines |
| 869 | } |
| 870 | |
| 871 | // Update the globs_time file whether or not we found changed globs, |
| 872 | // so that we don't rerun globs in the future that we just saw didn't change. |
| 873 | err = os.WriteFile( |
| 874 | finalOutFile+".globs_time", |
| 875 | []byte(fmt.Sprintf("%d\n", globCheckStartTime)), |
| 876 | 0666, |
| 877 | ) |
| 878 | if err != nil { |
| 879 | return err |
| 880 | } |
| 881 | |
| 882 | if hasChangedGlobs { |
| 883 | fmt.Fprintf(os.Stdout, "Globs changed, rerunning soong...\n") |
Steven Moreland | 8e47abc | 2024-10-01 23:38:15 +0000 | [diff] [blame] | 884 | fmt.Fprintf(os.Stdout, "One culprit glob (may be more): %s\n", changedGlobName) |
Cole Faust | 2fec412 | 2024-09-07 17:28:11 -0700 | [diff] [blame] | 885 | // Write the current time to the glob_results file. We just need |
| 886 | // some unique value to trigger a rerun, it doesn't matter what it is. |
| 887 | err = os.WriteFile( |
| 888 | finalOutFile+".glob_results", |
| 889 | []byte(fmt.Sprintf("%d\n", globCheckStartTime)), |
| 890 | 0666, |
| 891 | ) |
| 892 | if err != nil { |
| 893 | return err |
| 894 | } |
| 895 | } |
| 896 | return nil |
| 897 | } |
| 898 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 899 | // loadSoongBuildMetrics reads out/soong_build_metrics.pb if it was generated by soong_build and copies the |
| 900 | // events stored in it into the soong_ui trace to provide introspection into how long the different phases of |
| 901 | // soong_build are taking. |
| 902 | func loadSoongBuildMetrics(ctx Context, config Config, oldTimestamp time.Time) { |
| 903 | soongBuildMetricsFile := config.SoongBuildMetrics() |
| 904 | if metricsStat, err := os.Stat(soongBuildMetricsFile); err != nil { |
| 905 | ctx.Verbosef("Failed to stat %s: %s", soongBuildMetricsFile, err) |
| 906 | return |
| 907 | } else if !metricsStat.ModTime().After(oldTimestamp) { |
| 908 | ctx.Verbosef("%s timestamp not later after running soong, expected %s > %s", |
| 909 | soongBuildMetricsFile, metricsStat.ModTime(), oldTimestamp) |
| 910 | return |
| 911 | } |
| 912 | |
Colin Cross | b67b061 | 2023-10-31 10:02:45 -0700 | [diff] [blame] | 913 | metricsData, err := os.ReadFile(soongBuildMetricsFile) |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 914 | if err != nil { |
| 915 | ctx.Verbosef("Failed to read %s: %s", soongBuildMetricsFile, err) |
| 916 | return |
| 917 | } |
| 918 | |
| 919 | soongBuildMetrics := metrics_proto.SoongBuildMetrics{} |
| 920 | err = proto.Unmarshal(metricsData, &soongBuildMetrics) |
| 921 | if err != nil { |
| 922 | ctx.Verbosef("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 923 | return |
| 924 | } |
| 925 | for _, event := range soongBuildMetrics.Events { |
| 926 | desc := event.GetDescription() |
| 927 | if dot := strings.LastIndexByte(desc, '.'); dot >= 0 { |
| 928 | desc = desc[dot+1:] |
| 929 | } |
| 930 | ctx.Tracer.Complete(desc, ctx.Thread, |
| 931 | event.GetStartTime(), event.GetStartTime()+event.GetRealTime()) |
| 932 | } |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 933 | for _, event := range soongBuildMetrics.PerfCounters { |
| 934 | timestamp := event.GetTime() |
| 935 | for _, group := range event.Groups { |
| 936 | counters := make([]tracer.Counter, 0, len(group.Counters)) |
| 937 | for _, counter := range group.Counters { |
| 938 | counters = append(counters, tracer.Counter{ |
| 939 | Name: counter.GetName(), |
| 940 | Value: counter.GetValue(), |
| 941 | }) |
| 942 | } |
| 943 | ctx.Tracer.CountersAtTime(group.GetName(), ctx.Thread, timestamp, counters) |
| 944 | } |
| 945 | } |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 946 | } |
| 947 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 948 | func runMicrofactory(ctx Context, config Config, name string, pkg string, mapping map[string]string) { |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 949 | ctx.BeginTrace(metrics.RunSoong, name) |
| 950 | defer ctx.EndTrace() |
| 951 | cfg := microfactory.Config{TrimPath: absPath(ctx, ".")} |
| 952 | for pkgPrefix, pathPrefix := range mapping { |
| 953 | cfg.Map(pkgPrefix, pathPrefix) |
| 954 | } |
| 955 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 956 | exePath := filepath.Join(config.SoongOutDir(), name) |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 957 | dir := filepath.Dir(exePath) |
| 958 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 959 | ctx.Fatalf("cannot create %s: %s", dir, err) |
| 960 | } |
| 961 | if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil { |
| 962 | ctx.Fatalf("failed to build %s: %s", name, err) |
| 963 | } |
| 964 | } |