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" |
| 20 | "os" |
| 21 | "path/filepath" |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 22 | "strings" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 23 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 24 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 25 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 26 | "android/soong/android" |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 27 | "android/soong/bp2build" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 30 | var ( |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 31 | docFile string |
| 32 | bazelQueryViewDir string |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 33 | ) |
| 34 | |
| 35 | func init() { |
| 36 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 37 | flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 38 | } |
| 39 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 40 | func newNameResolver(config android.Config) *android.NameResolver { |
| 41 | namespacePathsToExport := make(map[string]bool) |
| 42 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 43 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 44 | namespacePathsToExport[namespaceName] = true |
| 45 | } |
| 46 | |
| 47 | namespacePathsToExport["."] = true // always export the root namespace |
| 48 | |
| 49 | exportFilter := func(namespace *android.Namespace) bool { |
| 50 | return namespacePathsToExport[namespace.Path] |
| 51 | } |
| 52 | |
| 53 | return android.NewNameResolver(exportFilter) |
| 54 | } |
| 55 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 56 | // bazelConversionRequested checks that the user is intending to convert |
| 57 | // Blueprint to Bazel BUILD files. |
| 58 | func bazelConversionRequested(configuration android.Config) bool { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 59 | return configuration.IsEnvTrue("GENERATE_BAZEL_FILES") |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 60 | } |
| 61 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 62 | func newContext(configuration android.Config) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 63 | ctx := android.NewContext(configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 64 | ctx.Register() |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 65 | if !shouldPrepareBuildActions(configuration) { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 66 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 67 | } |
| 68 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 69 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 70 | return ctx |
| 71 | } |
| 72 | |
| 73 | func newConfig(srcDir string) android.Config { |
| 74 | configuration, err := android.NewConfig(srcDir, bootstrap.BuildDir, bootstrap.ModuleListFile) |
| 75 | if err != nil { |
| 76 | fmt.Fprintf(os.Stderr, "%s", err) |
| 77 | os.Exit(1) |
| 78 | } |
| 79 | return configuration |
| 80 | } |
| 81 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 82 | func main() { |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 83 | android.ReexecWithDelveMaybe() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 84 | flag.Parse() |
| 85 | |
| 86 | // The top-level Blueprints file is passed as the first argument. |
| 87 | srcDir := filepath.Dir(flag.Arg(0)) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 88 | var ctx *android.Context |
| 89 | configuration := newConfig(srcDir) |
Jingwen Chen | c4d91bc | 2020-11-24 22:59:26 -0500 | [diff] [blame] | 90 | extraNinjaDeps := []string{configuration.ProductVariablesFileName} |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 91 | |
| 92 | // Read the SOONG_DELVE again through configuration so that there is a dependency on the environment variable |
| 93 | // and soong_build will rerun when it is set for the first time. |
| 94 | if listen := configuration.Getenv("SOONG_DELVE"); listen != "" { |
| 95 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 96 | // enabled even if it completed successfully. |
| 97 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve")) |
| 98 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 99 | |
| 100 | if bazelConversionRequested(configuration) { |
| 101 | // Run the alternate pipeline of bp2build mutators and singleton to convert Blueprint to BUILD files |
| 102 | // before everything else. |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 103 | runBp2Build(srcDir, configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 104 | // Short-circuit and return. |
| 105 | return |
| 106 | } |
| 107 | |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 108 | if configuration.BazelContext.BazelEnabled() { |
| 109 | // Bazel-enabled mode. Soong runs in two passes. |
| 110 | // First pass: Analyze the build tree, but only store all bazel commands |
| 111 | // needed to correctly evaluate the tree in the second pass. |
| 112 | // TODO(cparsons): Don't output any ninja file, as the second pass will overwrite |
| 113 | // the incorrect results from the first pass, and file I/O is expensive. |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 114 | firstCtx := newContext(configuration) |
Chris Parsons | 3060ec7 | 2020-11-09 20:08:36 -0500 | [diff] [blame] | 115 | configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 116 | bootstrap.Main(firstCtx.Context, configuration, extraNinjaDeps...) |
| 117 | // Invoke bazel commands and save results for second pass. |
| 118 | if err := configuration.BazelContext.InvokeBazel(); err != nil { |
| 119 | fmt.Fprintf(os.Stderr, "%s", err) |
| 120 | os.Exit(1) |
| 121 | } |
| 122 | // Second pass: Full analysis, using the bazel command results. Output ninja file. |
| 123 | secondPassConfig, err := android.ConfigForAdditionalRun(configuration) |
| 124 | if err != nil { |
| 125 | fmt.Fprintf(os.Stderr, "%s", err) |
| 126 | os.Exit(1) |
| 127 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 128 | ctx = newContext(secondPassConfig) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 129 | bootstrap.Main(ctx.Context, secondPassConfig, extraNinjaDeps...) |
| 130 | } else { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 131 | ctx = newContext(configuration) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 132 | bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...) |
| 133 | } |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 134 | |
| 135 | // Convert the Soong module graph into Bazel BUILD files. |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 136 | if bazelQueryViewDir != "" { |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame^] | 137 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
| 138 | codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView) |
| 139 | if err := createBazelQueryView(codegenContext, bazelQueryViewDir); err != nil { |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 140 | fmt.Fprintf(os.Stderr, "%s", err) |
| 141 | os.Exit(1) |
| 142 | } |
| 143 | } |
| 144 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 145 | if docFile != "" { |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame] | 146 | if err := writeDocs(ctx, docFile); err != nil { |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 147 | fmt.Fprintf(os.Stderr, "%s", err) |
| 148 | os.Exit(1) |
| 149 | } |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 150 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 151 | |
| 152 | // TODO(ccross): make this a command line argument. Requires plumbing through blueprint |
| 153 | // to affect the command line of the primary builder. |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 154 | if shouldPrepareBuildActions(configuration) { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 155 | metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 156 | err := android.WriteMetrics(configuration, metricsFile) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 157 | if err != nil { |
| 158 | fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err) |
| 159 | os.Exit(1) |
| 160 | } |
| 161 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 162 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 163 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 164 | // Run Soong in the bp2build mode. This creates a standalone context that registers |
| 165 | // an alternate pipeline of mutators and singletons specifically for generating |
| 166 | // Bazel BUILD files instead of Ninja files. |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 167 | func runBp2Build(srcDir string, configuration android.Config) { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 168 | // Register an alternate set of singletons and mutators for bazel |
| 169 | // conversion for Bazel conversion. |
| 170 | bp2buildCtx := android.NewContext(configuration) |
| 171 | bp2buildCtx.RegisterForBazelConversion() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 172 | |
| 173 | // No need to generate Ninja build rules/statements from Modules and Singletons. |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 174 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 175 | bp2buildCtx.SetNameInterface(newNameResolver(configuration)) |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 176 | |
Jingwen Chen | 7dcc4fc | 2021-02-05 01:28:44 -0500 | [diff] [blame] | 177 | // The bp2build process is a purely functional process that only depends on |
| 178 | // Android.bp files. It must not depend on the values of per-build product |
| 179 | // configurations or variables, since those will generate different BUILD |
| 180 | // files based on how the user has configured their tree. |
| 181 | bp2buildCtx.SetModuleListFile(bootstrap.ModuleListFile) |
| 182 | extraNinjaDeps, err := bp2buildCtx.ListModulePaths(srcDir) |
| 183 | if err != nil { |
| 184 | panic(err) |
| 185 | } |
| 186 | extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ") |
| 187 | |
| 188 | // Run the loading and analysis pipeline to prepare the graph of regular |
| 189 | // Modules parsed from Android.bp files, and the BazelTargetModules mapped |
| 190 | // from the regular Modules. |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 191 | bootstrap.Main(bp2buildCtx.Context, configuration, extraNinjaDeps...) |
| 192 | |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame^] | 193 | // Run the code-generation phase to convert BazelTargetModules to BUILD files |
| 194 | // and print conversion metrics to the user. |
Jingwen Chen | 33832f9 | 2021-01-24 22:55:54 -0500 | [diff] [blame] | 195 | codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build) |
Jingwen Chen | 164e086 | 2021-02-19 00:48:40 -0500 | [diff] [blame^] | 196 | metrics := bp2build.Codegen(codegenContext) |
| 197 | |
| 198 | // Only report metrics when in bp2build mode. The metrics aren't relevant |
| 199 | // for queryview, since that's a total repo-wide conversion and there's a |
| 200 | // 1:1 mapping for each module. |
| 201 | metrics.Print() |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 202 | |
| 203 | // Workarounds to support running bp2build in a clean AOSP checkout with no |
| 204 | // prior builds, and exiting early as soon as the BUILD files get generated, |
| 205 | // therefore not creating build.ninja files that soong_ui and callers of |
| 206 | // soong_build expects. |
| 207 | // |
| 208 | // These files are: build.ninja and build.ninja.d. Since Kati hasn't been |
| 209 | // ran as well, and `nothing` is defined in a .mk file, there isn't a ninja |
| 210 | // target called `nothing`, so we manually create it here. |
| 211 | // |
| 212 | // Even though outFile (build.ninja) and depFile (build.ninja.d) are values |
| 213 | // passed into bootstrap.Main, they are package-private fields in bootstrap. |
| 214 | // Short of modifying Blueprint to add an exported getter, inlining them |
| 215 | // here is the next-best practical option. |
| 216 | ninjaFileName := "build.ninja" |
| 217 | ninjaFile := android.PathForOutput(codegenContext, ninjaFileName) |
| 218 | ninjaFileD := android.PathForOutput(codegenContext, ninjaFileName+".d") |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 219 | // A workaround to create the 'nothing' ninja target so `m nothing` works, |
| 220 | // since bp2build runs without Kati, and the 'nothing' target is declared in |
| 221 | // a Makefile. |
| 222 | android.WriteFileToOutputDir(ninjaFile, []byte("build nothing: phony\n phony_output = true\n"), 0666) |
| 223 | android.WriteFileToOutputDir( |
| 224 | ninjaFileD, |
| 225 | []byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFileName, extraNinjaDepsString)), |
| 226 | 0666) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 227 | } |
| 228 | |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 229 | // shouldPrepareBuildActions reads configuration and flags if build actions |
| 230 | // should be generated. |
| 231 | func shouldPrepareBuildActions(configuration android.Config) bool { |
| 232 | // Generating Soong docs |
| 233 | if docFile != "" { |
| 234 | return false |
| 235 | } |
| 236 | |
| 237 | // Generating a directory for Soong query (queryview) |
| 238 | if bazelQueryViewDir != "" { |
| 239 | return false |
| 240 | } |
| 241 | |
| 242 | // Generating a directory for converted Bazel BUILD files |
| 243 | return !bazelConversionRequested(configuration) |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 244 | } |