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:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
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
25 uint32_t opts_flags[1 + (COUNT_FLAGS / 32)];
26 uint32_t opts_warn [1 + (COUNT_WARNINGS / 32)];
29 const char *opts_output = "progs.dat";
30 int opts_standard = COMPILER_GMQCC;
31 bool opts_debug = false;
32 bool opts_memchk = false;
34 typedef struct { char *filename; int type; } argitem;
35 VECTOR_MAKE(argitem, items);
41 static const char *app_name;
44 printf("usage: %s [options] [files...]", app_name);
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");
63 " -fdarkplaces-string-table-bug\n"
64 " patch the string table to work with some bugged darkplaces versions\n"
66 " omits certain null-bytes for a smaller output - requires a patched engine\n"
71 static void strtocmd(char *str)
78 if (isalpha(*str) && !isupper(*str)) {
85 static void strtononcmd(char *buf, const char *str)
87 for(; *str; ++buf, ++str) {
92 if (isalpha(*str) && isupper(*str)) {
93 *buf = *str + 'a' - 'A';
101 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
104 for (i = 0; i < listsize; ++i) {
105 if (!strcmp(name, list[i].name)) {
106 longbit lb = list[i].bit;
109 flags[lb.idx] |= (1<<(lb.bit));
111 flags[lb.idx] &= ~(1<<(lb.bit));
116 flags[0] &= ~(1<<(lb));
123 static bool options_setflag(const char *name, bool on) {
124 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
126 static bool options_setwarn(const char *name, bool on) {
127 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
130 static bool options_witharg(int *argc_, char ***argv_, char **out) {
132 char **argv = *argv_;
138 /* eat up the next */
139 if (argc < 2) /* no parameter was provided */
148 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
150 char **argv = *argv_;
152 size_t len = strlen(optname);
154 if (strncmp(argv[0]+ds, optname, len))
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;
164 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
167 /* using --opt param */
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);
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);
180 static bool options_parse(int argc, char **argv) {
184 while (!argend && argc > 1) {
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;
203 printf("Unknown standard: %s\n", argarg);
208 if (!strcmp(argv[0]+1, "debug")) {
212 if (!strcmp(argv[0]+1, "memchk")) {
217 switch (argv[0][1]) {
218 /* -h, show usage but exit with 0 */
224 /* handle all -fflags */
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);
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);
241 else if (!options_setflag(argv[0]+2, true)) {
242 printf("unknown flag: %s\n", 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);
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;
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);
267 else if (!options_setwarn(argv[0]+2, true)) {
268 printf("unknown warning: %s\n", argv[0]+2);
274 if (!options_witharg(&argc, &argv, &argarg)) {
275 printf("option -O requires a numerical argument\n");
278 opts_O = atoi(argarg);
282 if (!options_witharg(&argc, &argv, &argarg)) {
283 printf("option -o requires an argument: the output file name\n");
286 opts_output = argarg;
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"));
297 item.filename = argarg;
303 /* anything following -- is considered a non-option argument */
307 /* All long options without arguments */
308 else if (!strcmp(argv[0]+2, "help")) {
313 /* All long options with arguments */
314 if (options_long_witharg("output", &argc, &argv, &argarg))
315 opts_output = argarg;
318 printf("Unknown parameter: %s\n", argv[0]);
325 printf("Unknown parameter: %s\n", argv[0]);
331 /* it's a QC filename */
333 item.filename = argv[0];
341 int main(int argc, char **argv) {
345 if (!options_parse(argc, argv)) {
349 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
350 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
352 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
353 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
355 printf("output = %s\n", opts_output);
356 printf("optimization level = %i\n", (int)opts_O);
357 printf("standard = %i\n", opts_standard);
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" :
371 printf("Mode: progs.src\n");
374 util_debug("COM", "starting ...\n");
378 util_debug("COM", "cleaning ...\n");