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")) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(` |
| 39 | builddir = {{.OutDir}} |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 40 | {{if .HasKatiSuffix}}include {{.KatiNinjaFile}} |
| 41 | {{end -}} |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 42 | include {{.SoongNinjaFile}} |
| 43 | build {{.CombinedNinjaFile}}: phony {{.SoongNinjaFile}} |
| 44 | `)) |
| 45 | |
| 46 | func createCombinedBuildNinjaFile(ctx Context, config Config) { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 47 | // If we're in SkipMake mode, skip creating this file if it already exists |
| 48 | if config.SkipMake() { |
| 49 | if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) { |
| 50 | return |
| 51 | } |
| 52 | } |
| 53 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 54 | file, err := os.Create(config.CombinedNinjaFile()) |
| 55 | if err != nil { |
| 56 | ctx.Fatalln("Failed to create combined ninja file:", err) |
| 57 | } |
| 58 | defer file.Close() |
| 59 | |
| 60 | if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil { |
| 61 | ctx.Fatalln("Failed to write combined ninja file:", err) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | const ( |
| 66 | BuildNone = iota |
| 67 | BuildProductConfig = 1 << iota |
| 68 | BuildSoong = 1 << iota |
| 69 | BuildKati = 1 << iota |
| 70 | BuildNinja = 1 << iota |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 71 | RunBuildTests = 1 << iota |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 72 | BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja |
| 73 | ) |
| 74 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 75 | func checkCaseSensitivity(ctx Context, config Config) { |
| 76 | outDir := config.OutDir() |
| 77 | lowerCase := filepath.Join(outDir, "casecheck.txt") |
| 78 | upperCase := filepath.Join(outDir, "CaseCheck.txt") |
| 79 | lowerData := "a" |
| 80 | upperData := "B" |
| 81 | |
| 82 | err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777) |
| 83 | if err != nil { |
| 84 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 85 | } |
| 86 | |
| 87 | err = ioutil.WriteFile(upperCase, []byte(upperData), 0777) |
| 88 | if err != nil { |
| 89 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 90 | } |
| 91 | |
| 92 | res, err := ioutil.ReadFile(lowerCase) |
| 93 | if err != nil { |
| 94 | ctx.Fatalln("Failed to check case sensitivity:", err) |
| 95 | } |
| 96 | |
| 97 | if string(res) != lowerData { |
| 98 | ctx.Println("************************************************************") |
| 99 | ctx.Println("You are building on a case-insensitive filesystem.") |
| 100 | ctx.Println("Please move your source tree to a case-sensitive filesystem.") |
| 101 | ctx.Println("************************************************************") |
| 102 | ctx.Fatalln("Case-insensitive filesystems not supported") |
| 103 | } |
| 104 | } |
| 105 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 106 | func help(ctx Context, config Config, what int) { |
Jeff Gaston | df4a081 | 2017-05-30 20:11:20 -0700 | [diff] [blame] | 107 | cmd := Command(ctx, config, "help.sh", "build/make/help.sh") |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 108 | cmd.Sandbox = dumpvarsSandbox |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 109 | cmd.Stdout = ctx.Stdout() |
| 110 | cmd.Stderr = ctx.Stderr() |
| 111 | cmd.RunOrFatal() |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 114 | // Build the tree. The 'what' argument can be used to chose which components of |
| 115 | // the build to run. |
| 116 | func Build(ctx Context, config Config, what int) { |
| 117 | ctx.Verboseln("Starting build with args:", config.Arguments()) |
| 118 | ctx.Verboseln("Environment:", config.Environment().Environ()) |
| 119 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 120 | if config.SkipMake() { |
| 121 | ctx.Verboseln("Skipping Make/Kati as requested") |
| 122 | what = what & (BuildSoong | BuildNinja) |
| 123 | } |
| 124 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 125 | if inList("help", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 126 | help(ctx, config, what) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 127 | return |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 128 | } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 129 | clean(ctx, config, what) |
Dan Willemsen | 0b73b4b | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 130 | return |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jeff Gaston | 3615fe8 | 2017-05-24 13:14:34 -0700 | [diff] [blame] | 133 | // Make sure that no other Soong process is running with the same output directory |
| 134 | buildLock := BecomeSingletonOrFail(ctx, config) |
| 135 | defer buildLock.Unlock() |
| 136 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 137 | SetupOutDir(ctx, config) |
| 138 | |
Dan Willemsen | db8457c | 2017-05-12 16:38:17 -0700 | [diff] [blame] | 139 | checkCaseSensitivity(ctx, config) |
| 140 | |
Jeff Gaston | efc1b41 | 2017-03-29 17:29:06 -0700 | [diff] [blame] | 141 | ensureEmptyDirectoriesExist(ctx, config.TempDir()) |
| 142 | |
Dan Willemsen | 1849011 | 2018-05-25 16:30:04 -0700 | [diff] [blame^] | 143 | SetupPath(ctx, config) |
| 144 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 145 | if what&BuildProductConfig != 0 { |
| 146 | // Run make for product config |
| 147 | runMakeProductConfig(ctx, config) |
| 148 | } |
| 149 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 150 | if inList("installclean", config.Arguments()) { |
| 151 | installClean(ctx, config, what) |
| 152 | ctx.Println("Deleted images and staging directories.") |
| 153 | return |
| 154 | } else if inList("dataclean", config.Arguments()) { |
| 155 | dataClean(ctx, config, what) |
| 156 | ctx.Println("Deleted data files.") |
| 157 | return |
| 158 | } |
| 159 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 160 | if what&BuildSoong != 0 { |
| 161 | // Run Soong |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 162 | runSoong(ctx, config) |
| 163 | } |
| 164 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 165 | if what&BuildKati != 0 { |
| 166 | // Run ckati |
| 167 | runKati(ctx, config) |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 168 | |
| 169 | ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777) |
| 170 | } else { |
| 171 | // Load last Kati Suffix if it exists |
| 172 | if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil { |
| 173 | ctx.Verboseln("Loaded previous kati config:", string(katiSuffix)) |
| 174 | config.SetKatiSuffix(string(katiSuffix)) |
| 175 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Colin Cross | 3719349 | 2017-11-16 17:55:00 -0800 | [diff] [blame] | 178 | // Write combined ninja file |
| 179 | createCombinedBuildNinjaFile(ctx, config) |
| 180 | |
| 181 | if what&RunBuildTests != 0 { |
| 182 | testForDanglingRules(ctx, config) |
| 183 | } |
| 184 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 185 | if what&BuildNinja != 0 { |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 186 | if !config.SkipMake() { |
| 187 | installCleanIfNecessary(ctx, config) |
| 188 | } |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 189 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 190 | // Run ninja |
| 191 | runNinja(ctx, config) |
| 192 | } |
| 193 | } |