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