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