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::CharReaderBuilder builder;
93     if (!Json::parseFromStream(builder, stream, &json, /* errorMessage = */ nullptr)) {
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::StreamWriterBuilder factory;
136     factory["indentation"] = "  ";
137     std::unique_ptr<Json::StreamWriter> const writer(factory.newStreamWriter());
138     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);
145     writer->write(json, &stream);
146 }
147 
148 string
GetBuildVar(const string & name,bool quiet)149 BuildVars::GetBuildVar(const string& name, bool quiet)
150 {
151     int err;
152 
153     map<string,string>::iterator it = m_cache.find(name);
154     if (it == m_cache.end()) {
155         Command cmd("build/soong/soong_ui.bash");
156         cmd.AddArg("--dumpvar-mode");
157         cmd.AddArg(name);
158 
159         string output = trim(get_command_output(cmd, &err, quiet));
160         if (err == 0) {
161             m_cache[name] = output;
162             save();
163             return output;
164         } else {
165             return string();
166         }
167     } else {
168         return it->second;
169     }
170 }
171 
172 void
json_error(const string & filename,const char * error,bool quiet)173 json_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 
182 static void
get_values(const Json::Value & json,const string & name,vector<string> * result)183 get_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 
201 void
read_modules(const string & buildOut,const string & device,map<string,Module> * result,bool quiet)202 read_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;
216     Json::CharReaderBuilder builder;
217     if (!Json::parseFromStream(builder, stream, &json, /* errorMessage = */ nullptr)) {
218         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"
248                     || cl == "APPS" || cl == "NATIVE_TESTS")) {
249                 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 
271 int
build_goals(const vector<string> & goals)272 build_goals(const vector<string>& goals)
273 {
274     Command cmd("build/soong/soong_ui.bash");
275     cmd.AddArg("--make-mode");
276     for (size_t i=0; i<goals.size(); i++) {
277         cmd.AddArg(goals[i]);
278     }
279 
280     return run_command(cmd);
281 }
282 
283