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