blob: a0f223b2c56c5f92f963ff313e0bfa6c10291f0a [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
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +020084 debugCompilation bool
85 subninjas []string
86 primaryBuilderInvocations []bootstrap.PrimaryBuilderInvocation
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010087}
88
Lukacs T. Berkia806e412021-09-01 08:57:48 +020089func (c BlueprintConfig) HostToolDir() string {
90 return c.toolDir
91}
92
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020093func (c BlueprintConfig) SoongOutDir() string {
94 return c.soongOutDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010095}
96
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020097func (c BlueprintConfig) OutDir() string {
98 return c.outDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010099}
100
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +0200101func (c BlueprintConfig) RunGoTests() bool {
102 return c.runGoTests
103}
104
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +0100105func (c BlueprintConfig) DebugCompilation() bool {
106 return c.debugCompilation
107}
108
Lukacs T. Berkiea1a31c2021-09-02 09:58:09 +0200109func (c BlueprintConfig) Subninjas() []string {
110 return c.subninjas
111}
112
113func (c BlueprintConfig) PrimaryBuilderInvocations() []bootstrap.PrimaryBuilderInvocation {
114 return c.primaryBuilderInvocations
115}
116
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200117func environmentArgs(config Config, tag string) []string {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200118 return []string{
119 "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile),
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200120 "--used_env", config.UsedEnvFile(tag),
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200121 }
122}
Spandan Das8f99ae62021-06-11 16:48:06 +0000123
124func 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. Berki89fcdcb2021-09-07 09:10:33 +0200138func 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. Berki56ebaf32021-08-12 14:03:55 +0200169func bootstrapBlueprint(ctx Context, config Config) {
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100170 ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
171 defer ctx.EndTrace()
172
Cole Faust65f298c2021-10-28 16:05:13 -0700173 mainSoongBuildExtraArgs := []string{"-o", config.SoongNinjaFile()}
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200174 if config.EmptyNinjaFile() {
175 mainSoongBuildExtraArgs = append(mainSoongBuildExtraArgs, "--empty-ninja-file")
Lukacs T. Berki745380c2021-04-12 12:07:44 +0200176 }
177
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200178 mainSoongBuildInvocation := primaryBuilderInvocation(
179 config,
180 soongBuildTag,
Cole Faust65f298c2021-10-28 16:05:13 -0700181 config.SoongNinjaFile(),
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200182 mainSoongBuildExtraArgs)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200183
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200184 if config.bazelBuildMode() == mixedBuild {
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200185 // 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. Berkif8e24282021-04-14 10:31:00 +0200190 }
191
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200192 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. Berki56ebaf32021-08-12 14:03:55 +0200230 }
231
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200232 // 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. Berkif8e24282021-04-14 10:31:00 +0200237 }
238
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200239 var blueprintArgs bootstrap.Args
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200240
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200241 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. Berkif8e24282021-04-14 10:31:00 +0200244
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100245 blueprintCtx := blueprint.NewContext()
246 blueprintCtx.SetIgnoreUnknownModuleTypes(true)
247 blueprintConfig := BlueprintConfig{
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200248 soongOutDir: config.SoongOutDir(),
249 toolDir: config.HostToolDir(),
250 outDir: config.OutDir(),
251 runGoTests: !config.skipSoongTests,
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200252 // 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. Berkid1e3f1f2021-03-16 08:55:23 +0100261 }
262
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200263 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. Berkid518e1a2021-04-14 13:49:50 +0200266 if err != nil {
267 ctx.Fatalf("Error writing depfile '%s': %s", bootstrapDepFile, err)
268 }
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100269}
270
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200271func checkEnvironmentFile(currentEnv *Environment, envFile string) {
272 getenv := func(k string) string {
273 v, _ := currentEnv.Get(k)
274 return v
275 }
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200276
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200277 if stale, _ := shared.StaleEnvFile(envFile, getenv); stale {
278 os.Remove(envFile)
279 }
280}
281
Dan Willemsen1e704462016-08-21 15:17:17 -0700282func runSoong(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800283 ctx.BeginTrace(metrics.RunSoong, "soong")
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700284 defer ctx.EndTrace()
285
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100286 // 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. Berkif8e24282021-04-14 10:31:00 +0200290 envFile := filepath.Join(config.SoongOutDir(), availableEnvFile)
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100291
Lukacs T. Berki9d7cf6a2021-08-16 14:08:57 +0200292 dir := filepath.Join(config.SoongOutDir(), ".bootstrap")
293 if err := os.MkdirAll(dir, 0755); err != nil {
294 ctx.Fatalf("Cannot mkdir " + dir)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100295 }
Colin Cross00a8a3f2020-10-29 14:08:31 -0700296
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400297 buildMode := config.bazelBuildMode()
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200298 integratedBp2Build := buildMode == mixedBuild
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200299
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100300 // This is done unconditionally, but does not take a measurable amount of time
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200301 bootstrapBlueprint(ctx, config)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700302
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100303 soongBuildEnv := config.Environment().Copy()
304 soongBuildEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100305 // 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. Berkid1e3f1f2021-03-16 08:55:23 +0100312 // For Soong bootstrapping tests
313 if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
314 soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true")
315 }
316
Paul Duffin5e85c662021-03-05 12:26:14 +0000317 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. Berki7690c092021-02-26 14:27:36 +0100321
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700322 func() {
Nan Zhang17f27672018-12-12 16:01:49 -0800323 ctx.BeginTrace(metrics.RunSoong, "environment check")
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700324 defer ctx.EndTrace()
325
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200326 checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongBuildTag))
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200327
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200328 if integratedBp2Build || config.Bp2Build() {
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200329 checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(bp2buildTag))
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200330 }
331
332 if config.JsonModuleGraph() {
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200333 checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(jsonModuleGraphTag))
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200334 }
335
336 if config.Queryview() {
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200337 checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(queryviewTag))
Lukacs T. Berki89fcdcb2021-09-07 09:10:33 +0200338 }
339
340 if config.SoongDocs() {
Lukacs T. Berkie1df43f2021-09-08 15:31:14 +0200341 checkEnvironmentFile(soongBuildEnv, config.UsedEnvFile(soongDocsTag))
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700342 }
343 }()
344
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200345 runMicrofactory(ctx, config, filepath.Join(config.HostToolDir(), "bpglob"), "github.com/google/blueprint/bootstrap/bpglob",
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700346 map[string]string{"github.com/google/blueprint": "build/blueprint"})
Dan Willemsen5af1cbe2018-07-05 21:46:51 -0700347
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200348 ninja := func(name, ninjaFile string, targets ...string) {
Nan Zhang17f27672018-12-12 16:01:49 -0800349 ctx.BeginTrace(metrics.RunSoong, name)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700350 defer ctx.EndTrace()
351
Dan Willemsenb82471a2018-05-17 16:37:09 -0700352 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -0700353 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
354 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700355
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200356 ninjaArgs := []string{
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700357 "-d", "keepdepfile",
Dan Willemsen08218222020-05-18 14:02:02 -0700358 "-d", "stats",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700359 "-o", "usesphonyoutputs=yes",
360 "-o", "preremoveoutputs=yes",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700361 "-w", "dupbuild=err",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700362 "-w", "outputdir=err",
363 "-w", "missingoutfile=err",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700364 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700365 "--frontend_file", fifo,
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200366 "-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 Chen7c6089a2020-11-02 02:56:20 -0500372
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100373 var ninjaEnv Environment
Lukacs T. Berki73ab9282021-03-10 10:48:39 +0100374
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. Berkib14ad7b2021-03-09 10:43:57 +0100377 ninjaEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100378
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100379 cmd.Environment = &ninjaEnv
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700380 cmd.Sandbox = soongSandbox
Colin Cross7b97ecd2019-06-19 13:17:59 -0700381 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700382 }
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200383
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200384 targets := make([]string, 0, 0)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200385
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200386 if config.JsonModuleGraph() {
387 targets = append(targets, config.ModuleGraphFile())
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200388 }
389
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200390 if config.Bp2Build() {
391 targets = append(targets, config.Bp2BuildMarkerFile())
392 }
393
Lukacs T. Berki3a821692021-09-06 17:08:02 +0200394 if config.Queryview() {
395 targets = append(targets, config.QueryviewMarkerFile())
396 }
397
Lukacs T. Berkic6012f32021-09-06 18:31:46 +0200398 if config.SoongDocs() {
399 targets = append(targets, config.SoongDocsHtml())
400 }
401
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200402 if config.SoongBuildInvocationNeeded() {
403 // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
Cole Faust65f298c2021-10-28 16:05:13 -0700404 targets = append(targets, config.SoongNinjaFile())
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200405 }
406
407 ninja("bootstrap", ".bootstrap/build.ninja", targets...)
Colin Crossb72c9092020-02-10 11:23:49 -0800408
Jingwen Cheneb76c432021-01-28 08:22:12 -0500409 var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
410 if shouldCollectBuildSoongMetrics(config) {
411 soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
412 logSoongBuildMetrics(ctx, soongBuildMetrics)
413 }
Colin Crossb72c9092020-02-10 11:23:49 -0800414
Colin Cross8ba7d472020-06-25 11:27:52 -0700415 distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
416
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000417 if !config.SkipKati() {
Colin Cross8ba7d472020-06-25 11:27:52 -0700418 distGzipFile(ctx, config, config.SoongAndroidMk(), "soong")
419 distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
420 }
421
Jingwen Cheneb76c432021-01-28 08:22:12 -0500422 if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil {
Colin Crossb72c9092020-02-10 11:23:49 -0800423 ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
424 }
425}
426
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700427func 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 Cheneb76c432021-01-28 08:22:12 -0500446func shouldCollectBuildSoongMetrics(config Config) bool {
Jingwen Chendd9725c2021-06-24 08:41:16 +0000447 // Do not collect metrics protobuf if the soong_build binary ran as the
448 // bp2build converter or the JSON graph dump.
Lukacs T. Berkia1b93722021-09-02 17:23:06 +0200449 return config.SoongBuildInvocationNeeded()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500450}
451
Colin Crossb72c9092020-02-10 11:23:49 -0800452func 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
466func 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 Willemsen1e704462016-08-21 15:17:17 -0700474}