]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
gitattributes for whitespace
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012 
3  *     Dale Weiler
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #include "gmqcc.h"
24 // todo CLEANUP this argitem thing
25 typedef struct { char *name, type; } argitem;
26 VECTOR_MAKE(argitem, items);
27
28 /* global options */
29 bool opts_debug                     = false;
30 bool opts_memchk                    = false;
31 bool opts_darkplaces_stringtablebug = false;
32 bool opts_omit_nullcode             = false;
33 int  opts_compiler                  = COMPILER_GMQCC;
34
35 static const int usage(const char *const app) {
36     printf("usage:\n"
37            "    %s -c<file>          -oprog.dat -- compile file\n"
38            "    %s -a<file>          -oprog.dat -- assemble file\n"
39            "    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n"
40            "    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n"
41            "    example:\n"
42            "    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n"
43            "    additional flags:\n"
44            "        -debug           -- turns on compiler debug messages\n"
45            "        -memchk          -- turns on compiler memory leak check\n"
46            "        -help            -- prints this help/usage text\n"
47            "        -std             -- select the QuakeC compile type (types below):\n"
48            "            -std=qcc     -- original QuakeC\n"
49            "            -std=ftqecc  -- fteqcc QuakeC\n"
50            "            -std=qccx    -- qccx QuakeC\n"
51            "            -std=gmqcc   -- this compiler QuakeC (default selection)\n"
52            "    codegen flags:\n"
53            "        -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n"
54            "        -fomit-nullcode               -- omits the generation of null code (will break everywhere see propsal.txt)\n",
55             app,app,app,app,app
56     );
57     return -1;
58 }
59
60 int main(int argc, char **argv) {
61     size_t    itr = 0;
62     char     *app = &argv[0][0];
63     FILE     *fpp = NULL;
64     lex_file *lex = NULL;
65
66     /*
67      * Parse all command line arguments.  This is rather annoying to do
68      * because of all tiny corner cases.
69      */
70     if (argc <= 1 || (argv[1][0] != '-'))
71         return usage(app);
72
73     while ((argc > 1) && argv[1][0] == '-') {
74         switch (argv[1][1]) {
75             case 'v': {
76                 printf("GMQCC:\n"
77                        "    version:    %d.%d.%d (0x%08X)\n"
78                        "    build date: %s\n"
79                        "    build time: %s\n",
80                     (GMQCC_VERSION >> 16) & 0xFF,
81                     (GMQCC_VERSION >>  8) & 0xFF,
82                     (GMQCC_VERSION >>  0) & 0xFF,
83                     (GMQCC_VERSION),
84                     __DATE__,
85                     __TIME__
86                 );
87                 return 0;
88             }
89             #define param_argument(argtype) do {                             \
90                 if (argv[1][2]) {                                            \
91                     items_add((argitem){util_strdup(&argv[1][2]), argtype}); \
92                 } else {                                                     \
93                     ++argv;                                                  \
94                     --argc;                                                  \
95                     if (argc <= 1)                                           \
96                         goto clean_params_usage;                             \
97                     items_add((argitem){util_strdup(&argv[1][0]), argtype}); \
98                 }                                                            \
99             } while (0)
100
101             case 'c': param_argument(0); break; /* compile  */
102             case 'a': param_argument(1); break; /* assemble */
103             case 'i': param_argument(2); break; /* includes */
104             #undef parm_argument
105             default:
106                 if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = true; break; }
107                 if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; }
108                 if (!strncmp(&argv[1][1], "help",   4)) {
109                     return usage(app);
110                     break;
111                 }
112                 /* compiler type selection */
113                 if (!strncmp(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
114                 if (!strncmp(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
115                 if (!strncmp(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
116                 if (!strncmp(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
117                 if (!strncmp(&argv[1][1], "std=",       4 )) {
118                     printf("invalid std selection, supported types:\n"
119                            "    -std=qcc     -- original QuakeC\n"
120                            "    -std=ftqecc  -- fteqcc QuakeC\n"
121                            "    -std=qccx    -- qccx QuakeC\n"
122                            "    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
123                     return 0;
124                 }
125
126                 /* code specific switches */
127                 if (!strcmp(&argv[1][1], "fdarkplaces-stringtablebug")) {
128                     opts_darkplaces_stringtablebug = true;
129                     break;
130                 }
131                 if (!strcmp(&argv[1][1], "fomit-nullcode")) {
132                     opts_omit_nullcode = true;
133                     break;
134                 }
135                 return usage(app);
136                 
137         }
138         ++argv;
139         --argc;
140     }
141
142     /*
143      * options could depend on another option, this is where option
144      * validity checking like that would take place.
145      */
146     if (opts_memchk && !opts_debug) 
147         printf("Warning: cannot enable -memchk, without -debug.\n");
148
149     util_debug("COM", "starting ...\n");
150     /* multi file multi path compilation system */
151     for (; itr < items_elements; itr++) {
152         switch (items_data[itr].type) {
153             case 0:
154                 lex_init (items_data[itr].name, &lex);
155                 lex_parse(lex);
156                 lex_close(lex);
157                 break;
158             case 1:
159                 asm_init (items_data[itr].name, &fpp);
160                 asm_parse(fpp);
161                 asm_close(fpp);
162                 break;
163         }
164     }
165
166     util_debug("COM", "cleaning ...\n"); 
167     /* clean list */
168     for (itr = 0; itr < items_elements; itr++)
169         mem_d(items_data[itr].name);
170     mem_d(items_data);
171     
172     util_meminfo();
173     return 0;
174 clean_params_usage:
175     for (itr = 0; itr < items_elements; itr++)
176         mem_d(items_data[itr].name);
177     mem_d(items_data);
178     return usage(app);
179 }