]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
moving opts_warn and opts_werror to con.c
[xonotic/gmqcc.git] / main.c
1 /*
2  * Copyright (C) 2012
3  *     Dale Weiler
4  *     Wolfgang Bumiller
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy of
7  * this software and associated documentation files (the "Software"), to deal in
8  * the Software without restriction, including without limitation the rights to
9  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is furnished to do
11  * so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "gmqcc.h"
25 #include "lexer.h"
26
27 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
28
29 uint32_t    opts_O        = 1;
30 const char *opts_output   = "progs.dat";
31 int         opts_standard = COMPILER_GMQCC;
32 bool        opts_debug    = false;
33 bool        opts_memchk   = false;
34 bool        opts_dumpfin  = false;
35 bool        opts_dump     = false;
36 bool        opts_forcecrc = false;
37 bool        opts_pp_only  = false;
38 size_t      opts_max_array_size = 1024;
39
40 uint16_t    opts_forced_crc;
41
42 static bool opts_output_wasset = false;
43
44 /* set by the standard */
45 const oper_info *operators      = NULL;
46 size_t           operator_count = 0;
47
48 typedef struct { char *filename; int type; } argitem;
49 static argitem *items = NULL;
50
51 #define TYPE_QC  0
52 #define TYPE_ASM 1
53 #define TYPE_SRC 2
54
55 static const char *app_name;
56
57 static int usage() {
58     con_out("usage: %s [options] [files...]", app_name);
59     con_out("options:\n"
60             "  -h, --help             show this help message\n"
61             "  -debug                 turns on compiler debug messages\n"
62             "  -memchk                turns on compiler memory leak check\n");
63     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
64             "  -a filename            add an asm file to be assembled\n"
65             "  -s filename            add a progs.src file to be used\n");
66     con_out("  -E                     stop after preprocessing\n");
67     con_out("  -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     con_out("  -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     con_out("  -force-crc=num         force a specific checksum into the header\n");
78     con_out("\n");
79     con_out("flags:\n"
80             "  -fadjust-vector-fields\n"
81             "            when assigning a vector field, its _y and _z fields also get assigned\n"
82            );
83     return -1;
84 }
85
86 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
87     size_t i;
88
89     for (i = 0; i < listsize; ++i) {
90         if (!strcmp(name, list[i].name)) {
91             longbit lb = list[i].bit;
92 #if 0
93             if (on)
94                 flags[lb.idx] |= (1<<(lb.bit));
95             else
96                 flags[lb.idx] &= ~(1<<(lb.bit));
97 #else
98             if (on)
99                 flags[0] |= (1<<lb);
100             else
101                 flags[0] &= ~(1<<(lb));
102 #endif
103             return true;
104         }
105     }
106     return false;
107 }
108 static bool options_setflag(const char *name, bool on) {
109     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
110 }
111 static bool options_setwarn(const char *name, bool on) {
112     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
113 }
114
115 static bool options_witharg(int *argc_, char ***argv_, char **out) {
116     int  argc   = *argc_;
117     char **argv = *argv_;
118
119     if (argv[0][2]) {
120         *out = argv[0]+2;
121         return true;
122     }
123     /* eat up the next */
124     if (argc < 2) /* no parameter was provided */
125         return false;
126
127     *out = argv[1];
128     --*argc_;
129     ++*argv_;
130     return true;
131 }
132
133 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
134     int  argc   = *argc_;
135     char **argv = *argv_;
136
137     size_t len = strlen(optname);
138
139     if (strncmp(argv[0]+ds, optname, len))
140         return false;
141
142     /* it's --optname, check how the parameter is supplied */
143     if (argv[0][ds+len] == '=') {
144         /* using --opt=param */
145         *out = argv[0]+ds+len+1;
146         return true;
147     }
148
149     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
150         return false;
151
152     /* using --opt param */
153     *out = argv[1];
154     --*argc_;
155     ++*argv_;
156     return true;
157 }
158 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
159     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
160 }
161 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
162     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
163 }
164
165 static void options_set(uint32_t *flags, size_t idx, bool on)
166 {
167     longbit lb = LONGBIT(idx);
168 #if 0
169     if (on)
170         flags[lb.idx] |= (1<<(lb.bit));
171     else
172         flags[lb.idx] &= ~(1<<(lb.bit));
173 #else
174     if (on)
175         flags[0] |= (1<<(lb));
176     else
177         flags[0] &= ~(1<<(lb));
178 #endif
179 }
180
181 static bool options_parse(int argc, char **argv) {
182     bool argend = false;
183     size_t itr;
184     char  buffer[1024];
185     char *redirout = (char*)stdout;
186     char *redirerr = (char*)stderr;
187
188     while (!argend && argc > 1) {
189         char *argarg;
190         argitem item;
191
192         ++argv;
193         --argc;
194
195         if (argv[0][0] == '-') {
196     /* All gcc-type long options */
197             if (options_long_gcc("std", &argc, &argv, &argarg)) {
198                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
199                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
200                     opts_standard = COMPILER_GMQCC;
201                 } else if (!strcmp(argarg, "qcc")) {
202                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
203                     options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
204                     opts_standard = COMPILER_QCC;
205                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
206                     options_set(opts_flags, FTEPP,                true);
207                     options_set(opts_flags, TRANSLATABLE_STRINGS, true);
208                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
209                     options_set(opts_flags, ASSIGN_FUNCTION_TYPES, true);
210                     opts_standard = COMPILER_FTEQCC;
211                 } else if (!strcmp(argarg, "qccx")) {
212                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
213                     opts_standard = COMPILER_QCCX;
214                 } else {
215                     con_out("Unknown standard: %s\n", argarg);
216                     return false;
217                 }
218                 continue;
219             }
220             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
221                 opts_forcecrc = true;
222                 opts_forced_crc = strtol(argarg, NULL, 0);
223                 continue;
224             }
225             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
226                 con_change(redirout, redirerr);
227                 continue;
228             }
229             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
230                 con_change(redirout, redirerr);
231                 continue;
232             }
233
234             /* show defaults (like pathscale) */
235             if (!strcmp(argv[0]+1, "show-defaults")) {
236                 size_t itr;
237                 char   buffer[1024];
238                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
239                     if (!OPTS_FLAG(itr))
240                         continue;
241
242                     memset(buffer, 0, sizeof(buffer));
243                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
244
245                     con_out("-f%s ", buffer);
246                 }
247                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
248                     if (!OPTS_WARN(itr))
249                         continue;
250
251                     memset(buffer, 0, sizeof(buffer));
252                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
253                     con_out("-W%s ", buffer);
254                 }
255                 con_out("\n");
256                 exit(0);
257             }
258
259             if (!strcmp(argv[0]+1, "debug")) {
260                 opts_debug = true;
261                 continue;
262             }
263             if (!strcmp(argv[0]+1, "dump")) {
264                 opts_dump = true;
265                 continue;
266             }
267             if (!strcmp(argv[0]+1, "dumpfin")) {
268                 opts_dumpfin = true;
269                 continue;
270             }
271             if (!strcmp(argv[0]+1, "memchk")) {
272                 opts_memchk = true;
273                 continue;
274             }
275             if (!strcmp(argv[0]+1, "nocolor")) {
276                 con_color(0);
277                 continue;
278             }
279
280             switch (argv[0][1]) {
281                 /* -h, show usage but exit with 0 */
282                 case 'h':
283                     usage();
284                     exit(0);
285                     /* break; never reached because of exit(0) */
286
287                 case 'E':
288                     opts_pp_only = true;
289                     break;
290
291                 /* handle all -fflags */
292                 case 'f':
293                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
294                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
295                         con_out("Possible flags:\n");
296                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
297                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
298                             con_out(" -f%s\n", buffer);
299                         }
300                         exit(0);
301                     }
302                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
303                         if (!options_setflag(argv[0]+5, false)) {
304                             con_out("unknown flag: %s\n", argv[0]+2);
305                             return false;
306                         }
307                     }
308                     else if (!options_setflag(argv[0]+2, true)) {
309                         con_out("unknown flag: %s\n", argv[0]+2);
310                         return false;
311                     }
312                     break;
313                 case 'W':
314                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
315                     if (!strcmp(argv[0]+2, "HELP") || *(argv[0]+2) == '?') {
316                         con_out("Possible warnings:\n");
317                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
318                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
319                             con_out(" -W%s\n", buffer);
320                         }
321                         exit(0);
322                     }
323                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
324                         opts_werror = false;
325                         break;
326                     }
327                     else if (!strcmp(argv[0]+2, "ERROR")) {
328                         opts_werror = true;
329                         break;
330                     }
331                     else if (!strcmp(argv[0]+2, "NONE")) {
332                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
333                             opts_warn[itr] = 0;
334                         break;
335                     }
336                     else if (!strcmp(argv[0]+2, "ALL")) {
337                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
338                             opts_warn[itr] = 0xFFFFFFFFL;
339                         break;
340                     }
341                     if (!strncmp(argv[0]+2, "NO_", 3)) {
342                         if (!options_setwarn(argv[0]+5, false)) {
343                             con_out("unknown warning: %s\n", argv[0]+2);
344                             return false;
345                         }
346                     }
347                     else if (!options_setwarn(argv[0]+2, true)) {
348                         con_out("unknown warning: %s\n", argv[0]+2);
349                         return false;
350                     }
351                     break;
352
353                 case 'O':
354                     if (!options_witharg(&argc, &argv, &argarg)) {
355                         con_out("option -O requires a numerical argument\n");
356                         return false;
357                     }
358                     opts_O = atoi(argarg);
359                     break;
360
361                 case 'o':
362                     if (!options_witharg(&argc, &argv, &argarg)) {
363                         con_out("option -o requires an argument: the output file name\n");
364                         return false;
365                     }
366                     opts_output = argarg;
367                     opts_output_wasset = true;
368                     break;
369
370                 case 'a':
371                 case 's':
372                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
373                     if (!options_witharg(&argc, &argv, &argarg)) {
374                         con_out("option -a requires a filename %s\n",
375                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
376                         return false;
377                     }
378                     item.filename = argarg;
379                     vec_push(items, item);
380                     break;
381
382                 case '-':
383                     if (!argv[0][2]) {
384                         /* anything following -- is considered a non-option argument */
385                         argend = true;
386                         break;
387                     }
388             /* All long options without arguments */
389                     else if (!strcmp(argv[0]+2, "help")) {
390                         usage();
391                         exit(0);
392                     }
393                     else {
394             /* All long options with arguments */
395                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
396                             opts_output = argarg;
397                             opts_output_wasset = true;
398                         } else {
399                             con_out("Unknown parameter: %s\n", argv[0]);
400                             return false;
401                         }
402                     }
403                     break;
404
405                 default:
406                     con_out("Unknown parameter: %s\n", argv[0]);
407                     return false;
408             }
409         }
410         else
411         {
412             /* it's a QC filename */
413             item.filename = argv[0];
414             item.type     = TYPE_QC;
415             vec_push(items, item);
416         }
417     }
418     return true;
419 }
420
421 /* returns the line number, or -1 on error */
422 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
423     int    len;
424     char  *line;
425     char  *start;
426     char  *end;
427
428     line = *out;
429     len = util_getline(&line, alen, src);
430     if (len == -1)
431         return false;
432
433     /* start at first non-blank */
434     for (start = line; isspace(*start); ++start) {}
435     /* end at the first non-blank */
436     for (end = start;  *end && !isspace(*end);  ++end)   {}
437
438     *out = line;
439     /* move the actual filename to the beginning */
440     while (start != end) {
441         *line++ = *start++;
442     }
443     *line = 0;
444     return true;
445 }
446
447 int main(int argc, char **argv) {
448     size_t itr;
449     int retval = 0;
450     bool opts_output_free = false;
451     bool progs_src = false;
452     FILE *outfile = NULL;
453
454     app_name = argv[0];
455     con_init();
456
457     /* default options / warn flags */
458     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
459     options_set(opts_warn, WARN_EXTENSIONS, true);
460     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
461     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
462     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
463     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
464     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
465     options_set(opts_warn, WARN_VOID_VARIABLES, true);
466     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
467     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
468     options_set(opts_warn, WARN_FRAME_MACROS, true);
469     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
470     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
471     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
472     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
473     options_set(opts_warn, WARN_PREPROCESSOR, true);
474     options_set(opts_warn, WARN_MULTIFILE_IF, true);
475     options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
476     options_set(opts_warn, WARN_CONST_VAR, true);
477     options_set(opts_warn, WARN_MULTIBYTE_CHARACTER, true);
478
479     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
480     options_set(opts_flags, FTEPP, false);
481
482     if (!options_parse(argc, argv)) {
483         return usage();
484     }
485
486     /* the standard decides which set of operators to use */
487     if (opts_standard == COMPILER_GMQCC) {
488         operators = c_operators;
489         operator_count = c_operator_count;
490     } else if (opts_standard == COMPILER_FTEQCC) {
491         operators = fte_operators;
492         operator_count = fte_operator_count;
493     } else {
494         operators = qcc_operators;
495         operator_count = qcc_operator_count;
496     }
497
498     if (opts_dump) {
499         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
500             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
501         }
502         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
503             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
504         }
505         con_out("output = %s\n", opts_output);
506         con_out("optimization level = %i\n", (int)opts_O);
507         con_out("standard = %i\n", opts_standard);
508     }
509
510     if (opts_pp_only) {
511         if (opts_output_wasset) {
512             outfile = util_fopen(opts_output, "wb");
513             if (!outfile) {
514                 con_err("failed to open `%s` for writing\n", opts_output);
515                 retval = 1;
516                 goto cleanup;
517             }
518         }
519         else
520             outfile = stdout;
521     }
522
523     if (!opts_pp_only) {
524         if (!parser_init()) {
525             con_err("failed to initialize parser\n");
526             retval = 1;
527             goto cleanup;
528         }
529     }
530     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
531         if (!ftepp_init()) {
532             con_err("failed to initialize parser\n");
533             retval = 1;
534             goto cleanup;
535         }
536     }
537
538     util_debug("COM", "starting ...\n");
539
540     if (!vec_size(items)) {
541         FILE *src;
542         char *line;
543         size_t linelen = 0;
544
545         progs_src = true;
546
547         src = util_fopen("progs.src", "rb");
548         if (!src) {
549             con_err("failed to open `progs.src` for reading\n");
550             retval = 1;
551             goto cleanup;
552         }
553
554         line = NULL;
555         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
556             con_err("illformatted progs.src file: expected output filename in first line\n");
557             retval = 1;
558             goto srcdone;
559         }
560
561         if (!opts_output_wasset) {
562             opts_output = util_strdup(line);
563             opts_output_free = true;
564         }
565
566         while (progs_nextline(&line, &linelen, src)) {
567             argitem item;
568             if (!line[0] || (line[0] == '/' && line[1] == '/'))
569                 continue;
570             item.filename = util_strdup(line);
571             item.type     = TYPE_QC;
572             vec_push(items, item);
573         }
574
575 srcdone:
576         fclose(src);
577         mem_d(line);
578     }
579
580     if (retval)
581         goto cleanup;
582
583     if (vec_size(items)) {
584         if (!opts_pp_only) {
585             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
586             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
587         }
588         for (itr = 0; itr < vec_size(items); ++itr) {
589             if (!opts_pp_only) {
590                 con_out("  item: %s (%s)\n",
591                        items[itr].filename,
592                        ( (items[itr].type == TYPE_QC ? "qc" :
593                          (items[itr].type == TYPE_ASM ? "asm" :
594                          (items[itr].type == TYPE_SRC ? "progs.src" :
595                          ("unknown"))))));
596             }
597
598             if (opts_pp_only) {
599                 const char *out;
600                 if (!ftepp_preprocess_file(items[itr].filename)) {
601                     retval = 1;
602                     goto cleanup;
603                 }
604                 out = ftepp_get();
605                 if (out)
606                     fprintf(outfile, "%s", out);
607                 ftepp_flush();
608             }
609             else {
610                 if (OPTS_FLAG(FTEPP)) {
611                     const char *data;
612                     if (!ftepp_preprocess_file(items[itr].filename)) {
613                         retval = 1;
614                         goto cleanup;
615                     }
616                     data = ftepp_get();
617                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
618                         retval = 1;
619                         goto cleanup;
620                     }
621                     ftepp_flush();
622                 }
623                 else {
624                     if (!parser_compile_file(items[itr].filename)) {
625                         retval = 1;
626                         goto cleanup;
627                     }
628                 }
629             }
630
631             if (progs_src) {
632                 mem_d(items[itr].filename);
633                 items[itr].filename = NULL;
634             }
635         }
636
637         ftepp_finish();
638         if (!opts_pp_only) {
639             if (!parser_finish(opts_output)) {
640                 retval = 1;
641                 goto cleanup;
642             }
643         }
644     }
645
646     /* stuff */
647
648 cleanup:
649     util_debug("COM", "cleaning ...\n");
650     ftepp_finish();
651     con_close();
652     vec_free(items);
653
654     if (!opts_pp_only)
655         parser_cleanup();
656     if (opts_output_free)
657         mem_d((char*)opts_output);
658
659     lex_cleanup();
660     util_meminfo();
661     return retval;
662 }