blob: c39f49465054f759bba587e92a21dda8247bb42b [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;
Haibo Huang39f4db52021-02-25 10:44:18 -080092 Json::CharReaderBuilder builder;
93 std::string errorMessage;
94 if (!Json::parseFromStream(builder, stream, &json, &errorMessage)) {
Joe Onoratoce0bd062019-01-14 15:30:05 -080095 return;
96 }
97
98 if (!json.isObject()) {
99 return;
100 }
101
102 map<string,string> cache;
103
104 vector<string> names = json.getMemberNames();
105 const int N = names.size();
106 for (int i=0; i<N; i++) {
107 const string& name = names[i];
108 const Json::Value& value = json[name];
109 if (!value.isString()) {
110 continue;
111 }
112 cache[name] = value.asString();
113 }
114
115 // If all of the base variables match, then we can use this cache. Otherwise, use our
116 // base one. The next time someone reads a value, the new one, with our base varaibles
117 // will be saved.
118 if (map_contains(cache, "TARGET_PRODUCT", buildProduct)
119 && map_contains(cache, "TARGET_BUILD_VARIANT", buildVariant)
120 && map_contains(cache, "TARGET_BUILD_TYPE", buildType)) {
121 m_cache = cache;
122 }
123}
124
125BuildVars::~BuildVars()
126{
127}
128
129void
130BuildVars::save()
131{
132 if (m_filename == "") {
133 return;
134 }
135
Haibo Huang39f4db52021-02-25 10:44:18 -0800136 Json::StreamWriterBuilder factory;
137 factory["indentation"] = " ";
138 std::unique_ptr<Json::StreamWriter> const writer(factory.newStreamWriter());
Joe Onoratoce0bd062019-01-14 15:30:05 -0800139 Json::Value json(Json::objectValue);
140
141 for (map<string,string>::const_iterator it = m_cache.begin(); it != m_cache.end(); it++) {
142 json[it->first] = it->second;
143 }
144
145 std::ofstream stream(m_filename, std::ofstream::binary);
Haibo Huang39f4db52021-02-25 10:44:18 -0800146 writer->write(json, &stream);
Joe Onoratoce0bd062019-01-14 15:30:05 -0800147}
Joe Onorato0578cbc2016-10-19 17:03:06 -0700148
149string
Joe Onoratoce0bd062019-01-14 15:30:05 -0800150BuildVars::GetBuildVar(const string& name, bool quiet)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700151{
152 int err;
153
Joe Onoratoce0bd062019-01-14 15:30:05 -0800154 map<string,string>::iterator it = m_cache.find(name);
155 if (it == m_cache.end()) {
Dan Willemsena40118d2017-10-17 17:46:41 -0700156 Command cmd("build/soong/soong_ui.bash");
157 cmd.AddArg("--dumpvar-mode");
158 cmd.AddArg(name);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700159
160 string output = trim(get_command_output(cmd, &err, quiet));
161 if (err == 0) {
Joe Onoratoce0bd062019-01-14 15:30:05 -0800162 m_cache[name] = output;
163 save();
Joe Onorato0578cbc2016-10-19 17:03:06 -0700164 return output;
165 } else {
166 return string();
167 }
168 } else {
169 return it->second;
170 }
171}
172
Joe Onorato0578cbc2016-10-19 17:03:06 -0700173void
174json_error(const string& filename, const char* error, bool quiet)
175{
176 if (!quiet) {
177 print_error("Unable to parse module info file (%s): %s", error, filename.c_str());
178 print_error("Have you done a full build?");
179 }
180 exit(1);
181}
182
183static void
184get_values(const Json::Value& json, const string& name, vector<string>* result)
185{
186 Json::Value nullValue;
187
188 const Json::Value& value = json.get(name, nullValue);
189 if (!value.isArray()) {
190 return;
191 }
192
193 const int N = value.size();
194 for (int i=0; i<N; i++) {
195 const Json::Value& child = value[i];
196 if (child.isString()) {
197 result->push_back(child.asString());
198 }
199 }
200}
201
202void
203read_modules(const string& buildOut, const string& device, map<string,Module>* result, bool quiet)
204{
205 string filename(string(buildOut + "/target/product/") + device + "/module-info.json");
206 std::ifstream stream(filename, std::ifstream::binary);
207
208 if (stream.fail()) {
209 if (!quiet) {
210 print_error("Unable to open module info file: %s", filename.c_str());
211 print_error("Have you done a full build?");
212 }
213 exit(1);
214 }
215
216 Json::Value json;
Haibo Huang39f4db52021-02-25 10:44:18 -0800217 Json::CharReaderBuilder builder;
218 std::string errorMessage;
219 if (!Json::parseFromStream(builder, stream, &json, &errorMessage)) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700220 json_error(filename, "can't parse json format", quiet);
221 return;
222 }
223
224 if (!json.isObject()) {
225 json_error(filename, "root element not an object", quiet);
226 return;
227 }
228
229 vector<string> names = json.getMemberNames();
230 const int N = names.size();
231 for (int i=0; i<N; i++) {
232 const string& name = names[i];
233
234 const Json::Value& value = json[name];
235 if (!value.isObject()) {
236 continue;
237 }
238
239 Module module;
240
241 module.name = name;
242 get_values(value, "class", &module.classes);
243 get_values(value, "path", &module.paths);
244 get_values(value, "installed", &module.installed);
245
246 // Only keep classes we can handle
247 for (ssize_t i = module.classes.size() - 1; i >= 0; i--) {
248 string cl = module.classes[i];
249 if (!(cl == "JAVA_LIBRARIES" || cl == "EXECUTABLES" || cl == "SHARED_LIBRARIES"
Joe Onorato0520afd2017-10-12 00:16:10 -0700250 || cl == "APPS" || cl == "NATIVE_TESTS")) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700251 module.classes.erase(module.classes.begin() + i);
252 }
253 }
254 if (module.classes.size() == 0) {
255 continue;
256 }
257
258 // Only target modules (not host)
259 for (ssize_t i = module.installed.size() - 1; i >= 0; i--) {
260 string fn = module.installed[i];
261 if (!starts_with(fn, buildOut + "/target/")) {
262 module.installed.erase(module.installed.begin() + i);
263 }
264 }
265 if (module.installed.size() == 0) {
266 continue;
267 }
268
269 (*result)[name] = module;
270 }
271}
272
273int
274build_goals(const vector<string>& goals)
275{
Dan Willemsena40118d2017-10-17 17:46:41 -0700276 Command cmd("build/soong/soong_ui.bash");
277 cmd.AddArg("--make-mode");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700278 for (size_t i=0; i<goals.size(); i++) {
279 cmd.AddArg(goals[i]);
280 }
281
282 return run_command(cmd);
283}
284