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" |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 19 | "errors" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 20 | "flag" |
| 21 | "fmt" |
| 22 | "os" |
| 23 | "path/filepath" |
Chris Parsons | 8152a94 | 2023-06-06 16:17:50 +0000 | [diff] [blame] | 24 | "regexp" |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 25 | "strings" |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 26 | "time" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 28 | "android/soong/android" |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 29 | "android/soong/android/allowlists" |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 30 | "android/soong/bazel" |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 31 | "android/soong/bp2build" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 32 | "android/soong/shared" |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 33 | "android/soong/ui/metrics/bp2build_metrics_proto" |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 34 | |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 35 | "github.com/google/blueprint" |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 36 | "github.com/google/blueprint/bootstrap" |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 37 | "github.com/google/blueprint/deptools" |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 38 | "github.com/google/blueprint/metrics" |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 39 | androidProtobuf "google.golang.org/protobuf/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 42 | var ( |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 43 | topDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 44 | availableEnvFile string |
| 45 | usedEnvFile string |
| 46 | |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 47 | globFile string |
| 48 | globListDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 49 | delveListen string |
| 50 | delvePath string |
| 51 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 52 | cmdlineArgs android.CmdArgs |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 53 | ) |
| 54 | |
| 55 | func init() { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 56 | // Flags that make sense in every mode |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 57 | flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 58 | flag.StringVar(&cmdlineArgs.SoongOutDir, "soong_out", "", "Soong output directory (usually $TOP/out/soong)") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 59 | flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables") |
| 60 | flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 61 | flag.StringVar(&globFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output") |
| 62 | flag.StringVar(&globListDir, "globListDir", "", "the directory containing the glob list files") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 63 | flag.StringVar(&cmdlineArgs.OutDir, "out", "", "the ninja builddir directory") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 64 | flag.StringVar(&cmdlineArgs.ModuleListFile, "l", "", "file that lists filepaths to parse") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 65 | |
| 66 | // Debug flags |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 67 | flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging") |
| 68 | 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] | 69 | flag.StringVar(&cmdlineArgs.Cpuprofile, "cpuprofile", "", "write cpu profile to file") |
| 70 | flag.StringVar(&cmdlineArgs.TraceFile, "trace", "", "write trace to file") |
| 71 | flag.StringVar(&cmdlineArgs.Memprofile, "memprofile", "", "write memory profile to file") |
| 72 | flag.BoolVar(&cmdlineArgs.NoGC, "nogc", false, "turn off GC for debugging") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 73 | |
| 74 | // Flags representing various modes soong_build can run in |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 75 | flag.StringVar(&cmdlineArgs.ModuleGraphFile, "module_graph_file", "", "JSON module graph file to output") |
| 76 | flag.StringVar(&cmdlineArgs.ModuleActionsFile, "module_actions_file", "", "JSON file to output inputs/outputs of actions of modules") |
| 77 | flag.StringVar(&cmdlineArgs.DocFile, "soong_docs", "", "build documentation file to output") |
| 78 | flag.StringVar(&cmdlineArgs.BazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top") |
| 79 | flag.StringVar(&cmdlineArgs.BazelApiBp2buildDir, "bazel_api_bp2build_dir", "", "path to the bazel api_bp2build directory relative to --top") |
| 80 | flag.StringVar(&cmdlineArgs.Bp2buildMarker, "bp2build_marker", "", "If set, run bp2build, touch the specified marker file then exit") |
| 81 | flag.StringVar(&cmdlineArgs.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] | 82 | flag.StringVar(&cmdlineArgs.OutFile, "o", "build.ninja", "the Ninja file to output") |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 83 | flag.StringVar(&cmdlineArgs.SoongVariables, "soong_variables", "soong.variables", "the file contains all build variables") |
MarkDacek | d06db5d | 2022-11-29 00:47:59 +0000 | [diff] [blame] | 84 | 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] | 85 | flag.BoolVar(&cmdlineArgs.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file") |
LaMont Jones | 52a7243 | 2023-03-09 18:19:35 +0000 | [diff] [blame] | 86 | flag.BoolVar(&cmdlineArgs.MultitreeBuild, "multitree-build", false, "this is a multitree build") |
Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 87 | flag.BoolVar(&cmdlineArgs.BazelMode, "bazel-mode", false, "use bazel for analysis of certain modules") |
Jingwen Chen | e647db8 | 2022-11-01 11:28:29 +0000 | [diff] [blame] | 88 | flag.BoolVar(&cmdlineArgs.BazelModeStaging, "bazel-mode-staging", false, "use bazel for analysis of certain near-ready modules") |
Chris Parsons | 9402ca8 | 2023-02-23 17:28:06 -0500 | [diff] [blame] | 89 | flag.BoolVar(&cmdlineArgs.UseBazelProxy, "use-bazel-proxy", false, "communicate with bazel using unix socket proxy instead of spawning subprocesses") |
Jihoon Kang | 1bff034 | 2023-01-17 20:40:22 +0000 | [diff] [blame] | 90 | flag.BoolVar(&cmdlineArgs.BuildFromTextStub, "build-from-text-stub", false, "build Java stubs from API text files instead of source files") |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 91 | flag.BoolVar(&cmdlineArgs.EnsureAllowlistIntegrity, "ensure-allowlist-integrity", false, "verify that allowlisted modules are mixed-built") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 92 | // Flags that probably shouldn't be flags of soong_build, but we haven't found |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 93 | // the time to remove them yet |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 94 | flag.BoolVar(&cmdlineArgs.RunGoTests, "t", false, "build and run go tests during bootstrap") |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 95 | |
| 96 | // Disable deterministic randomization in the protobuf package, so incremental |
| 97 | // builds with unrelated Soong changes don't trigger large rebuilds (since we |
| 98 | // write out text protos in command lines, and command line changes trigger |
| 99 | // rebuilds). |
| 100 | androidProtobuf.DisableRand() |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 103 | func newNameResolver(config android.Config) *android.NameResolver { |
Paul Duffin | 3f7bf9f | 2022-11-08 12:21:15 +0000 | [diff] [blame] | 104 | return android.NewNameResolver(config) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Lukacs T. Berki | ffc9e8d | 2021-09-07 17:54:38 +0200 | [diff] [blame] | 107 | func newContext(configuration android.Config) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 108 | ctx := android.NewContext(configuration) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 109 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 110 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
Spandan Das | c576383 | 2022-11-08 18:42:16 +0000 | [diff] [blame] | 111 | ctx.AddIncludeTags(configuration.IncludeTags()...) |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 112 | ctx.AddSourceRootDirs(configuration.SourceRootDirs()...) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 113 | return ctx |
| 114 | } |
| 115 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 116 | // Bazel-enabled mode. Attaches a mutator to queue Bazel requests, adds a |
| 117 | // BeforePrepareBuildActionsHook to invoke Bazel, and then uses Bazel metadata |
| 118 | // for modules that should be handled by Bazel. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 119 | func runMixedModeBuild(ctx *android.Context, extraNinjaDeps []string) string { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 120 | ctx.EventHandler.Begin("mixed_build") |
| 121 | defer ctx.EventHandler.End("mixed_build") |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 122 | |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 123 | bazelHook := func() error { |
Cole Faust | bc65a3f | 2023-08-01 16:38:55 +0000 | [diff] [blame^] | 124 | err := ctx.Config().BazelContext.QueueBazelSandwichCqueryRequests(ctx.Config()) |
| 125 | if err != nil { |
| 126 | return err |
| 127 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 128 | return ctx.Config().BazelContext.InvokeBazel(ctx.Config(), ctx) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 129 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 130 | ctx.SetBeforePrepareBuildActionsHook(bazelHook) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 131 | ninjaDeps, err := bootstrap.RunBlueprint(cmdlineArgs.Args, bootstrap.DoEverything, ctx.Context, ctx.Config()) |
| 132 | maybeQuit(err, "") |
Chris Parsons | 027881c | 2022-05-24 15:38:38 -0400 | [diff] [blame] | 133 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 134 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 135 | bazelPaths, err := readFileLines(ctx.Config().Getenv("BAZEL_DEPS_FILE")) |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 136 | if err != nil { |
| 137 | panic("Bazel deps file not found: " + err.Error()) |
| 138 | } |
| 139 | ninjaDeps = append(ninjaDeps, bazelPaths...) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 140 | ninjaDeps = append(ninjaDeps, writeBuildGlobsNinjaFile(ctx)...) |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 141 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 142 | writeDepFile(cmdlineArgs.OutFile, ctx.EventHandler, ninjaDeps) |
Jeongik Cha | 591366d | 2023-05-08 11:32:52 +0900 | [diff] [blame] | 143 | |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 144 | if needToWriteNinjaHint(ctx) { |
Jeongik Cha | 591366d | 2023-05-08 11:32:52 +0900 | [diff] [blame] | 145 | writeNinjaHint(ctx) |
| 146 | } |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 147 | return cmdlineArgs.OutFile |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 150 | func needToWriteNinjaHint(ctx *android.Context) bool { |
| 151 | switch ctx.Config().GetenvWithDefault("SOONG_GENERATES_NINJA_HINT", "") { |
| 152 | case "always": |
| 153 | return true |
| 154 | case "depend": |
| 155 | if _, err := os.Stat(filepath.Join(ctx.Config().OutDir(), ".ninja_log")); errors.Is(err, os.ErrNotExist) { |
| 156 | return true |
| 157 | } |
| 158 | } |
| 159 | return false |
| 160 | } |
| 161 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 162 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 163 | func runQueryView(queryviewDir, queryviewMarker string, ctx *android.Context) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 164 | ctx.EventHandler.Begin("queryview") |
| 165 | defer ctx.EventHandler.End("queryview") |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 166 | codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.QueryView, topDir) |
Spandan Das | 98cb856 | 2023-03-09 23:05:47 +0000 | [diff] [blame] | 167 | err := createBazelWorkspace(codegenContext, shared.JoinPath(topDir, queryviewDir), false) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 168 | maybeQuit(err, "") |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 169 | touch(shared.JoinPath(topDir, queryviewMarker)) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 172 | // Run the code-generation phase to convert API contributions to BUILD files. |
| 173 | // Return marker file for the new synthetic workspace |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 174 | func runApiBp2build(ctx *android.Context, extraNinjaDeps []string) string { |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 175 | ctx.EventHandler.Begin("api_bp2build") |
| 176 | defer ctx.EventHandler.End("api_bp2build") |
Spandan Das | 255648c | 2023-01-11 03:05:24 +0000 | [diff] [blame] | 177 | // api_bp2build does not run the typical pipeline of soong mutators. |
| 178 | // Hoevever, it still runs the defaults mutator which can create dependencies. |
| 179 | // These dependencies might not always exist (e.g. in tests) |
| 180 | ctx.SetAllowMissingDependencies(ctx.Config().AllowMissingDependencies()) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 181 | ctx.RegisterForApiBazelConversion() |
| 182 | |
| 183 | // Register the Android.bp files in the tree |
| 184 | // Add them to the workspace's .d file |
| 185 | ctx.SetModuleListFile(cmdlineArgs.ModuleListFile) |
| 186 | if paths, err := ctx.ListModulePaths("."); err == nil { |
| 187 | extraNinjaDeps = append(extraNinjaDeps, paths...) |
| 188 | } else { |
| 189 | panic(err) |
| 190 | } |
| 191 | |
| 192 | // Run the loading and analysis phase |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 193 | ninjaDeps, err := bootstrap.RunBlueprint(cmdlineArgs.Args, |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 194 | bootstrap.StopBeforePrepareBuildActions, |
| 195 | ctx.Context, |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 196 | ctx.Config()) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 197 | maybeQuit(err, "") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 198 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 199 | |
| 200 | // Add the globbed dependencies |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 201 | ninjaDeps = append(ninjaDeps, writeBuildGlobsNinjaFile(ctx)...) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 202 | |
| 203 | // Run codegen to generate BUILD files |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 204 | codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.ApiBp2build, topDir) |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 205 | absoluteApiBp2buildDir := shared.JoinPath(topDir, cmdlineArgs.BazelApiBp2buildDir) |
Spandan Das | 98cb856 | 2023-03-09 23:05:47 +0000 | [diff] [blame] | 206 | // Always generate bp2build_all_srcs filegroups in api_bp2build. |
| 207 | // This is necessary to force each Android.bp file to create an equivalent BUILD file |
| 208 | // and prevent package boundray issues. |
| 209 | // e.g. |
| 210 | // Source |
| 211 | // f/b/Android.bp |
| 212 | // java_library{ |
| 213 | // name: "foo", |
| 214 | // api: "api/current.txt", |
| 215 | // } |
| 216 | // |
| 217 | // f/b/api/Android.bp <- will cause package boundary issues |
| 218 | // |
| 219 | // Gen |
| 220 | // f/b/BUILD |
| 221 | // java_contribution{ |
| 222 | // name: "foo.contribution", |
| 223 | // api: "//f/b/api:current.txt", |
| 224 | // } |
| 225 | // |
| 226 | // If we don't generate f/b/api/BUILD, foo.contribution will be unbuildable. |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 227 | err = createBazelWorkspace(codegenContext, absoluteApiBp2buildDir, true) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 228 | maybeQuit(err, "") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 229 | ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) |
| 230 | |
| 231 | // Create soong_injection repository |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 232 | soongInjectionFiles, workspaceFiles, err := bp2build.CreateSoongInjectionDirFiles(codegenContext, bp2build.CreateCodegenMetrics()) |
Cole Faust | 9e384e2 | 2023-02-08 17:43:09 -0800 | [diff] [blame] | 233 | maybeQuit(err, "") |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 234 | absoluteSoongInjectionDir := shared.JoinPath(topDir, ctx.Config().SoongOutDir(), bazel.SoongInjectionDirName) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 235 | for _, file := range soongInjectionFiles { |
Spandan Das | 067210f | 2022-12-07 01:14:52 +0000 | [diff] [blame] | 236 | // The API targets in api_bp2build workspace do not have any dependency on api_bp2build. |
| 237 | // But we need to create these files to prevent errors during Bazel analysis. |
| 238 | // These need to be created in Read-Write mode. |
| 239 | // This is because the subsequent step (bp2build in api domain analysis) creates them in Read-Write mode |
| 240 | // to allow users to edit/experiment in the synthetic workspace. |
| 241 | writeReadWriteFile(absoluteSoongInjectionDir, file) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 242 | } |
Cole Faust | f8231dd | 2023-04-21 17:37:11 -0700 | [diff] [blame] | 243 | for _, file := range workspaceFiles { |
| 244 | writeReadWriteFile(absoluteApiBp2buildDir, file) |
| 245 | } |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 246 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 247 | workspace := shared.JoinPath(ctx.Config().SoongOutDir(), "api_bp2build") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 248 | // Create the symlink forest |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 249 | symlinkDeps, _, _ := bp2build.PlantSymlinkForest( |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 250 | ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE"), |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 251 | topDir, |
| 252 | workspace, |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 253 | cmdlineArgs.BazelApiBp2buildDir, |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 254 | apiBuildFileExcludes(ctx)) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 255 | ninjaDeps = append(ninjaDeps, symlinkDeps...) |
| 256 | |
| 257 | workspaceMarkerFile := workspace + ".marker" |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 258 | writeDepFile(workspaceMarkerFile, ctx.EventHandler, ninjaDeps) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 259 | touch(shared.JoinPath(topDir, workspaceMarkerFile)) |
| 260 | return workspaceMarkerFile |
| 261 | } |
| 262 | |
| 263 | // With some exceptions, api_bp2build does not have any dependencies on the checked-in BUILD files |
| 264 | // Exclude them from the generated workspace to prevent unrelated errors during the loading phase |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 265 | func apiBuildFileExcludes(ctx *android.Context) []string { |
| 266 | ret := bazelArtifacts() |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 267 | srcs, err := getExistingBazelRelatedFiles(topDir) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 268 | maybeQuit(err, "Error determining existing Bazel-related files") |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 269 | for _, src := range srcs { |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 270 | // Exclude all src BUILD files |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 271 | if src != "WORKSPACE" && |
| 272 | src != "BUILD" && |
| 273 | src != "BUILD.bazel" && |
| 274 | !strings.HasPrefix(src, "build/bazel") && |
Spandan Das | 6b91a38 | 2022-11-30 02:17:06 +0000 | [diff] [blame] | 275 | !strings.HasPrefix(src, "external/bazel-skylib") && |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 276 | !strings.HasPrefix(src, "prebuilts/clang") { |
| 277 | ret = append(ret, src) |
| 278 | } |
| 279 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 280 | // Android.bp files for api surfaces are mounted to out/, but out/ should not be a |
| 281 | // dep for api_bp2build. Otherwise, api_bp2build will be run every single time |
| 282 | ret = append(ret, ctx.Config().OutDir()) |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 283 | return ret |
| 284 | } |
| 285 | |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 286 | func writeNinjaHint(ctx *android.Context) error { |
Jeongik Cha | 73d4911 | 2023-05-04 18:16:11 +0900 | [diff] [blame] | 287 | ctx.BeginEvent("ninja_hint") |
| 288 | defer ctx.EndEvent("ninja_hint") |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 289 | // The current predictor focuses on reducing false negatives. |
| 290 | // If there are too many false positives (e.g., most modules are marked as positive), |
| 291 | // real long-running jobs cannot run early. |
| 292 | // Therefore, the model should be adjusted in this case. |
| 293 | // The model should also be adjusted if there are critical false negatives. |
| 294 | predicate := func(j *blueprint.JsonModule) (prioritized bool, weight int) { |
| 295 | prioritized = false |
| 296 | weight = 0 |
| 297 | for prefix, w := range allowlists.HugeModuleTypePrefixMap { |
| 298 | if strings.HasPrefix(j.Type, prefix) { |
| 299 | prioritized = true |
| 300 | weight = w |
| 301 | return |
| 302 | } |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 303 | } |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 304 | dep_count := len(j.Deps) |
| 305 | src_count := 0 |
| 306 | for _, a := range j.Module["Actions"].([]blueprint.JSONAction) { |
| 307 | src_count += len(a.Inputs) |
| 308 | } |
| 309 | input_size := dep_count + src_count |
| 310 | |
| 311 | // Current threshold is an arbitrary value which only consider recall rather than accuracy. |
| 312 | if input_size > allowlists.INPUT_SIZE_THRESHOLD { |
| 313 | prioritized = true |
| 314 | weight += ((input_size) / allowlists.INPUT_SIZE_THRESHOLD) * allowlists.DEFAULT_PRIORITIZED_WEIGHT |
| 315 | |
| 316 | // To prevent some modules from having too large a priority value. |
| 317 | if weight > allowlists.HIGH_PRIORITIZED_WEIGHT { |
| 318 | weight = allowlists.HIGH_PRIORITIZED_WEIGHT |
| 319 | } |
| 320 | } |
| 321 | return |
| 322 | } |
| 323 | |
| 324 | outputsMap := ctx.Context.GetWeightedOutputsFromPredicate(predicate) |
| 325 | var outputBuilder strings.Builder |
| 326 | for output, weight := range outputsMap { |
| 327 | outputBuilder.WriteString(fmt.Sprintf("%s,%d\n", output, weight)) |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 328 | } |
| 329 | weightListFile := filepath.Join(topDir, ctx.Config().OutDir(), ".ninja_weight_list") |
| 330 | |
| 331 | err := os.WriteFile(weightListFile, []byte(outputBuilder.String()), 0644) |
| 332 | if err != nil { |
| 333 | return fmt.Errorf("could not write ninja weight list file %s", err) |
| 334 | } |
| 335 | return nil |
| 336 | } |
| 337 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 338 | func writeMetrics(configuration android.Config, eventHandler *metrics.EventHandler, metricsDir string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 339 | if len(metricsDir) < 1 { |
| 340 | fmt.Fprintf(os.Stderr, "\nMissing required env var for generating soong metrics: LOG_DIR\n") |
| 341 | os.Exit(1) |
| 342 | } |
| 343 | metricsFile := filepath.Join(metricsDir, "soong_build_metrics.pb") |
| 344 | err := android.WriteMetrics(configuration, eventHandler, metricsFile) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 345 | maybeQuit(err, "error writing soong_build metrics %s", metricsFile) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 346 | } |
| 347 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 348 | // Errors out if any modules expected to be mixed_built were not, unless |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 349 | // the modules did not exist. |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 350 | func checkForAllowlistIntegrityError(configuration android.Config, isStagingMode bool) error { |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 351 | modules := findMisconfiguredModules(configuration, isStagingMode) |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 352 | if len(modules) == 0 { |
| 353 | return nil |
| 354 | } |
| 355 | |
| 356 | return fmt.Errorf("Error: expected the following modules to be mixed_built: %s", modules) |
| 357 | } |
| 358 | |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 359 | // Returns true if the given module has all of the following true: |
| 360 | // 1. Is allowlisted to be built with Bazel. |
| 361 | // 2. Has a variant which is *not* built with Bazel. |
| 362 | // 3. Has no variant which is built with Bazel. |
| 363 | // |
| 364 | // This indicates the allowlisting of this variant had no effect. |
| 365 | // TODO(b/280457637): Return true for nonexistent modules. |
| 366 | func isAllowlistMisconfiguredForModule(module string, mixedBuildsEnabled map[string]struct{}, mixedBuildsDisabled map[string]struct{}) bool { |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 367 | _, enabled := mixedBuildsEnabled[module] |
| 368 | |
| 369 | if enabled { |
| 370 | return false |
| 371 | } |
| 372 | |
| 373 | _, disabled := mixedBuildsDisabled[module] |
| 374 | return disabled |
| 375 | |
| 376 | } |
| 377 | |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 378 | // Returns the list of modules that should have been mixed_built (per the |
| 379 | // allowlists and cmdline flags) but were not. |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 380 | // Note: nonexistent modules are excluded from the list. See b/280457637 |
| 381 | func findMisconfiguredModules(configuration android.Config, isStagingMode bool) []string { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 382 | retval := []string{} |
| 383 | forceEnabledModules := configuration.BazelModulesForceEnabledByFlag() |
| 384 | |
| 385 | mixedBuildsEnabled := configuration.GetMixedBuildsEnabledModules() |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 386 | mixedBuildsDisabled := configuration.GetMixedBuildsDisabledModules() |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 387 | for _, module := range allowlists.ProdMixedBuildsEnabledList { |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 388 | if isAllowlistMisconfiguredForModule(module, mixedBuildsEnabled, mixedBuildsDisabled) { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 389 | retval = append(retval, module) |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | if isStagingMode { |
| 394 | for _, module := range allowlists.StagingMixedBuildsEnabledList { |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 395 | if isAllowlistMisconfiguredForModule(module, mixedBuildsEnabled, mixedBuildsDisabled) { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 396 | retval = append(retval, module) |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | for module, _ := range forceEnabledModules { |
MarkDacek | 6f6b962 | 2023-05-02 16:28:55 +0000 | [diff] [blame] | 402 | if isAllowlistMisconfiguredForModule(module, mixedBuildsEnabled, mixedBuildsDisabled) { |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 403 | retval = append(retval, module) |
| 404 | } |
| 405 | } |
| 406 | return retval |
| 407 | } |
| 408 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 409 | func writeJsonModuleGraphAndActions(ctx *android.Context, cmdArgs android.CmdArgs) { |
| 410 | graphFile, graphErr := os.Create(shared.JoinPath(topDir, cmdArgs.ModuleGraphFile)) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 411 | maybeQuit(graphErr, "graph err") |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 412 | defer graphFile.Close() |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 413 | actionsFile, actionsErr := os.Create(shared.JoinPath(topDir, cmdArgs.ModuleActionsFile)) |
| 414 | maybeQuit(actionsErr, "actions err") |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 415 | defer actionsFile.Close() |
| 416 | ctx.Context.PrintJSONGraphAndActions(graphFile, actionsFile) |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 417 | } |
| 418 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 419 | func writeBuildGlobsNinjaFile(ctx *android.Context) []string { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 420 | ctx.EventHandler.Begin("globs_ninja_file") |
| 421 | defer ctx.EventHandler.End("globs_ninja_file") |
| 422 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 423 | globDir := bootstrap.GlobDirectory(ctx.Config().SoongOutDir(), globListDir) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 424 | err := bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{ |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 425 | GlobLister: ctx.Globs, |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 426 | GlobFile: globFile, |
| 427 | GlobDir: globDir, |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 428 | SrcDir: ctx.SrcDir(), |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 429 | }, ctx.Config()) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 430 | maybeQuit(err, "") |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 431 | return bootstrap.GlobFileListFiles(globDir) |
| 432 | } |
| 433 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 434 | func writeDepFile(outputFile string, eventHandler *metrics.EventHandler, ninjaDeps []string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 435 | eventHandler.Begin("ninja_deps") |
| 436 | defer eventHandler.End("ninja_deps") |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 437 | depFile := shared.JoinPath(topDir, outputFile+".d") |
| 438 | err := deptools.WriteDepFile(depFile, outputFile, ninjaDeps) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 439 | maybeQuit(err, "error writing depfile '%s'", depFile) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 440 | } |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 441 | |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 442 | // runSoongOnlyBuild runs the standard Soong build in a number of different modes. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 443 | func runSoongOnlyBuild(ctx *android.Context, extraNinjaDeps []string) string { |
Paul Duffin | 39eae8f | 2022-11-05 14:59:52 +0000 | [diff] [blame] | 444 | ctx.EventHandler.Begin("soong_build") |
| 445 | defer ctx.EventHandler.End("soong_build") |
| 446 | |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 447 | var stopBefore bootstrap.StopBefore |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 448 | switch ctx.Config().BuildMode { |
| 449 | case android.GenerateModuleGraph: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 450 | stopBefore = bootstrap.StopBeforeWriteNinja |
Usta Shrestha | 7fae695 | 2022-12-21 11:46:28 -0500 | [diff] [blame] | 451 | case android.GenerateQueryView, android.GenerateDocFile: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 452 | stopBefore = bootstrap.StopBeforePrepareBuildActions |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 453 | default: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 454 | stopBefore = bootstrap.DoEverything |
| 455 | } |
| 456 | |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 457 | ninjaDeps, err := bootstrap.RunBlueprint(cmdlineArgs.Args, stopBefore, ctx.Context, ctx.Config()) |
| 458 | maybeQuit(err, "") |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 459 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 460 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 461 | globListFiles := writeBuildGlobsNinjaFile(ctx) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 462 | ninjaDeps = append(ninjaDeps, globListFiles...) |
| 463 | |
| 464 | // Convert the Soong module graph into Bazel BUILD files. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 465 | switch ctx.Config().BuildMode { |
| 466 | case android.GenerateQueryView: |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 467 | queryviewMarkerFile := cmdlineArgs.BazelQueryViewDir + ".marker" |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 468 | runQueryView(cmdlineArgs.BazelQueryViewDir, queryviewMarkerFile, ctx) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 469 | writeDepFile(queryviewMarkerFile, ctx.EventHandler, ninjaDeps) |
| 470 | return queryviewMarkerFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 471 | case android.GenerateModuleGraph: |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 472 | writeJsonModuleGraphAndActions(ctx, cmdlineArgs) |
| 473 | writeDepFile(cmdlineArgs.ModuleGraphFile, ctx.EventHandler, ninjaDeps) |
| 474 | return cmdlineArgs.ModuleGraphFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 475 | case android.GenerateDocFile: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 476 | // TODO: we could make writeDocs() return the list of documentation files |
| 477 | // written and add them to the .d file. Then soong_docs would be re-run |
| 478 | // whenever one is deleted. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 479 | err := writeDocs(ctx, shared.JoinPath(topDir, cmdlineArgs.DocFile)) |
| 480 | maybeQuit(err, "error building Soong documentation") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 481 | writeDepFile(cmdlineArgs.DocFile, ctx.EventHandler, ninjaDeps) |
| 482 | return cmdlineArgs.DocFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 483 | default: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 484 | // The actual output (build.ninja) was written in the RunBlueprint() call |
| 485 | // above |
| 486 | writeDepFile(cmdlineArgs.OutFile, ctx.EventHandler, ninjaDeps) |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 487 | if needToWriteNinjaHint(ctx) { |
Jeongik Cha | 591366d | 2023-05-08 11:32:52 +0900 | [diff] [blame] | 488 | writeNinjaHint(ctx) |
| 489 | } |
Paul Duffin | b713ddf | 2022-11-05 16:14:30 +0000 | [diff] [blame] | 490 | return cmdlineArgs.OutFile |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 491 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | // soong_ui dumps the available environment variables to |
| 495 | // soong.environment.available . Then soong_build itself is run with an empty |
| 496 | // environment so that the only way environment variables can be accessed is |
| 497 | // using Config, which tracks access to them. |
| 498 | |
| 499 | // At the end of the build, a file called soong.environment.used is written |
| 500 | // containing the current value of all used environment variables. The next |
| 501 | // time soong_ui is run, it checks whether any environment variables that was |
| 502 | // used had changed and if so, it deletes soong.environment.used to cause a |
| 503 | // rebuild. |
| 504 | // |
| 505 | // The dependency of build.ninja on soong.environment.used is declared in |
| 506 | // build.ninja.d |
| 507 | func parseAvailableEnv() map[string]string { |
| 508 | if availableEnvFile == "" { |
| 509 | fmt.Fprintf(os.Stderr, "--available_env not set\n") |
| 510 | os.Exit(1) |
| 511 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 512 | result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile)) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 513 | maybeQuit(err, "error reading available environment file '%s'", availableEnvFile) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 514 | return result |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 515 | } |
| 516 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 517 | func main() { |
| 518 | flag.Parse() |
| 519 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 520 | shared.ReexecWithDelveMaybe(delveListen, delvePath) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 521 | android.InitSandbox(topDir) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 522 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 523 | availableEnv := parseAvailableEnv() |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 524 | configuration, err := android.NewConfig(cmdlineArgs, availableEnv) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 525 | maybeQuit(err, "") |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 526 | if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 527 | configuration.SetAllowMissingDependencies() |
| 528 | } |
| 529 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 530 | extraNinjaDeps := []string{configuration.ProductVariablesFileName, usedEnvFile} |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 531 | if shared.IsDebugging() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 532 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 533 | // enabled even if it completed successfully. |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 534 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.SoongOutDir(), "always_rerun_for_delve")) |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 535 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 536 | |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 537 | // Bypass configuration.Getenv, as LOG_DIR does not need to be dependency tracked. By definition, it will |
| 538 | // change between every CI build, so tracking it would require re-running Soong for every build. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 539 | metricsDir := availableEnv["LOG_DIR"] |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 540 | |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 541 | ctx := newContext(configuration) |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 542 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 543 | var finalOutputFile string |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 544 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 545 | // Run Soong for a specific activity, like bp2build, queryview |
| 546 | // or the actual Soong build for the build.ninja file. |
| 547 | switch configuration.BuildMode { |
| 548 | case android.SymlinkForest: |
| 549 | finalOutputFile = runSymlinkForestCreation(ctx, extraNinjaDeps, metricsDir) |
| 550 | case android.Bp2build: |
| 551 | // Run the alternate pipeline of bp2build mutators and singleton to convert |
| 552 | // Blueprint to BUILD files before everything else. |
| 553 | finalOutputFile = runBp2Build(ctx, extraNinjaDeps, metricsDir) |
| 554 | case android.ApiBp2build: |
| 555 | finalOutputFile = runApiBp2build(ctx, extraNinjaDeps) |
| 556 | writeMetrics(configuration, ctx.EventHandler, metricsDir) |
| 557 | default: |
| 558 | ctx.Register() |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 559 | isMixedBuildsEnabled := configuration.IsMixedBuildsEnabled() |
| 560 | if isMixedBuildsEnabled { |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 561 | finalOutputFile = runMixedModeBuild(ctx, extraNinjaDeps) |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 562 | if cmdlineArgs.EnsureAllowlistIntegrity { |
| 563 | if err := checkForAllowlistIntegrityError(configuration, cmdlineArgs.BazelModeStaging); err != nil { |
| 564 | maybeQuit(err, "") |
| 565 | } |
| 566 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 567 | } else { |
| 568 | finalOutputFile = runSoongOnlyBuild(ctx, extraNinjaDeps) |
| 569 | } |
| 570 | writeMetrics(configuration, ctx.EventHandler, metricsDir) |
| 571 | } |
Chris Parsons | c83398f | 2023-05-31 18:41:41 +0000 | [diff] [blame] | 572 | |
| 573 | // Register this environment variablesas being an implicit dependencies of |
| 574 | // soong_build. Changes to this environment variable will result in |
| 575 | // retriggering soong_build. |
| 576 | configuration.Getenv("USE_BAZEL_VERSION") |
| 577 | |
Chris Parsons | a3ae007 | 2023-05-10 21:10:08 +0000 | [diff] [blame] | 578 | writeUsedEnvironmentFile(configuration) |
| 579 | |
| 580 | // Touch the output file so that it's the newest file created by soong_build. |
| 581 | // This is necessary because, if soong_build generated any files which |
| 582 | // are ninja inputs to the main output file, then ninja would superfluously |
| 583 | // rebuild this output file on the next build invocation. |
| 584 | touch(shared.JoinPath(topDir, finalOutputFile)) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 585 | } |
| 586 | |
Chris Parsons | a3ae007 | 2023-05-10 21:10:08 +0000 | [diff] [blame] | 587 | func writeUsedEnvironmentFile(configuration android.Config) { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 588 | if usedEnvFile == "" { |
| 589 | return |
| 590 | } |
| 591 | |
| 592 | path := shared.JoinPath(topDir, usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 593 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 594 | maybeQuit(err, "error writing used environment file '%s'\n", usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 595 | |
Usta Shrestha | 2ba28a3 | 2022-10-24 11:33:09 -0400 | [diff] [blame] | 596 | if preexistingData, err := os.ReadFile(path); err != nil { |
| 597 | if !os.IsNotExist(err) { |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 598 | maybeQuit(err, "error reading used environment file '%s'", usedEnvFile) |
Usta Shrestha | 2ba28a3 | 2022-10-24 11:33:09 -0400 | [diff] [blame] | 599 | } |
| 600 | } else if bytes.Equal(preexistingData, data) { |
| 601 | // used environment file is unchanged |
| 602 | return |
| 603 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 604 | err = os.WriteFile(path, data, 0666) |
| 605 | maybeQuit(err, "error writing used environment file '%s'", usedEnvFile) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 606 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 607 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 608 | func touch(path string) { |
| 609 | f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 610 | maybeQuit(err, "Error touching '%s'", path) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 611 | err = f.Close() |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 612 | maybeQuit(err, "Error touching '%s'", path) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 613 | |
| 614 | currentTime := time.Now().Local() |
| 615 | err = os.Chtimes(path, currentTime, currentTime) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 616 | maybeQuit(err, "error touching '%s'", path) |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 617 | } |
| 618 | |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 619 | // Read the bazel.list file that the Soong Finder already dumped earlier (hopefully) |
| 620 | // It contains the locations of BUILD files, BUILD.bazel files, etc. in the source dir |
| 621 | func getExistingBazelRelatedFiles(topDir string) ([]string, error) { |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 622 | bazelFinderFile := filepath.Join(filepath.Dir(cmdlineArgs.ModuleListFile), "bazel.list") |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 623 | if !filepath.IsAbs(bazelFinderFile) { |
| 624 | // Assume this was a relative path under topDir |
| 625 | bazelFinderFile = filepath.Join(topDir, bazelFinderFile) |
| 626 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 627 | return readFileLines(bazelFinderFile) |
Rupert Shuttleworth | e03bb61 | 2021-05-20 19:34:50 -0400 | [diff] [blame] | 628 | } |
| 629 | |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 630 | func bazelArtifacts() []string { |
| 631 | return []string{ |
| 632 | "bazel-bin", |
| 633 | "bazel-genfiles", |
| 634 | "bazel-out", |
| 635 | "bazel-testlogs", |
Usta (Tsering) Shrestha | c4c07b1 | 2022-11-08 18:31:14 -0500 | [diff] [blame] | 636 | "bazel-workspace", |
Spandan Das | 5af0bd3 | 2022-09-28 20:43:08 +0000 | [diff] [blame] | 637 | "bazel-" + filepath.Base(topDir), |
| 638 | } |
| 639 | } |
| 640 | |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 641 | // This could in theory easily be separated into a binary that generically |
| 642 | // merges two directories into a symlink tree. The main obstacle is that this |
| 643 | // function currently depends on both Bazel-specific knowledge (the existence |
| 644 | // of bazel-* symlinks) and configuration (the set of BUILD.bazel files that |
| 645 | // should and should not be kept) |
| 646 | // |
| 647 | // Ideally, bp2build would write a file that contains instructions to the |
| 648 | // symlink tree creation binary. Then the latter would not need to depend on |
| 649 | // the very heavy-weight machinery of soong_build . |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 650 | func runSymlinkForestCreation(ctx *android.Context, extraNinjaDeps []string, metricsDir string) string { |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 651 | var ninjaDeps []string |
| 652 | var mkdirCount, symlinkCount uint64 |
| 653 | |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 654 | ctx.EventHandler.Do("symlink_forest", func() { |
Usta (Tsering) Shrestha | 93b2a9b | 2022-12-01 05:55:35 +0000 | [diff] [blame] | 655 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 656 | verbose := ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") |
Usta (Tsering) Shrestha | 93b2a9b | 2022-12-01 05:55:35 +0000 | [diff] [blame] | 657 | |
| 658 | // PlantSymlinkForest() returns all the directories that were readdir()'ed. |
| 659 | // Such a directory SHOULD be added to `ninjaDeps` so that a child directory |
| 660 | // or file created/deleted under it would trigger an update of the symlink forest. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 661 | generatedRoot := shared.JoinPath(ctx.Config().SoongOutDir(), "bp2build") |
| 662 | workspaceRoot := shared.JoinPath(ctx.Config().SoongOutDir(), "workspace") |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 663 | var symlinkForestDeps []string |
Usta (Tsering) Shrestha | 93b2a9b | 2022-12-01 05:55:35 +0000 | [diff] [blame] | 664 | ctx.EventHandler.Do("plant", func() { |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 665 | symlinkForestDeps, mkdirCount, symlinkCount = bp2build.PlantSymlinkForest( |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 666 | verbose, topDir, workspaceRoot, generatedRoot, excludedFromSymlinkForest(ctx, verbose)) |
Usta (Tsering) Shrestha | 93b2a9b | 2022-12-01 05:55:35 +0000 | [diff] [blame] | 667 | }) |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 668 | ninjaDeps = append(ninjaDeps, symlinkForestDeps...) |
Usta (Tsering) Shrestha | 93b2a9b | 2022-12-01 05:55:35 +0000 | [diff] [blame] | 669 | }) |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 670 | |
| 671 | writeDepFile(cmdlineArgs.SymlinkForestMarker, ctx.EventHandler, ninjaDeps) |
| 672 | touch(shared.JoinPath(topDir, cmdlineArgs.SymlinkForestMarker)) |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 673 | codegenMetrics := bp2build.ReadCodegenMetrics(metricsDir) |
| 674 | if codegenMetrics == nil { |
| 675 | m := bp2build.CreateCodegenMetrics() |
| 676 | codegenMetrics = &m |
| 677 | } else { |
| 678 | //TODO (usta) we cannot determine if we loaded a stale file, i.e. from an unrelated prior |
| 679 | //invocation of codegen. We should simply use a separate .pb file |
| 680 | } |
Usta Shrestha | da15c61 | 2022-11-08 14:12:36 -0500 | [diff] [blame] | 681 | codegenMetrics.SetSymlinkCount(symlinkCount) |
| 682 | codegenMetrics.SetMkDirCount(mkdirCount) |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 683 | writeBp2BuildMetrics(codegenMetrics, ctx.EventHandler, metricsDir) |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 684 | return cmdlineArgs.SymlinkForestMarker |
Lukacs T. Berki | c541cd2 | 2022-10-26 07:26:50 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 687 | func excludedFromSymlinkForest(ctx *android.Context, verbose bool) []string { |
| 688 | excluded := bazelArtifacts() |
| 689 | if cmdlineArgs.OutDir[0] != '/' { |
| 690 | excluded = append(excluded, cmdlineArgs.OutDir) |
| 691 | } |
| 692 | |
| 693 | // Find BUILD files in the srcDir which are not in the allowlist |
| 694 | // (android.Bp2BuildConversionAllowlist#ShouldKeepExistingBuildFileForDir) |
| 695 | // and return their paths so they can be left out of the Bazel workspace dir (i.e. ignored) |
| 696 | existingBazelFiles, err := getExistingBazelRelatedFiles(topDir) |
| 697 | maybeQuit(err, "Error determining existing Bazel-related files") |
| 698 | |
| 699 | for _, path := range existingBazelFiles { |
| 700 | fullPath := shared.JoinPath(topDir, path) |
| 701 | fileInfo, err2 := os.Stat(fullPath) |
| 702 | if err2 != nil { |
| 703 | // Warn about error, but continue trying to check files |
| 704 | fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", fullPath, err2) |
| 705 | continue |
| 706 | } |
| 707 | // Exclude only files named 'BUILD' or 'BUILD.bazel' and unless forcibly kept |
| 708 | if fileInfo.IsDir() || |
| 709 | (fileInfo.Name() != "BUILD" && fileInfo.Name() != "BUILD.bazel") || |
| 710 | ctx.Config().Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(filepath.Dir(path)) { |
| 711 | // Don't ignore this existing build file |
| 712 | continue |
| 713 | } |
| 714 | if verbose { |
| 715 | fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", path) |
| 716 | } |
| 717 | excluded = append(excluded, path) |
| 718 | } |
| 719 | |
| 720 | // Temporarily exclude stuff to make `bazel build //external/...` (and `bazel build //frameworks/...`) work |
| 721 | excluded = append(excluded, |
| 722 | // FIXME: 'autotest_lib' is a symlink back to external/autotest, and this causes an infinite |
| 723 | // symlink expansion error for Bazel |
| 724 | "external/autotest/venv/autotest_lib", |
| 725 | "external/autotest/autotest_lib", |
| 726 | "external/autotest/client/autotest_lib/client", |
| 727 | |
| 728 | // FIXME: The external/google-fruit/extras/bazel_root/third_party/fruit dir is poison |
| 729 | // It contains several symlinks back to real source dirs, and those source dirs contain |
| 730 | // BUILD files we want to ignore |
| 731 | "external/google-fruit/extras/bazel_root/third_party/fruit", |
| 732 | |
| 733 | // FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue |
| 734 | "frameworks/compile/slang", |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 735 | ) |
| 736 | return excluded |
| 737 | } |
| 738 | |
Chris Parsons | 8152a94 | 2023-06-06 16:17:50 +0000 | [diff] [blame] | 739 | // buildTargetsByPackage parses Bazel BUILD.bazel and BUILD files under |
| 740 | // the workspace, and returns a map containing names of Bazel targets defined in |
| 741 | // these BUILD files. |
| 742 | // For example, maps "//foo/bar" to ["baz", "qux"] if `//foo/bar:{baz,qux}` exist. |
| 743 | func buildTargetsByPackage(ctx *android.Context) map[string][]string { |
| 744 | existingBazelFiles, err := getExistingBazelRelatedFiles(topDir) |
| 745 | maybeQuit(err, "Error determining existing Bazel-related files") |
| 746 | |
| 747 | result := map[string][]string{} |
| 748 | |
| 749 | // Search for instances of `name = "$NAME"` (with arbitrary spacing). |
| 750 | targetNameRegex := regexp.MustCompile(`(?m)^\s*name\s*=\s*\"([^\"]+)\"`) |
| 751 | |
| 752 | for _, path := range existingBazelFiles { |
| 753 | if !ctx.Config().Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(filepath.Dir(path)) { |
| 754 | continue |
| 755 | } |
| 756 | fullPath := shared.JoinPath(topDir, path) |
| 757 | sourceDir := filepath.Dir(path) |
| 758 | fileInfo, err := os.Stat(fullPath) |
| 759 | maybeQuit(err, "Error accessing Bazel file '%s'", fullPath) |
| 760 | |
| 761 | if !fileInfo.IsDir() && |
| 762 | (fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") { |
| 763 | // Process this BUILD file. |
| 764 | buildFileContent, err := os.ReadFile(fullPath) |
| 765 | maybeQuit(err, "Error reading Bazel file '%s'", fullPath) |
| 766 | |
| 767 | matches := targetNameRegex.FindAllStringSubmatch(string(buildFileContent), -1) |
| 768 | for _, match := range matches { |
| 769 | result[sourceDir] = append(result[sourceDir], match[1]) |
| 770 | } |
| 771 | } |
| 772 | } |
| 773 | return result |
| 774 | } |
| 775 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 776 | // Run Soong in the bp2build mode. This creates a standalone context that registers |
| 777 | // an alternate pipeline of mutators and singletons specifically for generating |
| 778 | // Bazel BUILD files instead of Ninja files. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 779 | func runBp2Build(ctx *android.Context, extraNinjaDeps []string, metricsDir string) string { |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 780 | var codegenMetrics *bp2build.CodegenMetrics |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 781 | ctx.EventHandler.Do("bp2build", func() { |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 782 | |
Chris Parsons | 8152a94 | 2023-06-06 16:17:50 +0000 | [diff] [blame] | 783 | ctx.EventHandler.Do("read_build", func() { |
| 784 | ctx.Config().SetBazelBuildFileTargets(buildTargetsByPackage(ctx)) |
| 785 | }) |
| 786 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 787 | // Propagate "allow misssing dependencies" bit. This is normally set in |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 788 | // newContext(), but we create ctx without calling that method. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 789 | ctx.SetAllowMissingDependencies(ctx.Config().AllowMissingDependencies()) |
| 790 | ctx.SetNameInterface(newNameResolver(ctx.Config())) |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 791 | ctx.RegisterForBazelConversion() |
| 792 | ctx.SetModuleListFile(cmdlineArgs.ModuleListFile) |
Chris Parsons | 3a5c170 | 2023-06-13 01:10:20 +0000 | [diff] [blame] | 793 | // Skip cloning modules during bp2build's blueprint run. Some mutators set |
| 794 | // bp2build-related module values which should be preserved during codegen. |
| 795 | ctx.SkipCloneModulesAfterMutators = true |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 796 | |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 797 | var ninjaDeps []string |
| 798 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 799 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 800 | // Run the loading and analysis pipeline to prepare the graph of regular |
| 801 | // Modules parsed from Android.bp files, and the BazelTargetModules mapped |
| 802 | // from the regular Modules. |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 803 | ctx.EventHandler.Do("bootstrap", func() { |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 804 | blueprintArgs := cmdlineArgs |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 805 | bootstrapDeps, err := bootstrap.RunBlueprint(blueprintArgs.Args, |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 806 | bootstrap.StopBeforePrepareBuildActions, ctx.Context, ctx.Config()) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 807 | maybeQuit(err, "") |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 808 | ninjaDeps = append(ninjaDeps, bootstrapDeps...) |
| 809 | }) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 810 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 811 | globListFiles := writeBuildGlobsNinjaFile(ctx) |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 812 | ninjaDeps = append(ninjaDeps, globListFiles...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 813 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 814 | // Run the code-generation phase to convert BazelTargetModules to BUILD files |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 815 | // and print conversion codegenMetrics to the user. |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 816 | codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.Bp2Build, topDir) |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 817 | ctx.EventHandler.Do("codegen", func() { |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 818 | codegenMetrics = bp2build.Codegen(codegenContext) |
| 819 | }) |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 820 | |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 821 | ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 822 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 823 | writeDepFile(cmdlineArgs.Bp2buildMarker, ctx.EventHandler, ninjaDeps) |
| 824 | touch(shared.JoinPath(topDir, cmdlineArgs.Bp2buildMarker)) |
Usta Shrestha | 5c6b948 | 2022-05-26 12:22:17 -0400 | [diff] [blame] | 825 | }) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 826 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 827 | // Only report metrics when in bp2build mode. The metrics aren't relevant |
| 828 | // for queryview, since that's a total repo-wide conversion and there's a |
| 829 | // 1:1 mapping for each module. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 830 | if ctx.Config().IsEnvTrue("BP2BUILD_VERBOSE") { |
Usta Shrestha | a117c58 | 2022-10-04 12:13:25 -0400 | [diff] [blame] | 831 | codegenMetrics.Print() |
Sasha Smundak | 0fd93e0 | 2022-05-19 19:34:31 -0700 | [diff] [blame] | 832 | } |
Paul Duffin | b4e8d91 | 2022-11-04 13:35:50 +0000 | [diff] [blame] | 833 | writeBp2BuildMetrics(codegenMetrics, ctx.EventHandler, metricsDir) |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 834 | return cmdlineArgs.Bp2buildMarker |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 835 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 836 | |
| 837 | // Write Bp2Build metrics into $LOG_DIR |
Paul Duffin | f490656 | 2022-11-05 14:21:16 +0000 | [diff] [blame] | 838 | func writeBp2BuildMetrics(codegenMetrics *bp2build.CodegenMetrics, eventHandler *metrics.EventHandler, metricsDir string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 839 | for _, event := range eventHandler.CompletedEvents() { |
usta | 4f5d2c1 | 2022-10-28 23:32:01 -0400 | [diff] [blame] | 840 | codegenMetrics.AddEvent(&bp2build_metrics_proto.Event{ |
| 841 | Name: event.Id, |
| 842 | StartTime: uint64(event.Start.UnixNano()), |
| 843 | RealTime: event.RuntimeNanoseconds(), |
| 844 | }) |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 845 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 947fdbf | 2021-11-10 09:55:20 -0500 | [diff] [blame] | 846 | if len(metricsDir) < 1 { |
| 847 | fmt.Fprintf(os.Stderr, "\nMissing required env var for generating bp2build metrics: LOG_DIR\n") |
| 848 | os.Exit(1) |
| 849 | } |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 850 | 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] | 851 | } |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 852 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 853 | func readFileLines(path string) ([]string, error) { |
| 854 | data, err := os.ReadFile(path) |
| 855 | if err == nil { |
| 856 | return strings.Split(strings.TrimSpace(string(data)), "\n"), nil |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 857 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 858 | return nil, err |
| 859 | |
| 860 | } |
| 861 | func maybeQuit(err error, format string, args ...interface{}) { |
| 862 | if err == nil { |
| 863 | return |
| 864 | } |
| 865 | if format != "" { |
| 866 | fmt.Fprintln(os.Stderr, fmt.Sprintf(format, args...)+": "+err.Error()) |
| 867 | } else { |
| 868 | fmt.Fprintln(os.Stderr, err) |
| 869 | } |
| 870 | os.Exit(1) |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 871 | } |