blob: 285f1569b690e64a98d576f3b932883ab719cf7e [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 {
Patrice Arrudaae2694b2020-06-04 19:34:41 +000057 // It's not safe to use the same TMPDIR as the build, as that can be removed.
Dan Willemsen4e2456b2019-10-03 16:45:58 -070058 tmpDir, err := ioutil.TempDir("", "dumpvars")
59 if err != nil {
60 return nil, err
61 }
62 defer os.RemoveAll(tmpDir)
63
Patrice Arrudaae2694b2020-06-04 19:34:41 +000064 SetupLitePath(ctx, config, tmpDir)
Dan Willemsen4e2456b2019-10-03 16:45:58 -070065
Patrice Arrudaae2694b2020-06-04 19:34:41 +000066 ret, err = dumpMakeVars(ctx, config, goals, makeVars, false, tmpDir)
Dan Willemsen6f037522018-10-21 09:20:47 -070067 if err != nil {
68 return ret, err
69 }
70 } else {
71 ret = make(map[string]string)
72 }
73
74 for _, v := range vars {
75 if f, ok := soongUiVars[v]; ok {
76 ret[v] = f()
77 }
78 }
79
80 return ret, nil
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070081}
82
Patrice Arrudaae2694b2020-06-04 19:34:41 +000083func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool, tmpDir string) (map[string]string, error) {
Nan Zhang17f27672018-12-12 16:01:49 -080084 ctx.BeginTrace(metrics.RunKati, "dumpvars")
Dan Willemsend9f6fa22016-08-21 15:17:17 -070085 defer ctx.EndTrace()
86
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070087 cmd := Command(ctx, config, "dumpvars",
88 config.PrebuiltBuildTool("ckati"),
89 "-f", "build/make/core/config.mk",
90 "--color_warnings",
Dan Willemsen0c518512018-01-09 02:09:52 -080091 "--kati_stats",
Dan Willemsen1e704462016-08-21 15:17:17 -070092 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070093 "MAKECMDGOALS="+strings.Join(goals, " "))
94 cmd.Environment.Set("CALLED_FROM_SETUP", "true")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070095 if write_soong_vars {
96 cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true")
97 }
98 cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
Patrice Arrudaae2694b2020-06-04 19:34:41 +000099 if tmpDir != "" {
100 cmd.Environment.Set("TMPDIR", tmpDir)
101 }
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700102 cmd.Sandbox = dumpvarsSandbox
Dan Willemsen0c518512018-01-09 02:09:52 -0800103 output := bytes.Buffer{}
104 cmd.Stdout = &output
105 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -0700106 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -0800107 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -0700108 }
Dan Willemsen0c518512018-01-09 02:09:52 -0800109 cmd.StartOrFatal()
110 // TODO: error out when Stderr contains any content
Dan Willemsenb82471a2018-05-17 16:37:09 -0700111 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen0c518512018-01-09 02:09:52 -0800112 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -0700113
114 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -0800115 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -0700116 if len(line) == 0 {
117 continue
118 }
119
120 if key, value, ok := decodeKeyValue(line); ok {
121 if value, ok = singleUnquote(value); ok {
122 ret[key] = value
123 ctx.Verboseln(key, value)
124 } else {
125 return nil, fmt.Errorf("Failed to parse make line: %q", line)
126 }
127 } else {
128 return nil, fmt.Errorf("Failed to parse make line: %q", line)
129 }
130 }
Nan Zhang17f27672018-12-12 16:01:49 -0800131 if ctx.Metrics != nil {
132 ctx.Metrics.SetMetadataMetrics(ret)
133 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700134
135 return ret, nil
136}
137
Dan Willemsen051133b2017-07-14 11:29:29 -0700138// Variables to print out in the top banner
139var BannerVars = []string{
140 "PLATFORM_VERSION_CODENAME",
141 "PLATFORM_VERSION",
142 "TARGET_PRODUCT",
143 "TARGET_BUILD_VARIANT",
144 "TARGET_BUILD_TYPE",
145 "TARGET_BUILD_APPS",
Martin Stjernholm08802332020-06-04 17:00:01 +0100146 "TARGET_BUILD_UNBUNDLED",
Dan Willemsen051133b2017-07-14 11:29:29 -0700147 "TARGET_ARCH",
148 "TARGET_ARCH_VARIANT",
149 "TARGET_CPU_VARIANT",
150 "TARGET_2ND_ARCH",
151 "TARGET_2ND_ARCH_VARIANT",
152 "TARGET_2ND_CPU_VARIANT",
153 "HOST_ARCH",
154 "HOST_2ND_ARCH",
155 "HOST_OS",
156 "HOST_OS_EXTRA",
157 "HOST_CROSS_OS",
158 "HOST_CROSS_ARCH",
159 "HOST_CROSS_2ND_ARCH",
160 "HOST_BUILD_TYPE",
161 "BUILD_ID",
162 "OUT_DIR",
163 "AUX_OS_VARIANT_LIST",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800164 "PRODUCT_SOONG_NAMESPACES",
Paul Duffin43f7bf02021-05-05 22:00:51 +0100165 "SOONG_SDK_SNAPSHOT_PREFER",
Paul Duffin39abf8f2021-09-24 14:58:27 +0100166 "SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE",
Paul Duffinfb9a7f92021-07-06 17:18:42 +0100167 "SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR",
Paul Duffin43f7bf02021-05-05 22:00:51 +0100168 "SOONG_SDK_SNAPSHOT_VERSION",
Dan Willemsen051133b2017-07-14 11:29:29 -0700169}
170
171func Banner(make_vars map[string]string) string {
172 b := &bytes.Buffer{}
173
174 fmt.Fprintln(b, "============================================")
175 for _, name := range BannerVars {
176 if make_vars[name] != "" {
177 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
178 }
179 }
180 fmt.Fprint(b, "============================================")
181
182 return b.String()
183}
184
Dan Willemsen1e704462016-08-21 15:17:17 -0700185func runMakeProductConfig(ctx Context, config Config) {
186 // Variables to export into the environment of Kati/Ninja
187 exportEnvVars := []string{
188 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsena2a8ecb2019-07-29 15:14:11 -0700189 // modified by a buildspec.mk
Dan Willemsen1e704462016-08-21 15:17:17 -0700190 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700191 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700192 "TARGET_BUILD_APPS",
Martin Stjernholm08802332020-06-04 17:00:01 +0100193 "TARGET_BUILD_UNBUNDLED",
Dan Willemsen1e704462016-08-21 15:17:17 -0700194
195 // compiler wrappers set up by make
196 "CC_WRAPPER",
197 "CXX_WRAPPER",
Ramy Medhat9a90fe52020-04-13 13:21:23 -0400198 "RBE_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900199 "JAVAC_WRAPPER",
Ramy Medhat8ea054a2020-01-27 14:19:44 -0500200 "R8_WRAPPER",
201 "D8_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700202
203 // ccache settings
204 "CCACHE_COMPILERCHECK",
205 "CCACHE_SLOPPINESS",
206 "CCACHE_BASEDIR",
207 "CCACHE_CPP2",
Yi Kong6adf2582022-04-17 15:01:06 +0800208
209 // LLVM compiler wrapper options
210 "TOOLCHAIN_RUSAGE_OUTPUT",
Dan Willemsen1e704462016-08-21 15:17:17 -0700211 }
212
Dan Willemsen1e704462016-08-21 15:17:17 -0700213 allVars := append(append([]string{
214 // Used to execute Kati and Ninja
215 "NINJA_GOALS",
216 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700217
218 // To find target/product/<DEVICE>
219 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700220
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700221 // So that later Kati runs can find BoardConfig.mk faster
222 "TARGET_DEVICE_DIR",
223
Dan Willemsen3d60b112018-04-04 22:25:56 -0700224 // Whether --werror_overriding_commands will work
225 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700226
Dan Willemsen25e6f092019-04-09 10:22:43 -0700227 // Whether to enable the network during the build
228 "BUILD_BROKEN_USES_NETWORK",
229
Dan Willemsene3336352020-01-02 19:10:38 -0800230 // Extra environment variables to be exported to ninja
231 "BUILD_BROKEN_NINJA_USES_ENV_VARS",
232
Spandan Dasa3639e62021-05-25 19:14:02 +0000233 // Used to restrict write access to source tree
234 "BUILD_BROKEN_SRC_DIR_IS_WRITABLE",
235 "BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST",
236
Dan Willemsenb58f1202018-06-21 10:12:53 -0700237 // Not used, but useful to be in the soong.log
Steven Moreland00020282019-03-07 09:27:27 -0800238 "BOARD_VNDK_VERSION",
Dan Willemsen368e5562019-04-17 14:44:33 -0700239
240 "DEFAULT_WARNING_BUILD_MODULE_TYPES",
241 "DEFAULT_ERROR_BUILD_MODULE_TYPES",
Logan Chienf9cf9ac2019-09-20 11:37:30 -0700242 "BUILD_BROKEN_PREBUILT_ELF_FILES",
Inseob Kim822fdca2019-10-11 14:55:33 +0900243 "BUILD_BROKEN_TREBLE_SYSPROP_NEVERALLOW",
Dan Willemsen368e5562019-04-17 14:44:33 -0700244 "BUILD_BROKEN_USES_BUILD_COPY_HEADERS",
245 "BUILD_BROKEN_USES_BUILD_EXECUTABLE",
246 "BUILD_BROKEN_USES_BUILD_FUZZ_TEST",
247 "BUILD_BROKEN_USES_BUILD_HEADER_LIBRARY",
248 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_JAVA_LIBRARY",
249 "BUILD_BROKEN_USES_BUILD_HOST_DALVIK_STATIC_JAVA_LIBRARY",
250 "BUILD_BROKEN_USES_BUILD_HOST_EXECUTABLE",
Dan Willemsen368e5562019-04-17 14:44:33 -0700251 "BUILD_BROKEN_USES_BUILD_HOST_JAVA_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700252 "BUILD_BROKEN_USES_BUILD_HOST_PREBUILT",
253 "BUILD_BROKEN_USES_BUILD_HOST_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700254 "BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700255 "BUILD_BROKEN_USES_BUILD_JAVA_LIBRARY",
256 "BUILD_BROKEN_USES_BUILD_MULTI_PREBUILT",
Dan Willemsen368e5562019-04-17 14:44:33 -0700257 "BUILD_BROKEN_USES_BUILD_NATIVE_TEST",
258 "BUILD_BROKEN_USES_BUILD_NOTICE_FILE",
259 "BUILD_BROKEN_USES_BUILD_PACKAGE",
260 "BUILD_BROKEN_USES_BUILD_PHONY_PACKAGE",
261 "BUILD_BROKEN_USES_BUILD_PREBUILT",
262 "BUILD_BROKEN_USES_BUILD_RRO_PACKAGE",
263 "BUILD_BROKEN_USES_BUILD_SHARED_LIBRARY",
Dan Willemsen368e5562019-04-17 14:44:33 -0700264 "BUILD_BROKEN_USES_BUILD_STATIC_JAVA_LIBRARY",
265 "BUILD_BROKEN_USES_BUILD_STATIC_LIBRARY",
Dan Willemsen051133b2017-07-14 11:29:29 -0700266 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700267
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800268 makeVars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true, "")
Dan Willemsen1e704462016-08-21 15:17:17 -0700269 if err != nil {
270 ctx.Fatalln("Error dumping make vars:", err)
271 }
272
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800273 env := config.Environment()
Dan Willemsen1e704462016-08-21 15:17:17 -0700274 // Print the banner like make does
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800275 if !env.IsEnvTrue("ANDROID_QUIET_BUILD") {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800276 fmt.Fprintln(ctx.Writer, Banner(makeVars))
Sasha Smundakc0c9ef92019-01-23 09:52:57 -0800277 }
Dan Willemsen1e704462016-08-21 15:17:17 -0700278
279 // Populate the environment
Dan Willemsen1e704462016-08-21 15:17:17 -0700280 for _, name := range exportEnvVars {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800281 if makeVars[name] == "" {
Dan Willemsen1e704462016-08-21 15:17:17 -0700282 env.Unset(name)
283 } else {
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800284 env.Set(name, makeVars[name])
Dan Willemsen1e704462016-08-21 15:17:17 -0700285 }
286 }
287
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800288 config.SetKatiArgs(strings.Fields(makeVars["KATI_GOALS"]))
289 config.SetNinjaArgs(strings.Fields(makeVars["NINJA_GOALS"]))
290 config.SetTargetDevice(makeVars["TARGET_DEVICE"])
291 config.SetTargetDeviceDir(makeVars["TARGET_DEVICE_DIR"])
Spandan Dasa3639e62021-05-25 19:14:02 +0000292 config.sandboxConfig.SetSrcDirIsRO(makeVars["BUILD_BROKEN_SRC_DIR_IS_WRITABLE"] == "false")
293 config.sandboxConfig.SetSrcDirRWAllowlist(strings.Fields(makeVars["BUILD_BROKEN_SRC_DIR_RW_ALLOWLIST"]))
Dan Willemsen3d60b112018-04-04 22:25:56 -0700294
Jaewoong Jung18aefc12020-12-21 09:11:10 -0800295 config.SetBuildBrokenDupRules(makeVars["BUILD_BROKEN_DUP_RULES"] == "true")
296 config.SetBuildBrokenUsesNetwork(makeVars["BUILD_BROKEN_USES_NETWORK"] == "true")
297 config.SetBuildBrokenNinjaUsesEnvVars(strings.Fields(makeVars["BUILD_BROKEN_NINJA_USES_ENV_VARS"]))
Dan Willemsen1e704462016-08-21 15:17:17 -0700298}