blob: 55d5466e70c44b420d24f2885a02f6082cd05d63 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// Copyright 2015 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
15package main
16
17import (
18 "flag"
19 "fmt"
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +010020 "io/ioutil"
Colin Cross3f40fa42015-01-30 17:27:36 -080021 "os"
22 "path/filepath"
Jingwen Cheneb76c432021-01-28 08:22:12 -050023 "strings"
Lukacs T. Berkic99c9472021-03-24 10:50:06 +010024 "time"
Colin Cross3f40fa42015-01-30 17:27:36 -080025
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020026 "android/soong/bp2build"
Lukacs T. Berki7690c092021-02-26 14:27:36 +010027 "android/soong/shared"
Colin Crosscb33a002021-04-12 22:25:17 -070028
Colin Cross70b40592015-03-23 12:57:34 -070029 "github.com/google/blueprint/bootstrap"
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +020030 "github.com/google/blueprint/deptools"
Colin Cross3f40fa42015-01-30 17:27:36 -080031
Colin Cross635c3b02016-05-18 15:37:25 -070032 "android/soong/android"
Colin Cross3f40fa42015-01-30 17:27:36 -080033)
34
Colin Crosse87040b2017-12-11 15:52:26 -080035var (
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020036 topDir string
37 outDir string
38 availableEnvFile string
39 usedEnvFile string
40
41 delveListen string
42 delvePath string
43
Jingwen Chen50f93d22020-11-05 07:42:11 -050044 docFile string
45 bazelQueryViewDir string
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020046 bp2buildMarker string
Colin Crosse87040b2017-12-11 15:52:26 -080047)
48
49func init() {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020050 // Flags that make sense in every mode
Lukacs T. Berki7690c092021-02-26 14:27:36 +010051 flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree")
52 flag.StringVar(&outDir, "out", "", "Soong output directory (usually $TOP/out/soong)")
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020053 flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables")
54 flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables")
55
56 // Debug flags
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +010057 flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging")
58 flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set")
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020059
60 // Flags representing various modes soong_build can run in
Colin Crosse87040b2017-12-11 15:52:26 -080061 flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
Lukacs T. Berki47a9d0c2021-03-08 16:34:09 +010062 flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top")
Lukacs T. Berkif8e24282021-04-14 10:31:00 +020063 flag.StringVar(&bp2buildMarker, "bp2build_marker", "", "If set, run bp2build, touch the specified marker file then exit")
Colin Crosse87040b2017-12-11 15:52:26 -080064}
65
Jeff Gaston088e29e2017-11-29 16:47:17 -080066func newNameResolver(config android.Config) *android.NameResolver {
67 namespacePathsToExport := make(map[string]bool)
68
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070069 for _, namespaceName := range config.ExportedNamespaces() {
Jeff Gaston088e29e2017-11-29 16:47:17 -080070 namespacePathsToExport[namespaceName] = true
71 }
72
73 namespacePathsToExport["."] = true // always export the root namespace
74
75 exportFilter := func(namespace *android.Namespace) bool {
76 return namespacePathsToExport[namespace.Path]
77 }
78
79 return android.NewNameResolver(exportFilter)
80}
81
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +020082func newContext(configuration android.Config, prepareBuildActions bool) *android.Context {
Colin Crossae8600b2020-10-29 17:09:13 -070083 ctx := android.NewContext(configuration)
Jingwen Chendaa54bc2020-12-14 02:58:54 -050084 ctx.Register()
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +020085 if !prepareBuildActions {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040086 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
87 }
88 ctx.SetNameInterface(newNameResolver(configuration))
89 ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
90 return ctx
91}
92
Lukacs T. Berki53b2f362021-04-12 14:04:24 +020093func newConfig(srcDir, outDir string, availableEnv map[string]string) android.Config {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +020094 configuration, err := android.NewConfig(srcDir, outDir, bootstrap.CmdlineArgs.ModuleListFile, availableEnv)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040095 if err != nil {
96 fmt.Fprintf(os.Stderr, "%s", err)
97 os.Exit(1)
98 }
99 return configuration
100}
101
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200102// Bazel-enabled mode. Soong runs in two passes.
103// First pass: Analyze the build tree, but only store all bazel commands
104// needed to correctly evaluate the tree in the second pass.
105// TODO(cparsons): Don't output any ninja file, as the second pass will overwrite
106// the incorrect results from the first pass, and file I/O is expensive.
107func runMixedModeBuild(configuration android.Config, firstCtx *android.Context, extraNinjaDeps []string) {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200108 var firstArgs, secondArgs bootstrap.Args
109
110 firstArgs = bootstrap.CmdlineArgs
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200111 configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200112 bootstrap.RunBlueprint(firstArgs, firstCtx.Context, configuration, extraNinjaDeps...)
113
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200114 // Invoke bazel commands and save results for second pass.
115 if err := configuration.BazelContext.InvokeBazel(); err != nil {
116 fmt.Fprintf(os.Stderr, "%s", err)
117 os.Exit(1)
118 }
119 // Second pass: Full analysis, using the bazel command results. Output ninja file.
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200120 secondConfig, err := android.ConfigForAdditionalRun(configuration)
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200121 if err != nil {
122 fmt.Fprintf(os.Stderr, "%s", err)
123 os.Exit(1)
124 }
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200125 secondCtx := newContext(secondConfig, true)
126 secondArgs = bootstrap.CmdlineArgs
127 ninjaDeps := bootstrap.RunBlueprint(secondArgs, secondCtx.Context, secondConfig, extraNinjaDeps...)
128 err = deptools.WriteDepFile(shared.JoinPath(topDir, secondArgs.DepFile), secondArgs.OutFile, ninjaDeps)
129 if err != nil {
130 fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", secondArgs.DepFile, err)
131 os.Exit(1)
132 }
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200133}
134
135// Run the code-generation phase to convert BazelTargetModules to BUILD files.
136func runQueryView(configuration android.Config, ctx *android.Context) {
137 codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView)
138 absoluteQueryViewDir := shared.JoinPath(topDir, bazelQueryViewDir)
139 if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil {
140 fmt.Fprintf(os.Stderr, "%s", err)
141 os.Exit(1)
142 }
143}
144
145func runSoongDocs(configuration android.Config, extraNinjaDeps []string) {
146 ctx := newContext(configuration, false)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200147 soongDocsArgs := bootstrap.CmdlineArgs
148 bootstrap.RunBlueprint(soongDocsArgs, ctx.Context, configuration, extraNinjaDeps...)
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200149 if err := writeDocs(ctx, configuration, docFile); err != nil {
150 fmt.Fprintf(os.Stderr, "%s", err)
151 os.Exit(1)
152 }
153}
154
155func writeMetrics(configuration android.Config) {
Lukacs T. Berki745380c2021-04-12 12:07:44 +0200156 metricsFile := filepath.Join(configuration.BuildDir(), "soong_build_metrics.pb")
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200157 err := android.WriteMetrics(configuration, metricsFile)
158 if err != nil {
159 fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
160 os.Exit(1)
161 }
162}
163
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200164func writeJsonModuleGraph(configuration android.Config, ctx *android.Context, path string, extraNinjaDeps []string) {
165 f, err := os.Create(path)
166 if err != nil {
167 fmt.Fprintf(os.Stderr, "%s", err)
168 os.Exit(1)
169 }
170
171 defer f.Close()
172 ctx.Context.PrintJSONGraph(f)
173 writeFakeNinjaFile(extraNinjaDeps, configuration.BuildDir())
174}
175
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200176func doChosenActivity(configuration android.Config, extraNinjaDeps []string) string {
177 bazelConversionRequested := configuration.IsEnvTrue("GENERATE_BAZEL_FILES") || bp2buildMarker != ""
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200178 mixedModeBuild := configuration.BazelContext.BazelEnabled()
179 generateQueryView := bazelQueryViewDir != ""
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200180 jsonModuleFile := configuration.Getenv("SOONG_DUMP_JSON_MODULE_GRAPH")
181
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200182 blueprintArgs := bootstrap.CmdlineArgs
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200183 prepareBuildActions := !generateQueryView && jsonModuleFile == ""
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200184 if bazelConversionRequested {
185 // Run the alternate pipeline of bp2build mutators and singleton to convert
186 // Blueprint to BUILD files before everything else.
187 runBp2Build(configuration, extraNinjaDeps)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200188 if bp2buildMarker != "" {
189 return bp2buildMarker
190 } else {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200191 return bootstrap.CmdlineArgs.OutFile
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200192 }
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200193 }
194
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200195 ctx := newContext(configuration, prepareBuildActions)
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200196 if mixedModeBuild {
197 runMixedModeBuild(configuration, ctx, extraNinjaDeps)
198 } else {
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200199 ninjaDeps := bootstrap.RunBlueprint(blueprintArgs, ctx.Context, configuration, extraNinjaDeps...)
200 err := deptools.WriteDepFile(shared.JoinPath(topDir, blueprintArgs.DepFile), blueprintArgs.OutFile, ninjaDeps)
201 if err != nil {
202 fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", blueprintArgs.DepFile, err)
203 os.Exit(1)
204 }
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200205 }
206
207 // Convert the Soong module graph into Bazel BUILD files.
208 if generateQueryView {
209 runQueryView(configuration, ctx)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200210 return bootstrap.CmdlineArgs.OutFile // TODO: This is a lie
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200211 }
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200212
213 if jsonModuleFile != "" {
214 writeJsonModuleGraph(configuration, ctx, jsonModuleFile, extraNinjaDeps)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200215 return bootstrap.CmdlineArgs.OutFile // TODO: This is a lie
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200216 }
217
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200218 writeMetrics(configuration)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200219 return bootstrap.CmdlineArgs.OutFile
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200220}
221
222// soong_ui dumps the available environment variables to
223// soong.environment.available . Then soong_build itself is run with an empty
224// environment so that the only way environment variables can be accessed is
225// using Config, which tracks access to them.
226
227// At the end of the build, a file called soong.environment.used is written
228// containing the current value of all used environment variables. The next
229// time soong_ui is run, it checks whether any environment variables that was
230// used had changed and if so, it deletes soong.environment.used to cause a
231// rebuild.
232//
233// The dependency of build.ninja on soong.environment.used is declared in
234// build.ninja.d
235func parseAvailableEnv() map[string]string {
236 if availableEnvFile == "" {
237 fmt.Fprintf(os.Stderr, "--available_env not set\n")
238 os.Exit(1)
239 }
240
241 result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile))
242 if err != nil {
243 fmt.Fprintf(os.Stderr, "error reading available environment file '%s': %s\n", availableEnvFile, err)
244 os.Exit(1)
245 }
246
247 return result
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200248}
249
Colin Cross3f40fa42015-01-30 17:27:36 -0800250func main() {
251 flag.Parse()
252
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100253 shared.ReexecWithDelveMaybe(delveListen, delvePath)
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100254 android.InitSandbox(topDir)
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100255
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200256 availableEnv := parseAvailableEnv()
Lukacs T. Berki53b2f362021-04-12 14:04:24 +0200257
Colin Cross3f40fa42015-01-30 17:27:36 -0800258 // The top-level Blueprints file is passed as the first argument.
259 srcDir := filepath.Dir(flag.Arg(0))
Lukacs T. Berki53b2f362021-04-12 14:04:24 +0200260 configuration := newConfig(srcDir, outDir, availableEnv)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100261 extraNinjaDeps := []string{
262 configuration.ProductVariablesFileName,
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200263 usedEnvFile,
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100264 }
Colin Crossaa812d12019-06-19 13:33:24 -0700265
Lukacs T. Berkid1e3f1f2021-03-16 08:55:23 +0100266 if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" {
267 configuration.SetAllowMissingDependencies()
268 }
269
Lukacs T. Berki7d613bf2021-03-02 10:09:41 +0100270 if shared.IsDebugging() {
Colin Crossaa812d12019-06-19 13:33:24 -0700271 // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is
272 // enabled even if it completed successfully.
273 extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve"))
274 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500275
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200276 if docFile != "" {
277 // We don't write an used variables file when generating documentation
278 // because that is done from within the actual builds as a Ninja action and
279 // thus it would overwrite the actual used variables file so this is
280 // special-cased.
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200281 // TODO: Fix this by not passing --used_env to the soong_docs invocation
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200282 runSoongDocs(configuration, extraNinjaDeps)
283 return
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400284 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500285
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200286 finalOutputFile := doChosenActivity(configuration, extraNinjaDeps)
287 writeUsedEnvironmentFile(configuration, finalOutputFile)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100288}
289
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200290func writeUsedEnvironmentFile(configuration android.Config, finalOutputFile string) {
291 if usedEnvFile == "" {
292 return
293 }
294
295 path := shared.JoinPath(topDir, usedEnvFile)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100296 data, err := shared.EnvFileContents(configuration.EnvDeps())
297 if err != nil {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200298 fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err)
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100299 os.Exit(1)
300 }
301
302 err = ioutil.WriteFile(path, data, 0666)
303 if err != nil {
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200304 fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err)
Lukacs T. Berkic99c9472021-03-24 10:50:06 +0100305 os.Exit(1)
306 }
307
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200308 // Touch the output file so that it's not older than the file we just
Lukacs T. Berkic99c9472021-03-24 10:50:06 +0100309 // wrote. We can't write the environment file earlier because one an access
310 // new environment variables while writing it.
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200311 touch(shared.JoinPath(topDir, finalOutputFile))
Colin Cross3f40fa42015-01-30 17:27:36 -0800312}
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000313
Lukacs T. Berki97bb9f12021-04-01 18:28:45 +0200314// Workarounds to support running bp2build in a clean AOSP checkout with no
315// prior builds, and exiting early as soon as the BUILD files get generated,
316// therefore not creating build.ninja files that soong_ui and callers of
317// soong_build expects.
318//
319// These files are: build.ninja and build.ninja.d. Since Kati hasn't been
320// ran as well, and `nothing` is defined in a .mk file, there isn't a ninja
321// target called `nothing`, so we manually create it here.
322func writeFakeNinjaFile(extraNinjaDeps []string, buildDir string) {
323 extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ")
324
325 ninjaFileName := "build.ninja"
326 ninjaFile := shared.JoinPath(topDir, buildDir, ninjaFileName)
327 ninjaFileD := shared.JoinPath(topDir, buildDir, ninjaFileName)
328 // A workaround to create the 'nothing' ninja target so `m nothing` works,
329 // since bp2build runs without Kati, and the 'nothing' target is declared in
330 // a Makefile.
331 ioutil.WriteFile(ninjaFile, []byte("build nothing: phony\n phony_output = true\n"), 0666)
332 ioutil.WriteFile(ninjaFileD,
333 []byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFileName, extraNinjaDepsString)),
334 0666)
335}
336
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200337func touch(path string) {
338 f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
339 if err != nil {
340 fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err)
341 os.Exit(1)
342 }
343
344 err = f.Close()
345 if err != nil {
346 fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err)
347 os.Exit(1)
348 }
349
350 currentTime := time.Now().Local()
351 err = os.Chtimes(path, currentTime, currentTime)
352 if err != nil {
353 fmt.Fprintf(os.Stderr, "error touching '%s': %s\n", path, err)
354 os.Exit(1)
355 }
356}
357
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500358// Run Soong in the bp2build mode. This creates a standalone context that registers
359// an alternate pipeline of mutators and singletons specifically for generating
360// Bazel BUILD files instead of Ninja files.
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200361func runBp2Build(configuration android.Config, extraNinjaDeps []string) {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500362 // Register an alternate set of singletons and mutators for bazel
363 // conversion for Bazel conversion.
364 bp2buildCtx := android.NewContext(configuration)
Jingwen Cheneb76c432021-01-28 08:22:12 -0500365
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200366 // Propagate "allow misssing dependencies" bit. This is normally set in
367 // newContext(), but we create bp2buildCtx without calling that method.
368 bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500369 bp2buildCtx.SetNameInterface(newNameResolver(configuration))
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200370 bp2buildCtx.RegisterForBazelConversion()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500371
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500372 // The bp2build process is a purely functional process that only depends on
373 // Android.bp files. It must not depend on the values of per-build product
374 // configurations or variables, since those will generate different BUILD
375 // files based on how the user has configured their tree.
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200376 bp2buildCtx.SetModuleListFile(bootstrap.CmdlineArgs.ModuleListFile)
Lukacs T. Berki6790ebc2021-04-01 17:55:58 +0200377 modulePaths, err := bp2buildCtx.ListModulePaths(configuration.SrcDir())
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500378 if err != nil {
379 panic(err)
380 }
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500381
Lukacs T. Berkif0b3b942021-03-23 11:46:47 +0100382 extraNinjaDeps = append(extraNinjaDeps, modulePaths...)
383
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200384 // No need to generate Ninja build rules/statements from Modules and Singletons.
385 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
386
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500387 // Run the loading and analysis pipeline to prepare the graph of regular
388 // Modules parsed from Android.bp files, and the BazelTargetModules mapped
389 // from the regular Modules.
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200390 blueprintArgs := bootstrap.CmdlineArgs
391 ninjaDeps := bootstrap.RunBlueprint(blueprintArgs, bp2buildCtx.Context, configuration, extraNinjaDeps...)
392
Colin Crosscb33a002021-04-12 22:25:17 -0700393 ninjaDeps = append(ninjaDeps, bootstrap.GlobFileListFiles(configuration)...)
Lukacs T. Berkid518e1a2021-04-14 13:49:50 +0200394
395 depFile := bp2buildMarker + ".d"
396 err = deptools.WriteDepFile(shared.JoinPath(topDir, depFile), bp2buildMarker, ninjaDeps)
397 if err != nil {
398 fmt.Fprintf(os.Stderr, "Cannot write depfile '%s': %s\n", depFile, err)
399 os.Exit(1)
400 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500401
Jingwen Chen164e0862021-02-19 00:48:40 -0500402 // Run the code-generation phase to convert BazelTargetModules to BUILD files
403 // and print conversion metrics to the user.
Jingwen Chen33832f92021-01-24 22:55:54 -0500404 codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build)
Jingwen Chen164e0862021-02-19 00:48:40 -0500405 metrics := bp2build.Codegen(codegenContext)
406
407 // Only report metrics when in bp2build mode. The metrics aren't relevant
408 // for queryview, since that's a total repo-wide conversion and there's a
409 // 1:1 mapping for each module.
410 metrics.Print()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500411
Liz Kammerba3ea162021-02-17 13:22:03 -0500412 extraNinjaDeps = append(extraNinjaDeps, codegenContext.AdditionalNinjaDeps()...)
Lukacs T. Berkif8e24282021-04-14 10:31:00 +0200413 if bp2buildMarker != "" {
414 touch(shared.JoinPath(topDir, bp2buildMarker))
415 } else {
416 writeFakeNinjaFile(extraNinjaDeps, codegenContext.Config().BuildDir())
417 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500418}