Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 18 | "bytes" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 19 | "fmt" |
Dan Willemsen | 4e2456b | 2019-10-03 16:45:58 -0700 | [diff] [blame] | 20 | "io/ioutil" |
| 21 | "os" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 22 | "strings" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 23 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 24 | "android/soong/ui/metrics" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 25 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 26 | ) |
| 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 36 | // vars is the list of variables to read. The values will be put in the |
| 37 | // returned map. |
Dan Willemsen | 6f03752 | 2018-10-21 09:20:47 -0700 | [diff] [blame] | 38 | // |
| 39 | // variables controlled by soong_ui directly are now returned without needing |
| 40 | // to call into make, to retain compatibility. |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 41 | func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) { |
Dan Willemsen | 6f03752 | 2018-10-21 09:20:47 -0700 | [diff] [blame] | 42 | soongUiVars := map[string]func() string{ |
| 43 | "OUT_DIR": func() string { return config.OutDir() }, |
| 44 | "DIST_DIR": func() string { return config.DistDir() }, |
Dan Willemsen | b6699a1 | 2019-10-07 15:26:26 -0700 | [diff] [blame] | 45 | "TMPDIR": func() string { return absPath(ctx, config.TempDir()) }, |
Dan Willemsen | 6f03752 | 2018-10-21 09:20:47 -0700 | [diff] [blame] | 46 | } |
| 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 Willemsen | 4e2456b | 2019-10-03 16:45:58 -0700 | [diff] [blame] | 57 | 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 Willemsen | 6f03752 | 2018-10-21 09:20:47 -0700 | [diff] [blame] | 68 | 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 Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) { |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 86 | ctx.BeginTrace(metrics.RunKati, "dumpvars") |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 87 | defer ctx.EndTrace() |
| 88 | |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 89 | cmd := Command(ctx, config, "dumpvars", |
| 90 | config.PrebuiltBuildTool("ckati"), |
| 91 | "-f", "build/make/core/config.mk", |
| 92 | "--color_warnings", |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 93 | "--kati_stats", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 94 | "dump-many-vars", |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 95 | "MAKECMDGOALS="+strings.Join(goals, " ")) |
| 96 | cmd.Environment.Set("CALLED_FROM_SETUP", "true") |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 97 | 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 Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 102 | output := bytes.Buffer{} |
| 103 | cmd.Stdout = &output |
| 104 | pipe, err := cmd.StderrPipe() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 105 | if err != nil { |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 106 | ctx.Fatalln("Error getting output pipe for ckati:", err) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 107 | } |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 108 | cmd.StartOrFatal() |
| 109 | // TODO: error out when Stderr contains any content |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 110 | status.KatiReader(ctx.Status.StartTool(), pipe) |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 111 | cmd.WaitOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 112 | |
| 113 | ret := make(map[string]string, len(vars)) |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 114 | for _, line := range strings.Split(output.String(), "\n") { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 115 | 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 Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 130 | if ctx.Metrics != nil { |
| 131 | ctx.Metrics.SetMetadataMetrics(ret) |
| 132 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 133 | |
| 134 | return ret, nil |
| 135 | } |
| 136 | |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 137 | // Variables to print out in the top banner |
| 138 | var 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 Stjernholm | 0880233 | 2020-06-04 17:00:01 +0100 | [diff] [blame^] | 145 | "TARGET_BUILD_UNBUNDLED", |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 146 | "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 Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 165 | "PRODUCT_SOONG_NAMESPACES", |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | func 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 182 | func 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 Willemsen | a2a8ecb | 2019-07-29 15:14:11 -0700 | [diff] [blame] | 186 | // modified by a buildspec.mk |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 187 | "TARGET_PRODUCT", |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 188 | "TARGET_BUILD_VARIANT", |
Dan Willemsen | 04a16c7 | 2017-05-25 22:18:57 -0700 | [diff] [blame] | 189 | "TARGET_BUILD_APPS", |
Martin Stjernholm | 0880233 | 2020-06-04 17:00:01 +0100 | [diff] [blame^] | 190 | "TARGET_BUILD_UNBUNDLED", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 191 | |
| 192 | // compiler wrappers set up by make |
| 193 | "CC_WRAPPER", |
| 194 | "CXX_WRAPPER", |
Ramy Medhat | 9a90fe5 | 2020-04-13 13:21:23 -0400 | [diff] [blame] | 195 | "RBE_WRAPPER", |
Yoshisato Yanagisawa | 13fd3ff | 2017-04-05 11:05:31 +0900 | [diff] [blame] | 196 | "JAVAC_WRAPPER", |
Ramy Medhat | 8ea054a | 2020-01-27 14:19:44 -0500 | [diff] [blame] | 197 | "R8_WRAPPER", |
| 198 | "D8_WRAPPER", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 199 | |
| 200 | // ccache settings |
| 201 | "CCACHE_COMPILERCHECK", |
| 202 | "CCACHE_SLOPPINESS", |
| 203 | "CCACHE_BASEDIR", |
| 204 | "CCACHE_CPP2", |
| 205 | } |
| 206 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 207 | allVars := append(append([]string{ |
| 208 | // Used to execute Kati and Ninja |
| 209 | "NINJA_GOALS", |
| 210 | "KATI_GOALS", |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 211 | |
| 212 | // To find target/product/<DEVICE> |
| 213 | "TARGET_DEVICE", |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 214 | |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 215 | // So that later Kati runs can find BoardConfig.mk faster |
| 216 | "TARGET_DEVICE_DIR", |
| 217 | |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 218 | // Whether --werror_overriding_commands will work |
| 219 | "BUILD_BROKEN_DUP_RULES", |
Dan Willemsen | b58f120 | 2018-06-21 10:12:53 -0700 | [diff] [blame] | 220 | |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 221 | // Whether to enable the network during the build |
| 222 | "BUILD_BROKEN_USES_NETWORK", |
| 223 | |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 224 | // Extra environment variables to be exported to ninja |
| 225 | "BUILD_BROKEN_NINJA_USES_ENV_VARS", |
| 226 | |
Dan Willemsen | b58f120 | 2018-06-21 10:12:53 -0700 | [diff] [blame] | 227 | // Not used, but useful to be in the soong.log |
Steven Moreland | 0002028 | 2019-03-07 09:27:27 -0800 | [diff] [blame] | 228 | "BOARD_VNDK_VERSION", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 229 | |
| 230 | "DEFAULT_WARNING_BUILD_MODULE_TYPES", |
| 231 | "DEFAULT_ERROR_BUILD_MODULE_TYPES", |
Logan Chien | f9cf9ac | 2019-09-20 11:37:30 -0700 | [diff] [blame] | 232 | "BUILD_BROKEN_PREBUILT_ELF_FILES", |
Inseob Kim | 822fdca | 2019-10-11 14:55:33 +0900 | [diff] [blame] | 233 | "BUILD_BROKEN_TREBLE_SYSPROP_NEVERALLOW", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 234 | "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 Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 241 | "BUILD_BROKEN_USES_BUILD_HOST_JAVA_LIBRARY", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 242 | "BUILD_BROKEN_USES_BUILD_HOST_PREBUILT", |
| 243 | "BUILD_BROKEN_USES_BUILD_HOST_SHARED_LIBRARY", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 244 | "BUILD_BROKEN_USES_BUILD_HOST_STATIC_LIBRARY", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 245 | "BUILD_BROKEN_USES_BUILD_JAVA_LIBRARY", |
| 246 | "BUILD_BROKEN_USES_BUILD_MULTI_PREBUILT", |
Dan Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 247 | "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 Willemsen | 368e556 | 2019-04-17 14:44:33 -0700 | [diff] [blame] | 254 | "BUILD_BROKEN_USES_BUILD_STATIC_JAVA_LIBRARY", |
| 255 | "BUILD_BROKEN_USES_BUILD_STATIC_LIBRARY", |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 256 | }, exportEnvVars...), BannerVars...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 257 | |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 258 | make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 259 | if err != nil { |
| 260 | ctx.Fatalln("Error dumping make vars:", err) |
| 261 | } |
| 262 | |
Sasha Smundak | c0c9ef9 | 2019-01-23 09:52:57 -0800 | [diff] [blame] | 263 | env := config.Environment() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 264 | // Print the banner like make does |
Sasha Smundak | c0c9ef9 | 2019-01-23 09:52:57 -0800 | [diff] [blame] | 265 | if !env.IsEnvTrue("ANDROID_QUIET_BUILD") { |
Colin Cross | 097ed2a | 2019-06-08 21:48:58 -0700 | [diff] [blame] | 266 | fmt.Fprintln(ctx.Writer, Banner(make_vars)) |
Sasha Smundak | c0c9ef9 | 2019-01-23 09:52:57 -0800 | [diff] [blame] | 267 | } |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 268 | |
| 269 | // Populate the environment |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 270 | 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 Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 280 | config.SetTargetDevice(make_vars["TARGET_DEVICE"]) |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 281 | config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"]) |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 282 | |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 283 | config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true") |
Dan Willemsen | 9dc69a4 | 2018-06-22 13:18:06 -0700 | [diff] [blame] | 284 | config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] == "true") |
Dan Willemsen | 25e6f09 | 2019-04-09 10:22:43 -0700 | [diff] [blame] | 285 | config.SetBuildBrokenUsesNetwork(make_vars["BUILD_BROKEN_USES_NETWORK"] == "true") |
Dan Willemsen | e333635 | 2020-01-02 19:10:38 -0800 | [diff] [blame] | 286 | config.SetBuildBrokenNinjaUsesEnvVars(strings.Fields(make_vars["BUILD_BROKEN_NINJA_USES_ENV_VARS"])) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 287 | } |