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