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