blob: 5c8d30e9c492c26a6ef930c3016a877eaee24fb7 [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",
Martin Stjernholm08802332020-06-04 17:00:01 +0100145 "TARGET_BUILD_UNBUNDLED",
Dan Willemsen051133b2017-07-14 11:29:29 -0700146 "TARGET_ARCH",
147 "TARGET_ARCH_VARIANT",
148 "TARGET_CPU_VARIANT",
149 "TARGET_2ND_ARCH",
150 "TARGET_2ND_ARCH_VARIANT",
151 "TARGET_2ND_CPU_VARIANT",
152 "HOST_ARCH",
153 "HOST_2ND_ARCH",
154 "HOST_OS",
155 "HOST_OS_EXTRA",
156 "HOST_CROSS_OS",
157 "HOST_CROSS_ARCH",
158 "HOST_CROSS_2ND_ARCH",
159 "HOST_BUILD_TYPE",
160 "BUILD_ID",
161 "OUT_DIR",
162 "AUX_OS_VARIANT_LIST",
163 "TARGET_BUILD_PDK",
164 "PDK_FUSION_PLATFORM_ZIP",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800165 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700166}
167
168func Banner(make_vars map[string]string) string {
169 b := &bytes.Buffer{}
170
171 fmt.Fprintln(b, "============================================")
172 for _, name := range BannerVars {
173 if make_vars[name] != "" {
174 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
175 }
176 }
177 fmt.Fprint(b, "============================================")
178
179 return b.String()
180}
181
Dan Willemsen1e704462016-08-21 15:17:17 -0700182func runMakeProductConfig(ctx Context, config Config) {
183 // Variables to export into the environment of Kati/Ninja
184 exportEnvVars := []string{
185 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsena2a8ecb2019-07-29 15:14:11 -0700186 // modified by a buildspec.mk
Dan Willemsen1e704462016-08-21 15:17:17 -0700187 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700188 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700189 "TARGET_BUILD_APPS",
Martin Stjernholm08802332020-06-04 17:00:01 +0100190 "TARGET_BUILD_UNBUNDLED",
Dan Willemsen1e704462016-08-21 15:17:17 -0700191
192 // compiler wrappers set up by make
193 "CC_WRAPPER",
194 "CXX_WRAPPER",
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400195 "RBE_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900196 "JAVAC_WRAPPER",
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500197 "R8_WRAPPER",
198 "D8_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700199
200 // ccache settings
201 "CCACHE_COMPILERCHECK",
202 "CCACHE_SLOPPINESS",
203 "CCACHE_BASEDIR",
204 "CCACHE_CPP2",
205 }
206
Dan Willemsen1e704462016-08-21 15:17:17 -0700207 allVars := append(append([]string{
208 // Used to execute Kati and Ninja
209 "NINJA_GOALS",
210 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700211
212 // To find target/product/<DEVICE>
213 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700214
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700215 // So that later Kati runs can find BoardConfig.mk faster
216 "TARGET_DEVICE_DIR",
217
Dan Willemsen3d60b112018-04-04 22:25:56 -0700218 // Whether --werror_overriding_commands will work
219 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700220
Dan Willemsen25e6f092019-04-09 10:22:43 -0700221 // Whether to enable the network during the build
222 "BUILD_BROKEN_USES_NETWORK",
223
Dan Willemsene3336352020-01-02 19:10:38 -0800224 // Extra environment variables to be exported to ninja
225 "BUILD_BROKEN_NINJA_USES_ENV_VARS",
226
Dan Willemsenb58f1202018-06-21 10:12:53 -0700227 // Not used, but useful to be in the soong.log
Steven Moreland00020282019-03-07 09:27:27 -0800228 "BOARD_VNDK_VERSION",
Dan Willemsen368e5562019-04-17 14:44:33 -0700229
230 "DEFAULT_WARNING_BUILD_MODULE_TYPES",
231 "DEFAULT_ERROR_BUILD_MODULE_TYPES",
Logan Chienf9cf9ac2019-09-20 11:37:30 -0700232 "BUILD_BROKEN_PREBUILT_ELF_FILES",
Inseob Kim822fdca2019-10-11 14:55:33 +0900233 "BUILD_BROKEN_TREBLE_SYSPROP_NEVERALLOW",
Dan Willemsen368e5562019-04-17 14:44:33 -0700234 "BUILD_BROKEN_USES_BUILD_COPY_HEADERS",
235 "BUILD_BROKEN_USES_BUILD_EXECUTABLE",
236 "BUILD_BROKEN_USES_BUILD_FUZZ_TEST",
237 "BUILD_BROKEN_USES_BUILD_HEADER_LIBRARY",
238 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_JAVA_LIBRARY",
239 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY",
240 "BUILD_BROKEN_USES_BUILD_HOST_EXECUTABLE",
Dan Willemsen368e5562019-04-17 14:44:33 -0700241 "BUILD_BROKEN_USES_BUILD_HOST_JAVA_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700242 "BUILD_BROKEN_USES_BUILD_HOST_PREBUILT",
243 "BUILD_BROKEN_USES_BUILD_HOST_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700244 "BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700245 "BUILD_BROKEN_USES_BUILD_JAVA_LIBRARY",
246 "BUILD_BROKEN_USES_BUILD_MULTI_PREBUILT",
Dan Willemsen368e5562019-04-17 14:44:33 -0700247 "BUILD_BROKEN_USES_BUILD_NATIVE_TEST",
248 "BUILD_BROKEN_USES_BUILD_NOTICE_FILE",
249 "BUILD_BROKEN_USES_BUILD_PACKAGE",
250 "BUILD_BROKEN_USES_BUILD_PHONY_PACKAGE",
251 "BUILD_BROKEN_USES_BUILD_PREBUILT",
252 "BUILD_BROKEN_USES_BUILD_RRO_PACKAGE",
253 "BUILD_BROKEN_USES_BUILD_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700254 "BUILD_BROKEN_USES_BUILD_STATIC_JAVA_LIBRARY",
255 "BUILD_BROKEN_USES_BUILD_STATIC_LIBRARY",
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 Willemsene3336352020-01-02 19:10:38 -0800286 config.SetBuildBrokenNinjaUsesEnvVars(strings.Fields(make_vars["BUILD_BROKEN_NINJA_USES_ENV_VARS"]))
Dan Willemsen1e704462016-08-21 15:17:17 -0700287}