blob: b84dd7d7b422b46617a3e23491f8d8cba6775eb2 [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"))
29 ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make"))
30 // The ninja_build file is used by our buildbots to understand that the output
31 // can be parsed as ninja output.
32 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build"))
33}
34
35var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
36builddir = {{.OutDir}}
37include {{.KatiNinjaFile}}
38include {{.SoongNinjaFile}}
39build {{.CombinedNinjaFile}}: phony {{.SoongNinjaFile}}
40`))
41
42func createCombinedBuildNinjaFile(ctx Context, config Config) {
43 file, err := os.Create(config.CombinedNinjaFile())
44 if err != nil {
45 ctx.Fatalln("Failed to create combined ninja file:", err)
46 }
47 defer file.Close()
48
49 if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil {
50 ctx.Fatalln("Failed to write combined ninja file:", err)
51 }
52}
53
54const (
55 BuildNone = iota
56 BuildProductConfig = 1 << iota
57 BuildSoong = 1 << iota
58 BuildKati = 1 << iota
59 BuildNinja = 1 << iota
60 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
61)
62
Dan Willemsendb8457c2017-05-12 16:38:17 -070063func checkCaseSensitivity(ctx Context, config Config) {
64 outDir := config.OutDir()
65 lowerCase := filepath.Join(outDir, "casecheck.txt")
66 upperCase := filepath.Join(outDir, "CaseCheck.txt")
67 lowerData := "a"
68 upperData := "B"
69
70 err := ioutil.WriteFile(lowerCase, []byte(lowerData), 0777)
71 if err != nil {
72 ctx.Fatalln("Failed to check case sensitivity:", err)
73 }
74
75 err = ioutil.WriteFile(upperCase, []byte(upperData), 0777)
76 if err != nil {
77 ctx.Fatalln("Failed to check case sensitivity:", err)
78 }
79
80 res, err := ioutil.ReadFile(lowerCase)
81 if err != nil {
82 ctx.Fatalln("Failed to check case sensitivity:", err)
83 }
84
85 if string(res) != lowerData {
86 ctx.Println("************************************************************")
87 ctx.Println("You are building on a case-insensitive filesystem.")
88 ctx.Println("Please move your source tree to a case-sensitive filesystem.")
89 ctx.Println("************************************************************")
90 ctx.Fatalln("Case-insensitive filesystems not supported")
91 }
92}
93
Dan Willemsen1e704462016-08-21 15:17:17 -070094// Build the tree. The 'what' argument can be used to chose which components of
95// the build to run.
96func Build(ctx Context, config Config, what int) {
97 ctx.Verboseln("Starting build with args:", config.Arguments())
98 ctx.Verboseln("Environment:", config.Environment().Environ())
99
100 if inList("help", config.Arguments()) {
Dan Willemsen269a8c72017-05-03 17:15:47 -0700101 cmd := Command(ctx, config, "make",
102 "make", "-f", "build/core/help.mk")
103 cmd.Sandbox = makeSandbox
Dan Willemsen1e704462016-08-21 15:17:17 -0700104 cmd.Stdout = ctx.Stdout()
105 cmd.Stderr = ctx.Stderr()
Dan Willemsen269a8c72017-05-03 17:15:47 -0700106 cmd.RunOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700107 return
Dan Willemsen0b73b4b2017-05-12 19:28:13 -0700108 } else if inList("clean", config.Arguments()) || inList("clobber", config.Arguments()) {
109 // We can't use os.RemoveAll, since we don't want to remove the
110 // output directory itself, in case it's a symlink. So just do
111 // exactly what make used to do.
112 cmd := Command(ctx, config, "rm -rf $OUT_DIR/*",
113 "/bin/bash", "-c", "rm -rf "+config.OutDir()+"/*")
114 cmd.Stdout = ctx.Stdout()
115 cmd.Stderr = ctx.Stderr()
116 cmd.RunOrFatal()
117 ctx.Println("Entire build directory removed.")
118 return
Dan Willemsen1e704462016-08-21 15:17:17 -0700119 }
120
Dan Willemsendb8457c2017-05-12 16:38:17 -0700121 // Start getting java version as early as possible
122 getJavaVersions(ctx, config)
123
Dan Willemsen1e704462016-08-21 15:17:17 -0700124 SetupOutDir(ctx, config)
125
Dan Willemsendb8457c2017-05-12 16:38:17 -0700126 checkCaseSensitivity(ctx, config)
127
Dan Willemsen1e704462016-08-21 15:17:17 -0700128 if what&BuildProductConfig != 0 {
129 // Run make for product config
130 runMakeProductConfig(ctx, config)
131 }
132
133 if what&BuildSoong != 0 {
134 // Run Soong
135 runSoongBootstrap(ctx, config)
136 runSoong(ctx, config)
137 }
138
Dan Willemsendb8457c2017-05-12 16:38:17 -0700139 // Check the java versions we read earlier
140 checkJavaVersion(ctx, config)
141
Dan Willemsen1e704462016-08-21 15:17:17 -0700142 if what&BuildKati != 0 {
143 // Run ckati
144 runKati(ctx, config)
145 }
146
147 if what&BuildNinja != 0 {
148 // Write combined ninja file
149 createCombinedBuildNinjaFile(ctx, config)
150
151 // Run ninja
152 runNinja(ctx, config)
153 }
154}