blob: bea08b67c2c1b8f7aa5339d4c78fba206605e259 [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")
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070051 if write_soong_vars {
52 cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true")
53 }
54 cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
55 cmd.Sandbox = dumpvarsSandbox
Dan Willemsen0c518512018-01-09 02:09:52 -080056 output := bytes.Buffer{}
57 cmd.Stdout = &output
58 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -070059 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -080060 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -070061 }
Dan Willemsen0c518512018-01-09 02:09:52 -080062 cmd.StartOrFatal()
63 // TODO: error out when Stderr contains any content
Dan Willemsenb82471a2018-05-17 16:37:09 -070064 status.KatiReader(ctx.Status.StartTool(), pipe)
Dan Willemsen0c518512018-01-09 02:09:52 -080065 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -070066
67 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -080068 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -070069 if len(line) == 0 {
70 continue
71 }
72
73 if key, value, ok := decodeKeyValue(line); ok {
74 if value, ok = singleUnquote(value); ok {
75 ret[key] = value
76 ctx.Verboseln(key, value)
77 } else {
78 return nil, fmt.Errorf("Failed to parse make line: %q", line)
79 }
80 } else {
81 return nil, fmt.Errorf("Failed to parse make line: %q", line)
82 }
83 }
84
85 return ret, nil
86}
87
Dan Willemsen051133b2017-07-14 11:29:29 -070088// Variables to print out in the top banner
89var BannerVars = []string{
90 "PLATFORM_VERSION_CODENAME",
91 "PLATFORM_VERSION",
92 "TARGET_PRODUCT",
93 "TARGET_BUILD_VARIANT",
94 "TARGET_BUILD_TYPE",
95 "TARGET_BUILD_APPS",
96 "TARGET_ARCH",
97 "TARGET_ARCH_VARIANT",
98 "TARGET_CPU_VARIANT",
99 "TARGET_2ND_ARCH",
100 "TARGET_2ND_ARCH_VARIANT",
101 "TARGET_2ND_CPU_VARIANT",
102 "HOST_ARCH",
103 "HOST_2ND_ARCH",
104 "HOST_OS",
105 "HOST_OS_EXTRA",
106 "HOST_CROSS_OS",
107 "HOST_CROSS_ARCH",
108 "HOST_CROSS_2ND_ARCH",
109 "HOST_BUILD_TYPE",
110 "BUILD_ID",
111 "OUT_DIR",
112 "AUX_OS_VARIANT_LIST",
113 "TARGET_BUILD_PDK",
114 "PDK_FUSION_PLATFORM_ZIP",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800115 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700116}
117
118func Banner(make_vars map[string]string) string {
119 b := &bytes.Buffer{}
120
121 fmt.Fprintln(b, "============================================")
122 for _, name := range BannerVars {
123 if make_vars[name] != "" {
124 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
125 }
126 }
127 fmt.Fprint(b, "============================================")
128
129 return b.String()
130}
131
Dan Willemsen1e704462016-08-21 15:17:17 -0700132func runMakeProductConfig(ctx Context, config Config) {
133 // Variables to export into the environment of Kati/Ninja
134 exportEnvVars := []string{
135 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsen04a16c72017-05-25 22:18:57 -0700136 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -0700137 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700138 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700139 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700140
141 // compiler wrappers set up by make
142 "CC_WRAPPER",
143 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900144 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700145
146 // ccache settings
147 "CCACHE_COMPILERCHECK",
148 "CCACHE_SLOPPINESS",
149 "CCACHE_BASEDIR",
150 "CCACHE_CPP2",
151 }
152
Dan Willemsen1e704462016-08-21 15:17:17 -0700153 allVars := append(append([]string{
154 // Used to execute Kati and Ninja
155 "NINJA_GOALS",
156 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700157
158 // To find target/product/<DEVICE>
159 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700160
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700161 // So that later Kati runs can find BoardConfig.mk faster
162 "TARGET_DEVICE_DIR",
163
Dan Willemsen3d60b112018-04-04 22:25:56 -0700164 // Whether --werror_overriding_commands will work
165 "BUILD_BROKEN_DUP_RULES",
Dan Willemsenb58f1202018-06-21 10:12:53 -0700166
Dan Willemsend8aa39d2018-08-27 15:01:03 -0700167 // Used to turn on --werror_ options in Kati
168 "BUILD_BROKEN_PHONY_TARGETS",
169
Dan Willemsenb58f1202018-06-21 10:12:53 -0700170 // Not used, but useful to be in the soong.log
171 "BUILD_BROKEN_ANDROIDMK_EXPORTS",
172 "BUILD_BROKEN_DUP_COPY_HEADERS",
Dan Willemsen051133b2017-07-14 11:29:29 -0700173 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700174
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700175 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700176 if err != nil {
177 ctx.Fatalln("Error dumping make vars:", err)
178 }
179
180 // Print the banner like make does
Dan Willemsenb82471a2018-05-17 16:37:09 -0700181 ctx.Writer.Print(Banner(make_vars))
Dan Willemsen1e704462016-08-21 15:17:17 -0700182
183 // Populate the environment
184 env := config.Environment()
185 for _, name := range exportEnvVars {
186 if make_vars[name] == "" {
187 env.Unset(name)
188 } else {
189 env.Set(name, make_vars[name])
190 }
191 }
192
193 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
194 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
Dan Willemsen02781d52017-05-12 19:28:13 -0700195 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen6ab79db2018-05-02 00:06:28 -0700196 config.SetTargetDeviceDir(make_vars["TARGET_DEVICE_DIR"])
Dan Willemsen3d60b112018-04-04 22:25:56 -0700197
Dan Willemsenfa42f3c2018-06-15 21:54:47 -0700198 config.SetPdkBuild(make_vars["TARGET_BUILD_PDK"] == "true")
Dan Willemsen9dc69a42018-06-22 13:18:06 -0700199 config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] == "true")
Dan Willemsen14569582018-09-10 13:06:43 -0700200 config.SetBuildBrokenPhonyTargets(make_vars["BUILD_BROKEN_PHONY_TARGETS"] == "true")
Dan Willemsen1e704462016-08-21 15:17:17 -0700201}