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 | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 18 | "io/ioutil" |
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" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 22 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 23 | "android/soong/ui/metrics" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 24 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 25 | "android/soong/ui/status" |
| 26 | |
| 27 | "android/soong/shared" |
| 28 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 29 | "github.com/google/blueprint" |
| 30 | "github.com/google/blueprint/bootstrap" |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 31 | "github.com/google/blueprint/deptools" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 32 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 33 | |
Dan Willemsen | 4591b64 | 2021-05-24 14:24:12 -0700 | [diff] [blame] | 34 | "google.golang.org/protobuf/proto" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 35 | ) |
| 36 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 37 | const ( |
| 38 | availableEnvFile = "soong.environment.available" |
| 39 | usedEnvFile = "soong.environment.used" |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 40 | |
| 41 | soongBuildTag = "build" |
| 42 | bp2buildTag = "bp2build" |
| 43 | jsonModuleGraphTag = "modulegraph" |
| 44 | queryviewTag = "queryview" |
| 45 | soongDocsTag = "soong_docs" |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 46 | ) |
| 47 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 48 | func writeEnvironmentFile(ctx Context, envFile string, envDeps map[string]string) error { |
| 49 | data, err := shared.EnvFileContents(envDeps) |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | return ioutil.WriteFile(envFile, data, 0644) |
| 55 | } |
| 56 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 57 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 58 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 59 | // However, the execution of <builddir>/build.ninja happens later in |
| 60 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 61 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 62 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 63 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 64 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 65 | // 1. We use "Microfactory", a simple tool to compile Go code, to build |
| 66 | // first itself, then soong_ui from soong_ui.bash. This binary contains |
| 67 | // parts of soong_build that are needed to build itself. |
| 68 | // 2. This simplified version of soong_build then reads the Blueprint files |
| 69 | // that describe itself and emits .bootstrap/build.ninja that describes |
| 70 | // how to build its full version and use that to produce the final Ninja |
| 71 | // file Soong emits. |
| 72 | // 3. soong_ui executes .bootstrap/build.ninja |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 73 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 74 | // (After this, Kati is executed to parse the Makefiles, but that's not part of |
| 75 | // bootstrapping Soong) |
| 76 | |
| 77 | // A tiny struct used to tell Blueprint that it's in bootstrap mode. It would |
| 78 | // probably be nicer to use a flag in bootstrap.Args instead. |
| 79 | type BlueprintConfig struct { |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 80 | toolDir string |
| 81 | soongOutDir string |
| 82 | outDir string |
| 83 | runGoTests bool |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 84 | debugCompilation bool |
| 85 | subninjas []string |
| 86 | primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 87 | } |
| 88 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 89 | func (c BlueprintConfig) HostToolDir() string { |
| 90 | return c.toolDir |
| 91 | } |
| 92 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 93 | func (c BlueprintConfig) SoongOutDir() string { |
| 94 | return c.soongOutDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 97 | func (c BlueprintConfig) OutDir() string { |
| 98 | return c.outDir |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 99 | } |
| 100 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 101 | func (c BlueprintConfig) RunGoTests() bool { |
| 102 | return c.runGoTests |
| 103 | } |
| 104 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 105 | func (c BlueprintConfig) DebugCompilation() bool { |
| 106 | return c.debugCompilation |
| 107 | } |
| 108 | |
Lukacs T. Berki | ea1a31c | 2021-09-02 09:58:09 +0200 | [diff] [blame] | 109 | func (c BlueprintConfig) Subninjas() []string { |
| 110 | return c.subninjas |
| 111 | } |
| 112 | |
| 113 | func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation { |
| 114 | return c.primaryBuilderInvocations |
| 115 | } |
| 116 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 117 | func environmentArgs(config Config, tag string) []string { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 118 | return []string{ |
| 119 | "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile), |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 120 | "--used_env", config.UsedEnvFile(tag), |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 121 | } |
| 122 | } |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 123 | |
| 124 | func writeEmptyGlobFile(ctx Context, path string) { |
| 125 | err := os.MkdirAll(filepath.Dir(path), 0777) |
| 126 | if err != nil { |
| 127 | ctx.Fatalf("Failed to create parent directories of empty ninja glob file '%s': %s", path, err) |
| 128 | } |
| 129 | |
| 130 | if _, err := os.Stat(path); os.IsNotExist(err) { |
| 131 | err = ioutil.WriteFile(path, nil, 0666) |
| 132 | if err != nil { |
| 133 | ctx.Fatalf("Failed to create empty ninja glob file '%s': %s", path, err) |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 138 | func primaryBuilderInvocation(config Config, name string, output string, specificArgs []string) bootstrap.PrimaryBuilderInvocation { |
| 139 | commonArgs := make([]string, 0, 0) |
| 140 | |
| 141 | if !config.skipSoongTests { |
| 142 | commonArgs = append(commonArgs, "-t") |
| 143 | } |
| 144 | |
| 145 | commonArgs = append(commonArgs, "-l", filepath.Join(config.FileListDir(), "Android.bp.list")) |
| 146 | |
| 147 | if os.Getenv("SOONG_DELVE") != "" { |
| 148 | commonArgs = append(commonArgs, "--delve_listen", os.Getenv("SOONG_DELVE")) |
| 149 | commonArgs = append(commonArgs, "--delve_path", shared.ResolveDelveBinary()) |
| 150 | } |
| 151 | |
| 152 | allArgs := make([]string, 0, 0) |
| 153 | allArgs = append(allArgs, specificArgs...) |
| 154 | allArgs = append(allArgs, |
| 155 | "--globListDir", name, |
| 156 | "--globFile", config.NamedGlobFile(name)) |
| 157 | |
| 158 | allArgs = append(allArgs, commonArgs...) |
| 159 | allArgs = append(allArgs, environmentArgs(config, name)...) |
| 160 | allArgs = append(allArgs, "Android.bp") |
| 161 | |
| 162 | return bootstrap.PrimaryBuilderInvocation{ |
| 163 | Inputs: []string{"Android.bp"}, |
| 164 | Outputs: []string{output}, |
| 165 | Args: allArgs, |
| 166 | } |
| 167 | } |
| 168 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 169 | func bootstrapBlueprint(ctx Context, config Config) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 170 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 171 | defer ctx.EndTrace() |
| 172 | |
Cole Faust | 65f298c | 2021-10-28 16:05:13 -0700 | [diff] [blame] | 173 | mainSoongBuildExtraArgs := []string{"-o", config.SoongNinjaFile()} |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 174 | if config.EmptyNinjaFile() { |
| 175 | mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file") |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 176 | } |
| 177 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 178 | mainSoongBuildInvocation := primaryBuilderInvocation( |
| 179 | config, |
| 180 | soongBuildTag, |
Cole Faust | 65f298c | 2021-10-28 16:05:13 -0700 | [diff] [blame] | 181 | config.SoongNinjaFile(), |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 182 | mainSoongBuildExtraArgs) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 183 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 184 | if config.bazelBuildMode() == mixedBuild { |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 185 | // Mixed builds call Bazel from soong_build and they therefore need the |
| 186 | // Bazel workspace to be available. Make that so by adding a dependency on |
| 187 | // the bp2build marker file to the action that invokes soong_build . |
| 188 | mainSoongBuildInvocation.Inputs = append(mainSoongBuildInvocation.Inputs, |
| 189 | config.Bp2BuildMarkerFile()) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 192 | bp2buildInvocation := primaryBuilderInvocation( |
| 193 | config, |
| 194 | bp2buildTag, |
| 195 | config.Bp2BuildMarkerFile(), |
| 196 | []string{ |
| 197 | "--bp2build_marker", config.Bp2BuildMarkerFile(), |
| 198 | }) |
| 199 | |
| 200 | jsonModuleGraphInvocation := primaryBuilderInvocation( |
| 201 | config, |
| 202 | jsonModuleGraphTag, |
| 203 | config.ModuleGraphFile(), |
| 204 | []string{ |
| 205 | "--module_graph_file", config.ModuleGraphFile(), |
| 206 | }) |
| 207 | |
| 208 | queryviewInvocation := primaryBuilderInvocation( |
| 209 | config, |
| 210 | queryviewTag, |
| 211 | config.QueryviewMarkerFile(), |
| 212 | []string{ |
| 213 | "--bazel_queryview_dir", filepath.Join(config.SoongOutDir(), "queryview"), |
| 214 | }) |
| 215 | |
| 216 | soongDocsInvocation := primaryBuilderInvocation( |
| 217 | config, |
| 218 | soongDocsTag, |
| 219 | config.SoongDocsHtml(), |
| 220 | []string{ |
| 221 | "--soong_docs", config.SoongDocsHtml(), |
| 222 | }) |
| 223 | |
| 224 | globFiles := []string{ |
| 225 | config.NamedGlobFile(soongBuildTag), |
| 226 | config.NamedGlobFile(bp2buildTag), |
| 227 | config.NamedGlobFile(jsonModuleGraphTag), |
| 228 | config.NamedGlobFile(queryviewTag), |
| 229 | config.NamedGlobFile(soongDocsTag), |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 232 | // The glob .ninja files are subninja'd. However, they are generated during |
| 233 | // the build itself so we write an empty file if the file does not exist yet |
| 234 | // so that the subninja doesn't fail on clean builds |
| 235 | for _, globFile := range globFiles { |
| 236 | writeEmptyGlobFile(ctx, globFile) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 237 | } |
| 238 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 239 | var blueprintArgs bootstrap.Args |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 240 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 241 | blueprintArgs.ModuleListFile = filepath.Join(config.FileListDir(), "Android.bp.list") |
| 242 | blueprintArgs.OutFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja") |
| 243 | blueprintArgs.EmptyNinjaFile = false |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 244 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 245 | blueprintCtx := blueprint.NewContext() |
| 246 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 247 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 248 | soongOutDir: config.SoongOutDir(), |
| 249 | toolDir: config.HostToolDir(), |
| 250 | outDir: config.OutDir(), |
| 251 | runGoTests: !config.skipSoongTests, |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 252 | // If we want to debug soong_build, we need to compile it for debugging |
| 253 | debugCompilation: os.Getenv("SOONG_DELVE") != "", |
| 254 | subninjas: globFiles, |
| 255 | primaryBuilderInvocations: []bootstrap.PrimaryBuilderInvocation{ |
| 256 | mainSoongBuildInvocation, |
| 257 | bp2buildInvocation, |
| 258 | jsonModuleGraphInvocation, |
| 259 | queryviewInvocation, |
| 260 | soongDocsInvocation}, |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 261 | } |
| 262 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 263 | bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig) |
| 264 | bootstrapDepFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja.d") |
| 265 | err := deptools.WriteDepFile(bootstrapDepFile, blueprintArgs.OutFile, bootstrapDeps) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 266 | if err != nil { |
| 267 | ctx.Fatalf("Error writing depfile '%s': %s", bootstrapDepFile, err) |
| 268 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 269 | } |
| 270 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 271 | func checkEnvironmentFile(currentEnv *Environment, envFile string) { |
| 272 | getenv := func(k string) string { |
| 273 | v, _ := currentEnv.Get(k) |
| 274 | return v |
| 275 | } |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 276 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 277 | if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 278 | os.Remove(envFile) |
| 279 | } |
| 280 | } |
| 281 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 282 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 283 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 284 | defer ctx.EndTrace() |
| 285 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 286 | // We have two environment files: .available is the one with every variable, |
| 287 | // .used with the ones that were actually used. The latter is used to |
| 288 | // determine whether Soong needs to be re-run since why re-run it if only |
| 289 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 290 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 291 | |
Lukacs T. Berki | 9d7cf6a | 2021-08-16 14:08:57 +0200 | [diff] [blame] | 292 | dir := filepath.Join(config.SoongOutDir(), ".bootstrap") |
| 293 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 294 | ctx.Fatalf("Cannot mkdir " + dir) |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 295 | } |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 296 | |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 297 | buildMode := config.bazelBuildMode() |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 298 | integratedBp2Build := buildMode == mixedBuild |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 299 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 300 | // 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] | 301 | bootstrapBlueprint(ctx, config) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 302 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 303 | soongBuildEnv := config.Environment().Copy() |
| 304 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 305 | // For Bazel mixed builds. |
| 306 | soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel") |
| 307 | soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome")) |
| 308 | soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output")) |
| 309 | soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
| 310 | soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
| 311 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 312 | // For Soong bootstrapping tests |
| 313 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 314 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 315 | } |
| 316 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 317 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 318 | if err != nil { |
| 319 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 320 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 321 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 322 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 323 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 324 | defer ctx.EndTrace() |
| 325 | |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 326 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongBuildTag)) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 327 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 328 | if integratedBp2Build || config.Bp2Build() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 329 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(bp2buildTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | if config.JsonModuleGraph() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 333 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | if config.Queryview() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 337 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(queryviewTag)) |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | if config.SoongDocs() { |
Lukacs T. Berki | e1df43f | 2021-09-08 15:31:14 +0200 | [diff] [blame] | 341 | checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongDocsTag)) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 342 | } |
| 343 | }() |
| 344 | |
Lukacs T. Berki | a806e41 | 2021-09-01 08:57:48 +0200 | [diff] [blame] | 345 | runMicrofactory(ctx, config, filepath.Join(config.HostToolDir(), "bpglob"), "github.com/google/blueprint/bootstrap/bpglob", |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 346 | map[string]string{"github.com/google/blueprint": "build/blueprint"}) |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 347 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 348 | ninja := func(name, ninjaFile string, targets ...string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 349 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 350 | defer ctx.EndTrace() |
| 351 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 352 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 353 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 354 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 355 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 356 | ninjaArgs := []string{ |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 357 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 358 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 359 | "-o", "usesphonyoutputs=yes", |
| 360 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 361 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 362 | "-w", "outputdir=err", |
| 363 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 364 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 365 | "--frontend_file", fifo, |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 366 | "-f", filepath.Join(config.SoongOutDir(), ninjaFile), |
| 367 | } |
| 368 | |
| 369 | ninjaArgs = append(ninjaArgs, targets...) |
| 370 | cmd := Command(ctx, config, "soong "+name, |
| 371 | config.PrebuiltBuildTool("ninja"), ninjaArgs...) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 372 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 373 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 374 | |
| 375 | // This is currently how the command line to invoke soong_build finds the |
| 376 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 377 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 378 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 379 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 380 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 381 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 382 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 383 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 384 | targets := make([]string, 0, 0) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 385 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 386 | if config.JsonModuleGraph() { |
| 387 | targets = append(targets, config.ModuleGraphFile()) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame] | 388 | } |
| 389 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 390 | if config.Bp2Build() { |
| 391 | targets = append(targets, config.Bp2BuildMarkerFile()) |
| 392 | } |
| 393 | |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 394 | if config.Queryview() { |
| 395 | targets = append(targets, config.QueryviewMarkerFile()) |
| 396 | } |
| 397 | |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 398 | if config.SoongDocs() { |
| 399 | targets = append(targets, config.SoongDocsHtml()) |
| 400 | } |
| 401 | |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 402 | if config.SoongBuildInvocationNeeded() { |
| 403 | // 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] | 404 | targets = append(targets, config.SoongNinjaFile()) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | ninja("bootstrap", ".bootstrap/build.ninja", targets...) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 408 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 409 | var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics |
| 410 | if shouldCollectBuildSoongMetrics(config) { |
| 411 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 412 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 413 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 414 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 415 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
| 416 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 417 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 418 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 419 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 420 | } |
| 421 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 422 | if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 423 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 424 | } |
| 425 | } |
| 426 | |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 427 | func runMicrofactory(ctx Context, config Config, relExePath string, pkg string, mapping map[string]string) { |
| 428 | name := filepath.Base(relExePath) |
| 429 | ctx.BeginTrace(metrics.RunSoong, name) |
| 430 | defer ctx.EndTrace() |
| 431 | cfg := microfactory.Config{TrimPath: absPath(ctx, ".")} |
| 432 | for pkgPrefix, pathPrefix := range mapping { |
| 433 | cfg.Map(pkgPrefix, pathPrefix) |
| 434 | } |
| 435 | |
| 436 | exePath := filepath.Join(config.SoongOutDir(), relExePath) |
| 437 | dir := filepath.Dir(exePath) |
| 438 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 439 | ctx.Fatalf("cannot create %s: %s", dir, err) |
| 440 | } |
| 441 | if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil { |
| 442 | ctx.Fatalf("failed to build %s: %s", name, err) |
| 443 | } |
| 444 | } |
| 445 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 446 | func shouldCollectBuildSoongMetrics(config Config) bool { |
Jingwen Chen | dd9725c | 2021-06-24 08:41:16 +0000 | [diff] [blame] | 447 | // Do not collect metrics protobuf if the soong_build binary ran as the |
| 448 | // bp2build converter or the JSON graph dump. |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 449 | return config.SoongBuildInvocationNeeded() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 450 | } |
| 451 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 452 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 453 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 454 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 455 | if err != nil { |
| 456 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 457 | } |
| 458 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 459 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 460 | if err != nil { |
| 461 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 462 | } |
| 463 | return soongBuildMetrics |
| 464 | } |
| 465 | |
| 466 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 467 | ctx.Verbosef("soong_build metrics:") |
| 468 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 469 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 470 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 471 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 472 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 473 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 474 | } |