1 //
2 // Copyright (C) 2015 LunarG, Inc.
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions
8 // are met:
9 //
10 // Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
19 // contributors may be used to endorse or promote products derived
20 // from this software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32 // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 // POSSIBILITY OF SUCH DAMAGE.
34 //
35
36 #include <iostream>
37 #include <fstream>
38 #include <cstring>
39 #include <stdexcept>
40 #ifndef OH_SDK
41 #include <filesystem>
42 #else
43 #include <sys/stat.h>
44 #endif
45
46 //
47 // Include remapper
48 //
49 #include "../SPIRV/SPVRemapper.h"
50
51 namespace {
52
53 typedef unsigned int SpvWord;
54
55 // Poor man's basename: given a complete path, return file portion.
56 // E.g:
57 // Linux: /foo/bar/test -> test
58 // Win: c:\foo\bar\test -> test
59 // It's not very efficient, but that doesn't matter for our minimal-duty use.
60 // Using boost::filesystem would be better in many ways, but want to avoid that dependency.
61
62 // OS dependent path separator (avoiding boost::filesystem dependency)
63 #if defined(_WIN32)
path_sep_char()64 char path_sep_char() { return '\\'; }
65 #else
path_sep_char()66 char path_sep_char() { return '/'; }
67 #endif
68
basename(const std::string filename)69 std::string basename(const std::string filename)
70 {
71 const size_t sepLoc = filename.find_last_of(path_sep_char());
72
73 return (sepLoc == filename.npos) ? filename : filename.substr(sepLoc+1);
74 }
75
errHandler(const std::string & str)76 void errHandler(const std::string& str) {
77 std::cout << str << std::endl;
78 exit(5);
79 }
80
logHandler(const std::string & str)81 void logHandler(const std::string& str) {
82 std::cout << str << std::endl;
83 }
84
85 // Read word stream from disk
read(std::vector<SpvWord> & spv,const std::string & inFilename,int verbosity)86 void read(std::vector<SpvWord>& spv, const std::string& inFilename, int verbosity)
87 {
88 std::ifstream fp;
89
90 if (verbosity > 0)
91 logHandler(std::string(" reading: ") + inFilename);
92
93 spv.clear();
94 fp.open(inFilename, std::fstream::in | std::fstream::binary);
95
96 if (fp.fail())
97 #ifndef OH_SDK
98 errHandler("error opening file for read: ");
99 #else
100 errHandler("error opening file for read, errno : " + std::to_string(errno));
101 #endif // OH_SDK
102 // Reserve space (for efficiency, not for correctness)
103 fp.seekg(0, fp.end);
104 spv.reserve(size_t(fp.tellg()) / sizeof(SpvWord));
105 fp.seekg(0, fp.beg);
106
107 while (!fp.eof()) {
108 SpvWord inWord;
109 fp.read((char *)&inWord, sizeof(inWord));
110
111 if (!fp.eof()) {
112 spv.push_back(inWord);
113 if (fp.fail())
114 errHandler(std::string("error reading file: ") + inFilename);
115 }
116 }
117 }
118
119 // Read strings from a file
read(std::vector<std::string> & strings,const std::string & inFilename,int verbosity)120 void read(std::vector<std::string>& strings, const std::string& inFilename, int verbosity)
121 {
122 std::ifstream fp;
123
124 if (verbosity > 0)
125 logHandler(std::string(" reading: ") + inFilename);
126
127 strings.clear();
128 fp.open(inFilename, std::fstream::in);
129
130 if (fp.fail())
131 errHandler("error opening file for read: ");
132
133 std::string line;
134 while (std::getline(fp, line))
135 {
136 // Ignore empty lines and lines starting with the comment marker '#'.
137 if (line.length() == 0 || line[0] == '#') {
138 continue;
139 }
140
141 strings.push_back(line);
142 }
143 }
144
write(std::vector<SpvWord> & spv,const std::string & outFile,int verbosity)145 void write(std::vector<SpvWord>& spv, const std::string& outFile, int verbosity)
146 {
147 if (outFile.empty())
148 errHandler("missing output filename.");
149
150 std::ofstream fp;
151
152 if (verbosity > 0)
153 logHandler(std::string(" writing: ") + outFile);
154
155 fp.open(outFile, std::fstream::out | std::fstream::binary);
156
157 if (fp.fail())
158 errHandler(std::string("error opening file for write: ") + outFile);
159
160 for (auto it = spv.cbegin(); it != spv.cend(); ++it) {
161 SpvWord word = *it;
162 fp.write((char *)&word, sizeof(word));
163 if (fp.fail())
164 errHandler(std::string("error writing file: ") + outFile);
165 }
166
167 // file is closed by destructor
168 }
169
170 // Print helpful usage message to stdout, and exit
usage(const char * const name,const char * const msg=nullptr)171 void usage(const char* const name, const char* const msg = nullptr)
172 {
173 if (msg)
174 std::cout << msg << std::endl << std::endl;
175
176 std::cout << "Usage: " << std::endl;
177
178 std::cout << " " << basename(name)
179 << " [-v[v[...]] | --verbose [int]]"
180 << " [--map (all|types|names|funcs)]"
181 << " [--dce (all|types|funcs)]"
182 << " [--opt (all|loadstore)]"
183 << " [--strip-all | --strip all | -s]"
184 << " [--strip-white-list]"
185 << " [--do-everything]"
186 << " --input | -i file1 [file2...] --output|-o DESTDIR | destfile1 [destfile2...]"
187 << std::endl;
188
189 std::cout << " " << basename(name) << " [--version | -V]" << std::endl;
190 std::cout << " " << basename(name) << " [--help | -?]" << std::endl;
191
192 exit(5);
193 }
194
195 // grind through each SPIR in turn
execute(const std::vector<std::string> & inputFiles,const std::vector<std::string> & outputDirOrFiles,const bool isSingleOutputDir,const std::string & whiteListFile,int opts,int verbosity)196 void execute(const std::vector<std::string>& inputFiles,
197 const std::vector<std::string>& outputDirOrFiles,
198 const bool isSingleOutputDir,
199 const std::string& whiteListFile,
200 int opts,
201 int verbosity)
202 {
203 std::vector<std::string> whiteListStrings;
204 if (!whiteListFile.empty())
205 read(whiteListStrings, whiteListFile, verbosity);
206
207 for (std::size_t ii=0; ii<inputFiles.size(); ii++) {
208 std::vector<SpvWord> spv;
209 read(spv, inputFiles[ii], verbosity);
210
211 spv::spirvbin_t(verbosity).remap(spv, whiteListStrings, opts);
212
213 if (isSingleOutputDir) {
214 // write all outputs to same directory
215 const std::string outFile = outputDirOrFiles[0] + path_sep_char() + basename(inputFiles[ii]);
216 write(spv, outFile, verbosity);
217 } else {
218 // write each input to its associated output
219 write(spv, outputDirOrFiles[ii], verbosity);
220 }
221 }
222
223 if (verbosity > 0)
224 std::cout << "Done: " << inputFiles.size() << " file(s) processed" << std::endl;
225 }
226
227 // Parse command line options
parseCmdLine(int argc,char ** argv,std::vector<std::string> & inputFiles,std::vector<std::string> & outputDirOrFiles,std::string & stripWhiteListFile,int & options,int & verbosity)228 void parseCmdLine(int argc,
229 char** argv,
230 std::vector<std::string>& inputFiles,
231 std::vector<std::string>& outputDirOrFiles,
232 std::string& stripWhiteListFile,
233 int& options,
234 int& verbosity)
235 {
236 if (argc < 2)
237 usage(argv[0]);
238
239 verbosity = 0;
240 options = spv::spirvbin_t::NONE;
241
242 // Parse command line.
243 // boost::program_options would be quite a bit nicer, but we don't want to
244 // introduce a dependency on boost.
245 for (int a=1; a<argc; ) {
246 const std::string arg = argv[a];
247
248 if (arg == "--output" || arg == "-o") {
249 // Collect output dirs or files
250 for (++a; a < argc && argv[a][0] != '-'; ++a)
251 outputDirOrFiles.push_back(argv[a]);
252
253 if (outputDirOrFiles.size() == 0)
254 usage(argv[0], "--output requires an argument");
255
256 // Remove trailing directory separator characters from all paths
257 for (std::size_t ii=0; ii<outputDirOrFiles.size(); ii++) {
258 auto path = outputDirOrFiles[ii];
259 while (!path.empty() && path.back() == path_sep_char())
260 path.pop_back();
261 }
262 }
263 else if (arg == "-vv") { verbosity = 2; ++a; } // verbosity shortcuts
264 else if (arg == "-vvv") { verbosity = 3; ++a; } // ...
265 else if (arg == "-vvvv") { verbosity = 4; ++a; } // ...
266 else if (arg == "-vvvvv") { verbosity = 5; ++a; } // ...
267
268 else if (arg == "--verbose" || arg == "-v") {
269 ++a;
270 verbosity = 1;
271
272 if (a < argc) {
273 char* end_ptr = nullptr;
274 int verb = ::strtol(argv[a], &end_ptr, 10);
275 // If we have not read to the end of the string or
276 // the string contained no elements, then we do not want to
277 // store the value.
278 if (*end_ptr == '\0' && end_ptr != argv[a]) {
279 verbosity = verb;
280 ++a;
281 }
282 }
283 }
284 else if (arg == "--version" || arg == "-V") {
285 std::cout << basename(argv[0]) << " version 0.97" << std::endl;
286 exit(0);
287 } else if (arg == "--input" || arg == "-i") {
288 // Collect input files
289 for (++a; a < argc && argv[a][0] != '-'; ++a)
290 inputFiles.push_back(argv[a]);
291 } else if (arg == "--do-everything") {
292 ++a;
293 options = options | spv::spirvbin_t::DO_EVERYTHING;
294 } else if (arg == "--strip-all" || arg == "-s") {
295 ++a;
296 options = options | spv::spirvbin_t::STRIP;
297 } else if (arg == "--strip") {
298 ++a;
299 if (strncmp(argv[a], "all", 3) == 0) {
300 options = options | spv::spirvbin_t::STRIP;
301 ++a;
302 }
303 } else if (arg == "--strip-white-list") {
304 ++a;
305 stripWhiteListFile = argv[a++];
306 } else if (arg == "--dce") {
307 // Parse comma (or colon, etc) separated list of things to dce
308 ++a;
309 for (const char* c = argv[a]; *c; ++c) {
310 if (strncmp(c, "all", 3) == 0) {
311 options = (options | spv::spirvbin_t::DCE_ALL);
312 c += 3;
313 } else if (strncmp(c, "*", 1) == 0) {
314 options = (options | spv::spirvbin_t::DCE_ALL);
315 c += 1;
316 } else if (strncmp(c, "funcs", 5) == 0) {
317 options = (options | spv::spirvbin_t::DCE_FUNCS);
318 c += 5;
319 } else if (strncmp(c, "types", 5) == 0) {
320 options = (options | spv::spirvbin_t::DCE_TYPES);
321 c += 5;
322 }
323 }
324 ++a;
325 } else if (arg == "--map") {
326 // Parse comma (or colon, etc) separated list of things to map
327 ++a;
328 for (const char* c = argv[a]; *c; ++c) {
329 if (strncmp(c, "all", 3) == 0) {
330 options = (options | spv::spirvbin_t::MAP_ALL);
331 c += 3;
332 } else if (strncmp(c, "*", 1) == 0) {
333 options = (options | spv::spirvbin_t::MAP_ALL);
334 c += 1;
335 } else if (strncmp(c, "types", 5) == 0) {
336 options = (options | spv::spirvbin_t::MAP_TYPES);
337 c += 5;
338 } else if (strncmp(c, "names", 5) == 0) {
339 options = (options | spv::spirvbin_t::MAP_NAMES);
340 c += 5;
341 } else if (strncmp(c, "funcs", 5) == 0) {
342 options = (options | spv::spirvbin_t::MAP_FUNCS);
343 c += 5;
344 }
345 }
346 ++a;
347 } else if (arg == "--opt") {
348 ++a;
349 for (const char* c = argv[a]; *c; ++c) {
350 if (strncmp(c, "all", 3) == 0) {
351 options = (options | spv::spirvbin_t::OPT_ALL);
352 c += 3;
353 } else if (strncmp(c, "*", 1) == 0) {
354 options = (options | spv::spirvbin_t::OPT_ALL);
355 c += 1;
356 } else if (strncmp(c, "loadstore", 9) == 0) {
357 options = (options | spv::spirvbin_t::OPT_LOADSTORE);
358 c += 9;
359 }
360 }
361 ++a;
362 } else if (arg == "--help" || arg == "-?") {
363 usage(argv[0]);
364 } else {
365 usage(argv[0], "Unknown command line option");
366 }
367 }
368 }
369
370 } // namespace
371
372 #ifdef OH_SDK
IsDirectory(const std::string & path)373 static bool IsDirectory(const std::string& path)
374 {
375 struct stat info;
376 if (stat(path.c_str(), &info) != 0) {
377 return false;
378 }
379 return S_ISDIR(info.st_mode);
380 }
381 #endif // OH_SDK
382
main(int argc,char ** argv)383 int main(int argc, char** argv)
384 {
385 std::vector<std::string> inputFiles;
386 std::vector<std::string> outputDirOrFiles;
387 std::string whiteListFile;
388 int opts;
389 int verbosity;
390
391 // handle errors by exiting
392 spv::spirvbin_t::registerErrorHandler(errHandler);
393
394 // Log messages to std::cout
395 spv::spirvbin_t::registerLogHandler(logHandler);
396
397 if (argc < 2)
398 usage(argv[0]);
399
400 parseCmdLine(argc, argv, inputFiles, outputDirOrFiles, whiteListFile, opts, verbosity);
401
402 if (outputDirOrFiles.empty())
403 usage(argv[0], "Output directory or file(s) required.");
404
405 const bool isMultiInput = inputFiles.size() > 1;
406 const bool isMultiOutput = outputDirOrFiles.size() > 1;
407 #ifndef OH_SDK
408 const bool isSingleOutputDir = !isMultiOutput && std::filesystem::is_directory(outputDirOrFiles[0]);
409 #else
410 const bool isSingleOutputDir = !isMultiOutput && IsDirectory(outputDirOrFiles[0]);
411 #endif
412 if (isMultiInput && !isMultiOutput && !isSingleOutputDir)
413 usage(argv[0], "Output is not a directory.");
414
415
416 if (isMultiInput && isMultiOutput && (outputDirOrFiles.size() != inputFiles.size()))
417 usage(argv[0], "Output must be either a single directory or one output file per input.");
418
419 // Main operations: read, remap, and write.
420 execute(inputFiles, outputDirOrFiles, isSingleOutputDir, whiteListFile, opts, verbosity);
421
422 // If we get here, everything went OK! Nothing more to be done.
423 }
424