Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 1 | // 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 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "fmt" |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 20 | "io/ioutil" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 21 | "os" |
| 22 | "path/filepath" |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 23 | "strings" |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 24 | "time" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 27 | "android/soong/bp2build" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 28 | "android/soong/shared" |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 29 | "android/soong/ui/metrics/bp2build_metrics_proto" |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 30 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 31 | "github.com/google/blueprint/bootstrap" |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 32 | "github.com/google/blueprint/deptools" |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 33 | "github.com/google/blueprint/metrics" |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 34 | androidProtobuf "google.golang.org/protobuf/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 35 | ) |
| 36 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 37 | var ( |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 38 | topDir string |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 39 | outDir string |
Lukacs T. Berki | d6cee7e | 2021-09-01 16:25:51 +0200 | [diff] [blame] | 40 | soongOutDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 41 | availableEnvFile string |
| 42 | usedEnvFile string |
| 43 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 44 | runGoTests bool |
| 45 | |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 46 | globFile string |
| 47 | globListDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 48 | delveListen string |
| 49 | delvePath string |
| 50 | |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 51 | moduleGraphFile string |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 52 | moduleActionsFile string |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 53 | docFile string |
| 54 | bazelQueryViewDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 55 | bp2buildMarker string |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 56 | |
| 57 | cmdlineArgs bootstrap.Args |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 58 | ) |
| 59 | |
| 60 | func init() { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 61 | // Flags that make sense in every mode |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 62 | flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree") |
Lukacs T. Berki | d6cee7e | 2021-09-01 16:25:51 +0200 | [diff] [blame] | 63 | flag.StringVar(&soongOutDir, "soong_out", "", "Soong output directory (usually $TOP/out/soong)") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 64 | flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables") |
| 65 | flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 66 | flag.StringVar(&globFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output") |
| 67 | flag.StringVar(&globListDir, "globListDir", "", "the directory containing the glob list files") |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 68 | flag.StringVar(&outDir, "out", "", "the ninja builddir directory") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 69 | flag.StringVar(&cmdlineArgs.ModuleListFile, "l", "", "file that lists filepaths to parse") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 70 | |
| 71 | // Debug flags |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 72 | flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging") |
| 73 | flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 74 | flag.StringVar(&cmdlineArgs.Cpuprofile, "cpuprofile", "", "write cpu profile to file") |
| 75 | flag.StringVar(&cmdlineArgs.TraceFile, "trace", "", "write trace to file") |
| 76 | flag.StringVar(&cmdlineArgs.Memprofile, "memprofile", "", "write memory profile to file") |
| 77 | flag.BoolVar(&cmdlineArgs.NoGC, "nogc", false, "turn off GC for debugging") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 78 | |
| 79 | // Flags representing various modes soong_build can run in |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 80 | flag.StringVar(&moduleGraphFile, "module_graph_file", "", "JSON module graph file to output") |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 81 | flag.StringVar(&moduleActionsFile, "module_actions_file", "", "JSON file to output inputs/outputs of actions of modules") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 82 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
Lukacs T. Berki | 47a9d0c | 2021-03-08 16:34:09 +0100 | [diff] [blame] | 83 | flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 84 | flag.StringVar(&bp2buildMarker, "bp2build_marker", "", "If set, run bp2build, touch the specified marker file then exit") |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 85 | flag.StringVar(&cmdlineArgs.OutFile, "o", "build.ninja", "the Ninja file to output") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 86 | flag.BoolVar(&cmdlineArgs.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file") |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 87 | flag.BoolVar(&cmdlineArgs.BazelMode, "bazel-mode", false, "use bazel for analysis of certain modules") |
| 88 | flag.BoolVar(&cmdlineArgs.BazelModeDev, "bazel-mode-dev", false, "use bazel for analysis of a large number of modules (less stable)") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 89 | |
| 90 | // Flags that probably shouldn't be flags of soong_build but we haven't found |
| 91 | // the time to remove them yet |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 92 | flag.BoolVar(&runGoTests, "t", false, "build and run go tests during bootstrap") |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 93 | |
| 94 | // Disable deterministic randomization in the protobuf package, so incremental |
| 95 | // builds with unrelated Soong changes don't trigger large rebuilds (since we |
| 96 | // write out text protos in command lines, and command line changes trigger |
| 97 | // rebuilds). |
| 98 | androidProtobuf.DisableRand() |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 101 | func newNameResolver(config android.Config) *android.NameResolver { |
| 102 | namespacePathsToExport := make(map[string]bool) |
| 103 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 104 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 105 | namespacePathsToExport[namespaceName] = true |
| 106 | } |
| 107 | |
| 108 | namespacePathsToExport["."] = true // always export the root namespace |
| 109 | |
| 110 | exportFilter := func(namespace *android.Namespace) bool { |
| 111 | return namespacePathsToExport[namespace.Path] |
| 112 | } |
| 113 | |
| 114 | return android.NewNameResolver(exportFilter) |
| 115 | } |
| 116 | |
Lukacs T. Berki | ffc9e8d | 2021-09-07 17:54:38 +0200 | [diff] [blame] | 117 | func newContext(configuration android.Config) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 118 | ctx := android.NewContext(configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 119 | ctx.Register() |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 120 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 121 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 122 | return ctx |
| 123 | } |
| 124 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 125 | func newConfig(availableEnv map[string]string) android.Config { |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 126 | var buildMode android.SoongBuildMode |
| 127 | |
| 128 | if bp2buildMarker != "" { |
| 129 | buildMode = android.Bp2build |
| 130 | } else if bazelQueryViewDir != "" { |
| 131 | buildMode = android.GenerateQueryView |
| 132 | } else if moduleGraphFile != "" { |
| 133 | buildMode = android.GenerateModuleGraph |
| 134 | } else if docFile != "" { |
| 135 | buildMode = android.GenerateDocFile |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 136 | } else if cmdlineArgs.BazelModeDev { |
| 137 | buildMode = android.BazelDevMode |
| 138 | } else if cmdlineArgs.BazelMode { |
| 139 | buildMode = android.BazelProdMode |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 140 | } else { |
| 141 | buildMode = android.AnalysisNoBazel |
| 142 | } |
| 143 | |
| 144 | configuration, err := android.NewConfig(cmdlineArgs.ModuleListFile, buildMode, runGoTests, outDir, soongOutDir, availableEnv) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 145 | if err != nil { |
| 146 | fmt.Fprintf(os.Stderr, "%s", err) |
| 147 | os.Exit(1) |
| 148 | } |
| 149 | return configuration |
| 150 | } |
| 151 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 152 | // Bazel-enabled mode. Attaches a mutator to queue Bazel requests, adds a |
| 153 | // BeforePrepareBuildActionsHook to invoke Bazel, and then uses Bazel metadata |
| 154 | // for modules that should be handled by Bazel. |
| 155 | func runMixedModeBuild(configuration android.Config, ctx *android.Context, extraNinjaDeps []string) { |
| 156 | ctx.EventHandler.Begin("mixed_build") |
| 157 | defer ctx.EventHandler.End("mixed_build") |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 158 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 159 | bazelHook := func() error { |
| 160 | ctx.EventHandler.Begin("bazel") |
| 161 | defer ctx.EventHandler.End("bazel") |
Yu Liu | 8d82ac5 | 2022-05-17 15:13:28 -0700 | [diff] [blame] | 162 | return configuration.BazelContext.InvokeBazel(configuration) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 163 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 164 | ctx.SetBeforePrepareBuildActionsHook(bazelHook) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 165 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 166 | ninjaDeps := bootstrap.RunBlueprint(cmdlineArgs, bootstrap.DoEverything, ctx.Context, configuration) |
Chris Parsons | 027881c | 2022-05-24 15:38:38 -0400 | [diff] [blame] | 167 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 168 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 169 | globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 170 | ninjaDeps = append(ninjaDeps, globListFiles...) |
| 171 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 172 | writeDepFile(cmdlineArgs.OutFile, *ctx.EventHandler, ninjaDeps) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 176 | func runQueryView(queryviewDir, queryviewMarker string, configuration android.Config, ctx *android.Context) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 177 | ctx.EventHandler.Begin("queryview") |
| 178 | defer ctx.EventHandler.End("queryview") |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 179 | codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView) |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 180 | absoluteQueryViewDir := shared.JoinPath(topDir, queryviewDir) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 181 | if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil { |
| 182 | fmt.Fprintf(os.Stderr, "%s", err) |
| 183 | os.Exit(1) |
| 184 | } |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 185 | |
| 186 | touch(shared.JoinPath(topDir, queryviewMarker)) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 189 | func writeMetrics(configuration android.Config, eventHandler metrics.EventHandler, metricsDir string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 190 | if len(metricsDir) < 1 { |
| 191 | fmt.Fprintf(os.Stderr, "\nMissing required env var for generating soong metrics: LOG_DIR\n") |
| 192 | os.Exit(1) |
| 193 | } |
| 194 | metricsFile := filepath.Join(metricsDir, "soong_build_metrics.pb") |
| 195 | err := android.WriteMetrics(configuration, eventHandler, metricsFile) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 196 | if err != nil { |
| 197 | fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err) |
| 198 | os.Exit(1) |
| 199 | } |
| 200 | } |
| 201 | |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 202 | func writeJsonModuleGraphAndActions(ctx *android.Context, graphPath string, actionsPath string) { |
| 203 | graphFile, graphErr := os.Create(shared.JoinPath(topDir, graphPath)) |
| 204 | actionsFile, actionsErr := os.Create(shared.JoinPath(topDir, actionsPath)) |
| 205 | if graphErr != nil || actionsErr != nil { |
| 206 | fmt.Fprintf(os.Stderr, "Graph err: %s, actions err: %s", graphErr, actionsErr) |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 207 | os.Exit(1) |
| 208 | } |
| 209 | |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 210 | defer graphFile.Close() |
| 211 | defer actionsFile.Close() |
| 212 | ctx.Context.PrintJSONGraphAndActions(graphFile, actionsFile) |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 215 | func writeBuildGlobsNinjaFile(ctx *android.Context, buildDir string, config interface{}) []string { |
| 216 | ctx.EventHandler.Begin("globs_ninja_file") |
| 217 | defer ctx.EventHandler.End("globs_ninja_file") |
| 218 | |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 219 | globDir := bootstrap.GlobDirectory(buildDir, globListDir) |
| 220 | bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{ |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 221 | GlobLister: ctx.Globs, |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 222 | GlobFile: globFile, |
| 223 | GlobDir: globDir, |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 224 | SrcDir: ctx.SrcDir(), |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 225 | }, config) |
| 226 | return bootstrap.GlobFileListFiles(globDir) |
| 227 | } |
| 228 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 229 | func writeDepFile(outputFile string, eventHandler metrics.EventHandler, ninjaDeps []string) { |
| 230 | eventHandler.Begin("ninja_deps") |
| 231 | defer eventHandler.End("ninja_deps") |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 232 | depFile := shared.JoinPath(topDir, outputFile+".d") |
| 233 | err := deptools.WriteDepFile(depFile, outputFile, ninjaDeps) |
| 234 | if err != nil { |
| 235 | fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", depFile, err) |
| 236 | os.Exit(1) |
| 237 | } |
| 238 | } |
| 239 | |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame] | 240 | // doChosenActivity runs Soong for a specific activity, like bp2build, queryview |
| 241 | // or the actual Soong build for the build.ninja file. Returns the top level |
| 242 | // output file of the specific activity. |
usta | cd245f7 | 2022-08-24 11:45:19 -0400 | [diff] [blame] | 243 | func doChosenActivity(ctx *android.Context, configuration android.Config, extraNinjaDeps []string) string { |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 244 | if configuration.BuildMode == android.Bp2build { |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 245 | // Run the alternate pipeline of bp2build mutators and singleton to convert |
| 246 | // Blueprint to BUILD files before everything else. |
| 247 | runBp2Build(configuration, extraNinjaDeps) |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame] | 248 | return bp2buildMarker |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 249 | } else if configuration.IsMixedBuildsEnabled() { |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 250 | runMixedModeBuild(configuration, ctx, extraNinjaDeps) |
| 251 | } else { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 252 | var stopBefore bootstrap.StopBefore |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 253 | if configuration.BuildMode == android.GenerateModuleGraph { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 254 | stopBefore = bootstrap.StopBeforeWriteNinja |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 255 | } else if configuration.BuildMode == android.GenerateQueryView || configuration.BuildMode == android.GenerateDocFile { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 256 | stopBefore = bootstrap.StopBeforePrepareBuildActions |
| 257 | } else { |
| 258 | stopBefore = bootstrap.DoEverything |
| 259 | } |
| 260 | |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 261 | ninjaDeps := bootstrap.RunBlueprint(cmdlineArgs, stopBefore, ctx.Context, configuration) |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 262 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 263 | |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 264 | globListFiles := writeBuildGlobsNinjaFile(ctx, configuration.SoongOutDir(), configuration) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 265 | ninjaDeps = append(ninjaDeps, globListFiles...) |
| 266 | |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 267 | // Convert the Soong module graph into Bazel BUILD files. |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 268 | if configuration.BuildMode == android.GenerateQueryView { |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 269 | queryviewMarkerFile := bazelQueryViewDir + ".marker" |
| 270 | runQueryView(bazelQueryViewDir, queryviewMarkerFile, configuration, ctx) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 271 | writeDepFile(queryviewMarkerFile, *ctx.EventHandler, ninjaDeps) |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 272 | return queryviewMarkerFile |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 273 | } else if configuration.BuildMode == android.GenerateModuleGraph { |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 274 | writeJsonModuleGraphAndActions(ctx, moduleGraphFile, moduleActionsFile) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 275 | writeDepFile(moduleGraphFile, *ctx.EventHandler, ninjaDeps) |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 276 | return moduleGraphFile |
Chris Parsons | ad87601 | 2022-08-20 14:48:32 -0400 | [diff] [blame] | 277 | } else if configuration.BuildMode == android.GenerateDocFile { |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 278 | // TODO: we could make writeDocs() return the list of documentation files |
| 279 | // written and add them to the .d file. Then soong_docs would be re-run |
| 280 | // whenever one is deleted. |
| 281 | if err := writeDocs(ctx, shared.JoinPath(topDir, docFile)); err != nil { |
| 282 | fmt.Fprintf(os.Stderr, "error building Soong documentation: %s\n", err) |
| 283 | os.Exit(1) |
| 284 | } |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 285 | writeDepFile(docFile, *ctx.EventHandler, ninjaDeps) |
Lukacs T. Berki | c6012f3 | 2021-09-06 18:31:46 +0200 | [diff] [blame] | 286 | return docFile |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 287 | } else { |
| 288 | // The actual output (build.ninja) was written in the RunBlueprint() call |
| 289 | // above |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 290 | writeDepFile(cmdlineArgs.OutFile, *ctx.EventHandler, ninjaDeps) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 291 | } |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 292 | } |
| 293 | |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 294 | return cmdlineArgs.OutFile |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // soong_ui dumps the available environment variables to |
| 298 | // soong.environment.available . Then soong_build itself is run with an empty |
| 299 | // environment so that the only way environment variables can be accessed is |
| 300 | // using Config, which tracks access to them. |
| 301 | |
| 302 | // At the end of the build, a file called soong.environment.used is written |
| 303 | // containing the current value of all used environment variables. The next |
| 304 | // time soong_ui is run, it checks whether any environment variables that was |
| 305 | // used had changed and if so, it deletes soong.environment.used to cause a |
| 306 | // rebuild. |
| 307 | // |
| 308 | // The dependency of build.ninja on soong.environment.used is declared in |
| 309 | // build.ninja.d |
| 310 | func parseAvailableEnv() map[string]string { |
| 311 | if availableEnvFile == "" { |
| 312 | fmt.Fprintf(os.Stderr, "--available_env not set\n") |
| 313 | os.Exit(1) |
| 314 | } |
| 315 | |
| 316 | result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile)) |
| 317 | if err != nil { |
| 318 | fmt.Fprintf(os.Stderr, "error reading available environment file '%s': %s\n", availableEnvFile, err) |
| 319 | os.Exit(1) |
| 320 | } |
| 321 | |
| 322 | return result |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 325 | func main() { |
| 326 | flag.Parse() |
| 327 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 328 | shared.ReexecWithDelveMaybe(delveListen, delvePath) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 329 | android.InitSandbox(topDir) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 330 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 331 | availableEnv := parseAvailableEnv() |
Lukacs T. Berki | 53b2f36 | 2021-04-12 14:04:24 +0200 | [diff] [blame] | 332 | |
Lukacs T. Berki | 89fcdcb | 2021-09-07 09:10:33 +0200 | [diff] [blame] | 333 | configuration := newConfig(availableEnv) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 334 | extraNinjaDeps := []string{ |
| 335 | configuration.ProductVariablesFileName, |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 336 | usedEnvFile, |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 337 | } |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 338 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 339 | if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 340 | configuration.SetAllowMissingDependencies() |
| 341 | } |
| 342 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 343 | if shared.IsDebugging() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 344 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 345 | // enabled even if it completed successfully. |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 346 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.SoongOutDir(), "always_rerun_for_delve")) |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 347 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 348 | |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 349 | // Bypass configuration.Getenv, as LOG_DIR does not need to be dependency tracked. By definition, it will |
| 350 | // change between every CI build, so tracking it would require re-running Soong for every build. |
| 351 | logDir := availableEnv["LOG_DIR"] |
| 352 | |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 353 | ctx := newContext(configuration) |
| 354 | ctx.EventHandler.Begin("soong_build") |
| 355 | |
usta | cd245f7 | 2022-08-24 11:45:19 -0400 | [diff] [blame] | 356 | finalOutputFile := doChosenActivity(ctx, configuration, extraNinjaDeps) |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 357 | |
| 358 | ctx.EventHandler.End("soong_build") |
| 359 | writeMetrics(configuration, *ctx.EventHandler, logDir) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 360 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 361 | writeUsedEnvironmentFile(configuration, finalOutputFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 364 | func writeUsedEnvironmentFile(configuration android.Config, finalOutputFile string) { |
| 365 | if usedEnvFile == "" { |
| 366 | return |
| 367 | } |
| 368 | |
| 369 | path := shared.JoinPath(topDir, usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 370 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
| 371 | if err != nil { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 372 | fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 373 | os.Exit(1) |
| 374 | } |
| 375 | |
| 376 | err = ioutil.WriteFile(path, data, 0666) |
| 377 | if err != nil { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 378 | fmt.Fprintf(os.Stderr, "error writing used environment file '%s': %s\n", usedEnvFile, err) |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 379 | os.Exit(1) |
| 380 | } |
| 381 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 382 | // Touch the output file so that it's not older than the file we just |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 383 | // wrote. We can't write the environment file earlier because one an access |
| 384 | // new environment variables while writing it. |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 385 | touch(shared.JoinPath(topDir, finalOutputFile)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 386 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 387 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 388 | func touch(path string) { |
| 389 | f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) |
| 390 | if err != nil { |
| 391 | fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err) |
| 392 | os.Exit(1) |
| 393 | } |
| 394 | |
| 395 | err = f.Close() |
| 396 | if err != nil { |
| 397 | fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err) |
| 398 | os.Exit(1) |
| 399 | } |
| 400 | |
| 401 | currentTime := time.Now().Local() |
| 402 | err = os.Chtimes(path, currentTime, currentTime) |
| 403 | if err != nil { |
| 404 | fmt.Fprintf(os.Stderr, "error touching '%s': %s\n", path, err) |
| 405 | os.Exit(1) |
| 406 | } |
| 407 | } |
| 408 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 409 | // Find BUILD files in the srcDir which are not in the allowlist |
| 410 | // (android.Bp2BuildConversionAllowlist#ShouldKeepExistingBuildFileForDir) |
| 411 | // and return their paths so they can be left out of the Bazel workspace dir (i.e. ignored) |
| 412 | func getPathsToIgnoredBuildFiles(config android.Bp2BuildConversionAllowlist, topDir string, srcDirBazelFiles []string, verbose bool) []string { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 413 | paths := make([]string, 0) |
| 414 | |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 415 | for _, srcDirBazelFileRelativePath := range srcDirBazelFiles { |
| 416 | srcDirBazelFileFullPath := shared.JoinPath(topDir, srcDirBazelFileRelativePath) |
| 417 | fileInfo, err := os.Stat(srcDirBazelFileFullPath) |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 418 | if err != nil { |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 419 | // Warn about error, but continue trying to check files |
| 420 | fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", srcDirBazelFileFullPath, err) |
| 421 | continue |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 422 | } |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 423 | if fileInfo.IsDir() { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 424 | // Don't ignore entire directories |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 425 | continue |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 426 | } |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 427 | if fileInfo.Name() != "BUILD" && fileInfo.Name() != "BUILD.bazel" { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 428 | // Don't ignore this file - it is not a build file |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 429 | continue |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 430 | } |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 431 | if config.ShouldKeepExistingBuildFileForDir(filepath.Dir(srcDirBazelFileRelativePath)) { |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 432 | // Don't ignore this existing build file |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 433 | continue |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 434 | } |
Liz Kammer | 1aefc9d | 2021-10-18 16:59:00 -0400 | [diff] [blame] | 435 | if verbose { |
| 436 | fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", srcDirBazelFileRelativePath) |
| 437 | } |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 438 | paths = append(paths, srcDirBazelFileRelativePath) |
| 439 | } |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 440 | |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 441 | return paths |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | // Returns temporary symlink forest excludes necessary for bazel build //external/... (and bazel build //frameworks/...) to work |
| 445 | func getTemporaryExcludes() []string { |
| 446 | excludes := make([]string, 0) |
| 447 | |
| 448 | // FIXME: 'autotest_lib' is a symlink back to external/autotest, and this causes an infinite symlink expansion error for Bazel |
| 449 | excludes = append(excludes, "external/autotest/venv/autotest_lib") |
Jingwen Chen | c0c5021 | 2022-06-07 10:07:22 +0000 | [diff] [blame] | 450 | excludes = append(excludes, "external/autotest/autotest_lib") |
| 451 | excludes = append(excludes, "external/autotest/client/autotest_lib/client") |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 452 | |
| 453 | // FIXME: The external/google-fruit/extras/bazel_root/third_party/fruit dir is poison |
| 454 | // It contains several symlinks back to real source dirs, and those source dirs contain BUILD files we want to ignore |
| 455 | excludes = append(excludes, "external/google-fruit/extras/bazel_root/third_party/fruit") |
| 456 | |
| 457 | // FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue |
| 458 | excludes = append(excludes, "frameworks/compile/slang") |
| 459 | |
| 460 | return excludes |
| 461 | } |
| 462 | |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 463 | // Read the bazel.list file that the Soong Finder already dumped earlier (hopefully) |
| 464 | // It contains the locations of BUILD files, BUILD.bazel files, etc. in the source dir |
| 465 | func getExistingBazelRelatedFiles(topDir string) ([]string, error) { |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 466 | bazelFinderFile := filepath.Join(filepath.Dir(cmdlineArgs.ModuleListFile), "bazel.list") |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 467 | if !filepath.IsAbs(bazelFinderFile) { |
| 468 | // Assume this was a relative path under topDir |
| 469 | bazelFinderFile = filepath.Join(topDir, bazelFinderFile) |
| 470 | } |
| 471 | data, err := ioutil.ReadFile(bazelFinderFile) |
| 472 | if err != nil { |
| 473 | return nil, err |
| 474 | } |
| 475 | files := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 476 | return files, nil |
| 477 | } |
| 478 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 479 | // Run Soong in the bp2build mode. This creates a standalone context that registers |
| 480 | // an alternate pipeline of mutators and singletons specifically for generating |
| 481 | // Bazel BUILD files instead of Ninja files. |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 482 | func runBp2Build(configuration android.Config, extraNinjaDeps []string) { |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 483 | var codegenMetrics bp2build.CodegenMetrics |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 484 | eventHandler := metrics.EventHandler{} |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 485 | eventHandler.Do("bp2build", func() { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 486 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 487 | // Register an alternate set of singletons and mutators for bazel |
| 488 | // conversion for Bazel conversion. |
| 489 | bp2buildCtx := android.NewContext(configuration) |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 490 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 491 | // Propagate "allow misssing dependencies" bit. This is normally set in |
| 492 | // newContext(), but we create bp2buildCtx without calling that method. |
| 493 | bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 494 | bp2buildCtx.SetNameInterface(newNameResolver(configuration)) |
| 495 | bp2buildCtx.RegisterForBazelConversion() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 496 | |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 497 | var ninjaDeps []string |
| 498 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 499 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 500 | // The bp2build process is a purely functional process that only depends on |
| 501 | // Android.bp files. It must not depend on the values of per-build product |
| 502 | // configurations or variables, since those will generate different BUILD |
| 503 | // files based on how the user has configured their tree. |
| 504 | bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile) |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 505 | if modulePaths, err := bp2buildCtx.ListModulePaths("."); err != nil { |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 506 | panic(err) |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 507 | } else { |
| 508 | ninjaDeps = append(ninjaDeps, modulePaths...) |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 509 | } |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 510 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 511 | // Run the loading and analysis pipeline to prepare the graph of regular |
| 512 | // Modules parsed from Android.bp files, and the BazelTargetModules mapped |
| 513 | // from the regular Modules. |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 514 | eventHandler.Do("bootstrap", func() { |
| 515 | blueprintArgs := cmdlineArgs |
| 516 | bootstrapDeps := bootstrap.RunBlueprint(blueprintArgs, bootstrap.StopBeforePrepareBuildActions, bp2buildCtx.Context, configuration) |
| 517 | ninjaDeps = append(ninjaDeps, bootstrapDeps...) |
| 518 | }) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 519 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 520 | globListFiles := writeBuildGlobsNinjaFile(bp2buildCtx, configuration.SoongOutDir(), configuration) |
| 521 | ninjaDeps = append(ninjaDeps, globListFiles...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 522 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 523 | // Run the code-generation phase to convert BazelTargetModules to BUILD files |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 524 | // and print conversion codegenMetrics to the user. |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 525 | codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build) |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 526 | eventHandler.Do("codegen", func() { |
| 527 | codegenMetrics = bp2build.Codegen(codegenContext) |
| 528 | }) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 529 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 530 | generatedRoot := shared.JoinPath(configuration.SoongOutDir(), "bp2build") |
| 531 | workspaceRoot := shared.JoinPath(configuration.SoongOutDir(), "workspace") |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 532 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 533 | excludes := []string{ |
| 534 | "bazel-bin", |
| 535 | "bazel-genfiles", |
| 536 | "bazel-out", |
| 537 | "bazel-testlogs", |
| 538 | "bazel-" + filepath.Base(topDir), |
| 539 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 540 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 541 | if outDir[0] != '/' { |
| 542 | excludes = append(excludes, outDir) |
| 543 | } |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 544 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 545 | existingBazelRelatedFiles, err := getExistingBazelRelatedFiles(topDir) |
| 546 | if err != nil { |
| 547 | fmt.Fprintf(os.Stderr, "Error determining existing Bazel-related files: %s\n", err) |
| 548 | os.Exit(1) |
| 549 | } |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 550 | |
Cole Faust | 324a92e | 2022-08-23 15:29:05 -0700 | [diff] [blame] | 551 | pathsToIgnoredBuildFiles := getPathsToIgnoredBuildFiles(configuration.Bp2buildPackageConfig, topDir, existingBazelRelatedFiles, configuration.IsEnvTrue("BP2BUILD_VERBOSE")) |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 552 | excludes = append(excludes, pathsToIgnoredBuildFiles...) |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 553 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 554 | excludes = append(excludes, getTemporaryExcludes()...) |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 555 | |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 556 | // PlantSymlinkForest() returns all the directories that were readdir()'ed. |
| 557 | // Such a directory SHOULD be added to `ninjaDeps` so that a child directory |
| 558 | // or file created/deleted under it would trigger an update of the symlink |
| 559 | // forest. |
| 560 | eventHandler.Do("symlink_forest", func() { |
| 561 | symlinkForestDeps := bp2build.PlantSymlinkForest( |
| 562 | configuration, topDir, workspaceRoot, generatedRoot, ".", excludes) |
| 563 | ninjaDeps = append(ninjaDeps, symlinkForestDeps...) |
| 564 | }) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 565 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 566 | ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 567 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 568 | writeDepFile(bp2buildMarker, eventHandler, ninjaDeps) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 569 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 570 | // Create an empty bp2build marker file. |
| 571 | touch(shared.JoinPath(topDir, bp2buildMarker)) |
| 572 | }) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 573 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 574 | // Only report metrics when in bp2build mode. The metrics aren't relevant |
| 575 | // for queryview, since that's a total repo-wide conversion and there's a |
| 576 | // 1:1 mapping for each module. |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 577 | if configuration.IsEnvTrue("BP2BUILD_VERBOSE") { |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 578 | codegenMetrics.Print() |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 579 | } |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame^] | 580 | writeBp2BuildMetrics(&codegenMetrics, configuration, eventHandler) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 581 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 582 | |
| 583 | // Write Bp2Build metrics into $LOG_DIR |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 584 | func writeBp2BuildMetrics(codegenMetrics *bp2build.CodegenMetrics, |
| 585 | configuration android.Config, eventHandler metrics.EventHandler) { |
| 586 | for _, event := range eventHandler.CompletedEvents() { |
| 587 | codegenMetrics.Events = append(codegenMetrics.Events, |
| 588 | &bp2build_metrics_proto.Event{ |
| 589 | Name: event.Id, |
| 590 | StartTime: uint64(event.Start.UnixNano()), |
| 591 | RealTime: event.RuntimeNanoseconds(), |
| 592 | }) |
| 593 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 594 | metricsDir := configuration.Getenv("LOG_DIR") |
| 595 | if len(metricsDir) < 1 { |
| 596 | fmt.Fprintf(os.Stderr, "\nMissing required env var for generating bp2build metrics: LOG_DIR\n") |
| 597 | os.Exit(1) |
| 598 | } |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 599 | codegenMetrics.Write(metricsDir) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 600 | } |