Debian Codegraph @

tools/resgen/src/main.cpp

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