1 // Copyright (c) 2014-2018 The Khronos Group Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and/or associated documentation files (the "Materials"),
5 // to deal in the Materials without restriction, including without limitation
6 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 // and/or sell copies of the Materials, and to permit persons to whom the
8 // Materials are furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Materials.
12 //
13 // MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS KHRONOS
14 // STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS SPECIFICATIONS AND
15 // HEADER INFORMATION ARE LOCATED AT https://www.khronos.org/registry/
16 //
17 // THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM,OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE USE OR OTHER DEALINGS
23 // IN THE MATERIALS.
24 
25 //#include <fstream>
26 #include <string>
27 #include <algorithm>
28 
29 #include "jsonToSpirv.h"
30 #include "header.h"
31 
32 // Command-line options
33 enum TOptions {
34     EOptionNone                       = 0x000,
35     EOptionPrintHeader                = 0x008,
36 };
37 
38 std::string jsonPath;
39 int Options;
40 spv::TLanguage Language;
41 
Usage()42 void Usage()
43 {
44     printf("Usage: spirv option [file]\n"
45            "\n"
46            "  -h <language> print header for given language to stdout, from one of:\n"
47            "     C      - C99 header\n"
48            "     C++    - C++03 or greater header (also accepts C++03)\n"
49            "     C++11  - C++11 or greater header\n"
50            "     JSON   - JSON format data\n"
51            "     Lua    - Lua module\n"
52            "     Python - Python module (also accepts Py)\n"
53            "     C#     - C# module (also accepts CSharp)\n"
54            "  -H print header in all supported languages to files in current directory\n"
55            );
56 }
57 
tolower_s(std::string s)58 std::string tolower_s(std::string s)
59 {
60     std::transform(s.begin(), s.end(), s.begin(), ::tolower);
61     return s;
62 }
63 
ProcessArguments(int argc,char * argv[])64 bool ProcessArguments(int argc, char* argv[])
65 {
66     argc--;
67     argv++;
68     for (; argc >= 1; argc--, argv++) {
69         if (argv[0][0] == '-') {
70             switch (argv[0][1]) {
71             case 'H':
72                 Options |= EOptionPrintHeader;
73                 Language = spv::ELangAll;
74                 break;
75             case 'h': {
76                 if (argc < 2)
77                     return false;
78 
79                 Options |= EOptionPrintHeader;
80                 const std::string language(tolower_s(argv[1]));
81 
82                 if (language == "c") {
83                     Language = spv::ELangC;
84                 } else if (language == "c++" || language == "c++03") {
85                     Language = spv::ELangCPP;
86                 } else if (language == "c++11") {
87                     Language = spv::ELangCPP11;
88                 } else if (language == "json") {
89                     Language = spv::ELangJSON;
90                 } else if (language == "lua") {
91                     Language = spv::ELangLua;
92                 } else if (language == "python" || language == "py") {
93                     Language = spv::ELangPython;
94                 }
95                 else if (language == "c#" || language == "csharp") {
96                     Language = spv::ELangCSharp;
97                 } else
98                     return false;
99 
100                 return true;
101             }
102             default:
103                 return false;
104             }
105         } else {
106             jsonPath = std::string(argv[0]);
107         }
108     }
109 
110     return true;
111 }
112 
main(int argc,char * argv[])113 int main(int argc, char* argv[])
114 {
115     if (argc < 2 || ! ProcessArguments(argc, argv)) {
116         Usage();
117         return 1;
118     }
119 
120     spv::jsonToSpirv(jsonPath);
121     if (Options & EOptionPrintHeader)
122         spv::PrintHeader(Language, std::cout);
123 
124     return 0;
125 }
126