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" |
| 22 | ) |
| 23 | |
| 24 | // Ensures the out directory exists, and has the proper files to prevent kati |
| 25 | // from recursing into it. |
| 26 | func SetupOutDir(ctx Context, config Config) { |
| 27 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk")) |
| 28 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk")) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 29 | if !config.SkipMake() { |
| 30 | ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make")) |
| 31 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 32 | // The ninja_build file is used by our buildbots to understand that the output |
| 33 | // can be parsed as ninja output. |
| 34 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build")) |
Jeff Gaston | b64fc1c | 2017-08-04 12:30:12 -0700 | [diff] [blame] | 35 | ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir")) |
Colin Cross | 28f527c | 2019-11-26 16:19:04 -0800 | [diff] [blame^] | 36 | |
| 37 | if buildDateTimeFile, ok := config.environ.Get("BUILD_DATETIME_FILE"); ok { |
| 38 | err := ioutil.WriteFile(buildDateTimeFile, []byte(config.buildDateTime), 0777) |
| 39 | if err != nil { |
| 40 | ctx.Fatalln("Failed to write BUILD_DATETIME to file:", err) |
| 41 | } |
| 42 | } else { |
| 43 | ctx.Fatalln("Missing BUILD_DATETIME_FILE") |
| 44 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(` |
| 48 | builddir = {{.OutDir}} |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 49 | pool local_pool |
| 50 | depth = {{.Parallel}} |
| 51 | build _kati_always_build_: phony |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 52 | {{if .HasKatiSuffix}}subninja {{.KatiBuildNinjaFile}} |
| 53 | subninja {{.KatiPackageNinjaFile}} |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 54 | {{end -}} |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 55 | subninja {{.SoongNinjaFile}} |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 56 | `)) |
| 57 | |
| 58 | func createCombinedBuildNinjaFile(ctx Context, config Config) { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 59 | // If we're in SkipMake mode, skip creating this file if it already exists |
| 60 | if config.SkipMake() { |
| 61 | if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) { |
| 62 | return |
| 63 | } |
| 64 | } |
| 65 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 66 | file, err := os.Create(config.CombinedNinjaFile()) |
| 67 | if err != nil { |
| 68 | ctx.Fatalln("Failed to create combined ninja file:", err) |
| 69 | } |
| 70 | defer file.Close() |
| 71 | |
| 72 | if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil { |
| 73 | ctx.Fatalln("Failed to write combined ninja file:", err) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | const ( |
| 78 | BuildNone = iota |
| 79 | BuildProductConfig = 1 << iota |
| 80 | BuildSoong = 1 << iota |
| 81 | BuildKati = 1 << iota |
| 82 | BuildNinja = 1 << iota |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 83 | RunBuildTests = 1 << iota |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 84 | BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja |
| 85 | ) |
| 86 | |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 87 | func checkProblematicFiles(ctx Context) { |
| 88 | files := []string{"Android.mk", "CleanSpec.mk"} |
| 89 | for _, file := range files { |
| 90 | if _, err := os.Stat(file); !os.IsNotExist(err) { |
| 91 | absolute := absPath(ctx, file) |
| 92 | ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file) |
| 93 | ctx.Fatalf(" rm %s\n", absolute) |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 98 | func checkCaseSensitivity(ctx Context, config Config) { |
| 99 | outDir := config.OutDir() |
| 100 | lowerCase := filepath.Join(outDir, "casecheck.txt") |
| 101 | upperCase := filepath.Join(outDir, "CaseCheck.txt") |
| 102 | lowerData := "a" |
| 103 | upperData := "B" |
| 104 | |
| 105 | err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777) |
| 106 | if err != nil { |
| 107 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 108 | } |
| 109 | |
| 110 | err = ioutil.WriteFile(upperCase, []byte(upperData), 0777) |
| 111 | if err != nil { |
| 112 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 113 | } |
| 114 | |
| 115 | res, err := ioutil.ReadFile(lowerCase) |
| 116 | if err != nil { |
| 117 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 118 | } |
| 119 | |
| 120 | if string(res) != lowerData { |
| 121 | ctx.Println("************************************************************") |
| 122 | ctx.Println("You are building on a case-insensitive filesystem.") |
| 123 | ctx.Println("Please move your source tree to a case-sensitive filesystem.") |
| 124 | ctx.Println("************************************************************") |
| 125 | ctx.Fatalln("Case-insensitive filesystems not supported") |
| 126 | } |
| 127 | } |
| 128 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 129 | func help(ctx Context, config Config, what int) { |
Jeff Gaston | df4a081 | 2017-05-30 20:11:20 -0700 | [diff] [blame] | 130 | cmd := Command(ctx, config, "help.sh", "build/make/help.sh") |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 131 | cmd.Sandbox = dumpvarsSandbox |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 132 | cmd.RunAndPrintOrFatal() |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 135 | // Build the tree. The 'what' argument can be used to chose which components of |
| 136 | // the build to run. |
| 137 | func Build(ctx Context, config Config, what int) { |
| 138 | ctx.Verboseln("Starting build with args:", config.Arguments()) |
| 139 | ctx.Verboseln("Environment:", config.Environment().Environ()) |
| 140 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 141 | if config.SkipMake() { |
| 142 | ctx.Verboseln("Skipping Make/Kati as requested") |
| 143 | what = what & (BuildSoong | BuildNinja) |
| 144 | } |
| 145 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 146 | if inList("help", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 147 | help(ctx, config, what) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 148 | return |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 149 | } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 150 | clean(ctx, config, what) |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 151 | return |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Jeff Gaston | 3615fe8 | 2017-05-24 13:14:34 -0700 | [diff] [blame] | 154 | // Make sure that no other Soong process is running with the same output directory |
| 155 | buildLock := BecomeSingletonOrFail(ctx, config) |
| 156 | defer buildLock.Unlock() |
| 157 | |
Anton Hansson | ecf0f10 | 2018-09-19 22:14:17 +0100 | [diff] [blame] | 158 | checkProblematicFiles(ctx) |
| 159 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 160 | SetupOutDir(ctx, config) |
| 161 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 162 | checkCaseSensitivity(ctx, config) |
| 163 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 164 | ensureEmptyDirectoriesExist(ctx, config.TempDir()) |
| 165 | |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame] | 166 | SetupPath(ctx, config) |
| 167 | |
Yoshisato Yanagisawa | 2cb0e5d | 2019-01-10 10:14:16 +0900 | [diff] [blame] | 168 | if config.StartGoma() { |
| 169 | // Ensure start Goma compiler_proxy |
| 170 | startGoma(ctx, config) |
| 171 | } |
| 172 | |
Ramy Medhat | bbf2567 | 2019-07-17 12:30:04 +0000 | [diff] [blame] | 173 | if config.StartRBE() { |
| 174 | // Ensure RBE proxy is started |
| 175 | startRBE(ctx, config) |
| 176 | } |
| 177 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 178 | if what&BuildProductConfig != 0 { |
| 179 | // Run make for product config |
| 180 | runMakeProductConfig(ctx, config) |
| 181 | } |
| 182 | |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 183 | if inList("installclean", config.Arguments()) || |
| 184 | inList("install-clean", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 185 | installClean(ctx, config, what) |
| 186 | ctx.Println("Deleted images and staging directories.") |
| 187 | return |
Colin Cross | 806fd94 | 2019-05-03 13:35:58 -0700 | [diff] [blame] | 188 | } else if inList("dataclean", config.Arguments()) || |
| 189 | inList("data-clean", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 190 | dataClean(ctx, config, what) |
| 191 | ctx.Println("Deleted data files.") |
| 192 | return |
| 193 | } |
| 194 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 195 | if what&BuildSoong != 0 { |
| 196 | // Run Soong |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 197 | runSoong(ctx, config) |
| 198 | } |
| 199 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 200 | if what&BuildKati != 0 { |
| 201 | // Run ckati |
Dan Willemsen | 2997123 | 2018-09-26 14:58:30 -0700 | [diff] [blame] | 202 | genKatiSuffix(ctx, config) |
| 203 | runKatiCleanSpec(ctx, config) |
| 204 | runKatiBuild(ctx, config) |
Dan Willemsen | fb1271a | 2018-09-26 15:00:42 -0700 | [diff] [blame] | 205 | runKatiPackage(ctx, config) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 206 | |
| 207 | ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777) |
| 208 | } else { |
| 209 | // Load last Kati Suffix if it exists |
| 210 | if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil { |
| 211 | ctx.Verboseln("Loaded previous kati config:", string(katiSuffix)) |
| 212 | config.SetKatiSuffix(string(katiSuffix)) |
| 213 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 216 | // Write combined ninja file |
| 217 | createCombinedBuildNinjaFile(ctx, config) |
| 218 | |
| 219 | if what&RunBuildTests != 0 { |
| 220 | testForDanglingRules(ctx, config) |
| 221 | } |
| 222 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 223 | if what&BuildNinja != 0 { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 224 | if !config.SkipMake() { |
| 225 | installCleanIfNecessary(ctx, config) |
| 226 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 227 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 228 | // Run ninja |
| 229 | runNinja(ctx, config) |
| 230 | } |
| 231 | } |