blob: ad57d02bd4f894e7b2a851ca4058b38930b59f62 [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 Willemsen051133b2017-07-14 11:29:29 -070018 "bytes"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "fmt"
Dan Willemsen1e704462016-08-21 15:17:17 -070020 "strings"
Dan Willemsenb82471a2018-05-17 16:37:09 -070021
22 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070023)
24
25// DumpMakeVars can be used to extract the values of Make variables after the
26// product configurations are loaded. This is roughly equivalent to the
27// `get_build_var` bash function.
28//
29// goals can be used to set MAKECMDGOALS, which emulates passing arguments to
30// Make without actually building them. So all the variables based on
31// MAKECMDGOALS can be read.
32//
Dan Willemsen1e704462016-08-21 15:17:17 -070033// vars is the list of variables to read. The values will be put in the
34// returned map.
Dan Willemsen6f037522018-10-21 09:20:47 -070035//
36// variables controlled by soong_ui directly are now returned without needing
37// to call into make, to retain compatibility.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070038func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) {
Dan Willemsen6f037522018-10-21 09:20:47 -070039 soongUiVars := map[string]func() string{
40 "OUT_DIR": func() string { return config.OutDir() },
41 "DIST_DIR": func() string { return config.DistDir() },
42 }
43
44 makeVars := make([]string, 0, len(vars))
45 for _, v := range vars {
46 if _, ok := soongUiVars[v]; !ok {
47 makeVars = append(makeVars, v)
48 }
49 }
50
51 var ret map[string]string
52 if len(makeVars) > 0 {
53 var err error
54 ret, err = dumpMakeVars(ctx, config, goals, makeVars, false)
55 if err != nil {
56 return ret, err
57 }
58 } else {
59 ret = make(map[string]string)
60 }
61
62 for _, v := range vars {
63 if f, ok := soongUiVars[v]; ok {
64 ret[v] = f()
65 }
66 }
67
68 return ret, nil
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070069}
70
71func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070072 ctx.BeginTrace("dumpvars")
73 defer ctx.EndTrace()
74
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070075 cmd := Command(ctx, config, "dumpvars",
76 config.PrebuiltBuildTool("ckati"),
77 "-f", "build/make/core/config.mk",
78 "--color_warnings",
Dan Willemsen0c518512018-01-09 02:09:52 -080079 "--kati_stats",
Dan Willemsen1e704462016-08-21 15:17:17 -070080 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070081 "MAKECMDGOALS="+strings.Join(goals, " "))
82 cmd.Environment.Set("CALLED_FROM_SETUP", "true")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070083 if write_soong_vars {
84 cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true")
85 }
86 cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
87 cmd.Sandbox = dumpvarsSandbox
Dan Willemsen0c518512018-01-09 02:09:52 -080088 output := bytes.Buffer{}
89 cmd.Stdout = &output
90 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -070091 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -080092 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -070093 }
Dan Willemsen0c518512018-01-09 02:09:52 -080094 cmd.StartOrFatal()
95 // TODO: error out when Stderr contains any content
Dan Willemsenb82471a2018-05-17 16:37:09 -070096 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen0c518512018-01-09 02:09:52 -080097 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -070098
99 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -0800100 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -0700101 if len(line) == 0 {
102 continue
103 }
104
105 if key, value, ok := decodeKeyValue(line); ok {
106 if value, ok = singleUnquote(value); ok {
107 ret[key] = value
108 ctx.Verboseln(key, value)
109 } else {
110 return nil, fmt.Errorf("Failed to parse make line: %q", line)
111 }
112 } else {
113 return nil, fmt.Errorf("Failed to parse make line: %q", line)
114 }
115 }
116
117 return ret, nil
118}
119
Dan Willemsen051133b2017-07-14 11:29:29 -0700120// Variables to print out in the top banner
121var BannerVars = []string{
122 "PLATFORM_VERSION_CODENAME",
123 "PLATFORM_VERSION",
124 "TARGET_PRODUCT",
125 "TARGET_BUILD_VARIANT",
126 "TARGET_BUILD_TYPE",
127 "TARGET_BUILD_APPS",
128 "TARGET_ARCH",
129 "TARGET_ARCH_VARIANT",
130 "TARGET_CPU_VARIANT",
131 "TARGET_2ND_ARCH",
132 "TARGET_2ND_ARCH_VARIANT",
133 "TARGET_2ND_CPU_VARIANT",
134 "HOST_ARCH",
135 "HOST_2ND_ARCH",
136 "HOST_OS",
137 "HOST_OS_EXTRA",
138 "HOST_CROSS_OS",
139 "HOST_CROSS_ARCH",
140 "HOST_CROSS_2ND_ARCH",
141 "HOST_BUILD_TYPE",
142 "BUILD_ID",
143 "OUT_DIR",
144 "AUX_OS_VARIANT_LIST",
145 "TARGET_BUILD_PDK",
146 "PDK_FUSION_PLATFORM_ZIP",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800147 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700148}
149
150func Banner(make_vars map[string]string) string {
151 b := &bytes.Buffer{}
152
153 fmt.Fprintln(b, "============================================")
154 for _, name := range BannerVars {
155 if make_vars[name] != "" {
156 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
157 }
158 }
159 fmt.Fprint(b, "============================================")
160
161 return b.String()
162}
163
Dan Willemsen1e704462016-08-21 15:17:17 -0700164func runMakeProductConfig(ctx Context, config Config) {
165 // Variables to export into the environment of Kati/Ninja
166 exportEnvVars := []string{
167 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsen04a16c72017-05-25 22:18:57 -0700168 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -0700169 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700170 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700171 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700172
173 // compiler wrappers set up by make
174 "CC_WRAPPER",
175 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900176 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700177
178 // ccache settings
179 "CCACHE_COMPILERCHECK",
180 "CCACHE_SLOPPINESS",
181 "CCACHE_BASEDIR",
182 "CCACHE_CPP2",
183 }
184
Dan Willemsen1e704462016-08-21 15:17:17 -0700185 allVars := append(append([]string{
186 // Used to execute Kati and Ninja
187 "NINJA_GOALS",
188 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700189
190 // To find target/product/<DEVICE>
191 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700192
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700193 // So that later Kati runs can find BoardConfig.mk faster
194 "TARGET_DEVICE_DIR",
195
Dan Willemsen3d60b112018-04-04 22:25:56 -0700196 // Whether --werror_overriding_commands will work
197 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700198
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700199 // Used to turn on --werror_ options in Kati
200 "BUILD_BROKEN_PHONY_TARGETS",
201
Dan Willemsenb58f1202018-06-21 10:12:53 -0700202 // Not used, but useful to be in the soong.log
203 "BUILD_BROKEN_ANDROIDMK_EXPORTS",
204 "BUILD_BROKEN_DUP_COPY_HEADERS",
Dan Willemsen051133b2017-07-14 11:29:29 -0700205 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700206
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700207 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700208 if err != nil {
209 ctx.Fatalln("Error dumping make vars:", err)
210 }
211
212 // Print the banner like make does
Dan Willemsenb82471a2018-05-17 16:37:09 -0700213 ctx.Writer.Print(Banner(make_vars))
Dan Willemsen1e704462016-08-21 15:17:17 -0700214
215 // Populate the environment
216 env := config.Environment()
217 for _, name := range exportEnvVars {
218 if make_vars[name] == "" {
219 env.Unset(name)
220 } else {
221 env.Set(name, make_vars[name])
222 }
223 }
224
225 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
226 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
Dan Willemsen02781d52017-05-12 19:28:13 -0700227 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700228 config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"])
Dan Willemsen3d60b112018-04-04 22:25:56 -0700229
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700230 config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true")
Dan Willemsen9dc69a42018-06-22 13:18:06 -0700231 config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] == "true")
Dan Willemsen14569582018-09-10 13:06:43 -0700232 config.SetBuildBrokenPhonyTargets(make_vars["BUILD_BROKEN_PHONY_TARGETS"] == "true")
Dan Willemsen1e704462016-08-21 15:17:17 -0700233}