]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
reuse old opts_, renamed all opt_ to opts_ to be consistent, making code.c use OPTS_F...
[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
25 uint32_t    opts_flags[1 + (COUNT_FLAGS / 32)];
26 uint32_t    opts_warn [1 + (COUNT_WARNINGS / 32)];
27
28 uint32_t    opts_O        = 1;
29 const char *opts_output   = "progs.dat";
30 int         opts_standard = COMPILER_GMQCC;
31 bool        opts_debug    = false;
32 bool        opts_memchk   = false;
33
34 typedef struct { char *filename; int type; } argitem;
35 VECTOR_MAKE(argitem, items);
36
37 #define TYPE_QC  0
38 #define TYPE_ASM 1
39 #define TYPE_SRC 2
40
41 static const char *app_name;
42
43 static int usage() {
44     printf("usage: %s [options] [files...]", app_name);
45     printf("options:\n"
46            "  -h, --help             show this help message\n"
47            "  -debug                 turns on compiler debug messages\n"
48            "  -memchk                turns on compiler memory leak check\n");
49     printf("  -o, --output=file      output file, defaults to progs.dat\n"
50            "  -a filename            add an asm file to be assembled\n"
51            "  -s filename            add a progs.src file to be used\n");
52     printf("  -f<flag>               enable a flag\n"
53            "  -fno-<flag>            disable a flag\n"
54            "  -std standard          select one of the following standards\n"
55            "       -std=qcc          original QuakeC\n"
56            "       -std=fteqcc       fteqcc QuakeC\n"
57            "       -std=gmqcc        this compiler (default)\n");
58     printf("  -W<warning>            enable a warning\n"
59            "  -Wno-<warning>         disable a warning\n"
60            "  -Wall                  enable all warnings\n");
61     printf("\n");
62     printf("flags:\n"
63            "  -fdarkplaces-string-table-bug\n"
64            "            patch the string table to work with some bugged darkplaces versions\n"
65            "  -fomit-nullbytes\n"
66            "            omits certain null-bytes for a smaller output - requires a patched engine\n"
67            );
68     return -1;
69 }
70
71 static void strtocmd(char *str)
72 {
73     for(; *str; ++str) {
74         if (*str == '-') {
75             *str = '_';
76             continue;
77         }
78         if (isalpha(*str) && !isupper(*str)) {
79             *str += 'A' - 'a';
80             continue;
81         }
82     }
83 }
84
85 static void strtononcmd(char *buf, const char *str)
86 {
87     for(; *str; ++buf, ++str) {
88         if (*str == '_') {
89             *buf = '-';
90             continue;
91         }
92         if (isalpha(*str) && isupper(*str)) {
93             *buf = *str + 'a' - 'A';
94             continue;
95         }
96         *buf = *str;
97     }
98     *buf = 0;
99 }
100
101 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
102     size_t i;
103
104     for (i = 0; i < listsize; ++i) {
105         if (!strcmp(name, list[i].name)) {
106             longbit lb = list[i].bit;
107 #if 0
108             if (on)
109                 flags[lb.idx] |= (1<<(lb.bit));
110             else
111                 flags[lb.idx] &= ~(1<<(lb.bit));
112 #else
113             if (on)
114                 flags[0] |= (1<<lb);
115             else
116                 flags[0] &= ~(1<<(lb));
117 #endif
118             return true;
119         }
120     }
121     return false;
122 }
123 static bool options_setflag(const char *name, bool on) {
124     return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
125 }
126 static bool options_setwarn(const char *name, bool on) {
127     return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
128 }
129
130 static bool options_witharg(int *argc_, char ***argv_, char **out) {
131     int  argc   = *argc_;
132     char **argv = *argv_;
133
134     if (argv[0][2]) {
135         *out = argv[0]+2;
136         return true;
137     }
138     /* eat up the next */
139     if (argc < 2) /* no parameter was provided */
140         return false;
141
142     *out = argv[1];
143     --*argc_;
144     ++*argv_;
145     return true;
146 }
147
148 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
149     int  argc   = *argc_;
150     char **argv = *argv_;
151
152     size_t len = strlen(optname);
153
154     if (strncmp(argv[0]+ds, optname, len))
155         return false;
156
157     /* it's --optname, check how the parameter is supplied */
158     if (argv[0][ds+len] == '=') {
159         /* using --opt=param */
160         *out = argv[0]+ds+len+1;
161         return true;
162     }
163
164     if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
165         return false;
166
167     /* using --opt param */
168     *out = argv[1];
169     --*argc_;
170     ++*argv_;
171     return true;
172 }
173 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
174     return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
175 }
176 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
177     return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
178 }
179
180 static bool options_parse(int argc, char **argv) {
181     bool argend = false;
182     size_t itr;
183     char buffer[1024];
184     while (!argend && argc > 1) {
185         char *argarg;
186         argitem item;
187
188         ++argv;
189         --argc;
190
191         if (argv[0][0] == '-') {
192     /* All gcc-type long options */
193             if (options_long_gcc("std", &argc, &argv, &argarg)) {
194                 if      (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
195                     opts_standard = COMPILER_GMQCC;
196                 else if (!strcmp(argarg, "qcc"))
197                     opts_standard = COMPILER_QCC;
198                 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
199                     opts_standard = COMPILER_FTEQCC;
200                 else if (!strcmp(argarg, "qccx"))
201                     opts_standard = COMPILER_QCCX;
202                 else {
203                     printf("Unknown standard: %s\n", argarg);
204                     return false;
205                 }
206                 continue;
207             }
208             if (!strcmp(argv[0]+1, "debug")) {
209                 opts_debug = true;
210                 continue;
211             }
212             if (!strcmp(argv[0]+1, "memchk")) {
213                 opts_memchk = true;
214                 continue;
215             }
216
217             switch (argv[0][1]) {
218                 /* -h, show usage but exit with 0 */
219                 case 'h':
220                     usage();
221                     exit(0);
222                     break;
223
224                 /* handle all -fflags */
225                 case 'f':
226                     strtocmd(argv[0]+2);
227                     if (!strcmp(argv[0]+2, "HELP")) {
228                         printf("Possible flags:\n");
229                         for (itr = 0; itr < COUNT_FLAGS; ++itr) {
230                             strtononcmd(buffer, opts_flag_list[itr].name);
231                             printf(" -f%s\n", buffer);
232                         }
233                         exit(0);
234                     }
235                     else if (!strncmp(argv[0]+2, "NO-", 3)) {
236                         if (!options_setflag(argv[0]+5, false)) {
237                             printf("unknown flag: %s\n", argv[0]+2);
238                             return false;
239                         }
240                     }
241                     else if (!options_setflag(argv[0]+2, true)) {
242                         printf("unknown flag: %s\n", argv[0]+2);
243                         return false;
244                     }
245                     break;
246                 case 'W':
247                     strtocmd(argv[0]+2);
248                     if (!strcmp(argv[0]+2, "HELP")) {
249                         printf("Possible warnings:\n");
250                         for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
251                             strtononcmd(buffer, opts_warn_list[itr].name);
252                             printf(" -W%s\n", buffer);
253                         }
254                         exit(0);
255                     }
256                     else if (!strcmp(argv[0]+2, "all")) {
257                         for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
258                             opts_warn[itr] = 0xFFFFFFFFL;
259                         break;
260                     }
261                     if (!strncmp(argv[0]+2, "no-", 3)) {
262                         if (!options_setwarn(argv[0]+5, false)) {
263                             printf("unknown warning: %s\n", argv[0]+2);
264                             return false;
265                         }
266                     }
267                     else if (!options_setwarn(argv[0]+2, true)) {
268                         printf("unknown warning: %s\n", argv[0]+2);
269                         return false;
270                     }
271                     break;
272
273                 case 'O':
274                     if (!options_witharg(&argc, &argv, &argarg)) {
275                         printf("option -O requires a numerical argument\n");
276                         return false;
277                     }
278                     opts_O = atoi(argarg);
279                     break;
280
281                 case 'o':
282                     if (!options_witharg(&argc, &argv, &argarg)) {
283                         printf("option -o requires an argument: the output file name\n");
284                         return false;
285                     }
286                     opts_output = argarg;
287                     break;
288
289                 case 'a':
290                 case 's':
291                     item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
292                     if (!options_witharg(&argc, &argv, &argarg)) {
293                         printf("option -a requires a filename %s\n",
294                                 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
295                         return false;
296                     }
297                     item.filename = argarg;
298                     items_add(item);
299                     break;
300
301                 case '-':
302                     if (!argv[0][2]) {
303                         /* anything following -- is considered a non-option argument */
304                         argend = true;
305                         break;
306                     }
307             /* All long options without arguments */
308                     else if (!strcmp(argv[0]+2, "help")) {
309                         usage();
310                         exit(0);
311                     }
312                     else {
313             /* All long options with arguments */
314                         if (options_long_witharg("output", &argc, &argv, &argarg))
315                             opts_output = argarg;
316                         else
317                         {
318                             printf("Unknown parameter: %s\n", argv[0]);
319                             return false;
320                         }
321                     }
322                     break;
323
324                 default:
325                     printf("Unknown parameter: %s\n", argv[0]);
326                     return false;
327             }
328         }
329         else
330         {
331             /* it's a QC filename */
332             argitem item;
333             item.filename = argv[0];
334             item.type     = TYPE_QC;
335             items_add(item);
336         }
337     }
338     return true;
339 }
340
341 int main(int argc, char **argv) {
342     size_t itr;
343     app_name = argv[0];
344
345     if (!options_parse(argc, argv)) {
346         return usage();
347     }
348
349     for (itr = 0; itr < COUNT_FLAGS; ++itr) {
350         printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
351     }
352     for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
353         printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
354     }
355     printf("output = %s\n", opts_output);
356     printf("optimization level = %i\n", (int)opts_O);
357     printf("standard = %i\n", opts_standard);
358
359     if (items_elements) {
360         printf("Mode: manual\n");
361         printf("There are %lu items to compile:\n", (unsigned long)items_elements);
362         for (itr = 0; itr < items_elements; ++itr) {
363             printf("  item: %s (%s)\n",
364                    items_data[itr].filename,
365                    ( (items_data[itr].type == TYPE_QC ? "qc" :
366                      (items_data[itr].type == TYPE_ASM ? "asm" :
367                      (items_data[itr].type == TYPE_SRC ? "progs.src" :
368                      ("unknown"))))));
369         }
370     } else {
371         printf("Mode: progs.src\n");
372     }
373
374     util_debug("COM", "starting ...\n");
375
376     /* stuff */
377
378     util_debug("COM", "cleaning ...\n");
379
380     util_meminfo();
381     return 0;
382 }