| 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 ( | 
| Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 18 | "io/ioutil" | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 19 | "os" | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "path/filepath" | 
| Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 21 | "sync" | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 22 | "text/template" | 
| Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 23 |  | 
|  | 24 | "android/soong/ui/metrics" | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 25 | ) | 
|  | 26 |  | 
| Jingwen Chen | cda22c9 | 2020-11-23 00:22:30 -0500 | [diff] [blame] | 27 | // SetupOutDir ensures the out directory exists, and has the proper files to | 
|  | 28 | // prevent kati from recursing into it. | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 29 | func SetupOutDir(ctx Context, config Config) { | 
|  | 30 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk")) | 
|  | 31 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk")) | 
| Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 32 |  | 
|  | 33 | // Potentially write a marker file for whether kati is enabled. This is used by soong_build to | 
|  | 34 | // potentially run the AndroidMk singleton and postinstall commands. | 
|  | 35 | // Note that the absence of the  file does not not preclude running Kati for product | 
|  | 36 | // configuration purposes. | 
|  | 37 | katiEnabledMarker := filepath.Join(config.SoongOutDir(), ".soong.kati_enabled") | 
|  | 38 | if config.SkipKatiNinja() { | 
|  | 39 | os.Remove(katiEnabledMarker) | 
|  | 40 | // Note that we can not remove the file for SkipKati builds yet -- some continuous builds | 
|  | 41 | // --skip-make builds rely on kati targets being defined. | 
|  | 42 | } else if !config.SkipKati() { | 
|  | 43 | ensureEmptyFileExists(ctx, katiEnabledMarker) | 
| Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 44 | } | 
| Anton Hansson | 17fc5a0 | 2021-06-18 16:37:14 +0100 | [diff] [blame] | 45 |  | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 46 | // The ninja_build file is used by our buildbots to understand that the output | 
|  | 47 | // can be parsed as ninja output. | 
|  | 48 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build")) | 
| Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 49 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir")) | 
| Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 50 |  | 
|  | 51 | if buildDateTimeFile, ok := config.environ.Get("BUILD_DATETIME_FILE"); ok { | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 52 | err := ioutil.WriteFile(buildDateTimeFile, []byte(config.buildDateTime), 0666) // a+rw | 
| Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 53 | if err != nil { | 
|  | 54 | ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err) | 
|  | 55 | } | 
|  | 56 | } else { | 
|  | 57 | ctx.Fatalln("Missing BUILD_DATETIME_FILE") | 
|  | 58 | } | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
|  | 61 | var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(` | 
|  | 62 | builddir = {{.OutDir}} | 
| Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 63 | {{if .UseRemoteBuild }}pool local_pool | 
| Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 64 | depth = {{.Parallel}} | 
| Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 65 | {{end -}} | 
|  | 66 | pool highmem_pool | 
|  | 67 | depth = {{.HighmemParallel}} | 
| Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 68 | {{if and (not .SkipKatiNinja) .HasKatiSuffix}}subninja {{.KatiBuildNinjaFile}} | 
| Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 69 | subninja {{.KatiPackageNinjaFile}} | 
| Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 70 | {{end -}} | 
| Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 71 | subninja {{.SoongNinjaFile}} | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 72 | `)) | 
|  | 73 |  | 
|  | 74 | func createCombinedBuildNinjaFile(ctx Context, config Config) { | 
| Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 75 | // If we're in SkipKati mode but want to run kati ninja, skip creating this file if it already exists | 
|  | 76 | if config.SkipKati() && !config.SkipKatiNinja() { | 
| Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 77 | if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) { | 
|  | 78 | return | 
|  | 79 | } | 
|  | 80 | } | 
|  | 81 |  | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 82 | file, err := os.Create(config.CombinedNinjaFile()) | 
|  | 83 | if err != nil { | 
|  | 84 | ctx.Fatalln("Failed to create combined ninja file:", err) | 
|  | 85 | } | 
|  | 86 | defer file.Close() | 
|  | 87 |  | 
|  | 88 | if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil { | 
|  | 89 | ctx.Fatalln("Failed to write combined ninja file:", err) | 
|  | 90 | } | 
|  | 91 | } | 
|  | 92 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 93 | // These are bitmasks which can be used to check whether various flags are set e.g. whether to use Bazel. | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 94 | const ( | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 95 | _ = iota | 
|  | 96 | // Whether to run the kati config step. | 
|  | 97 | RunProductConfig = 1 << iota | 
|  | 98 | // Whether to run soong to generate a ninja file. | 
|  | 99 | RunSoong = 1 << iota | 
|  | 100 | // Whether to run kati to generate a ninja file. | 
|  | 101 | RunKati = 1 << iota | 
| Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 102 | // Whether to include the kati-generated ninja file in the combined ninja. | 
|  | 103 | RunKatiNinja = 1 << iota | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 104 | // Whether to run ninja on the combined ninja. | 
|  | 105 | RunNinja = 1 << iota | 
|  | 106 | // Whether to run bazel on the combined ninja. | 
| Chris Parsons | 19ab9a4 | 2022-08-30 13:15:04 -0400 | [diff] [blame] | 107 | RunBazel      = 1 << iota | 
|  | 108 | RunBuildTests = 1 << iota | 
|  | 109 | RunAll        = RunProductConfig | RunSoong | RunKati | RunKatiNinja | RunNinja | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 110 | ) | 
|  | 111 |  | 
| Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 112 | // checkBazelMode fails the build if there are conflicting arguments for which bazel | 
|  | 113 | // build mode to use. | 
|  | 114 | func checkBazelMode(ctx Context, config Config) { | 
| Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 115 | if config.Environment().IsEnvTrue("USE_BAZEL_ANALYSIS") { | 
| Chris Parsons | 1bb58da | 2022-08-30 13:37:57 -0400 | [diff] [blame] | 116 | ctx.Fatalln("USE_BAZEL_ANALYSIS is deprecated. Unset USE_BAZEL_ANALYSIS.\n" + | 
|  | 117 | "Use --bazel-mode-dev instead. For example: `m --bazel-mode-dev nothing`") | 
| Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 118 | } | 
|  | 119 | if config.bazelProdMode && config.bazelDevMode { | 
| Chris Parsons | 1bb58da | 2022-08-30 13:37:57 -0400 | [diff] [blame] | 120 | ctx.Fatalln("Conflicting bazel mode.\n" + | 
| Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 121 | "Do not specify both --bazel-mode and --bazel-mode-dev") | 
|  | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 125 | // 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] | 126 | func checkProblematicFiles(ctx Context) { | 
|  | 127 | files := []string{"Android.mk", "CleanSpec.mk"} | 
|  | 128 | for _, file := range files { | 
|  | 129 | if _, err := os.Stat(file); !os.IsNotExist(err) { | 
|  | 130 | absolute := absPath(ctx, file) | 
|  | 131 | ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file) | 
|  | 132 | ctx.Fatalf("    rm %s\n", absolute) | 
|  | 133 | } | 
|  | 134 | } | 
|  | 135 | } | 
|  | 136 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 137 | // 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] | 138 | func checkCaseSensitivity(ctx Context, config Config) { | 
|  | 139 | outDir := config.OutDir() | 
|  | 140 | lowerCase := filepath.Join(outDir, "casecheck.txt") | 
|  | 141 | upperCase := filepath.Join(outDir, "CaseCheck.txt") | 
|  | 142 | lowerData := "a" | 
|  | 143 | upperData := "B" | 
|  | 144 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 145 | if err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0666); err != nil { // a+rw | 
| Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 146 | ctx.Fatalln("Failed to check case sensitivity:", err) | 
|  | 147 | } | 
|  | 148 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 149 | if err := ioutil.WriteFile(upperCase, []byte(upperData), 0666); err != nil { // a+rw | 
| Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 150 | ctx.Fatalln("Failed to check case sensitivity:", err) | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | res, err := ioutil.ReadFile(lowerCase) | 
|  | 154 | if err != nil { | 
|  | 155 | ctx.Fatalln("Failed to check case sensitivity:", err) | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | if string(res) != lowerData { | 
|  | 159 | ctx.Println("************************************************************") | 
|  | 160 | ctx.Println("You are building on a case-insensitive filesystem.") | 
|  | 161 | ctx.Println("Please move your source tree to a case-sensitive filesystem.") | 
|  | 162 | ctx.Println("************************************************************") | 
|  | 163 | ctx.Fatalln("Case-insensitive filesystems not supported") | 
|  | 164 | } | 
|  | 165 | } | 
|  | 166 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 167 | // help prints a help/usage message, via the build/make/help.sh script. | 
|  | 168 | func help(ctx Context, config Config) { | 
| Jeff Gaston | df4a081 | 2017-05-30 20:11:20 -0700 | [diff] [blame] | 169 | cmd := Command(ctx, config, "help.sh", "build/make/help.sh") | 
| Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 170 | cmd.Sandbox = dumpvarsSandbox | 
| Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 171 | cmd.RunAndPrintOrFatal() | 
| Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 172 | } | 
|  | 173 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 174 | // checkRAM warns if there probably isn't enough RAM to complete a build. | 
|  | 175 | func checkRAM(ctx Context, config Config) { | 
| Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 176 | if totalRAM := config.TotalRAM(); totalRAM != 0 { | 
|  | 177 | ram := float32(totalRAM) / (1024 * 1024 * 1024) | 
|  | 178 | ctx.Verbosef("Total RAM: %.3vGB", ram) | 
|  | 179 |  | 
|  | 180 | if ram <= 16 { | 
|  | 181 | ctx.Println("************************************************************") | 
|  | 182 | ctx.Printf("You are building on a machine with %.3vGB of RAM\n", ram) | 
|  | 183 | ctx.Println("") | 
|  | 184 | ctx.Println("The minimum required amount of free memory is around 16GB,") | 
|  | 185 | ctx.Println("and even with that, some configurations may not work.") | 
|  | 186 | ctx.Println("") | 
|  | 187 | ctx.Println("If you run into segfaults or other errors, try reducing your") | 
|  | 188 | ctx.Println("-j value.") | 
|  | 189 | ctx.Println("************************************************************") | 
|  | 190 | } else if ram <= float32(config.Parallel()) { | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 191 | // Want at least 1GB of RAM per job. | 
| Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 192 | ctx.Printf("Warning: high -j%d count compared to %.3vGB of RAM", config.Parallel(), ram) | 
|  | 193 | ctx.Println("If you run into segfaults or other errors, try a lower -j value") | 
|  | 194 | } | 
|  | 195 | } | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
| Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 198 | // Build the tree. Various flags in `config` govern which components of | 
|  | 199 | // the build to run. | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 200 | func Build(ctx Context, config Config) { | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 201 | ctx.Verboseln("Starting build with args:", config.Arguments()) | 
|  | 202 | ctx.Verboseln("Environment:", config.Environment().Environ()) | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 203 |  | 
| Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 204 | ctx.BeginTrace(metrics.Total, "total") | 
|  | 205 | defer ctx.EndTrace() | 
|  | 206 |  | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 207 | if inList("help", config.Arguments()) { | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 208 | help(ctx, config) | 
| Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 209 | return | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 210 | } | 
|  | 211 |  | 
| Jeff Gaston | 3615fe8 | 2017-05-24 13:14:34 -0700 | [diff] [blame] | 212 | // Make sure that no other Soong process is running with the same output directory | 
|  | 213 | buildLock := BecomeSingletonOrFail(ctx, config) | 
|  | 214 | defer buildLock.Unlock() | 
|  | 215 |  | 
| Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 216 | logArgsOtherThan := func(specialTargets ...string) { | 
|  | 217 | var ignored []string | 
|  | 218 | for _, a := range config.Arguments() { | 
|  | 219 | if !inList(a, specialTargets) { | 
|  | 220 | ignored = append(ignored, a) | 
|  | 221 | } | 
|  | 222 | } | 
|  | 223 | if len(ignored) > 0 { | 
|  | 224 | ctx.Printf("ignoring arguments %q", ignored) | 
|  | 225 | } | 
|  | 226 | } | 
|  | 227 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 228 | if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { | 
| Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 229 | logArgsOtherThan("clean", "clobber") | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 230 | clean(ctx, config) | 
|  | 231 | return | 
|  | 232 | } | 
|  | 233 |  | 
| Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 234 | defer waitForDist(ctx) | 
|  | 235 |  | 
| Chris Parsons | ef615e5 | 2022-08-18 22:04:11 -0400 | [diff] [blame] | 236 | checkBazelMode(ctx, config) | 
|  | 237 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 238 | // 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] | 239 | checkProblematicFiles(ctx) | 
|  | 240 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 241 | checkRAM(ctx, config) | 
|  | 242 |  | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 243 | SetupOutDir(ctx, config) | 
|  | 244 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 245 | // 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] | 246 | checkCaseSensitivity(ctx, config) | 
|  | 247 |  | 
| Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 248 | ensureEmptyDirectoriesExist(ctx, config.TempDir()) | 
|  | 249 |  | 
| Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 250 | SetupPath(ctx, config) | 
|  | 251 |  | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 252 | what := RunAll | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 253 | if config.Checkbuild() { | 
|  | 254 | what |= RunBuildTests | 
|  | 255 | } | 
| Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 256 | if config.SkipConfig() { | 
|  | 257 | ctx.Verboseln("Skipping Config as requested") | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 258 | what = what &^ RunProductConfig | 
| Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 259 | } | 
| Anton Hansson | 5e5c48b | 2020-11-27 12:35:20 +0000 | [diff] [blame] | 260 | if config.SkipKati() { | 
|  | 261 | ctx.Verboseln("Skipping Kati as requested") | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 262 | what = what &^ RunKati | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 263 | } | 
| Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 264 | if config.SkipKatiNinja() { | 
|  | 265 | ctx.Verboseln("Skipping use of Kati ninja as requested") | 
|  | 266 | what = what &^ RunKatiNinja | 
|  | 267 | } | 
| Lukacs T. Berki | cef87b6 | 2021-08-10 15:01:13 +0200 | [diff] [blame] | 268 | if config.SkipSoong() { | 
|  | 269 | ctx.Verboseln("Skipping use of Soong as requested") | 
|  | 270 | what = what &^ RunSoong | 
|  | 271 | } | 
|  | 272 |  | 
| Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 273 | if config.SkipNinja() { | 
|  | 274 | ctx.Verboseln("Skipping Ninja as requested") | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 275 | what = what &^ RunNinja | 
| Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Lukacs T. Berki | a1b9372 | 2021-09-02 17:23:06 +0200 | [diff] [blame] | 278 | if !config.SoongBuildInvocationNeeded() { | 
|  | 279 | // This means that the output of soong_build is not needed and thus it would | 
|  | 280 | // run unnecessarily. In addition, if this code wasn't there invocations | 
|  | 281 | // with only special-cased target names like "m bp2build" would result in | 
|  | 282 | // passing Ninja the empty target list and it would then build the default | 
|  | 283 | // targets which is not what the user asked for. | 
|  | 284 | what = what &^ RunNinja | 
|  | 285 | what = what &^ RunKati | 
|  | 286 | } | 
|  | 287 |  | 
| Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 288 | if config.StartGoma() { | 
| Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 289 | startGoma(ctx, config) | 
|  | 290 | } | 
|  | 291 |  | 
| Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 292 | if config.StartRBE() { | 
| Kousik Kumar | 4c180ad | 2022-05-27 07:48:37 -0400 | [diff] [blame] | 293 | cleanupRBELogsDir(ctx, config) | 
| Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 294 | startRBE(ctx, config) | 
| Kousik Kumar | a1d8fa9 | 2022-03-18 02:50:31 -0400 | [diff] [blame] | 295 | defer DumpRBEMetrics(ctx, config, filepath.Join(config.LogsDir(), "rbe_metrics.pb")) | 
| Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 298 | if what&RunProductConfig != 0 { | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 299 | runMakeProductConfig(ctx, config) | 
|  | 300 | } | 
|  | 301 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 302 | // Everything below here depends on product config. | 
|  | 303 |  | 
| Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 304 | if inList("installclean", config.Arguments()) || | 
|  | 305 | inList("install-clean", config.Arguments()) { | 
| Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 306 | logArgsOtherThan("installclean", "install-clean") | 
| Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 307 | installClean(ctx, config) | 
| Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 308 | ctx.Println("Deleted images and staging directories.") | 
|  | 309 | return | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
|  | 312 | if inList("dataclean", config.Arguments()) || | 
| Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 313 | inList("data-clean", config.Arguments()) { | 
| Usta Shrestha | 675564d | 2022-08-09 18:03:23 -0400 | [diff] [blame] | 314 | logArgsOtherThan("dataclean", "data-clean") | 
| Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 315 | dataClean(ctx, config) | 
| Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 316 | ctx.Println("Deleted data files.") | 
|  | 317 | return | 
|  | 318 | } | 
|  | 319 |  | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 320 | if what&RunSoong != 0 { | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 321 | runSoong(ctx, config) | 
|  | 322 | } | 
|  | 323 |  | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 324 | if what&RunKati != 0 { | 
| Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 325 | genKatiSuffix(ctx, config) | 
|  | 326 | runKatiCleanSpec(ctx, config) | 
|  | 327 | runKatiBuild(ctx, config) | 
| Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 328 | runKatiPackage(ctx, config) | 
| Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 329 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 330 | ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0666) // a+rw | 
| Anton Hansson | 0b55bdb | 2021-06-04 10:08:08 +0100 | [diff] [blame] | 331 | } else if what&RunKatiNinja != 0 { | 
| Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 332 | // Load last Kati Suffix if it exists | 
|  | 333 | if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil { | 
|  | 334 | ctx.Verboseln("Loaded previous kati config:", string(katiSuffix)) | 
|  | 335 | config.SetKatiSuffix(string(katiSuffix)) | 
|  | 336 | } | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 339 | // Write combined ninja file | 
|  | 340 | createCombinedBuildNinjaFile(ctx, config) | 
|  | 341 |  | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 342 | distGzipFile(ctx, config, config.CombinedNinjaFile()) | 
|  | 343 |  | 
| Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 344 | if what&RunBuildTests != 0 { | 
|  | 345 | testForDanglingRules(ctx, config) | 
|  | 346 | } | 
|  | 347 |  | 
| 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 | } | 
| Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 352 |  | 
| Lukacs T. Berki | d1e3f1f | 2021-03-16 08:55:23 +0100 | [diff] [blame] | 353 | runNinjaForBuild(ctx, config) | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 354 | } | 
| Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 355 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 356 | // Currently, using Bazel requires Kati and Soong to run first, so check whether to run Bazel last. | 
| Anton Hansson | 5a7861a | 2021-06-04 10:09:01 +0100 | [diff] [blame] | 357 | if what&RunBazel != 0 { | 
| Rupert Shuttleworth | 680387b | 2020-10-25 12:31:27 +0000 | [diff] [blame] | 358 | runBazel(ctx, config) | 
|  | 359 | } | 
| Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 360 | } | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 361 |  | 
| Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 362 | var distWaitGroup sync.WaitGroup | 
|  | 363 |  | 
|  | 364 | // waitForDist waits for all backgrounded distGzipFile and distFile writes to finish | 
|  | 365 | func waitForDist(ctx Context) { | 
|  | 366 | ctx.BeginTrace("soong_ui", "dist") | 
|  | 367 | defer ctx.EndTrace() | 
|  | 368 |  | 
|  | 369 | distWaitGroup.Wait() | 
|  | 370 | } | 
|  | 371 |  | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 372 | // 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] | 373 | // are printed but non-fatal. Uses the distWaitGroup func for backgrounding (optimization). | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 374 | func distGzipFile(ctx Context, config Config, src string, subDirs ...string) { | 
|  | 375 | if !config.Dist() { | 
|  | 376 | return | 
|  | 377 | } | 
|  | 378 |  | 
|  | 379 | subDir := filepath.Join(subDirs...) | 
| Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 380 | destDir := filepath.Join(config.RealDistDir(), "soong_ui", subDir) | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 381 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 382 | if err := os.MkdirAll(destDir, 0777); err != nil { // a+rwx | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 383 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 384 | } | 
|  | 385 |  | 
| Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 386 | distWaitGroup.Add(1) | 
|  | 387 | go func() { | 
|  | 388 | defer distWaitGroup.Done() | 
|  | 389 | if err := gzipFileToDir(src, destDir); err != nil { | 
|  | 390 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) | 
|  | 391 | } | 
|  | 392 | }() | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 393 | } | 
|  | 394 |  | 
|  | 395 | // 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] | 396 | // non-fatal. Uses the distWaitGroup func for backgrounding (optimization). | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 397 | func distFile(ctx Context, config Config, src string, subDirs ...string) { | 
|  | 398 | if !config.Dist() { | 
|  | 399 | return | 
|  | 400 | } | 
|  | 401 |  | 
|  | 402 | subDir := filepath.Join(subDirs...) | 
| Rupert Shuttleworth | 3c9f5ac | 2020-12-10 11:32:38 +0000 | [diff] [blame] | 403 | destDir := filepath.Join(config.RealDistDir(), "soong_ui", subDir) | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 404 |  | 
| Rupert Shuttleworth | eeb5caa | 2020-11-25 07:13:54 +0000 | [diff] [blame] | 405 | if err := os.MkdirAll(destDir, 0777); err != nil { // a+rwx | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 406 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 407 | } | 
|  | 408 |  | 
| Dan Willemsen | 80d7261 | 2022-04-20 21:45:00 -0700 | [diff] [blame] | 409 | distWaitGroup.Add(1) | 
|  | 410 | go func() { | 
|  | 411 | defer distWaitGroup.Done() | 
|  | 412 | if _, err := copyFile(src, filepath.Join(destDir, filepath.Base(src))); err != nil { | 
|  | 413 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) | 
|  | 414 | } | 
|  | 415 | }() | 
| Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 416 | } |