blob: d6059c0bf1848ac88cd206c64b5d9b55d92228f9 [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 (
18 "os"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "path/filepath"
20 "text/template"
21)
22
23// Ensures the out directory exists, and has the proper files to prevent kati
24// from recursing into it.
25func SetupOutDir(ctx Context, config Config) {
26 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "Android.mk"))
27 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "CleanSpec.mk"))
28 ensureEmptyFileExists(ctx, filepath.Join(config.SoongOutDir(), ".soong.in_make"))
29 // The ninja_build file is used by our buildbots to understand that the output
30 // can be parsed as ninja output.
31 ensureEmptyFileExists(ctx, filepath.Join(config.OutDir(), "ninja_build"))
32}
33
34var combinedBuildNinjaTemplate = template.Must(template.New("combined").Parse(`
35builddir = {{.OutDir}}
36include {{.KatiNinjaFile}}
37include {{.SoongNinjaFile}}
38build {{.CombinedNinjaFile}}: phony {{.SoongNinjaFile}}
39`))
40
41func createCombinedBuildNinjaFile(ctx Context, config Config) {
42 file, err := os.Create(config.CombinedNinjaFile())
43 if err != nil {
44 ctx.Fatalln("Failed to create combined ninja file:", err)
45 }
46 defer file.Close()
47
48 if err := combinedBuildNinjaTemplate.Execute(file, config); err != nil {
49 ctx.Fatalln("Failed to write combined ninja file:", err)
50 }
51}
52
53const (
54 BuildNone = iota
55 BuildProductConfig = 1 << iota
56 BuildSoong = 1 << iota
57 BuildKati = 1 << iota
58 BuildNinja = 1 << iota
59 BuildAll = BuildProductConfig | BuildSoong | BuildKati | BuildNinja
60)
61
62// Build the tree. The 'what' argument can be used to chose which components of
63// the build to run.
64func Build(ctx Context, config Config, what int) {
65 ctx.Verboseln("Starting build with args:", config.Arguments())
66 ctx.Verboseln("Environment:", config.Environment().Environ())
67
68 if inList("help", config.Arguments()) {
Dan Willemsen269a8c72017-05-03 17:15:47 -070069 cmd := Command(ctx, config, "make",
70 "make", "-f", "build/core/help.mk")
71 cmd.Sandbox = makeSandbox
Dan Willemsen1e704462016-08-21 15:17:17 -070072 cmd.Stdout = ctx.Stdout()
73 cmd.Stderr = ctx.Stderr()
Dan Willemsen269a8c72017-05-03 17:15:47 -070074 cmd.RunOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -070075 return
76 }
77
78 SetupOutDir(ctx, config)
79
80 if what&BuildProductConfig != 0 {
81 // Run make for product config
82 runMakeProductConfig(ctx, config)
83 }
84
85 if what&BuildSoong != 0 {
86 // Run Soong
87 runSoongBootstrap(ctx, config)
88 runSoong(ctx, config)
89 }
90
91 if what&BuildKati != 0 {
92 // Run ckati
93 runKati(ctx, config)
94 }
95
96 if what&BuildNinja != 0 {
97 // Write combined ninja file
98 createCombinedBuildNinjaFile(ctx, config)
99
100 // Run ninja
101 runNinja(ctx, config)
102 }
103}