blob: 546fd1ac1a51482bb5011df770361158c60a6e50 [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
24 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070025)
26
27var spaceSlashReplacer = strings.NewReplacer("/", "_", " ", "_")
28
Dan Willemsen29971232018-09-26 14:58:30 -070029const katiBuildSuffix = ""
30const katiCleanspecSuffix = "-cleanspec"
31
Dan Willemsen1e704462016-08-21 15:17:17 -070032// genKatiSuffix creates a suffix for kati-generated files so that we can cache
33// them based on their inputs. So this should encode all common changes to Kati
34// inputs. Currently that includes the TARGET_PRODUCT, kati-processed command
35// line arguments, and the directories specified by mm/mmm.
36func genKatiSuffix(ctx Context, config Config) {
37 katiSuffix := "-" + config.TargetProduct()
38 if args := config.KatiArgs(); len(args) > 0 {
39 katiSuffix += "-" + spaceSlashReplacer.Replace(strings.Join(args, "_"))
40 }
41 if oneShot, ok := config.Environment().Get("ONE_SHOT_MAKEFILE"); ok {
42 katiSuffix += "-" + spaceSlashReplacer.Replace(oneShot)
43 }
44
45 // If the suffix is too long, replace it with a md5 hash and write a
46 // file that contains the original suffix.
47 if len(katiSuffix) > 64 {
48 shortSuffix := "-" + fmt.Sprintf("%x", md5.Sum([]byte(katiSuffix)))
49 config.SetKatiSuffix(shortSuffix)
50
51 ctx.Verbosef("Kati ninja suffix too long: %q", katiSuffix)
52 ctx.Verbosef("Replacing with: %q", shortSuffix)
53
Dan Willemsen29971232018-09-26 14:58:30 -070054 if err := ioutil.WriteFile(strings.TrimSuffix(config.KatiBuildNinjaFile(), "ninja")+"suf", []byte(katiSuffix), 0777); err != nil {
Dan Willemsen1e704462016-08-21 15:17:17 -070055 ctx.Println("Error writing suffix file:", err)
56 }
57 } else {
58 config.SetKatiSuffix(katiSuffix)
59 }
60}
61
Dan Willemsen29971232018-09-26 14:58:30 -070062func runKati(ctx Context, config Config, extraSuffix string, args []string) {
Dan Willemsenf173d592017-04-27 14:28:00 -070063 executable := config.PrebuiltBuildTool("ckati")
Dan Willemsen29971232018-09-26 14:58:30 -070064 args = append([]string{
Dan Willemsen1e704462016-08-21 15:17:17 -070065 "--ninja",
66 "--ninja_dir=" + config.OutDir(),
Dan Willemsen29971232018-09-26 14:58:30 -070067 "--ninja_suffix=" + config.KatiSuffix() + extraSuffix,
68 "--no_ninja_prelude",
Dan Willemsen1e704462016-08-21 15:17:17 -070069 "--regen",
70 "--ignore_optional_include=" + filepath.Join(config.OutDir(), "%.P"),
71 "--detect_android_echo",
Dan Willemsenc38d3662017-02-24 10:53:23 -080072 "--color_warnings",
73 "--gen_all_targets",
Dan Willemsen29971232018-09-26 14:58:30 -070074 "--use_find_emulator",
Dan Willemsen418420e2017-05-30 14:07:45 -070075 "--werror_find_emulator",
Dan Willemsend368d6f2018-06-15 21:53:18 -070076 "--no_builtin_rules",
77 "--werror_suffix_rules",
Dan Willemsenf880b582018-07-23 23:09:05 -070078 "--warn_real_to_phony",
79 "--warn_phony_looks_real",
Dan Willemsen75d2c172017-10-12 20:46:34 -070080 "--kati_stats",
Dan Willemsen29971232018-09-26 14:58:30 -070081 }, args...)
82
83 args = append(args,
84 "SOONG_MAKEVARS_MK="+config.SoongMakeVarsMk(),
85 "TARGET_DEVICE_DIR="+config.TargetDeviceDir())
86
87 cmd := Command(ctx, config, "ckati", executable, args...)
88 cmd.Sandbox = katiSandbox
89 pipe, err := cmd.StdoutPipe()
90 if err != nil {
91 ctx.Fatalln("Error getting output pipe for ckati:", err)
92 }
93 cmd.Stderr = cmd.Stdout
94
95 cmd.StartOrFatal()
96 status.KatiReader(ctx.Status.StartTool(), pipe)
97 cmd.WaitOrFatal()
98}
99
100func runKatiBuild(ctx Context, config Config) {
101 ctx.BeginTrace("kati build")
102 defer ctx.EndTrace()
103
104 args := []string{
Dan Willemsen25a56182018-08-31 20:25:32 -0700105 "--writable", config.OutDir() + "/",
106 "--writable", config.DistDir() + "/",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700107 "-f", "build/make/core/main.mk",
Dan Willemsen1e704462016-08-21 15:17:17 -0700108 }
109
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700110 // PDK builds still uses a few implicit rules
111 if !config.IsPdkBuild() {
Dan Willemsen2bc456e2018-06-21 21:41:35 -0700112 args = append(args, "--werror_implicit_rules")
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700113 }
114
Dan Willemsen3d60b112018-04-04 22:25:56 -0700115 if !config.BuildBrokenDupRules() {
116 args = append(args, "--werror_overriding_commands")
117 }
118
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700119 if !config.BuildBrokenPhonyTargets() {
Dan Willemsen25a56182018-08-31 20:25:32 -0700120 args = append(args,
121 "--werror_real_to_phony",
122 "--werror_phony_looks_real",
123 "--werror_writable")
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700124 }
125
Dan Willemsen1e704462016-08-21 15:17:17 -0700126 args = append(args, config.KatiArgs()...)
127
Dan Willemsen29971232018-09-26 14:58:30 -0700128 args = append(args, "SOONG_ANDROID_MK="+config.SoongAndroidMk())
Dan Willemsen1e704462016-08-21 15:17:17 -0700129
Dan Willemsen29971232018-09-26 14:58:30 -0700130 runKati(ctx, config, katiBuildSuffix, args)
Dan Willemsen1e704462016-08-21 15:17:17 -0700131}
Dan Willemsen29f88272017-02-18 18:12:41 -0800132
Dan Willemsen59fdf962017-07-24 22:26:54 -0700133func runKatiCleanSpec(ctx Context, config Config) {
134 ctx.BeginTrace("kati cleanspec")
135 defer ctx.EndTrace()
136
Dan Willemsen29971232018-09-26 14:58:30 -0700137 runKati(ctx, config, katiCleanspecSuffix, []string{
138 "--werror_implicit_rules",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700139 "--werror_overriding_commands",
Dan Willemsen59fdf962017-07-24 22:26:54 -0700140 "-f", "build/make/core/cleanbuild.mk",
Dan Willemsen29971232018-09-26 14:58:30 -0700141 })
Dan Willemsen59fdf962017-07-24 22:26:54 -0700142}