blob: 627091321b2e3627b615b3d359a36dad602ad2a9 [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
54BuildVars::BuildVars(const string& outDir, const string& buildProduct,
55 const string& buildVariant, const string& buildType)
56 :m_filename(),
57 m_cache()
58{
59 m_cache["TARGET_PRODUCT"] = buildProduct;
60 m_cache["TARGET_BUILD_VARIANT"] = buildVariant;
61 m_cache["TARGET_BUILD_TYPE"] = buildType;
62
63 // If we have any problems reading the file, that's ok, just do
64 // uncached calls to make / soong.
65
66 if (outDir == "") {
67 return;
68 }
69
70
71 m_filename = make_cache_filename(outDir);
72
73 std::ifstream stream(m_filename, std::ifstream::binary);
74
75 if (stream.fail()) {
76 return;
77 }
78
79 Json::Value json;
80 Json::Reader reader;
81 if (!reader.parse(stream, json)) {
82 return;
83 }
84
85 if (!json.isObject()) {
86 return;
87 }
88
89 map<string,string> cache;
90
91 vector<string> names = json.getMemberNames();
92 const int N = names.size();
93 for (int i=0; i<N; i++) {
94 const string& name = names[i];
95 const Json::Value& value = json[name];
96 if (!value.isString()) {
97 continue;
98 }
99 cache[name] = value.asString();
100 }
101
102 // If all of the base variables match, then we can use this cache. Otherwise, use our
103 // base one. The next time someone reads a value, the new one, with our base varaibles
104 // will be saved.
105 if (map_contains(cache, "TARGET_PRODUCT", buildProduct)
106 && map_contains(cache, "TARGET_BUILD_VARIANT", buildVariant)
107 && map_contains(cache, "TARGET_BUILD_TYPE", buildType)) {
108 m_cache = cache;
109 }
110}
111
112BuildVars::~BuildVars()
113{
114}
115
116void
117BuildVars::save()
118{
119 if (m_filename == "") {
120 return;
121 }
122
123 Json::StyledStreamWriter writer(" ");
124
125 Json::Value json(Json::objectValue);
126
127 for (map<string,string>::const_iterator it = m_cache.begin(); it != m_cache.end(); it++) {
128 json[it->first] = it->second;
129 }
130
131 std::ofstream stream(m_filename, std::ofstream::binary);
132 writer.write(stream, json);
133}
Joe Onorato0578cbc2016-10-19 17:03:06 -0700134
135string
Joe Onoratoce0bd062019-01-14 15:30:05 -0800136BuildVars::GetBuildVar(const string& name, bool quiet)
Joe Onorato0578cbc2016-10-19 17:03:06 -0700137{
138 int err;
139
Joe Onoratoce0bd062019-01-14 15:30:05 -0800140 map<string,string>::iterator it = m_cache.find(name);
141 if (it == m_cache.end()) {
Dan Willemsena40118d2017-10-17 17:46:41 -0700142 Command cmd("build/soong/soong_ui.bash");
143 cmd.AddArg("--dumpvar-mode");
144 cmd.AddArg(name);
Joe Onorato0578cbc2016-10-19 17:03:06 -0700145
146 string output = trim(get_command_output(cmd, &err, quiet));
147 if (err == 0) {
Joe Onoratoce0bd062019-01-14 15:30:05 -0800148 m_cache[name] = output;
149 save();
Joe Onorato0578cbc2016-10-19 17:03:06 -0700150 return output;
151 } else {
152 return string();
153 }
154 } else {
155 return it->second;
156 }
157}
158
Joe Onorato0578cbc2016-10-19 17:03:06 -0700159void
160json_error(const string& filename, const char* error, bool quiet)
161{
162 if (!quiet) {
163 print_error("Unable to parse module info file (%s): %s", error, filename.c_str());
164 print_error("Have you done a full build?");
165 }
166 exit(1);
167}
168
169static void
170get_values(const Json::Value& json, const string& name, vector<string>* result)
171{
172 Json::Value nullValue;
173
174 const Json::Value& value = json.get(name, nullValue);
175 if (!value.isArray()) {
176 return;
177 }
178
179 const int N = value.size();
180 for (int i=0; i<N; i++) {
181 const Json::Value& child = value[i];
182 if (child.isString()) {
183 result->push_back(child.asString());
184 }
185 }
186}
187
188void
189read_modules(const string& buildOut, const string& device, map<string,Module>* result, bool quiet)
190{
191 string filename(string(buildOut + "/target/product/") + device + "/module-info.json");
192 std::ifstream stream(filename, std::ifstream::binary);
193
194 if (stream.fail()) {
195 if (!quiet) {
196 print_error("Unable to open module info file: %s", filename.c_str());
197 print_error("Have you done a full build?");
198 }
199 exit(1);
200 }
201
202 Json::Value json;
203 Json::Reader reader;
204 if (!reader.parse(stream, json)) {
205 json_error(filename, "can't parse json format", quiet);
206 return;
207 }
208
209 if (!json.isObject()) {
210 json_error(filename, "root element not an object", quiet);
211 return;
212 }
213
214 vector<string> names = json.getMemberNames();
215 const int N = names.size();
216 for (int i=0; i<N; i++) {
217 const string& name = names[i];
218
219 const Json::Value& value = json[name];
220 if (!value.isObject()) {
221 continue;
222 }
223
224 Module module;
225
226 module.name = name;
227 get_values(value, "class", &module.classes);
228 get_values(value, "path", &module.paths);
229 get_values(value, "installed", &module.installed);
230
231 // Only keep classes we can handle
232 for (ssize_t i = module.classes.size() - 1; i >= 0; i--) {
233 string cl = module.classes[i];
234 if (!(cl == "JAVA_LIBRARIES" || cl == "EXECUTABLES" || cl == "SHARED_LIBRARIES"
Joe Onorato0520afd2017-10-12 00:16:10 -0700235 || cl == "APPS" || cl == "NATIVE_TESTS")) {
Joe Onorato0578cbc2016-10-19 17:03:06 -0700236 module.classes.erase(module.classes.begin() + i);
237 }
238 }
239 if (module.classes.size() == 0) {
240 continue;
241 }
242
243 // Only target modules (not host)
244 for (ssize_t i = module.installed.size() - 1; i >= 0; i--) {
245 string fn = module.installed[i];
246 if (!starts_with(fn, buildOut + "/target/")) {
247 module.installed.erase(module.installed.begin() + i);
248 }
249 }
250 if (module.installed.size() == 0) {
251 continue;
252 }
253
254 (*result)[name] = module;
255 }
256}
257
258int
259build_goals(const vector<string>& goals)
260{
Dan Willemsena40118d2017-10-17 17:46:41 -0700261 Command cmd("build/soong/soong_ui.bash");
262 cmd.AddArg("--make-mode");
Joe Onorato0578cbc2016-10-19 17:03:06 -0700263 for (size_t i=0; i<goals.size(); i++) {
264 cmd.AddArg(goals[i]);
265 }
266
267 return run_command(cmd);
268}
269