blob: eb43ed655fa2388b1e62ade22933283f016e9592 [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"
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 Willemsen1e704462016-08-21 15:17:17 -070031// vars is the list of variables to read. The values will be put in the
32// returned map.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070033func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) {
34 return dumpMakeVars(ctx, config, goals, vars, false)
35}
36
37func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070038 ctx.BeginTrace("dumpvars")
39 defer ctx.EndTrace()
40
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070041 cmd := Command(ctx, config, "dumpvars",
42 config.PrebuiltBuildTool("ckati"),
43 "-f", "build/make/core/config.mk",
44 "--color_warnings",
Dan Willemsen0c518512018-01-09 02:09:52 -080045 "--kati_stats",
Dan Willemsen1e704462016-08-21 15:17:17 -070046 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070047 "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 Willemsen0c518512018-01-09 02:09:52 -080055 output := bytes.Buffer{}
56 cmd.Stdout = &output
57 pipe, err := cmd.StderrPipe()
Dan Willemsen1e704462016-08-21 15:17:17 -070058 if err != nil {
Dan Willemsen0c518512018-01-09 02:09:52 -080059 ctx.Fatalln("Error getting output pipe for ckati:", err)
Dan Willemsen1e704462016-08-21 15:17:17 -070060 }
Dan Willemsen0c518512018-01-09 02:09:52 -080061 cmd.StartOrFatal()
62 // TODO: error out when Stderr contains any content
63 katiRewriteOutput(ctx, pipe)
64 cmd.WaitOrFatal()
Dan Willemsen1e704462016-08-21 15:17:17 -070065
66 ret := make(map[string]string, len(vars))
Dan Willemsen0c518512018-01-09 02:09:52 -080067 for _, line := range strings.Split(output.String(), "\n") {
Dan Willemsen1e704462016-08-21 15:17:17 -070068 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 Willemsen051133b2017-07-14 11:29:29 -070087// Variables to print out in the top banner
88var 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 Gaston088e29e2017-11-29 16:47:17 -0800114 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700115}
116
117func 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 Willemsen1e704462016-08-21 15:17:17 -0700131func 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 Willemsen04a16c72017-05-25 22:18:57 -0700135 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -0700136 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700137 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700138 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700139
140 // compiler wrappers set up by make
141 "CC_WRAPPER",
142 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900143 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700144
145 // ccache settings
146 "CCACHE_COMPILERCHECK",
147 "CCACHE_SLOPPINESS",
148 "CCACHE_BASEDIR",
149 "CCACHE_CPP2",
150 }
151
Dan Willemsen1e704462016-08-21 15:17:17 -0700152 allVars := append(append([]string{
153 // Used to execute Kati and Ninja
154 "NINJA_GOALS",
155 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700156
157 // To find target/product/<DEVICE>
158 "TARGET_DEVICE",
Dan Willemsen3d60b112018-04-04 22:25:56 -0700159
160 // Whether --werror_overriding_commands will work
161 "BUILD_BROKEN_DUP_RULES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700162 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700163
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700164 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700165 if err != nil {
166 ctx.Fatalln("Error dumping make vars:", err)
167 }
168
169 // Print the banner like make does
Dan Willemsen051133b2017-07-14 11:29:29 -0700170 fmt.Fprintln(ctx.Stdout(), Banner(make_vars))
Dan Willemsen1e704462016-08-21 15:17:17 -0700171
172 // Populate the environment
173 env := config.Environment()
174 for _, name := range exportEnvVars {
175 if make_vars[name] == "" {
176 env.Unset(name)
177 } else {
178 env.Set(name, make_vars[name])
179 }
180 }
181
182 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
183 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
Dan Willemsen02781d52017-05-12 19:28:13 -0700184 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen3d60b112018-04-04 22:25:56 -0700185
186 config.SetBuildBrokenDupRules(make_vars["BUILD_BROKEN_DUP_RULES"] != "false")
Dan Willemsen1e704462016-08-21 15:17:17 -0700187}