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