blob: 205d5ae5fbfba6edb5eb87b0509ded7dbfd9c8be [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 "crypto/md5"
19 "fmt"
20 "io/ioutil"
Dan Willemsen1e704462016-08-21 15:17:17 -070021 "path/filepath"
Dan Willemsen1e704462016-08-21 15:17:17 -070022 "strings"
Dan Willemsenb82471a2018-05-17 16:37:09 -070023
Nan Zhang17f27672018-12-12 16:01:49 -080024 "android/soong/ui/metrics"
Dan Willemsenb82471a2018-05-17 16:37:09 -070025 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
28var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
29
Dan Willemsen29971232018-09-26 14:58:30 -070030const katiBuildSuffix = ""
31const katiCleanspecSuffix = "-cleanspec"
Dan Willemsenfb1271a2018-09-26 15:00:42 -070032const katiPackageSuffix = "-package"
Dan Willemsen29971232018-09-26 14:58:30 -070033
Dan Willemsen1e704462016-08-21 15:17:17 -070034// genKatiSuffix creates a suffix for kati-generated files so that we can cache
35// them based on their inputs. So this should encode all common changes to Kati
36// inputs. Currently that includes the TARGET_PRODUCT, kati-processed command
37// line arguments, and the directories specified by mm/mmm.
38func genKatiSuffix(ctx Context, config Config) {
39 katiSuffix := "-" + config.TargetProduct()
40 if args := config.KatiArgs(); len(args) > 0 {
41 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
42 }
43 if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
44 katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot)
45 }
46
47 // If the suffix is too long, replace it with a md5 hash and write a
48 // file that contains the original suffix.
49 if len(katiSuffix) > 64 {
50 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
51 config.SetKatiSuffix(shortSuffix)
52
53 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
54 ctx.Verbosef("Replacing with: %q", shortSuffix)
55
Dan Willemsen29971232018-09-26 14:58:30 -070056 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070057 ctx.Println("Error writing suffix file:", err)
58 }
59 } else {
60 config.SetKatiSuffix(katiSuffix)
61 }
62}
63
Dan Willemsenfb1271a2018-09-26 15:00:42 -070064func runKati(ctx Context, config Config, extraSuffix string, args []string, envFunc func(*Environment)) {
Dan Willemsenf173d592017-04-27 14:28:00 -070065 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen29971232018-09-26 14:58:30 -070066 args = append([]string{
Dan Willemsen1e704462016-08-21 15:17:17 -070067 "--ninja",
68 "--ninja_dir=" + config.OutDir(),
Dan Willemsen29971232018-09-26 14:58:30 -070069 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
70 "--no_ninja_prelude",
Dan Willemsen1e704462016-08-21 15:17:17 -070071 "--regen",
72 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
73 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080074 "--color_warnings",
75 "--gen_all_targets",
Dan Willemsen29971232018-09-26 14:58:30 -070076 "--use_find_emulator",
Dan Willemsen418420e2017-05-30 14:07:45 -070077 "--werror_find_emulator",
Dan Willemsend368d6f2018-06-15 21:53:18 -070078 "--no_builtin_rules",
79 "--werror_suffix_rules",
Dan Willemsenf880b582018-07-23 23:09:05 -070080 "--warn_real_to_phony",
81 "--warn_phony_looks_real",
Dan Willemsen75d2c172017-10-12 20:46:34 -070082 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -070083 }, args...)
84
Dan Willemsenf99915f2018-10-25 22:04:42 -070085 if config.Environment().IsEnvTrue("EMPTY_NINJA_FILE") {
86 args = append(args, "--empty_ninja_file")
87 }
88
Dan Willemsen29971232018-09-26 14:58:30 -070089 cmd := Command(ctx, config, "ckati", executable, args...)
90 cmd.Sandbox = katiSandbox
91 pipe, err := cmd.StdoutPipe()
92 if err != nil {
93 ctx.Fatalln("Error getting output pipe for ckati:", err)
94 }
95 cmd.Stderr = cmd.Stdout
96
Dan Willemsenfb1271a2018-09-26 15:00:42 -070097 envFunc(cmd.Environment)
98
Dan Willemsen29971232018-09-26 14:58:30 -070099 cmd.StartOrFatal()
100 status.KatiReader(ctx.Status.StartTool(), pipe)
101 cmd.WaitOrFatal()
102}
103
104func runKatiBuild(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800105 ctx.BeginTrace(metrics.RunKati, "kati build")
Dan Willemsen29971232018-09-26 14:58:30 -0700106 defer ctx.EndTrace()
107
108 args := []string{
Dan Willemsen25a56182018-08-31 20:25:32 -0700109 "--writable", config.OutDir() + "/",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700110 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700111 }
112
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700113 // PDK builds still uses a few implicit rules
114 if !config.IsPdkBuild() {
Dan Willemsen2bc456e2018-06-21 21:41:35 -0700115 args = append(args, "--werror_implicit_rules")
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700116 }
117
Dan Willemsen3d60b112018-04-04 22:25:56 -0700118 if !config.BuildBrokenDupRules() {
119 args = append(args, "--werror_overriding_commands")
120 }
121
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700122 if !config.BuildBrokenPhonyTargets() {
Dan Willemsen25a56182018-08-31 20:25:32 -0700123 args = append(args,
124 "--werror_real_to_phony",
125 "--werror_phony_looks_real",
126 "--werror_writable")
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700127 }
128
Dan Willemsen1e704462016-08-21 15:17:17 -0700129 args = append(args, config.KatiArgs()...)
130
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700131 args = append(args,
132 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
133 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
134 "TARGET_DEVICE_DIR="+config.TargetDeviceDir(),
135 "KATI_PACKAGE_MK_DIR="+config.KatiPackageMkDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700136
Dan Willemsen2d31a442018-10-20 21:33:41 -0700137 runKati(ctx, config, katiBuildSuffix, args, func(env *Environment) {})
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700138}
139
140func runKatiPackage(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800141 ctx.BeginTrace(metrics.RunKati, "kati package")
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700142 defer ctx.EndTrace()
143
144 args := []string{
145 "--writable", config.DistDir() + "/",
146 "--werror_writable",
147 "--werror_implicit_rules",
148 "--werror_overriding_commands",
149 "--werror_real_to_phony",
150 "--werror_phony_looks_real",
151 "-f", "build/make/packaging/main.mk",
152 "KATI_PACKAGE_MK_DIR=" + config.KatiPackageMkDir(),
153 }
154
155 runKati(ctx, config, katiPackageSuffix, args, func(env *Environment) {
156 env.Allow([]string{
157 // Some generic basics
158 "LANG",
159 "LC_MESSAGES",
160 "PATH",
161 "PWD",
162 "TMPDIR",
163
164 // Tool configs
165 "JAVA_HOME",
166 "PYTHONDONTWRITEBYTECODE",
167
168 // Build configuration
169 "ANDROID_BUILD_SHELL",
170 "DIST_DIR",
171 "OUT_DIR",
172 }...)
173
174 if config.Dist() {
175 env.Set("DIST", "true")
Dan Willemsen2d31a442018-10-20 21:33:41 -0700176 env.Set("DIST_DIR", config.DistDir())
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700177 }
178 })
Dan Willemsen1e704462016-08-21 15:17:17 -0700179}
Dan Willemsen29f88272017-02-18 18:12:41 -0800180
Dan Willemsen59fdf962017-07-24 22:26:54 -0700181func runKatiCleanSpec(ctx Context, config Config) {
Nan Zhang17f27672018-12-12 16:01:49 -0800182 ctx.BeginTrace(metrics.RunKati, "kati cleanspec")
Dan Willemsen59fdf962017-07-24 22:26:54 -0700183 defer ctx.EndTrace()
184
Dan Willemsen29971232018-09-26 14:58:30 -0700185 runKati(ctx, config, katiCleanspecSuffix, []string{
186 "--werror_implicit_rules",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700187 "--werror_overriding_commands",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700188 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsenfb1271a2018-09-26 15:00:42 -0700189 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
190 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen2d31a442018-10-20 21:33:41 -0700191 }, func(env *Environment) {})
Dan Willemsen59fdf962017-07-24 22:26:54 -0700192}