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" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 19 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 21 | "strconv" |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 22 | "strings" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 23 | |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 24 | "android/soong/bazel" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 25 | "android/soong/ui/metrics" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 26 | "android/soong/ui/status" |
| 27 | |
| 28 | "android/soong/shared" |
| 29 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 30 | "github.com/google/blueprint" |
| 31 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 32 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 35 | const ( |
| 36 | availableEnvFile = "soong.environment.available" |
| 37 | usedEnvFile = "soong.environment.used" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 38 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 39 | soongBuildTag = "build" |
| 40 | bp2buildFilesTag = "bp2build_files" |
| 41 | bp2buildWorkspaceTag = "bp2build_workspace" |
| 42 | jsonModuleGraphTag = "modulegraph" |
| 43 | queryviewTag = "queryview" |
| 44 | apiBp2buildTag = "api_bp2build" |
| 45 | soongDocsTag = "soong_docs" |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 46 | |
| 47 | // bootstrapEpoch is used to determine if an incremental build is incompatible with the current |
| 48 | // version of bootstrap and needs cleaning before continuing the build. Increment this for |
| 49 | // incompatible changes, for example when moving the location of the bpglob binary that is |
| 50 | // 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] | 51 | bootstrapEpoch = 1 |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 52 | ) |
| 53 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 54 | func writeEnvironmentFile(_ Context, envFile string, envDeps map[string]string) error { |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 55 | data, err := shared.EnvFileContents(envDeps) |
| 56 | if err != nil { |
| 57 | return err |
| 58 | } |
| 59 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 60 | return os.WriteFile(envFile, data, 0644) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 63 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 64 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 65 | // However, the execution of <builddir>/build.ninja happens later in |
| 66 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 67 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 68 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 69 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 70 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 71 | // 1. We use "Microfactory", a simple tool to compile Go code, to build |
| 72 | // first itself, then soong_ui from soong_ui.bash. This binary contains |
| 73 | // parts of soong_build that are needed to build itself. |
| 74 | // 2. This simplified version of soong_build then reads the Blueprint files |
| 75 | // that describe itself and emits .bootstrap/build.ninja that describes |
| 76 | // how to build its full version and use that to produce the final Ninja |
| 77 | // file Soong emits. |
| 78 | // 3. soong_ui executes .bootstrap/build.ninja |
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 | // (After this, Kati is executed to parse the Makefiles, but that's not part of |
| 81 | // bootstrapping Soong) |
| 82 | |
| 83 | // A tiny struct used to tell Blueprint that it's in bootstrap mode. It would |
| 84 | // probably be nicer to use a flag in bootstrap.Args instead. |
| 85 | type BlueprintConfig struct { |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 86 | toolDir string |
| 87 | soongOutDir string |
| 88 | outDir string |
| 89 | runGoTests bool |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 90 | debugCompilation bool |
| 91 | subninjas []string |
| 92 | primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 95 | func (c BlueprintConfig) HostToolDir() string { |
| 96 | return c.toolDir |
| 97 | } |
| 98 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 99 | func (c BlueprintConfig) SoongOutDir() string { |
| 100 | return c.soongOutDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 103 | func (c BlueprintConfig) OutDir() string { |
| 104 | return c.outDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 107 | func (c BlueprintConfig) RunGoTests() bool { |
| 108 | return c.runGoTests |
| 109 | } |
| 110 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 111 | func (c BlueprintConfig) DebugCompilation() bool { |
| 112 | return c.debugCompilation |
| 113 | } |
| 114 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 115 | func (c BlueprintConfig) Subninjas() []string { |
| 116 | return c.subninjas |
| 117 | } |
| 118 | |
| 119 | func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation { |
| 120 | return c.primaryBuilderInvocations |
| 121 | } |
| 122 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 123 | func environmentArgs(config Config, tag string) []string { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 124 | return []string{ |
| 125 | "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile), |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 126 | "--used_env", config.UsedEnvFile(tag), |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 127 | } |
| 128 | } |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 129 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 130 | func writeEmptyFile(ctx Context, path string) { |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 131 | err := os.MkdirAll(filepath.Dir(path), 0777) |
| 132 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 133 | 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] | 134 | } |
| 135 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 136 | if exists, err := fileExists(path); err != nil { |
| 137 | ctx.Fatalf("Failed to check if file '%s' exists: %s", path, err) |
| 138 | } else if !exists { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 139 | err = os.WriteFile(path, nil, 0666) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 140 | if err != nil { |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 141 | ctx.Fatalf("Failed to create empty file '%s': %s", path, err) |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 146 | func fileExists(path string) (bool, error) { |
| 147 | if _, err := os.Stat(path); os.IsNotExist(err) { |
| 148 | return false, nil |
| 149 | } else if err != nil { |
| 150 | return false, err |
| 151 | } |
| 152 | return true, nil |
| 153 | } |
| 154 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 155 | type PrimaryBuilderFactory struct { |
| 156 | name string |
| 157 | description string |
| 158 | config Config |
| 159 | output string |
| 160 | specificArgs []string |
| 161 | debugPort string |
| 162 | } |
| 163 | |
| 164 | func (pb PrimaryBuilderFactory) primaryBuilderInvocation() bootstrap.PrimaryBuilderInvocation { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 165 | commonArgs := make([]string, 0, 0) |
| 166 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 167 | if !pb.config.skipSoongTests { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 168 | commonArgs = append(commonArgs, "-t") |
| 169 | } |
| 170 | |
LaMont Jones | 80f7035 | 2023-03-20 19:58:25 +0000 | [diff] [blame] | 171 | if pb.config.multitreeBuild { |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 172 | commonArgs = append(commonArgs, "--multitree-build") |
| 173 | } |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 174 | if pb.config.buildFromTextStub { |
| 175 | commonArgs = append(commonArgs, "--build-from-text-stub") |
| 176 | } |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 177 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 178 | 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] | 179 | invocationEnv := make(map[string]string) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 180 | if pb.debugPort != "" { |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 181 | //debug mode |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 182 | commonArgs = append(commonArgs, "--delve_listen", pb.debugPort, |
| 183 | "--delve_path", shared.ResolveDelveBinary()) |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 184 | // GODEBUG=asyncpreemptoff=1 disables the preemption of goroutines. This |
| 185 | // is useful because the preemption happens by sending SIGURG to the OS |
| 186 | // thread hosting the goroutine in question and each signal results in |
| 187 | // work that needs to be done by Delve; it uses ptrace to debug the Go |
| 188 | // process and the tracer process must deal with every signal (it is not |
| 189 | // possible to selectively ignore SIGURG). This makes debugging slower, |
| 190 | // sometimes by an order of magnitude depending on luck. |
| 191 | // The original reason for adding async preemption to Go is here: |
| 192 | // https://github.com/golang/proposal/blob/master/design/24543-non-cooperative-preemption.md |
| 193 | invocationEnv["GODEBUG"] = "asyncpreemptoff=1" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 194 | } |
| 195 | |
usta | fb67fd1 | 2022-08-19 19:26:00 -0400 | [diff] [blame] | 196 | var allArgs []string |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 197 | allArgs = append(allArgs, pb.specificArgs...) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 198 | allArgs = append(allArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 199 | "--globListDir", pb.name, |
| 200 | "--globFile", pb.config.NamedGlobFile(pb.name)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 201 | |
| 202 | allArgs = append(allArgs, commonArgs...) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 203 | allArgs = append(allArgs, environmentArgs(pb.config, pb.name)...) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 204 | if profileCpu := os.Getenv("SOONG_PROFILE_CPU"); profileCpu != "" { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 205 | allArgs = append(allArgs, "--cpuprofile", profileCpu+"."+pb.name) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 206 | } |
| 207 | if profileMem := os.Getenv("SOONG_PROFILE_MEM"); profileMem != "" { |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 208 | allArgs = append(allArgs, "--memprofile", profileMem+"."+pb.name) |
Sasha Smundak | faa97b7 | 2022-11-18 15:32:49 -0800 | [diff] [blame] | 209 | } |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 210 | allArgs = append(allArgs, "Android.bp") |
| 211 | |
| 212 | return bootstrap.PrimaryBuilderInvocation{ |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 213 | Inputs: []string{"Android.bp"}, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 214 | Outputs: []string{pb.output}, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 215 | Args: allArgs, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 216 | Description: pb.description, |
Lukacs T. Berki | 2fad341 | 2022-01-04 14:40:13 +0100 | [diff] [blame] | 217 | // NB: Changing the value of this environment variable will not result in a |
| 218 | // rebuild. The bootstrap Ninja file will change, but apparently Ninja does |
| 219 | // not consider changing the pool specified in a statement a change that's |
| 220 | // worth rebuilding for. |
| 221 | Console: os.Getenv("SOONG_UNBUFFERED_OUTPUT") == "1", |
Lukacs T. Berki | 1364427 | 2022-01-05 10:29:56 +0100 | [diff] [blame] | 222 | Env: invocationEnv, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 226 | // bootstrapEpochCleanup deletes files used by bootstrap during incremental builds across |
| 227 | // incompatible changes. Incompatible changes are marked by incrementing the bootstrapEpoch |
| 228 | // constant. A tree is considered out of date for the current epoch of the |
| 229 | // .soong.bootstrap.epoch.<epoch> file doesn't exist. |
| 230 | func bootstrapEpochCleanup(ctx Context, config Config) { |
| 231 | epochFile := fmt.Sprintf(".soong.bootstrap.epoch.%d", bootstrapEpoch) |
| 232 | epochPath := filepath.Join(config.SoongOutDir(), epochFile) |
| 233 | if exists, err := fileExists(epochPath); err != nil { |
| 234 | ctx.Fatalf("failed to check if bootstrap epoch file %q exists: %q", epochPath, err) |
| 235 | } else if !exists { |
| 236 | // The tree is out of date for the current epoch, delete files used by bootstrap |
| 237 | // and force the primary builder to rerun. |
| 238 | os.Remove(filepath.Join(config.SoongOutDir(), "build.ninja")) |
| 239 | for _, globFile := range bootstrapGlobFileList(config) { |
| 240 | os.Remove(globFile) |
| 241 | } |
| 242 | |
| 243 | // Mark the tree as up to date with the current epoch by writing the epoch marker file. |
| 244 | writeEmptyFile(ctx, epochPath) |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | func bootstrapGlobFileList(config Config) []string { |
| 249 | return []string{ |
| 250 | config.NamedGlobFile(soongBuildTag), |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 251 | config.NamedGlobFile(bp2buildFilesTag), |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 252 | config.NamedGlobFile(jsonModuleGraphTag), |
| 253 | config.NamedGlobFile(queryviewTag), |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 254 | config.NamedGlobFile(apiBp2buildTag), |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 255 | config.NamedGlobFile(soongDocsTag), |
| 256 | } |
| 257 | } |
| 258 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 259 | func bootstrapBlueprint(ctx Context, config Config) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 260 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 261 | defer ctx.EndTrace() |
| 262 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 263 | // Clean up some files for incremental builds across incompatible changes. |
| 264 | bootstrapEpochCleanup(ctx, config) |
| 265 | |
Cole Faust | 65f298c | 2021-10-28 16:05:13 -0700 | [diff] [blame] | 266 | mainSoongBuildExtraArgs := []string{"-o", config.SoongNinjaFile()} |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 267 | if config.EmptyNinjaFile() { |
| 268 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file") |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 269 | } |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 270 | if config.bazelProdMode { |
| 271 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--bazel-mode") |
| 272 | } |
| 273 | if config.bazelDevMode { |
| 274 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--bazel-mode-dev") |
| 275 | } |
MarkDacek | b78465d | 2022-10-18 20:10:16 +0000 | [diff] [blame] | 276 | if config.bazelStagingMode { |
| 277 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--bazel-mode-staging") |
| 278 | } |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 279 | if config.IsPersistentBazelEnabled() { |
| 280 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--use-bazel-proxy") |
| 281 | } |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 282 | if len(config.bazelForceEnabledModules) > 0 { |
| 283 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--bazel-force-enabled-modules="+config.bazelForceEnabledModules) |
| 284 | } |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 285 | if config.MultitreeBuild() { |
| 286 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--multitree-build") |
| 287 | } |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 288 | if config.buildFromTextStub { |
| 289 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--build-from-text-stub") |
| 290 | } |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 291 | if config.ensureAllowlistIntegrity { |
| 292 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--ensure-allowlist-integrity") |
| 293 | } |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 294 | |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 295 | queryviewDir := filepath.Join(config.SoongOutDir(), "queryview") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 296 | // The BUILD files will be generated in out/soong/.api_bp2build (no symlinks to src files) |
| 297 | // The final workspace will be generated in out/soong/api_bp2build |
| 298 | apiBp2buildDir := filepath.Join(config.SoongOutDir(), ".api_bp2build") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 299 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 300 | pbfs := []PrimaryBuilderFactory{ |
| 301 | { |
| 302 | name: soongBuildTag, |
| 303 | description: fmt.Sprintf("analyzing Android.bp files and generating ninja file at %s", config.SoongNinjaFile()), |
| 304 | config: config, |
| 305 | output: config.SoongNinjaFile(), |
| 306 | specificArgs: mainSoongBuildExtraArgs, |
Jingwen Chen | 78fd87f | 2021-12-06 13:27:43 +0000 | [diff] [blame] | 307 | }, |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 308 | { |
| 309 | name: bp2buildFilesTag, |
| 310 | description: fmt.Sprintf("converting Android.bp files to BUILD files at %s/bp2build", config.SoongOutDir()), |
| 311 | config: config, |
| 312 | output: config.Bp2BuildFilesMarkerFile(), |
| 313 | specificArgs: []string{"--bp2build_marker", config.Bp2BuildFilesMarkerFile()}, |
| 314 | }, |
| 315 | { |
| 316 | name: bp2buildWorkspaceTag, |
| 317 | description: "Creating Bazel symlink forest", |
| 318 | config: config, |
| 319 | output: config.Bp2BuildWorkspaceMarkerFile(), |
| 320 | specificArgs: []string{"--symlink_forest_marker", config.Bp2BuildWorkspaceMarkerFile()}, |
| 321 | }, |
| 322 | { |
| 323 | name: jsonModuleGraphTag, |
| 324 | description: fmt.Sprintf("generating the Soong module graph at %s", config.ModuleGraphFile()), |
| 325 | config: config, |
| 326 | output: config.ModuleGraphFile(), |
| 327 | specificArgs: []string{ |
| 328 | "--module_graph_file", config.ModuleGraphFile(), |
| 329 | "--module_actions_file", config.ModuleActionsFile(), |
| 330 | }, |
| 331 | }, |
| 332 | { |
| 333 | name: queryviewTag, |
| 334 | description: fmt.Sprintf("generating the Soong module graph as a Bazel workspace at %s", queryviewDir), |
| 335 | config: config, |
| 336 | output: config.QueryviewMarkerFile(), |
| 337 | specificArgs: []string{"--bazel_queryview_dir", queryviewDir}, |
| 338 | }, |
| 339 | { |
| 340 | name: apiBp2buildTag, |
| 341 | description: fmt.Sprintf("generating BUILD files for API contributions at %s", apiBp2buildDir), |
| 342 | config: config, |
| 343 | output: config.ApiBp2buildMarkerFile(), |
| 344 | specificArgs: []string{"--bazel_api_bp2build_dir", apiBp2buildDir}, |
| 345 | }, |
| 346 | { |
| 347 | name: soongDocsTag, |
| 348 | description: fmt.Sprintf("generating Soong docs at %s", config.SoongDocsHtml()), |
| 349 | config: config, |
| 350 | output: config.SoongDocsHtml(), |
| 351 | specificArgs: []string{"--soong_docs", config.SoongDocsHtml()}, |
| 352 | }, |
| 353 | } |
| 354 | |
| 355 | // Figure out which invocations will be run under the debugger: |
| 356 | // * SOONG_DELVE if set specifies listening port |
| 357 | // * SOONG_DELVE_STEPS if set specifies specific invocations to be debugged, otherwise all are |
| 358 | debuggedInvocations := make(map[string]bool) |
| 359 | delvePort := os.Getenv("SOONG_DELVE") |
| 360 | if delvePort != "" { |
| 361 | if steps := os.Getenv("SOONG_DELVE_STEPS"); steps != "" { |
| 362 | var validSteps []string |
| 363 | for _, pbf := range pbfs { |
| 364 | debuggedInvocations[pbf.name] = false |
| 365 | validSteps = append(validSteps, pbf.name) |
| 366 | |
| 367 | } |
| 368 | for _, step := range strings.Split(steps, ",") { |
| 369 | if _, ok := debuggedInvocations[step]; ok { |
| 370 | debuggedInvocations[step] = true |
| 371 | } else { |
| 372 | ctx.Fatalf("SOONG_DELVE_STEPS contains unknown soong_build step %s\n"+ |
| 373 | "Valid steps are %v", step, validSteps) |
| 374 | } |
| 375 | } |
| 376 | } else { |
| 377 | // SOONG_DELVE_STEPS is not set, run all steps in the debugger |
| 378 | for _, pbf := range pbfs { |
| 379 | debuggedInvocations[pbf.name] = true |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | var invocations []bootstrap.PrimaryBuilderInvocation |
| 385 | for _, pbf := range pbfs { |
| 386 | if debuggedInvocations[pbf.name] { |
| 387 | pbf.debugPort = delvePort |
| 388 | } |
| 389 | pbi := pbf.primaryBuilderInvocation() |
| 390 | // Some invocations require adjustment: |
| 391 | switch pbf.name { |
| 392 | case soongBuildTag: |
| 393 | if config.BazelBuildEnabled() { |
| 394 | // Mixed builds call Bazel from soong_build and they therefore need the |
| 395 | // Bazel workspace to be available. Make that so by adding a dependency on |
| 396 | // the bp2build marker file to the action that invokes soong_build . |
| 397 | pbi.OrderOnlyInputs = append(pbi.OrderOnlyInputs, config.Bp2BuildWorkspaceMarkerFile()) |
| 398 | } |
| 399 | case bp2buildWorkspaceTag: |
| 400 | pbi.Inputs = append(pbi.Inputs, |
| 401 | config.Bp2BuildFilesMarkerFile(), |
| 402 | filepath.Join(config.FileListDir(), "bazel.list")) |
Wei Li | 2c9e8d6 | 2023-05-05 01:07:15 -0700 | [diff] [blame] | 403 | case bp2buildFilesTag: |
| 404 | pbi.Inputs = append(pbi.Inputs, filepath.Join(config.FileListDir(), "METADATA.list")) |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 405 | } |
| 406 | invocations = append(invocations, pbi) |
| 407 | } |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 408 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 409 | // The glob .ninja files are subninja'd. However, they are generated during |
| 410 | // the build itself so we write an empty file if the file does not exist yet |
| 411 | // so that the subninja doesn't fail on clean builds |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 412 | for _, globFile := range bootstrapGlobFileList(config) { |
| 413 | writeEmptyFile(ctx, globFile) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 414 | } |
| 415 | |
Sasha Smundak | 4cbe83a | 2022-11-28 17:02:40 -0800 | [diff] [blame] | 416 | blueprintArgs := bootstrap.Args{ |
| 417 | ModuleListFile: filepath.Join(config.FileListDir(), "Android.bp.list"), |
| 418 | OutFile: shared.JoinPath(config.SoongOutDir(), "bootstrap.ninja"), |
| 419 | EmptyNinjaFile: false, |
| 420 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 421 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 422 | blueprintCtx := blueprint.NewContext() |
Spandan Das | c576383 | 2022-11-08 18:42:16 +0000 | [diff] [blame] | 423 | blueprintCtx.AddIncludeTags(config.GetIncludeTags()...) |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 424 | blueprintCtx.AddSourceRootDirs(config.GetSourceRootDirs()...) |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 425 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 426 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 427 | soongOutDir: config.SoongOutDir(), |
| 428 | toolDir: config.HostToolDir(), |
| 429 | outDir: config.OutDir(), |
| 430 | runGoTests: !config.skipSoongTests, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 431 | // 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] | 432 | debugCompilation: delvePort != "", |
| 433 | subninjas: bootstrapGlobFileList(config), |
| 434 | primaryBuilderInvocations: invocations, |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 435 | } |
| 436 | |
usta | 5bb4a5d | 2022-08-24 12:53:46 -0400 | [diff] [blame] | 437 | // since `bootstrap.ninja` is regenerated unconditionally, we ignore the deps, i.e. little |
| 438 | // reason to write a `bootstrap.ninja.d` file |
| 439 | _ = bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig) |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 442 | func checkEnvironmentFile(currentEnv *Environment, envFile string) { |
| 443 | getenv := func(k string) string { |
| 444 | v, _ := currentEnv.Get(k) |
| 445 | return v |
| 446 | } |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 447 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 448 | if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 449 | os.Remove(envFile) |
| 450 | } |
| 451 | } |
| 452 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 453 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 454 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 455 | defer ctx.EndTrace() |
| 456 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 457 | // We have two environment files: .available is the one with every variable, |
| 458 | // .used with the ones that were actually used. The latter is used to |
| 459 | // determine whether Soong needs to be re-run since why re-run it if only |
| 460 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 461 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 462 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 463 | // 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] | 464 | bootstrapBlueprint(ctx, config) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 465 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 466 | soongBuildEnv := config.Environment().Copy() |
| 467 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 468 | // For Bazel mixed builds. |
Joe Onorato | ba29f38 | 2022-10-24 06:38:11 -0700 | [diff] [blame] | 469 | soongBuildEnv.Set("BAZEL_PATH", "./build/bazel/bin/bazel") |
Chris Parsons | a9bef14 | 2022-09-28 15:07:46 -0400 | [diff] [blame] | 470 | // Bazel's HOME var is set to an output subdirectory which doesn't exist. This |
| 471 | // prevents Bazel from file I/O in the actual user HOME directory. |
| 472 | soongBuildEnv.Set("BAZEL_HOME", absPath(ctx, filepath.Join(config.BazelOutDir(), "bazelhome"))) |
Liz Kammer | 2af5ea8 | 2022-11-11 14:21:03 -0500 | [diff] [blame] | 473 | soongBuildEnv.Set("BAZEL_OUTPUT_BASE", config.bazelOutputBase()) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 474 | soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
| 475 | soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
Alex Márquez Pérez MuñÃz DÃaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 476 | soongBuildEnv.Set("LOG_DIR", config.LogsDir()) |
Jingwen Chen | 3b13b61 | 2022-10-17 12:14:26 +0000 | [diff] [blame] | 477 | soongBuildEnv.Set("BAZEL_DEPS_FILE", absPath(ctx, filepath.Join(config.BazelOutDir(), "bazel.list"))) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 478 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 479 | // For Soong bootstrapping tests |
| 480 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 481 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 482 | } |
| 483 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 484 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 485 | if err != nil { |
| 486 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 487 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 488 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 489 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 490 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 491 | defer ctx.EndTrace() |
| 492 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 493 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongBuildTag)) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 494 | |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 495 | if config.BazelBuildEnabled() || config.Bp2Build() { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 496 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(bp2buildFilesTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | if config.JsonModuleGraph() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 500 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | if config.Queryview() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 504 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(queryviewTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 505 | } |
| 506 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 507 | if config.ApiBp2build() { |
| 508 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(apiBp2buildTag)) |
| 509 | } |
| 510 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 511 | if config.SoongDocs() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 512 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongDocsTag)) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 513 | } |
| 514 | }() |
| 515 | |
Colin Cross | 9191df2 | 2021-10-29 13:11:32 -0700 | [diff] [blame] | 516 | runMicrofactory(ctx, config, "bpglob", "github.com/google/blueprint/bootstrap/bpglob", |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 517 | map[string]string{"github.com/google/blueprint": "build/blueprint"}) |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 518 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 519 | ninja := func(name, ninjaFile string, targets ...string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 520 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 521 | defer ctx.EndTrace() |
| 522 | |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 523 | if config.IsPersistentBazelEnabled() { |
| 524 | bazelProxy := bazel.NewProxyServer(ctx.Logger, config.OutDir(), filepath.Join(config.SoongOutDir(), "workspace")) |
| 525 | bazelProxy.Start() |
| 526 | defer bazelProxy.Close() |
| 527 | } |
| 528 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 529 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 530 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 531 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 532 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 533 | ninjaArgs := []string{ |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 534 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 535 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 536 | "-o", "usesphonyoutputs=yes", |
| 537 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 538 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 539 | "-w", "outputdir=err", |
| 540 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 541 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 542 | "--frontend_file", fifo, |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 543 | "-f", filepath.Join(config.SoongOutDir(), ninjaFile), |
| 544 | } |
| 545 | |
Usta Shrestha | 8dc8b0a | 2022-08-10 17:39:37 -0400 | [diff] [blame] | 546 | if extra, ok := config.Environment().Get("SOONG_UI_NINJA_ARGS"); ok { |
| 547 | ctx.Printf(`CAUTION: arguments in $SOONG_UI_NINJA_ARGS=%q, e.g. "-n", can make soong_build FAIL or INCORRECT`, extra) |
| 548 | ninjaArgs = append(ninjaArgs, strings.Fields(extra)...) |
| 549 | } |
| 550 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 551 | ninjaArgs = append(ninjaArgs, targets...) |
| 552 | cmd := Command(ctx, config, "soong "+name, |
| 553 | config.PrebuiltBuildTool("ninja"), ninjaArgs...) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 554 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 555 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 556 | |
| 557 | // This is currently how the command line to invoke soong_build finds the |
| 558 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 559 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 560 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 561 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 562 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 563 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 564 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 565 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 566 | targets := make([]string, 0, 0) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 567 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 568 | if config.JsonModuleGraph() { |
| 569 | targets = append(targets, config.ModuleGraphFile()) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 570 | } |
| 571 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 572 | if config.Bp2Build() { |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 573 | targets = append(targets, config.Bp2BuildWorkspaceMarkerFile()) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 574 | } |
| 575 | |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 576 | if config.Queryview() { |
| 577 | targets = append(targets, config.QueryviewMarkerFile()) |
| 578 | } |
| 579 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 580 | if config.ApiBp2build() { |
| 581 | targets = append(targets, config.ApiBp2buildMarkerFile()) |
| 582 | } |
| 583 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 584 | if config.SoongDocs() { |
| 585 | targets = append(targets, config.SoongDocsHtml()) |
| 586 | } |
| 587 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 588 | if config.SoongBuildInvocationNeeded() { |
| 589 | // 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] | 590 | targets = append(targets, config.SoongNinjaFile()) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 591 | } |
| 592 | |
usta | 7f56eaa | 2022-10-27 23:01:02 -0400 | [diff] [blame] | 593 | ninja("bootstrap", "bootstrap.ninja", targets...) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 594 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 595 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
Jihoon Kang | 9f4f8a3 | 2022-08-16 00:57:30 +0000 | [diff] [blame] | 596 | distFile(ctx, config, config.SoongVarsFile(), "soong") |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 597 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 598 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 599 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 600 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 601 | } |
| 602 | |
Rob Seymour | 33cd10d | 2022-03-02 23:10:25 +0000 | [diff] [blame] | 603 | if config.JsonModuleGraph() { |
| 604 | distGzipFile(ctx, config, config.ModuleGraphFile(), "soong") |
| 605 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 606 | } |
| 607 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 608 | 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] | 609 | ctx.BeginTrace(metrics.RunSoong, name) |
| 610 | defer ctx.EndTrace() |
| 611 | cfg := microfactory.Config{TrimPath: absPath(ctx, ".")} |
| 612 | for pkgPrefix, pathPrefix := range mapping { |
| 613 | cfg.Map(pkgPrefix, pathPrefix) |
| 614 | } |
| 615 | |
Lukacs T. Berki | 90b4334 | 2021-11-02 14:42:04 +0100 | [diff] [blame] | 616 | exePath := filepath.Join(config.SoongOutDir(), name) |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 617 | dir := filepath.Dir(exePath) |
| 618 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 619 | ctx.Fatalf("cannot create %s: %s", dir, err) |
| 620 | } |
| 621 | if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil { |
| 622 | ctx.Fatalf("failed to build %s: %s", name, err) |
| 623 | } |
| 624 | } |