Debian Codegraph @

tools/resgen/src/main.cpp

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /*
2 * Copyright (C) 2018 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 <utils/Path.h>
18
19 #include <getopt/getopt.h>
20
21 #include <algorithm>
22 #include <fstream>
23 #include <iomanip>
24 #include <iostream>
25 #include <sstream>
26 #include <string>
27 #include <vector>
28
29 #include <string.h>
30
31 using namespace std;
32 using namespace utils;
33
34 static const char* g_jsonMagicString = "__RESGEN__";
35 static const char* g_packageName = "resources";
36 static const char* g_deployDir = ".";
37 static bool g_keepExtension = false;
38 static bool g_appendNull = false;
39 static bool g_generateC = false;
40 static bool g_quietMode = false;
41 static bool g_embedJson = false;
42
43 static const char* USAGE = R"TXT(
44 filament-resgen aggregates a sequence of binary blobs, each of which becomes a "resource" whose id
45 is the basename of the input file. It produces the following set of files:
46
47 resources.h ......... declares sizes and offsets for each resource
48 resources.S ......... small assembly file with incbin directive and rodata section
49 resources.apple.S ... ditto but with different rodata name and underscore prefixes
50 resources.bin ....... the aggregated binary blob that the incbin refers to
51
52 Usage:
53 filament-resgen [options] <input_file_0> <input_file_1> ...
54
55 Options:
56 --help, -h
57 Print this message
58 --license, -L
59 Print copyright and license information
60 --package=string, -p string
61 Name of the resource package (defaults to "resources")
62 This is used to generate filenames and symbol prefixes
63 --deploy=dir, -x dir (defaults to ".")
64 Generate everything needed for deployment into <dir>
65 --keep, -k
66 Keep file extensions when generating symbols
67 --text, -t
68 Append a null terminator to each data blob
69 --cfile, -c
70 Generate xxd-style C file (useful for WebAssembly)
71 --json, -j
72 Embed a JSON string in the output that provides a summary
73 of all resource sizes and names. Useful for size analysis.
74 --quiet, -q
75 Suppress console output
76
77 Examples:
78 RESGEN -cp textures jungle.png beach.png
79 > Generated files: textures.h, textures.S, textures.apple.S, textures.bin, textures.c
80 > Generated symbols: TEXTURES_JUNGLE_DATA, TEXTURES_JUNGLE_SIZE,
81 TEXTURES_BEACH_DATA, TEXTURES_BEACH_SIZE
82 )TXT";
83
84 static const char* APPLE_ASM_TEMPLATE = R"ASM(
85 .global _{RESOURCES}PACKAGE
86 .section __TEXT,__const
87 _{RESOURCES}PACKAGE:
88 .incbin "{resources}.bin"
89 )ASM";
90
91 static const char* ASM_TEMPLATE = R"ASM(
92 .global {RESOURCES}PACKAGE
93 .section .rodata
94 {RESOURCES}PACKAGE:
95 .incbin "{resources}.bin"
96 )ASM";
97
98 static void printUsage(const char* name) {
99 std::string execName(Path(name).getName());
100 const std::string from("RESGEN");
101 std::string usage(USAGE);
102 for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) {
103 usage.replace(pos, from.length(), execName);
104 }
105 puts(usage.c_str());
106 }
107
108 static void license() {
109 static const char *license[] = {
110 #include "licenses/licenses.inc"
111 nullptr
112 };
113
114 const char **p = &license[0];
115 while (*p)
116 std::cout << *p++ << std::endl;
117 }
118
119 static int handleArguments(int argc, char* argv[]) {
120 static constexpr const char* OPTSTR = "hLp:x:ktcqj";
121 static const struct option OPTIONS[] = {
122 { "help", no_argument, 0, 'h' },
123 { "license", no_argument, 0, 'L' },
124 { "package", required_argument, 0, 'p' },
125 { "deploy", required_argument, 0, 'x' },
126 { "keep", no_argument, 0, 'k' },
127 { "text", no_argument, 0, 't' },
128 { "cfile", no_argument, 0, 'c' },
129 { "quiet", no_argument, 0, 'q' },
130 { "json", no_argument, 0, 'j' },
131 { 0, 0, 0, 0 } // termination of the option list
132 };
133
134 int opt;
135 int optionIndex = 0;
136
137 while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &optionIndex)) >= 0) {
138 std::string arg(optarg ? optarg : "");
139 switch (opt) {
140 default:
141 case 'h':
142 printUsage(argv[0]);
143 exit(0);
144 case 'L':
145 license();
146 exit(0);
147 case 'p':
148 g_packageName = optarg;
149 break;
150 case 'x':
151 g_deployDir = optarg;
152 break;
153 case 'k':
154 g_keepExtension = true;
155 break;
156 case 't':
157 g_appendNull = true;
158 break;
159 case 'c':
160 g_generateC = true;
161 break;
162 case 'q':
163 g_quietMode = true;
164 break;
165 case 'j':
166 g_embedJson = true;
167 break;
168 }
169 }
170
171 return optind;
172 }
173
174 int main(int argc, char* argv[]) {
175 const int optionIndex = handleArguments(argc, argv);
176 const int numArgs = argc - optionIndex;
177 if (numArgs < 1) {
178 printUsage(argv[0]);
179 return 1;
180 }
181
182 vector<Path> inputPaths;
183 for (int argIndex = optionIndex; argIndex < argc; ++argIndex) {
184 inputPaths.emplace_back(argv[argIndex]);
185 }
186 if (g_embedJson) {
187 inputPaths.push_back(g_jsonMagicString);
188 }
189
190 std::string packageFile = g_packageName;
191 std::string packagePrefix = std::string(g_packageName) + "_";
192 transform(packageFile.begin(), packageFile.end(), packageFile.begin(), ::tolower);
193 transform(packagePrefix.begin(), packagePrefix.end(), packagePrefix.begin(), ::toupper);
194 std::string package = packagePrefix + "PACKAGE";
195
196 const Path deployDir(g_deployDir);
197 if (!deployDir.exists()) {
198 deployDir.mkdirRecursive();
199 }
200
201 const Path appleAsmPath(deployDir + (packageFile + ".apple.S"));
202 const Path asmPath(deployDir + (packageFile + ".S"));
203 const Path binPath(deployDir + (packageFile + ".bin"));
204 const Path headerPath(deployDir + (packageFile + ".h"));
205 const Path xxdPath(deployDir + (packageFile + ".c"));
206
207 // In the assembly language templates, replace {RESOURCES} with packagePrefix and replace
208 // {resources} with packageFile.
209 const std::string k1("{RESOURCES}");
210 const std::string k2("{resources}");
211 std::string aasmstr(APPLE_ASM_TEMPLATE);
212 std::string asmstr(ASM_TEMPLATE);
213 for (size_t pos = aasmstr.find(k1); pos != std::string::npos; pos = aasmstr.find(k1, pos))
214 aasmstr.replace(pos, k1.length(), packagePrefix);
215 for (size_t pos = aasmstr.find(k2); pos != std::string::npos; pos = aasmstr.find(k2, pos))
216 aasmstr.replace(pos, k2.length(), packageFile);
217 for (size_t pos = asmstr.find(k1); pos != std::string::npos; pos = asmstr.find(k1, pos))
218 asmstr.replace(pos, k1.length(), packagePrefix);
219 for (size_t pos = asmstr.find(k2); pos != std::string::npos; pos = asmstr.find(k2, pos))
220 asmstr.replace(pos, k2.length(), packageFile);
221
222 // Open the Apple-friendly assembly language file.
223 ofstream appleAsmStream(appleAsmPath.getPath());
224 if (!appleAsmStream) {
225 cerr << "Unable to open " << appleAsmPath << endl;
226 exit(1);
227 }
228
229 // Open the non-Apple assembly language file.
230 ofstream asmStream(asmPath.getPath());
231 if (!asmStream) {
232 cerr << "Unable to open " << asmPath << endl;
233 exit(1);
234 }
235
236 // Open the bin file for writing.
237 ofstream binStream(binPath.getPath(), ios::binary);
238 if (!binStream) {
239 cerr << "Unable to open " << binPath << endl;
240 exit(1);
241 }
242
243 // Open the header file stream for writing.
244 ostringstream headerStream;
245 headerStream << "#ifndef " << packagePrefix << "H_" << endl
246 << "#define " << packagePrefix << "H_" << endl << endl
247 << "#include <stdint.h>" << endl << endl
248 << "extern \"C\" {" << endl
249 << " extern const uint8_t " << package << "[];" << endl;
250
251 ostringstream headerMacros;
252 ostringstream xxdDefinitions;
253 ostringstream appleDataAsmStream;
254 ostringstream dataAsmStream;
255 ostringstream jsonStream;
256
257 // Open the generated C file for writing.
258 ofstream xxdStream;
259 if (g_generateC) {
260 xxdStream = ofstream(xxdPath.getPath());
261 if (!xxdStream) {
262 cerr << "Unable to open " << xxdPath << endl;
263 exit(1);
264 }
265 xxdStream << "#include <stdint.h>\n\nconst uint8_t " << package << "[] = {\n";
266 }
267
268 // Consume each input file and write it back out into the various output streams.
269 jsonStream << "{";
270 size_t offset = 0;
271 for (const auto& inPath : inputPaths) {
272 vector<uint8_t> content;
273 if (inPath != g_jsonMagicString) {
274 ifstream inStream(inPath.getPath(), ios::binary);
275 if (!inStream) {
276 cerr << "Unable to open " << inPath << endl;
277 exit(1);
278 }
279 content = vector<uint8_t>((istreambuf_iterator<char>(inStream)), {});
280 } else {
281 // To finalize the JSON string, we replace the trailing comma with an end bracket and
282 // prefix it with the magic identifier and string size.
283 std::string jsonString = jsonStream.str();
284 jsonString[jsonString.size()-1] = '}';
285 ostringstream jsonBlob;
286 jsonBlob << g_jsonMagicString << "\0";
287 jsonBlob << jsonString.size() << "\0";
288 jsonBlob << jsonString;
289 jsonString = jsonBlob.str();
290 uint8_t const* jsonPtr = (uint8_t const*) jsonString.c_str();
291 content = vector<uint8_t>(jsonPtr, jsonPtr + jsonBlob.str().size());
292 }
293 if (g_appendNull) {
294 content.push_back(0);
295 }
296
297 // Formulate the resource name and the prefixed resource name.
298 std::string rname = g_keepExtension ? inPath.getName() : inPath.getNameWithoutExtension();
299 replace(rname.begin(), rname.end(), '.', '_');
300 transform(rname.begin(), rname.end(), rname.begin(), ::toupper);
301 const std::string prname = packagePrefix + rname;
302
303 // Write the binary blob into the bin file.
304 binStream.write((const char*) content.data(), content.size());
305
306 // Write the offsets and sizes.
307 headerMacros
308 << "#define " << prname << "_DATA (" << package << " + " << prname << "_OFFSET)\n";
309
310 headerStream
311 << " extern int " << prname << "_OFFSET;\n"
312 << " extern int " << prname << "_SIZE;\n";
313
314 dataAsmStream
315 << prname << "_OFFSET:\n"
316 << " .int " << offset << "\n"
317 << prname << "_SIZE:\n"
318 << " .int " << content.size() << "\n";
319
320 asmStream
321 << " .global " << prname << "_OFFSET;\n"
322 << " .global " << prname << "_SIZE;\n";
323
324 appleDataAsmStream
325 << "_" << prname << "_OFFSET:\n"
326 << " .int " << offset << "\n"
327 << "_" << prname << "_SIZE:\n"
328 << " .int " << content.size() << "\n";
329
330 appleAsmStream
331 << " .global _" << prname << "_OFFSET;\n"
332 << " .global _" << prname << "_SIZE;\n";
333
334 // Write the xxd-style ASCII array, followed by a blank line.
335 if (g_generateC) {
336 xxdDefinitions
337 << "int " << prname << "_OFFSET = " << offset << ";\n"
338 << "int " << prname << "_SIZE = " << content.size() << ";\n";
339
340 xxdStream << "// " << rname << "\n";
341 xxdStream << setfill('0') << hex;
342 size_t i = 0;
343 for (; i < content.size(); i++) {
344 if (i > 0 && i % 20 == 0) {
345 xxdStream << "\n";
346 }
347 xxdStream << "0x" << setw(2) << (int) content[i] << ", ";
348 }
349 if (i % 20 != 0) xxdStream << "\n";
350 xxdStream << "\n";
351 }
352
353 jsonStream << "\"" << rname << "\":" << content.size() << ",";
354 offset += content.size();
355 }
356
357 headerStream << "}\n" << headerMacros.str();
358 headerStream << "\n#endif\n";
359
360 // To optimize builds, avoid overwriting the header file if nothing has changed.
361 bool headerIsDirty = true;
362 ifstream headerInStream(headerPath.getPath(), std::ifstream::ate);
363 string headerContents = headerStream.str();
364 if (headerInStream) {
365 long fileSize = static_cast<long>(headerInStream.tellg());
366 if (fileSize == headerContents.size()) {
367 vector<char> previous(fileSize);
368 headerInStream.seekg(0);
369 headerInStream.read(previous.data(), fileSize);
370 headerIsDirty = 0 != memcmp(previous.data(), headerContents.c_str(), fileSize);
371 }
372 }
373
374 if (headerIsDirty) {
375 ofstream headerOutStream(headerPath.getPath());
376 if (!headerOutStream) {
377 cerr << "Unable to open " << headerPath << endl;
378 exit(1);
379 }
380 headerOutStream << headerContents;
381 }
382
383 asmStream << asmstr << dataAsmStream.str() << endl;
384 asmStream.close();
385
386 appleAsmStream << aasmstr << appleDataAsmStream.str() << endl;
387 appleAsmStream.close();
388
389 if (!g_quietMode) {
390 cout << "Generated files: "
391 << headerPath << " "
392 << asmPath << " "
393 << appleAsmPath << " "
394 << binPath;
395 }
396
397 if (g_generateC) {
398 xxdStream << "};\n\n" << xxdDefinitions.str();
399 if (!g_quietMode) {
400 cout << " " << xxdPath;
401 }
402 }
403
404 if (!g_quietMode) {
405 cout << endl;
406 }
407 }