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