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