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