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 ( |
Usta Shrestha | 2ba28a3 | 2022-10-24 11:33:09 -0400 | [diff] [blame] | 18 | "bytes" |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 19 | "encoding/json" |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 20 | "errors" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 21 | "flag" |
| 22 | "fmt" |
| 23 | "os" |
| 24 | "path/filepath" |
Jingwen Chen | eb76c43 | 2021-01-28 08:22:12 -0500 | [diff] [blame] | 25 | "strings" |
Lukacs T. Berki | c99c947 | 2021-03-24 10:50:06 +0100 | [diff] [blame] | 26 | "time" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 27 | |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 28 | "android/soong/android" |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 29 | "android/soong/android/allowlists" |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 30 | "android/soong/bp2build" |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 31 | "android/soong/shared" |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 32 | "github.com/google/blueprint" |
Colin Cross | 70b4059 | 2015-03-23 12:57:34 -0700 | [diff] [blame] | 33 | "github.com/google/blueprint/bootstrap" |
Lukacs T. Berki | d518e1a | 2021-04-14 13:49:50 +0200 | [diff] [blame] | 34 | "github.com/google/blueprint/deptools" |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 35 | "github.com/google/blueprint/metrics" |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 36 | "github.com/google/blueprint/proptools" |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 37 | androidProtobuf "google.golang.org/protobuf/android" |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 38 | ) |
| 39 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 40 | var ( |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 41 | topDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 42 | availableEnvFile string |
| 43 | usedEnvFile string |
| 44 | |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 45 | globFile string |
| 46 | globListDir string |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 47 | delveListen string |
| 48 | delvePath string |
| 49 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 50 | cmdlineArgs android.CmdArgs |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 51 | ) |
| 52 | |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 53 | const configCacheFile = "config.cache" |
| 54 | |
| 55 | type ConfigCache struct { |
| 56 | EnvDepsHash uint64 |
| 57 | ProductVariableFileTimestamp int64 |
| 58 | SoongBuildFileTimestamp int64 |
| 59 | } |
| 60 | |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 61 | func init() { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 62 | // Flags that make sense in every mode |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 63 | flag.StringVar(&topDir, "top", "", "Top directory of the Android source tree") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 64 | flag.StringVar(&cmdlineArgs.SoongOutDir, "soong_out", "", "Soong output directory (usually $TOP/out/soong)") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 65 | flag.StringVar(&availableEnvFile, "available_env", "", "File containing available environment variables") |
| 66 | flag.StringVar(&usedEnvFile, "used_env", "", "File containing used environment variables") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 67 | flag.StringVar(&globFile, "globFile", "build-globs.ninja", "the Ninja file of globs to output") |
| 68 | flag.StringVar(&globListDir, "globListDir", "", "the directory containing the glob list files") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 69 | flag.StringVar(&cmdlineArgs.OutDir, "out", "", "the ninja builddir directory") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 70 | flag.StringVar(&cmdlineArgs.ModuleListFile, "l", "", "file that lists filepaths to parse") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 71 | |
| 72 | // Debug flags |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 73 | flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging") |
| 74 | flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 75 | flag.StringVar(&cmdlineArgs.Cpuprofile, "cpuprofile", "", "write cpu profile to file") |
| 76 | flag.StringVar(&cmdlineArgs.TraceFile, "trace", "", "write trace to file") |
| 77 | flag.StringVar(&cmdlineArgs.Memprofile, "memprofile", "", "write memory profile to file") |
| 78 | flag.BoolVar(&cmdlineArgs.NoGC, "nogc", false, "turn off GC for debugging") |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 79 | |
| 80 | // Flags representing various modes soong_build can run in |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 81 | flag.StringVar(&cmdlineArgs.ModuleGraphFile, "module_graph_file", "", "JSON module graph file to output") |
| 82 | flag.StringVar(&cmdlineArgs.ModuleActionsFile, "module_actions_file", "", "JSON file to output inputs/outputs of actions of modules") |
| 83 | flag.StringVar(&cmdlineArgs.DocFile, "soong_docs", "", "build documentation file to output") |
| 84 | flag.StringVar(&cmdlineArgs.BazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top") |
Lukacs T. Berki | f900807 | 2021-08-16 15:24:48 +0200 | [diff] [blame] | 85 | flag.StringVar(&cmdlineArgs.OutFile, "o", "build.ninja", "the Ninja file to output") |
Kiyoung Kim | a37d9ba | 2023-04-19 13:13:45 +0900 | [diff] [blame] | 86 | flag.StringVar(&cmdlineArgs.SoongVariables, "soong_variables", "soong.variables", "the file contains all build variables") |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 87 | flag.BoolVar(&cmdlineArgs.EmptyNinjaFile, "empty-ninja-file", false, "write out a 0-byte ninja file") |
Jihoon Kang | 2a929ad | 2023-06-08 19:02:07 +0000 | [diff] [blame] | 88 | flag.BoolVar(&cmdlineArgs.BuildFromSourceStub, "build-from-source-stub", false, "build Java stubs from source files instead of API text files") |
MarkDacek | f47e142 | 2023-04-19 16:47:36 +0000 | [diff] [blame] | 89 | flag.BoolVar(&cmdlineArgs.EnsureAllowlistIntegrity, "ensure-allowlist-integrity", false, "verify that allowlisted modules are mixed-built") |
Joe Onorato | e5ed347 | 2024-02-02 14:52:05 -0800 | [diff] [blame] | 90 | flag.StringVar(&cmdlineArgs.ModuleDebugFile, "soong_module_debug", "", "soong module debug info file to write") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 91 | // Flags that probably shouldn't be flags of soong_build, but we haven't found |
Lukacs T. Berki | b078ade | 2021-08-31 10:42:08 +0200 | [diff] [blame] | 92 | // the time to remove them yet |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 93 | flag.BoolVar(&cmdlineArgs.RunGoTests, "t", false, "build and run go tests during bootstrap") |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 94 | flag.BoolVar(&cmdlineArgs.IncrementalBuildActions, "incremental-build-actions", false, "generate build actions incrementally") |
Dan Willemsen | 66213a6 | 2021-09-21 17:50:30 -0700 | [diff] [blame] | 95 | |
| 96 | // Disable deterministic randomization in the protobuf package, so incremental |
| 97 | // builds with unrelated Soong changes don't trigger large rebuilds (since we |
| 98 | // write out text protos in command lines, and command line changes trigger |
| 99 | // rebuilds). |
| 100 | androidProtobuf.DisableRand() |
Colin Cross | e87040b | 2017-12-11 15:52:26 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 103 | func newNameResolver(config android.Config) *android.NameResolver { |
Paul Duffin | 3f7bf9f | 2022-11-08 12:21:15 +0000 | [diff] [blame] | 104 | return android.NewNameResolver(config) |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Lukacs T. Berki | ffc9e8d | 2021-09-07 17:54:38 +0200 | [diff] [blame] | 107 | func newContext(configuration android.Config) *android.Context { |
Colin Cross | ae8600b | 2020-10-29 17:09:13 -0700 | [diff] [blame] | 108 | ctx := android.NewContext(configuration) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 109 | ctx.SetNameInterface(newNameResolver(configuration)) |
| 110 | ctx.SetAllowMissingDependencies(configuration.AllowMissingDependencies()) |
Sam Delmerico | 98a7329 | 2023-02-21 11:50:29 -0500 | [diff] [blame] | 111 | ctx.AddSourceRootDirs(configuration.SourceRootDirs()...) |
Chris Parsons | f3c96ef | 2020-09-29 02:23:17 -0400 | [diff] [blame] | 112 | return ctx |
| 113 | } |
| 114 | |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 115 | func needToWriteNinjaHint(ctx *android.Context) bool { |
| 116 | switch ctx.Config().GetenvWithDefault("SOONG_GENERATES_NINJA_HINT", "") { |
| 117 | case "always": |
| 118 | return true |
| 119 | case "depend": |
Cole Faust | 6bb2832 | 2024-05-13 11:57:16 -0700 | [diff] [blame] | 120 | if _, err := os.Stat(filepath.Join(topDir, ctx.Config().OutDir(), ".ninja_log")); errors.Is(err, os.ErrNotExist) { |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 121 | return true |
| 122 | } |
| 123 | } |
| 124 | return false |
| 125 | } |
| 126 | |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 127 | // Run the code-generation phase to convert BazelTargetModules to BUILD files. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 128 | func runQueryView(queryviewDir, queryviewMarker string, ctx *android.Context) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 129 | ctx.EventHandler.Begin("queryview") |
| 130 | defer ctx.EventHandler.End("queryview") |
Cole Faust | b85d1a1 | 2022-11-08 18:14:01 -0800 | [diff] [blame] | 131 | codegenContext := bp2build.NewCodegenContext(ctx.Config(), ctx, bp2build.QueryView, topDir) |
Spandan Das | 98cb856 | 2023-03-09 23:05:47 +0000 | [diff] [blame] | 132 | err := createBazelWorkspace(codegenContext, shared.JoinPath(topDir, queryviewDir), false) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 133 | maybeQuit(err, "") |
Lukacs T. Berki | 3a82169 | 2021-09-06 17:08:02 +0200 | [diff] [blame] | 134 | touch(shared.JoinPath(topDir, queryviewMarker)) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 135 | } |
| 136 | |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 137 | func writeNinjaHint(ctx *android.Context) error { |
Jeongik Cha | 73d4911 | 2023-05-04 18:16:11 +0900 | [diff] [blame] | 138 | ctx.BeginEvent("ninja_hint") |
| 139 | defer ctx.EndEvent("ninja_hint") |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 140 | // The current predictor focuses on reducing false negatives. |
| 141 | // If there are too many false positives (e.g., most modules are marked as positive), |
| 142 | // real long-running jobs cannot run early. |
| 143 | // Therefore, the model should be adjusted in this case. |
| 144 | // The model should also be adjusted if there are critical false negatives. |
| 145 | predicate := func(j *blueprint.JsonModule) (prioritized bool, weight int) { |
| 146 | prioritized = false |
| 147 | weight = 0 |
| 148 | for prefix, w := range allowlists.HugeModuleTypePrefixMap { |
| 149 | if strings.HasPrefix(j.Type, prefix) { |
| 150 | prioritized = true |
| 151 | weight = w |
| 152 | return |
| 153 | } |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 154 | } |
Jeongik Cha | b745e2e | 2023-04-11 14:28:43 +0900 | [diff] [blame] | 155 | dep_count := len(j.Deps) |
| 156 | src_count := 0 |
| 157 | for _, a := range j.Module["Actions"].([]blueprint.JSONAction) { |
| 158 | src_count += len(a.Inputs) |
| 159 | } |
| 160 | input_size := dep_count + src_count |
| 161 | |
| 162 | // Current threshold is an arbitrary value which only consider recall rather than accuracy. |
| 163 | if input_size > allowlists.INPUT_SIZE_THRESHOLD { |
| 164 | prioritized = true |
| 165 | weight += ((input_size) / allowlists.INPUT_SIZE_THRESHOLD) * allowlists.DEFAULT_PRIORITIZED_WEIGHT |
| 166 | |
| 167 | // To prevent some modules from having too large a priority value. |
| 168 | if weight > allowlists.HIGH_PRIORITIZED_WEIGHT { |
| 169 | weight = allowlists.HIGH_PRIORITIZED_WEIGHT |
| 170 | } |
| 171 | } |
| 172 | return |
| 173 | } |
| 174 | |
| 175 | outputsMap := ctx.Context.GetWeightedOutputsFromPredicate(predicate) |
| 176 | var outputBuilder strings.Builder |
| 177 | for output, weight := range outputsMap { |
| 178 | outputBuilder.WriteString(fmt.Sprintf("%s,%d\n", output, weight)) |
Jeongik Cha | e114e60 | 2023-03-19 00:12:39 +0900 | [diff] [blame] | 179 | } |
| 180 | weightListFile := filepath.Join(topDir, ctx.Config().OutDir(), ".ninja_weight_list") |
| 181 | |
| 182 | err := os.WriteFile(weightListFile, []byte(outputBuilder.String()), 0644) |
| 183 | if err != nil { |
| 184 | return fmt.Errorf("could not write ninja weight list file %s", err) |
| 185 | } |
| 186 | return nil |
| 187 | } |
| 188 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 189 | func writeMetrics(configuration android.Config, eventHandler *metrics.EventHandler, metricsDir string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 190 | if len(metricsDir) < 1 { |
| 191 | fmt.Fprintf(os.Stderr, "\nMissing required env var for generating soong metrics: LOG_DIR\n") |
| 192 | os.Exit(1) |
| 193 | } |
| 194 | metricsFile := filepath.Join(metricsDir, "soong_build_metrics.pb") |
| 195 | err := android.WriteMetrics(configuration, eventHandler, metricsFile) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 196 | maybeQuit(err, "error writing soong_build metrics %s", metricsFile) |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 199 | func writeJsonModuleGraphAndActions(ctx *android.Context, cmdArgs android.CmdArgs) { |
| 200 | graphFile, graphErr := os.Create(shared.JoinPath(topDir, cmdArgs.ModuleGraphFile)) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 201 | maybeQuit(graphErr, "graph err") |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 202 | defer graphFile.Close() |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 203 | actionsFile, actionsErr := os.Create(shared.JoinPath(topDir, cmdArgs.ModuleActionsFile)) |
| 204 | maybeQuit(actionsErr, "actions err") |
kgui | 6700724 | 2022-01-25 13:50:25 +0800 | [diff] [blame] | 205 | defer actionsFile.Close() |
| 206 | ctx.Context.PrintJSONGraphAndActions(graphFile, actionsFile) |
Lukacs T. Berki | 97bb9f1 | 2021-04-01 18:28:45 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 209 | func writeBuildGlobsNinjaFile(ctx *android.Context) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 210 | ctx.EventHandler.Begin("globs_ninja_file") |
| 211 | defer ctx.EventHandler.End("globs_ninja_file") |
| 212 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 213 | globDir := bootstrap.GlobDirectory(ctx.Config().SoongOutDir(), globListDir) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 214 | err := bootstrap.WriteBuildGlobsNinjaFile(&bootstrap.GlobSingleton{ |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 215 | GlobLister: ctx.Globs, |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 216 | GlobFile: globFile, |
| 217 | GlobDir: globDir, |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 218 | SrcDir: ctx.SrcDir(), |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 219 | }, ctx.Config()) |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 220 | maybeQuit(err, "") |
Lukacs T. Berki | 809d2ed | 2021-08-18 10:55:32 +0200 | [diff] [blame] | 221 | } |
| 222 | |
Paul Duffin | 780a185 | 2022-11-05 10:17:12 +0000 | [diff] [blame] | 223 | func writeDepFile(outputFile string, eventHandler *metrics.EventHandler, ninjaDeps []string) { |
Chris Parsons | 715b08f | 2022-03-22 19:23:40 -0400 | [diff] [blame] | 224 | eventHandler.Begin("ninja_deps") |
| 225 | defer eventHandler.End("ninja_deps") |
Lukacs T. Berki | e571dc3 | 2021-08-25 14:14:13 +0200 | [diff] [blame] | 226 | depFile := shared.JoinPath(topDir, outputFile+".d") |
| 227 | err := deptools.WriteDepFile(depFile, outputFile, ninjaDeps) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 228 | maybeQuit(err, "error writing depfile '%s'", depFile) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 229 | } |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 230 | |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 231 | // Check if there are changes to the environment file, product variable file and |
| 232 | // soong_build binary, in which case no incremental will be performed. |
| 233 | func incrementalValid(config android.Config, configCacheFile string) (*ConfigCache, bool) { |
| 234 | var newConfigCache ConfigCache |
| 235 | data, err := os.ReadFile(shared.JoinPath(topDir, usedEnvFile)) |
| 236 | if err != nil { |
| 237 | // Clean build |
| 238 | if os.IsNotExist(err) { |
| 239 | data = []byte{} |
| 240 | } else { |
| 241 | maybeQuit(err, "") |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | newConfigCache.EnvDepsHash, err = proptools.CalculateHash(data) |
| 246 | newConfigCache.ProductVariableFileTimestamp = getFileTimestamp(filepath.Join(topDir, cmdlineArgs.SoongVariables)) |
| 247 | newConfigCache.SoongBuildFileTimestamp = getFileTimestamp(filepath.Join(topDir, config.HostToolDir(), "soong_build")) |
| 248 | //TODO(b/344917959): out/soong/dexpreopt.config might need to be checked as well. |
| 249 | |
| 250 | file, err := os.Open(configCacheFile) |
| 251 | if err != nil && os.IsNotExist(err) { |
| 252 | return &newConfigCache, false |
| 253 | } |
| 254 | maybeQuit(err, "") |
| 255 | defer file.Close() |
| 256 | |
| 257 | var configCache ConfigCache |
| 258 | decoder := json.NewDecoder(file) |
| 259 | err = decoder.Decode(&configCache) |
| 260 | maybeQuit(err, "") |
| 261 | |
| 262 | return &newConfigCache, newConfigCache == configCache |
| 263 | } |
| 264 | |
| 265 | func getFileTimestamp(file string) int64 { |
| 266 | stat, err := os.Stat(file) |
| 267 | if err == nil { |
| 268 | return stat.ModTime().UnixMilli() |
| 269 | } else if !os.IsNotExist(err) { |
| 270 | maybeQuit(err, "") |
| 271 | } |
| 272 | return 0 |
| 273 | } |
| 274 | |
| 275 | func writeConfigCache(configCache *ConfigCache, configCacheFile string) { |
| 276 | file, err := os.Create(configCacheFile) |
| 277 | maybeQuit(err, "") |
| 278 | defer file.Close() |
| 279 | |
| 280 | encoder := json.NewEncoder(file) |
| 281 | err = encoder.Encode(*configCache) |
| 282 | maybeQuit(err, "") |
| 283 | } |
| 284 | |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 285 | // runSoongOnlyBuild runs the standard Soong build in a number of different modes. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 286 | func runSoongOnlyBuild(ctx *android.Context, extraNinjaDeps []string) string { |
Paul Duffin | 39eae8f | 2022-11-05 14:59:52 +0000 | [diff] [blame] | 287 | ctx.EventHandler.Begin("soong_build") |
| 288 | defer ctx.EventHandler.End("soong_build") |
| 289 | |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 290 | var stopBefore bootstrap.StopBefore |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 291 | switch ctx.Config().BuildMode { |
| 292 | case android.GenerateModuleGraph: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 293 | stopBefore = bootstrap.StopBeforeWriteNinja |
Usta Shrestha | 7fae695 | 2022-12-21 11:46:28 -0500 | [diff] [blame] | 294 | case android.GenerateQueryView, android.GenerateDocFile: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 295 | stopBefore = bootstrap.StopBeforePrepareBuildActions |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 296 | default: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 297 | stopBefore = bootstrap.DoEverything |
| 298 | } |
| 299 | |
Lukacs T. Berki | c357c81 | 2023-06-20 09:30:06 +0000 | [diff] [blame] | 300 | ninjaDeps, err := bootstrap.RunBlueprint(cmdlineArgs.Args, stopBefore, ctx.Context, ctx.Config()) |
| 301 | maybeQuit(err, "") |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 302 | ninjaDeps = append(ninjaDeps, extraNinjaDeps...) |
| 303 | |
Cole Faust | 8c0b11e | 2024-01-02 17:02:52 -0800 | [diff] [blame] | 304 | writeBuildGlobsNinjaFile(ctx) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 305 | |
| 306 | // Convert the Soong module graph into Bazel BUILD files. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 307 | switch ctx.Config().BuildMode { |
| 308 | case android.GenerateQueryView: |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 309 | queryviewMarkerFile := cmdlineArgs.BazelQueryViewDir + ".marker" |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 310 | runQueryView(cmdlineArgs.BazelQueryViewDir, queryviewMarkerFile, ctx) |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 311 | writeDepFile(queryviewMarkerFile, ctx.EventHandler, ninjaDeps) |
| 312 | return queryviewMarkerFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 313 | case android.GenerateModuleGraph: |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 314 | writeJsonModuleGraphAndActions(ctx, cmdlineArgs) |
| 315 | writeDepFile(cmdlineArgs.ModuleGraphFile, ctx.EventHandler, ninjaDeps) |
| 316 | return cmdlineArgs.ModuleGraphFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 317 | case android.GenerateDocFile: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 318 | // TODO: we could make writeDocs() return the list of documentation files |
| 319 | // written and add them to the .d file. Then soong_docs would be re-run |
| 320 | // whenever one is deleted. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 321 | err := writeDocs(ctx, shared.JoinPath(topDir, cmdlineArgs.DocFile)) |
| 322 | maybeQuit(err, "error building Soong documentation") |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 323 | writeDepFile(cmdlineArgs.DocFile, ctx.EventHandler, ninjaDeps) |
| 324 | return cmdlineArgs.DocFile |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 325 | default: |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 326 | // The actual output (build.ninja) was written in the RunBlueprint() call |
| 327 | // above |
| 328 | writeDepFile(cmdlineArgs.OutFile, ctx.EventHandler, ninjaDeps) |
Jeongik Cha | a87506f | 2023-06-01 23:16:41 +0900 | [diff] [blame] | 329 | if needToWriteNinjaHint(ctx) { |
Jeongik Cha | 591366d | 2023-05-08 11:32:52 +0900 | [diff] [blame] | 330 | writeNinjaHint(ctx) |
| 331 | } |
Paul Duffin | b713ddf | 2022-11-05 16:14:30 +0000 | [diff] [blame] | 332 | return cmdlineArgs.OutFile |
Paul Duffin | 0c09a43 | 2022-11-05 15:28:04 +0000 | [diff] [blame] | 333 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | // soong_ui dumps the available environment variables to |
| 337 | // soong.environment.available . Then soong_build itself is run with an empty |
| 338 | // environment so that the only way environment variables can be accessed is |
| 339 | // using Config, which tracks access to them. |
| 340 | |
| 341 | // At the end of the build, a file called soong.environment.used is written |
| 342 | // containing the current value of all used environment variables. The next |
| 343 | // time soong_ui is run, it checks whether any environment variables that was |
| 344 | // used had changed and if so, it deletes soong.environment.used to cause a |
| 345 | // rebuild. |
| 346 | // |
| 347 | // The dependency of build.ninja on soong.environment.used is declared in |
| 348 | // build.ninja.d |
| 349 | func parseAvailableEnv() map[string]string { |
| 350 | if availableEnvFile == "" { |
| 351 | fmt.Fprintf(os.Stderr, "--available_env not set\n") |
| 352 | os.Exit(1) |
| 353 | } |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 354 | result, err := shared.EnvFromFile(shared.JoinPath(topDir, availableEnvFile)) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 355 | maybeQuit(err, "error reading available environment file '%s'", availableEnvFile) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 356 | return result |
Lukacs T. Berki | 6790ebc | 2021-04-01 17:55:58 +0200 | [diff] [blame] | 357 | } |
| 358 | |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 359 | func main() { |
| 360 | flag.Parse() |
| 361 | |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 362 | shared.ReexecWithDelveMaybe(delveListen, delvePath) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 363 | android.InitSandbox(topDir) |
Lukacs T. Berki | 7690c09 | 2021-02-26 14:27:36 +0100 | [diff] [blame] | 364 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 365 | availableEnv := parseAvailableEnv() |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 366 | configuration, err := android.NewConfig(cmdlineArgs, availableEnv) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 367 | maybeQuit(err, "") |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 368 | if configuration.Getenv("ALLOW_MISSING_DEPENDENCIES") == "true" { |
| 369 | configuration.SetAllowMissingDependencies() |
| 370 | } |
| 371 | |
Sasha Smundak | af5ca92 | 2022-12-12 21:23:34 -0800 | [diff] [blame] | 372 | extraNinjaDeps := []string{configuration.ProductVariablesFileName, usedEnvFile} |
Lukacs T. Berki | 7d613bf | 2021-03-02 10:09:41 +0100 | [diff] [blame] | 373 | if shared.IsDebugging() { |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 374 | // Add a non-existent file to the dependencies so that soong_build will rerun when the debugger is |
| 375 | // enabled even if it completed successfully. |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 376 | extraNinjaDeps = append(extraNinjaDeps, filepath.Join(configuration.SoongOutDir(), "always_rerun_for_delve")) |
Colin Cross | aa812d1 | 2019-06-19 13:33:24 -0700 | [diff] [blame] | 377 | } |
Jingwen Chen | daa54bc | 2020-12-14 02:58:54 -0500 | [diff] [blame] | 378 | |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 379 | // Bypass configuration.Getenv, as LOG_DIR does not need to be dependency tracked. By definition, it will |
| 380 | // change between every CI build, so tracking it would require re-running Soong for every build. |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 381 | metricsDir := availableEnv["LOG_DIR"] |
Dan Willemsen | ccf36aa | 2022-04-20 23:11:43 -0700 | [diff] [blame] | 382 | |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 383 | ctx := newContext(configuration) |
Colin Cross | 46b0c75 | 2023-10-27 14:56:12 -0700 | [diff] [blame] | 384 | android.StartBackgroundMetrics(configuration) |
Joe Onorato | 2e5e401 | 2022-06-07 17:16:08 -0700 | [diff] [blame] | 385 | |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 386 | var configCache *ConfigCache |
| 387 | configFile := filepath.Join(topDir, ctx.Config().OutDir(), configCacheFile) |
| 388 | incremental := false |
| 389 | ctx.SetIncrementalEnabled(cmdlineArgs.IncrementalBuildActions) |
| 390 | if cmdlineArgs.IncrementalBuildActions { |
| 391 | configCache, incremental = incrementalValid(ctx.Config(), configFile) |
| 392 | } |
| 393 | ctx.SetIncrementalAnalysis(incremental) |
| 394 | |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 395 | ctx.Register() |
| 396 | finalOutputFile := runSoongOnlyBuild(ctx, extraNinjaDeps) |
Yu Liu | fa29764 | 2024-06-11 00:13:02 +0000 | [diff] [blame^] | 397 | |
| 398 | if ctx.GetIncrementalEnabled() { |
| 399 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
| 400 | maybeQuit(err, "") |
| 401 | configCache.EnvDepsHash, err = proptools.CalculateHash(data) |
| 402 | maybeQuit(err, "") |
| 403 | writeConfigCache(configCache, configFile) |
| 404 | } |
| 405 | |
Colin Cross | b63d7b3 | 2023-12-07 16:54:51 -0800 | [diff] [blame] | 406 | writeMetrics(configuration, ctx.EventHandler, metricsDir) |
Chris Parsons | c83398f | 2023-05-31 18:41:41 +0000 | [diff] [blame] | 407 | |
Chris Parsons | a3ae007 | 2023-05-10 21:10:08 +0000 | [diff] [blame] | 408 | writeUsedEnvironmentFile(configuration) |
| 409 | |
| 410 | // Touch the output file so that it's the newest file created by soong_build. |
| 411 | // This is necessary because, if soong_build generated any files which |
| 412 | // are ninja inputs to the main output file, then ninja would superfluously |
| 413 | // rebuild this output file on the next build invocation. |
| 414 | touch(shared.JoinPath(topDir, finalOutputFile)) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 415 | } |
| 416 | |
Chris Parsons | a3ae007 | 2023-05-10 21:10:08 +0000 | [diff] [blame] | 417 | func writeUsedEnvironmentFile(configuration android.Config) { |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 418 | if usedEnvFile == "" { |
| 419 | return |
| 420 | } |
| 421 | |
| 422 | path := shared.JoinPath(topDir, usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 423 | data, err := shared.EnvFileContents(configuration.EnvDeps()) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 424 | maybeQuit(err, "error writing used environment file '%s'\n", usedEnvFile) |
Lukacs T. Berki | f0b3b94 | 2021-03-23 11:46:47 +0100 | [diff] [blame] | 425 | |
Usta Shrestha | 2ba28a3 | 2022-10-24 11:33:09 -0400 | [diff] [blame] | 426 | if preexistingData, err := os.ReadFile(path); err != nil { |
| 427 | if !os.IsNotExist(err) { |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 428 | maybeQuit(err, "error reading used environment file '%s'", usedEnvFile) |
Usta Shrestha | 2ba28a3 | 2022-10-24 11:33:09 -0400 | [diff] [blame] | 429 | } |
| 430 | } else if bytes.Equal(preexistingData, data) { |
| 431 | // used environment file is unchanged |
| 432 | return |
| 433 | } |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 434 | err = os.WriteFile(path, data, 0666) |
| 435 | maybeQuit(err, "error writing used environment file '%s'", usedEnvFile) |
Colin Cross | 3f40fa4 | 2015-01-30 17:27:36 -0800 | [diff] [blame] | 436 | } |
Jingwen Chen | 5ba7e47 | 2020-07-15 10:06:41 +0000 | [diff] [blame] | 437 | |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 438 | func touch(path string) { |
| 439 | f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 440 | maybeQuit(err, "Error touching '%s'", path) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 441 | err = f.Close() |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 442 | maybeQuit(err, "Error touching '%s'", path) |
Lukacs T. Berki | f8e2428 | 2021-04-14 10:31:00 +0200 | [diff] [blame] | 443 | |
| 444 | currentTime := time.Now().Local() |
| 445 | err = os.Chtimes(path, currentTime, currentTime) |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 446 | maybeQuit(err, "error touching '%s'", path) |
Rupert Shuttleworth | 0096079 | 2021-05-12 21:20:13 -0400 | [diff] [blame] | 447 | } |
| 448 | |
Sasha Smundak | 1845f42 | 2022-12-13 14:18:58 -0800 | [diff] [blame] | 449 | func maybeQuit(err error, format string, args ...interface{}) { |
| 450 | if err == nil { |
| 451 | return |
| 452 | } |
| 453 | if format != "" { |
| 454 | fmt.Fprintln(os.Stderr, fmt.Sprintf(format, args...)+": "+err.Error()) |
| 455 | } else { |
| 456 | fmt.Fprintln(os.Stderr, err) |
| 457 | } |
| 458 | os.Exit(1) |
MarkDacek | 0d5bca5 | 2022-10-10 20:07:48 +0000 | [diff] [blame] | 459 | } |