1 // Copyright (c) 2014-2019 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 " D - D module\n"
55 " Beef - Beef module\n"
56 " -H print header in all supported languages to files in current directory\n"
57 );
58 }
59
tolower_s(std::string s)60 std::string tolower_s(std::string s)
61 {
62 std::transform(s.begin(), s.end(), s.begin(), ::tolower);
63 return s;
64 }
65
ProcessArguments(int argc,char * argv[])66 bool ProcessArguments(int argc, char* argv[])
67 {
68 argc--;
69 argv++;
70 for (; argc >= 1; argc--, argv++) {
71 if (argv[0][0] == '-') {
72 switch (argv[0][1]) {
73 case 'H':
74 Options |= EOptionPrintHeader;
75 Language = spv::ELangAll;
76 break;
77 case 'h': {
78 if (argc < 2)
79 return false;
80
81 Options |= EOptionPrintHeader;
82 const std::string language(tolower_s(argv[1]));
83
84 if (language == "c") {
85 Language = spv::ELangC;
86 } else if (language == "c++" || language == "c++03") {
87 Language = spv::ELangCPP;
88 } else if (language == "c++11") {
89 Language = spv::ELangCPP11;
90 } else if (language == "json") {
91 Language = spv::ELangJSON;
92 } else if (language == "lua") {
93 Language = spv::ELangLua;
94 } else if (language == "python" || language == "py") {
95 Language = spv::ELangPython;
96 } else if (language == "c#" || language == "csharp") {
97 Language = spv::ELangCSharp;
98 } else if (language == "d") {
99 Language = spv::ELangD;
100 } else if (language == "beef") {
101 Language = spv::ELangBeef;
102 } else
103 return false;
104
105 return true;
106 }
107 default:
108 return false;
109 }
110 } else {
111 jsonPath = std::string(argv[0]);
112 }
113 }
114
115 return true;
116 }
117
main(int argc,char * argv[])118 int main(int argc, char* argv[])
119 {
120 if (argc < 2 || ! ProcessArguments(argc, argv)) {
121 Usage();
122 return 1;
123 }
124
125 spv::jsonToSpirv(jsonPath, (Options & EOptionPrintHeader) != 0);
126 if (Options & EOptionPrintHeader)
127 spv::PrintHeader(Language, std::cout);
128
129 return 0;
130 }
131