blob: 9aa481006a1a4155ed3c080ea28d240be6436843 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
Colin Crossb72c9092020-02-10 11:23:49 -080018 "io/ioutil"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070019 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "path/filepath"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070021 "strconv"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070022
Dan Willemsen4591b642021-05-24 14:24:12 -070023 "android/soong/ui/metrics"
Colin Crossb72c9092020-02-10 11:23:49 -080024 soong_metrics_proto "android/soong/ui/metrics/metrics_proto"
Dan Willemsen4591b642021-05-24 14:24:12 -070025 "android/soong/ui/status"
26
27 "android/soong/shared"
28
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010029 "github.com/google/blueprint"
30 "github.com/google/blueprint/bootstrap"
Dan Willemsen4591b642021-05-24 14:24:12 -070031 "github.com/google/blueprint/deptools"
Dan Willemsen99a75cd2017-08-04 16:04:04 -070032 "github.com/google/blueprint/microfactory"
Dan Willemsenb82471a2018-05-17 16:37:09 -070033
Dan Willemsen4591b642021-05-24 14:24:12 -070034 "google.golang.org/protobuf/proto"
Dan Willemsen1e704462016-08-21 15:17:17 -070035)
36
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020037const (
38 availableEnvFile = "soong.environment.available"
39 usedEnvFile = "soong.environment.used"
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +020040
41 soongBuildTag = "build"
42 bp2buildTag = "bp2build"
43 jsonModuleGraphTag = "modulegraph"
44 queryviewTag = "queryview"
45 soongDocsTag = "soong_docs"
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020046)
47
Lukacs T. Berki7690c092021-02-26 14:27:36 +010048func 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 Shuttleworthb7d97102020-11-25 10:19:29 +000057// This uses Android.bp files and various tools to generate <builddir>/build.ninja.
58//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010059// However, the execution of <builddir>/build.ninja happens later in
60// build/soong/ui/build/build.go#Build()
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000061//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010062// We want to rely on as few prebuilts as possible, so we need to bootstrap
63// Soong. The process is as follows:
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000064//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010065// 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 Shuttleworthb7d97102020-11-25 10:19:29 +000073//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010074// (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.
79type BlueprintConfig struct {
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +020080 toolDir string
81 soongOutDir string
82 outDir string
83 runGoTests bool
84 useValidations bool
85 debugCompilation bool
86 subninjas []string
87 primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010088}
89
Lukacs T. Berkia806e412021-09-01 08:57:48 +020090func (c BlueprintConfig) HostToolDir() string {
91 return c.toolDir
92}
93
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020094func (c BlueprintConfig) SoongOutDir() string {
95 return c.soongOutDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010096}
97
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020098func (c BlueprintConfig) OutDir() string {
99 return c.outDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100100}
101
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +0200102func (c BlueprintConfig) RunGoTests() bool {
103 return c.runGoTests
104}
105
106func (c BlueprintConfig) UseValidationsForGoTests() bool {
107 return c.useValidations
108}
109
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +0100110func (c BlueprintConfig) DebugCompilation() bool {
111 return c.debugCompilation
112}
113
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +0200114func (c BlueprintConfig) Subninjas() []string {
115 return c.subninjas
116}
117
118func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation {
119 return c.primaryBuilderInvocations
120}
121
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200122func environmentArgs(config Config, suffix string) []string {
123 return []string{
124 "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile),
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200125 "--used_env", shared.JoinPath(config.SoongOutDir(), usedEnvFile+"."+suffix),
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200126 }
127}
Spandan Das8f99ae62021-06-11 16:48:06 +0000128
129func writeEmptyGlobFile(ctx Context, path string) {
130 err := os.MkdirAll(filepath.Dir(path), 0777)
131 if err != nil {
132 ctx.Fatalf("Failed to create parent directories of empty ninja glob file '%s': %s", path, err)
133 }
134
135 if _, err := os.Stat(path); os.IsNotExist(err) {
136 err = ioutil.WriteFile(path, nil, 0666)
137 if err != nil {
138 ctx.Fatalf("Failed to create empty ninja glob file '%s': %s", path, err)
139 }
140 }
141}
142
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200143func primaryBuilderInvocation(config Config, name string, output string, specificArgs []string) bootstrap.PrimaryBuilderInvocation {
144 commonArgs := make([]string, 0, 0)
145
146 if !config.skipSoongTests {
147 commonArgs = append(commonArgs, "-t")
148 }
149
150 commonArgs = append(commonArgs, "-l", filepath.Join(config.FileListDir(), "Android.bp.list"))
151
152 if os.Getenv("SOONG_DELVE") != "" {
153 commonArgs = append(commonArgs, "--delve_listen", os.Getenv("SOONG_DELVE"))
154 commonArgs = append(commonArgs, "--delve_path", shared.ResolveDelveBinary())
155 }
156
157 allArgs := make([]string, 0, 0)
158 allArgs = append(allArgs, specificArgs...)
159 allArgs = append(allArgs,
160 "--globListDir", name,
161 "--globFile", config.NamedGlobFile(name))
162
163 allArgs = append(allArgs, commonArgs...)
164 allArgs = append(allArgs, environmentArgs(config, name)...)
165 allArgs = append(allArgs, "Android.bp")
166
167 return bootstrap.PrimaryBuilderInvocation{
168 Inputs: []string{"Android.bp"},
169 Outputs: []string{output},
170 Args: allArgs,
171 }
172}
173
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200174func bootstrapBlueprint(ctx Context, config Config) {
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100175 ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
176 defer ctx.EndTrace()
177
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200178 mainSoongBuildExtraArgs := []string{"-o", config.MainNinjaFile()}
179 if config.EmptyNinjaFile() {
180 mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file")
Lukacs T. Berki745380c2021-04-12 12:07:44 +0200181 }
182
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200183 mainSoongBuildInvocation := primaryBuilderInvocation(
184 config,
185 soongBuildTag,
186 config.MainNinjaFile(),
187 mainSoongBuildExtraArgs)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200188
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200189 if config.bazelBuildMode() == mixedBuild {
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200190 // Mixed builds call Bazel from soong_build and they therefore need the
191 // Bazel workspace to be available. Make that so by adding a dependency on
192 // the bp2build marker file to the action that invokes soong_build .
193 mainSoongBuildInvocation.Inputs = append(mainSoongBuildInvocation.Inputs,
194 config.Bp2BuildMarkerFile())
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200195 }
196
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200197 bp2buildInvocation := primaryBuilderInvocation(
198 config,
199 bp2buildTag,
200 config.Bp2BuildMarkerFile(),
201 []string{
202 "--bp2build_marker", config.Bp2BuildMarkerFile(),
203 })
204
205 jsonModuleGraphInvocation := primaryBuilderInvocation(
206 config,
207 jsonModuleGraphTag,
208 config.ModuleGraphFile(),
209 []string{
210 "--module_graph_file", config.ModuleGraphFile(),
211 })
212
213 queryviewInvocation := primaryBuilderInvocation(
214 config,
215 queryviewTag,
216 config.QueryviewMarkerFile(),
217 []string{
218 "--bazel_queryview_dir", filepath.Join(config.SoongOutDir(), "queryview"),
219 })
220
221 soongDocsInvocation := primaryBuilderInvocation(
222 config,
223 soongDocsTag,
224 config.SoongDocsHtml(),
225 []string{
226 "--soong_docs", config.SoongDocsHtml(),
227 })
228
229 globFiles := []string{
230 config.NamedGlobFile(soongBuildTag),
231 config.NamedGlobFile(bp2buildTag),
232 config.NamedGlobFile(jsonModuleGraphTag),
233 config.NamedGlobFile(queryviewTag),
234 config.NamedGlobFile(soongDocsTag),
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200235 }
236
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200237 // The glob .ninja files are subninja'd. However, they are generated during
238 // the build itself so we write an empty file if the file does not exist yet
239 // so that the subninja doesn't fail on clean builds
240 for _, globFile := range globFiles {
241 writeEmptyGlobFile(ctx, globFile)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200242 }
243
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200244 var blueprintArgs bootstrap.Args
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200245
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200246 blueprintArgs.ModuleListFile = filepath.Join(config.FileListDir(), "Android.bp.list")
247 blueprintArgs.OutFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja")
248 blueprintArgs.EmptyNinjaFile = false
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200249
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100250 blueprintCtx := blueprint.NewContext()
251 blueprintCtx.SetIgnoreUnknownModuleTypes(true)
252 blueprintConfig := BlueprintConfig{
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200253 soongOutDir: config.SoongOutDir(),
254 toolDir: config.HostToolDir(),
255 outDir: config.OutDir(),
256 runGoTests: !config.skipSoongTests,
257 useValidations: !config.skipSoongTests,
258 // If we want to debug soong_build, we need to compile it for debugging
259 debugCompilation: os.Getenv("SOONG_DELVE") != "",
260 subninjas: globFiles,
261 primaryBuilderInvocations: []bootstrap.PrimaryBuilderInvocation{
262 mainSoongBuildInvocation,
263 bp2buildInvocation,
264 jsonModuleGraphInvocation,
265 queryviewInvocation,
266 soongDocsInvocation},
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100267 }
268
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200269 bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.DoEverything, blueprintCtx, blueprintConfig)
270 bootstrapDepFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja.d")
271 err := deptools.WriteDepFile(bootstrapDepFile, blueprintArgs.OutFile, bootstrapDeps)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200272 if err != nil {
273 ctx.Fatalf("Error writing depfile '%s': %s", bootstrapDepFile, err)
274 }
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100275}
276
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200277func checkEnvironmentFile(currentEnv *Environment, envFile string) {
278 getenv := func(k string) string {
279 v, _ := currentEnv.Get(k)
280 return v
281 }
282 if stale, _ := shared.StaleEnvFile(envFile, getenv); stale {
283 os.Remove(envFile)
284 }
285}
286
Dan Willemsen1e704462016-08-21 15:17:17 -0700287func runSoong(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800288 ctx.BeginTrace(metrics.RunSoong, "soong")
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700289 defer ctx.EndTrace()
290
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100291 // We have two environment files: .available is the one with every variable,
292 // .used with the ones that were actually used. The latter is used to
293 // determine whether Soong needs to be re-run since why re-run it if only
294 // unused variables were changed?
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200295 envFile := filepath.Join(config.SoongOutDir(), availableEnvFile)
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100296
Lukacs T. Berki9d7cf6a2021-08-16 14:08:57 +0200297 dir := filepath.Join(config.SoongOutDir(), ".bootstrap")
298 if err := os.MkdirAll(dir, 0755); err != nil {
299 ctx.Fatalf("Cannot mkdir " + dir)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100300 }
Colin Cross00a8a3f2020-10-29 14:08:31 -0700301
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400302 buildMode := config.bazelBuildMode()
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200303 integratedBp2Build := buildMode == mixedBuild
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200304
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100305 // This is done unconditionally, but does not take a measurable amount of time
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200306 bootstrapBlueprint(ctx, config)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700307
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100308 soongBuildEnv := config.Environment().Copy()
309 soongBuildEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100310 // For Bazel mixed builds.
311 soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel")
312 soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome"))
313 soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output"))
314 soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, "."))
315 soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir())
316
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100317 // For Soong bootstrapping tests
318 if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
319 soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true")
320 }
321
Paul Duffin5e85c662021-03-05 12:26:14 +0000322 err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap())
323 if err != nil {
324 ctx.Fatalf("failed to write environment file %s: %s", envFile, err)
325 }
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100326
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700327 func() {
Nan Zhang17f27672018-12-12 16:01:49 -0800328 ctx.BeginTrace(metrics.RunSoong, "environment check")
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700329 defer ctx.EndTrace()
330
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200331 checkEnvironmentFile(soongBuildEnv, filepath.Join(config.SoongOutDir(), usedEnvFile+".build"))
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200332
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200333 if integratedBp2Build || config.Bp2Build() {
334 checkEnvironmentFile(soongBuildEnv, filepath.Join(config.SoongOutDir(), usedEnvFile+".bp2build"))
335 }
336
337 if config.JsonModuleGraph() {
338 checkEnvironmentFile(soongBuildEnv, filepath.Join(config.SoongOutDir(), usedEnvFile+".modulegraph"))
339 }
340
341 if config.Queryview() {
342 checkEnvironmentFile(soongBuildEnv, filepath.Join(config.SoongOutDir(), usedEnvFile+".queryview"))
343 }
344
345 if config.SoongDocs() {
346 checkEnvironmentFile(soongBuildEnv, filepath.Join(config.SoongOutDir(), usedEnvFile+".soong_docs"))
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700347 }
348 }()
349
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200350 runMicrofactory(ctx, config, filepath.Join(config.HostToolDir(), "bpglob"), "github.com/google/blueprint/bootstrap/bpglob",
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700351 map[string]string{"github.com/google/blueprint": "build/blueprint"})
Dan Willemsen5af1cbe2018-07-05 21:46:51 -0700352
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200353 ninja := func(name, ninjaFile string, targets ...string) {
Nan Zhang17f27672018-12-12 16:01:49 -0800354 ctx.BeginTrace(metrics.RunSoong, name)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700355 defer ctx.EndTrace()
356
Dan Willemsenb82471a2018-05-17 16:37:09 -0700357 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -0700358 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
359 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700360
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200361 ninjaArgs := []string{
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700362 "-d", "keepdepfile",
Dan Willemsen08218222020-05-18 14:02:02 -0700363 "-d", "stats",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700364 "-o", "usesphonyoutputs=yes",
365 "-o", "preremoveoutputs=yes",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700366 "-w", "dupbuild=err",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700367 "-w", "outputdir=err",
368 "-w", "missingoutfile=err",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700369 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700370 "--frontend_file", fifo,
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200371 "-f", filepath.Join(config.SoongOutDir(), ninjaFile),
372 }
373
374 ninjaArgs = append(ninjaArgs, targets...)
375 cmd := Command(ctx, config, "soong "+name,
376 config.PrebuiltBuildTool("ninja"), ninjaArgs...)
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500377
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100378 var ninjaEnv Environment
Lukacs T. Berki73ab9282021-03-10 10:48:39 +0100379
380 // This is currently how the command line to invoke soong_build finds the
381 // root of the source tree and the output root
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100382 ninjaEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100383
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100384 cmd.Environment = &ninjaEnv
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700385 cmd.Sandbox = soongSandbox
Colin Cross7b97ecd2019-06-19 13:17:59 -0700386 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700387 }
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200388
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200389 targets := make([]string, 0, 0)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200390
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200391 if config.JsonModuleGraph() {
392 targets = append(targets, config.ModuleGraphFile())
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200393 }
394
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200395 if config.Bp2Build() {
396 targets = append(targets, config.Bp2BuildMarkerFile())
397 }
398
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200399 if config.Queryview() {
400 targets = append(targets, config.QueryviewMarkerFile())
401 }
402
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200403 if config.SoongDocs() {
404 targets = append(targets, config.SoongDocsHtml())
405 }
406
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200407 if config.SoongBuildInvocationNeeded() {
408 // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
409 targets = append(targets, config.MainNinjaFile())
410 }
411
412 ninja("bootstrap", ".bootstrap/build.ninja", targets...)
Colin Crossb72c9092020-02-10 11:23:49 -0800413
Jingwen Cheneb76c432021-01-28 08:22:12 -0500414 var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
415 if shouldCollectBuildSoongMetrics(config) {
416 soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
417 logSoongBuildMetrics(ctx, soongBuildMetrics)
418 }
Colin Crossb72c9092020-02-10 11:23:49 -0800419
Colin Cross8ba7d472020-06-25 11:27:52 -0700420 distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
421
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000422 if !config.SkipKati() {
Colin Cross8ba7d472020-06-25 11:27:52 -0700423 distGzipFile(ctx, config, config.SoongAndroidMk(), "soong")
424 distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
425 }
426
Jingwen Cheneb76c432021-01-28 08:22:12 -0500427 if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil {
Colin Crossb72c9092020-02-10 11:23:49 -0800428 ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
429 }
430}
431
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700432func runMicrofactory(ctx Context, config Config, relExePath string, pkg string, mapping map[string]string) {
433 name := filepath.Base(relExePath)
434 ctx.BeginTrace(metrics.RunSoong, name)
435 defer ctx.EndTrace()
436 cfg := microfactory.Config{TrimPath: absPath(ctx, ".")}
437 for pkgPrefix, pathPrefix := range mapping {
438 cfg.Map(pkgPrefix, pathPrefix)
439 }
440
441 exePath := filepath.Join(config.SoongOutDir(), relExePath)
442 dir := filepath.Dir(exePath)
443 if err := os.MkdirAll(dir, 0777); err != nil {
444 ctx.Fatalf("cannot create %s: %s", dir, err)
445 }
446 if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil {
447 ctx.Fatalf("failed to build %s: %s", name, err)
448 }
449}
450
Jingwen Cheneb76c432021-01-28 08:22:12 -0500451func shouldCollectBuildSoongMetrics(config Config) bool {
Jingwen Chendd9725c2021-06-24 08:41:16 +0000452 // Do not collect metrics protobuf if the soong_build binary ran as the
453 // bp2build converter or the JSON graph dump.
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200454 return config.SoongBuildInvocationNeeded()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500455}
456
Colin Crossb72c9092020-02-10 11:23:49 -0800457func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
458 soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb")
459 buf, err := ioutil.ReadFile(soongBuildMetricsFile)
460 if err != nil {
461 ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err)
462 }
463 soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{}
464 err = proto.Unmarshal(buf, soongBuildMetrics)
465 if err != nil {
466 ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err)
467 }
468 return soongBuildMetrics
469}
470
471func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) {
472 ctx.Verbosef("soong_build metrics:")
473 ctx.Verbosef(" modules: %v", metrics.GetModules())
474 ctx.Verbosef(" variants: %v", metrics.GetVariants())
475 ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6)
476 ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount())
477 ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6)
478
Dan Willemsen1e704462016-08-21 15:17:17 -0700479}