blob: 2a88732b50b17f70594de5f399caf15d2812a900 [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;
Haibo Huang1c9baa32021-03-03 16:41:39 -080093 if (!Json::parseFromStream(builder, stream, &json, /* errorMessage = */ nullptr)) {
Joe Onoratoce0bd062019-01-14 15:30:05 -080094 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
Haibo Huang39f4db52021-02-25 10:44:18 -0800135 Json::StreamWriterBuilder factory;
136 factory["indentation"] = " ";
137 std::unique_ptr<Json::StreamWriter> const writer(factory.newStreamWriter());
Joe Onoratoce0bd062019-01-14 15:30:05 -0800138 Json::Value json(Json::objectValue);
139
140 for (map<string,string>::const_iterator it = m_cache.begin(); it != m_cache.end(); it++) {
141 json[it->first] = it->second;
142 }
143
144 std::ofstream stream(m_filename, std::ofstream::binary);
Haibo Huang39f4db52021-02-25 10:44:18 -0800145 writer->write(json, &stream);
Joe Onoratoce0bd062019-01-14 15:30:05 -0800146}
Joe Onorato0578cbc2016-10-19 17:03:06 -0700147
148string
Joe Onoratoce0bd062019-01-14 15:30:05 -0800149BuildVars::GetBuildVar(const string& name, bool quiet)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700150{
151 int err;
152
Joe Onoratoce0bd062019-01-14 15:30:05 -0800153 map<string,string>::iterator it = m_cache.find(name);
154 if (it == m_cache.end()) {
Dan Willemsena40118d2017-10-17 17:46:41 -0700155 Command cmd("build/soong/soong_ui.bash");
156 cmd.AddArg("--dumpvar-mode");
157 cmd.AddArg(name);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700158
159 string output = trim(get_command_output(cmd, &err, quiet));
160 if (err == 0) {
Joe Onoratoce0bd062019-01-14 15:30:05 -0800161 m_cache[name] = output;
162 save();
Joe Onorato0578cbc2016-10-19 17:03:06 -0700163 return output;
164 } else {
165 return string();
166 }
167 } else {
168 return it->second;
169 }
170}
171
Joe Onorato0578cbc2016-10-19 17:03:06 -0700172void
173json_error(const string& filename, const char* error, bool quiet)
174{
175 if (!quiet) {
176 print_error("Unable to parse module info file (%s): %s", error, filename.c_str());
177 print_error("Have you done a full build?");
178 }
179 exit(1);
180}
181
182static void
183get_values(const Json::Value& json, const string& name, vector<string>* result)
184{
185 Json::Value nullValue;
186
187 const Json::Value& value = json.get(name, nullValue);
188 if (!value.isArray()) {
189 return;
190 }
191
192 const int N = value.size();
193 for (int i=0; i<N; i++) {
194 const Json::Value& child = value[i];
195 if (child.isString()) {
196 result->push_back(child.asString());
197 }
198 }
199}
200
201void
202read_modules(const string& buildOut, const string& device, map<string,Module>* result, bool quiet)
203{
204 string filename(string(buildOut + "/target/product/") + device + "/module-info.json");
205 std::ifstream stream(filename, std::ifstream::binary);
206
207 if (stream.fail()) {
208 if (!quiet) {
209 print_error("Unable to open module info file: %s", filename.c_str());
210 print_error("Have you done a full build?");
211 }
212 exit(1);
213 }
214
215 Json::Value json;
Haibo Huang39f4db52021-02-25 10:44:18 -0800216 Json::CharReaderBuilder builder;
Haibo Huang1c9baa32021-03-03 16:41:39 -0800217 if (!Json::parseFromStream(builder, stream, &json, /* errorMessage = */ nullptr)) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700218 json_error(filename, "can't parse json format", quiet);
219 return;
220 }
221
222 if (!json.isObject()) {
223 json_error(filename, "root element not an object", quiet);
224 return;
225 }
226
227 vector<string> names = json.getMemberNames();
228 const int N = names.size();
229 for (int i=0; i<N; i++) {
230 const string& name = names[i];
231
232 const Json::Value& value = json[name];
233 if (!value.isObject()) {
234 continue;
235 }
236
237 Module module;
238
239 module.name = name;
240 get_values(value, "class", &module.classes);
241 get_values(value, "path", &module.paths);
242 get_values(value, "installed", &module.installed);
243
244 // Only keep classes we can handle
245 for (ssize_t i = module.classes.size() - 1; i >= 0; i--) {
246 string cl = module.classes[i];
247 if (!(cl == "JAVA_LIBRARIES" || cl == "EXECUTABLES" || cl == "SHARED_LIBRARIES"
Joe Onorato0520afd2017-10-12 00:16:10 -0700248 || cl == "APPS" || cl == "NATIVE_TESTS")) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700249 module.classes.erase(module.classes.begin() + i);
250 }
251 }
252 if (module.classes.size() == 0) {
253 continue;
254 }
255
256 // Only target modules (not host)
257 for (ssize_t i = module.installed.size() - 1; i >= 0; i--) {
258 string fn = module.installed[i];
259 if (!starts_with(fn, buildOut + "/target/")) {
260 module.installed.erase(module.installed.begin() + i);
261 }
262 }
263 if (module.installed.size() == 0) {
264 continue;
265 }
266
267 (*result)[name] = module;
268 }
269}
270
271int
272build_goals(const vector<string>& goals)
273{
Dan Willemsena40118d2017-10-17 17:46:41 -0700274 Command cmd("build/soong/soong_ui.bash");
275 cmd.AddArg("--make-mode");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700276 for (size_t i=0; i<goals.size(); i++) {
277 cmd.AddArg(goals[i]);
278 }
279
280 return run_command(cmd);
281}
282