blob: a627daed6657970c9a54b825f8e357c7dfbf3a9f [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"
40)
41
Lukacs T. Berki7690c092021-02-26 14:27:36 +010042func writeEnvironmentFile(ctx Context, envFile string, envDeps map[string]string) error {
43 data, err := shared.EnvFileContents(envDeps)
44 if err != nil {
45 return err
46 }
47
48 return ioutil.WriteFile(envFile, data, 0644)
49}
50
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000051// This uses Android.bp files and various tools to generate <builddir>/build.ninja.
52//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010053// However, the execution of <builddir>/build.ninja happens later in
54// build/soong/ui/build/build.go#Build()
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000055//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010056// We want to rely on as few prebuilts as possible, so we need to bootstrap
57// Soong. The process is as follows:
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000058//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010059// 1. We use "Microfactory", a simple tool to compile Go code, to build
60// first itself, then soong_ui from soong_ui.bash. This binary contains
61// parts of soong_build that are needed to build itself.
62// 2. This simplified version of soong_build then reads the Blueprint files
63// that describe itself and emits .bootstrap/build.ninja that describes
64// how to build its full version and use that to produce the final Ninja
65// file Soong emits.
66// 3. soong_ui executes .bootstrap/build.ninja
Rupert Shuttleworthb7d97102020-11-25 10:19:29 +000067//
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010068// (After this, Kati is executed to parse the Makefiles, but that's not part of
69// bootstrapping Soong)
70
71// A tiny struct used to tell Blueprint that it's in bootstrap mode. It would
72// probably be nicer to use a flag in bootstrap.Args instead.
73type BlueprintConfig struct {
Lukacs T. Berkia806e412021-09-01 08:57:48 +020074 toolDir string
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020075 soongOutDir string
76 outDir string
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +010077 debugCompilation bool
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010078}
79
Lukacs T. Berkia806e412021-09-01 08:57:48 +020080func (c BlueprintConfig) HostToolDir() string {
81 return c.toolDir
82}
83
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020084func (c BlueprintConfig) SoongOutDir() string {
85 return c.soongOutDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010086}
87
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +020088func (c BlueprintConfig) OutDir() string {
89 return c.outDir
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +010090}
91
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +010092func (c BlueprintConfig) DebugCompilation() bool {
93 return c.debugCompilation
94}
95
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020096func environmentArgs(config Config, suffix string) []string {
97 return []string{
98 "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile),
99 "--used_env", shared.JoinPath(config.SoongOutDir(), usedEnvFile+suffix),
100 }
101}
Spandan Das8f99ae62021-06-11 16:48:06 +0000102
103func writeEmptyGlobFile(ctx Context, path string) {
104 err := os.MkdirAll(filepath.Dir(path), 0777)
105 if err != nil {
106 ctx.Fatalf("Failed to create parent directories of empty ninja glob file '%s': %s", path, err)
107 }
108
109 if _, err := os.Stat(path); os.IsNotExist(err) {
110 err = ioutil.WriteFile(path, nil, 0666)
111 if err != nil {
112 ctx.Fatalf("Failed to create empty ninja glob file '%s': %s", path, err)
113 }
114 }
115}
116
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200117func bootstrapBlueprint(ctx Context, config Config) {
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100118 ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap")
119 defer ctx.EndTrace()
120
121 var args bootstrap.Args
122
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200123 bootstrapGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.ninja")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200124 bp2buildGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.bp2build.ninja")
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200125 moduleGraphGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.modulegraph.ninja")
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200126
127 // The glob .ninja files are subninja'd. However, they are generated during
128 // the build itself so we write an empty file so that the subninja doesn't
129 // fail on clean builds
Spandan Das8f99ae62021-06-11 16:48:06 +0000130 writeEmptyGlobFile(ctx, bootstrapGlobFile)
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200131 writeEmptyGlobFile(ctx, bp2buildGlobFile)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200132 bootstrapDepFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja.d")
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200133
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100134 args.RunGoTests = !config.skipSoongTests
135 args.UseValidations = true // Use validations to depend on tests
Lukacs T. Berkib078ade2021-08-31 10:42:08 +0200136 args.SoongOutDir = config.SoongOutDir()
137 args.OutDir = config.OutDir()
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100138 args.ModuleListFile = filepath.Join(config.FileListDir(), "Android.bp.list")
139 args.OutFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja")
Spandan Das8f99ae62021-06-11 16:48:06 +0000140 // The primary builder (aka soong_build) will use bootstrapGlobFile as the globFile to generate build.ninja(.d)
141 // Building soong_build does not require a glob file
142 // Using "" instead of "<soong_build_glob>.ninja" will ensure that an unused glob file is not written to out/soong/.bootstrap during StagePrimary
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200143 args.Subninjas = []string{bootstrapGlobFile, bp2buildGlobFile}
Colin Crossf3bdbcb2021-06-01 11:43:55 -0700144 args.EmptyNinjaFile = config.EmptyNinjaFile()
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100145
Lukacs T. Berki745380c2021-04-12 12:07:44 +0200146 args.DelveListen = os.Getenv("SOONG_DELVE")
147 if args.DelveListen != "" {
148 args.DelvePath = shared.ResolveDelveBinary()
149 }
150
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200151 commonArgs := bootstrap.PrimaryBuilderExtraFlags(args, config.MainNinjaFile())
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200152 mainSoongBuildInputs := []string{"Android.bp"}
153
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200154 if config.bazelBuildMode() == mixedBuild {
155 mainSoongBuildInputs = append(mainSoongBuildInputs, config.Bp2BuildMarkerFile())
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200156 }
157
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200158 soongBuildArgs := []string{
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200159 "--globListDir", "build",
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200160 "--globFile", bootstrapGlobFile,
161 }
162
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200163 soongBuildArgs = append(soongBuildArgs, commonArgs...)
164 soongBuildArgs = append(soongBuildArgs, environmentArgs(config, "")...)
165 soongBuildArgs = append(soongBuildArgs, "Android.bp")
166
167 mainSoongBuildInvocation := bootstrap.PrimaryBuilderInvocation{
168 Inputs: mainSoongBuildInputs,
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200169 Outputs: []string{config.MainNinjaFile()},
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200170 Args: soongBuildArgs,
171 }
172
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200173 bp2buildArgs := []string{
174 "--bp2build_marker", config.Bp2BuildMarkerFile(),
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200175 "--globListDir", "bp2build",
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200176 "--globFile", bp2buildGlobFile,
177 }
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200178
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200179 bp2buildArgs = append(bp2buildArgs, commonArgs...)
180 bp2buildArgs = append(bp2buildArgs, environmentArgs(config, ".bp2build")...)
181 bp2buildArgs = append(bp2buildArgs, "Android.bp")
182
183 bp2buildInvocation := bootstrap.PrimaryBuilderInvocation{
184 Inputs: []string{"Android.bp"},
185 Outputs: []string{config.Bp2BuildMarkerFile()},
186 Args: bp2buildArgs,
187 }
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200188
189 moduleGraphArgs := []string{
190 "--module_graph_file", config.ModuleGraphFile(),
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200191 "--globListDir", "modulegraph",
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200192 "--globFile", moduleGraphGlobFile,
193 }
194
195 moduleGraphArgs = append(moduleGraphArgs, commonArgs...)
196 moduleGraphArgs = append(moduleGraphArgs, environmentArgs(config, ".modulegraph")...)
197 moduleGraphArgs = append(moduleGraphArgs, "Android.bp")
198
199 moduleGraphInvocation := bootstrap.PrimaryBuilderInvocation{
200 Inputs: []string{"Android.bp"},
201 Outputs: []string{config.ModuleGraphFile()},
202 Args: moduleGraphArgs,
203 }
204
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200205 args.PrimaryBuilderInvocations = []bootstrap.PrimaryBuilderInvocation{
206 bp2buildInvocation,
207 mainSoongBuildInvocation,
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200208 moduleGraphInvocation,
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200209 }
210
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100211 blueprintCtx := blueprint.NewContext()
212 blueprintCtx.SetIgnoreUnknownModuleTypes(true)
213 blueprintConfig := BlueprintConfig{
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200214 soongOutDir: config.SoongOutDir(),
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200215 toolDir: config.HostToolDir(),
Lukacs T. Berki9f6c24a2021-08-26 15:07:24 +0200216 outDir: config.OutDir(),
Lukacs T. Berki5f6cb1d2021-03-17 15:03:14 +0100217 debugCompilation: os.Getenv("SOONG_DELVE") != "",
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100218 }
219
Lukacs T. Berkib078ade2021-08-31 10:42:08 +0200220 args.EmptyNinjaFile = false
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200221 bootstrapDeps := bootstrap.RunBlueprint(args, blueprintCtx, blueprintConfig)
222 err := deptools.WriteDepFile(bootstrapDepFile, args.OutFile, bootstrapDeps)
223 if err != nil {
224 ctx.Fatalf("Error writing depfile '%s': %s", bootstrapDepFile, err)
225 }
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100226}
227
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200228func checkEnvironmentFile(currentEnv *Environment, envFile string) {
229 getenv := func(k string) string {
230 v, _ := currentEnv.Get(k)
231 return v
232 }
233 if stale, _ := shared.StaleEnvFile(envFile, getenv); stale {
234 os.Remove(envFile)
235 }
236}
237
Dan Willemsen1e704462016-08-21 15:17:17 -0700238func runSoong(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800239 ctx.BeginTrace(metrics.RunSoong, "soong")
Dan Willemsend9f6fa22016-08-21 15:17:17 -0700240 defer ctx.EndTrace()
241
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100242 // We have two environment files: .available is the one with every variable,
243 // .used with the ones that were actually used. The latter is used to
244 // determine whether Soong needs to be re-run since why re-run it if only
245 // unused variables were changed?
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200246 envFile := filepath.Join(config.SoongOutDir(), availableEnvFile)
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100247
Lukacs T. Berki9d7cf6a2021-08-16 14:08:57 +0200248 dir := filepath.Join(config.SoongOutDir(), ".bootstrap")
249 if err := os.MkdirAll(dir, 0755); err != nil {
250 ctx.Fatalf("Cannot mkdir " + dir)
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100251 }
Colin Cross00a8a3f2020-10-29 14:08:31 -0700252
Chris Parsonsec1a3dc2021-04-20 15:32:07 -0400253 buildMode := config.bazelBuildMode()
254 integratedBp2Build := (buildMode == mixedBuild) || (buildMode == generateBuildFiles)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200255
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100256 // This is done unconditionally, but does not take a measurable amount of time
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200257 bootstrapBlueprint(ctx, config)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700258
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100259 soongBuildEnv := config.Environment().Copy()
260 soongBuildEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100261 // For Bazel mixed builds.
262 soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel")
263 soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome"))
264 soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output"))
265 soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, "."))
266 soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir())
267
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100268 // For Soong bootstrapping tests
269 if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
270 soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true")
271 }
272
Paul Duffin5e85c662021-03-05 12:26:14 +0000273 err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap())
274 if err != nil {
275 ctx.Fatalf("failed to write environment file %s: %s", envFile, err)
276 }
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100277
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700278 func() {
Nan Zhang17f27672018-12-12 16:01:49 -0800279 ctx.BeginTrace(metrics.RunSoong, "environment check")
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700280 defer ctx.EndTrace()
281
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200282 soongBuildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile)
283 checkEnvironmentFile(soongBuildEnv, soongBuildEnvFile)
284
285 if integratedBp2Build {
286 bp2buildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile+".bp2build")
287 checkEnvironmentFile(soongBuildEnv, bp2buildEnvFile)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700288 }
289 }()
290
Lukacs T. Berkia806e412021-09-01 08:57:48 +0200291 runMicrofactory(ctx, config, filepath.Join(config.HostToolDir(), "bpglob"), "github.com/google/blueprint/bootstrap/bpglob",
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700292 map[string]string{"github.com/google/blueprint": "build/blueprint"})
Dan Willemsen5af1cbe2018-07-05 21:46:51 -0700293
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200294 ninja := func(name, ninjaFile string, targets ...string) {
Nan Zhang17f27672018-12-12 16:01:49 -0800295 ctx.BeginTrace(metrics.RunSoong, name)
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700296 defer ctx.EndTrace()
297
Dan Willemsenb82471a2018-05-17 16:37:09 -0700298 fifo := filepath.Join(config.OutDir(), ".ninja_fifo")
Colin Crossb98d3bc2019-03-21 16:02:58 -0700299 nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo)
300 defer nr.Close()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700301
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200302 ninjaArgs := []string{
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700303 "-d", "keepdepfile",
Dan Willemsen08218222020-05-18 14:02:02 -0700304 "-d", "stats",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700305 "-o", "usesphonyoutputs=yes",
306 "-o", "preremoveoutputs=yes",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700307 "-w", "dupbuild=err",
Dan Willemsen6587bed2020-04-18 20:25:59 -0700308 "-w", "outputdir=err",
309 "-w", "missingoutfile=err",
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700310 "-j", strconv.Itoa(config.Parallel()),
Dan Willemsen02736672018-07-17 17:54:31 -0700311 "--frontend_file", fifo,
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200312 "-f", filepath.Join(config.SoongOutDir(), ninjaFile),
313 }
314
315 ninjaArgs = append(ninjaArgs, targets...)
316 cmd := Command(ctx, config, "soong "+name,
317 config.PrebuiltBuildTool("ninja"), ninjaArgs...)
Jingwen Chen7c6089a2020-11-02 02:56:20 -0500318
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100319 var ninjaEnv Environment
Lukacs T. Berki73ab9282021-03-10 10:48:39 +0100320
321 // This is currently how the command line to invoke soong_build finds the
322 // root of the source tree and the output root
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100323 ninjaEnv.Set("TOP", os.Getenv("TOP"))
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100324
Lukacs T. Berkib14ad7b2021-03-09 10:43:57 +0100325 cmd.Environment = &ninjaEnv
Dan Willemsen99a75cd2017-08-04 16:04:04 -0700326 cmd.Sandbox = soongSandbox
Colin Cross7b97ecd2019-06-19 13:17:59 -0700327 cmd.RunAndStreamOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700328 }
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200329
330 var target string
331
332 if config.bazelBuildMode() == generateBuildFiles {
333 target = config.Bp2BuildMarkerFile()
Lukacs T. Berkie571dc32021-08-25 14:14:13 +0200334 } else if config.bazelBuildMode() == generateJsonModuleGraph {
335 target = config.ModuleGraphFile()
Lukacs T. Berki56ebaf32021-08-12 14:03:55 +0200336 } else {
337 // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build().
338 target = config.MainNinjaFile()
339 }
340
341 ninja("bootstrap", ".bootstrap/build.ninja", target)
Colin Crossb72c9092020-02-10 11:23:49 -0800342
Jingwen Cheneb76c432021-01-28 08:22:12 -0500343 var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics
344 if shouldCollectBuildSoongMetrics(config) {
345 soongBuildMetrics := loadSoongBuildMetrics(ctx, config)
346 logSoongBuildMetrics(ctx, soongBuildMetrics)
347 }
Colin Crossb72c9092020-02-10 11:23:49 -0800348
Colin Cross8ba7d472020-06-25 11:27:52 -0700349 distGzipFile(ctx, config, config.SoongNinjaFile(), "soong")
350
Anton Hansson5e5c48b2020-11-27 12:35:20 +0000351 if !config.SkipKati() {
Colin Cross8ba7d472020-06-25 11:27:52 -0700352 distGzipFile(ctx, config, config.SoongAndroidMk(), "soong")
353 distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong")
354 }
355
Jingwen Cheneb76c432021-01-28 08:22:12 -0500356 if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil {
Colin Crossb72c9092020-02-10 11:23:49 -0800357 ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics)
358 }
359}
360
Sasha Smundak7ae80a72021-04-09 12:03:51 -0700361func runMicrofactory(ctx Context, config Config, relExePath string, pkg string, mapping map[string]string) {
362 name := filepath.Base(relExePath)
363 ctx.BeginTrace(metrics.RunSoong, name)
364 defer ctx.EndTrace()
365 cfg := microfactory.Config{TrimPath: absPath(ctx, ".")}
366 for pkgPrefix, pathPrefix := range mapping {
367 cfg.Map(pkgPrefix, pathPrefix)
368 }
369
370 exePath := filepath.Join(config.SoongOutDir(), relExePath)
371 dir := filepath.Dir(exePath)
372 if err := os.MkdirAll(dir, 0777); err != nil {
373 ctx.Fatalf("cannot create %s: %s", dir, err)
374 }
375 if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil {
376 ctx.Fatalf("failed to build %s: %s", name, err)
377 }
378}
379
Jingwen Cheneb76c432021-01-28 08:22:12 -0500380func shouldCollectBuildSoongMetrics(config Config) bool {
Jingwen Chendd9725c2021-06-24 08:41:16 +0000381 // Do not collect metrics protobuf if the soong_build binary ran as the
382 // bp2build converter or the JSON graph dump.
383 return config.bazelBuildMode() != generateBuildFiles && config.bazelBuildMode() != generateJsonModuleGraph
Jingwen Cheneb76c432021-01-28 08:22:12 -0500384}
385
Colin Crossb72c9092020-02-10 11:23:49 -0800386func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics {
387 soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb")
388 buf, err := ioutil.ReadFile(soongBuildMetricsFile)
389 if err != nil {
390 ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err)
391 }
392 soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{}
393 err = proto.Unmarshal(buf, soongBuildMetrics)
394 if err != nil {
395 ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err)
396 }
397 return soongBuildMetrics
398}
399
400func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) {
401 ctx.Verbosef("soong_build metrics:")
402 ctx.Verbosef(" modules: %v", metrics.GetModules())
403 ctx.Verbosef(" variants: %v", metrics.GetVariants())
404 ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6)
405 ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount())
406 ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6)
407
Dan Willemsen1e704462016-08-21 15:17:17 -0700408}