blob: 1d5e7b3af640dfa17eaab8f117c3c8279b4cffc3 [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"
22
Colin Cross70b40592015-03-23 12:57:34 -070023 "github.com/google/blueprint/bootstrap"
Colin Cross3f40fa42015-01-30 17:27:36 -080024
Colin Cross635c3b02016-05-18 15:37:25 -070025 "android/soong/android"
Jingwen Chendaa54bc2020-12-14 02:58:54 -050026 "android/soong/bp2build"
Colin Cross3f40fa42015-01-30 17:27:36 -080027)
28
Colin Crosse87040b2017-12-11 15:52:26 -080029var (
Jingwen Chen50f93d22020-11-05 07:42:11 -050030 docFile string
31 bazelQueryViewDir string
Colin Crosse87040b2017-12-11 15:52:26 -080032)
33
34func init() {
35 flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
Jingwen Chen50f93d22020-11-05 07:42:11 -050036 flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory")
Colin Crosse87040b2017-12-11 15:52:26 -080037}
38
Jeff Gaston088e29e2017-11-29 16:47:17 -080039func newNameResolver(config android.Config) *android.NameResolver {
40 namespacePathsToExport := make(map[string]bool)
41
Dan Willemsen3fb1fae2018-03-12 15:30:26 -070042 for _, namespaceName := range config.ExportedNamespaces() {
Jeff Gaston088e29e2017-11-29 16:47:17 -080043 namespacePathsToExport[namespaceName] = true
44 }
45
46 namespacePathsToExport["."] = true // always export the root namespace
47
48 exportFilter := func(namespace *android.Namespace) bool {
49 return namespacePathsToExport[namespace.Path]
50 }
51
52 return android.NewNameResolver(exportFilter)
53}
54
Jingwen Chen4133ce62020-12-02 04:34:15 -050055// bazelConversionRequested checks that the user is intending to convert
56// Blueprint to Bazel BUILD files.
57func bazelConversionRequested(configuration android.Config) bool {
Jingwen Chendaa54bc2020-12-14 02:58:54 -050058 return configuration.IsEnvTrue("GENERATE_BAZEL_FILES")
Jingwen Chen4133ce62020-12-02 04:34:15 -050059}
60
Jingwen Chendaa54bc2020-12-14 02:58:54 -050061func newContext(configuration android.Config) *android.Context {
Colin Crossae8600b2020-10-29 17:09:13 -070062 ctx := android.NewContext(configuration)
Jingwen Chendaa54bc2020-12-14 02:58:54 -050063 ctx.Register()
Jingwen Chen4133ce62020-12-02 04:34:15 -050064 if !shouldPrepareBuildActions(configuration) {
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040065 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
66 }
67 ctx.SetNameInterface(newNameResolver(configuration))
68 ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
69 return ctx
70}
71
72func newConfig(srcDir string) android.Config {
73 configuration, err := android.NewConfig(srcDir, bootstrap.BuildDir, bootstrap.ModuleListFile)
74 if err != nil {
75 fmt.Fprintf(os.Stderr, "%s", err)
76 os.Exit(1)
77 }
78 return configuration
79}
80
Colin Cross3f40fa42015-01-30 17:27:36 -080081func main() {
Lukacs T. Berkia5e0f712020-05-18 09:50:18 +020082 android.ReexecWithDelveMaybe()
Colin Cross3f40fa42015-01-30 17:27:36 -080083 flag.Parse()
84
85 // The top-level Blueprints file is passed as the first argument.
86 srcDir := filepath.Dir(flag.Arg(0))
Chris Parsonsf3c96ef2020-09-29 02:23:17 -040087 var ctx *android.Context
88 configuration := newConfig(srcDir)
Jingwen Chenc4d91bc2020-11-24 22:59:26 -050089 extraNinjaDeps := []string{configuration.ProductVariablesFileName}
Colin Crossaa812d12019-06-19 13:33:24 -070090
91 // Read the SOONG_DELVE again through configuration so that there is a dependency on the environment variable
92 // and soong_build will rerun when it is set for the first time.
93 if listen := configuration.Getenv("SOONG_DELVE"); listen != "" {
94 // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is
95 // enabled even if it completed successfully.
96 extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.BuildDir(), "always_rerun_for_delve"))
97 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -050098
99 if bazelConversionRequested(configuration) {
100 // Run the alternate pipeline of bp2build mutators and singleton to convert Blueprint to BUILD files
101 // before everything else.
102 runBp2Build(configuration, extraNinjaDeps)
103 // Short-circuit and return.
104 return
105 }
106
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400107 if configuration.BazelContext.BazelEnabled() {
108 // Bazel-enabled mode. Soong runs in two passes.
109 // First pass: Analyze the build tree, but only store all bazel commands
110 // needed to correctly evaluate the tree in the second pass.
111 // TODO(cparsons): Don't output any ninja file, as the second pass will overwrite
112 // the incorrect results from the first pass, and file I/O is expensive.
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500113 firstCtx := newContext(configuration)
Chris Parsons3060ec72020-11-09 20:08:36 -0500114 configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400115 bootstrap.Main(firstCtx.Context, configuration, extraNinjaDeps...)
116 // Invoke bazel commands and save results for second pass.
117 if err := configuration.BazelContext.InvokeBazel(); err != nil {
118 fmt.Fprintf(os.Stderr, "%s", err)
119 os.Exit(1)
120 }
121 // Second pass: Full analysis, using the bazel command results. Output ninja file.
122 secondPassConfig, err := android.ConfigForAdditionalRun(configuration)
123 if err != nil {
124 fmt.Fprintf(os.Stderr, "%s", err)
125 os.Exit(1)
126 }
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500127 ctx = newContext(secondPassConfig)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400128 bootstrap.Main(ctx.Context, secondPassConfig, extraNinjaDeps...)
129 } else {
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500130 ctx = newContext(configuration)
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400131 bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...)
132 }
Jingwen Chen4133ce62020-12-02 04:34:15 -0500133
134 // Convert the Soong module graph into Bazel BUILD files.
Jingwen Chen50f93d22020-11-05 07:42:11 -0500135 if bazelQueryViewDir != "" {
136 if err := createBazelQueryView(ctx, bazelQueryViewDir); err != nil {
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000137 fmt.Fprintf(os.Stderr, "%s", err)
138 os.Exit(1)
139 }
140 }
141
Colin Crosse87040b2017-12-11 15:52:26 -0800142 if docFile != "" {
Sasha Smundakff483392019-02-07 12:10:56 -0800143 if err := writeDocs(ctx, docFile); err != nil {
Colin Cross7089c272019-01-25 22:43:35 -0800144 fmt.Fprintf(os.Stderr, "%s", err)
145 os.Exit(1)
146 }
Colin Crosse87040b2017-12-11 15:52:26 -0800147 }
Colin Crossb72c9092020-02-10 11:23:49 -0800148
149 // TODO(ccross): make this a command line argument. Requires plumbing through blueprint
150 // to affect the command line of the primary builder.
Jingwen Chen4133ce62020-12-02 04:34:15 -0500151 if shouldPrepareBuildActions(configuration) {
Colin Crossb72c9092020-02-10 11:23:49 -0800152 metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb")
Chris Parsonsf3c96ef2020-09-29 02:23:17 -0400153 err := android.WriteMetrics(configuration, metricsFile)
Colin Crossb72c9092020-02-10 11:23:49 -0800154 if err != nil {
155 fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err)
156 os.Exit(1)
157 }
158 }
Colin Cross3f40fa42015-01-30 17:27:36 -0800159}
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000160
Jingwen Chendaa54bc2020-12-14 02:58:54 -0500161// Run Soong in the bp2build mode. This creates a standalone context that registers
162// an alternate pipeline of mutators and singletons specifically for generating
163// Bazel BUILD files instead of Ninja files.
164func runBp2Build(configuration android.Config, extraNinjaDeps []string) {
165 // Register an alternate set of singletons and mutators for bazel
166 // conversion for Bazel conversion.
167 bp2buildCtx := android.NewContext(configuration)
168 bp2buildCtx.RegisterForBazelConversion()
169 configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions)
170 bp2buildCtx.SetNameInterface(newNameResolver(configuration))
171 bootstrap.Main(bp2buildCtx.Context, configuration, extraNinjaDeps...)
172
173 codegenContext := bp2build.NewCodegenContext(configuration, *bp2buildCtx)
174 bp2build.Codegen(codegenContext)
175}
176
Jingwen Chen4133ce62020-12-02 04:34:15 -0500177// shouldPrepareBuildActions reads configuration and flags if build actions
178// should be generated.
179func shouldPrepareBuildActions(configuration android.Config) bool {
180 // Generating Soong docs
181 if docFile != "" {
182 return false
183 }
184
185 // Generating a directory for Soong query (queryview)
186 if bazelQueryViewDir != "" {
187 return false
188 }
189
190 // Generating a directory for converted Bazel BUILD files
191 return !bazelConversionRequested(configuration)
Jingwen Chen5ba7e472020-07-15 10:06:41 +0000192}