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" |
| 21 | "text/template" |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 22 | |
| 23 | "android/soong/ui/metrics" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // Ensures the out directory exists, and has the proper files to prevent kati |
| 27 | // from recursing into it. |
| 28 | func SetupOutDir(ctx Context, config Config) { |
| 29 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk")) |
| 30 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk")) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 31 | if !config.SkipMake() { |
| 32 | ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make")) |
| 33 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 34 | // The ninja_build file is used by our buildbots to understand that the output |
| 35 | // can be parsed as ninja output. |
| 36 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 37 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir")) |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame] | 38 | |
| 39 | if buildDateTimeFile, ok := config.environ.Get("BUILD_DATETIME_FILE"); ok { |
| 40 | err := ioutil.WriteFile(buildDateTimeFile, []byte(config.buildDateTime), 0777) |
| 41 | if err != nil { |
| 42 | ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err) |
| 43 | } |
| 44 | } else { |
| 45 | ctx.Fatalln("Missing BUILD_DATETIME_FILE") |
| 46 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(` |
| 50 | builddir = {{.OutDir}} |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 51 | {{if .UseRemoteBuild }}pool local_pool |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 52 | depth = {{.Parallel}} |
Colin Cross | 8b8bec3 | 2019-11-15 13:18:43 -0800 | [diff] [blame] | 53 | {{end -}} |
| 54 | pool highmem_pool |
| 55 | depth = {{.HighmemParallel}} |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 56 | {{if .HasKatiSuffix}}subninja {{.KatiBuildNinjaFile}} |
| 57 | subninja {{.KatiPackageNinjaFile}} |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 58 | {{end -}} |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 59 | subninja {{.SoongNinjaFile}} |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 60 | `)) |
| 61 | |
| 62 | func createCombinedBuildNinjaFile(ctx Context, config Config) { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 63 | // If we're in SkipMake mode, skip creating this file if it already exists |
| 64 | if config.SkipMake() { |
| 65 | if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) { |
| 66 | return |
| 67 | } |
| 68 | } |
| 69 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 70 | file, err := os.Create(config.CombinedNinjaFile()) |
| 71 | if err != nil { |
| 72 | ctx.Fatalln("Failed to create combined ninja file:", err) |
| 73 | } |
| 74 | defer file.Close() |
| 75 | |
| 76 | if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil { |
| 77 | ctx.Fatalln("Failed to write combined ninja file:", err) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | const ( |
| 82 | BuildNone = iota |
| 83 | BuildProductConfig = 1 << iota |
| 84 | BuildSoong = 1 << iota |
| 85 | BuildKati = 1 << iota |
| 86 | BuildNinja = 1 << iota |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 87 | RunBuildTests = 1 << iota |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 88 | BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja |
| 89 | ) |
| 90 | |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 91 | func checkProblematicFiles(ctx Context) { |
| 92 | files := []string{"Android.mk", "CleanSpec.mk"} |
| 93 | for _, file := range files { |
| 94 | if _, err := os.Stat(file); !os.IsNotExist(err) { |
| 95 | absolute := absPath(ctx, file) |
| 96 | ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file) |
| 97 | ctx.Fatalf(" rm %s\n", absolute) |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 102 | func checkCaseSensitivity(ctx Context, config Config) { |
| 103 | outDir := config.OutDir() |
| 104 | lowerCase := filepath.Join(outDir, "casecheck.txt") |
| 105 | upperCase := filepath.Join(outDir, "CaseCheck.txt") |
| 106 | lowerData := "a" |
| 107 | upperData := "B" |
| 108 | |
| 109 | err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777) |
| 110 | if err != nil { |
| 111 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 112 | } |
| 113 | |
| 114 | err = ioutil.WriteFile(upperCase, []byte(upperData), 0777) |
| 115 | if err != nil { |
| 116 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 117 | } |
| 118 | |
| 119 | res, err := ioutil.ReadFile(lowerCase) |
| 120 | if err != nil { |
| 121 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 122 | } |
| 123 | |
| 124 | if string(res) != lowerData { |
| 125 | ctx.Println("************************************************************") |
| 126 | ctx.Println("You are building on a case-insensitive filesystem.") |
| 127 | ctx.Println("Please move your source tree to a case-sensitive filesystem.") |
| 128 | ctx.Println("************************************************************") |
| 129 | ctx.Fatalln("Case-insensitive filesystems not supported") |
| 130 | } |
| 131 | } |
| 132 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 133 | func help(ctx Context, config Config, what int) { |
Jeff Gaston | df4a081 | 2017-05-30 20:11:20 -0700 | [diff] [blame] | 134 | cmd := Command(ctx, config, "help.sh", "build/make/help.sh") |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 135 | cmd.Sandbox = dumpvarsSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 136 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 139 | // Build the tree. The 'what' argument can be used to chose which components of |
| 140 | // the build to run. |
| 141 | func Build(ctx Context, config Config, what int) { |
| 142 | ctx.Verboseln("Starting build with args:", config.Arguments()) |
| 143 | ctx.Verboseln("Environment:", config.Environment().Environ()) |
Dan Willemsen | 570a292 | 2020-05-26 23:02:29 -0700 | [diff] [blame] | 144 | |
| 145 | if totalRAM := config.TotalRAM(); totalRAM != 0 { |
| 146 | ram := float32(totalRAM) / (1024 * 1024 * 1024) |
| 147 | ctx.Verbosef("Total RAM: %.3vGB", ram) |
| 148 | |
| 149 | if ram <= 16 { |
| 150 | ctx.Println("************************************************************") |
| 151 | ctx.Printf("You are building on a machine with %.3vGB of RAM\n", ram) |
| 152 | ctx.Println("") |
| 153 | ctx.Println("The minimum required amount of free memory is around 16GB,") |
| 154 | ctx.Println("and even with that, some configurations may not work.") |
| 155 | ctx.Println("") |
| 156 | ctx.Println("If you run into segfaults or other errors, try reducing your") |
| 157 | ctx.Println("-j value.") |
| 158 | ctx.Println("************************************************************") |
| 159 | } else if ram <= float32(config.Parallel()) { |
| 160 | ctx.Printf("Warning: high -j%d count compared to %.3vGB of RAM", config.Parallel(), ram) |
| 161 | ctx.Println("If you run into segfaults or other errors, try a lower -j value") |
| 162 | } |
| 163 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 164 | |
Colin Cross | 74cda72 | 2020-01-16 15:25:50 -0800 | [diff] [blame] | 165 | ctx.BeginTrace(metrics.Total, "total") |
| 166 | defer ctx.EndTrace() |
| 167 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 168 | if config.SkipMake() { |
| 169 | ctx.Verboseln("Skipping Make/Kati as requested") |
| 170 | what = what & (BuildSoong | BuildNinja) |
| 171 | } |
| 172 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 173 | if inList("help", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 174 | help(ctx, config, what) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 175 | return |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 176 | } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 177 | clean(ctx, config, what) |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 178 | return |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Jeff Gaston | 3615fe8 | 2017-05-24 13:14:34 -0700 | [diff] [blame] | 181 | // Make sure that no other Soong process is running with the same output directory |
| 182 | buildLock := BecomeSingletonOrFail(ctx, config) |
| 183 | defer buildLock.Unlock() |
| 184 | |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 185 | checkProblematicFiles(ctx) |
| 186 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 187 | SetupOutDir(ctx, config) |
| 188 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 189 | checkCaseSensitivity(ctx, config) |
| 190 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 191 | ensureEmptyDirectoriesExist(ctx, config.TempDir()) |
| 192 | |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 193 | SetupPath(ctx, config) |
| 194 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 195 | if config.StartGoma() { |
| 196 | // Ensure start Goma compiler_proxy |
| 197 | startGoma(ctx, config) |
| 198 | } |
| 199 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 200 | if config.StartRBE() { |
| 201 | // Ensure RBE proxy is started |
| 202 | startRBE(ctx, config) |
| 203 | } |
| 204 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 205 | if what&BuildProductConfig != 0 { |
| 206 | // Run make for product config |
| 207 | runMakeProductConfig(ctx, config) |
| 208 | } |
| 209 | |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 210 | if inList("installclean", config.Arguments()) || |
| 211 | inList("install-clean", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 212 | installClean(ctx, config, what) |
| 213 | ctx.Println("Deleted images and staging directories.") |
| 214 | return |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 215 | } else if inList("dataclean", config.Arguments()) || |
| 216 | inList("data-clean", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 217 | dataClean(ctx, config, what) |
| 218 | ctx.Println("Deleted data files.") |
| 219 | return |
| 220 | } |
| 221 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 222 | if what&BuildSoong != 0 { |
| 223 | // Run Soong |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 224 | runSoong(ctx, config) |
| 225 | } |
| 226 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 227 | if what&BuildKati != 0 { |
| 228 | // Run ckati |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 229 | genKatiSuffix(ctx, config) |
| 230 | runKatiCleanSpec(ctx, config) |
| 231 | runKatiBuild(ctx, config) |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 232 | runKatiPackage(ctx, config) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 233 | |
| 234 | ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777) |
| 235 | } else { |
| 236 | // Load last Kati Suffix if it exists |
| 237 | if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil { |
| 238 | ctx.Verboseln("Loaded previous kati config:", string(katiSuffix)) |
| 239 | config.SetKatiSuffix(string(katiSuffix)) |
| 240 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 243 | // Write combined ninja file |
| 244 | createCombinedBuildNinjaFile(ctx, config) |
| 245 | |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 246 | distGzipFile(ctx, config, config.CombinedNinjaFile()) |
| 247 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 248 | if what&RunBuildTests != 0 { |
| 249 | testForDanglingRules(ctx, config) |
| 250 | } |
| 251 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 252 | if what&BuildNinja != 0 { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 253 | if !config.SkipMake() { |
| 254 | installCleanIfNecessary(ctx, config) |
| 255 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 256 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 257 | // Run ninja |
| 258 | runNinja(ctx, config) |
| 259 | } |
| 260 | } |
Colin Cross | 8ba7d47 | 2020-06-25 11:27:52 -0700 | [diff] [blame] | 261 | |
| 262 | // distGzipFile writes a compressed copy of src to the distDir if dist is enabled. Failures |
| 263 | // are printed but non-fatal. |
| 264 | func distGzipFile(ctx Context, config Config, src string, subDirs ...string) { |
| 265 | if !config.Dist() { |
| 266 | return |
| 267 | } |
| 268 | |
| 269 | subDir := filepath.Join(subDirs...) |
| 270 | destDir := filepath.Join(config.DistDir(), "soong_ui", subDir) |
| 271 | |
| 272 | err := os.MkdirAll(destDir, 0777) |
| 273 | if err != nil { |
| 274 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) |
| 275 | |
| 276 | } |
| 277 | |
| 278 | err = gzipFileToDir(src, destDir) |
| 279 | if err != nil { |
| 280 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | // distFile writes a copy of src to the distDir if dist is enabled. Failures are printed but |
| 285 | // non-fatal. |
| 286 | func distFile(ctx Context, config Config, src string, subDirs ...string) { |
| 287 | if !config.Dist() { |
| 288 | return |
| 289 | } |
| 290 | |
| 291 | subDir := filepath.Join(subDirs...) |
| 292 | destDir := filepath.Join(config.DistDir(), "soong_ui", subDir) |
| 293 | |
| 294 | err := os.MkdirAll(destDir, 0777) |
| 295 | if err != nil { |
| 296 | ctx.Printf("failed to mkdir %s: %s", destDir, err.Error()) |
| 297 | |
| 298 | } |
| 299 | |
| 300 | _, err = copyFile(src, filepath.Join(destDir, filepath.Base(src))) |
| 301 | if err != nil { |
| 302 | ctx.Printf("failed to dist %s: %s", filepath.Base(src), err.Error()) |
| 303 | } |
| 304 | } |