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 #include <filesystem>
41
42 //
43 // Include remapper
44 //
45 #include "../SPIRV/SPVRemapper.h"
46
47 namespace {
48
49 typedef unsigned int SpvWord;
50
51 // Poor man's basename: given a complete path, return file portion.
52 // E.g:
53 // Linux: /foo/bar/test -> test
54 // Win: c:\foo\bar\test -> test
55 // It's not very efficient, but that doesn't matter for our minimal-duty use.
56 // Using boost::filesystem would be better in many ways, but want to avoid that dependency.
57
58 // OS dependent path separator (avoiding boost::filesystem dependency)
59 #if defined(_WIN32)
path_sep_char()60 char path_sep_char() { return '\\'; }
61 #else
path_sep_char()62 char path_sep_char() { return '/'; }
63 #endif
64
basename(const std::string filename)65 std::string basename(const std::string filename)
66 {
67 const size_t sepLoc = filename.find_last_of(path_sep_char());
68
69 return (sepLoc == filename.npos) ? filename : filename.substr(sepLoc+1);
70 }
71
errHandler(const std::string & str)72 void errHandler(const std::string& str) {
73 std::cout << str << std::endl;
74 exit(5);
75 }
76
logHandler(const std::string & str)77 void logHandler(const std::string& str) {
78 std::cout << str << std::endl;
79 }
80
81 // Read word stream from disk
read(std::vector<SpvWord> & spv,const std::string & inFilename,int verbosity)82 void read(std::vector<SpvWord>& spv, const std::string& inFilename, int verbosity)
83 {
84 std::ifstream fp;
85
86 if (verbosity > 0)
87 logHandler(std::string(" reading: ") + inFilename);
88
89 spv.clear();
90 fp.open(inFilename, std::fstream::in | std::fstream::binary);
91
92 if (fp.fail())
93 errHandler("error opening file for read: ");
94
95 // Reserve space (for efficiency, not for correctness)
96 fp.seekg(0, fp.end);
97 spv.reserve(size_t(fp.tellg()) / sizeof(SpvWord));
98 fp.seekg(0, fp.beg);
99
100 while (!fp.eof()) {
101 SpvWord inWord;
102 fp.read((char *)&inWord, sizeof(inWord));
103
104 if (!fp.eof()) {
105 spv.push_back(inWord);
106 if (fp.fail())
107 errHandler(std::string("error reading file: ") + inFilename);
108 }
109 }
110 }
111
112 // Read strings from a file
read(std::vector<std::string> & strings,const std::string & inFilename,int verbosity)113 void read(std::vector<std::string>& strings, const std::string& inFilename, int verbosity)
114 {
115 std::ifstream fp;
116
117 if (verbosity > 0)
118 logHandler(std::string(" reading: ") + inFilename);
119
120 strings.clear();
121 fp.open(inFilename, std::fstream::in);
122
123 if (fp.fail())
124 errHandler("error opening file for read: ");
125
126 std::string line;
127 while (std::getline(fp, line))
128 {
129 // Ignore empty lines and lines starting with the comment marker '#'.
130 if (line.length() == 0 || line[0] == '#') {
131 continue;
132 }
133
134 strings.push_back(line);
135 }
136 }
137
write(std::vector<SpvWord> & spv,const std::string & outFile,int verbosity)138 void write(std::vector<SpvWord>& spv, const std::string& outFile, int verbosity)
139 {
140 if (outFile.empty())
141 errHandler("missing output filename.");
142
143 std::ofstream fp;
144
145 if (verbosity > 0)
146 logHandler(std::string(" writing: ") + outFile);
147
148 fp.open(outFile, std::fstream::out | std::fstream::binary);
149
150 if (fp.fail())
151 errHandler(std::string("error opening file for write: ") + outFile);
152
153 for (auto it = spv.cbegin(); it != spv.cend(); ++it) {
154 SpvWord word = *it;
155 fp.write((char *)&word, sizeof(word));
156 if (fp.fail())
157 errHandler(std::string("error writing file: ") + outFile);
158 }
159
160 // file is closed by destructor
161 }
162
163 // Print helpful usage message to stdout, and exit
usage(const char * const name,const char * const msg=nullptr)164 void usage(const char* const name, const char* const msg = nullptr)
165 {
166 if (msg)
167 std::cout << msg << std::endl << std::endl;
168
169 std::cout << "Usage: " << std::endl;
170
171 std::cout << " " << basename(name)
172 << " [-v[v[...]] | --verbose [int]]"
173 << " [--map (all|types|names|funcs)]"
174 << " [--dce (all|types|funcs)]"
175 << " [--opt (all|loadstore)]"
176 << " [--strip-all | --strip all | -s]"
177 << " [--strip-white-list]"
178 << " [--do-everything]"
179 << " --input | -i file1 [file2...] --output|-o DESTDIR | destfile1 [destfile2...]"
180 << std::endl;
181
182 std::cout << " " << basename(name) << " [--version | -V]" << std::endl;
183 std::cout << " " << basename(name) << " [--help | -?]" << std::endl;
184
185 exit(5);
186 }
187
188 // 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)189 void execute(const std::vector<std::string>& inputFiles,
190 const std::vector<std::string>& outputDirOrFiles,
191 const bool isSingleOutputDir,
192 const std::string& whiteListFile,
193 int opts,
194 int verbosity)
195 {
196 std::vector<std::string> whiteListStrings;
197 if (!whiteListFile.empty())
198 read(whiteListStrings, whiteListFile, verbosity);
199
200 for (std::size_t ii=0; ii<inputFiles.size(); ii++) {
201 std::vector<SpvWord> spv;
202 read(spv, inputFiles[ii], verbosity);
203
204 spv::spirvbin_t(verbosity).remap(spv, whiteListStrings, opts);
205
206 if (isSingleOutputDir) {
207 // write all outputs to same directory
208 const std::string outFile = outputDirOrFiles[0] + path_sep_char() + basename(inputFiles[ii]);
209 write(spv, outFile, verbosity);
210 } else {
211 // write each input to its associated output
212 write(spv, outputDirOrFiles[ii], verbosity);
213 }
214 }
215
216 if (verbosity > 0)
217 std::cout << "Done: " << inputFiles.size() << " file(s) processed" << std::endl;
218 }
219
220 // 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)221 void parseCmdLine(int argc,
222 char** argv,
223 std::vector<std::string>& inputFiles,
224 std::vector<std::string>& outputDirOrFiles,
225 std::string& stripWhiteListFile,
226 int& options,
227 int& verbosity)
228 {
229 if (argc < 2)
230 usage(argv[0]);
231
232 verbosity = 0;
233 options = spv::spirvbin_t::NONE;
234
235 // Parse command line.
236 // boost::program_options would be quite a bit nicer, but we don't want to
237 // introduce a dependency on boost.
238 for (int a=1; a<argc; ) {
239 const std::string arg = argv[a];
240
241 if (arg == "--output" || arg == "-o") {
242 // Collect output dirs or files
243 for (++a; a < argc && argv[a][0] != '-'; ++a)
244 outputDirOrFiles.push_back(argv[a]);
245
246 if (outputDirOrFiles.size() == 0)
247 usage(argv[0], "--output requires an argument");
248
249 // Remove trailing directory separator characters from all paths
250 for (std::size_t ii=0; ii<outputDirOrFiles.size(); ii++) {
251 auto path = outputDirOrFiles[ii];
252 while (!path.empty() && path.back() == path_sep_char())
253 path.pop_back();
254 }
255 }
256 else if (arg == "-vv") { verbosity = 2; ++a; } // verbosity shortcuts
257 else if (arg == "-vvv") { verbosity = 3; ++a; } // ...
258 else if (arg == "-vvvv") { verbosity = 4; ++a; } // ...
259 else if (arg == "-vvvvv") { verbosity = 5; ++a; } // ...
260
261 else if (arg == "--verbose" || arg == "-v") {
262 ++a;
263 verbosity = 1;
264
265 if (a < argc) {
266 char* end_ptr = nullptr;
267 int verb = ::strtol(argv[a], &end_ptr, 10);
268 // If we have not read to the end of the string or
269 // the string contained no elements, then we do not want to
270 // store the value.
271 if (*end_ptr == '\0' && end_ptr != argv[a]) {
272 verbosity = verb;
273 ++a;
274 }
275 }
276 }
277 else if (arg == "--version" || arg == "-V") {
278 std::cout << basename(argv[0]) << " version 0.97" << std::endl;
279 exit(0);
280 } else if (arg == "--input" || arg == "-i") {
281 // Collect input files
282 for (++a; a < argc && argv[a][0] != '-'; ++a)
283 inputFiles.push_back(argv[a]);
284 } else if (arg == "--do-everything") {
285 ++a;
286 options = options | spv::spirvbin_t::DO_EVERYTHING;
287 } else if (arg == "--strip-all" || arg == "-s") {
288 ++a;
289 options = options | spv::spirvbin_t::STRIP;
290 } else if (arg == "--strip") {
291 ++a;
292 if (strncmp(argv[a], "all", 3) == 0) {
293 options = options | spv::spirvbin_t::STRIP;
294 ++a;
295 }
296 } else if (arg == "--strip-white-list") {
297 ++a;
298 stripWhiteListFile = argv[a++];
299 } else if (arg == "--dce") {
300 // Parse comma (or colon, etc) separated list of things to dce
301 ++a;
302 for (const char* c = argv[a]; *c; ++c) {
303 if (strncmp(c, "all", 3) == 0) {
304 options = (options | spv::spirvbin_t::DCE_ALL);
305 c += 3;
306 } else if (strncmp(c, "*", 1) == 0) {
307 options = (options | spv::spirvbin_t::DCE_ALL);
308 c += 1;
309 } else if (strncmp(c, "funcs", 5) == 0) {
310 options = (options | spv::spirvbin_t::DCE_FUNCS);
311 c += 5;
312 } else if (strncmp(c, "types", 5) == 0) {
313 options = (options | spv::spirvbin_t::DCE_TYPES);
314 c += 5;
315 }
316 }
317 ++a;
318 } else if (arg == "--map") {
319 // Parse comma (or colon, etc) separated list of things to map
320 ++a;
321 for (const char* c = argv[a]; *c; ++c) {
322 if (strncmp(c, "all", 3) == 0) {
323 options = (options | spv::spirvbin_t::MAP_ALL);
324 c += 3;
325 } else if (strncmp(c, "*", 1) == 0) {
326 options = (options | spv::spirvbin_t::MAP_ALL);
327 c += 1;
328 } else if (strncmp(c, "types", 5) == 0) {
329 options = (options | spv::spirvbin_t::MAP_TYPES);
330 c += 5;
331 } else if (strncmp(c, "names", 5) == 0) {
332 options = (options | spv::spirvbin_t::MAP_NAMES);
333 c += 5;
334 } else if (strncmp(c, "funcs", 5) == 0) {
335 options = (options | spv::spirvbin_t::MAP_FUNCS);
336 c += 5;
337 }
338 }
339 ++a;
340 } else if (arg == "--opt") {
341 ++a;
342 for (const char* c = argv[a]; *c; ++c) {
343 if (strncmp(c, "all", 3) == 0) {
344 options = (options | spv::spirvbin_t::OPT_ALL);
345 c += 3;
346 } else if (strncmp(c, "*", 1) == 0) {
347 options = (options | spv::spirvbin_t::OPT_ALL);
348 c += 1;
349 } else if (strncmp(c, "loadstore", 9) == 0) {
350 options = (options | spv::spirvbin_t::OPT_LOADSTORE);
351 c += 9;
352 }
353 }
354 ++a;
355 } else if (arg == "--help" || arg == "-?") {
356 usage(argv[0]);
357 } else {
358 usage(argv[0], "Unknown command line option");
359 }
360 }
361 }
362
363 } // namespace
364
main(int argc,char ** argv)365 int main(int argc, char** argv)
366 {
367 std::vector<std::string> inputFiles;
368 std::vector<std::string> outputDirOrFiles;
369 std::string whiteListFile;
370 int opts;
371 int verbosity;
372
373 // handle errors by exiting
374 spv::spirvbin_t::registerErrorHandler(errHandler);
375
376 // Log messages to std::cout
377 spv::spirvbin_t::registerLogHandler(logHandler);
378
379 if (argc < 2)
380 usage(argv[0]);
381
382 parseCmdLine(argc, argv, inputFiles, outputDirOrFiles, whiteListFile, opts, verbosity);
383
384 if (outputDirOrFiles.empty())
385 usage(argv[0], "Output directory or file(s) required.");
386
387 const bool isMultiInput = inputFiles.size() > 1;
388 const bool isMultiOutput = outputDirOrFiles.size() > 1;
389 const bool isSingleOutputDir = !isMultiOutput && std::filesystem::is_directory(outputDirOrFiles[0]);
390
391 if (isMultiInput && !isMultiOutput && !isSingleOutputDir)
392 usage(argv[0], "Output is not a directory.");
393
394
395 if (isMultiInput && isMultiOutput && (outputDirOrFiles.size() != inputFiles.size()))
396 usage(argv[0], "Output must be either a single directory or one output file per input.");
397
398 // Main operations: read, remap, and write.
399 execute(inputFiles, outputDirOrFiles, isSingleOutputDir, whiteListFile, opts, verbosity);
400
401 // If we get here, everything went OK! Nothing more to be done.
402 }
403