]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Add -Werror to the --help message
[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 #include "lexer.h"
25
26 #include <time.h>
27
28 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
29 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
30
31 uint32_t    opts_O        = 1;
32 const char *opts_output   = "progs.dat";
33 int         opts_standard = COMPILER_GMQCC;
34 bool        opts_debug    = false;
35 bool        opts_memchk   = false;
36 bool        opts_dump     = false;
37 bool        opts_werror   = false;
38 bool        opts_forcecrc = false;
39 bool        opts_benchmark = false;
40
41 uint16_t    opts_forced_crc;
42
43 static bool opts_output_wasset = false;
44
45 /* set by the standard */
46 const oper_info *operators      = NULL;
47 size_t           operator_count = 0;
48
49 typedef struct { char *filename; int type; } argitem;
50 VECTOR_MAKE(argitem, items);
51
52 #define TYPE_QC  0
53 #define TYPE_ASM 1
54 #define TYPE_SRC 2
55
56 static const char *app_name;
57
58 static int usage() {
59     printf("usage: %s [options] [files...]", app_name);
60     printf("options:\n"
61            "  -h, --help             show this help message\n"
62            "  -debug                 turns on compiler debug messages\n"
63            "  -memchk                turns on compiler memory leak check\n");
64     printf("  -o, --output=file      output file, defaults to progs.dat\n"
65            "  -a filename            add an asm file to be assembled\n"
66            "  -s filename            add a progs.src file to be used\n");
67     printf("  -f<flag>               enable a flag\n"
68            "  -fno-<flag>            disable a flag\n"
69            "  -std standard          select one of the following standards\n"
70            "       -std=qcc          original QuakeC\n"
71            "       -std=fteqcc       fteqcc QuakeC\n"
72            "       -std=gmqcc        this compiler (default)\n");
73     printf("  -W<warning>            enable a warning\n"
74            "  -Wno-<warning>         disable a warning\n"
75            "  -Wall                  enable all warnings\n");
76            "  -Werror                treat warnings as errors\n");
77     printf("  -force-crc=num         force a specific checksum into the header\n");
78     printf("\n");
79     printf("flags:\n"
80            "  -fdarkplaces-string-table-bug\n"
81            "            patch the string table to work with some bugged darkplaces versions\n"
82            "  -fomit-nullbytes\n"
83            "            omits certain null-bytes for a smaller output - requires a patched engine\n"
84            );
85     return -1;
86 }
87
88 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
89     size_t i;
90
91     for (i = 0; i < listsize; ++i) {
92         if (!strcmp(name, list[i].name)) {
93             longbit lb = list[i].bit;
94 #if 0
95             if (on)
96                 flags[lb.idx] |= (1<<(lb.bit));
97             else
98                 flags[lb.idx] &= ~(1<<(lb.bit));
99 #else
100             if (on)
101                 flags[0] |= (1<<lb);
102             else
103                 flags[0] &= ~(1<<(lb));
104 #endif
105             return true;
106         }
107     }
108     return false;
109 }
110 static bool options_setflag(const char *name, bool on) {
111     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
112 }
113 static bool options_setwarn(const char *name, bool on) {
114     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
115 }
116
117 static bool options_witharg(int *argc_, char ***argv_, char **out) {
118     int  argc   = *argc_;
119     char **argv = *argv_;
120
121     if (argv[0][2]) {
122         *out = argv[0]+2;
123         return true;
124     }
125     /* eat up the next */
126     if (argc < 2) /* no parameter was provided */
127         return false;
128
129     *out = argv[1];
130     --*argc_;
131     ++*argv_;
132     return true;
133 }
134
135 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
136     int  argc   = *argc_;
137     char **argv = *argv_;
138
139     size_t len = strlen(optname);
140
141     if (strncmp(argv[0]+ds, optname, len))
142         return false;
143
144     /* it's --optname, check how the parameter is supplied */
145     if (argv[0][ds+len] == '=') {
146         /* using --opt=param */
147         *out = argv[0]+ds+len+1;
148         return true;
149     }
150
151     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
152         return false;
153
154     /* using --opt param */
155     *out = argv[1];
156     --*argc_;
157     ++*argv_;
158     return true;
159 }
160 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
161     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
162 }
163 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
164     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
165 }
166
167 static bool options_parse(int argc, char **argv) {
168     bool argend = false;
169     size_t itr;
170     char buffer[1024];
171     while (!argend && argc > 1) {
172         char *argarg;
173         argitem item;
174
175         ++argv;
176         --argc;
177
178         if (argv[0][0] == '-') {
179     /* All gcc-type long options */
180             if (options_long_gcc("std", &argc, &argv, &argarg)) {
181                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
182                     opts_standard = COMPILER_GMQCC;
183                 else if (!strcmp(argarg, "qcc"))
184                     opts_standard = COMPILER_QCC;
185                 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
186                     opts_standard = COMPILER_FTEQCC;
187                 else if (!strcmp(argarg, "qccx"))
188                     opts_standard = COMPILER_QCCX;
189                 else {
190                     printf("Unknown standard: %s\n", argarg);
191                     return false;
192                 }
193                 continue;
194             }
195             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
196                 opts_forcecrc = true;
197                 opts_forced_crc = strtol(argarg, NULL, 0);
198                 continue;
199             }
200             if (!strcmp(argv[0]+1, "debug")) {
201                 opts_debug = true;
202                 continue;
203             }
204             if (!strcmp(argv[0]+1, "dump")) {
205                 opts_dump = true;
206                 continue;
207             }
208             if (!strcmp(argv[0]+1, "memchk")) {
209                 opts_memchk = true;
210                 continue;
211             }
212             if (!strcmp(argv[0]+1, "benchmark")) {
213                 opts_benchmark = true;
214                 continue;
215             }
216
217             switch (argv[0][1]) {
218                 /* -h, show usage but exit with 0 */
219                 case 'h':
220                     usage();
221                     exit(0);
222                     break;
223
224                 /* handle all -fflags */
225                 case 'f':
226                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
227                     if (!strcmp(argv[0]+2, "HELP")) {
228                         printf("Possible flags:\n");
229                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
230                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
231                             printf(" -f%s\n", buffer);
232                         }
233                         exit(0);
234                     }
235                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
236                         if (!options_setflag(argv[0]+5, false)) {
237                             printf("unknown flag: %s\n", argv[0]+2);
238                             return false;
239                         }
240                     }
241                     else if (!options_setflag(argv[0]+2, true)) {
242                         printf("unknown flag: %s\n", argv[0]+2);
243                         return false;
244                     }
245                     break;
246                 case 'W':
247                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
248                     if (!strcmp(argv[0]+2, "HELP")) {
249                         printf("Possible warnings:\n");
250                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
251                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
252                             printf(" -W%s\n", buffer);
253                         }
254                         exit(0);
255                     }
256                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
257                         opts_werror = false;
258                         break;
259                     }
260                     else if (!strcmp(argv[0]+2, "ERROR")) {
261                         opts_werror = true;
262                         break;
263                     }
264                     else if (!strcmp(argv[0]+2, "NONE")) {
265                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
266                             opts_warn[itr] = 0;
267                         break;
268                     }
269                     else if (!strcmp(argv[0]+2, "ALL")) {
270                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
271                             opts_warn[itr] = 0xFFFFFFFFL;
272                         break;
273                     }
274                     if (!strncmp(argv[0]+2, "NO_", 3)) {
275                         if (!options_setwarn(argv[0]+5, false)) {
276                             printf("unknown warning: %s\n", argv[0]+2);
277                             return false;
278                         }
279                     }
280                     else if (!options_setwarn(argv[0]+2, true)) {
281                         printf("unknown warning: %s\n", argv[0]+2);
282                         return false;
283                     }
284                     break;
285
286                 case 'O':
287                     if (!options_witharg(&argc, &argv, &argarg)) {
288                         printf("option -O requires a numerical argument\n");
289                         return false;
290                     }
291                     opts_O = atoi(argarg);
292                     break;
293
294                 case 'o':
295                     if (!options_witharg(&argc, &argv, &argarg)) {
296                         printf("option -o requires an argument: the output file name\n");
297                         return false;
298                     }
299                     opts_output = argarg;
300                     opts_output_wasset = true;
301                     break;
302
303                 case 'a':
304                 case 's':
305                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
306                     if (!options_witharg(&argc, &argv, &argarg)) {
307                         printf("option -a requires a filename %s\n",
308                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
309                         return false;
310                     }
311                     item.filename = argarg;
312                     items_add(item);
313                     break;
314
315                 case '-':
316                     if (!argv[0][2]) {
317                         /* anything following -- is considered a non-option argument */
318                         argend = true;
319                         break;
320                     }
321             /* All long options without arguments */
322                     else if (!strcmp(argv[0]+2, "help")) {
323                         usage();
324                         exit(0);
325                     }
326                     else {
327             /* All long options with arguments */
328                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
329                             opts_output = argarg;
330                             opts_output_wasset = true;
331                         } else {
332                             printf("Unknown parameter: %s\n", argv[0]);
333                             return false;
334                         }
335                     }
336                     break;
337
338                 default:
339                     printf("Unknown parameter: %s\n", argv[0]);
340                     return false;
341             }
342         }
343         else
344         {
345             /* it's a QC filename */
346             argitem item;
347             item.filename = argv[0];
348             item.type     = TYPE_QC;
349             items_add(item);
350         }
351     }
352     return true;
353 }
354
355 static void options_set(uint32_t *flags, size_t idx, bool on)
356 {
357     longbit lb = LONGBIT(idx);
358 #if 0
359     if (on)
360         flags[lb.idx] |= (1<<(lb.bit));
361     else
362         flags[lb.idx] &= ~(1<<(lb.bit));
363 #else
364     if (on)
365         flags[0] |= (1<<(lb));
366     else
367         flags[0] &= ~(1<<(lb));
368 #endif
369 }
370
371 /* returns the line number, or -1 on error */
372 static bool progs_nextline(char **out, size_t *alen,FILE *src)
373 {
374     int    len;
375     char  *line;
376     char  *start;
377     char  *end;
378
379     line = *out;
380     len = util_getline(&line, alen, src);
381     if (len == -1)
382         return false;
383
384     /* start at first non-blank */
385     for (start = line; isspace(*start); ++start) {}
386     /* end at the first non-blank */
387     for (end = start;  *end && !isspace(*end);  ++end)   {}
388
389     *out = line;
390     /* move the actual filename to the beginning */
391     while (start != end) {
392         *line++ = *start++;
393     }
394     *line = 0;
395     return true;
396 }
397
398 int main(int argc, char **argv) {
399     size_t itr;
400     int retval = 0;
401     bool opts_output_free = false;
402
403     struct timespec ta, tb, tc;
404
405     app_name = argv[0];
406
407     /* default options / warn flags */
408     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
409     options_set(opts_warn, WARN_EXTENSIONS, true);
410     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
411     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
412     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
413     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
414     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
415     options_set(opts_warn, WARN_VOID_VARIABLES, true);
416     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
417     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
418     options_set(opts_warn, WARN_FRAME_MACROS, true);
419     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
420     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
421
422     if (!options_parse(argc, argv)) {
423         return usage();
424     }
425
426     /* the standard decides which set of operators to use */
427     if (opts_standard == COMPILER_GMQCC) {
428         operators = c_operators;
429         operator_count = c_operator_count;
430     } else {
431         operators = qcc_operators;
432         operator_count = qcc_operator_count;
433     }
434
435     if (opts_dump) {
436         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
437             printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
438         }
439         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
440             printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
441         }
442         printf("output = %s\n", opts_output);
443         printf("optimization level = %i\n", (int)opts_O);
444         printf("standard = %i\n", opts_standard);
445     }
446
447     if (!parser_init()) {
448         printf("failed to initialize parser\n");
449         retval = 1;
450         goto cleanup;
451     }
452
453     util_debug("COM", "starting ...\n");
454
455     if (items_elements) {
456         printf("Mode: manual\n");
457         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
458         if (opts_benchmark)
459             clock_gettime(CLOCK_MONOTONIC, &ta);
460         for (itr = 0; itr < items_elements; ++itr) {
461             printf("  item: %s (%s)\n",
462                    items_data[itr].filename,
463                    ( (items_data[itr].type == TYPE_QC ? "qc" :
464                      (items_data[itr].type == TYPE_ASM ? "asm" :
465                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
466                      ("unknown"))))));
467
468             if (!parser_compile(items_data[itr].filename)) {
469                 retval = 1;
470                 goto cleanup;
471             }
472         }
473
474         if (opts_benchmark)
475             clock_gettime(CLOCK_MONOTONIC, &tb);
476         parser_finish(opts_output);
477         if (opts_benchmark)
478             clock_gettime(CLOCK_MONOTONIC, &tc);
479
480     } else {
481         FILE *src;
482         char *line;
483         size_t linelen = 0;
484
485         printf("Mode: progs.src\n");
486         src = util_fopen("progs.src", "rb");
487         if (!src) {
488             printf("failed to open `progs.src` for reading\n");
489             retval = 1;
490             goto cleanup;
491         }
492
493         line = NULL;
494         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
495             printf("illformatted progs.src file: expected output filename in first line\n");
496             retval = 1;
497             goto srcdone;
498         }
499
500         if (!opts_output_wasset) {
501             opts_output = util_strdup(line);
502             opts_output_free = true;
503         }
504
505         if (opts_benchmark)
506             clock_gettime(CLOCK_MONOTONIC, &ta);
507         while (progs_nextline(&line, &linelen, src)) {
508             if (!line[0] || (line[0] == '/' && line[1] == '/'))
509                 continue;
510             printf("  src: %s\n", line);
511             if (!parser_compile(line)) {
512                 retval = 1;
513                 goto srcdone;
514             }
515         }
516
517         if (opts_benchmark)
518             clock_gettime(CLOCK_MONOTONIC, &tb);
519         parser_finish(opts_output);
520         if (opts_benchmark)
521             clock_gettime(CLOCK_MONOTONIC, &tc);
522
523 srcdone:
524         fclose(src);
525         mem_d(line);
526     }
527     if (opts_benchmark)
528     {
529         printf("started parsing at: %lu:%09lu\n", (unsigned long)ta.tv_sec, (unsigned long)ta.tv_nsec);
530         printf("started codegen at: %lu:%09lu\n", (unsigned long)tb.tv_sec, (unsigned long)tb.tv_nsec);
531         printf("       finished at: %lu:%09lu\n", (unsigned long)tc.tv_sec, (unsigned long)tc.tv_nsec);
532         {
533             size_t sec = tb.tv_sec - ta.tv_sec;
534             size_t nsec = (tb.tv_nsec + sec * 1000000000L) - ta.tv_nsec;
535             printf("Parsing took %lu\n", (unsigned long)nsec);
536         }
537         {
538             size_t sec = tc.tv_sec - tb.tv_sec;
539             size_t nsec = (tc.tv_nsec + sec * 1000000000L) - tb.tv_nsec;
540             printf("Codegen took %lu\n", (unsigned long)nsec);
541         }
542     }
543
544     /* stuff */
545
546 cleanup:
547     util_debug("COM", "cleaning ...\n");
548
549     mem_d(items_data);
550
551     parser_cleanup();
552     if (opts_output_free)
553         mem_d((char*)opts_output);
554
555     lex_cleanup();
556     util_meminfo();
557     return retval;
558 }