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