blob: e7f995fdafc6969a9444b2b157634644206f9551 [file] [log] [blame]
Colin Cross3f40fa42015-01-30 17:27:36 -08001// 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
15package main
16
17import (
18 "flag"
19 "fmt"
20 "os"
21 "path/filepath"
Jingwen Cheneb76c432021-01-28 08:22:12 -050022 "strings"
Colin Cross3f40fa42015-01-30 17:27:36 -080023
Lukacs T. Berki7690c092021-02-26 14:27:36 +010024 "android/soong/shared"
Colin Cross70b40592015-03-23 12:57:34 -070025 "github.com/google/blueprint/bootstrap"
Colin Cross3f40fa42015-01-30 17:27:36 -080026
Colin Cross635c3b02016-05-18 15:37:25 -070027 "android/soong/android"
Jingwen Chendaa54bc2020-12-14 02:58:54 -050028 "android/soong/bp2build"
Colin Cross3f40fa42015-01-30 17:27:36 -080029)
30
Colin Crosse87040b2017-12-11 15:52:26 -080031var (
Lukacs T. Berki7690c092021-02-26 14:27:36 +010032 topDir string
33 outDir string
Jingwen Chen50f93d22020-11-05 07:42:11 -050034 docFile string
35 bazelQueryViewDir string
Colin Crosse87040b2017-12-11 15:52:26 -080036)
37
38func init() {
Lukacs T. Berki7690c092021-02-26 14:27:36 +010039 flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree")
40 flag.StringVar(&outDir, "out", "", "Soong output directory (usually $TOP/out/soong)")
Colin Crosse87040b2017-12-11 15:52:26 -080041 flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
Jingwen Chen50f93d22020-11-05 07:42:11 -050042 flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory")
Colin Crosse87040b2017-12-11 15:52:26 -080043}
44
Jeff Gaston088e29e2017-11-29 16:47:17 -080045func newNameResolver(config android.Config) *android.NameResolver {
46 namespacePathsToExport := make(map[string]bool)
47
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070048 for _, namespaceName := range config.ExportedNamespaces() {
Jeff Gaston088e29e2017-11-29 16:47:17 -080049 namespacePathsToExport[namespaceName] = true
50 }
51
52 namespacePathsToExport["."] = true // always export the root namespace
53
54 exportFilter := func(namespace *android.Namespace) bool {
55 return namespacePathsToExport[namespace.Path]
56 }
57
58 return android.NewNameResolver(exportFilter)
59}
60
Jingwen Chen4133ce62020-12-02 04:34:15 -050061// bazelConversionRequested checks that the user is intending to convert
62// Blueprint to Bazel BUILD files.
63func bazelConversionRequested(configuration android.Config) bool {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050064 return configuration.IsEnvTrue("GENERATE_BAZEL_FILES")
Jingwen Chen4133ce62020-12-02 04:34:15 -050065}
66
Jingwen Chendaa54bc2020-12-14 02:58:54 -050067func newContext(configuration android.Config) *android.Context {
Colin Crossae8600b2020-10-29 17:09:13 -070068 ctx := android.NewContext(configuration)
Jingwen Chendaa54bc2020-12-14 02:58:54 -050069 ctx.Register()
Jingwen Chen4133ce62020-12-02 04:34:15 -050070 if !shouldPrepareBuildActions(configuration) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040071 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
72 }
73 ctx.SetNameInterface(newNameResolver(configuration))
74 ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
75 return ctx
76}
77
78func newConfig(srcDir string) android.Config {
79 configuration, err := android.NewConfig(srcDir, bootstrap.BuildDir, bootstrap.ModuleListFile)
80 if err != nil {
81 fmt.Fprintf(os.Stderr, "%s", err)
82 os.Exit(1)
83 }
84 return configuration
85}
86
Colin Cross3f40fa42015-01-30 17:27:36 -080087func main() {
88 flag.Parse()
89
Lukacs T. Berki7690c092021-02-26 14:27:36 +010090 android.InitSandbox(topDir)
91 android.InitEnvironment(shared.JoinPath(topDir, outDir, "soong.environment.available"))
92 android.ReexecWithDelveMaybe()
93
Colin Cross3f40fa42015-01-30 17:27:36 -080094 // The top-level Blueprints file is passed as the first argument.
95 srcDir := filepath.Dir(flag.Arg(0))
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040096 var ctx *android.Context
97 configuration := newConfig(srcDir)
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050098 extraNinjaDeps := []string{configuration.ProductVariablesFileName}
Colin Crossaa812d12019-06-19 13:33:24 -070099
Lukacs T. Berki7690c092021-02-26 14:27:36 +0100100 // These two are here so that we restart a non-debugged soong_build when the
101 // user sets SOONG_DELVE the first time.
102 configuration.Getenv("SOONG_DELVE")
103 configuration.Getenv("SOONG_DELVE_PATH")
Colin Crossaa812d12019-06-19 13:33:24 -0700104 // Read the SOONG_DELVE again through configuration so that there is a dependency on the environment variable
105 // and soong_build will rerun when it is set for the first time.
106 if listen := configuration.Getenv("SOONG_DELVE"); listen != "" {
107 // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is
108 // enabled even if it completed successfully.
109 extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve"))
110 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500111
112 if bazelConversionRequested(configuration) {
113 // Run the alternate pipeline of bp2build mutators and singleton to convert Blueprint to BUILD files
114 // before everything else.
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500115 runBp2Build(srcDir, configuration)
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500116 // Short-circuit and return.
117 return
118 }
119
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400120 if configuration.BazelContext.BazelEnabled() {
121 // Bazel-enabled mode. Soong runs in two passes.
122 // First pass: Analyze the build tree, but only store all bazel commands
123 // needed to correctly evaluate the tree in the second pass.
124 // TODO(cparsons): Don't output any ninja file, as the second pass will overwrite
125 // the incorrect results from the first pass, and file I/O is expensive.
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500126 firstCtx := newContext(configuration)
Chris Parsons3060ec72020-11-09 20:08:36 -0500127 configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400128 bootstrap.Main(firstCtx.Context, configuration, extraNinjaDeps...)
129 // Invoke bazel commands and save results for second pass.
130 if err := configuration.BazelContext.InvokeBazel(); err != nil {
131 fmt.Fprintf(os.Stderr, "%s", err)
132 os.Exit(1)
133 }
134 // Second pass: Full analysis, using the bazel command results. Output ninja file.
135 secondPassConfig, err := android.ConfigForAdditionalRun(configuration)
136 if err != nil {
137 fmt.Fprintf(os.Stderr, "%s", err)
138 os.Exit(1)
139 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500140 ctx = newContext(secondPassConfig)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400141 bootstrap.Main(ctx.Context, secondPassConfig, extraNinjaDeps...)
142 } else {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500143 ctx = newContext(configuration)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400144 bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...)
145 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500146
147 // Convert the Soong module graph into Bazel BUILD files.
Jingwen Chen50f93d22020-11-05 07:42:11 -0500148 if bazelQueryViewDir != "" {
Jingwen Chen164e0862021-02-19 00:48:40 -0500149 // Run the code-generation phase to convert BazelTargetModules to BUILD files.
150 codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView)
151 if err := createBazelQueryView(codegenContext, bazelQueryViewDir); err != nil {
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000152 fmt.Fprintf(os.Stderr, "%s", err)
153 os.Exit(1)
154 }
155 }
156
Colin Crosse87040b2017-12-11 15:52:26 -0800157 if docFile != "" {
Sasha Smundakff483392019-02-07 12:10:56 -0800158 if err := writeDocs(ctx, docFile); err != nil {
Colin Cross7089c272019-01-25 22:43:35 -0800159 fmt.Fprintf(os.Stderr, "%s", err)
160 os.Exit(1)
161 }
Colin Crosse87040b2017-12-11 15:52:26 -0800162 }
Colin Crossb72c9092020-02-10 11:23:49 -0800163
164 // TODO(ccross): make this a command line argument. Requires plumbing through blueprint
165 // to affect the command line of the primary builder.
Jingwen Chen4133ce62020-12-02 04:34:15 -0500166 if shouldPrepareBuildActions(configuration) {
Colin Crossb72c9092020-02-10 11:23:49 -0800167 metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400168 err := android.WriteMetrics(configuration, metricsFile)
Colin Crossb72c9092020-02-10 11:23:49 -0800169 if err != nil {
170 fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
171 os.Exit(1)
172 }
173 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800174}
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000175
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500176// Run Soong in the bp2build mode. This creates a standalone context that registers
177// an alternate pipeline of mutators and singletons specifically for generating
178// Bazel BUILD files instead of Ninja files.
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500179func runBp2Build(srcDir string, configuration android.Config) {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500180 // Register an alternate set of singletons and mutators for bazel
181 // conversion for Bazel conversion.
182 bp2buildCtx := android.NewContext(configuration)
183 bp2buildCtx.RegisterForBazelConversion()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500184
185 // No need to generate Ninja build rules/statements from Modules and Singletons.
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500186 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
187 bp2buildCtx.SetNameInterface(newNameResolver(configuration))
Jingwen Cheneb76c432021-01-28 08:22:12 -0500188
Jingwen Chen7dcc4fc2021-02-05 01:28:44 -0500189 // The bp2build process is a purely functional process that only depends on
190 // Android.bp files. It must not depend on the values of per-build product
191 // configurations or variables, since those will generate different BUILD
192 // files based on how the user has configured their tree.
193 bp2buildCtx.SetModuleListFile(bootstrap.ModuleListFile)
194 extraNinjaDeps, err := bp2buildCtx.ListModulePaths(srcDir)
195 if err != nil {
196 panic(err)
197 }
198 extraNinjaDepsString := strings.Join(extraNinjaDeps, " \\\n ")
199
200 // Run the loading and analysis pipeline to prepare the graph of regular
201 // Modules parsed from Android.bp files, and the BazelTargetModules mapped
202 // from the regular Modules.
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500203 bootstrap.Main(bp2buildCtx.Context, configuration, extraNinjaDeps...)
204
Jingwen Chen164e0862021-02-19 00:48:40 -0500205 // Run the code-generation phase to convert BazelTargetModules to BUILD files
206 // and print conversion metrics to the user.
Jingwen Chen33832f92021-01-24 22:55:54 -0500207 codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx, bp2build.Bp2Build)
Jingwen Chen164e0862021-02-19 00:48:40 -0500208 metrics := bp2build.Codegen(codegenContext)
209
210 // Only report metrics when in bp2build mode. The metrics aren't relevant
211 // for queryview, since that's a total repo-wide conversion and there's a
212 // 1:1 mapping for each module.
213 metrics.Print()
Jingwen Cheneb76c432021-01-28 08:22:12 -0500214
215 // Workarounds to support running bp2build in a clean AOSP checkout with no
216 // prior builds, and exiting early as soon as the BUILD files get generated,
217 // therefore not creating build.ninja files that soong_ui and callers of
218 // soong_build expects.
219 //
220 // These files are: build.ninja and build.ninja.d. Since Kati hasn't been
221 // ran as well, and `nothing` is defined in a .mk file, there isn't a ninja
222 // target called `nothing`, so we manually create it here.
223 //
224 // Even though outFile (build.ninja) and depFile (build.ninja.d) are values
225 // passed into bootstrap.Main, they are package-private fields in bootstrap.
226 // Short of modifying Blueprint to add an exported getter, inlining them
227 // here is the next-best practical option.
228 ninjaFileName := "build.ninja"
229 ninjaFile := android.PathForOutput(codegenContext, ninjaFileName)
230 ninjaFileD := android.PathForOutput(codegenContext, ninjaFileName+".d")
Jingwen Cheneb76c432021-01-28 08:22:12 -0500231 // A workaround to create the 'nothing' ninja target so `m nothing` works,
232 // since bp2build runs without Kati, and the 'nothing' target is declared in
233 // a Makefile.
234 android.WriteFileToOutputDir(ninjaFile, []byte("build nothing: phony\n phony_output = true\n"), 0666)
235 android.WriteFileToOutputDir(
236 ninjaFileD,
237 []byte(fmt.Sprintf("%s: \\\n %s\n", ninjaFileName, extraNinjaDepsString)),
238 0666)
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500239}
240
Jingwen Chen4133ce62020-12-02 04:34:15 -0500241// shouldPrepareBuildActions reads configuration and flags if build actions
242// should be generated.
243func shouldPrepareBuildActions(configuration android.Config) bool {
244 // Generating Soong docs
245 if docFile != "" {
246 return false
247 }
248
249 // Generating a directory for Soong query (queryview)
250 if bazelQueryViewDir != "" {
251 return false
252 }
253
254 // Generating a directory for converted Bazel BUILD files
255 return !bazelConversionRequested(configuration)
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000256}