blob: de36dce9a677429c5f6f6b3dbf6244362e445727 [file] [log] [blame]
Dan Willemsen1e704462016-08-21 15:17:17 -07001// 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
15package build
16
17import (
Dan Willemsendb8457c2017-05-12 16:38:17 -070018 "io/ioutil"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "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.
26func SetupOutDir(ctx Context, config Config) {
27 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk"))
28 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk"))
Dan Willemsene0879fc2017-08-04 15:06:27 -070029 if !config.SkipMake() {
30 ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make"))
31 }
Dan Willemsen1e704462016-08-21 15:17:17 -070032 // 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 Gastonb64fc1c2017-08-04 12:30:12 -070035 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir"))
Colin Cross28f527c2019-11-26 16:19:04 -080036
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 Willemsen1e704462016-08-21 15:17:17 -070045}
46
47var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
48builddir = {{.OutDir}}
Colin Cross8b8bec32019-11-15 13:18:43 -080049{{if .UseRemoteBuild }}pool local_pool
Dan Willemsen29971232018-09-26 14:58:30 -070050 depth = {{.Parallel}}
Colin Cross8b8bec32019-11-15 13:18:43 -080051{{end -}}
52pool highmem_pool
53 depth = {{.HighmemParallel}}
Dan Willemsen29971232018-09-26 14:58:30 -070054build _kati_always_build_: phony
Dan Willemsenfb1271a2018-09-26 15:00:42 -070055{{if .HasKatiSuffix}}subninja {{.KatiBuildNinjaFile}}
56subninja {{.KatiPackageNinjaFile}}
Dan Willemsene0879fc2017-08-04 15:06:27 -070057{{end -}}
Dan Willemsenfb1271a2018-09-26 15:00:42 -070058subninja {{.SoongNinjaFile}}
Dan Willemsen1e704462016-08-21 15:17:17 -070059`))
60
61func createCombinedBuildNinjaFile(ctx Context, config Config) {
Dan Willemsene0879fc2017-08-04 15:06:27 -070062 // If we're in SkipMake mode, skip creating this file if it already exists
63 if config.SkipMake() {
64 if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) {
65 return
66 }
67 }
68
Dan Willemsen1e704462016-08-21 15:17:17 -070069 file, err := os.Create(config.CombinedNinjaFile())
70 if err != nil {
71 ctx.Fatalln("Failed to create combined ninja file:", err)
72 }
73 defer file.Close()
74
75 if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil {
76 ctx.Fatalln("Failed to write combined ninja file:", err)
77 }
78}
79
80const (
81 BuildNone = iota
82 BuildProductConfig = 1 << iota
83 BuildSoong = 1 << iota
84 BuildKati = 1 << iota
85 BuildNinja = 1 << iota
Colin Cross37193492017-11-16 17:55:00 -080086 RunBuildTests = 1 << iota
Dan Willemsen1e704462016-08-21 15:17:17 -070087 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
88)
89
Anton Hanssonecf0f102018-09-19 22:14:17 +010090func checkProblematicFiles(ctx Context) {
91 files := []string{"Android.mk", "CleanSpec.mk"}
92 for _, file := range files {
93 if _, err := os.Stat(file); !os.IsNotExist(err) {
94 absolute := absPath(ctx, file)
95 ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file)
96 ctx.Fatalf(" rm %s\n", absolute)
97 }
98 }
99}
100
Dan Willemsendb8457c2017-05-12 16:38:17 -0700101func checkCaseSensitivity(ctx Context, config Config) {
102 outDir := config.OutDir()
103 lowerCase := filepath.Join(outDir, "casecheck.txt")
104 upperCase := filepath.Join(outDir, "CaseCheck.txt")
105 lowerData := "a"
106 upperData := "B"
107
108 err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
109 if err != nil {
110 ctx.Fatalln("Failed to check case sensitivity:", err)
111 }
112
113 err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
114 if err != nil {
115 ctx.Fatalln("Failed to check case sensitivity:", err)
116 }
117
118 res, err := ioutil.ReadFile(lowerCase)
119 if err != nil {
120 ctx.Fatalln("Failed to check case sensitivity:", err)
121 }
122
123 if string(res) != lowerData {
124 ctx.Println("************************************************************")
125 ctx.Println("You are building on a case-insensitive filesystem.")
126 ctx.Println("Please move your source tree to a case-sensitive filesystem.")
127 ctx.Println("************************************************************")
128 ctx.Fatalln("Case-insensitive filesystems not supported")
129 }
130}
131
Dan Willemsenf052f782017-05-18 15:29:04 -0700132func help(ctx Context, config Config, what int) {
Jeff Gastondf4a0812017-05-30 20:11:20 -0700133 cmd := Command(ctx, config, "help.sh", "build/make/help.sh")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700134 cmd.Sandbox = dumpvarsSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -0700135 cmd.RunAndPrintOrFatal()
Dan Willemsen02781d52017-05-12 19:28:13 -0700136}
137
Dan Willemsen1e704462016-08-21 15:17:17 -0700138// Build the tree. The 'what' argument can be used to chose which components of
139// the build to run.
140func Build(ctx Context, config Config, what int) {
141 ctx.Verboseln("Starting build with args:", config.Arguments())
142 ctx.Verboseln("Environment:", config.Environment().Environ())
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800143 ctx.Verbosef("Total RAM: %dGB", config.TotalRAM()/1024/1024/1024)
Dan Willemsen1e704462016-08-21 15:17:17 -0700144
Dan Willemsene0879fc2017-08-04 15:06:27 -0700145 if config.SkipMake() {
146 ctx.Verboseln("Skipping Make/Kati as requested")
147 what = what & (BuildSoong | BuildNinja)
148 }
149
Dan Willemsen1e704462016-08-21 15:17:17 -0700150 if inList("help", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700151 help(ctx, config, what)
Dan Willemsen1e704462016-08-21 15:17:17 -0700152 return
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700153 } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700154 clean(ctx, config, what)
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700155 return
Dan Willemsen1e704462016-08-21 15:17:17 -0700156 }
157
Jeff Gaston3615fe82017-05-24 13:14:34 -0700158 // Make sure that no other Soong process is running with the same output directory
159 buildLock := BecomeSingletonOrFail(ctx, config)
160 defer buildLock.Unlock()
161
Anton Hanssonecf0f102018-09-19 22:14:17 +0100162 checkProblematicFiles(ctx)
163
Dan Willemsen1e704462016-08-21 15:17:17 -0700164 SetupOutDir(ctx, config)
165
Dan Willemsendb8457c2017-05-12 16:38:17 -0700166 checkCaseSensitivity(ctx, config)
167
Jeff Gastonefc1b412017-03-29 17:29:06 -0700168 ensureEmptyDirectoriesExist(ctx, config.TempDir())
169
Dan Willemsen18490112018-05-25 16:30:04 -0700170 SetupPath(ctx, config)
171
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900172 if config.StartGoma() {
173 // Ensure start Goma compiler_proxy
174 startGoma(ctx, config)
175 }
176
Ramy Medhatbbf25672019-07-17 12:30:04 +0000177 if config.StartRBE() {
178 // Ensure RBE proxy is started
179 startRBE(ctx, config)
180 }
181
Dan Willemsen1e704462016-08-21 15:17:17 -0700182 if what&BuildProductConfig != 0 {
183 // Run make for product config
184 runMakeProductConfig(ctx, config)
185 }
186
Colin Cross806fd942019-05-03 13:35:58 -0700187 if inList("installclean", config.Arguments()) ||
188 inList("install-clean", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700189 installClean(ctx, config, what)
190 ctx.Println("Deleted images and staging directories.")
191 return
Colin Cross806fd942019-05-03 13:35:58 -0700192 } else if inList("dataclean", config.Arguments()) ||
193 inList("data-clean", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700194 dataClean(ctx, config, what)
195 ctx.Println("Deleted data files.")
196 return
197 }
198
Dan Willemsen1e704462016-08-21 15:17:17 -0700199 if what&BuildSoong != 0 {
200 // Run Soong
Dan Willemsen1e704462016-08-21 15:17:17 -0700201 runSoong(ctx, config)
202 }
203
Dan Willemsen1e704462016-08-21 15:17:17 -0700204 if what&BuildKati != 0 {
205 // Run ckati
Dan Willemsen29971232018-09-26 14:58:30 -0700206 genKatiSuffix(ctx, config)
207 runKatiCleanSpec(ctx, config)
208 runKatiBuild(ctx, config)
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700209 runKatiPackage(ctx, config)
Dan Willemsene0879fc2017-08-04 15:06:27 -0700210
211 ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777)
212 } else {
213 // Load last Kati Suffix if it exists
214 if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil {
215 ctx.Verboseln("Loaded previous kati config:", string(katiSuffix))
216 config.SetKatiSuffix(string(katiSuffix))
217 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700218 }
219
Colin Cross37193492017-11-16 17:55:00 -0800220 // Write combined ninja file
221 createCombinedBuildNinjaFile(ctx, config)
222
223 if what&RunBuildTests != 0 {
224 testForDanglingRules(ctx, config)
225 }
226
Dan Willemsen1e704462016-08-21 15:17:17 -0700227 if what&BuildNinja != 0 {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700228 if !config.SkipMake() {
229 installCleanIfNecessary(ctx, config)
230 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700231
Dan Willemsen1e704462016-08-21 15:17:17 -0700232 // Run ninja
233 runNinja(ctx, config)
234 }
235}