blob: 96f2274f956b0398b9dd411fd50171b3b799c65b [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 Willemsen1e704462016-08-21 15:17:17 -070045 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070046 "MAKECMDGOALS="+strings.Join(goals, " "))
47 cmd.Environment.Set("CALLED_FROM_SETUP", "true")
48 cmd.Environment.Set("BUILD_SYSTEM", "build/make/core")
49 if write_soong_vars {
50 cmd.Environment.Set("WRITE_SOONG_VARIABLES", "true")
51 }
52 cmd.Environment.Set("DUMP_MANY_VARS", strings.Join(vars, " "))
53 cmd.Sandbox = dumpvarsSandbox
Dan Willemsen1e704462016-08-21 15:17:17 -070054 // TODO: error out when Stderr contains any content
55 cmd.Stderr = ctx.Stderr()
Dan Willemsen1e704462016-08-21 15:17:17 -070056 output, err := cmd.Output()
57 if err != nil {
58 return nil, err
59 }
60
61 ret := make(map[string]string, len(vars))
62 for _, line := range strings.Split(string(output), "\n") {
63 if len(line) == 0 {
64 continue
65 }
66
67 if key, value, ok := decodeKeyValue(line); ok {
68 if value, ok = singleUnquote(value); ok {
69 ret[key] = value
70 ctx.Verboseln(key, value)
71 } else {
72 return nil, fmt.Errorf("Failed to parse make line: %q", line)
73 }
74 } else {
75 return nil, fmt.Errorf("Failed to parse make line: %q", line)
76 }
77 }
78
79 return ret, nil
80}
81
Dan Willemsen051133b2017-07-14 11:29:29 -070082// Variables to print out in the top banner
83var BannerVars = []string{
84 "PLATFORM_VERSION_CODENAME",
85 "PLATFORM_VERSION",
86 "TARGET_PRODUCT",
87 "TARGET_BUILD_VARIANT",
88 "TARGET_BUILD_TYPE",
89 "TARGET_BUILD_APPS",
90 "TARGET_ARCH",
91 "TARGET_ARCH_VARIANT",
92 "TARGET_CPU_VARIANT",
93 "TARGET_2ND_ARCH",
94 "TARGET_2ND_ARCH_VARIANT",
95 "TARGET_2ND_CPU_VARIANT",
96 "HOST_ARCH",
97 "HOST_2ND_ARCH",
98 "HOST_OS",
99 "HOST_OS_EXTRA",
100 "HOST_CROSS_OS",
101 "HOST_CROSS_ARCH",
102 "HOST_CROSS_2ND_ARCH",
103 "HOST_BUILD_TYPE",
104 "BUILD_ID",
105 "OUT_DIR",
106 "AUX_OS_VARIANT_LIST",
107 "TARGET_BUILD_PDK",
108 "PDK_FUSION_PLATFORM_ZIP",
Jeff Gaston088e29e2017-11-29 16:47:17 -0800109 "PRODUCT_SOONG_NAMESPACES",
Dan Willemsen051133b2017-07-14 11:29:29 -0700110}
111
112func Banner(make_vars map[string]string) string {
113 b := &bytes.Buffer{}
114
115 fmt.Fprintln(b, "============================================")
116 for _, name := range BannerVars {
117 if make_vars[name] != "" {
118 fmt.Fprintf(b, "%s=%s\n", name, make_vars[name])
119 }
120 }
121 fmt.Fprint(b, "============================================")
122
123 return b.String()
124}
125
Dan Willemsen1e704462016-08-21 15:17:17 -0700126func runMakeProductConfig(ctx Context, config Config) {
127 // Variables to export into the environment of Kati/Ninja
128 exportEnvVars := []string{
129 // So that we can use the correct TARGET_PRODUCT if it's been
Dan Willemsen04a16c72017-05-25 22:18:57 -0700130 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -0700131 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -0700132 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -0700133 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -0700134
135 // compiler wrappers set up by make
136 "CC_WRAPPER",
137 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +0900138 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -0700139
140 // ccache settings
141 "CCACHE_COMPILERCHECK",
142 "CCACHE_SLOPPINESS",
143 "CCACHE_BASEDIR",
144 "CCACHE_CPP2",
145 }
146
Dan Willemsen1e704462016-08-21 15:17:17 -0700147 allVars := append(append([]string{
148 // Used to execute Kati and Ninja
149 "NINJA_GOALS",
150 "KATI_GOALS",
Dan Willemsen02781d52017-05-12 19:28:13 -0700151
152 // To find target/product/<DEVICE>
153 "TARGET_DEVICE",
Dan Willemsen051133b2017-07-14 11:29:29 -0700154 }, exportEnvVars...), BannerVars...)
Dan Willemsen1e704462016-08-21 15:17:17 -0700155
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700156 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700157 if err != nil {
158 ctx.Fatalln("Error dumping make vars:", err)
159 }
160
161 // Print the banner like make does
Dan Willemsen051133b2017-07-14 11:29:29 -0700162 fmt.Fprintln(ctx.Stdout(), Banner(make_vars))
Dan Willemsen1e704462016-08-21 15:17:17 -0700163
164 // Populate the environment
165 env := config.Environment()
166 for _, name := range exportEnvVars {
167 if make_vars[name] == "" {
168 env.Unset(name)
169 } else {
170 env.Set(name, make_vars[name])
171 }
172 }
173
174 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
175 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
Dan Willemsen02781d52017-05-12 19:28:13 -0700176 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen1e704462016-08-21 15:17:17 -0700177}