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