]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
Importing a tiny README file
[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_dump     = false;
36 bool        opts_werror   = false;
37 bool        opts_forcecrc = false;
38 bool        opts_pp_only  = false;
39 size_t      opts_max_array_size = 1024;
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 static argitem *items = NULL;
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     con_out("usage: %s [options] [files...]", app_name);
60     con_out("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     con_out("  -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     con_out("  -E                     stop after preprocessing\n");
68     con_out("  -f<flag>               enable a flag\n"
69             "  -fno-<flag>            disable a flag\n"
70             "  -std standard          select one of the following standards\n"
71             "       -std=qcc          original QuakeC\n"
72             "       -std=fteqcc       fteqcc QuakeC\n"
73             "       -std=gmqcc        this compiler (default)\n");
74     con_out("  -W<warning>            enable a warning\n"
75             "  -Wno-<warning>         disable a warning\n"
76             "  -Wall                  enable all warnings\n"
77             "  -Werror                treat warnings as errors\n");
78     con_out("  -force-crc=num         force a specific checksum into the header\n");
79     con_out("\n");
80     con_out("flags:\n"
81             "  -fadjust-vector-fields\n"
82             "            when assigning a vector field, its _y and _z fields also get assigned\n"
83            );
84     return -1;
85 }
86
87 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
88     size_t i;
89
90     for (i = 0; i < listsize; ++i) {
91         if (!strcmp(name, list[i].name)) {
92             longbit lb = list[i].bit;
93 #if 0
94             if (on)
95                 flags[lb.idx] |= (1<<(lb.bit));
96             else
97                 flags[lb.idx] &= ~(1<<(lb.bit));
98 #else
99             if (on)
100                 flags[0] |= (1<<lb);
101             else
102                 flags[0] &= ~(1<<(lb));
103 #endif
104             return true;
105         }
106     }
107     return false;
108 }
109 static bool options_setflag(const char *name, bool on) {
110     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
111 }
112 static bool options_setwarn(const char *name, bool on) {
113     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
114 }
115
116 static bool options_witharg(int *argc_, char ***argv_, char **out) {
117     int  argc   = *argc_;
118     char **argv = *argv_;
119
120     if (argv[0][2]) {
121         *out = argv[0]+2;
122         return true;
123     }
124     /* eat up the next */
125     if (argc < 2) /* no parameter was provided */
126         return false;
127
128     *out = argv[1];
129     --*argc_;
130     ++*argv_;
131     return true;
132 }
133
134 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
135     int  argc   = *argc_;
136     char **argv = *argv_;
137
138     size_t len = strlen(optname);
139
140     if (strncmp(argv[0]+ds, optname, len))
141         return false;
142
143     /* it's --optname, check how the parameter is supplied */
144     if (argv[0][ds+len] == '=') {
145         /* using --opt=param */
146         *out = argv[0]+ds+len+1;
147         return true;
148     }
149
150     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
151         return false;
152
153     /* using --opt param */
154     *out = argv[1];
155     --*argc_;
156     ++*argv_;
157     return true;
158 }
159 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
160     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
161 }
162 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
163     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
164 }
165
166 static void options_set(uint32_t *flags, size_t idx, bool on)
167 {
168     longbit lb = LONGBIT(idx);
169 #if 0
170     if (on)
171         flags[lb.idx] |= (1<<(lb.bit));
172     else
173         flags[lb.idx] &= ~(1<<(lb.bit));
174 #else
175     if (on)
176         flags[0] |= (1<<(lb));
177     else
178         flags[0] &= ~(1<<(lb));
179 #endif
180 }
181
182 static bool options_parse(int argc, char **argv) {
183     bool argend = false;
184     size_t itr;
185     char  buffer[1024];
186     char *redirout = (char*)stdout;
187     char *redirerr = (char*)stderr;
188
189     while (!argend && argc > 1) {
190         char *argarg;
191         argitem item;
192
193         ++argv;
194         --argc;
195
196         if (argv[0][0] == '-') {
197     /* All gcc-type long options */
198             if (options_long_gcc("std", &argc, &argv, &argarg)) {
199                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default")) {
200                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
201                     opts_standard = COMPILER_GMQCC;
202                 } else if (!strcmp(argarg, "qcc")) {
203                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
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, ADJUST_VECTOR_FIELDS, false);
208                     opts_standard = COMPILER_FTEQCC;
209                 } else if (!strcmp(argarg, "qccx")) {
210                     options_set(opts_flags, ADJUST_VECTOR_FIELDS, false);
211                     opts_standard = COMPILER_QCCX;
212                 } else {
213                     con_out("Unknown standard: %s\n", argarg);
214                     return false;
215                 }
216                 continue;
217             }
218             if (options_long_gcc("force-crc", &argc, &argv, &argarg)) {
219                 opts_forcecrc = true;
220                 opts_forced_crc = strtol(argarg, NULL, 0);
221                 continue;
222             }
223             if (options_long_gcc("redirout", &argc, &argv, &redirout)) {
224                 continue;
225             }
226             if (options_long_gcc("redirerr", &argc, &argv, &redirerr)) {
227                 continue;
228             }
229
230             if (!strcmp(argv[0]+1, "debug")) {
231                 opts_debug = true;
232                 continue;
233             }
234             if (!strcmp(argv[0]+1, "dump")) {
235                 opts_dump = true;
236                 continue;
237             }
238             if (!strcmp(argv[0]+1, "memchk")) {
239                 opts_memchk = true;
240                 continue;
241             }
242             if (!strcmp(argv[0]+1, "nocolor")) {
243                 con_color(0);
244                 continue;
245             }
246
247             switch (argv[0][1]) {
248                 /* -h, show usage but exit with 0 */
249                 case 'h':
250                     usage();
251                     exit(0);
252                     break;
253
254                 case 'E':
255                     opts_pp_only = true;
256                     break;
257
258                 /* handle all -fflags */
259                 case 'f':
260                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
261                     if (!strcmp(argv[0]+2, "HELP")) {
262                         con_out("Possible flags:\n");
263                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
264                             util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
265                             con_out(" -f%s\n", buffer);
266                         }
267                         exit(0);
268                     }
269                     else if (!strncmp(argv[0]+2, "NO_", 3)) {
270                         if (!options_setflag(argv[0]+5, false)) {
271                             con_out("unknown flag: %s\n", argv[0]+2);
272                             return false;
273                         }
274                     }
275                     else if (!options_setflag(argv[0]+2, true)) {
276                         con_out("unknown flag: %s\n", argv[0]+2);
277                         return false;
278                     }
279                     break;
280                 case 'W':
281                     util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
282                     if (!strcmp(argv[0]+2, "HELP")) {
283                         con_out("Possible warnings:\n");
284                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
285                             util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
286                             con_out(" -W%s\n", buffer);
287                         }
288                         exit(0);
289                     }
290                     else if (!strcmp(argv[0]+2, "NO_ERROR")) {
291                         opts_werror = false;
292                         break;
293                     }
294                     else if (!strcmp(argv[0]+2, "ERROR")) {
295                         opts_werror = true;
296                         break;
297                     }
298                     else if (!strcmp(argv[0]+2, "NONE")) {
299                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
300                             opts_warn[itr] = 0;
301                         break;
302                     }
303                     else if (!strcmp(argv[0]+2, "ALL")) {
304                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
305                             opts_warn[itr] = 0xFFFFFFFFL;
306                         break;
307                     }
308                     if (!strncmp(argv[0]+2, "NO_", 3)) {
309                         if (!options_setwarn(argv[0]+5, false)) {
310                             con_out("unknown warning: %s\n", argv[0]+2);
311                             return false;
312                         }
313                     }
314                     else if (!options_setwarn(argv[0]+2, true)) {
315                         con_out("unknown warning: %s\n", argv[0]+2);
316                         return false;
317                     }
318                     break;
319
320                 case 'O':
321                     if (!options_witharg(&argc, &argv, &argarg)) {
322                         con_out("option -O requires a numerical argument\n");
323                         return false;
324                     }
325                     opts_O = atoi(argarg);
326                     break;
327
328                 case 'o':
329                     if (!options_witharg(&argc, &argv, &argarg)) {
330                         con_out("option -o requires an argument: the output file name\n");
331                         return false;
332                     }
333                     opts_output = argarg;
334                     opts_output_wasset = true;
335                     break;
336
337                 case 'a':
338                 case 's':
339                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
340                     if (!options_witharg(&argc, &argv, &argarg)) {
341                         con_out("option -a requires a filename %s\n",
342                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
343                         return false;
344                     }
345                     item.filename = argarg;
346                     vec_push(items, item);
347                     break;
348
349                 case '-':
350                     if (!argv[0][2]) {
351                         /* anything following -- is considered a non-option argument */
352                         argend = true;
353                         break;
354                     }
355             /* All long options without arguments */
356                     else if (!strcmp(argv[0]+2, "help")) {
357                         usage();
358                         exit(0);
359                     }
360                     else {
361             /* All long options with arguments */
362                         if (options_long_witharg("output", &argc, &argv, &argarg)) {
363                             opts_output = argarg;
364                             opts_output_wasset = true;
365                         } else {
366                             con_out("Unknown parameter: %s\n", argv[0]);
367                             return false;
368                         }
369                     }
370                     break;
371
372                 default:
373                     con_out("Unknown parameter: %s\n", argv[0]);
374                     return false;
375             }
376         }
377         else
378         {
379             /* it's a QC filename */
380             argitem item;
381             item.filename = argv[0];
382             item.type     = TYPE_QC;
383             vec_push(items, item);
384         }
385     }
386     con_change(redirout, redirerr);
387     return true;
388 }
389
390 /* returns the line number, or -1 on error */
391 static bool progs_nextline(char **out, size_t *alen,FILE *src) {
392     int    len;
393     char  *line;
394     char  *start;
395     char  *end;
396
397     line = *out;
398     len = util_getline(&line, alen, src);
399     if (len == -1)
400         return false;
401
402     /* start at first non-blank */
403     for (start = line; isspace(*start); ++start) {}
404     /* end at the first non-blank */
405     for (end = start;  *end && !isspace(*end);  ++end)   {}
406
407     *out = line;
408     /* move the actual filename to the beginning */
409     while (start != end) {
410         *line++ = *start++;
411     }
412     *line = 0;
413     return true;
414 }
415
416 int main(int argc, char **argv) {
417     size_t itr;
418     int retval = 0;
419     bool opts_output_free = false;
420     bool progs_src = false;
421     FILE *outfile = NULL;
422
423     app_name = argv[0];
424     con_init();
425
426     /* default options / warn flags */
427     options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
428     options_set(opts_warn, WARN_EXTENSIONS, true);
429     options_set(opts_warn, WARN_FIELD_REDECLARED, true);
430     options_set(opts_warn, WARN_TOO_FEW_PARAMETERS, true);
431     options_set(opts_warn, WARN_MISSING_RETURN_VALUES, true);
432     options_set(opts_warn, WARN_USED_UNINITIALIZED, true);
433     options_set(opts_warn, WARN_LOCAL_CONSTANTS, true);
434     options_set(opts_warn, WARN_VOID_VARIABLES, true);
435     options_set(opts_warn, WARN_IMPLICIT_FUNCTION_POINTER, true);
436     options_set(opts_warn, WARN_VARIADIC_FUNCTION, true);
437     options_set(opts_warn, WARN_FRAME_MACROS, true);
438     options_set(opts_warn, WARN_UNUSED_VARIABLE, true);
439     options_set(opts_warn, WARN_EFFECTLESS_STATEMENT, true);
440     options_set(opts_warn, WARN_END_SYS_FIELDS, true);
441     options_set(opts_warn, WARN_ASSIGN_FUNCTION_TYPES, true);
442     options_set(opts_warn, WARN_PREPROCESSOR, true);
443     options_set(opts_warn, WARN_MULTIFILE_IF, true);
444
445     options_set(opts_flags, ADJUST_VECTOR_FIELDS, true);
446     options_set(opts_flags, FTEPP, false);
447
448     if (!options_parse(argc, argv)) {
449         return usage();
450     }
451
452     /* the standard decides which set of operators to use */
453     if (opts_standard == COMPILER_GMQCC) {
454         operators = c_operators;
455         operator_count = c_operator_count;
456     } else {
457         operators = qcc_operators;
458         operator_count = qcc_operator_count;
459     }
460
461     if (opts_dump) {
462         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
463             con_out("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
464         }
465         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
466             con_out("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
467         }
468         con_out("output = %s\n", opts_output);
469         con_out("optimization level = %i\n", (int)opts_O);
470         con_out("standard = %i\n", opts_standard);
471     }
472
473     if (opts_pp_only) {
474         if (opts_output_wasset) {
475             outfile = util_fopen(opts_output, "wb");
476             if (!outfile) {
477                 con_err("failed to open `%s` for writing\n", opts_output);
478                 retval = 1;
479                 goto cleanup;
480             }
481         }
482         else
483             outfile = stdout;
484     }
485
486     if (!opts_pp_only) {
487         if (!parser_init()) {
488             con_err("failed to initialize parser\n");
489             retval = 1;
490             goto cleanup;
491         }
492     }
493     if (opts_pp_only || OPTS_FLAG(FTEPP)) {
494         if (!ftepp_init()) {
495             con_err("failed to initialize parser\n");
496             retval = 1;
497             goto cleanup;
498         }
499     }
500
501     util_debug("COM", "starting ...\n");
502
503     if (!vec_size(items)) {
504         FILE *src;
505         char *line;
506         size_t linelen = 0;
507
508         progs_src = true;
509
510         src = util_fopen("progs.src", "rb");
511         if (!src) {
512             con_err("failed to open `progs.src` for reading\n");
513             retval = 1;
514             goto cleanup;
515         }
516
517         line = NULL;
518         if (!progs_nextline(&line, &linelen, src) || !line[0]) {
519             con_err("illformatted progs.src file: expected output filename in first line\n");
520             retval = 1;
521             goto srcdone;
522         }
523
524         if (!opts_output_wasset) {
525             opts_output = util_strdup(line);
526             opts_output_free = true;
527         }
528
529         while (progs_nextline(&line, &linelen, src)) {
530             argitem item;
531             if (!line[0] || (line[0] == '/' && line[1] == '/'))
532                 continue;
533             item.filename = util_strdup(line);
534             item.type     = TYPE_QC;
535             vec_push(items, item);
536         }
537
538 srcdone:
539         fclose(src);
540         mem_d(line);
541     }
542
543     if (retval)
544         goto cleanup;
545
546     if (vec_size(items)) {
547         if (!opts_pp_only) {
548             con_out("Mode: %s\n", (progs_src ? "progs.src" : "manual"));
549             con_out("There are %lu items to compile:\n", (unsigned long)vec_size(items));
550         }
551         for (itr = 0; itr < vec_size(items); ++itr) {
552             if (!opts_pp_only) {
553                 con_out("  item: %s (%s)\n",
554                        items[itr].filename,
555                        ( (items[itr].type == TYPE_QC ? "qc" :
556                          (items[itr].type == TYPE_ASM ? "asm" :
557                          (items[itr].type == TYPE_SRC ? "progs.src" :
558                          ("unknown"))))));
559             }
560
561             if (opts_pp_only) {
562                 if (!ftepp_preprocess_file(items[itr].filename)) {
563                     retval = 1;
564                     goto cleanup;
565                 }
566                 fprintf(outfile, "%s", ftepp_get());
567                 ftepp_flush();
568             }
569             else {
570                 if (OPTS_FLAG(FTEPP)) {
571                     const char *data;
572                     if (!ftepp_preprocess_file(items[itr].filename)) {
573                         retval = 1;
574                         goto cleanup;
575                     }
576                     data = ftepp_get();
577                     if (!parser_compile_string_len(items[itr].filename, data, vec_size(data)-1)) {
578                         retval = 1;
579                         goto cleanup;
580                     }
581                     ftepp_flush();
582                 }
583                 else {
584                     if (!parser_compile_file(items[itr].filename)) {
585                         retval = 1;
586                         goto cleanup;
587                     }
588                 }
589             }
590
591             if (progs_src) {
592                 mem_d(items[itr].filename);
593                 items[itr].filename = NULL;
594             }
595         }
596
597         ftepp_finish();
598         if (!opts_pp_only) {
599             if (!parser_finish(opts_output)) {
600                 retval = 1;
601                 goto cleanup;
602             }
603         }
604     }
605
606     /* stuff */
607
608 cleanup:
609     util_debug("COM", "cleaning ...\n");
610     ftepp_finish();
611     con_close();
612     vec_free(items);
613
614     if (!opts_pp_only)
615         parser_cleanup();
616     if (opts_output_free)
617         mem_d((char*)opts_output);
618
619     lex_cleanup();
620     util_meminfo();
621     return retval;
622 }