blob: 8581387c30314d4e6fc21c5e7e4fc68d369985ef [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 Willemsen4e2456b2019-10-03 16:45:58 -070020 "io/ioutil"
21 "os"
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
28// DumpMakeVars can be used to extract the values of Make variables after the
29// product configurations are loaded. This is roughly equivalent to the
30// `get_build_var` bash function.
31//
32// goals can be used to set MAKECMDGOALS, which emulates passing arguments to
33// Make without actually building them. So all the variables based on
34// MAKECMDGOALS can be read.
35//
Dan Willemsen1e704462016-08-21 15:17:17 -070036// vars is the list of variables to read. The values will be put in the
37// returned map.
Dan Willemsen6f037522018-10-21 09:20:47 -070038//
39// variables controlled by soong_ui directly are now returned without needing
40// to call into make, to retain compatibility.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070041func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) {
Dan Willemsen6f037522018-10-21 09:20:47 -070042 soongUiVars := map[string]func() string{
43 "OUT_DIR": func() string { return config.OutDir() },
44 "DIST_DIR": func() string { return config.DistDir() },
Dan Willemsenb6699a12019-10-07 15:26:26 -070045 "TMPDIR": func() string { return absPath(ctx, config.TempDir()) },
Dan Willemsen6f037522018-10-21 09:20:47 -070046 }
47
48 makeVars := make([]string, 0, len(vars))
49 for _, v := range vars {
50 if _, ok := soongUiVars[v]; !ok {
51 makeVars = append(makeVars, v)
52 }
53 }
54
55 var ret map[string]string
56 if len(makeVars) > 0 {
Dan Willemsen4e2456b2019-10-03 16:45:58 -070057 tmpDir, err := ioutil.TempDir("", "dumpvars")
58 if err != nil {
59 return nil, err
60 }
61 defer os.RemoveAll(tmpDir)
62
63 // It's not safe to use the same TMPDIR as the build, as that can be removed.
64 config.Environment().Set("TMPDIR", tmpDir)
65
66 SetupLitePath(ctx, config)
67
Dan Willemsen6f037522018-10-21 09:20:47 -070068 ret, err = dumpMakeVars(ctx, config, goals, makeVars, false)
69 if err != nil {
70 return ret, err
71 }
72 } else {
73 ret = make(map[string]string)
74 }
75
76 for _, v := range vars {
77 if f, ok := soongUiVars[v]; ok {
78 ret[v] = f()
79 }
80 }
81
82 return ret, nil
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070083}
84
85func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
Nan Zhang17f27672018-12-12 16:01:49 -080086 ctx.BeginTrace(metrics.RunKati, "dumpvars")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070087 defer ctx.EndTrace()
88
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070089 cmd := Command(ctx, config, "dumpvars",
90 config.PrebuiltBuildTool("ckati"),
91 "-f", "build/make/core/config.mk",
92 "--color_warnings",
Dan Willemsen0c518512018-01-09 02:09:52 -080093 "--kati_stats",
Dan Willemsen1e704462016-08-21 15:17:17 -070094 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070095 "MAKECMDGOALS="+strings.Join(goals, " "))
96 cmd.Environment.Set("CALLED_FROM_SETUP", "true")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070097 if write_soong_vars {
98 cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true")
99 }
100 cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
101 cmd.Sandbox = dumpvarsSandbox
Dan Willemsen0c518512018-01-09 02:09:52 -0800102 output := bytes.Buffer{}
103 cmd.Stdout = &output
104 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -0700105 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -0800106 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -0700107 }
Dan Willemsen0c518512018-01-09 02:09:52 -0800108 cmd.StartOrFatal()
109 // TODO: error out when Stderr contains any content
Dan Willemsenb82471a2018-05-17 16:37:09 -0700110 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen0c518512018-01-09 02:09:52 -0800111 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700112
113 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -0800114 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -0700115 if len(line) == 0 {
116 continue
117 }
118
119 if key, value, ok := decodeKeyValue(line); ok {
120 if value, ok = singleUnquote(value); ok {
121 ret[key] = value
122 ctx.Verboseln(key, value)
123 } else {
124 return nil, fmt.Errorf("Failed to parse make line: %q", line)
125 }
126 } else {
127 return nil, fmt.Errorf("Failed to parse make line: %q", line)
128 }
129 }
Nan Zhang17f27672018-12-12 16:01:49 -0800130 if ctx.Metrics != nil {
131 ctx.Metrics.SetMetadataMetrics(ret)
132 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700133
134 return ret, nil
135}
136
Dan Willemsen051133b2017-07-14 11:29:29 -0700137// Variables to print out in the top banner
138var BannerVars = []string{
139 "PLATFORM_VERSION_CODENAME",
140 "PLATFORM_VERSION",
141 "TARGET_PRODUCT",
142 "TARGET_BUILD_VARIANT",
143 "TARGET_BUILD_TYPE",
144 "TARGET_BUILD_APPS",
145 "TARGET_ARCH",
146 "TARGET_ARCH_VARIANT",
147 "TARGET_CPU_VARIANT",
148 "TARGET_2ND_ARCH",
149 "TARGET_2ND_ARCH_VARIANT",
150 "TARGET_2ND_CPU_VARIANT",
151 "HOST_ARCH",
152 "HOST_2ND_ARCH",
153 "HOST_OS",
154 "HOST_OS_EXTRA",
155 "HOST_CROSS_OS",
156 "HOST_CROSS_ARCH",
157 "HOST_CROSS_2ND_ARCH",
158 "HOST_BUILD_TYPE",
159 "BUILD_ID",
160 "OUT_DIR",
161 "AUX_OS_VARIANT_LIST",
162 "TARGET_BUILD_PDK",
163 "PDK_FUSION_PLATFORM_ZIP",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800164 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700165}
166
167func Banner(make_vars map[string]string) string {
168 b := &bytes.Buffer{}
169
170 fmt.Fprintln(b, "============================================")
171 for _, name := range BannerVars {
172 if make_vars[name] != "" {
173 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
174 }
175 }
176 fmt.Fprint(b, "============================================")
177
178 return b.String()
179}
180
Dan Willemsen1e704462016-08-21 15:17:17 -0700181func runMakeProductConfig(ctx Context, config Config) {
182 // Variables to export into the environment of Kati/Ninja
183 exportEnvVars := []string{
184 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsena2a8ecb2019-07-29 15:14:11 -0700185 // modified by a buildspec.mk
Dan Willemsen1e704462016-08-21 15:17:17 -0700186 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700187 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700188 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700189
190 // compiler wrappers set up by make
191 "CC_WRAPPER",
192 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900193 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700194
195 // ccache settings
196 "CCACHE_COMPILERCHECK",
197 "CCACHE_SLOPPINESS",
198 "CCACHE_BASEDIR",
199 "CCACHE_CPP2",
200 }
201
Dan Willemsen1e704462016-08-21 15:17:17 -0700202 allVars := append(append([]string{
203 // Used to execute Kati and Ninja
204 "NINJA_GOALS",
205 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700206
207 // To find target/product/<DEVICE>
208 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700209
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700210 // So that later Kati runs can find BoardConfig.mk faster
211 "TARGET_DEVICE_DIR",
212
Dan Willemsen3d60b112018-04-04 22:25:56 -0700213 // Whether --werror_overriding_commands will work
214 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700215
Dan Willemsen25e6f092019-04-09 10:22:43 -0700216 // Whether to enable the network during the build
217 "BUILD_BROKEN_USES_NETWORK",
218
Dan Willemsenb58f1202018-06-21 10:12:53 -0700219 // Not used, but useful to be in the soong.log
Steven Moreland00020282019-03-07 09:27:27 -0800220 "BOARD_VNDK_VERSION",
Dan Willemsen368e5562019-04-17 14:44:33 -0700221
222 "DEFAULT_WARNING_BUILD_MODULE_TYPES",
223 "DEFAULT_ERROR_BUILD_MODULE_TYPES",
Logan Chienf9cf9ac2019-09-20 11:37:30 -0700224 "BUILD_BROKEN_PREBUILT_ELF_FILES",
Dan Willemsen368e5562019-04-17 14:44:33 -0700225 "BUILD_BROKEN_USES_BUILD_AUX_EXECUTABLE",
226 "BUILD_BROKEN_USES_BUILD_AUX_STATIC_LIBRARY",
227 "BUILD_BROKEN_USES_BUILD_COPY_HEADERS",
228 "BUILD_BROKEN_USES_BUILD_EXECUTABLE",
229 "BUILD_BROKEN_USES_BUILD_FUZZ_TEST",
230 "BUILD_BROKEN_USES_BUILD_HEADER_LIBRARY",
231 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_JAVA_LIBRARY",
232 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY",
233 "BUILD_BROKEN_USES_BUILD_HOST_EXECUTABLE",
234 "BUILD_BROKEN_USES_BUILD_HOST_FUZZ_TEST",
235 "BUILD_BROKEN_USES_BUILD_HOST_JAVA_LIBRARY",
236 "BUILD_BROKEN_USES_BUILD_HOST_NATIVE_TEST",
237 "BUILD_BROKEN_USES_BUILD_HOST_PREBUILT",
238 "BUILD_BROKEN_USES_BUILD_HOST_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700239 "BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY",
240 "BUILD_BROKEN_USES_BUILD_HOST_STATIC_TEST_LIBRARY",
241 "BUILD_BROKEN_USES_BUILD_HOST_TEST_CONFIG",
242 "BUILD_BROKEN_USES_BUILD_JAVA_LIBRARY",
243 "BUILD_BROKEN_USES_BUILD_MULTI_PREBUILT",
244 "BUILD_BROKEN_USES_BUILD_NATIVE_BENCHMARK",
245 "BUILD_BROKEN_USES_BUILD_NATIVE_TEST",
246 "BUILD_BROKEN_USES_BUILD_NOTICE_FILE",
247 "BUILD_BROKEN_USES_BUILD_PACKAGE",
248 "BUILD_BROKEN_USES_BUILD_PHONY_PACKAGE",
249 "BUILD_BROKEN_USES_BUILD_PREBUILT",
250 "BUILD_BROKEN_USES_BUILD_RRO_PACKAGE",
251 "BUILD_BROKEN_USES_BUILD_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700252 "BUILD_BROKEN_USES_BUILD_STATIC_JAVA_LIBRARY",
253 "BUILD_BROKEN_USES_BUILD_STATIC_LIBRARY",
254 "BUILD_BROKEN_USES_BUILD_STATIC_TEST_LIBRARY",
255 "BUILD_BROKEN_USES_BUILD_TARGET_TEST_CONFIG",
Dan Willemsen051133b2017-07-14 11:29:29 -0700256 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700257
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700258 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700259 if err != nil {
260 ctx.Fatalln("Error dumping make vars:", err)
261 }
262
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800263 env := config.Environment()
Dan Willemsen1e704462016-08-21 15:17:17 -0700264 // Print the banner like make does
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800265 if !env.IsEnvTrue("ANDROID_QUIET_BUILD") {
Colin Cross097ed2a2019-06-08 21:48:58 -0700266 fmt.Fprintln(ctx.Writer, Banner(make_vars))
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800267 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700268
269 // Populate the environment
Dan Willemsen1e704462016-08-21 15:17:17 -0700270 for _, name := range exportEnvVars {
271 if make_vars[name] == "" {
272 env.Unset(name)
273 } else {
274 env.Set(name, make_vars[name])
275 }
276 }
277
278 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
279 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
Dan Willemsen02781d52017-05-12 19:28:13 -0700280 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700281 config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"])
Dan Willemsen3d60b112018-04-04 22:25:56 -0700282
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700283 config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true")
Dan Willemsen9dc69a42018-06-22 13:18:06 -0700284 config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] == "true")
Dan Willemsen25e6f092019-04-09 10:22:43 -0700285 config.SetBuildBrokenUsesNetwork(make_vars["BUILD_BROKEN_USES_NETWORK"] == "true")
Dan Willemsen1e704462016-08-21 15:17:17 -0700286}