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 | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 20 | "strings" |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 21 | |
| 22 | "android/soong/ui/status" |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 23 | ) |
| 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 Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 33 | // vars is the list of variables to read. The values will be put in the |
| 34 | // returned map. |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 35 | func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) { |
| 36 | return dumpMakeVars(ctx, config, goals, vars, false) |
| 37 | } |
| 38 | |
| 39 | func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) { |
Dan Willemsen | d9f6fa2 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 40 | ctx.BeginTrace("dumpvars") |
| 41 | defer ctx.EndTrace() |
| 42 | |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 43 | cmd := Command(ctx, config, "dumpvars", |
| 44 | config.PrebuiltBuildTool("ckati"), |
| 45 | "-f", "build/make/core/config.mk", |
| 46 | "--color_warnings", |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 47 | "--kati_stats", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 48 | "dump-many-vars", |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 49 | "MAKECMDGOALS="+strings.Join(goals, " ")) |
| 50 | cmd.Environment.Set("CALLED_FROM_SETUP", "true") |
| 51 | cmd.Environment.Set("BUILD_SYSTEM", "build/make/core") |
| 52 | if write_soong_vars { |
| 53 | cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true") |
| 54 | } |
| 55 | cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " ")) |
| 56 | cmd.Sandbox = dumpvarsSandbox |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 57 | output := bytes.Buffer{} |
| 58 | cmd.Stdout = &output |
| 59 | pipe, err := cmd.StderrPipe() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 60 | if err != nil { |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 61 | ctx.Fatalln("Error getting output pipe for ckati:", err) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 62 | } |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 63 | cmd.StartOrFatal() |
| 64 | // TODO: error out when Stderr contains any content |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 65 | status.KatiReader(ctx.Status.StartTool(), pipe) |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 66 | cmd.WaitOrFatal() |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 67 | |
| 68 | ret := make(map[string]string, len(vars)) |
Dan Willemsen | 0c51851 | 2018-01-09 02:09:52 -0800 | [diff] [blame] | 69 | for _, line := range strings.Split(output.String(), "\n") { |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 70 | if len(line) == 0 { |
| 71 | continue |
| 72 | } |
| 73 | |
| 74 | if key, value, ok := decodeKeyValue(line); ok { |
| 75 | if value, ok = singleUnquote(value); ok { |
| 76 | ret[key] = value |
| 77 | ctx.Verboseln(key, value) |
| 78 | } else { |
| 79 | return nil, fmt.Errorf("Failed to parse make line: %q", line) |
| 80 | } |
| 81 | } else { |
| 82 | return nil, fmt.Errorf("Failed to parse make line: %q", line) |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return ret, nil |
| 87 | } |
| 88 | |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 89 | // Variables to print out in the top banner |
| 90 | var BannerVars = []string{ |
| 91 | "PLATFORM_VERSION_CODENAME", |
| 92 | "PLATFORM_VERSION", |
| 93 | "TARGET_PRODUCT", |
| 94 | "TARGET_BUILD_VARIANT", |
| 95 | "TARGET_BUILD_TYPE", |
| 96 | "TARGET_BUILD_APPS", |
| 97 | "TARGET_ARCH", |
| 98 | "TARGET_ARCH_VARIANT", |
| 99 | "TARGET_CPU_VARIANT", |
| 100 | "TARGET_2ND_ARCH", |
| 101 | "TARGET_2ND_ARCH_VARIANT", |
| 102 | "TARGET_2ND_CPU_VARIANT", |
| 103 | "HOST_ARCH", |
| 104 | "HOST_2ND_ARCH", |
| 105 | "HOST_OS", |
| 106 | "HOST_OS_EXTRA", |
| 107 | "HOST_CROSS_OS", |
| 108 | "HOST_CROSS_ARCH", |
| 109 | "HOST_CROSS_2ND_ARCH", |
| 110 | "HOST_BUILD_TYPE", |
| 111 | "BUILD_ID", |
| 112 | "OUT_DIR", |
| 113 | "AUX_OS_VARIANT_LIST", |
| 114 | "TARGET_BUILD_PDK", |
| 115 | "PDK_FUSION_PLATFORM_ZIP", |
Jeff Gaston | 088e29e | 2017-11-29 16:47:17 -0800 | [diff] [blame] | 116 | "PRODUCT_SOONG_NAMESPACES", |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func Banner(make_vars map[string]string) string { |
| 120 | b := &bytes.Buffer{} |
| 121 | |
| 122 | fmt.Fprintln(b, "============================================") |
| 123 | for _, name := range BannerVars { |
| 124 | if make_vars[name] != "" { |
| 125 | fmt.Fprintf(b, "%s=%s\n", name, make_vars[name]) |
| 126 | } |
| 127 | } |
| 128 | fmt.Fprint(b, "============================================") |
| 129 | |
| 130 | return b.String() |
| 131 | } |
| 132 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 133 | func runMakeProductConfig(ctx Context, config Config) { |
| 134 | // Variables to export into the environment of Kati/Ninja |
| 135 | exportEnvVars := []string{ |
| 136 | // So that we can use the correct TARGET_PRODUCT if it's been |
Dan Willemsen | 04a16c7 | 2017-05-25 22:18:57 -0700 | [diff] [blame] | 137 | // modified by PRODUCT-*/APP-* arguments |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 138 | "TARGET_PRODUCT", |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 139 | "TARGET_BUILD_VARIANT", |
Dan Willemsen | 04a16c7 | 2017-05-25 22:18:57 -0700 | [diff] [blame] | 140 | "TARGET_BUILD_APPS", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 141 | |
| 142 | // compiler wrappers set up by make |
| 143 | "CC_WRAPPER", |
| 144 | "CXX_WRAPPER", |
Yoshisato Yanagisawa | 13fd3ff | 2017-04-05 11:05:31 +0900 | [diff] [blame] | 145 | "JAVAC_WRAPPER", |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 146 | |
| 147 | // ccache settings |
| 148 | "CCACHE_COMPILERCHECK", |
| 149 | "CCACHE_SLOPPINESS", |
| 150 | "CCACHE_BASEDIR", |
| 151 | "CCACHE_CPP2", |
| 152 | } |
| 153 | |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 154 | allVars := append(append([]string{ |
| 155 | // Used to execute Kati and Ninja |
| 156 | "NINJA_GOALS", |
| 157 | "KATI_GOALS", |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 158 | |
| 159 | // To find target/product/<DEVICE> |
| 160 | "TARGET_DEVICE", |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 161 | |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 162 | // So that later Kati runs can find BoardConfig.mk faster |
| 163 | "TARGET_DEVICE_DIR", |
| 164 | |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 165 | // Whether --werror_overriding_commands will work |
| 166 | "BUILD_BROKEN_DUP_RULES", |
Dan Willemsen | b58f120 | 2018-06-21 10:12:53 -0700 | [diff] [blame] | 167 | |
| 168 | // Not used, but useful to be in the soong.log |
| 169 | "BUILD_BROKEN_ANDROIDMK_EXPORTS", |
| 170 | "BUILD_BROKEN_DUP_COPY_HEADERS", |
| 171 | "BUILD_BROKEN_PHONY_TARGETS", |
Dan Willemsen | 051133b | 2017-07-14 11:29:29 -0700 | [diff] [blame] | 172 | }, exportEnvVars...), BannerVars...) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 173 | |
Dan Willemsen | b2e6c2e | 2017-07-13 17:24:44 -0700 | [diff] [blame] | 174 | make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 175 | if err != nil { |
| 176 | ctx.Fatalln("Error dumping make vars:", err) |
| 177 | } |
| 178 | |
| 179 | // Print the banner like make does |
Dan Willemsen | b82471a | 2018-05-17 16:37:09 -0700 | [diff] [blame] | 180 | ctx.Writer.Print(Banner(make_vars)) |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 181 | |
| 182 | // Populate the environment |
| 183 | env := config.Environment() |
| 184 | for _, name := range exportEnvVars { |
| 185 | if make_vars[name] == "" { |
| 186 | env.Unset(name) |
| 187 | } else { |
| 188 | env.Set(name, make_vars[name]) |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"])) |
| 193 | config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"])) |
Dan Willemsen | 02781d5 | 2017-05-12 19:28:13 -0700 | [diff] [blame] | 194 | config.SetTargetDevice(make_vars["TARGET_DEVICE"]) |
Dan Willemsen | 6ab79db | 2018-05-02 00:06:28 -0700 | [diff] [blame] | 195 | config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"]) |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 196 | |
Dan Willemsen | fa42f3c | 2018-06-15 21:54:47 -0700 | [diff] [blame] | 197 | config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true") |
Dan Willemsen | 3d60b11 | 2018-04-04 22:25:56 -0700 | [diff] [blame] | 198 | config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] != "false") |
Dan Willemsen | 1e70446 | 2016-08-21 15:17:17 -0700 | [diff] [blame] | 199 | } |