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