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