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