blob: df64a801e2133954a5c83a4e7b204789e69adc56 [file] [log] [blame]
Joe Onorato0578cbc2016-10-19 17:03:06 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "make.h"
18
19#include "command.h"
20#include "print.h"
21#include "util.h"
22
23#include <json/reader.h>
Joe Onoratoce0bd062019-01-14 15:30:05 -080024#include <json/writer.h>
Joe Onorato0578cbc2016-10-19 17:03:06 -070025#include <json/value.h>
26
27#include <fstream>
28#include <string>
29#include <map>
Romain Guyd973aff2017-01-11 18:34:57 -080030#include <thread>
Joe Onorato0578cbc2016-10-19 17:03:06 -070031
32#include <sys/types.h>
33#include <dirent.h>
34#include <string.h>
35
36using namespace std;
37
Joe Onoratoce0bd062019-01-14 15:30:05 -080038static bool
39map_contains(const map<string,string>& m, const string& k, const string& v) {
40 map<string,string>::const_iterator it = m.find(k);
41 if (it == m.end()) {
42 return false;
43 }
44 return it->second == v;
45}
46
47static string
48make_cache_filename(const string& outDir)
49{
50 string filename(outDir);
51 return filename + "/.bit_cache";
52}
53
Joe Onorato6c97f492019-02-27 20:42:37 -050054bool
55Module::HasClass(const string& cl)
56{
57 for (vector<string>::const_iterator c = classes.begin(); c != classes.end(); c++) {
58 if (*c == cl) {
59 return true;
60 }
61 }
62 return false;
63}
64
65
Joe Onoratoce0bd062019-01-14 15:30:05 -080066BuildVars::BuildVars(const string& outDir, const string& buildProduct,
67 const string& buildVariant, const string& buildType)
68 :m_filename(),
69 m_cache()
70{
71 m_cache["TARGET_PRODUCT"] = buildProduct;
72 m_cache["TARGET_BUILD_VARIANT"] = buildVariant;
73 m_cache["TARGET_BUILD_TYPE"] = buildType;
74
75 // If we have any problems reading the file, that's ok, just do
76 // uncached calls to make / soong.
77
78 if (outDir == "") {
79 return;
80 }
81
82
83 m_filename = make_cache_filename(outDir);
84
85 std::ifstream stream(m_filename, std::ifstream::binary);
86
87 if (stream.fail()) {
88 return;
89 }
90
91 Json::Value json;
92 Json::Reader reader;
93 if (!reader.parse(stream, json)) {
94 return;
95 }
96
97 if (!json.isObject()) {
98 return;
99 }
100
101 map<string,string> cache;
102
103 vector<string> names = json.getMemberNames();
104 const int N = names.size();
105 for (int i=0; i<N; i++) {
106 const string& name = names[i];
107 const Json::Value& value = json[name];
108 if (!value.isString()) {
109 continue;
110 }
111 cache[name] = value.asString();
112 }
113
114 // If all of the base variables match, then we can use this cache. Otherwise, use our
115 // base one. The next time someone reads a value, the new one, with our base varaibles
116 // will be saved.
117 if (map_contains(cache, "TARGET_PRODUCT", buildProduct)
118 && map_contains(cache, "TARGET_BUILD_VARIANT", buildVariant)
119 && map_contains(cache, "TARGET_BUILD_TYPE", buildType)) {
120 m_cache = cache;
121 }
122}
123
124BuildVars::~BuildVars()
125{
126}
127
128void
129BuildVars::save()
130{
131 if (m_filename == "") {
132 return;
133 }
134
135 Json::StyledStreamWriter writer(" ");
136
137 Json::Value json(Json::objectValue);
138
139 for (map<string,string>::const_iterator it = m_cache.begin(); it != m_cache.end(); it++) {
140 json[it->first] = it->second;
141 }
142
143 std::ofstream stream(m_filename, std::ofstream::binary);
144 writer.write(stream, json);
145}
Joe Onorato0578cbc2016-10-19 17:03:06 -0700146
147string
Joe Onoratoce0bd062019-01-14 15:30:05 -0800148BuildVars::GetBuildVar(const string& name, bool quiet)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700149{
150 int err;
151
Joe Onoratoce0bd062019-01-14 15:30:05 -0800152 map<string,string>::iterator it = m_cache.find(name);
153 if (it == m_cache.end()) {
Dan Willemsena40118d2017-10-17 17:46:41 -0700154 Command cmd("build/soong/soong_ui.bash");
155 cmd.AddArg("--dumpvar-mode");
156 cmd.AddArg(name);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700157
158 string output = trim(get_command_output(cmd, &err, quiet));
159 if (err == 0) {
Joe Onoratoce0bd062019-01-14 15:30:05 -0800160 m_cache[name] = output;
161 save();
Joe Onorato0578cbc2016-10-19 17:03:06 -0700162 return output;
163 } else {
164 return string();
165 }
166 } else {
167 return it->second;
168 }
169}
170
Joe Onorato0578cbc2016-10-19 17:03:06 -0700171void
172json_error(const string& filename, const char* error, bool quiet)
173{
174 if (!quiet) {
175 print_error("Unable to parse module info file (%s): %s", error, filename.c_str());
176 print_error("Have you done a full build?");
177 }
178 exit(1);
179}
180
181static void
182get_values(const Json::Value& json, const string& name, vector<string>* result)
183{
184 Json::Value nullValue;
185
186 const Json::Value& value = json.get(name, nullValue);
187 if (!value.isArray()) {
188 return;
189 }
190
191 const int N = value.size();
192 for (int i=0; i<N; i++) {
193 const Json::Value& child = value[i];
194 if (child.isString()) {
195 result->push_back(child.asString());
196 }
197 }
198}
199
200void
201read_modules(const string& buildOut, const string& device, map<string,Module>* result, bool quiet)
202{
203 string filename(string(buildOut + "/target/product/") + device + "/module-info.json");
204 std::ifstream stream(filename, std::ifstream::binary);
205
206 if (stream.fail()) {
207 if (!quiet) {
208 print_error("Unable to open module info file: %s", filename.c_str());
209 print_error("Have you done a full build?");
210 }
211 exit(1);
212 }
213
214 Json::Value json;
215 Json::Reader reader;
216 if (!reader.parse(stream, json)) {
217 json_error(filename, "can't parse json format", quiet);
218 return;
219 }
220
221 if (!json.isObject()) {
222 json_error(filename, "root element not an object", quiet);
223 return;
224 }
225
226 vector<string> names = json.getMemberNames();
227 const int N = names.size();
228 for (int i=0; i<N; i++) {
229 const string& name = names[i];
230
231 const Json::Value& value = json[name];
232 if (!value.isObject()) {
233 continue;
234 }
235
236 Module module;
237
238 module.name = name;
239 get_values(value, "class", &module.classes);
240 get_values(value, "path", &module.paths);
241 get_values(value, "installed", &module.installed);
242
243 // Only keep classes we can handle
244 for (ssize_t i = module.classes.size() - 1; i >= 0; i--) {
245 string cl = module.classes[i];
246 if (!(cl == "JAVA_LIBRARIES" || cl == "EXECUTABLES" || cl == "SHARED_LIBRARIES"
Joe Onorato0520afd2017-10-12 00:16:10 -0700247 || cl == "APPS" || cl == "NATIVE_TESTS")) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700248 module.classes.erase(module.classes.begin() + i);
249 }
250 }
251 if (module.classes.size() == 0) {
252 continue;
253 }
254
255 // Only target modules (not host)
256 for (ssize_t i = module.installed.size() - 1; i >= 0; i--) {
257 string fn = module.installed[i];
258 if (!starts_with(fn, buildOut + "/target/")) {
259 module.installed.erase(module.installed.begin() + i);
260 }
261 }
262 if (module.installed.size() == 0) {
263 continue;
264 }
265
266 (*result)[name] = module;
267 }
268}
269
270int
271build_goals(const vector<string>& goals)
272{
Dan Willemsena40118d2017-10-17 17:46:41 -0700273 Command cmd("build/soong/soong_ui.bash");
274 cmd.AddArg("--make-mode");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700275 for (size_t i=0; i<goals.size(); i++) {
276 cmd.AddArg(goals[i]);
277 }
278
279 return run_command(cmd);
280}
281