blob: 7cfa1cf7063b723b5662c70a8a2a2490c6639fb4 [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"
22 "strconv"
23 "strings"
Dan Willemsenb82471a2018-05-17 16:37:09 -070024
25 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070026)
27
28var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
29
30// genKatiSuffix creates a suffix for kati-generated files so that we can cache
31// them based on their inputs. So this should encode all common changes to Kati
32// inputs. Currently that includes the TARGET_PRODUCT, kati-processed command
33// line arguments, and the directories specified by mm/mmm.
34func genKatiSuffix(ctx Context, config Config) {
35 katiSuffix := "-" + config.TargetProduct()
36 if args := config.KatiArgs(); len(args) > 0 {
37 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
38 }
39 if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
40 katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot)
41 }
42
43 // If the suffix is too long, replace it with a md5 hash and write a
44 // file that contains the original suffix.
45 if len(katiSuffix) > 64 {
46 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
47 config.SetKatiSuffix(shortSuffix)
48
49 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
50 ctx.Verbosef("Replacing with: %q", shortSuffix)
51
52 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
53 ctx.Println("Error writing suffix file:", err)
54 }
55 } else {
56 config.SetKatiSuffix(katiSuffix)
57 }
58}
59
60func runKati(ctx Context, config Config) {
Dan Willemsen59fdf962017-07-24 22:26:54 -070061 genKatiSuffix(ctx, config)
62
63 runKatiCleanSpec(ctx, config)
64
Dan Willemsend9f6fa22016-08-21 15:17:17 -070065 ctx.BeginTrace("kati")
66 defer ctx.EndTrace()
67
Dan Willemsenf173d592017-04-27 14:28:00 -070068 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen1e704462016-08-21 15:17:17 -070069 args := []string{
70 "--ninja",
71 "--ninja_dir=" + config.OutDir(),
72 "--ninja_suffix=" + config.KatiSuffix(),
73 "--regen",
74 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
75 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080076 "--color_warnings",
77 "--gen_all_targets",
Dan Willemsen418420e2017-05-30 14:07:45 -070078 "--werror_find_emulator",
Dan Willemsend368d6f2018-06-15 21:53:18 -070079 "--no_builtin_rules",
80 "--werror_suffix_rules",
Dan Willemsen75d2c172017-10-12 20:46:34 -070081 "--kati_stats",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070082 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -070083 }
84
Dan Willemsenfa42f3c2018-06-15 21:54:47 -070085 // PDK builds still uses a few implicit rules
86 if !config.IsPdkBuild() {
Dan Willemsen2bc456e2018-06-21 21:41:35 -070087 args = append(args, "--werror_implicit_rules")
Dan Willemsenfa42f3c2018-06-15 21:54:47 -070088 }
89
Dan Willemsen3d60b112018-04-04 22:25:56 -070090 if !config.BuildBrokenDupRules() {
91 args = append(args, "--werror_overriding_commands")
92 }
93
Dan Willemsen1e704462016-08-21 15:17:17 -070094 if !config.Environment().IsFalse("KATI_EMULATE_FIND") {
95 args = append(args, "--use_find_emulator")
96 }
97
Dan Willemsen1e704462016-08-21 15:17:17 -070098 args = append(args, config.KatiArgs()...)
99
100 args = append(args,
Dan Willemsen1e704462016-08-21 15:17:17 -0700101 "BUILDING_WITH_NINJA=true",
102 "SOONG_ANDROID_MK="+config.SoongAndroidMk(),
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700103 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
104 "TARGET_DEVICE_DIR="+config.TargetDeviceDir())
Dan Willemsen1e704462016-08-21 15:17:17 -0700105
106 if config.UseGoma() {
107 args = append(args, "-j"+strconv.Itoa(config.Parallel()))
108 }
109
Dan Willemsen269a8c72017-05-03 17:15:47 -0700110 cmd := Command(ctx, config, "ckati", executable, args...)
111 cmd.Sandbox = katiSandbox
Dan Willemsen29f88272017-02-18 18:12:41 -0800112 pipe, err := cmd.StdoutPipe()
113 if err != nil {
114 ctx.Fatalln("Error getting output pipe for ckati:", err)
115 }
116 cmd.Stderr = cmd.Stdout
117
Dan Willemsen269a8c72017-05-03 17:15:47 -0700118 cmd.StartOrFatal()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700119 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen269a8c72017-05-03 17:15:47 -0700120 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700121}
Dan Willemsen29f88272017-02-18 18:12:41 -0800122
Dan Willemsen59fdf962017-07-24 22:26:54 -0700123func runKatiCleanSpec(ctx Context, config Config) {
124 ctx.BeginTrace("kati cleanspec")
125 defer ctx.EndTrace()
126
127 executable := config.PrebuiltBuildTool("ckati")
128 args := []string{
129 "--ninja",
130 "--ninja_dir=" + config.OutDir(),
131 "--ninja_suffix=" + config.KatiSuffix() + "-cleanspec",
132 "--regen",
133 "--detect_android_echo",
134 "--color_warnings",
135 "--gen_all_targets",
136 "--werror_find_emulator",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700137 "--werror_overriding_commands",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700138 "--use_find_emulator",
Dan Willemsen81759842018-05-01 23:34:56 -0700139 "--kati_stats",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700140 "-f", "build/make/core/cleanbuild.mk",
141 "BUILDING_WITH_NINJA=true",
142 "SOONG_MAKEVARS_MK=" + config.SoongMakeVarsMk(),
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700143 "TARGET_DEVICE_DIR=" + config.TargetDeviceDir(),
Dan Willemsen59fdf962017-07-24 22:26:54 -0700144 }
145
146 cmd := Command(ctx, config, "ckati", executable, args...)
147 cmd.Sandbox = katiCleanSpecSandbox
Dan Willemsen81759842018-05-01 23:34:56 -0700148 pipe, err := cmd.StdoutPipe()
149 if err != nil {
150 ctx.Fatalln("Error getting output pipe for ckati:", err)
151 }
152 cmd.Stderr = cmd.Stdout
Dan Willemsen59fdf962017-07-24 22:26:54 -0700153
Dan Willemsen81759842018-05-01 23:34:56 -0700154 cmd.StartOrFatal()
Dan Willemsenb82471a2018-05-17 16:37:09 -0700155 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen81759842018-05-01 23:34:56 -0700156 cmd.WaitOrFatal()
Dan Willemsen59fdf962017-07-24 22:26:54 -0700157}