]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
flags.def and warns.def containing defined flags and warnings
[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
25 uint32_t    opt_flags[1 + (COUNT_FLAGS / 32)];
26 uint32_t    opt_warn [1 + (COUNT_WARNINGS / 32)];
27
28 uint32_t    opt_O        = 1;
29 const char *opt_output   = "progs.dat";
30 int         opt_standard = STD_DEF;
31
32 typedef struct { char *filename; int type; } argitem;
33 VECTOR_MAKE(argitem, items);
34
35 #define TYPE_QC  0
36 #define TYPE_ASM 1
37 #define TYPE_SRC 2
38
39 static const char *app_name;
40
41 static int usage() {
42     printf("usage: %s [options] [files...]", app_name);
43     printf("options:\n"
44            "  -h, --help             show this help message\n"
45            "  -o, --output=file      output file, defaults to progs.dat\n"
46            "  -a filename            add an asm file to be assembled\n"
47            "  -s filename            add a progs.src file to be used\n");
48     printf("  -f<flag>               enable a flag\n"
49            "  -fno-<flag>            disable a flag\n"
50            "  -std standard          select one of the following standards\n"
51            "       -std=qcc          original QuakeC\n"
52            "       -std=fteqcc       fteqcc QuakeC\n"
53            "       -std=gmqcc        this compiler (default)\n");
54     printf("  -W<warning>            enable a warning\n"
55            "  -Wno-<warning>         disable a warning\n"
56            "  -Wall                  enable all warnings\n");
57     printf("\n");
58     printf("flags:\n"
59            "  -fdarkplaces-string-table-bug\n"
60            "            patch the string table to work with some bugged darkplaces versions\n"
61            "  -fomit-nullbytes\n"
62            "            omits certain null-bytes for a smaller output - requires a patched engine\n"
63            );
64     return -1;
65 }
66
67 static void strtocmd(char *str)
68 {
69     for(; *str; ++str) {
70         if (*str == '-') {
71             *str = '_';
72             continue;
73         }
74         if (isalpha(*str) && !isupper(*str)) {
75             *str += 'A' - 'a';
76             continue;
77         }
78     }
79 }
80
81 static void strtononcmd(char *buf, const char *str)
82 {
83     for(; *str; ++buf, ++str) {
84         if (*str == '_') {
85             *buf = '-';
86             continue;
87         }
88         if (isalpha(*str) && isupper(*str)) {
89             *buf = *str + 'a' - 'A';
90             continue;
91         }
92         *buf = *str;
93     }
94     *buf = 0;
95 }
96
97 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opt_flag_def *list, size_t listsize) {
98     size_t i;
99
100     for (i = 0; i < listsize; ++i) {
101         if (!strcmp(name, list[i].name)) {
102             longbit lb = list[i].bit;
103 #if 0
104             if (on)
105                 flags[lb.idx] |= (1<<(lb.bit));
106             else
107                 flags[lb.idx] &= ~(1<<(lb.bit));
108 #else
109             if (on)
110                 flags[0] |= (1<<lb);
111             else
112                 flags[0] &= ~(1<<(lb));
113 #endif
114             return true;
115         }
116     }
117     return false;
118 }
119 static bool options_setflag(const char *name, bool on) {
120     return options_setflag_all(name, on, opt_flags, opt_flag_list, COUNT_FLAGS);
121 }
122 static bool options_setwarn(const char *name, bool on) {
123     return options_setflag_all(name, on, opt_warn, opt_warn_list, COUNT_WARNINGS);
124 }
125
126 static bool options_witharg(int *argc_, char ***argv_, char **out) {
127     int  argc   = *argc_;
128     char **argv = *argv_;
129
130     if (argv[0][2]) {
131         *out = argv[0]+2;
132         return true;
133     }
134     /* eat up the next */
135     if (argc < 2) /* no parameter was provided */
136         return false;
137
138     *out = argv[1];
139     --*argc_;
140     ++*argv_;
141     return true;
142 }
143
144 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
145     int  argc   = *argc_;
146     char **argv = *argv_;
147
148     size_t len = strlen(optname);
149
150     if (strncmp(argv[0]+ds, optname, len))
151         return false;
152
153     /* it's --optname, check how the parameter is supplied */
154     if (argv[0][ds+len] == '=') {
155         /* using --opt=param */
156         *out = argv[0]+ds+len+1;
157         return true;
158     }
159
160     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
161         return false;
162
163     /* using --opt param */
164     *out = argv[1];
165     --*argc_;
166     ++*argv_;
167     return true;
168 }
169 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
170     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
171 }
172 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
173     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
174 }
175
176 static bool options_parse(int argc, char **argv) {
177     bool argend = false;
178     size_t itr;
179     char buffer[1024];
180     while (!argend && argc > 1) {
181         char *argarg;
182         argitem item;
183
184         ++argv;
185         --argc;
186
187         if (argv[0][0] == '-') {
188     /* All gcc-type long options */
189             if (options_long_gcc("std", &argc, &argv, &argarg)) {
190                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
191                     opt_standard = STD_DEF;
192                 else if (!strcmp(argarg, "qcc"))
193                     opt_standard = STD_QCC;
194                 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
195                     opt_standard = STD_FTE;
196                 else {
197                     printf("Unknown standard: %s\n", argarg);
198                     return false;
199                 }
200                 continue;
201             }
202
203             switch (argv[0][1]) {
204                 /* -h, show usage but exit with 0 */
205                 case 'h':
206                     usage();
207                     exit(0);
208                     break;
209
210                 /* handle all -fflags */
211                 case 'f':
212                     strtocmd(argv[0]+2);
213                     if (!strcmp(argv[0]+2, "HELP")) {
214                         printf("Possible flags:\n");
215                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
216                             strtononcmd(buffer, opt_flag_list[itr].name);
217                             printf(" -f%s\n", buffer);
218                         }
219                         exit(0);
220                     }
221                     else if (!strncmp(argv[0]+2, "NO-", 3)) {
222                         if (!options_setflag(argv[0]+5, false)) {
223                             printf("unknown flag: %s\n", argv[0]+2);
224                             return false;
225                         }
226                     }
227                     else if (!options_setflag(argv[0]+2, true)) {
228                         printf("unknown flag: %s\n", argv[0]+2);
229                         return false;
230                     }
231                     break;
232                 case 'W':
233                     strtocmd(argv[0]+2);
234                     if (!strcmp(argv[0]+2, "HELP")) {
235                         printf("Possible warnings:\n");
236                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
237                             strtononcmd(buffer, opt_warn_list[itr].name);
238                             printf(" -W%s\n", buffer);
239                         }
240                         exit(0);
241                     }
242                     else if (!strcmp(argv[0]+2, "all")) {
243                         for (itr = 0; itr < sizeof(opt_warn)/sizeof(opt_warn[0]); ++itr)
244                             opt_warn[itr] = 0xFFFFFFFFL;
245                         break;
246                     }
247                     if (!strncmp(argv[0]+2, "no-", 3)) {
248                         if (!options_setwarn(argv[0]+5, false)) {
249                             printf("unknown warning: %s\n", argv[0]+2);
250                             return false;
251                         }
252                     }
253                     else if (!options_setwarn(argv[0]+2, true)) {
254                         printf("unknown warning: %s\n", argv[0]+2);
255                         return false;
256                     }
257                     break;
258
259                 case 'O':
260                     if (!options_witharg(&argc, &argv, &argarg)) {
261                         printf("option -O requires a numerical argument\n");
262                         return false;
263                     }
264                     opt_O = atoi(argarg);
265                     break;
266
267                 case 'o':
268                     if (!options_witharg(&argc, &argv, &argarg)) {
269                         printf("option -o requires an argument: the output file name\n");
270                         return false;
271                     }
272                     opt_output = argarg;
273                     break;
274
275                 case 'a':
276                 case 's':
277                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
278                     if (!options_witharg(&argc, &argv, &argarg)) {
279                         printf("option -a requires a filename %s\n",
280                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
281                         return false;
282                     }
283                     item.filename = argarg;
284                     items_add(item);
285                     break;
286
287                 case '-':
288                     if (!argv[0][2]) {
289                         /* anything following -- is considered a non-option argument */
290                         argend = true;
291                         break;
292                     }
293             /* All long options without arguments */
294                     else if (!strcmp(argv[0]+2, "help")) {
295                         usage();
296                         exit(0);
297                     }
298                     else {
299             /* All long options with arguments */
300                         if (options_long_witharg("output", &argc, &argv, &argarg))
301                             opt_output = argarg;
302                         else
303                         {
304                             printf("Unknown parameter: %s\n", argv[0]);
305                             return false;
306                         }
307                     }
308                     break;
309
310                 default:
311                     printf("Unknown parameter: %s\n", argv[0]);
312                     return false;
313             }
314         }
315         else
316         {
317             /* it's a QC filename */
318             argitem item;
319             item.filename = argv[0];
320             item.type     = TYPE_QC;
321             items_add(item);
322         }
323     }
324     return true;
325 }
326
327 int main(int argc, char **argv) {
328     size_t itr;
329     app_name = argv[0];
330
331     if (!options_parse(argc, argv)) {
332         return usage();
333     }
334
335     for (itr = 0; itr < COUNT_FLAGS; ++itr) {
336         printf("Flag %s = %i\n", opt_flag_list[itr].name, OPT_FLAG(itr));
337     }
338     for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
339         printf("Warning %s = %i\n", opt_warn_list[itr].name, OPT_WARN(itr));
340     }
341     printf("output = %s\n", opt_output);
342     printf("optimization level = %i\n", (int)opt_O);
343     printf("standard = %i\n", opt_standard);
344
345     if (items_elements) {
346         printf("Mode: manual\n");
347         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
348         for (itr = 0; itr < items_elements; ++itr) {
349             printf("  item: %s (%s)\n",
350                    items_data[itr].filename,
351                    ( (items_data[itr].type == TYPE_QC ? "qc" :
352                      (items_data[itr].type == TYPE_ASM ? "asm" :
353                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
354                      ("unknown"))))));
355         }
356     } else {
357         printf("Mode: progs.src\n");
358     }
359
360     util_debug("COM", "starting ...\n");
361
362     /* stuff */
363
364     util_debug("COM", "cleaning ...\n");
365
366     util_meminfo();
367     return 0;
368 }