]> git.xonotic.org Git - xonotic/gmqcc.git/blob - main.c
AUTHORS
[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 // todo CLEANUP this argitem thing
25 typedef struct { char *name, type; } argitem;
26 VECTOR_MAKE(argitem, items);
27
28 /* global options */
29 int opts_debug                     = 0;
30 int opts_memchk                    = 0;
31 int opts_compiler                  = COMPILER_GMQCC;
32 int opts_darkplaces_stringtablebug = 0;
33 int opts_omit_nullcode             = 0;
34
35 static const int usage(const char *const app) {
36     printf("usage:\n");
37     printf("    %s -c<file>          -oprog.dat -- compile file\n"     , app);
38     printf("    %s -a<file>          -oprog.dat -- assemble file\n"    , app);
39     printf("    %s -c<file> -i<file> -oprog.dat -- compile together (allowed multiple -i<file>)\n" , app);
40     printf("    %s -a<file> -i<file> -oprog.dat -- assemble together(allowed multiple -i<file>)\n", app);
41     printf("    example:\n");
42     printf("    %s -cfoo.qc -ibar.qc -oqc.dat -afoo.qs -ibar.qs -oqs.dat\n", app);
43     printf("    additional flags:\n");
44     printf("        -debug           -- turns on compiler debug messages\n");
45     printf("        -memchk          -- turns on compiler memory leak check\n");
46     printf("        -help            -- prints this help/usage text\n");
47     printf("        -std             -- select the QuakeC compile type (types below):\n");
48     printf("            -std=qcc     -- original QuakeC\n");
49     printf("            -std=ftqecc  -- fteqcc QuakeC\n");
50     printf("            -std=qccx    -- qccx QuakeC\n");
51     printf("            -std=gmqcc   -- this compiler QuakeC (default selection)\n");
52     printf("    codegen flags:\n");
53     printf("        -fdarkplaces-string-table-bug -- patches the string table to work with bugged versions of darkplaces\n");
54     printf("        -fomit-nullcode               -- omits the generation of null code (will break everywhere see propsal.txt)\n");
55     return -1;
56 }
57
58 int main(int argc, char **argv) {
59     size_t itr = 0;
60     char  *app = &argv[0][0];
61     FILE  *fpp = NULL;
62
63     /*
64      * Parse all command line arguments.  This is rather annoying to do
65      * because of all tiny corner cases.
66      */
67     if (argc <= 1 || (argv[1][0] != '-'))
68         return usage(app);
69
70     while ((argc > 1) && argv[1][0] == '-') {
71         switch (argv[1][1]) {
72             case 'c': items_add((argitem){util_strdup(&argv[1][2]), 0}); break; /* compile  */ 
73             case 'a': items_add((argitem){util_strdup(&argv[1][2]), 1}); break; /* assemble */
74             case 'i': items_add((argitem){util_strdup(&argv[1][2]), 2}); break; /* includes */
75             default:
76                 if (!strncmp(&argv[1][1], "debug" , 5)) { opts_debug  = 1; break; }
77                 if (!strncmp(&argv[1][1], "memchk", 6)) { opts_memchk = 1; break; }
78                 if (!strncmp(&argv[1][1], "help",   4)) {
79                     return usage(app);
80                     break;
81                 }
82                 /* compiler type selection */
83                 if (!strncmp(&argv[1][1], "std=qcc"   , 7 )) { opts_compiler = COMPILER_QCC;    break; }
84                 if (!strncmp(&argv[1][1], "std=fteqcc", 10)) { opts_compiler = COMPILER_FTEQCC; break; }
85                 if (!strncmp(&argv[1][1], "std=qccx",   8 )) { opts_compiler = COMPILER_QCCX;   break; }
86                 if (!strncmp(&argv[1][1], "std=gmqcc",  9 )) { opts_compiler = COMPILER_GMQCC;  break; }
87                 if (!strncmp(&argv[1][1], "std=",       4 )) {
88                     printf("invalid std selection, supported types:\n");
89                     printf("    -std=qcc     -- original QuakeC\n");
90                     printf("    -std=ftqecc  -- fteqcc QuakeC\n");
91                     printf("    -std=qccx    -- qccx QuakeC\n");
92                     printf("    -std=gmqcc   -- this compiler QuakeC (default selection)\n");
93                     return 0;
94                 }
95
96                 /* code specific switches */
97                 if (!strcmp(&argv[1][1], "fdarkplaces-stringtablebug")) {
98                     opts_darkplaces_stringtablebug = 1;
99                     break;
100                 }
101                 if (!strcmp(&argv[1][1], "fomit-nullcode")) {
102                     opts_omit_nullcode = 1;
103                     break;
104                 }
105                 return usage(app);
106                 
107         }
108         ++argv;
109         --argc;
110     }
111
112     /*
113      * options could depend on another option, this is where option
114      * validity checking like that would take place.
115      */
116     if (opts_memchk && !opts_debug) 
117         printf("Warning: cannot enable -memchk, without -debug.\n");
118
119     util_debug("COM", "starting ...\n");
120     /* multi file multi path compilation system */
121     for (; itr < items_elements; itr++) {
122         switch (items_data[itr].type) {
123             case 0:
124                 fpp = fopen(items_data[itr].name, "r");
125                 struct lex_file *lex = lex_open(fpp);
126                 parse_gen(lex);
127                 lex_close(lex);
128                 break;
129             case 1:
130                 asm_init (items_data[itr].name, &fpp);
131                 asm_parse(fpp);
132                 asm_close(fpp);
133                 break;
134         }
135     }
136
137     util_debug("COM", "cleaning ...\n"); 
138     /* clean list */
139     for (itr = 0; itr < items_elements; itr++)
140         mem_d(items_data[itr].name);
141     mem_d(items_data);
142
143     util_meminfo();
144     return 0;
145 }