blob: 32dc17b5b2c2bb7787a93eb7fefad6b3700af26a [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 "path/filepath"
20 "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//
31// extra_targets adds real arguments to the make command, in case other targets
32// actually need to be run (like the Soong config generator).
33//
34// vars is the list of variables to read. The values will be put in the
35// returned map.
36func DumpMakeVars(ctx Context, config Config, goals, extra_targets, vars []string) (map[string]string, error) {
Dan Willemsend9f6fa22016-08-21 15:17:17 -070037 ctx.BeginTrace("dumpvars")
38 defer ctx.EndTrace()
39
Dan Willemsen269a8c72017-05-03 17:15:47 -070040 cmd := Command(ctx, config, "make",
Dan Willemsen1e704462016-08-21 15:17:17 -070041 "make",
42 "--no-print-directory",
43 "-f", "build/core/config.mk",
44 "dump-many-vars",
45 "CALLED_FROM_SETUP=true",
46 "BUILD_SYSTEM=build/core",
47 "MAKECMDGOALS="+strings.Join(goals, " "),
48 "DUMP_MANY_VARS="+strings.Join(vars, " "),
49 "OUT_DIR="+config.OutDir())
Dan Willemsen1e704462016-08-21 15:17:17 -070050 cmd.Args = append(cmd.Args, extra_targets...)
Dan Willemsen269a8c72017-05-03 17:15:47 -070051 cmd.Sandbox = makeSandbox
Dan Willemsen1e704462016-08-21 15:17:17 -070052 // TODO: error out when Stderr contains any content
53 cmd.Stderr = ctx.Stderr()
Dan Willemsen1e704462016-08-21 15:17:17 -070054 output, err := cmd.Output()
55 if err != nil {
56 return nil, err
57 }
58
59 ret := make(map[string]string, len(vars))
60 for _, line := range strings.Split(string(output), "\n") {
61 if len(line) == 0 {
62 continue
63 }
64
65 if key, value, ok := decodeKeyValue(line); ok {
66 if value, ok = singleUnquote(value); ok {
67 ret[key] = value
68 ctx.Verboseln(key, value)
69 } else {
70 return nil, fmt.Errorf("Failed to parse make line: %q", line)
71 }
72 } else {
73 return nil, fmt.Errorf("Failed to parse make line: %q", line)
74 }
75 }
76
77 return ret, nil
78}
79
80func runMakeProductConfig(ctx Context, config Config) {
81 // Variables to export into the environment of Kati/Ninja
82 exportEnvVars := []string{
83 // So that we can use the correct TARGET_PRODUCT if it's been
84 // modified by PRODUCT-* arguments
85 "TARGET_PRODUCT",
86
87 // compiler wrappers set up by make
88 "CC_WRAPPER",
89 "CXX_WRAPPER",
Yoshisato Yanagisawa13fd3ff2017-04-05 11:05:31 +090090 "JAVAC_WRAPPER",
Dan Willemsen1e704462016-08-21 15:17:17 -070091
92 // ccache settings
93 "CCACHE_COMPILERCHECK",
94 "CCACHE_SLOPPINESS",
95 "CCACHE_BASEDIR",
96 "CCACHE_CPP2",
97 }
98
99 // Variables to print out in the top banner
100 bannerVars := []string{
101 "PLATFORM_VERSION_CODENAME",
102 "PLATFORM_VERSION",
103 "TARGET_PRODUCT",
104 "TARGET_BUILD_VARIANT",
105 "TARGET_BUILD_TYPE",
106 "TARGET_BUILD_APPS",
107 "TARGET_ARCH",
108 "TARGET_ARCH_VARIANT",
109 "TARGET_CPU_VARIANT",
110 "TARGET_2ND_ARCH",
111 "TARGET_2ND_ARCH_VARIANT",
112 "TARGET_2ND_CPU_VARIANT",
113 "HOST_ARCH",
114 "HOST_2ND_ARCH",
115 "HOST_OS",
116 "HOST_OS_EXTRA",
117 "HOST_CROSS_OS",
118 "HOST_CROSS_ARCH",
119 "HOST_CROSS_2ND_ARCH",
120 "HOST_BUILD_TYPE",
121 "BUILD_ID",
122 "OUT_DIR",
123 "AUX_OS_VARIANT_LIST",
124 "TARGET_BUILD_PDK",
125 "PDK_FUSION_PLATFORM_ZIP",
126 }
127
128 allVars := append(append([]string{
129 // Used to execute Kati and Ninja
130 "NINJA_GOALS",
131 "KATI_GOALS",
132 }, exportEnvVars...), bannerVars...)
133
134 make_vars, err := DumpMakeVars(ctx, config, config.Arguments(), []string{
135 filepath.Join(config.SoongOutDir(), "soong.variables"),
136 }, allVars)
137 if err != nil {
138 ctx.Fatalln("Error dumping make vars:", err)
139 }
140
141 // Print the banner like make does
142 fmt.Fprintln(ctx.Stdout(), "============================================")
143 for _, name := range bannerVars {
144 if make_vars[name] != "" {
145 fmt.Fprintf(ctx.Stdout(), "%s=%s\n", name, make_vars[name])
146 }
147 }
148 fmt.Fprintln(ctx.Stdout(), "============================================")
149
150 // Populate the environment
151 env := config.Environment()
152 for _, name := range exportEnvVars {
153 if make_vars[name] == "" {
154 env.Unset(name)
155 } else {
156 env.Set(name, make_vars[name])
157 }
158 }
159
160 config.SetKatiArgs(strings.Fields(make_vars["KATI_GOALS"]))
161 config.SetNinjaArgs(strings.Fields(make_vars["NINJA_GOALS"]))
162}