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