blob: 69ef003c289323a02d8907730f8ac7dd208542d5 [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"
Colin Cross74cda722020-01-16 15:25:50 -080022
23 "android/soong/ui/metrics"
Dan Willemsen1e704462016-08-21 15:17:17 -070024)
25
26// Ensures the out directory exists, and has the proper files to prevent kati
27// from recursing into it.
28func SetupOutDir(ctx Context, config Config) {
29 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk"))
30 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk"))
Dan Willemsene0879fc2017-08-04 15:06:27 -070031 if !config.SkipMake() {
32 ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make"))
33 }
Dan Willemsen1e704462016-08-21 15:17:17 -070034 // 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 Gastonb64fc1c2017-08-04 12:30:12 -070037 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), ".out-dir"))
Colin Cross28f527c2019-11-26 16:19:04 -080038
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 Willemsen1e704462016-08-21 15:17:17 -070047}
48
49var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
50builddir = {{.OutDir}}
Dan Willemsen29971232018-09-26 14:58:30 -070051pool local_pool
52 depth = {{.Parallel}}
53build _kati_always_build_: phony
Dan Willemsenfb1271a2018-09-26 15:00:42 -070054{{if .HasKatiSuffix}}subninja {{.KatiBuildNinjaFile}}
55subninja {{.KatiPackageNinjaFile}}
Dan Willemsene0879fc2017-08-04 15:06:27 -070056{{end -}}
Dan Willemsenfb1271a2018-09-26 15:00:42 -070057subninja {{.SoongNinjaFile}}
Dan Willemsen1e704462016-08-21 15:17:17 -070058`))
59
60func createCombinedBuildNinjaFile(ctx Context, config Config) {
Dan Willemsene0879fc2017-08-04 15:06:27 -070061 // If we're in SkipMake mode, skip creating this file if it already exists
62 if config.SkipMake() {
63 if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) {
64 return
65 }
66 }
67
Dan Willemsen1e704462016-08-21 15:17:17 -070068 file, err := os.Create(config.CombinedNinjaFile())
69 if err != nil {
70 ctx.Fatalln("Failed to create combined ninja file:", err)
71 }
72 defer file.Close()
73
74 if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil {
75 ctx.Fatalln("Failed to write combined ninja file:", err)
76 }
77}
78
79const (
80 BuildNone = iota
81 BuildProductConfig = 1 << iota
82 BuildSoong = 1 << iota
83 BuildKati = 1 << iota
84 BuildNinja = 1 << iota
Colin Cross37193492017-11-16 17:55:00 -080085 RunBuildTests = 1 << iota
Dan Willemsen1e704462016-08-21 15:17:17 -070086 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
87)
88
Anton Hanssonecf0f102018-09-19 22:14:17 +010089func checkProblematicFiles(ctx Context) {
90 files := []string{"Android.mk", "CleanSpec.mk"}
91 for _, file := range files {
92 if _, err := os.Stat(file); !os.IsNotExist(err) {
93 absolute := absPath(ctx, file)
94 ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file)
95 ctx.Fatalf(" rm %s\n", absolute)
96 }
97 }
98}
99
Dan Willemsendb8457c2017-05-12 16:38:17 -0700100func checkCaseSensitivity(ctx Context, config Config) {
101 outDir := config.OutDir()
102 lowerCase := filepath.Join(outDir, "casecheck.txt")
103 upperCase := filepath.Join(outDir, "CaseCheck.txt")
104 lowerData := "a"
105 upperData := "B"
106
107 err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
108 if err != nil {
109 ctx.Fatalln("Failed to check case sensitivity:", err)
110 }
111
112 err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
113 if err != nil {
114 ctx.Fatalln("Failed to check case sensitivity:", err)
115 }
116
117 res, err := ioutil.ReadFile(lowerCase)
118 if err != nil {
119 ctx.Fatalln("Failed to check case sensitivity:", err)
120 }
121
122 if string(res) != lowerData {
123 ctx.Println("************************************************************")
124 ctx.Println("You are building on a case-insensitive filesystem.")
125 ctx.Println("Please move your source tree to a case-sensitive filesystem.")
126 ctx.Println("************************************************************")
127 ctx.Fatalln("Case-insensitive filesystems not supported")
128 }
129}
130
Dan Willemsenf052f782017-05-18 15:29:04 -0700131func help(ctx Context, config Config, what int) {
Jeff Gastondf4a0812017-05-30 20:11:20 -0700132 cmd := Command(ctx, config, "help.sh", "build/make/help.sh")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700133 cmd.Sandbox = dumpvarsSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -0700134 cmd.RunAndPrintOrFatal()
Dan Willemsen02781d52017-05-12 19:28:13 -0700135}
136
Dan Willemsen1e704462016-08-21 15:17:17 -0700137// Build the tree. The 'what' argument can be used to chose which components of
138// the build to run.
139func Build(ctx Context, config Config, what int) {
140 ctx.Verboseln("Starting build with args:", config.Arguments())
141 ctx.Verboseln("Environment:", config.Environment().Environ())
Dan Willemsen2bb82d02019-12-27 09:35:42 -0800142 ctx.Verbosef("Total RAM: %dGB", config.TotalRAM()/1024/1024/1024)
Dan Willemsen1e704462016-08-21 15:17:17 -0700143
Colin Cross74cda722020-01-16 15:25:50 -0800144 ctx.BeginTrace(metrics.Total, "total")
145 defer ctx.EndTrace()
146
Dan Willemsene0879fc2017-08-04 15:06:27 -0700147 if config.SkipMake() {
148 ctx.Verboseln("Skipping Make/Kati as requested")
149 what = what & (BuildSoong | BuildNinja)
150 }
151
Dan Willemsen1e704462016-08-21 15:17:17 -0700152 if inList("help", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700153 help(ctx, config, what)
Dan Willemsen1e704462016-08-21 15:17:17 -0700154 return
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700155 } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700156 clean(ctx, config, what)
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700157 return
Dan Willemsen1e704462016-08-21 15:17:17 -0700158 }
159
Jeff Gaston3615fe82017-05-24 13:14:34 -0700160 // Make sure that no other Soong process is running with the same output directory
161 buildLock := BecomeSingletonOrFail(ctx, config)
162 defer buildLock.Unlock()
163
Anton Hanssonecf0f102018-09-19 22:14:17 +0100164 checkProblematicFiles(ctx)
165
Dan Willemsen1e704462016-08-21 15:17:17 -0700166 SetupOutDir(ctx, config)
167
Dan Willemsendb8457c2017-05-12 16:38:17 -0700168 checkCaseSensitivity(ctx, config)
169
Jeff Gastonefc1b412017-03-29 17:29:06 -0700170 ensureEmptyDirectoriesExist(ctx, config.TempDir())
171
Dan Willemsen18490112018-05-25 16:30:04 -0700172 SetupPath(ctx, config)
173
Yoshisato Yanagisawa2cb0e5d2019-01-10 10:14:16 +0900174 if config.StartGoma() {
175 // Ensure start Goma compiler_proxy
176 startGoma(ctx, config)
177 }
178
Ramy Medhatbbf25672019-07-17 12:30:04 +0000179 if config.StartRBE() {
180 // Ensure RBE proxy is started
181 startRBE(ctx, config)
182 }
183
Dan Willemsen1e704462016-08-21 15:17:17 -0700184 if what&BuildProductConfig != 0 {
185 // Run make for product config
186 runMakeProductConfig(ctx, config)
187 }
188
Colin Cross806fd942019-05-03 13:35:58 -0700189 if inList("installclean", config.Arguments()) ||
190 inList("install-clean", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700191 installClean(ctx, config, what)
192 ctx.Println("Deleted images and staging directories.")
193 return
Colin Cross806fd942019-05-03 13:35:58 -0700194 } else if inList("dataclean", config.Arguments()) ||
195 inList("data-clean", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700196 dataClean(ctx, config, what)
197 ctx.Println("Deleted data files.")
198 return
199 }
200
Dan Willemsen1e704462016-08-21 15:17:17 -0700201 if what&BuildSoong != 0 {
202 // Run Soong
Dan Willemsen1e704462016-08-21 15:17:17 -0700203 runSoong(ctx, config)
204 }
205
Dan Willemsen1e704462016-08-21 15:17:17 -0700206 if what&BuildKati != 0 {
207 // Run ckati
Dan Willemsen29971232018-09-26 14:58:30 -0700208 genKatiSuffix(ctx, config)
209 runKatiCleanSpec(ctx, config)
210 runKatiBuild(ctx, config)
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700211 runKatiPackage(ctx, config)
Dan Willemsene0879fc2017-08-04 15:06:27 -0700212
213 ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777)
214 } else {
215 // Load last Kati Suffix if it exists
216 if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil {
217 ctx.Verboseln("Loaded previous kati config:", string(katiSuffix))
218 config.SetKatiSuffix(string(katiSuffix))
219 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700220 }
221
Colin Cross37193492017-11-16 17:55:00 -0800222 // Write combined ninja file
223 createCombinedBuildNinjaFile(ctx, config)
224
225 if what&RunBuildTests != 0 {
226 testForDanglingRules(ctx, config)
227 }
228
Dan Willemsen1e704462016-08-21 15:17:17 -0700229 if what&BuildNinja != 0 {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700230 if !config.SkipMake() {
231 installCleanIfNecessary(ctx, config)
232 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700233
Dan Willemsen1e704462016-08-21 15:17:17 -0700234 // Run ninja
235 runNinja(ctx, config)
236 }
237}