1 /*
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>
24 #include <json/writer.h>
25 #include <json/value.h>
26
27 #include <fstream>
28 #include <string>
29 #include <map>
30 #include <thread>
31
32 #include <sys/types.h>
33 #include <dirent.h>
34 #include <string.h>
35
36 using namespace std;
37
38 static bool
map_contains(const map<string,string> & m,const string & k,const string & v)39 map_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
47 static string
make_cache_filename(const string & outDir)48 make_cache_filename(const string& outDir)
49 {
50 string filename(outDir);
51 return filename + "/.bit_cache";
52 }
53
54 bool
HasClass(const string & cl)55 Module::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
BuildVars(const string & outDir,const string & buildProduct,const string & buildVariant,const string & buildType)66 BuildVars::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
~BuildVars()124 BuildVars::~BuildVars()
125 {
126 }
127
128 void
save()129 BuildVars::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 }
146
147 string
GetBuildVar(const string & name,bool quiet)148 BuildVars::GetBuildVar(const string& name, bool quiet)
149 {
150 int err;
151
152 map<string,string>::iterator it = m_cache.find(name);
153 if (it == m_cache.end()) {
154 Command cmd("build/soong/soong_ui.bash");
155 cmd.AddArg("--dumpvar-mode");
156 cmd.AddArg(name);
157
158 string output = trim(get_command_output(cmd, &err, quiet));
159 if (err == 0) {
160 m_cache[name] = output;
161 save();
162 return output;
163 } else {
164 return string();
165 }
166 } else {
167 return it->second;
168 }
169 }
170
171 void
json_error(const string & filename,const char * error,bool quiet)172 json_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
181 static void
get_values(const Json::Value & json,const string & name,vector<string> * result)182 get_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
200 void
read_modules(const string & buildOut,const string & device,map<string,Module> * result,bool quiet)201 read_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"
247 || cl == "APPS" || cl == "NATIVE_TESTS")) {
248 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
270 int
build_goals(const vector<string> & goals)271 build_goals(const vector<string>& goals)
272 {
273 Command cmd("build/soong/soong_ui.bash");
274 cmd.AddArg("--make-mode");
275 for (size_t i=0; i<goals.size(); i++) {
276 cmd.AddArg(goals[i]);
277 }
278
279 return run_command(cmd);
280 }
281
282