Merge pull request #2997 from RandomShaper/remove_unused
[platform/upstream/glslang.git] / StandAlone / spirv-remap.cpp
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
41 #include "../SPIRV/SPVRemapper.h"
42
43 namespace {
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     // Read strings from a file
109     void read(std::vector<std::string>& strings, const std::string& inFilename, int verbosity)
110     {
111         std::ifstream fp;
112
113         if (verbosity > 0)
114             logHandler(std::string("  reading: ") + inFilename);
115
116         strings.clear();
117         fp.open(inFilename, std::fstream::in);
118
119         if (fp.fail())
120             errHandler("error opening file for read: ");
121
122         std::string line;
123         while (std::getline(fp, line))
124         {
125             // Ignore empty lines and lines starting with the comment marker '#'.
126             if (line.length() == 0 || line[0] == '#') {
127                 continue;
128             }
129
130             strings.push_back(line);
131         }
132     }
133
134     void write(std::vector<SpvWord>& spv, const std::string& outFile, int verbosity)
135     {
136         if (outFile.empty())
137             errHandler("missing output filename.");
138
139         std::ofstream fp;
140
141         if (verbosity > 0)
142             logHandler(std::string("  writing: ") + outFile);
143
144         fp.open(outFile, std::fstream::out | std::fstream::binary);
145
146         if (fp.fail())
147             errHandler(std::string("error opening file for write: ") + outFile);
148
149         for (auto it = spv.cbegin(); it != spv.cend(); ++it) {
150             SpvWord word = *it;
151             fp.write((char *)&word, sizeof(word));
152             if (fp.fail())
153                 errHandler(std::string("error writing file: ") + outFile);
154         }
155
156         // file is closed by destructor
157     }
158
159     // Print helpful usage message to stdout, and exit
160     void usage(const char* const name, const char* const msg = 0)
161     {
162         if (msg)
163             std::cout << msg << std::endl << std::endl;
164
165         std::cout << "Usage: " << std::endl;
166
167         std::cout << "  " << basename(name)
168             << " [-v[v[...]] | --verbose [int]]"
169             << " [--map (all|types|names|funcs)]"
170             << " [--dce (all|types|funcs)]"
171             << " [--opt (all|loadstore)]"
172             << " [--strip-all | --strip all | -s]"
173             << " [--strip-white-list]"
174             << " [--do-everything]"
175             << " --input | -i file1 [file2...] --output|-o DESTDIR"
176             << std::endl;
177
178         std::cout << "  " << basename(name) << " [--version | -V]" << std::endl;
179         std::cout << "  " << basename(name) << " [--help | -?]" << std::endl;
180
181         exit(5);
182     }
183
184     // grind through each SPIR in turn
185     void execute(const std::vector<std::string>& inputFile, const std::string& outputDir,
186                  const std::string& whiteListFile, int opts, int verbosity)
187     {
188         std::vector<std::string> whiteListStrings;
189         if(!whiteListFile.empty())
190             read(whiteListStrings, whiteListFile, verbosity);
191
192         for (auto it = inputFile.cbegin(); it != inputFile.cend(); ++it) {
193             const std::string &filename = *it;
194             std::vector<SpvWord> spv;
195             read(spv, filename, verbosity);
196             spv::spirvbin_t(verbosity).remap(spv, whiteListStrings, opts);
197             const std::string outfile = outputDir + path_sep_char() + basename(filename);
198             write(spv, outfile, verbosity);
199         }
200
201         if (verbosity > 0)
202             std::cout << "Done: " << inputFile.size() << " file(s) processed" << std::endl;
203     }
204
205     // Parse command line options
206     void parseCmdLine(int argc, char** argv, std::vector<std::string>& inputFile,
207         std::string& outputDir,
208         std::string& stripWhiteListFile,
209         int& options,
210         int& verbosity)
211     {
212         if (argc < 2)
213             usage(argv[0]);
214
215         verbosity  = 0;
216         options    = spv::spirvbin_t::NONE;
217
218         // Parse command line.
219         // boost::program_options would be quite a bit nicer, but we don't want to
220         // introduce a dependency on boost.
221         for (int a=1; a<argc; ) {
222             const std::string arg = argv[a];
223
224             if (arg == "--output" || arg == "-o") {
225                 // Output directory
226                 if (++a >= argc)
227                     usage(argv[0], "--output requires an argument");
228                 if (!outputDir.empty())
229                     usage(argv[0], "--output can be provided only once");
230
231                 outputDir = argv[a++];
232
233                 // Remove trailing directory separator characters
234                 while (!outputDir.empty() && outputDir.back() == path_sep_char())
235                     outputDir.pop_back();
236
237             }
238             else if (arg == "-vv")     { verbosity = 2; ++a; } // verbosity shortcuts
239             else if (arg == "-vvv")    { verbosity = 3; ++a; } // ...
240             else if (arg == "-vvvv")   { verbosity = 4; ++a; } // ...
241             else if (arg == "-vvvvv")  { verbosity = 5; ++a; } // ...
242
243             else if (arg == "--verbose" || arg == "-v") {
244                 ++a;
245                 verbosity = 1;
246
247                 if (a < argc) {
248                     char* end_ptr = 0;
249                     int verb = ::strtol(argv[a], &end_ptr, 10);
250                     // If we have not read to the end of the string or
251                     // the string contained no elements, then we do not want to
252                     // store the value.
253                     if (*end_ptr == '\0' && end_ptr != argv[a]) {
254                         verbosity = verb;
255                         ++a;
256                     }
257                 }
258             }
259             else if (arg == "--version" || arg == "-V") {
260                 std::cout << basename(argv[0]) << " version 0.97" << std::endl;
261                 exit(0);
262             } else if (arg == "--input" || arg == "-i") {
263                 // Collect input files
264                 for (++a; a < argc && argv[a][0] != '-'; ++a)
265                     inputFile.push_back(argv[a]);
266             } else if (arg == "--do-everything") {
267                 ++a;
268                 options = options | spv::spirvbin_t::DO_EVERYTHING;
269             } else if (arg == "--strip-all" || arg == "-s") {
270                 ++a;
271                 options = options | spv::spirvbin_t::STRIP;
272             } else if (arg == "--strip") {
273                 ++a;
274                 if (strncmp(argv[a], "all", 3) == 0) {
275                     options = options | spv::spirvbin_t::STRIP;
276                     ++a;
277                 }
278             } else if (arg == "--strip-white-list") {
279                 ++a;
280                 stripWhiteListFile = argv[a++];
281             } else if (arg == "--dce") {
282                 // Parse comma (or colon, etc) separated list of things to dce
283                 ++a;
284                 for (const char* c = argv[a]; *c; ++c) {
285                     if (strncmp(c, "all", 3) == 0) {
286                         options = (options | spv::spirvbin_t::DCE_ALL);
287                         c += 3;
288                     } else if (strncmp(c, "*", 1) == 0) {
289                         options = (options | spv::spirvbin_t::DCE_ALL);
290                         c += 1;
291                     } else if (strncmp(c, "funcs", 5) == 0) {
292                         options = (options | spv::spirvbin_t::DCE_FUNCS);
293                         c += 5;
294                     } else if (strncmp(c, "types", 5) == 0) {
295                         options = (options | spv::spirvbin_t::DCE_TYPES);
296                         c += 5;
297                     }
298                 }
299                 ++a;
300             } else if (arg == "--map") {
301                 // Parse comma (or colon, etc) separated list of things to map
302                 ++a;
303                 for (const char* c = argv[a]; *c; ++c) {
304                     if (strncmp(c, "all", 3) == 0) {
305                         options = (options | spv::spirvbin_t::MAP_ALL);
306                         c += 3;
307                     } else if (strncmp(c, "*", 1) == 0) {
308                         options = (options | spv::spirvbin_t::MAP_ALL);
309                         c += 1;
310                     } else if (strncmp(c, "types", 5) == 0) {
311                         options = (options | spv::spirvbin_t::MAP_TYPES);
312                         c += 5;
313                     } else if (strncmp(c, "names", 5) == 0) {
314                         options = (options | spv::spirvbin_t::MAP_NAMES);
315                         c += 5;
316                     } else if (strncmp(c, "funcs", 5) == 0) {
317                         options = (options | spv::spirvbin_t::MAP_FUNCS);
318                         c += 5;
319                     }
320                 }
321                 ++a;
322             } else if (arg == "--opt") {
323                 ++a;
324                 for (const char* c = argv[a]; *c; ++c) {
325                     if (strncmp(c, "all", 3) == 0) {
326                         options = (options | spv::spirvbin_t::OPT_ALL);
327                         c += 3;
328                     } else if (strncmp(c, "*", 1) == 0) {
329                         options = (options | spv::spirvbin_t::OPT_ALL);
330                         c += 1;
331                     } else if (strncmp(c, "loadstore", 9) == 0) {
332                         options = (options | spv::spirvbin_t::OPT_LOADSTORE);
333                         c += 9;
334                     }
335                 }
336                 ++a;
337             } else if (arg == "--help" || arg == "-?") {
338                 usage(argv[0]);
339             } else {
340                 usage(argv[0], "Unknown command line option");
341             }
342         }
343     }
344
345 } // namespace
346
347 int main(int argc, char** argv)
348 {
349     std::vector<std::string> inputFile;
350     std::string              outputDir;
351     std::string              whiteListFile;
352     int                      opts;
353     int                      verbosity;
354
355 #ifdef use_cpp11
356     // handle errors by exiting
357     spv::spirvbin_t::registerErrorHandler(errHandler);
358
359     // Log messages to std::cout
360     spv::spirvbin_t::registerLogHandler(logHandler);
361 #endif
362
363     if (argc < 2)
364         usage(argv[0]);
365
366     parseCmdLine(argc, argv, inputFile, outputDir, whiteListFile, opts, verbosity);
367
368     if (outputDir.empty())
369         usage(argv[0], "Output directory required");
370
371     // Main operations: read, remap, and write.
372     execute(inputFile, outputDir, whiteListFile, opts, verbosity);
373
374     // If we get here, everything went OK!  Nothing more to be done.
375 }