blob: 998f7428aa54a4824fe341acd30b936f15fdfd03 [file] [log] [blame]
John Kessenich140f3df2015-06-26 16:58:36 -06001//
John Kessenich927608b2017-01-06 12:34:14 -07002// Copyright (C) 2015 LunarG, Inc.
John Kessenich140f3df2015-06-26 16:58:36 -06003//
John Kessenich927608b2017-01-06 12:34:14 -07004// All rights reserved.
John Kessenich140f3df2015-06-26 16:58:36 -06005//
John Kessenich927608b2017-01-06 12:34:14 -07006// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
John Kessenich140f3df2015-06-26 16:58:36 -06009//
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//
John Kessenich927608b2017-01-06 12:34:14 -070022// 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.
John Kessenich140f3df2015-06-26 16:58:36 -060034//
35
36#include <iostream>
37#include <fstream>
38#include <cstring>
39#include <stdexcept>
40
41#include "../SPIRV/SPVRemapper.h"
42
43namespace {
44
45 typedef unsigned int SpvWord;
46
47 // Poor man's basename: given a complete path, return file portion.
48 // E.g:
49 // Linux: /foo/bar/test -> test
50 // Win: c:\foo\bar\test -> test
51 // It's not very efficient, but that doesn't matter for our minimal-duty use.
52 // Using boost::filesystem would be better in many ways, but want to avoid that dependency.
53
54 // OS dependent path separator (avoiding boost::filesystem dependency)
55#if defined(_WIN32)
56 char path_sep_char() { return '\\'; }
57#else
58 char path_sep_char() { return '/'; }
59#endif
60
61 std::string basename(const std::string filename)
62 {
63 const size_t sepLoc = filename.find_last_of(path_sep_char());
64
65 return (sepLoc == filename.npos) ? filename : filename.substr(sepLoc+1);
66 }
67
68 void errHandler(const std::string& str) {
69 std::cout << str << std::endl;
70 exit(5);
71 }
72
73 void logHandler(const std::string& str) {
74 std::cout << str << std::endl;
75 }
76
77 // Read word stream from disk
78 void read(std::vector<SpvWord>& spv, const std::string& inFilename, int verbosity)
79 {
80 std::ifstream fp;
81
82 if (verbosity > 0)
83 logHandler(std::string(" reading: ") + inFilename);
84
85 spv.clear();
86 fp.open(inFilename, std::fstream::in | std::fstream::binary);
87
88 if (fp.fail())
89 errHandler("error opening file for read: ");
90
91 // Reserve space (for efficiency, not for correctness)
92 fp.seekg(0, fp.end);
93 spv.reserve(size_t(fp.tellg()) / sizeof(SpvWord));
94 fp.seekg(0, fp.beg);
95
96 while (!fp.eof()) {
97 SpvWord inWord;
98 fp.read((char *)&inWord, sizeof(inWord));
99
100 if (!fp.eof()) {
101 spv.push_back(inWord);
102 if (fp.fail())
103 errHandler(std::string("error reading file: ") + inFilename);
104 }
105 }
106 }
107
108 void write(std::vector<SpvWord>& spv, const std::string& outFile, int verbosity)
109 {
110 if (outFile.empty())
111 errHandler("missing output filename.");
112
113 std::ofstream fp;
114
115 if (verbosity > 0)
116 logHandler(std::string(" writing: ") + outFile);
117
118 fp.open(outFile, std::fstream::out | std::fstream::binary);
119
120 if (fp.fail())
121 errHandler(std::string("error opening file for write: ") + outFile);
122
rdb32084e82016-02-23 22:17:38 +0100123 for (auto it = spv.cbegin(); it != spv.cend(); ++it) {
124 SpvWord word = *it;
John Kessenich140f3df2015-06-26 16:58:36 -0600125 fp.write((char *)&word, sizeof(word));
126 if (fp.fail())
127 errHandler(std::string("error writing file: ") + outFile);
128 }
129
130 // file is closed by destructor
131 }
132
133 // Print helpful usage message to stdout, and exit
134 void usage(const char* const name, const char* const msg = 0)
135 {
136 if (msg)
137 std::cout << msg << std::endl << std::endl;
138
139 std::cout << "Usage: " << std::endl;
140
141 std::cout << " " << basename(name)
142 << " [-v[v[...]] | --verbose [int]]"
143 << " [--map (all|types|names|funcs)]"
144 << " [--dce (all|types|funcs)]"
145 << " [--opt (all|loadstore)]"
John Kessenichecba76f2017-01-06 00:34:48 -0700146 << " [--strip-all | --strip all | -s]"
147 << " [--do-everything]"
John Kessenich140f3df2015-06-26 16:58:36 -0600148 << " --input | -i file1 [file2...] --output|-o DESTDIR"
149 << std::endl;
150
151 std::cout << " " << basename(name) << " [--version | -V]" << std::endl;
152 std::cout << " " << basename(name) << " [--help | -?]" << std::endl;
153
154 exit(5);
155 }
156
157 // grind through each SPIR in turn
158 void execute(const std::vector<std::string>& inputFile, const std::string& outputDir,
159 int opts, int verbosity)
160 {
rdb32084e82016-02-23 22:17:38 +0100161 for (auto it = inputFile.cbegin(); it != inputFile.cend(); ++it) {
162 const std::string &filename = *it;
John Kessenich140f3df2015-06-26 16:58:36 -0600163 std::vector<SpvWord> spv;
164 read(spv, filename, verbosity);
165 spv::spirvbin_t(verbosity).remap(spv, opts);
166
167 const std::string outfile = outputDir + path_sep_char() + basename(filename);
168
169 write(spv, outfile, verbosity);
170 }
171
172 if (verbosity > 0)
173 std::cout << "Done: " << inputFile.size() << " file(s) processed" << std::endl;
174 }
175
176 // Parse command line options
177 void parseCmdLine(int argc, char** argv, std::vector<std::string>& inputFile,
178 std::string& outputDir,
179 int& options,
180 int& verbosity)
181 {
182 if (argc < 2)
183 usage(argv[0]);
184
185 verbosity = 0;
186 options = spv::spirvbin_t::NONE;
187
188 // Parse command line.
189 // boost::program_options would be quite a bit nicer, but we don't want to
190 // introduce a dependency on boost.
191 for (int a=1; a<argc; ) {
192 const std::string arg = argv[a];
193
194 if (arg == "--output" || arg == "-o") {
195 // Output directory
196 if (++a >= argc)
197 usage(argv[0], "--output requires an argument");
198 if (!outputDir.empty())
199 usage(argv[0], "--output can be provided only once");
200
201 outputDir = argv[a++];
202
203 // Remove trailing directory separator characters
204 while (!outputDir.empty() && outputDir.back() == path_sep_char())
205 outputDir.pop_back();
206
207 }
208 else if (arg == "-vv") { verbosity = 2; ++a; } // verbosity shortcuts
209 else if (arg == "-vvv") { verbosity = 3; ++a; } // ...
210 else if (arg == "-vvvv") { verbosity = 4; ++a; } // ...
211 else if (arg == "-vvvvv") { verbosity = 5; ++a; } // ...
212
213 else if (arg == "--verbose" || arg == "-v") {
214 ++a;
215 verbosity = 1;
216
217 if (a < argc) {
Andrew Woloszyne837f992015-07-10 16:11:00 -0400218 char* end_ptr = 0;
219 int verb = ::strtol(argv[a], &end_ptr, 10);
220 // If we have not read to the end of the string or
221 // the string contained no elements, then we do not want to
222 // store the value.
223 if (*end_ptr == '\0' && end_ptr != argv[a]) {
224 verbosity = verb;
John Kessenich140f3df2015-06-26 16:58:36 -0600225 ++a;
Andrew Woloszyne837f992015-07-10 16:11:00 -0400226 }
John Kessenich140f3df2015-06-26 16:58:36 -0600227 }
228 }
229 else if (arg == "--version" || arg == "-V") {
230 std::cout << basename(argv[0]) << " version 0.97 " << __DATE__ << " " << __TIME__ << std::endl;
231 exit(0);
232 } else if (arg == "--input" || arg == "-i") {
233 // Collect input files
234 for (++a; a < argc && argv[a][0] != '-'; ++a)
235 inputFile.push_back(argv[a]);
236 } else if (arg == "--do-everything") {
237 ++a;
238 options = options | spv::spirvbin_t::DO_EVERYTHING;
239 } else if (arg == "--strip-all" || arg == "-s") {
240 ++a;
241 options = options | spv::spirvbin_t::STRIP;
242 } else if (arg == "--strip") {
243 ++a;
244 if (strncmp(argv[a], "all", 3) == 0) {
245 options = options | spv::spirvbin_t::STRIP;
246 ++a;
247 }
248 } else if (arg == "--dce") {
249 // Parse comma (or colon, etc) separated list of things to dce
250 ++a;
251 for (const char* c = argv[a]; *c; ++c) {
252 if (strncmp(c, "all", 3) == 0) {
253 options = (options | spv::spirvbin_t::DCE_ALL);
254 c += 3;
255 } else if (strncmp(c, "*", 1) == 0) {
256 options = (options | spv::spirvbin_t::DCE_ALL);
257 c += 1;
258 } else if (strncmp(c, "funcs", 5) == 0) {
259 options = (options | spv::spirvbin_t::DCE_FUNCS);
260 c += 5;
261 } else if (strncmp(c, "types", 5) == 0) {
262 options = (options | spv::spirvbin_t::DCE_TYPES);
263 c += 5;
264 }
265 }
266 ++a;
267 } else if (arg == "--map") {
268 // Parse comma (or colon, etc) separated list of things to map
269 ++a;
270 for (const char* c = argv[a]; *c; ++c) {
271 if (strncmp(c, "all", 3) == 0) {
272 options = (options | spv::spirvbin_t::MAP_ALL);
273 c += 3;
274 } else if (strncmp(c, "*", 1) == 0) {
275 options = (options | spv::spirvbin_t::MAP_ALL);
276 c += 1;
277 } else if (strncmp(c, "types", 5) == 0) {
278 options = (options | spv::spirvbin_t::MAP_TYPES);
279 c += 5;
280 } else if (strncmp(c, "names", 5) == 0) {
281 options = (options | spv::spirvbin_t::MAP_NAMES);
282 c += 5;
283 } else if (strncmp(c, "funcs", 5) == 0) {
284 options = (options | spv::spirvbin_t::MAP_FUNCS);
285 c += 5;
286 }
287 }
288 ++a;
289 } else if (arg == "--opt") {
290 ++a;
291 for (const char* c = argv[a]; *c; ++c) {
292 if (strncmp(c, "all", 3) == 0) {
293 options = (options | spv::spirvbin_t::OPT_ALL);
294 c += 3;
295 } else if (strncmp(c, "*", 1) == 0) {
296 options = (options | spv::spirvbin_t::OPT_ALL);
297 c += 1;
298 } else if (strncmp(c, "loadstore", 9) == 0) {
299 options = (options | spv::spirvbin_t::OPT_LOADSTORE);
300 c += 9;
301 }
302 }
303 ++a;
304 } else if (arg == "--help" || arg == "-?") {
305 usage(argv[0]);
306 } else {
307 usage(argv[0], "Unknown command line option");
308 }
309 }
310 }
311
312} // namespace
313
John Kessenich140f3df2015-06-26 16:58:36 -0600314int main(int argc, char** argv)
315{
316 std::vector<std::string> inputFile;
317 std::string outputDir;
318 int opts;
319 int verbosity;
320
321#ifdef use_cpp11
322 // handle errors by exiting
323 spv::spirvbin_t::registerErrorHandler(errHandler);
324
325 // Log messages to std::cout
326 spv::spirvbin_t::registerLogHandler(logHandler);
327#endif
328
329 if (argc < 2)
330 usage(argv[0]);
331
332 parseCmdLine(argc, argv, inputFile, outputDir, opts, verbosity);
333
334 if (outputDir.empty())
335 usage(argv[0], "Output directory required");
336
337 std::string errmsg;
338
339 // Main operations: read, remap, and write.
340 execute(inputFile, outputDir, opts, verbosity);
341
342 // If we get here, everything went OK! Nothing more to be done.
343}