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