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 ( |
| 18 | "flag" |
| 19 | "fmt" |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 20 | "io/fs" |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 21 | "io/ioutil" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 22 | "os" |
| 23 | "path/filepath" |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 24 | "strings" |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 25 | "time" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 26 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 27 | "android/soong/bp2build" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 28 | "android/soong/shared" |
Colin Cross | cb33a00 | 2021-04-12 22:25:17 -0700 | [diff] [blame] | 29 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 30 | "github.com/google/blueprint/bootstrap" |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 31 | "github.com/google/blueprint/deptools" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 32 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 33 | "android/soong/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 34 | ) |
| 35 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 36 | var ( |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 37 | topDir string |
| 38 | outDir string |
| 39 | availableEnvFile string |
| 40 | usedEnvFile string |
| 41 | |
| 42 | delveListen string |
| 43 | delvePath string |
| 44 | |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 45 | docFile string |
| 46 | bazelQueryViewDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 47 | bp2buildMarker string |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 48 | ) |
| 49 | |
| 50 | func init() { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 51 | // Flags that make sense in every mode |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 52 | flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree") |
| 53 | flag.StringVar(&outDir, "out", "", "Soong output directory (usually $TOP/out/soong)") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 54 | flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables") |
| 55 | flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables") |
| 56 | |
| 57 | // Debug flags |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 58 | flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging") |
| 59 | flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 60 | |
| 61 | // Flags representing various modes soong_build can run in |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 62 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
Lukacs T. Berki | 47a9d0c | 2021-03-08 16:34:09 +0100 | [diff] [blame] | 63 | flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 64 | flag.StringVar(&bp2buildMarker, "bp2build_marker", "", "If set, run bp2build, touch the specified marker file then exit") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 67 | func newNameResolver(config android.Config) *android.NameResolver { |
| 68 | namespacePathsToExport := make(map[string]bool) |
| 69 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 70 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 71 | namespacePathsToExport[namespaceName] = true |
| 72 | } |
| 73 | |
| 74 | namespacePathsToExport["."] = true // always export the root namespace |
| 75 | |
| 76 | exportFilter := func(namespace *android.Namespace) bool { |
| 77 | return namespacePathsToExport[namespace.Path] |
| 78 | } |
| 79 | |
| 80 | return android.NewNameResolver(exportFilter) |
| 81 | } |
| 82 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 83 | func newContext(configuration android.Config, prepareBuildActions bool) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 84 | ctx := android.NewContext(configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 85 | ctx.Register() |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 86 | if !prepareBuildActions { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 87 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 88 | } |
| 89 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 90 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 91 | return ctx |
| 92 | } |
| 93 | |
Lukacs T. Berki | 53b2f36 | 2021-04-12 14:04:24 +0200 | [diff] [blame] | 94 | func newConfig(srcDir, outDir string, availableEnv map[string]string) android.Config { |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 95 | configuration, err := android.NewConfig(srcDir, outDir, bootstrap.CmdlineArgs.ModuleListFile, availableEnv) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 96 | if err != nil { |
| 97 | fmt.Fprintf(os.Stderr, "%s", err) |
| 98 | os.Exit(1) |
| 99 | } |
| 100 | return configuration |
| 101 | } |
| 102 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 103 | // Bazel-enabled mode. Soong runs in two passes. |
| 104 | // First pass: Analyze the build tree, but only store all bazel commands |
| 105 | // needed to correctly evaluate the tree in the second pass. |
| 106 | // TODO(cparsons): Don't output any ninja file, as the second pass will overwrite |
| 107 | // the incorrect results from the first pass, and file I/O is expensive. |
| 108 | func runMixedModeBuild(configuration android.Config, firstCtx *android.Context, extraNinjaDeps []string) { |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 109 | var firstArgs, secondArgs bootstrap.Args |
| 110 | |
| 111 | firstArgs = bootstrap.CmdlineArgs |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 112 | configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja) |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 113 | bootstrap.RunBlueprint(firstArgs, firstCtx.Context, configuration) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 114 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 115 | // Invoke bazel commands and save results for second pass. |
| 116 | if err := configuration.BazelContext.InvokeBazel(); err != nil { |
| 117 | fmt.Fprintf(os.Stderr, "%s", err) |
| 118 | os.Exit(1) |
| 119 | } |
| 120 | // Second pass: Full analysis, using the bazel command results. Output ninja file. |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 121 | secondConfig, err := android.ConfigForAdditionalRun(configuration) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 122 | if err != nil { |
| 123 | fmt.Fprintf(os.Stderr, "%s", err) |
| 124 | os.Exit(1) |
| 125 | } |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 126 | secondCtx := newContext(secondConfig, true) |
| 127 | secondArgs = bootstrap.CmdlineArgs |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 128 | ninjaDeps := bootstrap.RunBlueprint(secondArgs, secondCtx.Context, secondConfig) |
| 129 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 130 | err = deptools.WriteDepFile(shared.JoinPath(topDir, secondArgs.DepFile), secondArgs.OutFile, ninjaDeps) |
| 131 | if err != nil { |
| 132 | fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", secondArgs.DepFile, err) |
| 133 | os.Exit(1) |
| 134 | } |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
| 138 | func runQueryView(configuration android.Config, ctx *android.Context) { |
| 139 | codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView) |
| 140 | absoluteQueryViewDir := shared.JoinPath(topDir, bazelQueryViewDir) |
| 141 | if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil { |
| 142 | fmt.Fprintf(os.Stderr, "%s", err) |
| 143 | os.Exit(1) |
| 144 | } |
| 145 | } |
| 146 | |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 147 | func runSoongDocs(configuration android.Config) { |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 148 | ctx := newContext(configuration, false) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 149 | soongDocsArgs := bootstrap.CmdlineArgs |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 150 | bootstrap.RunBlueprint(soongDocsArgs, ctx.Context, configuration) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 151 | if err := writeDocs(ctx, configuration, docFile); err != nil { |
| 152 | fmt.Fprintf(os.Stderr, "%s", err) |
| 153 | os.Exit(1) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | func writeMetrics(configuration android.Config) { |
Lukacs T. Berki | 745380c | 2021-04-12 12:07:44 +0200 | [diff] [blame] | 158 | metricsFile := filepath.Join(configuration.BuildDir(), "soong_build_metrics.pb") |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 159 | err := android.WriteMetrics(configuration, metricsFile) |
| 160 | if err != nil { |
| 161 | fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err) |
| 162 | os.Exit(1) |
| 163 | } |
| 164 | } |
| 165 | |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 166 | func writeJsonModuleGraph(configuration android.Config, ctx *android.Context, path string, extraNinjaDeps []string) { |
| 167 | f, err := os.Create(path) |
| 168 | if err != nil { |
| 169 | fmt.Fprintf(os.Stderr, "%s", err) |
| 170 | os.Exit(1) |
| 171 | } |
| 172 | |
| 173 | defer f.Close() |
| 174 | ctx.Context.PrintJSONGraph(f) |
| 175 | writeFakeNinjaFile(extraNinjaDeps, configuration.BuildDir()) |
| 176 | } |
| 177 | |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame^] | 178 | // doChosenActivity runs Soong for a specific activity, like bp2build, queryview |
| 179 | // or the actual Soong build for the build.ninja file. Returns the top level |
| 180 | // output file of the specific activity. |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 181 | func doChosenActivity(configuration android.Config, extraNinjaDeps []string) string { |
Chris Parsons | ec1a3dc | 2021-04-20 15:32:07 -0400 | [diff] [blame] | 182 | bazelConversionRequested := bp2buildMarker != "" |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 183 | mixedModeBuild := configuration.BazelContext.BazelEnabled() |
| 184 | generateQueryView := bazelQueryViewDir != "" |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 185 | jsonModuleFile := configuration.Getenv("SOONG_DUMP_JSON_MODULE_GRAPH") |
| 186 | |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 187 | blueprintArgs := bootstrap.CmdlineArgs |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 188 | prepareBuildActions := !generateQueryView && jsonModuleFile == "" |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 189 | if bazelConversionRequested { |
| 190 | // Run the alternate pipeline of bp2build mutators and singleton to convert |
| 191 | // Blueprint to BUILD files before everything else. |
| 192 | runBp2Build(configuration, extraNinjaDeps) |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame^] | 193 | return bp2buildMarker |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 194 | } |
| 195 | |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 196 | ctx := newContext(configuration, prepareBuildActions) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 197 | if mixedModeBuild { |
| 198 | runMixedModeBuild(configuration, ctx, extraNinjaDeps) |
| 199 | } else { |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 200 | ninjaDeps := bootstrap.RunBlueprint(blueprintArgs, ctx.Context, configuration) |
| 201 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 202 | err := deptools.WriteDepFile(shared.JoinPath(topDir, blueprintArgs.DepFile), blueprintArgs.OutFile, ninjaDeps) |
| 203 | if err != nil { |
| 204 | fmt.Fprintf(os.Stderr, "Error writing depfile '%s': %s\n", blueprintArgs.DepFile, err) |
| 205 | os.Exit(1) |
| 206 | } |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Convert the Soong module graph into Bazel BUILD files. |
| 210 | if generateQueryView { |
| 211 | runQueryView(configuration, ctx) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 212 | return bootstrap.CmdlineArgs.OutFile // TODO: This is a lie |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 213 | } |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 214 | |
| 215 | if jsonModuleFile != "" { |
| 216 | writeJsonModuleGraph(configuration, ctx, jsonModuleFile, extraNinjaDeps) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 217 | return bootstrap.CmdlineArgs.OutFile // TODO: This is a lie |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 218 | } |
| 219 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 220 | writeMetrics(configuration) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 221 | return bootstrap.CmdlineArgs.OutFile |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | // soong_ui dumps the available environment variables to |
| 225 | // soong.environment.available . Then soong_build itself is run with an empty |
| 226 | // environment so that the only way environment variables can be accessed is |
| 227 | // using Config, which tracks access to them. |
| 228 | |
| 229 | // At the end of the build, a file called soong.environment.used is written |
| 230 | // containing the current value of all used environment variables. The next |
| 231 | // time soong_ui is run, it checks whether any environment variables that was |
| 232 | // used had changed and if so, it deletes soong.environment.used to cause a |
| 233 | // rebuild. |
| 234 | // |
| 235 | // The dependency of build.ninja on soong.environment.used is declared in |
| 236 | // build.ninja.d |
| 237 | func parseAvailableEnv() map[string]string { |
| 238 | if availableEnvFile == "" { |
| 239 | fmt.Fprintf(os.Stderr, "--available_env not set\n") |
| 240 | os.Exit(1) |
| 241 | } |
| 242 | |
| 243 | result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile)) |
| 244 | if err != nil { |
| 245 | fmt.Fprintf(os.Stderr, "error reading available environment file '%s': %s\n", availableEnvFile, err) |
| 246 | os.Exit(1) |
| 247 | } |
| 248 | |
| 249 | return result |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 250 | } |
| 251 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 252 | func main() { |
| 253 | flag.Parse() |
| 254 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 255 | shared.ReexecWithDelveMaybe(delveListen, delvePath) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 256 | android.InitSandbox(topDir) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 257 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 258 | availableEnv := parseAvailableEnv() |
Lukacs T. Berki | 53b2f36 | 2021-04-12 14:04:24 +0200 | [diff] [blame] | 259 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 260 | // The top-level Blueprints file is passed as the first argument. |
| 261 | srcDir := filepath.Dir(flag.Arg(0)) |
Lukacs T. Berki | 53b2f36 | 2021-04-12 14:04:24 +0200 | [diff] [blame] | 262 | configuration := newConfig(srcDir, outDir, availableEnv) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 263 | extraNinjaDeps := []string{ |
| 264 | configuration.ProductVariablesFileName, |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 265 | usedEnvFile, |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 266 | } |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 267 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 268 | if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 269 | configuration.SetAllowMissingDependencies() |
| 270 | } |
| 271 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 272 | if shared.IsDebugging() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 273 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 274 | // enabled even if it completed successfully. |
| 275 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve")) |
| 276 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 277 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 278 | if docFile != "" { |
| 279 | // We don't write an used variables file when generating documentation |
| 280 | // because that is done from within the actual builds as a Ninja action and |
| 281 | // thus it would overwrite the actual used variables file so this is |
| 282 | // special-cased. |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 283 | // TODO: Fix this by not passing --used_env to the soong_docs invocation |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 284 | runSoongDocs(configuration) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 285 | return |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 286 | } |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 287 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 288 | finalOutputFile := doChosenActivity(configuration, extraNinjaDeps) |
| 289 | writeUsedEnvironmentFile(configuration, finalOutputFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 290 | } |
| 291 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 292 | func writeUsedEnvironmentFile(configuration android.Config, finalOutputFile string) { |
| 293 | if usedEnvFile == "" { |
| 294 | return |
| 295 | } |
| 296 | |
| 297 | path := shared.JoinPath(topDir, usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 298 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
| 299 | if err != nil { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 300 | 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] | 301 | os.Exit(1) |
| 302 | } |
| 303 | |
| 304 | err = ioutil.WriteFile(path, data, 0666) |
| 305 | if err != nil { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 306 | 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] | 307 | os.Exit(1) |
| 308 | } |
| 309 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 310 | // 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] | 311 | // wrote. We can't write the environment file earlier because one an access |
| 312 | // new environment variables while writing it. |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 313 | touch(shared.JoinPath(topDir, finalOutputFile)) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 314 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 315 | |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 316 | // Workarounds to support running bp2build in a clean AOSP checkout with no |
| 317 | // prior builds, and exiting early as soon as the BUILD files get generated, |
| 318 | // therefore not creating build.ninja files that soong_ui and callers of |
| 319 | // soong_build expects. |
| 320 | // |
| 321 | // These files are: build.ninja and build.ninja.d. Since Kati hasn't been |
| 322 | // ran as well, and `nothing` is defined in a .mk file, there isn't a ninja |
| 323 | // target called `nothing`, so we manually create it here. |
| 324 | func writeFakeNinjaFile(extraNinjaDeps []string, buildDir string) { |
| 325 | extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ") |
| 326 | |
| 327 | ninjaFileName := "build.ninja" |
| 328 | ninjaFile := shared.JoinPath(topDir, buildDir, ninjaFileName) |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame^] | 329 | ninjaFileD := shared.JoinPath(topDir, buildDir, ninjaFileName+".d") |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 330 | // A workaround to create the 'nothing' ninja target so `m nothing` works, |
| 331 | // since bp2build runs without Kati, and the 'nothing' target is declared in |
| 332 | // a Makefile. |
| 333 | ioutil.WriteFile(ninjaFile, []byte("build nothing: phony\n phony_output = true\n"), 0666) |
| 334 | ioutil.WriteFile(ninjaFileD, |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame^] | 335 | []byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFile, extraNinjaDepsString)), |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 336 | 0666) |
| 337 | } |
| 338 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 339 | func touch(path string) { |
| 340 | f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) |
| 341 | if err != nil { |
| 342 | fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err) |
| 343 | os.Exit(1) |
| 344 | } |
| 345 | |
| 346 | err = f.Close() |
| 347 | if err != nil { |
| 348 | fmt.Fprintf(os.Stderr, "Error touching '%s': %s\n", path, err) |
| 349 | os.Exit(1) |
| 350 | } |
| 351 | |
| 352 | currentTime := time.Now().Local() |
| 353 | err = os.Chtimes(path, currentTime, currentTime) |
| 354 | if err != nil { |
| 355 | fmt.Fprintf(os.Stderr, "error touching '%s': %s\n", path, err) |
| 356 | os.Exit(1) |
| 357 | } |
| 358 | } |
| 359 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 360 | // Find BUILD files in the srcDir which... |
| 361 | // |
| 362 | // - are not on the allow list (android/bazel.go#ShouldKeepExistingBuildFileForDir()) |
| 363 | // |
| 364 | // - won't be overwritten by corresponding bp2build generated files |
| 365 | // |
| 366 | // And return their paths so they can be left out of the Bazel workspace dir (i.e. ignored) |
| 367 | func getPathsToIgnoredBuildFiles(topDir string, outDir string, generatedRoot string) ([]string, error) { |
| 368 | paths := make([]string, 0) |
| 369 | |
| 370 | err := filepath.WalkDir(topDir, func(fFullPath string, fDirEntry fs.DirEntry, err error) error { |
| 371 | if err != nil { |
| 372 | // Warn about error, but continue trying to walk the directory tree |
| 373 | fmt.Fprintf(os.Stderr, "WARNING: Error accessing path '%s', err: %s\n", fFullPath, err) |
| 374 | return nil |
| 375 | } |
| 376 | if fDirEntry.IsDir() { |
| 377 | // Don't ignore entire directories |
| 378 | return nil |
| 379 | } |
| 380 | if !(fDirEntry.Name() == "BUILD" || fDirEntry.Name() == "BUILD.bazel") { |
| 381 | // Don't ignore this file - it is not a build file |
| 382 | return nil |
| 383 | } |
| 384 | f := strings.TrimPrefix(fFullPath, topDir+"/") |
| 385 | if strings.HasPrefix(f, ".repo/") { |
| 386 | // Don't check for files to ignore in the .repo dir (recursively) |
| 387 | return fs.SkipDir |
| 388 | } |
| 389 | if strings.HasPrefix(f, outDir+"/") { |
| 390 | // Don't check for files to ignore in the out dir (recursively) |
| 391 | return fs.SkipDir |
| 392 | } |
| 393 | if strings.HasPrefix(f, generatedRoot) { |
| 394 | // Don't check for files to ignore in the bp2build dir (recursively) |
| 395 | // NOTE: This is usually under outDir |
| 396 | return fs.SkipDir |
| 397 | } |
| 398 | fDir := filepath.Dir(f) |
| 399 | if android.ShouldKeepExistingBuildFileForDir(fDir) { |
| 400 | // Don't ignore this existing build file |
| 401 | return nil |
| 402 | } |
| 403 | f_bp2build := shared.JoinPath(topDir, generatedRoot, f) |
| 404 | if _, err := os.Stat(f_bp2build); err == nil { |
| 405 | // If bp2build generated an alternate BUILD file, don't exclude this workspace path |
| 406 | // BUILD file clash resolution happens later in the symlink forest creation |
| 407 | return nil |
| 408 | } |
| 409 | fmt.Fprintf(os.Stderr, "Ignoring existing BUILD file: %s\n", f) |
| 410 | paths = append(paths, f) |
| 411 | return nil |
| 412 | }) |
| 413 | |
| 414 | return paths, err |
| 415 | } |
| 416 | |
| 417 | // Returns temporary symlink forest excludes necessary for bazel build //external/... (and bazel build //frameworks/...) to work |
| 418 | func getTemporaryExcludes() []string { |
| 419 | excludes := make([]string, 0) |
| 420 | |
| 421 | // FIXME: 'autotest_lib' is a symlink back to external/autotest, and this causes an infinite symlink expansion error for Bazel |
| 422 | excludes = append(excludes, "external/autotest/venv/autotest_lib") |
| 423 | |
| 424 | // FIXME: The external/google-fruit/extras/bazel_root/third_party/fruit dir is poison |
| 425 | // It contains several symlinks back to real source dirs, and those source dirs contain BUILD files we want to ignore |
| 426 | excludes = append(excludes, "external/google-fruit/extras/bazel_root/third_party/fruit") |
| 427 | |
| 428 | // FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue |
| 429 | excludes = append(excludes, "frameworks/compile/slang") |
| 430 | |
| 431 | return excludes |
| 432 | } |
| 433 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 434 | // Run Soong in the bp2build mode. This creates a standalone context that registers |
| 435 | // an alternate pipeline of mutators and singletons specifically for generating |
| 436 | // Bazel BUILD files instead of Ninja files. |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 437 | func runBp2Build(configuration android.Config, extraNinjaDeps []string) { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 438 | // Register an alternate set of singletons and mutators for bazel |
| 439 | // conversion for Bazel conversion. |
| 440 | bp2buildCtx := android.NewContext(configuration) |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 441 | |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 442 | // Propagate "allow misssing dependencies" bit. This is normally set in |
| 443 | // newContext(), but we create bp2buildCtx without calling that method. |
| 444 | bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 445 | bp2buildCtx.SetNameInterface(newNameResolver(configuration)) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 446 | bp2buildCtx.RegisterForBazelConversion() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 447 | |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 448 | // The bp2build process is a purely functional process that only depends on |
| 449 | // Android.bp files. It must not depend on the values of per-build product |
| 450 | // configurations or variables, since those will generate different BUILD |
| 451 | // files based on how the user has configured their tree. |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 452 | bp2buildCtx.SetModuleListFile(bootstrap.CmdlineArgs.ModuleListFile) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 453 | modulePaths, err := bp2buildCtx.ListModulePaths(configuration.SrcDir()) |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 454 | if err != nil { |
| 455 | panic(err) |
| 456 | } |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 457 | |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 458 | extraNinjaDeps = append(extraNinjaDeps, modulePaths...) |
| 459 | |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 460 | // No need to generate Ninja build rules/statements from Modules and Singletons. |
| 461 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 462 | |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 463 | // Run the loading and analysis pipeline to prepare the graph of regular |
| 464 | // Modules parsed from Android.bp files, and the BazelTargetModules mapped |
| 465 | // from the regular Modules. |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 466 | blueprintArgs := bootstrap.CmdlineArgs |
Lukacs T. Berki | 6124c9b | 2021-04-15 15:06:40 +0200 | [diff] [blame] | 467 | ninjaDeps := bootstrap.RunBlueprint(blueprintArgs, bp2buildCtx.Context, configuration) |
| 468 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 469 | |
Colin Cross | cb33a00 | 2021-04-12 22:25:17 -0700 | [diff] [blame] | 470 | ninjaDeps = append(ninjaDeps, bootstrap.GlobFileListFiles(configuration)...) |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 471 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 472 | // Run the code-generation phase to convert BazelTargetModules to BUILD files |
| 473 | // and print conversion metrics to the user. |
| 474 | codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build) |
| 475 | metrics := bp2build.Codegen(codegenContext) |
| 476 | |
| 477 | generatedRoot := shared.JoinPath(configuration.BuildDir(), "bp2build") |
| 478 | workspaceRoot := shared.JoinPath(configuration.BuildDir(), "workspace") |
| 479 | |
| 480 | excludes := []string{ |
| 481 | "bazel-bin", |
| 482 | "bazel-genfiles", |
| 483 | "bazel-out", |
| 484 | "bazel-testlogs", |
| 485 | "bazel-" + filepath.Base(topDir), |
| 486 | } |
| 487 | |
| 488 | if bootstrap.CmdlineArgs.NinjaBuildDir[0] != '/' { |
| 489 | excludes = append(excludes, bootstrap.CmdlineArgs.NinjaBuildDir) |
| 490 | } |
| 491 | |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 492 | // FIXME: Don't hardcode this here |
| 493 | topLevelOutDir := "out" |
| 494 | |
| 495 | pathsToIgnoredBuildFiles, err := getPathsToIgnoredBuildFiles(topDir, topLevelOutDir, generatedRoot) |
| 496 | if err != nil { |
| 497 | fmt.Fprintf(os.Stderr, "Error walking SrcDir: '%s': %s\n", configuration.SrcDir(), err) |
| 498 | os.Exit(1) |
| 499 | } |
| 500 | excludes = append(excludes, pathsToIgnoredBuildFiles...) |
| 501 | |
| 502 | excludes = append(excludes, getTemporaryExcludes()...) |
| 503 | |
Lukacs T. Berki | b353cca | 2021-04-16 13:47:36 +0200 | [diff] [blame] | 504 | symlinkForestDeps := bp2build.PlantSymlinkForest( |
| 505 | topDir, workspaceRoot, generatedRoot, configuration.SrcDir(), excludes) |
| 506 | |
| 507 | // Only report metrics when in bp2build mode. The metrics aren't relevant |
| 508 | // for queryview, since that's a total repo-wide conversion and there's a |
| 509 | // 1:1 mapping for each module. |
| 510 | metrics.Print() |
| 511 | |
| 512 | ninjaDeps = append(ninjaDeps, codegenContext.AdditionalNinjaDeps()...) |
| 513 | ninjaDeps = append(ninjaDeps, symlinkForestDeps...) |
| 514 | |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 515 | depFile := bp2buildMarker + ".d" |
| 516 | err = deptools.WriteDepFile(shared.JoinPath(topDir, depFile), bp2buildMarker, ninjaDeps) |
| 517 | if err != nil { |
| 518 | fmt.Fprintf(os.Stderr, "Cannot write depfile '%s': %s\n", depFile, err) |
| 519 | os.Exit(1) |
| 520 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 521 | |
Jingwen Chen | 4fabaf5 | 2021-05-25 02:40:29 +0000 | [diff] [blame^] | 522 | // Create an empty bp2build marker file. |
| 523 | touch(shared.JoinPath(topDir, bp2buildMarker)) |
| 524 | |
| 525 | // bp2build *always* writes a fake Ninja file containing just the nothing |
| 526 | // phony target if it ever re-runs. This allows bp2build to exit early with |
| 527 | // GENERATE_BAZEL_FILES=1 m nothing. |
| 528 | // |
| 529 | // If bp2build is invoked as part of an integrated mixed build, the fake |
| 530 | // build.ninja file will be rewritten later into the real file anyway. |
| 531 | writeFakeNinjaFile(extraNinjaDeps, codegenContext.Config().BuildDir()) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 532 | } |