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 | |
Lukacs T. Berki | 3243aa5 | 2021-02-25 14:44:14 +0100 | [diff] [blame] | 23 | "android/soong/shared" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 24 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 25 | soong_metrics_proto "android/soong/ui/metrics/metrics_proto" |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/bootstrap" |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 28 | |
| 29 | "github.com/golang/protobuf/proto" |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint/microfactory" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 31 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 32 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 33 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 34 | ) |
| 35 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 36 | const ( |
| 37 | availableEnvFile = "soong.environment.available" |
| 38 | usedEnvFile = "soong.environment.used" |
| 39 | ) |
| 40 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 41 | func writeEnvironmentFile(ctx Context, envFile string, envDeps map[string]string) error { |
| 42 | data, err := shared.EnvFileContents(envDeps) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | return ioutil.WriteFile(envFile, data, 0644) |
| 48 | } |
| 49 | |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 50 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 51 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 52 | // However, the execution of <builddir>/build.ninja happens later in |
| 53 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 54 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 55 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 56 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 57 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 58 | // 1. We use "Microfactory", a simple tool to compile Go code, to build |
| 59 | // first itself, then soong_ui from soong_ui.bash. This binary contains |
| 60 | // parts of soong_build that are needed to build itself. |
| 61 | // 2. This simplified version of soong_build then reads the Blueprint files |
| 62 | // that describe itself and emits .bootstrap/build.ninja that describes |
| 63 | // how to build its full version and use that to produce the final Ninja |
| 64 | // file Soong emits. |
| 65 | // 3. soong_ui executes .bootstrap/build.ninja |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 66 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 67 | // (After this, Kati is executed to parse the Makefiles, but that's not part of |
| 68 | // bootstrapping Soong) |
| 69 | |
| 70 | // A tiny struct used to tell Blueprint that it's in bootstrap mode. It would |
| 71 | // probably be nicer to use a flag in bootstrap.Args instead. |
| 72 | type BlueprintConfig struct { |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 73 | srcDir string |
| 74 | buildDir string |
| 75 | ninjaBuildDir string |
| 76 | debugCompilation bool |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 77 | } |
| 78 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 79 | func (c BlueprintConfig) SrcDir() string { |
| 80 | return "." |
| 81 | } |
| 82 | |
| 83 | func (c BlueprintConfig) BuildDir() string { |
| 84 | return c.buildDir |
| 85 | } |
| 86 | |
| 87 | func (c BlueprintConfig) NinjaBuildDir() string { |
| 88 | return c.ninjaBuildDir |
| 89 | } |
| 90 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 91 | func (c BlueprintConfig) DebugCompilation() bool { |
| 92 | return c.debugCompilation |
| 93 | } |
| 94 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 95 | func environmentArgs(config Config, suffix string) []string { |
| 96 | return []string{ |
| 97 | "--available_env", shared.JoinPath(config.SoongOutDir(), availableEnvFile), |
| 98 | "--used_env", shared.JoinPath(config.SoongOutDir(), usedEnvFile+suffix), |
| 99 | } |
| 100 | } |
| 101 | func bootstrapBlueprint(ctx Context, config Config, integratedBp2Build bool) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 102 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 103 | defer ctx.EndTrace() |
| 104 | |
| 105 | var args bootstrap.Args |
| 106 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 107 | mainNinjaFile := shared.JoinPath(config.SoongOutDir(), "build.ninja") |
| 108 | globFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/soong-build-globs.ninja") |
| 109 | bootstrapGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.ninja") |
| 110 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 111 | args.RunGoTests = !config.skipSoongTests |
| 112 | args.UseValidations = true // Use validations to depend on tests |
| 113 | args.BuildDir = config.SoongOutDir() |
| 114 | args.NinjaBuildDir = config.OutDir() |
| 115 | args.TopFile = "Android.bp" |
| 116 | args.ModuleListFile = filepath.Join(config.FileListDir(), "Android.bp.list") |
| 117 | args.OutFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja") |
| 118 | args.DepFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja.d") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 119 | args.GlobFile = globFile |
Lukacs T. Berki | d7ce840 | 2021-03-17 14:03:51 +0100 | [diff] [blame] | 120 | args.GeneratingPrimaryBuilder = true |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 121 | |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 122 | args.DelveListen = os.Getenv("SOONG_DELVE") |
| 123 | if args.DelveListen != "" { |
| 124 | args.DelvePath = shared.ResolveDelveBinary() |
| 125 | } |
| 126 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 127 | commonArgs := bootstrap.PrimaryBuilderExtraFlags(args, bootstrapGlobFile, mainNinjaFile) |
| 128 | bp2BuildMarkerFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/bp2build_workspace_marker") |
| 129 | mainSoongBuildInputs := []string{"Android.bp"} |
| 130 | |
| 131 | if integratedBp2Build { |
| 132 | mainSoongBuildInputs = append(mainSoongBuildInputs, bp2BuildMarkerFile) |
| 133 | } |
| 134 | |
| 135 | soongBuildArgs := make([]string, 0) |
| 136 | soongBuildArgs = append(soongBuildArgs, commonArgs...) |
| 137 | soongBuildArgs = append(soongBuildArgs, environmentArgs(config, "")...) |
| 138 | soongBuildArgs = append(soongBuildArgs, "Android.bp") |
| 139 | |
| 140 | mainSoongBuildInvocation := bootstrap.PrimaryBuilderInvocation{ |
| 141 | Inputs: mainSoongBuildInputs, |
| 142 | Outputs: []string{mainNinjaFile}, |
| 143 | Args: soongBuildArgs, |
| 144 | } |
| 145 | |
| 146 | if integratedBp2Build { |
| 147 | bp2buildArgs := []string{"--bp2build_marker", bp2BuildMarkerFile} |
| 148 | bp2buildArgs = append(bp2buildArgs, commonArgs...) |
| 149 | bp2buildArgs = append(bp2buildArgs, environmentArgs(config, ".bp2build")...) |
| 150 | bp2buildArgs = append(bp2buildArgs, "Android.bp") |
| 151 | |
| 152 | bp2buildInvocation := bootstrap.PrimaryBuilderInvocation{ |
| 153 | Inputs: []string{"Android.bp"}, |
| 154 | Outputs: []string{bp2BuildMarkerFile}, |
| 155 | Args: bp2buildArgs, |
| 156 | } |
| 157 | args.PrimaryBuilderInvocations = []bootstrap.PrimaryBuilderInvocation{ |
| 158 | bp2buildInvocation, |
| 159 | mainSoongBuildInvocation, |
| 160 | } |
| 161 | } else { |
| 162 | args.PrimaryBuilderInvocations = []bootstrap.PrimaryBuilderInvocation{mainSoongBuildInvocation} |
| 163 | } |
| 164 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 165 | blueprintCtx := blueprint.NewContext() |
| 166 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 167 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 168 | srcDir: os.Getenv("TOP"), |
| 169 | buildDir: config.SoongOutDir(), |
| 170 | ninjaBuildDir: config.OutDir(), |
| 171 | debugCompilation: os.Getenv("SOONG_DELVE") != "", |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | bootstrap.RunBlueprint(args, blueprintCtx, blueprintConfig) |
| 175 | } |
| 176 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 177 | func checkEnvironmentFile(currentEnv *Environment, envFile string) { |
| 178 | getenv := func(k string) string { |
| 179 | v, _ := currentEnv.Get(k) |
| 180 | return v |
| 181 | } |
| 182 | if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 183 | os.Remove(envFile) |
| 184 | } |
| 185 | } |
| 186 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 187 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 188 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 189 | defer ctx.EndTrace() |
| 190 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 191 | // We have two environment files: .available is the one with every variable, |
| 192 | // .used with the ones that were actually used. The latter is used to |
| 193 | // determine whether Soong needs to be re-run since why re-run it if only |
| 194 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 195 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 196 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 197 | for _, n := range []string{".bootstrap", ".minibootstrap"} { |
| 198 | dir := filepath.Join(config.SoongOutDir(), n) |
| 199 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 200 | ctx.Fatalf("Cannot mkdir " + dir) |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 201 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 202 | } |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 203 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 204 | integratedBp2Build := config.Environment().IsEnvTrue("INTEGRATED_BP2BUILD") |
| 205 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 206 | // This is done unconditionally, but does not take a measurable amount of time |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 207 | bootstrapBlueprint(ctx, config, integratedBp2Build) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 208 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 209 | soongBuildEnv := config.Environment().Copy() |
| 210 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 211 | // For Bazel mixed builds. |
| 212 | soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel") |
| 213 | soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome")) |
| 214 | soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output")) |
| 215 | soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
| 216 | soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
| 217 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 218 | // For Soong bootstrapping tests |
| 219 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 220 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 221 | } |
| 222 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 223 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 224 | if err != nil { |
| 225 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 226 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 227 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 228 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 229 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 230 | defer ctx.EndTrace() |
| 231 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 232 | soongBuildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile) |
| 233 | checkEnvironmentFile(soongBuildEnv, soongBuildEnvFile) |
| 234 | |
| 235 | if integratedBp2Build { |
| 236 | bp2buildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile+".bp2build") |
| 237 | checkEnvironmentFile(soongBuildEnv, bp2buildEnvFile) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 238 | } |
| 239 | }() |
| 240 | |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 241 | var cfg microfactory.Config |
| 242 | cfg.Map("github.com/google/blueprint", "build/blueprint") |
| 243 | |
| 244 | cfg.TrimPath = absPath(ctx, ".") |
| 245 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 246 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 247 | ctx.BeginTrace(metrics.RunSoong, "bpglob") |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 248 | defer ctx.EndTrace() |
| 249 | |
| 250 | bpglob := filepath.Join(config.SoongOutDir(), ".minibootstrap/bpglob") |
| 251 | if _, err := microfactory.Build(&cfg, bpglob, "github.com/google/blueprint/bootstrap/bpglob"); err != nil { |
| 252 | ctx.Fatalln("Failed to build bpglob:", err) |
| 253 | } |
| 254 | }() |
| 255 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 256 | ninja := func(name, file string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 257 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 258 | defer ctx.EndTrace() |
| 259 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 260 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 261 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 262 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 263 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 264 | cmd := Command(ctx, config, "soong "+name, |
| 265 | config.PrebuiltBuildTool("ninja"), |
| 266 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 267 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 268 | "-o", "usesphonyoutputs=yes", |
| 269 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 270 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 271 | "-w", "outputdir=err", |
| 272 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 273 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 274 | "--frontend_file", fifo, |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 275 | "-f", filepath.Join(config.SoongOutDir(), file)) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 276 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 277 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 278 | |
| 279 | // This is currently how the command line to invoke soong_build finds the |
| 280 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 281 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 282 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 283 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 284 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 285 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 286 | } |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 287 | // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build(). |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 288 | ninja("bootstrap", ".bootstrap/build.ninja") |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 289 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 290 | var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics |
| 291 | if shouldCollectBuildSoongMetrics(config) { |
| 292 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 293 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 294 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 295 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 296 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
| 297 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 298 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 299 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 300 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 301 | } |
| 302 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 303 | if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 304 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 305 | } |
| 306 | } |
| 307 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 308 | func shouldCollectBuildSoongMetrics(config Config) bool { |
| 309 | // Do not collect metrics protobuf if the soong_build binary ran as the bp2build converter. |
| 310 | return config.Environment().IsFalse("GENERATE_BAZEL_FILES") |
| 311 | } |
| 312 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 313 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 314 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 315 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 316 | if err != nil { |
| 317 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 318 | } |
| 319 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 320 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 321 | if err != nil { |
| 322 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 323 | } |
| 324 | return soongBuildMetrics |
| 325 | } |
| 326 | |
| 327 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 328 | ctx.Verbosef("soong_build metrics:") |
| 329 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 330 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 331 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 332 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 333 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 334 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 335 | } |