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