blob: e6c3f56babd93fe9fd7e02537f8b5db88966adee [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 (
18 "fmt"
Dan Willemsen1e704462016-08-21 15:17:17 -070019 "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 Willemsen1e704462016-08-21 15:17:17 -070030// vars is the list of variables to read. The values will be put in the
31// returned map.
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070032func DumpMakeVars(ctx Context, config Config, goals, vars []string) (map[string]string, error) {
33 return dumpMakeVars(ctx, config, goals, vars, false)
34}
35
36func dumpMakeVars(ctx Context, config Config, goals, vars []string, write_soong_vars bool) (map[string]string, error) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070037 ctx.BeginTrace("dumpvars")
38 defer ctx.EndTrace()
39
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070040 cmd := Command(ctx, config, "dumpvars",
41 config.PrebuiltBuildTool("ckati"),
42 "-f", "build/make/core/config.mk",
43 "--color_warnings",
Dan Willemsen1e704462016-08-21 15:17:17 -070044 "dump-many-vars",
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -070045 "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 Willemsen1e704462016-08-21 15:17:17 -070053 // TODO: error out when Stderr contains any content
54 cmd.Stderr = ctx.Stderr()
Dan Willemsen1e704462016-08-21 15:17:17 -070055 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
81func 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 Willemsen04a16c72017-05-25 22:18:57 -070085 // modified by PRODUCT-*/APP-* arguments
Dan Willemsen1e704462016-08-21 15:17:17 -070086 "TARGET_PRODUCT",
Dan Willemsen02781d52017-05-12 19:28:13 -070087 "TARGET_BUILD_VARIANT",
Dan Willemsen04a16c72017-05-25 22:18:57 -070088 "TARGET_BUILD_APPS",
Dan Willemsen1e704462016-08-21 15:17:17 -070089
90 // compiler wrappers set up by make
91 "CC_WRAPPER",
92 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +090093 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -070094
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 Willemsen02781d52017-05-12 19:28:13 -0700135
136 // To find target/product/<DEVICE>
137 "TARGET_DEVICE",
Dan Willemsen1e704462016-08-21 15:17:17 -0700138 }, exportEnvVars...), bannerVars...)
139
Dan Willemsenb2e6c2e2017-07-13 17:24:44 -0700140 make_vars, err := dumpMakeVars(ctx, config, config.Arguments(), allVars, true)
Dan Willemsen1e704462016-08-21 15:17:17 -0700141 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 Willemsen02781d52017-05-12 19:28:13 -0700166 config.SetTargetDevice(make_vars["TARGET_DEVICE"])
Dan Willemsen1e704462016-08-21 15:17:17 -0700167}