Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // Copyright 2017 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 build |
| 16 | |
| 17 | import ( |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 18 | "fmt" |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 19 | "io/ioutil" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 21 | "path/filepath" |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 22 | "sync" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | "text/template" |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 24 | |
Peter Collingbourne | b805c61 | 2024-03-14 21:59:57 -0700 | [diff] [blame] | 25 | "android/soong/elf" |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 26 | "android/soong/ui/metrics" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 27 | ) |
| 28 | |
Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 29 | // SetupOutDir ensures the out directory exists, and has the proper files to |
| 30 | // prevent kati from recursing into it. |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 31 | func SetupOutDir(ctx Context, config Config) { |
| 32 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk")) |
| 33 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk")) |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 34 | ensureEmptyDirectoriesExist(ctx, config.TempDir()) |
Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 35 | |
| 36 | // Potentially write a marker file for whether kati is enabled. This is used by soong_build to |
| 37 | // potentially run the AndroidMk singleton and postinstall commands. |
| 38 | // Note that the absence of the file does not not preclude running Kati for product |
| 39 | // configuration purposes. |
| 40 | katiEnabledMarker := filepath.Join(config.SoongOutDir(), ".soong.kati_enabled") |
Cole Faust | 88f469e | 2025-01-21 13:05:05 -0800 | [diff] [blame^] | 41 | if config.SkipKati() || config.SkipKatiNinja() { |
Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 42 | os.Remove(katiEnabledMarker) |
Cole Faust | 88f469e | 2025-01-21 13:05:05 -0800 | [diff] [blame^] | 43 | } else { |
Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 44 | ensureEmptyFileExists(ctx, katiEnabledMarker) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 45 | } |
Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 46 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 47 | // The ninja_build file is used by our buildbots to understand that the output |
| 48 | // can be parsed as ninja output. |
| 49 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 50 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir")) |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 51 | |
| 52 | if buildDateTimeFile, ok := config.environ.Get("BUILD_DATETIME_FILE"); ok { |
Cole Faust | 88f469e | 2025-01-21 13:05:05 -0800 | [diff] [blame^] | 53 | err := os.WriteFile(buildDateTimeFile, []byte(config.buildDateTime), 0666) // a+rw |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 54 | if err != nil { |
| 55 | ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err) |
| 56 | } |
| 57 | } else { |
| 58 | ctx.Fatalln("Missing BUILD_DATETIME_FILE") |
| 59 | } |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 60 | |
| 61 | // BUILD_NUMBER should be set to the source control value that |
| 62 | // represents the current state of the source code. E.g., a |
| 63 | // perforce changelist number or a git hash. Can be an arbitrary string |
| 64 | // (to allow for source control that uses something other than numbers), |
| 65 | // but must be a single word and a valid file name. |
| 66 | // |
Cole Faust | 1ae7b77 | 2024-06-12 18:08:44 -0700 | [diff] [blame] | 67 | // If no BUILD_NUMBER is set, create a useful "I am an engineering build" |
| 68 | // value. Make it start with a non-digit so that anyone trying to parse |
| 69 | // it as an integer will probably get "0". This value used to contain |
| 70 | // a timestamp, but now that more dependencies are tracked in order to |
| 71 | // reduce the importance of `m installclean`, changing it every build |
| 72 | // causes unnecessary rebuilds for local development. |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 73 | buildNumber, ok := config.environ.Get("BUILD_NUMBER") |
| 74 | if ok { |
| 75 | writeValueIfChanged(ctx, config, config.OutDir(), "file_name_tag.txt", buildNumber) |
| 76 | } else { |
| 77 | var username string |
| 78 | if username, ok = config.environ.Get("BUILD_USERNAME"); !ok { |
| 79 | ctx.Fatalln("Missing BUILD_USERNAME") |
| 80 | } |
Cole Faust | 3ae36f4 | 2024-09-09 16:54:16 -0700 | [diff] [blame] | 81 | buildNumber = fmt.Sprintf("eng.%.6s", username) |
Cole Faust | 583dfb4 | 2023-09-28 13:56:30 -0700 | [diff] [blame] | 82 | writeValueIfChanged(ctx, config, config.OutDir(), "file_name_tag.txt", username) |
| 83 | } |
| 84 | // Write the build number to a file so it can be read back in |
| 85 | // without changing the command line every time. Avoids rebuilds |
| 86 | // when using ninja. |
| 87 | writeValueIfChanged(ctx, config, config.SoongOutDir(), "build_number.txt", buildNumber) |
Cole Faust | e14082c | 2025-01-06 15:38:34 -0800 | [diff] [blame] | 88 | |
| 89 | hostname, ok := config.environ.Get("BUILD_HOSTNAME") |
| 90 | if !ok { |
| 91 | var err error |
| 92 | hostname, err = os.Hostname() |
| 93 | if err != nil { |
| 94 | ctx.Println("Failed to read hostname:", err) |
| 95 | hostname = "unknown" |
| 96 | } |
| 97 | } |
| 98 | writeValueIfChanged(ctx, config, config.SoongOutDir(), "build_hostname.txt", hostname) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(` |
| 102 | builddir = {{.OutDir}} |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 103 | {{if .UseRemoteBuild }}pool local_pool |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 104 | depth = {{.Parallel}} |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 105 | {{end -}} |
| 106 | pool highmem_pool |
| 107 | depth = {{.HighmemParallel}} |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 108 | {{if and (not .SkipKatiNinja) .HasKatiSuffix}} |
| 109 | subninja {{.KatiBuildNinjaFile}} |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 110 | subninja {{.KatiPackageNinjaFile}} |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 111 | {{else}} |
| 112 | subninja {{.KatiSoongOnlyPackageNinjaFile}} |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 113 | {{end -}} |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 114 | subninja {{.SoongNinjaFile}} |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 115 | `)) |
| 116 | |
| 117 | func createCombinedBuildNinjaFile(ctx Context, config Config) { |
Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 118 | // If we're in SkipKati mode but want to run kati ninja, skip creating this file if it already exists |
| 119 | if config.SkipKati() && !config.SkipKatiNinja() { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 120 | if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) { |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 125 | file, err := os.Create(config.CombinedNinjaFile()) |
| 126 | if err != nil { |
| 127 | ctx.Fatalln("Failed to create combined ninja file:", err) |
| 128 | } |
| 129 | defer file.Close() |
| 130 | |
| 131 | if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil { |
| 132 | ctx.Fatalln("Failed to write combined ninja file:", err) |
| 133 | } |
| 134 | } |
| 135 | |
Sam Delmerico | d9a3435 | 2022-11-15 17:27:21 -0500 | [diff] [blame] | 136 | // These are bitmasks which can be used to check whether various flags are set |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 137 | const ( |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 138 | _ = iota |
| 139 | // Whether to run the kati config step. |
| 140 | RunProductConfig = 1 << iota |
| 141 | // Whether to run soong to generate a ninja file. |
| 142 | RunSoong = 1 << iota |
| 143 | // Whether to run kati to generate a ninja file. |
| 144 | RunKati = 1 << iota |
Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 145 | // Whether to include the kati-generated ninja file in the combined ninja. |
| 146 | RunKatiNinja = 1 << iota |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 147 | // Whether to run ninja on the combined ninja. |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 148 | RunNinja = 1 << iota |
| 149 | RunDistActions = 1 << iota |
| 150 | RunBuildTests = 1 << iota |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 151 | ) |
| 152 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 153 | // checkProblematicFiles fails the build if existing Android.mk or CleanSpec.mk files are found at the root of the tree. |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 154 | func checkProblematicFiles(ctx Context) { |
| 155 | files := []string{"Android.mk", "CleanSpec.mk"} |
| 156 | for _, file := range files { |
| 157 | if _, err := os.Stat(file); !os.IsNotExist(err) { |
| 158 | absolute := absPath(ctx, file) |
| 159 | ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file) |
| 160 | ctx.Fatalf(" rm %s\n", absolute) |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 165 | // checkCaseSensitivity issues a warning if a case-insensitive file system is being used. |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 166 | func checkCaseSensitivity(ctx Context, config Config) { |
| 167 | outDir := config.OutDir() |
| 168 | lowerCase := filepath.Join(outDir, "casecheck.txt") |
| 169 | upperCase := filepath.Join(outDir, "CaseCheck.txt") |
| 170 | lowerData := "a" |
| 171 | upperData := "B" |
| 172 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 173 | if err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0666); err != nil { // a+rw |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 174 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 175 | } |
| 176 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 177 | if err := ioutil.WriteFile(upperCase, []byte(upperData), 0666); err != nil { // a+rw |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 178 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 179 | } |
| 180 | |
| 181 | res, err := ioutil.ReadFile(lowerCase) |
| 182 | if err != nil { |
| 183 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 184 | } |
| 185 | |
| 186 | if string(res) != lowerData { |
| 187 | ctx.Println("************************************************************") |
| 188 | ctx.Println("You are building on a case-insensitive filesystem.") |
| 189 | ctx.Println("Please move your source tree to a case-sensitive filesystem.") |
| 190 | ctx.Println("************************************************************") |
| 191 | ctx.Fatalln("Case-insensitive filesystems not supported") |
| 192 | } |
| 193 | } |
| 194 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 195 | // help prints a help/usage message, via the build/make/help.sh script. |
| 196 | func help(ctx Context, config Config) { |
Jeff Gaston | df4a081 | 2017-05-30 20:11:20 -0700 | [diff] [blame] | 197 | cmd := Command(ctx, config, "help.sh", "build/make/help.sh") |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 198 | cmd.Sandbox = dumpvarsSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 199 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 202 | // checkRAM warns if there probably isn't enough RAM to complete a build. |
| 203 | func checkRAM(ctx Context, config Config) { |
Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 204 | if totalRAM := config.TotalRAM(); totalRAM != 0 { |
| 205 | ram := float32(totalRAM) / (1024 * 1024 * 1024) |
| 206 | ctx.Verbosef("Total RAM: %.3vGB", ram) |
| 207 | |
| 208 | if ram <= 16 { |
| 209 | ctx.Println("************************************************************") |
| 210 | ctx.Printf("You are building on a machine with %.3vGB of RAM\n", ram) |
| 211 | ctx.Println("") |
| 212 | ctx.Println("The minimum required amount of free memory is around 16GB,") |
| 213 | ctx.Println("and even with that, some configurations may not work.") |
| 214 | ctx.Println("") |
| 215 | ctx.Println("If you run into segfaults or other errors, try reducing your") |
| 216 | ctx.Println("-j value.") |
| 217 | ctx.Println("************************************************************") |
| 218 | } else if ram <= float32(config.Parallel()) { |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 219 | // Want at least 1GB of RAM per job. |
Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 220 | ctx.Printf("Warning: high -j%d count compared to %.3vGB of RAM", config.Parallel(), ram) |
| 221 | ctx.Println("If you run into segfaults or other errors, try a lower -j value") |
| 222 | } |
| 223 | } |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Taylor Santiago | 3c16e61 | 2024-05-30 14:41:31 -0700 | [diff] [blame] | 226 | func abfsBuildStarted(ctx Context, config Config) { |
| 227 | abfsBox := config.PrebuiltBuildTool("abfsbox") |
| 228 | cmdArgs := []string{"build-started", "--"} |
| 229 | cmdArgs = append(cmdArgs, config.Arguments()...) |
| 230 | cmd := Command(ctx, config, "abfsbox", abfsBox, cmdArgs...) |
| 231 | cmd.Sandbox = noSandbox |
| 232 | cmd.RunAndPrintOrFatal() |
| 233 | } |
| 234 | |
| 235 | func abfsBuildFinished(ctx Context, config Config, finished bool) { |
| 236 | var errMsg string |
| 237 | if !finished { |
| 238 | errMsg = "build was interrupted" |
| 239 | } |
| 240 | abfsBox := config.PrebuiltBuildTool("abfsbox") |
| 241 | cmdArgs := []string{"build-finished", "-e", errMsg, "--"} |
| 242 | cmdArgs = append(cmdArgs, config.Arguments()...) |
| 243 | cmd := Command(ctx, config, "abfsbox", abfsBox, cmdArgs...) |
| 244 | cmd.RunAndPrintOrFatal() |
| 245 | } |
| 246 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 247 | // Build the tree. Various flags in `config` govern which components of |
| 248 | // the build to run. |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 249 | func Build(ctx Context, config Config) { |
Taylor Santiago | 3c16e61 | 2024-05-30 14:41:31 -0700 | [diff] [blame] | 250 | done := false |
| 251 | if config.UseABFS() { |
| 252 | abfsBuildStarted(ctx, config) |
| 253 | defer func() { |
| 254 | abfsBuildFinished(ctx, config, done) |
| 255 | }() |
| 256 | } |
| 257 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 258 | ctx.Verboseln("Starting build with args:", config.Arguments()) |
| 259 | ctx.Verboseln("Environment:", config.Environment().Environ()) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 260 | |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 261 | ctx.BeginTrace(metrics.Total, "total") |
| 262 | defer ctx.EndTrace() |
| 263 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 264 | if inList("help", config.Arguments()) { |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 265 | help(ctx, config) |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 266 | return |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 267 | } |
| 268 | |
Jeff Gaston | 3615fe8 | 2017-05-24 13:14:34 -0700 | [diff] [blame] | 269 | // Make sure that no other Soong process is running with the same output directory |
| 270 | buildLock := BecomeSingletonOrFail(ctx, config) |
| 271 | defer buildLock.Unlock() |
| 272 | |
Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 273 | logArgsOtherThan := func(specialTargets ...string) { |
| 274 | var ignored []string |
| 275 | for _, a := range config.Arguments() { |
| 276 | if !inList(a, specialTargets) { |
| 277 | ignored = append(ignored, a) |
| 278 | } |
| 279 | } |
| 280 | if len(ignored) > 0 { |
| 281 | ctx.Printf("ignoring arguments %q", ignored) |
| 282 | } |
| 283 | } |
| 284 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 285 | if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { |
Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 286 | logArgsOtherThan("clean", "clobber") |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 287 | clean(ctx, config) |
| 288 | return |
| 289 | } |
| 290 | |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 291 | defer waitForDist(ctx) |
| 292 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 293 | // checkProblematicFiles aborts the build if Android.mk or CleanSpec.mk are found at the root of the tree. |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 294 | checkProblematicFiles(ctx) |
| 295 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 296 | checkRAM(ctx, config) |
| 297 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 298 | SetupOutDir(ctx, config) |
| 299 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 300 | // checkCaseSensitivity issues a warning if a case-insensitive file system is being used. |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 301 | checkCaseSensitivity(ctx, config) |
| 302 | |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 303 | SetupPath(ctx, config) |
| 304 | |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 305 | what := evaluateWhatToRun(config, ctx.Verboseln) |
Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 306 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 307 | if config.StartGoma() { |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 308 | startGoma(ctx, config) |
| 309 | } |
| 310 | |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 311 | rbeCh := make(chan bool) |
Colin Cross | e7151f9 | 2023-11-28 14:18:12 -0800 | [diff] [blame] | 312 | var rbePanic any |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 313 | if config.StartRBE() { |
Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 314 | cleanupRBELogsDir(ctx, config) |
Colin Cross | e7151f9 | 2023-11-28 14:18:12 -0800 | [diff] [blame] | 315 | checkRBERequirements(ctx, config) |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 316 | go func() { |
Colin Cross | e7151f9 | 2023-11-28 14:18:12 -0800 | [diff] [blame] | 317 | defer func() { |
| 318 | rbePanic = recover() |
| 319 | close(rbeCh) |
| 320 | }() |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 321 | startRBE(ctx, config) |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 322 | }() |
Kousik Kumar | a1d8fa9 | 2022-03-18 02:50:31 -0400 | [diff] [blame] | 323 | defer DumpRBEMetrics(ctx, config, filepath.Join(config.LogsDir(), "rbe_metrics.pb")) |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 324 | } else { |
| 325 | close(rbeCh) |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 328 | if what&RunProductConfig != 0 { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 329 | runMakeProductConfig(ctx, config) |
| 330 | } |
| 331 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 332 | // Everything below here depends on product config. |
| 333 | |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 334 | if inList("installclean", config.Arguments()) || |
| 335 | inList("install-clean", config.Arguments()) { |
Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 336 | logArgsOtherThan("installclean", "install-clean") |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 337 | installClean(ctx, config) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 338 | ctx.Println("Deleted images and staging directories.") |
| 339 | return |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | if inList("dataclean", config.Arguments()) || |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 343 | inList("data-clean", config.Arguments()) { |
Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 344 | logArgsOtherThan("dataclean", "data-clean") |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 345 | dataClean(ctx, config) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 346 | ctx.Println("Deleted data files.") |
| 347 | return |
| 348 | } |
| 349 | |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 350 | // Still generate the kati suffix in soong-only builds because soong-only still uses kati for |
| 351 | // the packaging step. Also, the kati suffix is used for the combined ninja file. |
| 352 | genKatiSuffix(ctx, config) |
| 353 | |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 354 | if what&RunSoong != 0 { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 355 | runSoong(ctx, config) |
| 356 | } |
| 357 | |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 358 | if what&RunKati != 0 { |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 359 | runKatiCleanSpec(ctx, config) |
| 360 | runKatiBuild(ctx, config) |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 361 | runKatiPackage(ctx, config, false) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 362 | |
Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 363 | } else if what&RunKatiNinja != 0 { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 364 | // Load last Kati Suffix if it exists |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 365 | if katiSuffix, err := os.ReadFile(config.LastKatiSuffixFile()); err == nil { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 366 | ctx.Verboseln("Loaded previous kati config:", string(katiSuffix)) |
| 367 | config.SetKatiSuffix(string(katiSuffix)) |
| 368 | } |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 369 | } else if what&RunSoong != 0 { |
| 370 | runKatiPackage(ctx, config, true) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Cole Faust | c5bfbdd | 2025-01-08 13:05:40 -0800 | [diff] [blame] | 373 | os.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0666) // a+rw |
| 374 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 375 | // Write combined ninja file |
| 376 | createCombinedBuildNinjaFile(ctx, config) |
| 377 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 378 | distGzipFile(ctx, config, config.CombinedNinjaFile()) |
| 379 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 380 | if what&RunBuildTests != 0 { |
| 381 | testForDanglingRules(ctx, config) |
| 382 | } |
| 383 | |
Ramy Medhat | c8f6cc2 | 2023-03-31 09:50:34 -0400 | [diff] [blame] | 384 | <-rbeCh |
Colin Cross | e7151f9 | 2023-11-28 14:18:12 -0800 | [diff] [blame] | 385 | if rbePanic != nil { |
| 386 | // If there was a ctx.Fatal in startRBE, rethrow it. |
| 387 | panic(rbePanic) |
| 388 | } |
| 389 | |
Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 390 | if what&RunNinja != 0 { |
| 391 | if what&RunKati != 0 { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 392 | installCleanIfNecessary(ctx, config) |
| 393 | } |
Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 394 | runNinjaForBuild(ctx, config) |
Peter Collingbourne | b805c61 | 2024-03-14 21:59:57 -0700 | [diff] [blame] | 395 | updateBuildIdDir(ctx, config) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 396 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 397 | |
| 398 | if what&RunDistActions != 0 { |
| 399 | runDistActions(ctx, config) |
| 400 | } |
Taylor Santiago | 3c16e61 | 2024-05-30 14:41:31 -0700 | [diff] [blame] | 401 | done = true |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 402 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 403 | |
Peter Collingbourne | b805c61 | 2024-03-14 21:59:57 -0700 | [diff] [blame] | 404 | func updateBuildIdDir(ctx Context, config Config) { |
| 405 | ctx.BeginTrace(metrics.RunShutdownTool, "update_build_id_dir") |
| 406 | defer ctx.EndTrace() |
| 407 | |
| 408 | symbolsDir := filepath.Join(config.ProductOut(), "symbols") |
| 409 | if err := elf.UpdateBuildIdDir(symbolsDir); err != nil { |
| 410 | ctx.Printf("failed to update %s/.build-id: %v", symbolsDir, err) |
| 411 | } |
| 412 | } |
| 413 | |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 414 | func evaluateWhatToRun(config Config, verboseln func(v ...interface{})) int { |
| 415 | //evaluate what to run |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 416 | what := 0 |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 417 | if config.Checkbuild() { |
| 418 | what |= RunBuildTests |
| 419 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 420 | if !config.SkipConfig() { |
| 421 | what |= RunProductConfig |
| 422 | } else { |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 423 | verboseln("Skipping Config as requested") |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 424 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 425 | if !config.SkipSoong() { |
| 426 | what |= RunSoong |
| 427 | } else { |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 428 | verboseln("Skipping use of Soong as requested") |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 429 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 430 | if !config.SkipKati() { |
| 431 | what |= RunKati |
| 432 | } else { |
| 433 | verboseln("Skipping Kati as requested") |
| 434 | } |
| 435 | if !config.SkipKatiNinja() { |
| 436 | what |= RunKatiNinja |
| 437 | } else { |
| 438 | verboseln("Skipping use of Kati ninja as requested") |
| 439 | } |
| 440 | if !config.SkipNinja() { |
| 441 | what |= RunNinja |
| 442 | } else { |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 443 | verboseln("Skipping Ninja as requested") |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | if !config.SoongBuildInvocationNeeded() { |
| 447 | // This means that the output of soong_build is not needed and thus it would |
| 448 | // run unnecessarily. In addition, if this code wasn't there invocations |
Joe Onorato | 35f300d | 2024-10-21 15:02:44 -0700 | [diff] [blame] | 449 | // with only special-cased target names would result in |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 450 | // passing Ninja the empty target list and it would then build the default |
| 451 | // targets which is not what the user asked for. |
| 452 | what = what &^ RunNinja |
| 453 | what = what &^ RunKati |
| 454 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 455 | |
| 456 | if config.Dist() { |
| 457 | what |= RunDistActions |
| 458 | } |
| 459 | |
usta | 6fffdd5 | 2022-09-19 13:16:18 -0400 | [diff] [blame] | 460 | return what |
| 461 | } |
| 462 | |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 463 | var distWaitGroup sync.WaitGroup |
| 464 | |
| 465 | // waitForDist waits for all backgrounded distGzipFile and distFile writes to finish |
| 466 | func waitForDist(ctx Context) { |
| 467 | ctx.BeginTrace("soong_ui", "dist") |
| 468 | defer ctx.EndTrace() |
| 469 | |
| 470 | distWaitGroup.Wait() |
| 471 | } |
| 472 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 473 | // distGzipFile writes a compressed copy of src to the distDir if dist is enabled. Failures |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 474 | // are printed but non-fatal. Uses the distWaitGroup func for backgrounding (optimization). |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 475 | func distGzipFile(ctx Context, config Config, src string, subDirs ...string) { |
| 476 | if !config.Dist() { |
| 477 | return |
| 478 | } |
| 479 | |
| 480 | subDir := filepath.Join(subDirs...) |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 481 | destDir := filepath.Join(config.RealDistDir(), "soong_ui", subDir) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 482 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 483 | if err := os.MkdirAll(destDir, 0777); err != nil { // a+rwx |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 484 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 485 | } |
| 486 | |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 487 | distWaitGroup.Add(1) |
| 488 | go func() { |
| 489 | defer distWaitGroup.Done() |
| 490 | if err := gzipFileToDir(src, destDir); err != nil { |
| 491 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) |
| 492 | } |
| 493 | }() |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | // distFile writes a copy of src to the distDir if dist is enabled. Failures are printed but |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 497 | // non-fatal. Uses the distWaitGroup func for backgrounding (optimization). |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 498 | func distFile(ctx Context, config Config, src string, subDirs ...string) { |
| 499 | if !config.Dist() { |
| 500 | return |
| 501 | } |
| 502 | |
| 503 | subDir := filepath.Join(subDirs...) |
Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 504 | destDir := filepath.Join(config.RealDistDir(), "soong_ui", subDir) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 505 | |
Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 506 | if err := os.MkdirAll(destDir, 0777); err != nil { // a+rwx |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 507 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 508 | } |
| 509 | |
Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 510 | distWaitGroup.Add(1) |
| 511 | go func() { |
| 512 | defer distWaitGroup.Done() |
| 513 | if _, err := copyFile(src, filepath.Join(destDir, filepath.Base(src))); err != nil { |
| 514 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) |
| 515 | } |
| 516 | }() |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 517 | } |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 518 | |
| 519 | // Actions to run on every build where 'dist' is in the actions. |
| 520 | // Be careful, anything added here slows down EVERY CI build |
| 521 | func runDistActions(ctx Context, config Config) { |
| 522 | runStagingSnapshot(ctx, config) |
Joe Onorato | d6a2999 | 2024-12-06 12:55:41 -0800 | [diff] [blame] | 523 | runSourceInputs(ctx, config) |
Joe Onorato | 7f29a66 | 2023-02-23 15:47:06 -0800 | [diff] [blame] | 524 | } |