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