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 | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 26 | "android/soong/shared" |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 27 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 28 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 29 | "android/soong/android" |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 30 | "android/soong/bp2build" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 31 | ) |
| 32 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 33 | var ( |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 34 | topDir string |
| 35 | outDir string |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 36 | docFile string |
| 37 | bazelQueryViewDir string |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 38 | delveListen string |
| 39 | delvePath string |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 40 | ) |
| 41 | |
| 42 | func init() { |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 43 | flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree") |
| 44 | flag.StringVar(&outDir, "out", "", "Soong output directory (usually $TOP/out/soong)") |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 45 | flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging") |
| 46 | flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 47 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
Lukacs T. Berki | 47a9d0c | 2021-03-08 16:34:09 +0100 | [diff] [blame] | 48 | flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 51 | func newNameResolver(config android.Config) *android.NameResolver { |
| 52 | namespacePathsToExport := make(map[string]bool) |
| 53 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 54 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 55 | namespacePathsToExport[namespaceName] = true |
| 56 | } |
| 57 | |
| 58 | namespacePathsToExport["."] = true // always export the root namespace |
| 59 | |
| 60 | exportFilter := func(namespace *android.Namespace) bool { |
| 61 | return namespacePathsToExport[namespace.Path] |
| 62 | } |
| 63 | |
| 64 | return android.NewNameResolver(exportFilter) |
| 65 | } |
| 66 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 67 | func newContext(configuration android.Config, prepareBuildActions bool) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 68 | ctx := android.NewContext(configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 69 | ctx.Register() |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 70 | if !prepareBuildActions { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 71 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 72 | } |
| 73 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 74 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 75 | return ctx |
| 76 | } |
| 77 | |
| 78 | func newConfig(srcDir string) android.Config { |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 79 | configuration, err := android.NewConfig(srcDir, bootstrap.CmdlineBuildDir(), bootstrap.CmdlineModuleListFile()) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 80 | if err != nil { |
| 81 | fmt.Fprintf(os.Stderr, "%s", err) |
| 82 | os.Exit(1) |
| 83 | } |
| 84 | return configuration |
| 85 | } |
| 86 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 87 | // Bazel-enabled mode. Soong runs in two passes. |
| 88 | // First pass: Analyze the build tree, but only store all bazel commands |
| 89 | // needed to correctly evaluate the tree in the second pass. |
| 90 | // TODO(cparsons): Don't output any ninja file, as the second pass will overwrite |
| 91 | // the incorrect results from the first pass, and file I/O is expensive. |
| 92 | func runMixedModeBuild(configuration android.Config, firstCtx *android.Context, extraNinjaDeps []string) { |
| 93 | configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja) |
| 94 | bootstrap.Main(firstCtx.Context, configuration, false, extraNinjaDeps...) |
| 95 | // Invoke bazel commands and save results for second pass. |
| 96 | if err := configuration.BazelContext.InvokeBazel(); err != nil { |
| 97 | fmt.Fprintf(os.Stderr, "%s", err) |
| 98 | os.Exit(1) |
| 99 | } |
| 100 | // Second pass: Full analysis, using the bazel command results. Output ninja file. |
| 101 | secondPassConfig, err := android.ConfigForAdditionalRun(configuration) |
| 102 | if err != nil { |
| 103 | fmt.Fprintf(os.Stderr, "%s", err) |
| 104 | os.Exit(1) |
| 105 | } |
| 106 | secondCtx := newContext(secondPassConfig, true) |
| 107 | bootstrap.Main(secondCtx.Context, secondPassConfig, false, extraNinjaDeps...) |
| 108 | } |
| 109 | |
| 110 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
| 111 | func runQueryView(configuration android.Config, ctx *android.Context) { |
| 112 | codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView) |
| 113 | absoluteQueryViewDir := shared.JoinPath(topDir, bazelQueryViewDir) |
| 114 | if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil { |
| 115 | fmt.Fprintf(os.Stderr, "%s", err) |
| 116 | os.Exit(1) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func runSoongDocs(configuration android.Config, extraNinjaDeps []string) { |
| 121 | ctx := newContext(configuration, false) |
| 122 | bootstrap.Main(ctx.Context, configuration, false, extraNinjaDeps...) |
| 123 | if err := writeDocs(ctx, configuration, docFile); err != nil { |
| 124 | fmt.Fprintf(os.Stderr, "%s", err) |
| 125 | os.Exit(1) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | func writeMetrics(configuration android.Config) { |
| 130 | metricsFile := filepath.Join(bootstrap.CmdlineBuildDir(), "soong_build_metrics.pb") |
| 131 | err := android.WriteMetrics(configuration, metricsFile) |
| 132 | if err != nil { |
| 133 | fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err) |
| 134 | os.Exit(1) |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func doChosenActivity(configuration android.Config, extraNinjaDeps []string) { |
| 139 | bazelConversionRequested := configuration.IsEnvTrue("GENERATE_BAZEL_FILES") |
| 140 | mixedModeBuild := configuration.BazelContext.BazelEnabled() |
| 141 | generateQueryView := bazelQueryViewDir != "" |
| 142 | |
| 143 | if bazelConversionRequested { |
| 144 | // Run the alternate pipeline of bp2build mutators and singleton to convert |
| 145 | // Blueprint to BUILD files before everything else. |
| 146 | runBp2Build(configuration, extraNinjaDeps) |
| 147 | return |
| 148 | } |
| 149 | |
| 150 | ctx := newContext(configuration, !generateQueryView) |
| 151 | if mixedModeBuild { |
| 152 | runMixedModeBuild(configuration, ctx, extraNinjaDeps) |
| 153 | } else { |
| 154 | bootstrap.Main(ctx.Context, configuration, false, extraNinjaDeps...) |
| 155 | } |
| 156 | |
| 157 | // Convert the Soong module graph into Bazel BUILD files. |
| 158 | if generateQueryView { |
| 159 | runQueryView(configuration, ctx) |
| 160 | return |
| 161 | } |
| 162 | writeMetrics(configuration) |
| 163 | } |
| 164 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 165 | func main() { |
| 166 | flag.Parse() |
| 167 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 168 | shared.ReexecWithDelveMaybe(delveListen, delvePath) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 169 | android.InitSandbox(topDir) |
| 170 | android.InitEnvironment(shared.JoinPath(topDir, outDir, "soong.environment.available")) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 171 | |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 172 | usedVariablesFile := shared.JoinPath(outDir, "soong.environment.used") |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 173 | // The top-level Blueprints file is passed as the first argument. |
| 174 | srcDir := filepath.Dir(flag.Arg(0)) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 175 | configuration := newConfig(srcDir) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 176 | extraNinjaDeps := []string{ |
| 177 | configuration.ProductVariablesFileName, |
| 178 | shared.JoinPath(outDir, "soong.environment.used"), |
| 179 | } |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 180 | |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 181 | if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 182 | configuration.SetAllowMissingDependencies() |
| 183 | } |
| 184 | |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 185 | // These two are here so that we restart a non-debugged soong_build when the |
| 186 | // user sets SOONG_DELVE the first time. |
| 187 | configuration.Getenv("SOONG_DELVE") |
| 188 | configuration.Getenv("SOONG_DELVE_PATH") |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 189 | if shared.IsDebugging() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 190 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 191 | // enabled even if it completed successfully. |
| 192 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve")) |
| 193 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 194 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 195 | if docFile != "" { |
| 196 | // We don't write an used variables file when generating documentation |
| 197 | // because that is done from within the actual builds as a Ninja action and |
| 198 | // thus it would overwrite the actual used variables file so this is |
| 199 | // special-cased. |
| 200 | runSoongDocs(configuration, extraNinjaDeps) |
| 201 | return |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 202 | } |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 203 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 204 | doChosenActivity(configuration, extraNinjaDeps) |
| 205 | writeUsedVariablesFile(shared.JoinPath(topDir, usedVariablesFile), configuration) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | func writeUsedVariablesFile(path string, configuration android.Config) { |
| 209 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
| 210 | if err != nil { |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 211 | fmt.Fprintf(os.Stderr, "error writing used variables file %s: %s\n", path, err) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 212 | os.Exit(1) |
| 213 | } |
| 214 | |
| 215 | err = ioutil.WriteFile(path, data, 0666) |
| 216 | if err != nil { |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 217 | fmt.Fprintf(os.Stderr, "error writing used variables file %s: %s\n", path, err) |
| 218 | os.Exit(1) |
| 219 | } |
| 220 | |
| 221 | // Touch the output Ninja file so that it's not older than the file we just |
| 222 | // wrote. We can't write the environment file earlier because one an access |
| 223 | // new environment variables while writing it. |
| 224 | outputNinjaFile := shared.JoinPath(topDir, bootstrap.CmdlineOutFile()) |
| 225 | currentTime := time.Now().Local() |
| 226 | err = os.Chtimes(outputNinjaFile, currentTime, currentTime) |
| 227 | if err != nil { |
| 228 | fmt.Fprintf(os.Stderr, "error touching output file %s: %s\n", outputNinjaFile, err) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 229 | os.Exit(1) |
| 230 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 231 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 232 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 233 | // Run Soong in the bp2build mode. This creates a standalone context that registers |
| 234 | // an alternate pipeline of mutators and singletons specifically for generating |
| 235 | // Bazel BUILD files instead of Ninja files. |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 236 | func runBp2Build(configuration android.Config, extraNinjaDeps []string) { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 237 | // Register an alternate set of singletons and mutators for bazel |
| 238 | // conversion for Bazel conversion. |
| 239 | bp2buildCtx := android.NewContext(configuration) |
| 240 | bp2buildCtx.RegisterForBazelConversion() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 241 | |
| 242 | // No need to generate Ninja build rules/statements from Modules and Singletons. |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 243 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 244 | bp2buildCtx.SetNameInterface(newNameResolver(configuration)) |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 245 | |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 246 | // The bp2build process is a purely functional process that only depends on |
| 247 | // Android.bp files. It must not depend on the values of per-build product |
| 248 | // configurations or variables, since those will generate different BUILD |
| 249 | // files based on how the user has configured their tree. |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 250 | bp2buildCtx.SetModuleListFile(bootstrap.CmdlineModuleListFile()) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 251 | modulePaths, err := bp2buildCtx.ListModulePaths(configuration.SrcDir()) |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 252 | if err != nil { |
| 253 | panic(err) |
| 254 | } |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 255 | |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 256 | extraNinjaDeps = append(extraNinjaDeps, modulePaths...) |
| 257 | |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 258 | // Run the loading and analysis pipeline to prepare the graph of regular |
| 259 | // Modules parsed from Android.bp files, and the BazelTargetModules mapped |
| 260 | // from the regular Modules. |
Lukacs T. Berki | d7ce840 | 2021-03-17 14:03:51 +0100 | [diff] [blame] | 261 | bootstrap.Main(bp2buildCtx.Context, configuration, false, extraNinjaDeps...) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 262 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 263 | // Run the code-generation phase to convert BazelTargetModules to BUILD files |
| 264 | // and print conversion metrics to the user. |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 265 | codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame] | 266 | metrics := bp2build.Codegen(codegenContext) |
| 267 | |
| 268 | // Only report metrics when in bp2build mode. The metrics aren't relevant |
| 269 | // for queryview, since that's a total repo-wide conversion and there's a |
| 270 | // 1:1 mapping for each module. |
| 271 | metrics.Print() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 272 | |
Liz Kammer | ba3ea16 | 2021-02-17 13:22:03 -0500 | [diff] [blame] | 273 | extraNinjaDeps = append(extraNinjaDeps, codegenContext.AdditionalNinjaDeps()...) |
| 274 | extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ") |
| 275 | |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 276 | // Workarounds to support running bp2build in a clean AOSP checkout with no |
| 277 | // prior builds, and exiting early as soon as the BUILD files get generated, |
| 278 | // therefore not creating build.ninja files that soong_ui and callers of |
| 279 | // soong_build expects. |
| 280 | // |
| 281 | // These files are: build.ninja and build.ninja.d. Since Kati hasn't been |
| 282 | // ran as well, and `nothing` is defined in a .mk file, there isn't a ninja |
| 283 | // target called `nothing`, so we manually create it here. |
| 284 | // |
| 285 | // Even though outFile (build.ninja) and depFile (build.ninja.d) are values |
| 286 | // passed into bootstrap.Main, they are package-private fields in bootstrap. |
| 287 | // Short of modifying Blueprint to add an exported getter, inlining them |
| 288 | // here is the next-best practical option. |
| 289 | ninjaFileName := "build.ninja" |
| 290 | ninjaFile := android.PathForOutput(codegenContext, ninjaFileName) |
| 291 | ninjaFileD := android.PathForOutput(codegenContext, ninjaFileName+".d") |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 292 | // A workaround to create the 'nothing' ninja target so `m nothing` works, |
| 293 | // since bp2build runs without Kati, and the 'nothing' target is declared in |
| 294 | // a Makefile. |
| 295 | android.WriteFileToOutputDir(ninjaFile, []byte("build nothing: phony\n phony_output = true\n"), 0666) |
| 296 | android.WriteFileToOutputDir( |
| 297 | ninjaFileD, |
| 298 | []byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFileName, extraNinjaDepsString)), |
| 299 | 0666) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 300 | } |