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