blob: 377481be958c3d1aa9b7c5bd7ff167f7f0f366a1 [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"))
Dan Willemsen1e704462016-08-21 15:17:17 -070036}
37
38var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
39builddir = {{.OutDir}}
Dan Willemsen29971232018-09-26 14:58:30 -070040pool local_pool
41 depth = {{.Parallel}}
42build _kati_always_build_: phony
43{{if .HasKatiSuffix}}include {{.KatiBuildNinjaFile}}
Dan Willemsene0879fc2017-08-04 15:06:27 -070044{{end -}}
Dan Willemsen1e704462016-08-21 15:17:17 -070045include {{.SoongNinjaFile}}
Dan Willemsen1e704462016-08-21 15:17:17 -070046`))
47
48func createCombinedBuildNinjaFile(ctx Context, config Config) {
Dan Willemsene0879fc2017-08-04 15:06:27 -070049 // If we're in SkipMake mode, skip creating this file if it already exists
50 if config.SkipMake() {
51 if _, err := os.Stat(config.CombinedNinjaFile()); err == nil || !os.IsNotExist(err) {
52 return
53 }
54 }
55
Dan Willemsen1e704462016-08-21 15:17:17 -070056 file, err := os.Create(config.CombinedNinjaFile())
57 if err != nil {
58 ctx.Fatalln("Failed to create combined ninja file:", err)
59 }
60 defer file.Close()
61
62 if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil {
63 ctx.Fatalln("Failed to write combined ninja file:", err)
64 }
65}
66
67const (
68 BuildNone = iota
69 BuildProductConfig = 1 << iota
70 BuildSoong = 1 << iota
71 BuildKati = 1 << iota
72 BuildNinja = 1 << iota
Colin Cross37193492017-11-16 17:55:00 -080073 RunBuildTests = 1 << iota
Dan Willemsen1e704462016-08-21 15:17:17 -070074 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
75)
76
Anton Hanssonecf0f102018-09-19 22:14:17 +010077func checkProblematicFiles(ctx Context) {
78 files := []string{"Android.mk", "CleanSpec.mk"}
79 for _, file := range files {
80 if _, err := os.Stat(file); !os.IsNotExist(err) {
81 absolute := absPath(ctx, file)
82 ctx.Printf("Found %s in tree root. This file needs to be removed to build.\n", file)
83 ctx.Fatalf(" rm %s\n", absolute)
84 }
85 }
86}
87
Dan Willemsendb8457c2017-05-12 16:38:17 -070088func checkCaseSensitivity(ctx Context, config Config) {
89 outDir := config.OutDir()
90 lowerCase := filepath.Join(outDir, "casecheck.txt")
91 upperCase := filepath.Join(outDir, "CaseCheck.txt")
92 lowerData := "a"
93 upperData := "B"
94
95 err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
96 if err != nil {
97 ctx.Fatalln("Failed to check case sensitivity:", err)
98 }
99
100 err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
101 if err != nil {
102 ctx.Fatalln("Failed to check case sensitivity:", err)
103 }
104
105 res, err := ioutil.ReadFile(lowerCase)
106 if err != nil {
107 ctx.Fatalln("Failed to check case sensitivity:", err)
108 }
109
110 if string(res) != lowerData {
111 ctx.Println("************************************************************")
112 ctx.Println("You are building on a case-insensitive filesystem.")
113 ctx.Println("Please move your source tree to a case-sensitive filesystem.")
114 ctx.Println("************************************************************")
115 ctx.Fatalln("Case-insensitive filesystems not supported")
116 }
117}
118
Dan Willemsenf052f782017-05-18 15:29:04 -0700119func help(ctx Context, config Config, what int) {
Jeff Gastondf4a0812017-05-30 20:11:20 -0700120 cmd := Command(ctx, config, "help.sh", "build/make/help.sh")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700121 cmd.Sandbox = dumpvarsSandbox
Dan Willemsenb82471a2018-05-17 16:37:09 -0700122 cmd.RunAndPrintOrFatal()
Dan Willemsen02781d52017-05-12 19:28:13 -0700123}
124
Dan Willemsen1e704462016-08-21 15:17:17 -0700125// Build the tree. The 'what' argument can be used to chose which components of
126// the build to run.
127func Build(ctx Context, config Config, what int) {
128 ctx.Verboseln("Starting build with args:", config.Arguments())
129 ctx.Verboseln("Environment:", config.Environment().Environ())
130
Dan Willemsene0879fc2017-08-04 15:06:27 -0700131 if config.SkipMake() {
132 ctx.Verboseln("Skipping Make/Kati as requested")
133 what = what & (BuildSoong | BuildNinja)
134 }
135
Dan Willemsen1e704462016-08-21 15:17:17 -0700136 if inList("help", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700137 help(ctx, config, what)
Dan Willemsen1e704462016-08-21 15:17:17 -0700138 return
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700139 } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700140 clean(ctx, config, what)
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700141 return
Dan Willemsen1e704462016-08-21 15:17:17 -0700142 }
143
Jeff Gaston3615fe82017-05-24 13:14:34 -0700144 // Make sure that no other Soong process is running with the same output directory
145 buildLock := BecomeSingletonOrFail(ctx, config)
146 defer buildLock.Unlock()
147
Anton Hanssonecf0f102018-09-19 22:14:17 +0100148 checkProblematicFiles(ctx)
149
Dan Willemsen1e704462016-08-21 15:17:17 -0700150 SetupOutDir(ctx, config)
151
Dan Willemsendb8457c2017-05-12 16:38:17 -0700152 checkCaseSensitivity(ctx, config)
153
Jeff Gastonefc1b412017-03-29 17:29:06 -0700154 ensureEmptyDirectoriesExist(ctx, config.TempDir())
155
Dan Willemsen18490112018-05-25 16:30:04 -0700156 SetupPath(ctx, config)
157
Dan Willemsen1e704462016-08-21 15:17:17 -0700158 if what&BuildProductConfig != 0 {
159 // Run make for product config
160 runMakeProductConfig(ctx, config)
161 }
162
Dan Willemsenf052f782017-05-18 15:29:04 -0700163 if inList("installclean", config.Arguments()) {
164 installClean(ctx, config, what)
165 ctx.Println("Deleted images and staging directories.")
166 return
167 } else if inList("dataclean", config.Arguments()) {
168 dataClean(ctx, config, what)
169 ctx.Println("Deleted data files.")
170 return
171 }
172
Dan Willemsen1e704462016-08-21 15:17:17 -0700173 if what&BuildSoong != 0 {
174 // Run Soong
Dan Willemsen1e704462016-08-21 15:17:17 -0700175 runSoong(ctx, config)
176 }
177
Dan Willemsen1e704462016-08-21 15:17:17 -0700178 if what&BuildKati != 0 {
179 // Run ckati
Dan Willemsen29971232018-09-26 14:58:30 -0700180 genKatiSuffix(ctx, config)
181 runKatiCleanSpec(ctx, config)
182 runKatiBuild(ctx, config)
Dan Willemsene0879fc2017-08-04 15:06:27 -0700183
184 ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777)
185 } else {
186 // Load last Kati Suffix if it exists
187 if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil {
188 ctx.Verboseln("Loaded previous kati config:", string(katiSuffix))
189 config.SetKatiSuffix(string(katiSuffix))
190 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700191 }
192
Colin Cross37193492017-11-16 17:55:00 -0800193 // Write combined ninja file
194 createCombinedBuildNinjaFile(ctx, config)
195
196 if what&RunBuildTests != 0 {
197 testForDanglingRules(ctx, config)
198 }
199
Dan Willemsen1e704462016-08-21 15:17:17 -0700200 if what&BuildNinja != 0 {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700201 if !config.SkipMake() {
202 installCleanIfNecessary(ctx, config)
203 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700204
Dan Willemsen1e704462016-08-21 15:17:17 -0700205 // Run ninja
206 runNinja(ctx, config)
207 }
208}