blob: 06bd74f2bfde6dd82a6ab7c1184ed5c7b960a8ad [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 Willemsen1e704462016-08-21 15:17:17 -070020 "strings"
Dan Willemsenb82471a2018-05-17 16:37:09 -070021
22 "android/soong/ui/status"
Dan Willemsen1e704462016-08-21 15:17:17 -070023)
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 Willemsen1e704462016-08-21 15:17:17 -070033// vars is the list of variables to read. The values will be put in the
34// returned map.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070035func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) {
36 return dumpMakeVars(ctx, config, goals, vars, false)
37}
38
39func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070040 ctx.BeginTrace("dumpvars")
41 defer ctx.EndTrace()
42
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070043 cmd := Command(ctx, config, "dumpvars",
44 config.PrebuiltBuildTool("ckati"),
45 "-f", "build/make/core/config.mk",
46 "--color_warnings",
Dan Willemsen0c518512018-01-09 02:09:52 -080047 "--kati_stats",
Dan Willemsen1e704462016-08-21 15:17:17 -070048 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070049 "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 Willemsen0c518512018-01-09 02:09:52 -080057 output := bytes.Buffer{}
58 cmd.Stdout = &output
59 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -070060 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -080061 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -070062 }
Dan Willemsen0c518512018-01-09 02:09:52 -080063 cmd.StartOrFatal()
64 // TODO: error out when Stderr contains any content
Dan Willemsenb82471a2018-05-17 16:37:09 -070065 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen0c518512018-01-09 02:09:52 -080066 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -070067
68 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -080069 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -070070 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 Willemsen051133b2017-07-14 11:29:29 -070089// Variables to print out in the top banner
90var 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 Gaston088e29e2017-11-29 16:47:17 -0800116 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700117}
118
119func 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 Willemsen1e704462016-08-21 15:17:17 -0700133func 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 Willemsen04a16c72017-05-25 22:18:57 -0700137 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -0700138 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700139 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700140 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700141
142 // compiler wrappers set up by make
143 "CC_WRAPPER",
144 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900145 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700146
147 // ccache settings
148 "CCACHE_COMPILERCHECK",
149 "CCACHE_SLOPPINESS",
150 "CCACHE_BASEDIR",
151 "CCACHE_CPP2",
152 }
153
Dan Willemsen1e704462016-08-21 15:17:17 -0700154 allVars := append(append([]string{
155 // Used to execute Kati and Ninja
156 "NINJA_GOALS",
157 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700158
159 // To find target/product/<DEVICE>
160 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700161
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700162 // So that later Kati runs can find BoardConfig.mk faster
163 "TARGET_DEVICE_DIR",
164
Dan Willemsen3d60b112018-04-04 22:25:56 -0700165 // Whether --werror_overriding_commands will work
166 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700167
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 Willemsen051133b2017-07-14 11:29:29 -0700172 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700173
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700174 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700175 if err != nil {
176 ctx.Fatalln("Error dumping make vars:", err)
177 }
178
179 // Print the banner like make does
Dan Willemsenb82471a2018-05-17 16:37:09 -0700180 ctx.Writer.Print(Banner(make_vars))
Dan Willemsen1e704462016-08-21 15:17:17 -0700181
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 Willemsen02781d52017-05-12 19:28:13 -0700194 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700195 config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"])
Dan Willemsen3d60b112018-04-04 22:25:56 -0700196
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700197 config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true")
Dan Willemsen3d60b112018-04-04 22:25:56 -0700198 config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] != "false")
Dan Willemsen1e704462016-08-21 15:17:17 -0700199}