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" |
| 22 | |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 23 | "github.com/google/blueprint/bootstrap" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 24 | |
Colin Cross | 635c3b0 | 2016-05-18 15:37:25 -0700 | [diff] [blame] | 25 | "android/soong/android" |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 26 | "android/soong/bp2build" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 29 | var ( |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 30 | docFile string |
| 31 | bazelQueryViewDir string |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 32 | ) |
| 33 | |
| 34 | func init() { |
| 35 | flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output") |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 36 | flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory") |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 39 | func newNameResolver(config android.Config) *android.NameResolver { |
| 40 | namespacePathsToExport := make(map[string]bool) |
| 41 | |
Dan Willemsen | 3fb1fae | 2018-03-12 15:30:26 -0700 | [diff] [blame] | 42 | for _, namespaceName := range config.ExportedNamespaces() { |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 43 | 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 Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 55 | // bazelConversionRequested checks that the user is intending to convert |
| 56 | // Blueprint to Bazel BUILD files. |
| 57 | func bazelConversionRequested(configuration android.Config) bool { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 58 | return configuration.IsEnvTrue("GENERATE_BAZEL_FILES") |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 59 | } |
| 60 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 61 | func newContext(configuration android.Config) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 62 | ctx := android.NewContext(configuration) |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 63 | ctx.Register() |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 64 | if !shouldPrepareBuildActions(configuration) { |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 65 | configuration.SetStopBefore(bootstrap.StopBeforePrepareBuildActions) |
| 66 | } |
| 67 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 68 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
| 69 | return ctx |
| 70 | } |
| 71 | |
| 72 | func 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 Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 81 | func main() { |
Lukacs T. Berki | a5e0f71 | 2020-05-18 09:50:18 +0200 | [diff] [blame] | 82 | android.ReexecWithDelveMaybe() |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 83 | flag.Parse() |
| 84 | |
| 85 | // The top-level Blueprints file is passed as the first argument. |
| 86 | srcDir := filepath.Dir(flag.Arg(0)) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 87 | var ctx *android.Context |
| 88 | configuration := newConfig(srcDir) |
Jingwen Chen | c4d91bc | 2020-11-24 22:59:26 -0500 | [diff] [blame] | 89 | extraNinjaDeps := []string{configuration.ProductVariablesFileName} |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 90 | |
| 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 Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 98 | |
| 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 Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 107 | 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 Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 113 | firstCtx := newContext(configuration) |
Chris Parsons | 3060ec7 | 2020-11-09 20:08:36 -0500 | [diff] [blame] | 114 | configuration.SetStopBefore(bootstrap.StopBeforeWriteNinja) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 115 | 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 Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 127 | ctx = newContext(secondPassConfig) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 128 | bootstrap.Main(ctx.Context, secondPassConfig, extraNinjaDeps...) |
| 129 | } else { |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 130 | ctx = newContext(configuration) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 131 | bootstrap.Main(ctx.Context, configuration, extraNinjaDeps...) |
| 132 | } |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 133 | |
| 134 | // Convert the Soong module graph into Bazel BUILD files. |
Jingwen Chen | 50f93d2 | 2020-11-05 07:42:11 -0500 | [diff] [blame] | 135 | if bazelQueryViewDir != "" { |
| 136 | if err := createBazelQueryView(ctx, bazelQueryViewDir); err != nil { |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 137 | fmt.Fprintf(os.Stderr, "%s", err) |
| 138 | os.Exit(1) |
| 139 | } |
| 140 | } |
| 141 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 142 | if docFile != "" { |
Sasha Smundak | ff48339 | 2019-02-07 12:10:56 -0800 | [diff] [blame] | 143 | if err := writeDocs(ctx, docFile); err != nil { |
Colin Cross | 7089c27 | 2019-01-25 22:43:35 -0800 | [diff] [blame] | 144 | fmt.Fprintf(os.Stderr, "%s", err) |
| 145 | os.Exit(1) |
| 146 | } |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 147 | } |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 148 | |
| 149 | // TODO(ccross): make this a command line argument. Requires plumbing through blueprint |
| 150 | // to affect the command line of the primary builder. |
Jingwen Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 151 | if shouldPrepareBuildActions(configuration) { |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 152 | metricsFile := filepath.Join(bootstrap.BuildDir, "soong_build_metrics.pb") |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 153 | err := android.WriteMetrics(configuration, metricsFile) |
Colin Cross | b72c909 | 2020-02-10 11:23:49 -0800 | [diff] [blame] | 154 | if err != nil { |
| 155 | fmt.Fprintf(os.Stderr, "error writing soong_build metrics %s: %s", metricsFile, err) |
| 156 | os.Exit(1) |
| 157 | } |
| 158 | } |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 159 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 160 | |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame^] | 161 | // 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. |
| 164 | func 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 Chen | 4133ce6 | 2020-12-02 04:34:15 -0500 | [diff] [blame] | 177 | // shouldPrepareBuildActions reads configuration and flags if build actions |
| 178 | // should be generated. |
| 179 | func 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 Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 192 | } |