]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
util_strncmpexact
[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 typedef struct { char *name, type; } argitem;
25 VECTOR_MAKE(argitem, items);
26
27 static const int usage(const char *const app) {
28     printf("usage:\n"
29            "    %s -c<file>          -oprog.dat -- compile file\n"
30            "    %s -a<file>          -oprog.dat -- assemble file\n"
31            "    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n"
32            "    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n"
33            "    example:\n"
34            "    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n", app, app, app, app, app);
35
36     printf("    additional flags:\n"
37            "        -debug           -- turns on compiler debug messages\n"
38            "        -memchk          -- turns on compiler memory leak check\n"
39            "        -help            -- prints this help/usage text\n"
40            "        -std             -- select the QuakeC compile type (types below):\n");
41
42     printf("            -std=qcc     -- original QuakeC\n"
43            "            -std=ftqecc  -- fteqcc QuakeC\n"
44            "            -std=qccx    -- qccx QuakeC\n"
45            "            -std=gmqcc   -- this compiler QuakeC (default selection)\n");
46
47     printf("    codegen flags:\n"
48            "        -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n"
49            "        -fomit-nullcode               -- omits the generation of null code (will break everywhere see propsal.txt)\n");
50     return -1;
51 }
52
53 int main(int argc, char **argv) {
54     size_t    itr = 0;
55     char     *app = &argv[0][0];
56     FILE     *fpp = NULL;
57     lex_file *lex = NULL;
58
59     /*
60      * Parse all command line arguments.  This is rather annoying to do
61      * because of all tiny corner cases.
62      */
63     if (argc <= 1 || (argv[1][0] != '-'))
64         return usage(app);
65
66     while ((argc > 1) && argv[1][0] == '-') {
67         switch (argv[1][1]) {
68             case 'v': {
69                 printf("GMQCC:\n"
70                        "    version:    %d.%d.%d (0x%08X)\n"
71                        "    build date: %s\n"
72                        "    build time: %s\n",
73                     (GMQCC_VERSION >> 16) & 0xFF,
74                     (GMQCC_VERSION >>  8) & 0xFF,
75                     (GMQCC_VERSION >>  0) & 0xFF,
76                     (GMQCC_VERSION),
77                     __DATE__,
78                     __TIME__
79                 );
80                 return 0;
81             }
82             #define param_argument(argtype) do {                             \
83                 argitem item;                                                \
84                 if (argv[1][2]) {                                            \
85                     item.name = util_strdup(&argv[1][2]);                    \
86                     item.type = argtype;                                     \
87                     items_add(item);                                         \
88                 } else {                                                     \
89                     ++argv;                                                  \
90                     --argc;                                                  \
91                     if (argc <= 1)                                           \
92                         goto clean_params_usage;                             \
93                     item.name = util_strdup(argv[1]);                        \
94                     item.type = argtype;                                     \
95                     items_add(item);                                         \
96                 }                                                            \
97             } while (0)
98
99             case 'c': { param_argument(0); break; } /* compile  */
100             case 'a': { param_argument(1); break; } /* assemble */
101             case 'i': { param_argument(2); break; } /* includes */
102             #undef parm_argument
103             default:
104                 if (util_strncmpexact(&argv[1][1], "debug" , 5)) { opts_debug  = true; break; }
105                 if (util_strncmpexact(&argv[1][1], "memchk", 6)) { opts_memchk = true; break; }
106                 if (util_strncmpexact(&argv[1][1], "help",   4)) {
107                     return usage(app);
108                     break;
109                 }
110                 /* compiler type selection */
111                 if (util_strncmpexact(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
112                 if (util_strncmpexact(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
113                 if (util_strncmpexact(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
114                 if (util_strncmpexact(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
115                 if (util_strncmpexact(&argv[1][1], "std=",       4 )) {
116                     printf("invalid std selection, supported types:\n"
117                            "    -std=qcc     -- original QuakeC\n"
118                            "    -std=ftqecc  -- fteqcc QuakeC\n"
119                            "    -std=qccx    -- qccx QuakeC\n"
120                            "    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
121                     return 0;
122                 }
123
124                 /* code specific switches */
125                 if (util_strncmpexact(&argv[1][1], "fdarkplaces-stringtablebug", 26)) {
126                     opts_darkplaces_stringtablebug = true;
127                     break;
128                 }
129                 if (util_strncmpexact(&argv[1][1], "fomit-nullcode", 14)) {
130                     opts_omit_nullcode = true;
131                     break;
132                 }
133                 return printf("invalid command line argument: %s\n", argv[1]);
134
135         }
136         ++argv;
137         --argc;
138     }
139     /*
140      * options could depend on another option, this is where option
141      * validity checking like that would take place.
142      */
143     if (opts_memchk && !opts_debug)
144         printf("Warning: cannot enable -memchk, without -debug.\n");
145     
146     util_debug("COM", "starting ...\n");
147     /* multi file multi path compilation system */
148     for (; itr < items_elements; itr++) {
149         switch (items_data[itr].type) {
150             case 0:
151                 lex_init (items_data[itr].name, &lex);
152                 if (lex) {
153                     lex_parse(lex);
154                     lex_close(lex);
155                 }
156                 break;
157             case 1:
158                 asm_init (items_data[itr].name, &fpp);
159                 if (fpp) {
160                     asm_parse(fpp);
161                     asm_close(fpp);
162                 }
163                 break;
164         }
165     }
166
167     util_debug("COM", "cleaning ...\n");
168     /* clean list */
169     for (itr = 0; itr < items_elements; itr++)
170         mem_d(items_data[itr].name);
171     mem_d(items_data);
172
173     util_meminfo();
174     return 0;
175
176 clean_params_usage:
177     for (itr = 0; itr < items_elements; itr++)
178         mem_d(items_data[itr].name);
179     mem_d(items_data);
180     return usage(app);
181 }