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;
33 bool opts_dump = false;
35 static bool opts_output_wasset = false;
37 typedef struct { char *filename; int type; } argitem;
38 VECTOR_MAKE(argitem, items);
44 static const char *app_name;
47 printf("usage: %s [options] [files...]", app_name);
49 " -h, --help show this help message\n"
50 " -debug turns on compiler debug messages\n"
51 " -memchk turns on compiler memory leak check\n");
52 printf(" -o, --output=file output file, defaults to progs.dat\n"
53 " -a filename add an asm file to be assembled\n"
54 " -s filename add a progs.src file to be used\n");
55 printf(" -f<flag> enable a flag\n"
56 " -fno-<flag> disable a flag\n"
57 " -std standard select one of the following standards\n"
58 " -std=qcc original QuakeC\n"
59 " -std=fteqcc fteqcc QuakeC\n"
60 " -std=gmqcc this compiler (default)\n");
61 printf(" -W<warning> enable a warning\n"
62 " -Wno-<warning> disable a warning\n"
63 " -Wall enable all warnings\n");
66 " -fdarkplaces-string-table-bug\n"
67 " patch the string table to work with some bugged darkplaces versions\n"
69 " omits certain null-bytes for a smaller output - requires a patched engine\n"
74 static bool options_setflag_all(const char *name, bool on, uint32_t *flags, const opts_flag_def *list, size_t listsize) {
77 for (i = 0; i < listsize; ++i) {
78 if (!strcmp(name, list[i].name)) {
79 longbit lb = list[i].bit;
82 flags[lb.idx] |= (1<<(lb.bit));
84 flags[lb.idx] &= ~(1<<(lb.bit));
89 flags[0] &= ~(1<<(lb));
96 static bool options_setflag(const char *name, bool on) {
97 return options_setflag_all(name, on, opts_flags, opts_flag_list, COUNT_FLAGS);
99 static bool options_setwarn(const char *name, bool on) {
100 return options_setflag_all(name, on, opts_warn, opts_warn_list, COUNT_WARNINGS);
103 static bool options_witharg(int *argc_, char ***argv_, char **out) {
105 char **argv = *argv_;
111 /* eat up the next */
112 if (argc < 2) /* no parameter was provided */
121 static bool options_long_witharg_all(const char *optname, int *argc_, char ***argv_, char **out, int ds, bool split) {
123 char **argv = *argv_;
125 size_t len = strlen(optname);
127 if (strncmp(argv[0]+ds, optname, len))
130 /* it's --optname, check how the parameter is supplied */
131 if (argv[0][ds+len] == '=') {
132 /* using --opt=param */
133 *out = argv[0]+ds+len+1;
137 if (!split || argc < ds) /* no parameter was provided, or only single-arg form accepted */
140 /* using --opt param */
146 static bool options_long_witharg(const char *optname, int *argc_, char ***argv_, char **out) {
147 return options_long_witharg_all(optname, argc_, argv_, out, 2, true);
149 static bool options_long_gcc(const char *optname, int *argc_, char ***argv_, char **out) {
150 return options_long_witharg_all(optname, argc_, argv_, out, 1, false);
153 static bool options_parse(int argc, char **argv) {
157 while (!argend && argc > 1) {
164 if (argv[0][0] == '-') {
165 /* All gcc-type long options */
166 if (options_long_gcc("std", &argc, &argv, &argarg)) {
167 if (!strcmp(argarg, "gmqcc") || !strcmp(argarg, "default"))
168 opts_standard = COMPILER_GMQCC;
169 else if (!strcmp(argarg, "qcc"))
170 opts_standard = COMPILER_QCC;
171 else if (!strcmp(argarg, "fte") || !strcmp(argarg, "fteqcc"))
172 opts_standard = COMPILER_FTEQCC;
173 else if (!strcmp(argarg, "qccx"))
174 opts_standard = COMPILER_QCCX;
176 printf("Unknown standard: %s\n", argarg);
181 if (!strcmp(argv[0]+1, "debug")) {
185 if (!strcmp(argv[0]+1, "dump")) {
189 if (!strcmp(argv[0]+1, "memchk")) {
194 switch (argv[0][1]) {
195 /* -h, show usage but exit with 0 */
201 /* handle all -fflags */
203 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
204 if (!strcmp(argv[0]+2, "HELP")) {
205 printf("Possible flags:\n");
206 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
207 util_strtononcmd(opts_flag_list[itr].name, buffer, sizeof(buffer));
208 printf(" -f%s\n", buffer);
212 else if (!strncmp(argv[0]+2, "NO_", 3)) {
213 if (!options_setflag(argv[0]+5, false)) {
214 printf("unknown flag: %s\n", argv[0]+2);
218 else if (!options_setflag(argv[0]+2, true)) {
219 printf("unknown flag: %s\n", argv[0]+2);
224 util_strtocmd(argv[0]+2, argv[0]+2, strlen(argv[0]+2)+1);
225 if (!strcmp(argv[0]+2, "HELP")) {
226 printf("Possible warnings:\n");
227 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
228 util_strtononcmd(opts_warn_list[itr].name, buffer, sizeof(buffer));
229 printf(" -W%s\n", buffer);
233 else if (!strcmp(argv[0]+2, "ALL")) {
234 for (itr = 0; itr < sizeof(opts_warn)/sizeof(opts_warn[0]); ++itr)
235 opts_warn[itr] = 0xFFFFFFFFL;
238 if (!strncmp(argv[0]+2, "NO_", 3)) {
239 if (!options_setwarn(argv[0]+5, false)) {
240 printf("unknown warning: %s\n", argv[0]+2);
244 else if (!options_setwarn(argv[0]+2, true)) {
245 printf("unknown warning: %s\n", argv[0]+2);
251 if (!options_witharg(&argc, &argv, &argarg)) {
252 printf("option -O requires a numerical argument\n");
255 opts_O = atoi(argarg);
259 if (!options_witharg(&argc, &argv, &argarg)) {
260 printf("option -o requires an argument: the output file name\n");
263 opts_output = argarg;
264 opts_output_wasset = true;
269 item.type = argv[0][1] == 'a' ? TYPE_ASM : TYPE_SRC;
270 if (!options_witharg(&argc, &argv, &argarg)) {
271 printf("option -a requires a filename %s\n",
272 (argv[0][1] == 'a' ? "containing QC-asm" : "containing a progs.src formatted list"));
275 item.filename = argarg;
281 /* anything following -- is considered a non-option argument */
285 /* All long options without arguments */
286 else if (!strcmp(argv[0]+2, "help")) {
291 /* All long options with arguments */
292 if (options_long_witharg("output", &argc, &argv, &argarg)) {
293 opts_output = argarg;
294 opts_output_wasset = true;
296 printf("Unknown parameter: %s\n", argv[0]);
303 printf("Unknown parameter: %s\n", argv[0]);
309 /* it's a QC filename */
311 item.filename = argv[0];
319 static void options_set(uint32_t *flags, size_t idx, bool on)
321 longbit lb = LONGBIT(idx);
324 flags[lb.idx] |= (1<<(lb.bit));
326 flags[lb.idx] &= ~(1<<(lb.bit));
329 flags[0] |= (1<<(lb));
331 flags[0] &= ~(1<<(lb));
335 /* returns the line number, or -1 on error */
336 static bool progs_nextline(char **out, FILE *src)
345 len = util_getline(&line, &alen, src);
349 /* start at first non-blank */
350 for (start = line; isspace(*start); ++start) {}
351 /* end at the first non-blank */
352 for (end = start; *end && !isspace(*end); ++end) {}
355 /* move the actual filename to the beginning */
356 while (start != end) {
363 int main(int argc, char **argv) {
366 bool opts_output_free = false;
370 /* default options / warn flags */
371 options_set(opts_warn, WARN_UNKNOWN_CONTROL_SEQUENCE, true);
372 options_set(opts_warn, WARN_EXTENSIONS, true);
373 options_set(opts_warn, WARN_FIELD_REDECLARED, true);
375 if (!options_parse(argc, argv)) {
380 for (itr = 0; itr < COUNT_FLAGS; ++itr) {
381 printf("Flag %s = %i\n", opts_flag_list[itr].name, OPTS_FLAG(itr));
383 for (itr = 0; itr < COUNT_WARNINGS; ++itr) {
384 printf("Warning %s = %i\n", opts_warn_list[itr].name, OPTS_WARN(itr));
386 printf("output = %s\n", opts_output);
387 printf("optimization level = %i\n", (int)opts_O);
388 printf("standard = %i\n", opts_standard);
391 if (!parser_init()) {
392 printf("failed to initialize parser\n");
397 util_debug("COM", "starting ...\n");
399 if (items_elements) {
400 printf("Mode: manual\n");
401 printf("There are %lu items to compile:\n", (unsigned long)items_elements);
402 for (itr = 0; itr < items_elements; ++itr) {
403 printf(" item: %s (%s)\n",
404 items_data[itr].filename,
405 ( (items_data[itr].type == TYPE_QC ? "qc" :
406 (items_data[itr].type == TYPE_ASM ? "asm" :
407 (items_data[itr].type == TYPE_SRC ? "progs.src" :
410 if (!parser_compile(items_data[itr].filename)) {
416 parser_finish(opts_output);
421 printf("Mode: progs.src\n");
422 src = util_fopen("progs.src", "rb");
424 printf("failed to open `progs.src` for reading\n");
430 if (!progs_nextline(&line, src) || !line[0]) {
431 printf("illformatted progs.src file: expected output filename in first line\n");
436 if (!opts_output_wasset) {
437 opts_output = util_strdup(line);
438 opts_output_free = true;
441 while (progs_nextline(&line, src)) {
442 if (!line[0] || (line[0] == '/' && line[1] == '/'))
444 printf(" src: %s\n", line);
445 if (!parser_compile(line)) {
451 parser_finish(opts_output);
461 util_debug("COM", "cleaning ...\n");
464 if (opts_output_free)
465 mem_d((char*)opts_output);