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