main.go 9.7 KB

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