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" |
| 40 | ) |
| 41 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 42 | func 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 Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 51 | // This uses Android.bp files and various tools to generate <builddir>/build.ninja. |
| 52 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 53 | // However, the execution of <builddir>/build.ninja happens later in |
| 54 | // build/soong/ui/build/build.go#Build() |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 55 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 56 | // We want to rely on as few prebuilts as possible, so we need to bootstrap |
| 57 | // Soong. The process is as follows: |
Rupert Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 58 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 59 | // 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 Shuttleworth | b7d9710 | 2020-11-25 10:19:29 +0000 | [diff] [blame] | 67 | // |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 68 | // (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. |
| 73 | type BlueprintConfig struct { |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 74 | srcDir string |
| 75 | buildDir string |
| 76 | ninjaBuildDir string |
| 77 | debugCompilation bool |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 80 | func (c BlueprintConfig) SrcDir() string { |
| 81 | return "." |
| 82 | } |
| 83 | |
| 84 | func (c BlueprintConfig) BuildDir() string { |
| 85 | return c.buildDir |
| 86 | } |
| 87 | |
| 88 | func (c BlueprintConfig) NinjaBuildDir() string { |
| 89 | return c.ninjaBuildDir |
| 90 | } |
| 91 | |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 92 | func (c BlueprintConfig) DebugCompilation() bool { |
| 93 | return c.debugCompilation |
| 94 | } |
| 95 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 96 | func 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 Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 102 | |
| 103 | func 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. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 117 | func bootstrapBlueprint(ctx Context, config Config) { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 118 | ctx.BeginTrace(metrics.RunSoong, "blueprint bootstrap") |
| 119 | defer ctx.EndTrace() |
| 120 | |
| 121 | var args bootstrap.Args |
| 122 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 123 | bootstrapGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.ninja") |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 124 | bp2buildGlobFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build-globs.bp2build.ninja") |
| 125 | |
| 126 | // The glob .ninja files are subninja'd. However, they are generated during |
| 127 | // the build itself so we write an empty file so that the subninja doesn't |
| 128 | // fail on clean builds |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 129 | writeEmptyGlobFile(ctx, bootstrapGlobFile) |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 130 | writeEmptyGlobFile(ctx, bp2buildGlobFile) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 131 | bootstrapDepFile := shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja.d") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 132 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 133 | args.RunGoTests = !config.skipSoongTests |
| 134 | args.UseValidations = true // Use validations to depend on tests |
| 135 | args.BuildDir = config.SoongOutDir() |
| 136 | args.NinjaBuildDir = config.OutDir() |
| 137 | args.TopFile = "Android.bp" |
| 138 | args.ModuleListFile = filepath.Join(config.FileListDir(), "Android.bp.list") |
| 139 | args.OutFile = shared.JoinPath(config.SoongOutDir(), ".bootstrap/build.ninja") |
Spandan Das | 8f99ae6 | 2021-06-11 16:48:06 +0000 | [diff] [blame] | 140 | // 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. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 143 | args.Subninjas = []string{bootstrapGlobFile, bp2buildGlobFile} |
Lukacs T. Berki | d7ce840 | 2021-03-17 14:03:51 +0100 | [diff] [blame] | 144 | args.GeneratingPrimaryBuilder = true |
Colin Cross | f3bdbcb | 2021-06-01 11:43:55 -0700 | [diff] [blame] | 145 | args.EmptyNinjaFile = config.EmptyNinjaFile() |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 146 | |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 147 | args.DelveListen = os.Getenv("SOONG_DELVE") |
| 148 | if args.DelveListen != "" { |
| 149 | args.DelvePath = shared.ResolveDelveBinary() |
| 150 | } |
| 151 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 152 | commonArgs := bootstrap.PrimaryBuilderExtraFlags(args, config.MainNinjaFile()) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 153 | mainSoongBuildInputs := []string{"Android.bp"} |
| 154 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 155 | if config.bazelBuildMode() == mixedBuild { |
| 156 | mainSoongBuildInputs = append(mainSoongBuildInputs, config.Bp2BuildMarkerFile()) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 157 | } |
| 158 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 159 | soongBuildArgs := []string{ |
| 160 | "--globListDir", "globs", |
| 161 | "--globFile", bootstrapGlobFile, |
| 162 | } |
| 163 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 164 | soongBuildArgs = append(soongBuildArgs, commonArgs...) |
| 165 | soongBuildArgs = append(soongBuildArgs, environmentArgs(config, "")...) |
| 166 | soongBuildArgs = append(soongBuildArgs, "Android.bp") |
| 167 | |
| 168 | mainSoongBuildInvocation := bootstrap.PrimaryBuilderInvocation{ |
| 169 | Inputs: mainSoongBuildInputs, |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 170 | Outputs: []string{config.MainNinjaFile()}, |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 171 | Args: soongBuildArgs, |
| 172 | } |
| 173 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 174 | bp2buildArgs := []string{ |
| 175 | "--bp2build_marker", config.Bp2BuildMarkerFile(), |
| 176 | "--globListDir", "globs.bp2build", |
| 177 | "--globFile", bp2buildGlobFile, |
| 178 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 179 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 180 | bp2buildArgs = append(bp2buildArgs, commonArgs...) |
| 181 | bp2buildArgs = append(bp2buildArgs, environmentArgs(config, ".bp2build")...) |
| 182 | bp2buildArgs = append(bp2buildArgs, "Android.bp") |
| 183 | |
| 184 | bp2buildInvocation := bootstrap.PrimaryBuilderInvocation{ |
| 185 | Inputs: []string{"Android.bp"}, |
| 186 | Outputs: []string{config.Bp2BuildMarkerFile()}, |
| 187 | Args: bp2buildArgs, |
| 188 | } |
| 189 | args.PrimaryBuilderInvocations = []bootstrap.PrimaryBuilderInvocation{ |
| 190 | bp2buildInvocation, |
| 191 | mainSoongBuildInvocation, |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 194 | blueprintCtx := blueprint.NewContext() |
| 195 | blueprintCtx.SetIgnoreUnknownModuleTypes(true) |
| 196 | blueprintConfig := BlueprintConfig{ |
Lukacs T. Berki | 5f6cb1d | 2021-03-17 15:03:14 +0100 | [diff] [blame] | 197 | srcDir: os.Getenv("TOP"), |
| 198 | buildDir: config.SoongOutDir(), |
| 199 | ninjaBuildDir: config.OutDir(), |
| 200 | debugCompilation: os.Getenv("SOONG_DELVE") != "", |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 203 | bootstrapDeps := bootstrap.RunBlueprint(args, blueprintCtx, blueprintConfig) |
| 204 | err := deptools.WriteDepFile(bootstrapDepFile, args.OutFile, bootstrapDeps) |
| 205 | if err != nil { |
| 206 | ctx.Fatalf("Error writing depfile '%s': %s", bootstrapDepFile, err) |
| 207 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 208 | } |
| 209 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 210 | func checkEnvironmentFile(currentEnv *Environment, envFile string) { |
| 211 | getenv := func(k string) string { |
| 212 | v, _ := currentEnv.Get(k) |
| 213 | return v |
| 214 | } |
| 215 | if stale, _ := shared.StaleEnvFile(envFile, getenv); stale { |
| 216 | os.Remove(envFile) |
| 217 | } |
| 218 | } |
| 219 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 220 | func runSoong(ctx Context, config Config) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 221 | ctx.BeginTrace(metrics.RunSoong, "soong") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 222 | defer ctx.EndTrace() |
| 223 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 224 | // We have two environment files: .available is the one with every variable, |
| 225 | // .used with the ones that were actually used. The latter is used to |
| 226 | // determine whether Soong needs to be re-run since why re-run it if only |
| 227 | // unused variables were changed? |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 228 | envFile := filepath.Join(config.SoongOutDir(), availableEnvFile) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 229 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 230 | for _, n := range []string{".bootstrap", ".minibootstrap"} { |
| 231 | dir := filepath.Join(config.SoongOutDir(), n) |
| 232 | if err := os.MkdirAll(dir, 0755); err != nil { |
| 233 | ctx.Fatalf("Cannot mkdir " + dir) |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 234 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 235 | } |
Colin Cross | 00a8a3f | 2020-10-29 14:08:31 -0700 | [diff] [blame] | 236 | |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 237 | buildMode := config.bazelBuildMode() |
| 238 | integratedBp2Build := (buildMode == mixedBuild) || (buildMode == generateBuildFiles) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 239 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 240 | // 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^] | 241 | bootstrapBlueprint(ctx, config) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 242 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 243 | soongBuildEnv := config.Environment().Copy() |
| 244 | soongBuildEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 245 | // For Bazel mixed builds. |
| 246 | soongBuildEnv.Set("BAZEL_PATH", "./tools/bazel") |
| 247 | soongBuildEnv.Set("BAZEL_HOME", filepath.Join(config.BazelOutDir(), "bazelhome")) |
| 248 | soongBuildEnv.Set("BAZEL_OUTPUT_BASE", filepath.Join(config.BazelOutDir(), "output")) |
| 249 | soongBuildEnv.Set("BAZEL_WORKSPACE", absPath(ctx, ".")) |
| 250 | soongBuildEnv.Set("BAZEL_METRICS_DIR", config.BazelMetricsDir()) |
| 251 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 252 | // For Soong bootstrapping tests |
| 253 | if os.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 254 | soongBuildEnv.Set("ALLOW_MISSING_DEPENDENCIES", "true") |
| 255 | } |
| 256 | |
Paul Duffin | 5e85c66 | 2021-03-05 12:26:14 +0000 | [diff] [blame] | 257 | err := writeEnvironmentFile(ctx, envFile, soongBuildEnv.AsMap()) |
| 258 | if err != nil { |
| 259 | ctx.Fatalf("failed to write environment file %s: %s", envFile, err) |
| 260 | } |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 261 | |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 262 | func() { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 263 | ctx.BeginTrace(metrics.RunSoong, "environment check") |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 264 | defer ctx.EndTrace() |
| 265 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 266 | soongBuildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile) |
| 267 | checkEnvironmentFile(soongBuildEnv, soongBuildEnvFile) |
| 268 | |
| 269 | if integratedBp2Build { |
| 270 | bp2buildEnvFile := filepath.Join(config.SoongOutDir(), usedEnvFile+".bp2build") |
| 271 | checkEnvironmentFile(soongBuildEnv, bp2buildEnvFile) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 272 | } |
| 273 | }() |
| 274 | |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 275 | runMicrofactory(ctx, config, ".minibootstrap/bpglob", "github.com/google/blueprint/bootstrap/bpglob", |
| 276 | map[string]string{"github.com/google/blueprint": "build/blueprint"}) |
Dan Willemsen | 5af1cbe | 2018-07-05 21:46:51 -0700 | [diff] [blame] | 277 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 278 | ninja := func(name, ninjaFile string, targets ...string) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 279 | ctx.BeginTrace(metrics.RunSoong, name) |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 280 | defer ctx.EndTrace() |
| 281 | |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 282 | fifo := filepath.Join(config.OutDir(), ".ninja_fifo") |
Colin Cross | b98d3bc | 2019-03-21 16:02:58 -0700 | [diff] [blame] | 283 | nr := status.NewNinjaReader(ctx, ctx.Status.StartTool(), fifo) |
| 284 | defer nr.Close() |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 285 | |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 286 | ninjaArgs := []string{ |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 287 | "-d", "keepdepfile", |
Dan Willemsen | 0821822 | 2020-05-18 14:02:02 -0700 | [diff] [blame] | 288 | "-d", "stats", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 289 | "-o", "usesphonyoutputs=yes", |
| 290 | "-o", "preremoveoutputs=yes", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 291 | "-w", "dupbuild=err", |
Dan Willemsen | 6587bed | 2020-04-18 20:25:59 -0700 | [diff] [blame] | 292 | "-w", "outputdir=err", |
| 293 | "-w", "missingoutfile=err", |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 294 | "-j", strconv.Itoa(config.Parallel()), |
Dan Willemsen | 0273667 | 2018-07-17 17:54:31 -0700 | [diff] [blame] | 295 | "--frontend_file", fifo, |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 296 | "-f", filepath.Join(config.SoongOutDir(), ninjaFile), |
| 297 | } |
| 298 | |
| 299 | ninjaArgs = append(ninjaArgs, targets...) |
| 300 | cmd := Command(ctx, config, "soong "+name, |
| 301 | config.PrebuiltBuildTool("ninja"), ninjaArgs...) |
Jingwen Chen | 7c6089a | 2020-11-02 02:56:20 -0500 | [diff] [blame] | 302 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 303 | var ninjaEnv Environment |
Lukacs T. Berki | 73ab928 | 2021-03-10 10:48:39 +0100 | [diff] [blame] | 304 | |
| 305 | // This is currently how the command line to invoke soong_build finds the |
| 306 | // root of the source tree and the output root |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 307 | ninjaEnv.Set("TOP", os.Getenv("TOP")) |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 308 | |
Lukacs T. Berki | b14ad7b | 2021-03-09 10:43:57 +0100 | [diff] [blame] | 309 | cmd.Environment = &ninjaEnv |
Dan Willemsen | 99a75cd | 2017-08-04 16:04:04 -0700 | [diff] [blame] | 310 | cmd.Sandbox = soongSandbox |
Colin Cross | 7b97ecd | 2019-06-19 13:17:59 -0700 | [diff] [blame] | 311 | cmd.RunAndStreamOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 312 | } |
Lukacs T. Berki | 56ebaf3 | 2021-08-12 14:03:55 +0200 | [diff] [blame^] | 313 | |
| 314 | var target string |
| 315 | |
| 316 | if config.bazelBuildMode() == generateBuildFiles { |
| 317 | target = config.Bp2BuildMarkerFile() |
| 318 | } else { |
| 319 | // This build generates <builddir>/build.ninja, which is used later by build/soong/ui/build/build.go#Build(). |
| 320 | target = config.MainNinjaFile() |
| 321 | } |
| 322 | |
| 323 | ninja("bootstrap", ".bootstrap/build.ninja", target) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 324 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 325 | var soongBuildMetrics *soong_metrics_proto.SoongBuildMetrics |
| 326 | if shouldCollectBuildSoongMetrics(config) { |
| 327 | soongBuildMetrics := loadSoongBuildMetrics(ctx, config) |
| 328 | logSoongBuildMetrics(ctx, soongBuildMetrics) |
| 329 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 330 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 331 | distGzipFile(ctx, config, config.SoongNinjaFile(), "soong") |
| 332 | |
Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 333 | if !config.SkipKati() { |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 334 | distGzipFile(ctx, config, config.SoongAndroidMk(), "soong") |
| 335 | distGzipFile(ctx, config, config.SoongMakeVarsMk(), "soong") |
| 336 | } |
| 337 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 338 | if shouldCollectBuildSoongMetrics(config) && ctx.Metrics != nil { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 339 | ctx.Metrics.SetSoongBuildMetrics(soongBuildMetrics) |
| 340 | } |
| 341 | } |
| 342 | |
Sasha Smundak | 7ae80a7 | 2021-04-09 12:03:51 -0700 | [diff] [blame] | 343 | func runMicrofactory(ctx Context, config Config, relExePath string, pkg string, mapping map[string]string) { |
| 344 | name := filepath.Base(relExePath) |
| 345 | ctx.BeginTrace(metrics.RunSoong, name) |
| 346 | defer ctx.EndTrace() |
| 347 | cfg := microfactory.Config{TrimPath: absPath(ctx, ".")} |
| 348 | for pkgPrefix, pathPrefix := range mapping { |
| 349 | cfg.Map(pkgPrefix, pathPrefix) |
| 350 | } |
| 351 | |
| 352 | exePath := filepath.Join(config.SoongOutDir(), relExePath) |
| 353 | dir := filepath.Dir(exePath) |
| 354 | if err := os.MkdirAll(dir, 0777); err != nil { |
| 355 | ctx.Fatalf("cannot create %s: %s", dir, err) |
| 356 | } |
| 357 | if _, err := microfactory.Build(&cfg, exePath, pkg); err != nil { |
| 358 | ctx.Fatalf("failed to build %s: %s", name, err) |
| 359 | } |
| 360 | } |
| 361 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 362 | func shouldCollectBuildSoongMetrics(config Config) bool { |
Jingwen Chen | dd9725c | 2021-06-24 08:41:16 +0000 | [diff] [blame] | 363 | // Do not collect metrics protobuf if the soong_build binary ran as the |
| 364 | // bp2build converter or the JSON graph dump. |
| 365 | return config.bazelBuildMode() != generateBuildFiles && config.bazelBuildMode() != generateJsonModuleGraph |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 368 | func loadSoongBuildMetrics(ctx Context, config Config) *soong_metrics_proto.SoongBuildMetrics { |
| 369 | soongBuildMetricsFile := filepath.Join(config.OutDir(), "soong", "soong_build_metrics.pb") |
| 370 | buf, err := ioutil.ReadFile(soongBuildMetricsFile) |
| 371 | if err != nil { |
| 372 | ctx.Fatalf("Failed to load %s: %s", soongBuildMetricsFile, err) |
| 373 | } |
| 374 | soongBuildMetrics := &soong_metrics_proto.SoongBuildMetrics{} |
| 375 | err = proto.Unmarshal(buf, soongBuildMetrics) |
| 376 | if err != nil { |
| 377 | ctx.Fatalf("Failed to unmarshal %s: %s", soongBuildMetricsFile, err) |
| 378 | } |
| 379 | return soongBuildMetrics |
| 380 | } |
| 381 | |
| 382 | func logSoongBuildMetrics(ctx Context, metrics *soong_metrics_proto.SoongBuildMetrics) { |
| 383 | ctx.Verbosef("soong_build metrics:") |
| 384 | ctx.Verbosef(" modules: %v", metrics.GetModules()) |
| 385 | ctx.Verbosef(" variants: %v", metrics.GetVariants()) |
| 386 | ctx.Verbosef(" max heap size: %v MB", metrics.GetMaxHeapSize()/1e6) |
| 387 | ctx.Verbosef(" total allocation count: %v", metrics.GetTotalAllocCount()) |
| 388 | ctx.Verbosef(" total allocation size: %v MB", metrics.GetTotalAllocSize()/1e6) |
| 389 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 390 | } |