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