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 ( |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 18 | "fmt" |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 19 | "io/fs" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 20 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "path/filepath" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 22 | "strconv" |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 23 | "strings" |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 24 | "sync" |
| 25 | "sync/atomic" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 26 | "time" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 27 | |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 28 | "android/soong/ui/tracer" |
| 29 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 30 | "android/soong/ui/metrics" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 31 | "android/soong/ui/metrics/metrics_proto" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 32 | "android/soong/ui/status" |
| 33 | |
| 34 | "android/soong/shared" |
| 35 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 36 | "github.com/google/blueprint" |
| 37 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 38 | "github.com/google/blueprint/microfactory" |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 39 | "github.com/google/blueprint/pathtools" |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 40 | |
| 41 | "google.golang.org/protobuf/proto" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 42 | ) |
| 43 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 44 | const ( |
| 45 | availableEnvFile = "soong.environment.available" |
| 46 | usedEnvFile = "soong.environment.used" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 47 | |
Colin Cross | 8d411ff | 2023-12-07 10:31:24 -0800 | [diff] [blame] | 48 | soongBuildTag = "build" |
| 49 | jsonModuleGraphTag = "modulegraph" |
| 50 | queryviewTag = "queryview" |
| 51 | soongDocsTag = "soong_docs" |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 52 | |
| 53 | // bootstrapEpoch is used to determine if an incremental build is incompatible with the current |
| 54 | // version of bootstrap and needs cleaning before continuing the build. Increment this for |
| 55 | // incompatible changes, for example when moving the location of the bpglob binary that is |
| 56 | // 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] | 57 | bootstrapEpoch = 1 |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 58 | ) |
| 59 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 60 | var ( |
| 61 | // Used during parallel update of symlinks in out directory to reflect new |
| 62 | // TOP dir. |
| 63 | symlinkWg sync.WaitGroup |
| 64 | numFound, numUpdated uint32 |
| 65 | ) |
| 66 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 67 | func writeEnvironmentFile(_ Context, envFile string, envDeps map[string]string) error { |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 68 | data, err := shared.EnvFileContents(envDeps) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 73 | return os.WriteFile(envFile, data, 0644) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 76 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 77 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 78 | // However, the execution of <builddir>/build.ninja happens later in |
| 79 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 80 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 81 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 82 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 83 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 84 | // 1. We use "Microfactory", a simple tool to compile Go code, to build |
| 85 | // first itself, then soong_ui from soong_ui.bash. This binary contains |
| 86 | // parts of soong_build that are needed to build itself. |
| 87 | // 2. This simplified version of soong_build then reads the Blueprint files |
| 88 | // that describe itself and emits .bootstrap/build.ninja that describes |
| 89 | // how to build its full version and use that to produce the final Ninja |
| 90 | // file Soong emits. |
| 91 | // 3. soong_ui executes .bootstrap/build.ninja |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 92 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 93 | // (After this, Kati is executed to parse the Makefiles, but that's not part of |
| 94 | // bootstrapping Soong) |
| 95 | |
| 96 | // A tiny struct used to tell Blueprint that it's in bootstrap mode. It would |
| 97 | // probably be nicer to use a flag in bootstrap.Args instead. |
| 98 | type BlueprintConfig struct { |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 99 | toolDir string |
| 100 | soongOutDir string |
| 101 | outDir string |
| 102 | runGoTests bool |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 103 | debugCompilation bool |
| 104 | subninjas []string |
| 105 | primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 106 | } |
| 107 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 108 | func (c BlueprintConfig) HostToolDir() string { |
| 109 | return c.toolDir |
| 110 | } |
| 111 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 112 | func (c BlueprintConfig) SoongOutDir() string { |
| 113 | return c.soongOutDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 116 | func (c BlueprintConfig) OutDir() string { |
| 117 | return c.outDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 120 | func (c BlueprintConfig) RunGoTests() bool { |
| 121 | return c.runGoTests |
| 122 | } |
| 123 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 124 | func (c BlueprintConfig) DebugCompilation() bool { |
| 125 | return c.debugCompilation |
| 126 | } |
| 127 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 128 | func (c BlueprintConfig) Subninjas() []string { |
| 129 | return c.subninjas |
| 130 | } |
| 131 | |
| 132 | func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation { |
| 133 | return c.primaryBuilderInvocations |
| 134 | } |
| 135 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 136 | func environmentArgs(config Config, tag string) []string { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 137 | return []string{ |
| 138 | "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile), |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 139 | "--used_env", config.UsedEnvFile(tag), |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 140 | } |
| 141 | } |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 142 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 143 | func writeEmptyFile(ctx Context, path string) { |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 144 | err := os.MkdirAll(filepath.Dir(path), 0777) |
| 145 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 146 | 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] | 147 | } |
| 148 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 149 | if exists, err := fileExists(path); err != nil { |
| 150 | ctx.Fatalf("Failed to check if file '%s' exists: %s", path, err) |
| 151 | } else if !exists { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 152 | err = os.WriteFile(path, nil, 0666) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 153 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 154 | ctx.Fatalf("Failed to create empty file '%s': %s", path, err) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 159 | func fileExists(path string) (bool, error) { |
| 160 | if _, err := os.Stat(path); os.IsNotExist(err) { |
| 161 | return false, nil |
| 162 | } else if err != nil { |
| 163 | return false, err |
| 164 | } |
| 165 | return true, nil |
| 166 | } |
| 167 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 168 | type PrimaryBuilderFactory struct { |
| 169 | name string |
| 170 | description string |
| 171 | config Config |
| 172 | output string |
| 173 | specificArgs []string |
| 174 | debugPort string |
| 175 | } |
| 176 | |
Jeongik Cha | ccf3700 | 2023-08-04 01:46:32 +0900 | [diff] [blame] | 177 | func getGlobPathName(config Config) string { |
| 178 | globPathName, ok := config.TargetProductOrErr() |
| 179 | if ok != nil { |
| 180 | globPathName = soongBuildTag |
| 181 | } |
| 182 | return globPathName |
| 183 | } |
| 184 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 185 | func getGlobPathNameFromPrimaryBuilderFactory(config Config, pb PrimaryBuilderFactory) string { |
| 186 | if pb.name == soongBuildTag { |
| 187 | // Glob path for soong build would be separated per product target |
| 188 | return getGlobPathName(config) |
| 189 | } |
| 190 | return pb.name |
| 191 | } |
| 192 | |
| 193 | func (pb PrimaryBuilderFactory) primaryBuilderInvocation(config Config) bootstrap.PrimaryBuilderInvocation { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 194 | commonArgs := make([]string, 0, 0) |
| 195 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 196 | if !pb.config.skipSoongTests { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 197 | commonArgs = append(commonArgs, "-t") |
| 198 | } |
| 199 | |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 200 | if pb.config.buildFromSourceStub { |
| 201 | commonArgs = append(commonArgs, "--build-from-source-stub") |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 202 | } |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 203 | |
Joe Onorato | e5ed347 | 2024-02-02 14:52:05 -0800 | [diff] [blame] | 204 | if pb.config.moduleDebugFile != "" { |
| 205 | commonArgs = append(commonArgs, "--soong_module_debug") |
| 206 | commonArgs = append(commonArgs, pb.config.moduleDebugFile) |
| 207 | } |
| 208 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 209 | 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] | 210 | invocationEnv := make(map[string]string) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 211 | if pb.debugPort != "" { |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 212 | //debug mode |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 213 | commonArgs = append(commonArgs, "--delve_listen", pb.debugPort, |
| 214 | "--delve_path", shared.ResolveDelveBinary()) |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 215 | // GODEBUG=asyncpreemptoff=1 disables the preemption of goroutines. This |
| 216 | // is useful because the preemption happens by sending SIGURG to the OS |
| 217 | // thread hosting the goroutine in question and each signal results in |
| 218 | // work that needs to be done by Delve; it uses ptrace to debug the Go |
| 219 | // process and the tracer process must deal with every signal (it is not |
| 220 | // possible to selectively ignore SIGURG). This makes debugging slower, |
| 221 | // sometimes by an order of magnitude depending on luck. |
| 222 | // The original reason for adding async preemption to Go is here: |
| 223 | // https://github.com/golang/proposal/blob/master/design/24543-non-cooperative-preemption.md |
| 224 | invocationEnv["GODEBUG"] = "asyncpreemptoff=1" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 225 | } |
| 226 | |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 227 | var allArgs []string |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 228 | allArgs = append(allArgs, pb.specificArgs...) |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 229 | globPathName := getGlobPathNameFromPrimaryBuilderFactory(config, pb) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 230 | allArgs = append(allArgs, |
Jeongik Cha | ccf3700 | 2023-08-04 01:46:32 +0900 | [diff] [blame] | 231 | "--globListDir", globPathName, |
| 232 | "--globFile", pb.config.NamedGlobFile(globPathName)) |
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 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 244 | globfiles := bootstrap.GlobFileListFiles(bootstrap.GlobDirectory(config.SoongOutDir(), globPathName)) |
| 245 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 246 | return bootstrap.PrimaryBuilderInvocation{ |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 247 | Inputs: []string{"Android.bp"}, |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 248 | Implicits: globfiles, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 249 | Outputs: []string{pb.output}, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 250 | Args: allArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 251 | Description: pb.description, |
Lukacs T. Berki | 2fad341 | 2022-01-04 14:40:13 +0100 | [diff] [blame] | 252 | // NB: Changing the value of this environment variable will not result in a |
| 253 | // rebuild. The bootstrap Ninja file will change, but apparently Ninja does |
| 254 | // not consider changing the pool specified in a statement a change that's |
| 255 | // worth rebuilding for. |
| 256 | Console: os.Getenv("SOONG_UNBUFFERED_OUTPUT") == "1", |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 257 | Env: invocationEnv, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 261 | // bootstrapEpochCleanup deletes files used by bootstrap during incremental builds across |
| 262 | // incompatible changes. Incompatible changes are marked by incrementing the bootstrapEpoch |
| 263 | // constant. A tree is considered out of date for the current epoch of the |
| 264 | // .soong.bootstrap.epoch.<epoch> file doesn't exist. |
| 265 | func bootstrapEpochCleanup(ctx Context, config Config) { |
| 266 | epochFile := fmt.Sprintf(".soong.bootstrap.epoch.%d", bootstrapEpoch) |
| 267 | epochPath := filepath.Join(config.SoongOutDir(), epochFile) |
| 268 | if exists, err := fileExists(epochPath); err != nil { |
| 269 | ctx.Fatalf("failed to check if bootstrap epoch file %q exists: %q", epochPath, err) |
| 270 | } else if !exists { |
| 271 | // The tree is out of date for the current epoch, delete files used by bootstrap |
| 272 | // and force the primary builder to rerun. |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 273 | soongNinjaFile := config.SoongNinjaFile() |
| 274 | os.Remove(soongNinjaFile) |
| 275 | for _, file := range blueprint.GetNinjaShardFiles(soongNinjaFile) { |
| 276 | if ok, _ := fileExists(file); ok { |
| 277 | os.Remove(file) |
| 278 | } |
| 279 | } |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 280 | for _, globFile := range bootstrapGlobFileList(config) { |
| 281 | os.Remove(globFile) |
| 282 | } |
| 283 | |
| 284 | // Mark the tree as up to date with the current epoch by writing the epoch marker file. |
| 285 | writeEmptyFile(ctx, epochPath) |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | func bootstrapGlobFileList(config Config) []string { |
| 290 | return []string{ |
Jeongik Cha | ccf3700 | 2023-08-04 01:46:32 +0900 | [diff] [blame] | 291 | config.NamedGlobFile(getGlobPathName(config)), |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 292 | config.NamedGlobFile(jsonModuleGraphTag), |
| 293 | config.NamedGlobFile(queryviewTag), |
| 294 | config.NamedGlobFile(soongDocsTag), |
| 295 | } |
| 296 | } |
| 297 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 298 | func bootstrapBlueprint(ctx Context, config Config) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 299 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 300 | defer ctx.EndTrace() |
| 301 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 302 | // Clean up some files for incremental builds across incompatible changes. |
| 303 | bootstrapEpochCleanup(ctx, config) |
| 304 | |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 305 | baseArgs := []string{"--soong_variables", config.SoongVarsFile()} |
| 306 | |
| 307 | mainSoongBuildExtraArgs := append(baseArgs, "-o", config.SoongNinjaFile()) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 308 | if config.EmptyNinjaFile() { |
| 309 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file") |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 310 | } |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 311 | if config.buildFromSourceStub { |
| 312 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--build-from-source-stub") |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 313 | } |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 314 | if config.ensureAllowlistIntegrity { |
| 315 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--ensure-allowlist-integrity") |
| 316 | } |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame] | 317 | if config.incrementalBuildActions { |
| 318 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--incremental-build-actions") |
| 319 | } |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 320 | |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 321 | queryviewDir := filepath.Join(config.SoongOutDir(), "queryview") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 322 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 323 | pbfs := []PrimaryBuilderFactory{ |
| 324 | { |
| 325 | name: soongBuildTag, |
| 326 | description: fmt.Sprintf("analyzing Android.bp files and generating ninja file at %s", config.SoongNinjaFile()), |
| 327 | config: config, |
| 328 | output: config.SoongNinjaFile(), |
| 329 | specificArgs: mainSoongBuildExtraArgs, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 330 | }, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 331 | { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 332 | name: jsonModuleGraphTag, |
| 333 | description: fmt.Sprintf("generating the Soong module graph at %s", config.ModuleGraphFile()), |
| 334 | config: config, |
| 335 | output: config.ModuleGraphFile(), |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 336 | specificArgs: append(baseArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 337 | "--module_graph_file", config.ModuleGraphFile(), |
| 338 | "--module_actions_file", config.ModuleActionsFile(), |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 339 | ), |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 340 | }, |
| 341 | { |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 342 | name: queryviewTag, |
| 343 | description: fmt.Sprintf("generating the Soong module graph as a Bazel workspace at %s", queryviewDir), |
| 344 | config: config, |
| 345 | output: config.QueryviewMarkerFile(), |
| 346 | specificArgs: append(baseArgs, |
| 347 | "--bazel_queryview_dir", queryviewDir, |
| 348 | ), |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 349 | }, |
| 350 | { |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 351 | name: soongDocsTag, |
| 352 | description: fmt.Sprintf("generating Soong docs at %s", config.SoongDocsHtml()), |
| 353 | config: config, |
| 354 | output: config.SoongDocsHtml(), |
| 355 | specificArgs: append(baseArgs, |
| 356 | "--soong_docs", config.SoongDocsHtml(), |
| 357 | ), |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 358 | }, |
| 359 | } |
| 360 | |
| 361 | // Figure out which invocations will be run under the debugger: |
| 362 | // * SOONG_DELVE if set specifies listening port |
| 363 | // * SOONG_DELVE_STEPS if set specifies specific invocations to be debugged, otherwise all are |
| 364 | debuggedInvocations := make(map[string]bool) |
| 365 | delvePort := os.Getenv("SOONG_DELVE") |
| 366 | if delvePort != "" { |
| 367 | if steps := os.Getenv("SOONG_DELVE_STEPS"); steps != "" { |
| 368 | var validSteps []string |
| 369 | for _, pbf := range pbfs { |
| 370 | debuggedInvocations[pbf.name] = false |
| 371 | validSteps = append(validSteps, pbf.name) |
| 372 | |
| 373 | } |
| 374 | for _, step := range strings.Split(steps, ",") { |
| 375 | if _, ok := debuggedInvocations[step]; ok { |
| 376 | debuggedInvocations[step] = true |
| 377 | } else { |
| 378 | ctx.Fatalf("SOONG_DELVE_STEPS contains unknown soong_build step %s\n"+ |
| 379 | "Valid steps are %v", step, validSteps) |
| 380 | } |
| 381 | } |
| 382 | } else { |
| 383 | // SOONG_DELVE_STEPS is not set, run all steps in the debugger |
| 384 | for _, pbf := range pbfs { |
| 385 | debuggedInvocations[pbf.name] = true |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | var invocations []bootstrap.PrimaryBuilderInvocation |
| 391 | for _, pbf := range pbfs { |
| 392 | if debuggedInvocations[pbf.name] { |
| 393 | pbf.debugPort = delvePort |
| 394 | } |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 395 | pbi := pbf.primaryBuilderInvocation(config) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 396 | invocations = append(invocations, pbi) |
| 397 | } |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 398 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 399 | blueprintArgs := bootstrap.Args{ |
| 400 | ModuleListFile: filepath.Join(config.FileListDir(), "Android.bp.list"), |
| 401 | OutFile: shared.JoinPath(config.SoongOutDir(), "bootstrap.ninja"), |
| 402 | EmptyNinjaFile: false, |
| 403 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 404 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 405 | blueprintCtx := blueprint.NewContext() |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 406 | blueprintCtx.AddSourceRootDirs(config.GetSourceRootDirs()...) |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 407 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 408 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 409 | soongOutDir: config.SoongOutDir(), |
| 410 | toolDir: config.HostToolDir(), |
| 411 | outDir: config.OutDir(), |
| 412 | runGoTests: !config.skipSoongTests, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 413 | // 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] | 414 | debugCompilation: delvePort != "", |
| 415 | subninjas: bootstrapGlobFileList(config), |
| 416 | primaryBuilderInvocations: invocations, |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 417 | } |
| 418 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 419 | // The glob ninja files are generated during the main build phase. However, the |
| 420 | // primary buildifer invocation depends on all of its glob files, even before |
| 421 | // it's been run. Generate a "empty" glob ninja file on the first run, |
| 422 | // so that the files can be there to satisfy the dependency. |
| 423 | for _, pb := range pbfs { |
| 424 | globPathName := getGlobPathNameFromPrimaryBuilderFactory(config, pb) |
| 425 | globNinjaFile := config.NamedGlobFile(globPathName) |
| 426 | if _, err := os.Stat(globNinjaFile); os.IsNotExist(err) { |
| 427 | err := bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{ |
| 428 | GlobLister: func() pathtools.MultipleGlobResults { return nil }, |
| 429 | GlobFile: globNinjaFile, |
| 430 | GlobDir: bootstrap.GlobDirectory(config.SoongOutDir(), globPathName), |
| 431 | SrcDir: ".", |
| 432 | }, blueprintConfig) |
| 433 | if err != nil { |
| 434 | ctx.Fatal(err) |
| 435 | } |
| 436 | } else if err != nil { |
| 437 | ctx.Fatal(err) |
| 438 | } |
| 439 | } |
| 440 | |
usta | 5bb4a5d | 2022-08-24 12:53:46 -0400 | [diff] [blame] | 441 | // since `bootstrap.ninja` is regenerated unconditionally, we ignore the deps, i.e. little |
| 442 | // reason to write a `bootstrap.ninja.d` file |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 443 | _, err := bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig) |
| 444 | if err != nil { |
| 445 | ctx.Fatal(err) |
| 446 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 447 | } |
| 448 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 449 | func checkEnvironmentFile(ctx Context, currentEnv *Environment, envFile string) { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 450 | getenv := func(k string) string { |
| 451 | v, _ := currentEnv.Get(k) |
| 452 | return v |
| 453 | } |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 454 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 455 | // Log the changed environment variables to ChangedEnvironmentVariable field |
| 456 | if stale, changedEnvironmentVariableList, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 457 | for _, changedEnvironmentVariable := range changedEnvironmentVariableList { |
| 458 | ctx.Metrics.AddChangedEnvironmentVariable(changedEnvironmentVariable) |
| 459 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 460 | os.Remove(envFile) |
| 461 | } |
| 462 | } |
| 463 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 464 | func updateSymlinks(ctx Context, dir, prevCWD, cwd string) error { |
| 465 | defer symlinkWg.Done() |
| 466 | |
| 467 | visit := func(path string, d fs.DirEntry, err error) error { |
| 468 | if d.IsDir() && path != dir { |
| 469 | symlinkWg.Add(1) |
| 470 | go updateSymlinks(ctx, path, prevCWD, cwd) |
| 471 | return filepath.SkipDir |
| 472 | } |
| 473 | f, err := d.Info() |
| 474 | if err != nil { |
| 475 | return err |
| 476 | } |
| 477 | // If the file is not a symlink, we don't have to update it. |
| 478 | if f.Mode()&os.ModeSymlink != os.ModeSymlink { |
| 479 | return nil |
| 480 | } |
| 481 | |
| 482 | atomic.AddUint32(&numFound, 1) |
| 483 | target, err := os.Readlink(path) |
| 484 | if err != nil { |
| 485 | return err |
| 486 | } |
| 487 | if strings.HasPrefix(target, prevCWD) && |
| 488 | (len(target) == len(prevCWD) || target[len(prevCWD)] == '/') { |
| 489 | target = filepath.Join(cwd, target[len(prevCWD):]) |
| 490 | if err := os.Remove(path); err != nil { |
| 491 | return err |
| 492 | } |
| 493 | if err := os.Symlink(target, path); err != nil { |
| 494 | return err |
| 495 | } |
| 496 | atomic.AddUint32(&numUpdated, 1) |
| 497 | } |
| 498 | return nil |
| 499 | } |
| 500 | |
| 501 | if err := filepath.WalkDir(dir, visit); err != nil { |
| 502 | return err |
| 503 | } |
| 504 | return nil |
| 505 | } |
| 506 | |
| 507 | func fixOutDirSymlinks(ctx Context, config Config, outDir string) error { |
| 508 | cwd, err := os.Getwd() |
| 509 | if err != nil { |
| 510 | return err |
| 511 | } |
| 512 | |
| 513 | // Record the .top as the very last thing in the function. |
| 514 | tf := filepath.Join(outDir, ".top") |
| 515 | defer func() { |
| 516 | if err := os.WriteFile(tf, []byte(cwd), 0644); err != nil { |
| 517 | fmt.Fprintf(os.Stderr, fmt.Sprintf("Unable to log CWD: %v", err)) |
| 518 | } |
| 519 | }() |
| 520 | |
| 521 | // Find the previous working directory if it was recorded. |
| 522 | var prevCWD string |
| 523 | pcwd, err := os.ReadFile(tf) |
| 524 | if err != nil { |
| 525 | if os.IsNotExist(err) { |
| 526 | // No previous working directory recorded, nothing to do. |
| 527 | return nil |
| 528 | } |
| 529 | return err |
| 530 | } |
| 531 | prevCWD = strings.Trim(string(pcwd), "\n") |
| 532 | |
| 533 | if prevCWD == cwd { |
| 534 | // We are in the same source dir, nothing to update. |
| 535 | return nil |
| 536 | } |
| 537 | |
| 538 | symlinkWg.Add(1) |
| 539 | if err := updateSymlinks(ctx, outDir, prevCWD, cwd); err != nil { |
| 540 | return err |
| 541 | } |
| 542 | symlinkWg.Wait() |
| 543 | ctx.Println(fmt.Sprintf("Updated %d/%d symlinks in dir %v", numUpdated, numFound, outDir)) |
| 544 | return nil |
| 545 | } |
| 546 | |
| 547 | func migrateOutputSymlinks(ctx Context, config Config) error { |
| 548 | // Figure out the real out directory ("out" could be a symlink). |
| 549 | outDir := config.OutDir() |
| 550 | s, err := os.Lstat(outDir) |
| 551 | if err != nil { |
| 552 | if os.IsNotExist(err) { |
| 553 | // No out dir exists, no symlinks to migrate. |
| 554 | return nil |
| 555 | } |
| 556 | return err |
| 557 | } |
| 558 | if s.Mode()&os.ModeSymlink == os.ModeSymlink { |
| 559 | target, err := filepath.EvalSymlinks(outDir) |
| 560 | if err != nil { |
| 561 | return err |
| 562 | } |
| 563 | outDir = target |
| 564 | } |
| 565 | return fixOutDirSymlinks(ctx, config, outDir) |
| 566 | } |
| 567 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 568 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 569 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 570 | defer ctx.EndTrace() |
| 571 | |
Kousik Kumar | ca390b2 | 2023-10-04 04:14:28 +0000 | [diff] [blame] | 572 | if err := migrateOutputSymlinks(ctx, config); err != nil { |
| 573 | ctx.Fatalf("failed to migrate output directory to current TOP dir: %v", err) |
| 574 | } |
| 575 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 576 | // We have two environment files: .available is the one with every variable, |
| 577 | // .used with the ones that were actually used. The latter is used to |
| 578 | // determine whether Soong needs to be re-run since why re-run it if only |
| 579 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 580 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 581 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 582 | // 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] | 583 | bootstrapBlueprint(ctx, config) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 584 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 585 | soongBuildEnv := config.Environment().Copy() |
| 586 | 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] | 587 | soongBuildEnv.Set("LOG_DIR", config.LogsDir()) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 588 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 589 | // For Soong bootstrapping tests |
| 590 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 591 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 592 | } |
| 593 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 594 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 595 | if err != nil { |
| 596 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 597 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 598 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 599 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 600 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 601 | defer ctx.EndTrace() |
| 602 | |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 603 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongBuildTag)) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 604 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 605 | if config.JsonModuleGraph() { |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 606 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | if config.Queryview() { |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 610 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(queryviewTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | if config.SoongDocs() { |
Jason Wu | 2520f5e | 2023-05-30 19:45:36 -0400 | [diff] [blame] | 614 | checkEnvironmentFile(ctx, soongBuildEnv, config.UsedEnvFile(soongDocsTag)) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 615 | } |
| 616 | }() |
| 617 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 618 | runMicrofactory(ctx, config, "bpglob", "github.com/google/blueprint/bootstrap/bpglob", |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 619 | map[string]string{"github.com/google/blueprint": "build/blueprint"}) |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 620 | |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 621 | ninja := func(targets ...string) { |
| 622 | ctx.BeginTrace(metrics.RunSoong, "bootstrap") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 623 | defer ctx.EndTrace() |
| 624 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 625 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 626 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 627 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 628 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 629 | ninjaArgs := []string{ |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 630 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 631 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 632 | "-o", "usesphonyoutputs=yes", |
| 633 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 634 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 635 | "-w", "outputdir=err", |
| 636 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 637 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 638 | "--frontend_file", fifo, |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 639 | "-f", filepath.Join(config.SoongOutDir(), "bootstrap.ninja"), |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 640 | } |
| 641 | |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 642 | if extra, ok := config.Environment().Get("SOONG_UI_NINJA_ARGS"); ok { |
| 643 | ctx.Printf(`CAUTION: arguments in $SOONG_UI_NINJA_ARGS=%q, e.g. "-n", can make soong_build FAIL or INCORRECT`, extra) |
| 644 | ninjaArgs = append(ninjaArgs, strings.Fields(extra)...) |
| 645 | } |
| 646 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 647 | ninjaArgs = append(ninjaArgs, targets...) |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 648 | cmd := Command(ctx, config, "soong bootstrap", |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 649 | config.PrebuiltBuildTool("ninja"), ninjaArgs...) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 650 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 651 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 652 | |
| 653 | // This is currently how the command line to invoke soong_build finds the |
| 654 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 655 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 656 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 657 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 658 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 659 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 660 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 661 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 662 | targets := make([]string, 0, 0) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 663 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 664 | if config.JsonModuleGraph() { |
| 665 | targets = append(targets, config.ModuleGraphFile()) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 666 | } |
| 667 | |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 668 | if config.Queryview() { |
| 669 | targets = append(targets, config.QueryviewMarkerFile()) |
| 670 | } |
| 671 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 672 | if config.SoongDocs() { |
| 673 | targets = append(targets, config.SoongDocsHtml()) |
| 674 | } |
| 675 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 676 | if config.SoongBuildInvocationNeeded() { |
| 677 | // 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] | 678 | targets = append(targets, config.SoongNinjaFile()) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 679 | } |
| 680 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 681 | beforeSoongTimestamp := time.Now() |
| 682 | |
usta | 49012ee | 2023-05-22 16:33:27 -0400 | [diff] [blame] | 683 | ninja(targets...) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 684 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 685 | loadSoongBuildMetrics(ctx, config, beforeSoongTimestamp) |
| 686 | |
Yu Liu | 1b2ddc8 | 2024-05-15 19:28:56 +0000 | [diff] [blame] | 687 | soongNinjaFile := config.SoongNinjaFile() |
| 688 | distGzipFile(ctx, config, soongNinjaFile, "soong") |
| 689 | for _, file := range blueprint.GetNinjaShardFiles(soongNinjaFile) { |
| 690 | if ok, _ := fileExists(file); ok { |
| 691 | distGzipFile(ctx, config, file, "soong") |
| 692 | } |
| 693 | } |
Jihoon Kang | 9f4f8a3 | 2022-08-16 00:57:30 +0000 | [diff] [blame] | 694 | distFile(ctx, config, config.SoongVarsFile(), "soong") |
Inseob Kim | 58c802f | 2024-06-11 10:59:00 +0900 | [diff] [blame] | 695 | distFile(ctx, config, config.SoongExtraVarsFile(), "soong") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 696 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 697 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 698 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 699 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 700 | } |
| 701 | |
Rob Seymour | 33cd10d | 2022-03-02 23:10:25 +0000 | [diff] [blame] | 702 | if config.JsonModuleGraph() { |
| 703 | distGzipFile(ctx, config, config.ModuleGraphFile(), "soong") |
| 704 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 707 | // loadSoongBuildMetrics reads out/soong_build_metrics.pb if it was generated by soong_build and copies the |
| 708 | // events stored in it into the soong_ui trace to provide introspection into how long the different phases of |
| 709 | // soong_build are taking. |
| 710 | func loadSoongBuildMetrics(ctx Context, config Config, oldTimestamp time.Time) { |
| 711 | soongBuildMetricsFile := config.SoongBuildMetrics() |
| 712 | if metricsStat, err := os.Stat(soongBuildMetricsFile); err != nil { |
| 713 | ctx.Verbosef("Failed to stat %s: %s", soongBuildMetricsFile, err) |
| 714 | return |
| 715 | } else if !metricsStat.ModTime().After(oldTimestamp) { |
| 716 | ctx.Verbosef("%s timestamp not later after running soong, expected %s > %s", |
| 717 | soongBuildMetricsFile, metricsStat.ModTime(), oldTimestamp) |
| 718 | return |
| 719 | } |
| 720 | |
Colin Cross | b67b061 | 2023-10-31 10:02:45 -0700 | [diff] [blame] | 721 | metricsData, err := os.ReadFile(soongBuildMetricsFile) |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 722 | if err != nil { |
| 723 | ctx.Verbosef("Failed to read %s: %s", soongBuildMetricsFile, err) |
| 724 | return |
| 725 | } |
| 726 | |
| 727 | soongBuildMetrics := metrics_proto.SoongBuildMetrics{} |
| 728 | err = proto.Unmarshal(metricsData, &soongBuildMetrics) |
| 729 | if err != nil { |
| 730 | ctx.Verbosef("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 731 | return |
| 732 | } |
| 733 | for _, event := range soongBuildMetrics.Events { |
| 734 | desc := event.GetDescription() |
| 735 | if dot := strings.LastIndexByte(desc, '.'); dot >= 0 { |
| 736 | desc = desc[dot+1:] |
| 737 | } |
| 738 | ctx.Tracer.Complete(desc, ctx.Thread, |
| 739 | event.GetStartTime(), event.GetStartTime()+event.GetRealTime()) |
| 740 | } |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 741 | for _, event := range soongBuildMetrics.PerfCounters { |
| 742 | timestamp := event.GetTime() |
| 743 | for _, group := range event.Groups { |
| 744 | counters := make([]tracer.Counter, 0, len(group.Counters)) |
| 745 | for _, counter := range group.Counters { |
| 746 | counters = append(counters, tracer.Counter{ |
| 747 | Name: counter.GetName(), |
| 748 | Value: counter.GetValue(), |
| 749 | }) |
| 750 | } |
| 751 | ctx.Tracer.CountersAtTime(group.GetName(), ctx.Thread, timestamp, counters) |
| 752 | } |
| 753 | } |
Colin Cross | aa9a273 | 2023-10-27 10:54:27 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 756 | 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] | 757 | ctx.BeginTrace(metrics.RunSoong, name) |
| 758 | defer ctx.EndTrace() |
| 759 | cfg := microfactory.Config{TrimPath: absPath(ctx, ".")} |
| 760 | for pkgPrefix, pathPrefix := range mapping { |
| 761 | cfg.Map(pkgPrefix, pathPrefix) |
| 762 | } |
| 763 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 764 | exePath := filepath.Join(config.SoongOutDir(), name) |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 765 | dir := filepath.Dir(exePath) |
| 766 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 767 | ctx.Fatalf("cannot create %s: %s", dir, err) |
| 768 | } |
| 769 | if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil { |
| 770 | ctx.Fatalf("failed to build %s: %s", name, err) |
| 771 | } |
| 772 | } |