blob: 0df22b34578b00ba1544eea50d10e0d191e3d2ba [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 Willemsene0879fc2017-08-04 15:06:27 -070040{{if .HasKatiSuffix}}include {{.KatiNinjaFile}}
41{{end -}}
Dan Willemsen1e704462016-08-21 15:17:17 -070042include {{.SoongNinjaFile}}
43build {{.CombinedNinjaFile}}: phony {{.SoongNinjaFile}}
44`))
45
46func createCombinedBuildNinjaFile(ctx Context, config Config) {
Dan Willemsene0879fc2017-08-04 15:06:27 -070047 // 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 Willemsen1e704462016-08-21 15:17:17 -070054 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
65const (
66 BuildNone = iota
67 BuildProductConfig = 1 << iota
68 BuildSoong = 1 << iota
69 BuildKati = 1 << iota
70 BuildNinja = 1 << iota
71 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
72)
73
Dan Willemsendb8457c2017-05-12 16:38:17 -070074func checkCaseSensitivity(ctx Context, config Config) {
75 outDir := config.OutDir()
76 lowerCase := filepath.Join(outDir, "casecheck.txt")
77 upperCase := filepath.Join(outDir, "CaseCheck.txt")
78 lowerData := "a"
79 upperData := "B"
80
81 err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
82 if err != nil {
83 ctx.Fatalln("Failed to check case sensitivity:", err)
84 }
85
86 err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
87 if err != nil {
88 ctx.Fatalln("Failed to check case sensitivity:", err)
89 }
90
91 res, err := ioutil.ReadFile(lowerCase)
92 if err != nil {
93 ctx.Fatalln("Failed to check case sensitivity:", err)
94 }
95
96 if string(res) != lowerData {
97 ctx.Println("************************************************************")
98 ctx.Println("You are building on a case-insensitive filesystem.")
99 ctx.Println("Please move your source tree to a case-sensitive filesystem.")
100 ctx.Println("************************************************************")
101 ctx.Fatalln("Case-insensitive filesystems not supported")
102 }
103}
104
Dan Willemsenf052f782017-05-18 15:29:04 -0700105func help(ctx Context, config Config, what int) {
Jeff Gastondf4a0812017-05-30 20:11:20 -0700106 cmd := Command(ctx, config, "help.sh", "build/make/help.sh")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700107 cmd.Sandbox = dumpvarsSandbox
Dan Willemsenf052f782017-05-18 15:29:04 -0700108 cmd.Stdout = ctx.Stdout()
109 cmd.Stderr = ctx.Stderr()
110 cmd.RunOrFatal()
Dan Willemsen02781d52017-05-12 19:28:13 -0700111}
112
Dan Willemsen1e704462016-08-21 15:17:17 -0700113// Build the tree. The 'what' argument can be used to chose which components of
114// the build to run.
115func Build(ctx Context, config Config, what int) {
116 ctx.Verboseln("Starting build with args:", config.Arguments())
117 ctx.Verboseln("Environment:", config.Environment().Environ())
118
Dan Willemsene0879fc2017-08-04 15:06:27 -0700119 if config.SkipMake() {
120 ctx.Verboseln("Skipping Make/Kati as requested")
121 what = what & (BuildSoong | BuildNinja)
122 }
123
Dan Willemsen1e704462016-08-21 15:17:17 -0700124 if inList("help", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700125 help(ctx, config, what)
Dan Willemsen1e704462016-08-21 15:17:17 -0700126 return
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700127 } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
Dan Willemsenf052f782017-05-18 15:29:04 -0700128 clean(ctx, config, what)
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700129 return
Dan Willemsen1e704462016-08-21 15:17:17 -0700130 }
131
Jeff Gaston3615fe82017-05-24 13:14:34 -0700132 // Make sure that no other Soong process is running with the same output directory
133 buildLock := BecomeSingletonOrFail(ctx, config)
134 defer buildLock.Unlock()
135
Dan Willemsen1e704462016-08-21 15:17:17 -0700136 SetupOutDir(ctx, config)
137
Dan Willemsendb8457c2017-05-12 16:38:17 -0700138 checkCaseSensitivity(ctx, config)
139
Jeff Gastonefc1b412017-03-29 17:29:06 -0700140 ensureEmptyDirectoriesExist(ctx, config.TempDir())
141
Dan Willemsen1e704462016-08-21 15:17:17 -0700142 if what&BuildProductConfig != 0 {
143 // Run make for product config
144 runMakeProductConfig(ctx, config)
145 }
146
Dan Willemsenf052f782017-05-18 15:29:04 -0700147 if inList("installclean", config.Arguments()) {
148 installClean(ctx, config, what)
149 ctx.Println("Deleted images and staging directories.")
150 return
151 } else if inList("dataclean", config.Arguments()) {
152 dataClean(ctx, config, what)
153 ctx.Println("Deleted data files.")
154 return
155 }
156
Dan Willemsen1e704462016-08-21 15:17:17 -0700157 if what&BuildSoong != 0 {
158 // Run Soong
Dan Willemsen1e704462016-08-21 15:17:17 -0700159 runSoong(ctx, config)
160 }
161
Dan Willemsen1e704462016-08-21 15:17:17 -0700162 if what&BuildKati != 0 {
163 // Run ckati
164 runKati(ctx, config)
Dan Willemsene0879fc2017-08-04 15:06:27 -0700165
166 ioutil.WriteFile(config.LastKatiSuffixFile(), []byte(config.KatiSuffix()), 0777)
167 } else {
168 // Load last Kati Suffix if it exists
169 if katiSuffix, err := ioutil.ReadFile(config.LastKatiSuffixFile()); err == nil {
170 ctx.Verboseln("Loaded previous kati config:", string(katiSuffix))
171 config.SetKatiSuffix(string(katiSuffix))
172 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700173 }
174
175 if what&BuildNinja != 0 {
Dan Willemsene0879fc2017-08-04 15:06:27 -0700176 if !config.SkipMake() {
177 installCleanIfNecessary(ctx, config)
178 }
Dan Willemsen02781d52017-05-12 19:28:13 -0700179
Dan Willemsen1e704462016-08-21 15:17:17 -0700180 // Write combined ninja file
181 createCombinedBuildNinjaFile(ctx, config)
182
183 // Run ninja
184 runNinja(ctx, config)
185 }
186}