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