]> de.git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Implemented -show-defaults
[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 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
29
30 uint32_t    opts_O        = 1;
31 const char *opts_output   = "progs.dat";
32 int         opts_standard = COMPILER_GMQCC;
33 bool        opts_debug    = false;
34 bool        opts_memchk   = false;
35 bool        opts_dumpfin  = false;
36 bool        opts_dump     = false;
37 bool        opts_werror   = false;
38 bool        opts_forcecrc = false;
39 bool        opts_pp_only  = false;
40 size_t      opts_max_array_size = 1024;
41
42 uint16_t    opts_forced_crc;
43
44 static bool opts_output_wasset = false;
45
46 /* set by the standard */
47 const oper_info *operators      = NULL;
48 size_t           operator_count = 0;
49
50 typedef struct { char *filename; int type; } argitem;
51 static argitem *items = NULL;
52
53 #define TYPE_QC  0
54 #define TYPE_ASM 1
55 #define TYPE_SRC 2
56
57 static const char *app_name;
58
59 static int usage() {
60     con_out("usage: %s [options] [files...]", app_name);
61     con_out("options:\n"
62             "  -h, --help             show this help message\n"
63             "  -debug                 turns on compiler debug messages\n"
64             "  -memchk                turns on compiler memory leak check\n");
65     con_out("  -o, --output=file      output file, defaults to progs.dat\n"
66             "  -a filename            add an asm file to be assembled\n"
67             "  -s filename            add a progs.src file to be used\n");
68     con_out("  -E                     stop after preprocessing\n");
69     con_out("  -f<flag>               enable a flag\n"
70             "  -fno-<flag>            disable a flag\n"
71             "  -std standard          select one of the following standards\n"
72             "       -std=qcc          original QuakeC\n"
73             "       -std=fteqcc       fteqcc QuakeC\n"
74             "       -std=gmqcc        this compiler (default)\n");
75     con_out("  -W<warning>            enable a warning\n"
76             "  -Wno-<warning>         disable a warning\n"
77             "  -Wall                  enable all warnings\n"
78             "  -Werror                treat warnings as errors\n");
79     con_out("  -force-crc=num         force a specific checksum into the header\n");
80     con_out("\n");
81     con_out("flags:\n"
82             "  -fadjust-vector-fields\n"
83             "            when assigning a vector field, its _y and _z fields also get assigned\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 void options_set(uint32_t *flags, size_t idx, bool on)
168 {
169     longbit lb = LONGBIT(idx);
170 #if 0
171     if (on)
172         flags[lb.idx] |= (1<<(lb.bit));
173     else
174         flags[lb.idx] &= ~(1<<(lb.bit));
175 #else
176     if (on)
177         flags[0] |= (1<<(lb));
178     else
179         flags[0] &= ~(1<<(lb));
180 #endif
181 }
182
183 static bool options_parse(int argc, char **argv) {
184     bool argend = false;
185     size_t itr;
186     char  buffer[1024];
187     char *redirout = (char*)stdout;
188     char *redirerr = (char*)stderr;
189
190     while (!argend && argc > 1) {
191         char *argarg;
192         argitem item;
193
194         ++argv;
195         --argc;
196
197         if (argv[0][0] == '-') {
198     /* All gcc-type long options */
199             if (options_long_gcc("std", &argc, &argv, &argarg)) {
200                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
201                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
202                     opts_standard = COMPILER_GMQCC;
203                 } else if (!strcmp(argarg, "qcc")) {
204                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
205                     opts_standard = COMPILER_QCC;
206                 } else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc")) {
207                     options_set(opts_flags, FTEPP,                true);
208                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
209                     opts_standard = COMPILER_FTEQCC;
210                 } else if (!strcmp(argarg, "qccx")) {
211                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
212                     opts_standard = COMPILER_QCCX;
213                 } else {
214                     con_out("Unknown standard: %s\n", argarg);
215                     return false;
216                 }
217                 continue;
218             }
219             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
220                 opts_forcecrc = true;
221                 opts_forced_crc = strtol(argarg, NULL, 0);
222                 continue;
223             }
224             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
225                 con_change(redirout, redirerr);
226                 continue;
227             }
228             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
229                 con_change(redirout, redirerr);
230                 continue;
231             }
232             
233             /* show defaults (like pathscale) */
234             if (!strcmp(argv[0]+1, "show-defaults")) {
235                 size_t itr;
236                 char   buffer[1024];
237                 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
238                     if (!OPTS_FLAG(itr))
239                         continue;
240                         
241                     memset(buffer, 0, sizeof(buffer));
242                     util_strtononcmd(opts_flag_list[itr].name, buffer, strlen(opts_flag_list[itr].name) + 1);
243     
244                     con_out("-f%s ", buffer);
245                 }
246                 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
247                     if (!OPTS_WARN(itr))
248                         continue;
249                     
250                     memset(buffer, 0, sizeof(buffer));
251                     util_strtononcmd(opts_warn_list[itr].name, buffer, strlen(opts_warn_list[itr].name) + 1);
252                     con_out("-W%s ", buffer);
253                 }
254                 con_out("\n");
255                 exit(0);
256             }
257
258             if (!strcmp(argv[0]+1, "debug")) {
259                 opts_debug = true;
260                 continue;
261             }
262             if (!strcmp(argv[0]+1, "dump")) {
263                 opts_dump = true;
264                 continue;
265             }
266             if (!strcmp(argv[0]+1, "dumpfin")) {
267                 opts_dumpfin = true;
268                 continue;
269             }
270             if (!strcmp(argv[0]+1, "memchk")) {
271                 opts_memchk = true;
272                 continue;
273             }
274             if (!strcmp(argv[0]+1, "nocolor")) {
275                 con_color(0);
276                 continue;
277             }
278
279             switch (argv[0][1]) {
280                 /* -h, show usage but exit with 0 */
281                 case 'h':
282                     usage();
283                     exit(0);
284                     /* break; never reached because of exit(0) */
285
286                 case 'E':
287                     opts_pp_only = true;
288                     break;
289
290                 /* handle all -fflags */
291                 case 'f':
292                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
293                     if (!strcmp(argv[0]+2, "HELP")) {
294                         con_out("Possible flags:\n");
295                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
296                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
297                             con_out(" -f%s\n", buffer);
298                         }
299                         exit(0);
300                     }
301                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
302                         if (!options_setflag(argv[0]+5, false)) {
303                             con_out("unknown flag: %s\n", argv[0]+2);
304                             return false;
305                         }
306                     }
307                     else if (!options_setflag(argv[0]+2, true)) {
308                         con_out("unknown flag: %s\n", argv[0]+2);
309                         return false;
310                     }
311                     break;
312                 case 'W':
313                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
314                     if (!strcmp(argv[0]+2, "HELP")) {
315                         con_out("Possible warnings:\n");
316                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
317                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
318                             con_out(" -W%s\n", buffer);
319                         }
320                         exit(0);
321                     }
322                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
323                         opts_werror = false;
324                         break;
325                     }
326                     else if (!strcmp(argv[0]+2, "ERROR")) {
327                         opts_werror = true;
328                         break;
329                     }
330                     else if (!strcmp(argv[0]+2, "NONE")) {
331                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
332                             opts_warn[itr] = 0;
333                         break;
334                     }
335                     else if (!strcmp(argv[0]+2, "ALL")) {
336                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
337                             opts_warn[itr] = 0xFFFFFFFFL;
338                         break;
339                     }
340                     if (!strncmp(argv[0]+2, "NO_", 3)) {
341                         if (!options_setwarn(argv[0]+5, false)) {
342                             con_out("unknown warning: %s\n", argv[0]+2);
343                             return false;
344                         }
345                     }
346                     else if (!options_setwarn(argv[0]+2, true)) {
347                         con_out("unknown warning: %s\n", argv[0]+2);
348                         return false;
349                     }
350                     break;
351
352                 case 'O':
353                     if (!options_witharg(&argc, &argv, &argarg)) {
354                         con_out("option -O requires a numerical argument\n");
355                         return false;
356                     }
357                     opts_O = atoi(argarg);
358                     break;
359
360                 case 'o':
361                     if (!options_witharg(&argc, &argv, &argarg)) {
362                         con_out("option -o requires an argument: the output file name\n");
363                         return false;
364                     }
365                     opts_output = argarg;
366                     opts_output_wasset = true;
367                     break;
368
369                 case 'a':
370                 case 's':
371                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
372                     if (!options_witharg(&argc, &argv, &argarg)) {
373                         con_out("option -a requires a filename %s\n",
374                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
375                         return false;
376                     }
377                     item.filename = argarg;
378                     vec_push(items, item);
379                     break;
380
381                 case '-':
382                     if (!argv[0][2]) {
383                         /* anything following -- is considered a non-option argument */
384                         argend = true;
385                         break;
386                     }
387             /* All long options without arguments */
388                     else if (!strcmp(argv[0]+2, "help")) {
389                         usage();
390                         exit(0);
391                     }
392                     else {
393             /* All long options with arguments */
394                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
395                             opts_output = argarg;
396                             opts_output_wasset = true;
397                         } else {
398                             con_out("Unknown parameter: %s\n", argv[0]);
399                             return false;
400                         }
401                     }
402                     break;
403
404                 default:
405                     con_out("Unknown parameter: %s\n", argv[0]);
406                     return false;
407             }
408         }
409         else
410         {
411             /* it's a QC filename */
412             item.filename = argv[0];
413             item.type     = TYPE_QC;
414             vec_push(items, item);
415         }
416     }
417     return true;
418 }
419
420 /* returns the line number, or -1 on error */
421 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
422     int    len;
423     char  *line;
424     char  *start;
425     char  *end;
426
427     line = *out;
428     len = util_getline(&line, alen, src);
429     if (len == -1)
430         return false;
431
432     /* start at first non-blank */
433     for (start = line; isspace(*start); ++start) {}
434     /* end at the first non-blank */
435     for (end = start;  *end && !isspace(*end);  ++end)   {}
436
437     *out = line;
438     /* move the actual filename to the beginning */
439     while (start != end) {
440         *line++ = *start++;
441     }
442     *line = 0;
443     return true;
444 }
445
446 int main(int argc, char **argv) {
447     size_t itr;
448     int retval = 0;
449     bool opts_output_free = false;
450     bool progs_src = false;
451     FILE *outfile = NULL;
452
453     app_name = argv[0];
454     con_init();
455
456     /* default options / warn flags */
457     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
458     options_set(opts_warn, WARN_EXTENSIONS, true);
459     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
460     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
461     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
462     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
463     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
464     options_set(opts_warn, WARN_VOID_VARIABLES, true);
465     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
466     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
467     options_set(opts_warn, WARN_FRAME_MACROS, true);
468     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
469     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
470     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
471     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
472     options_set(opts_warn, WARN_PREPROCESSOR, true);
473     options_set(opts_warn, WARN_MULTIFILE_IF, true);
474     options_set(opts_warn, WARN_DOUBLE_DECLARATION, true);
475
476     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
477     options_set(opts_flags, FTEPP, false);
478
479     if (!options_parse(argc, argv)) {
480         return usage();
481     }
482
483     /* the standard decides which set of operators to use */
484     if (opts_standard == COMPILER_GMQCC) {
485         operators = c_operators;
486         operator_count = c_operator_count;
487     } else if (opts_standard == COMPILER_FTEQCC) {
488         operators = fte_operators;
489         operator_count = fte_operator_count;
490     } else {
491         operators = qcc_operators;
492         operator_count = qcc_operator_count;
493     }
494
495     if (opts_dump) {
496         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
497             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
498         }
499         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
500             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
501         }
502         con_out("output = %s\n", opts_output);
503         con_out("optimization level = %i\n", (int)opts_O);
504         con_out("standard = %i\n", opts_standard);
505     }
506
507     if (opts_pp_only) {
508         if (opts_output_wasset) {
509             outfile = util_fopen(opts_output, "wb");
510             if (!outfile) {
511                 con_err("failed to open `%s` for writing\n", opts_output);
512                 retval = 1;
513                 goto cleanup;
514             }
515         }
516         else
517             outfile = stdout;
518     }
519
520     if (!opts_pp_only) {
521         if (!parser_init()) {
522             con_err("failed to initialize parser\n");
523             retval = 1;
524             goto cleanup;
525         }
526     }
527     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
528         if (!ftepp_init()) {
529             con_err("failed to initialize parser\n");
530             retval = 1;
531             goto cleanup;
532         }
533     }
534
535     util_debug("COM", "starting ...\n");
536
537     if (!vec_size(items)) {
538         FILE *src;
539         char *line;
540         size_t linelen = 0;
541
542         progs_src = true;
543
544         src = util_fopen("progs.src", "rb");
545         if (!src) {
546             con_err("failed to open `progs.src` for reading\n");
547             retval = 1;
548             goto cleanup;
549         }
550
551         line = NULL;
552         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
553             con_err("illformatted progs.src file: expected output filename in first line\n");
554             retval = 1;
555             goto srcdone;
556         }
557
558         if (!opts_output_wasset) {
559             opts_output = util_strdup(line);
560             opts_output_free = true;
561         }
562
563         while (progs_nextline(&line, &linelen, src)) {
564             argitem item;
565             if (!line[0] || (line[0] == '/' && line[1] == '/'))
566                 continue;
567             item.filename = util_strdup(line);
568             item.type     = TYPE_QC;
569             vec_push(items, item);
570         }
571
572 srcdone:
573         fclose(src);
574         mem_d(line);
575     }
576
577     if (retval)
578         goto cleanup;
579
580     if (vec_size(items)) {
581         if (!opts_pp_only) {
582             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
583             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
584         }
585         for (itr = 0; itr < vec_size(items); ++itr) {
586             if (!opts_pp_only) {
587                 con_out("  item: %s (%s)\n",
588                        items[itr].filename,
589                        ( (items[itr].type == TYPE_QC ? "qc" :
590                          (items[itr].type == TYPE_ASM ? "asm" :
591                          (items[itr].type == TYPE_SRC ? "progs.src" :
592                          ("unknown"))))));
593             }
594
595             if (opts_pp_only) {
596                 const char *out;
597                 if (!ftepp_preprocess_file(items[itr].filename)) {
598                     retval = 1;
599                     goto cleanup;
600                 }
601                 out = ftepp_get();
602                 if (out)
603                     fprintf(outfile, "%s", out);
604                 ftepp_flush();
605             }
606             else {
607                 if (OPTS_FLAG(FTEPP)) {
608                     const char *data;
609                     if (!ftepp_preprocess_file(items[itr].filename)) {
610                         retval = 1;
611                         goto cleanup;
612                     }
613                     data = ftepp_get();
614                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
615                         retval = 1;
616                         goto cleanup;
617                     }
618                     ftepp_flush();
619                 }
620                 else {
621                     if (!parser_compile_file(items[itr].filename)) {
622                         retval = 1;
623                         goto cleanup;
624                     }
625                 }
626             }
627
628             if (progs_src) {
629                 mem_d(items[itr].filename);
630                 items[itr].filename = NULL;
631             }
632         }
633
634         ftepp_finish();
635         if (!opts_pp_only) {
636             if (!parser_finish(opts_output)) {
637                 retval = 1;
638                 goto cleanup;
639             }
640         }
641     }
642
643     /* stuff */
644
645 cleanup:
646     util_debug("COM", "cleaning ...\n");
647     ftepp_finish();
648     con_close();
649     vec_free(items);
650
651     if (!opts_pp_only)
652         parser_cleanup();
653     if (opts_output_free)
654         mem_d((char*)opts_output);
655
656     lex_cleanup();
657     util_meminfo();
658     return retval;
659 }