comparison options.c @ 545:11d95002dfe1

Split option parsing logic into a separate source file.
author Rob Landley <rob@landley.net>
date Thu, 27 Dec 2007 20:53:10 -0600
parents
children 8c020de0af57
comparison
equal deleted inserted replaced
544:7fb1cf0d8a84 545:11d95002dfe1
1 /*
2 * options.c - Option parsing logic for tinycc
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
5 * Copyright (c) 2006-2007 Rob Landley
6 *
7 * Licensed under GPLv2, see file LICENSE in this tarball
8 */
9
10 #include "tcc.h"
11
12
13 void *xmalloc(unsigned long size);
14 void dynarray_add(void ***ptab, int *nb_ptr, void *data);
15 void add_dynarray_path(TCCState *s, char *pathname, struct dynarray *dd);
16 int strstart(char *str, char *val, char **ptr);
17 void warning(char *fmt, ...);
18 int init_output_type(TCCState *s);
19
20 extern char *tinycc_path;
21 int do_bounds_check = 0;
22 int do_debug = 0;
23 int next_tok_flags;
24
25 // Benchmark info
26 int do_bench = 0;
27 int total_lines;
28 int total_bytes;
29 int tok_ident;
30
31 // inlines
32 int is_space(int ch);
33
34
35 #define WD_ALL 0x0001 /* warning is activated when using -Wall */
36 #define FD_INVERT 0x0002 /* invert value before storing */
37
38 typedef struct FlagDef {
39 uint16_t offset;
40 uint16_t flags;
41 char *name;
42 } FlagDef;
43
44 static FlagDef warning_defs[] = {
45 { offsetof(TCCState, warn_unsupported), 0, "unsupported" },
46 { offsetof(TCCState, warn_write_strings), 0, "write-strings" },
47 { offsetof(TCCState, warn_error), 0, "error" },
48 { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL,
49 "implicit-function-declaration" },
50 };
51
52 static int set_flag(TCCState *s, FlagDef *flags, int nb_flags,
53 char *name, int value)
54 {
55 int i;
56 FlagDef *p;
57 char *r;
58
59 r = name;
60 if (r[0] == 'n' && r[1] == 'o' && r[2] == '-') {
61 r += 3;
62 value = !value;
63 }
64 for(i = 0, p = flags; i < nb_flags; i++, p++) {
65 if (!strcmp(r, p->name))
66 goto found;
67 }
68 return -1;
69 found:
70 if (p->flags & FD_INVERT)
71 value = !value;
72 *(int *)((uint8_t *)s + p->offset) = value;
73 return 0;
74 }
75
76
77 /* set/reset a warning */
78 int tcc_set_warning(TCCState *s, char *warning_name, int value)
79 {
80 int i;
81 FlagDef *p;
82
83 if (!strcmp(warning_name, "all")) {
84 for(i = 0, p = warning_defs; i < countof(warning_defs); i++, p++) {
85 if (p->flags & WD_ALL)
86 *(int *)((uint8_t *)s + p->offset) = 1;
87 }
88 return 0;
89 } else {
90 return set_flag(s, warning_defs, countof(warning_defs),
91 warning_name, value);
92 }
93 }
94
95 static FlagDef flag_defs[] = {
96 { offsetof(TCCState, char_is_unsigned), 0, "unsigned-char" },
97 { offsetof(TCCState, char_is_unsigned), FD_INVERT, "signed-char" },
98 { offsetof(TCCState, nocommon), FD_INVERT, "common" },
99 { offsetof(TCCState, leading_underscore), 0, "leading-underscore" },
100 };
101
102 /* set/reset a flag */
103 int tcc_set_flag(TCCState *s, char *flag_name, int value)
104 {
105 return set_flag(s, flag_defs, countof(flag_defs),
106 flag_name, value);
107 }
108
109 /* extract the basename of a file */
110 static char *tcc_basename(char *name)
111 {
112 char *p;
113 p = strrchr(name, '/');
114 #ifdef WIN32
115 if (!p)
116 p = strrchr(name, '\\');
117 #endif
118 if (!p)
119 p = name;
120 else
121 p++;
122 return p;
123 }
124
125 #if !defined(LIBTCC)
126
127 static int64_t getclock_us(void)
128 {
129 #ifdef WIN32
130 struct _timeb tb;
131 _ftime(&tb);
132 return (tb.time * 1000LL + tb.millitm) * 1000LL;
133 #else
134 struct timeval tv;
135 gettimeofday(&tv, NULL);
136 return tv.tv_sec * 1000000LL + tv.tv_usec;
137 #endif
138 }
139
140 void show_version(void)
141 {
142 printf("tinycc version " TINYCC_VERSION "\n");
143 }
144
145 void help(TCCState *s)
146 {
147 show_version();
148 printf("Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard, 2007 Rob Landley\n"
149 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
150 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-static]\n"
151 " [infile1 infile2...] [-run infile args...]\n"
152 "\n"
153 "General options:\n"
154 " -v Verbose compile, repeat for more verbosity\n"
155 " -c compile only - generate an object file\n"
156 " -o outfile set output filename\n"
157 " -Bdir set tcc internal library path\n"
158 " -bench output compilation statistics\n"
159 " -run run compiled source\n"
160 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
161 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
162 " -w disable all warnings\n"
163 "Preprocessor options:\n"
164 " -Idir add include path 'dir'\n"
165 " -Dsym[=val] define 'sym' with value 'val'\n"
166 " -Usym undefine 'sym'\n"
167 " -E preprocess only\n"
168 "Linker options:\n"
169 " -Ldir add library path 'dir'\n"
170 " -llib link with dynamic or static library 'lib'\n"
171 " -shared generate a shared library\n"
172 " -static static linking\n"
173 " -rdynamic export all global symbols to dynamic linker\n"
174 " -r output relocatable .o file\n"
175 "Debugger options:\n"
176 " -g generate runtime debug info\n"
177 #ifdef CONFIG_TCC_BCHECK
178 " -b compile with built-in memory and bounds checker (implies -g)\n"
179 #endif
180 );
181 }
182
183 #define TCC_OPTION_HAS_ARG 0x0001
184 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
185
186 typedef struct TCCOption {
187 char *name;
188 uint16_t index;
189 uint16_t flags;
190 } TCCOption;
191
192 enum {
193 TCC_OPTION_HELP,
194 TCC_OPTION_I,
195 TCC_OPTION_D,
196 TCC_OPTION_E,
197 TCC_OPTION_U,
198 TCC_OPTION_L,
199 TCC_OPTION_B,
200 TCC_OPTION_l,
201 TCC_OPTION_bench,
202 TCC_OPTION_b,
203 TCC_OPTION_g,
204 TCC_OPTION_c,
205 TCC_OPTION_static,
206 TCC_OPTION_shared,
207 TCC_OPTION_o,
208 TCC_OPTION_r,
209 TCC_OPTION_Wl,
210 TCC_OPTION_W,
211 TCC_OPTION_O,
212 TCC_OPTION_m,
213 TCC_OPTION_f,
214 TCC_OPTION_nostdinc,
215 TCC_OPTION_nostdlib,
216 TCC_OPTION_print_search_dirs,
217 TCC_OPTION_rdynamic,
218 TCC_OPTION_run,
219 TCC_OPTION_v,
220 TCC_OPTION_w,
221 TCC_OPTION_pipe,
222 };
223
224 static TCCOption tcc_options[] = {
225 { "h", TCC_OPTION_HELP, 0 },
226 { "?", TCC_OPTION_HELP, 0 },
227 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
228 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
229 { "E", TCC_OPTION_E, 0 },
230 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
231 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
232 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
233 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
234 { "bench", TCC_OPTION_bench, 0 },
235 #ifdef CONFIG_TCC_BCHECK
236 { "b", TCC_OPTION_b, 0 },
237 #endif
238 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
239 { "c", TCC_OPTION_c, 0 },
240 { "static", TCC_OPTION_static, 0 },
241 { "shared", TCC_OPTION_shared, 0 },
242 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
243 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
244 { "rdynamic", TCC_OPTION_rdynamic, 0 },
245 { "r", TCC_OPTION_r, 0 },
246 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
247 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
248 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
249 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
250 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
251 { "nostdinc", TCC_OPTION_nostdinc, 0 },
252 { "nostdlib", TCC_OPTION_nostdlib, 0 },
253 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
254 { "v", TCC_OPTION_v, 0 },
255 { "w", TCC_OPTION_w, 0 },
256 { "pipe", TCC_OPTION_pipe, 0},
257 { NULL },
258 };
259
260 /* convert 'str' into an array of space separated strings */
261 static int expand_args(char ***pargv, char *str)
262 {
263 char *s1;
264 char **argv, *arg;
265 int argc, len;
266
267 argc = 0;
268 argv = NULL;
269 for(;;) {
270 while (is_space(*str))
271 str++;
272 if (*str == '\0')
273 break;
274 s1 = str;
275 while (*str != '\0' && !is_space(*str))
276 str++;
277 len = str - s1;
278 arg = xmalloc(len + 1);
279 memcpy(arg, s1, len);
280 arg[len] = '\0';
281 dynarray_add((void ***)&argv, &argc, arg);
282 }
283 *pargv = argv;
284 return argc;
285 }
286
287 static char **files;
288 static int nb_files, nb_libraries;
289 static int multiple_files;
290 static int print_search_dirs;
291 static int reloc_output;
292 static char *outfile;
293
294 int parse_args(TCCState *s, int argc, char **argv)
295 {
296 int optind;
297 TCCOption *popt;
298 char *optarg, *p1, *r1;
299 char *r;
300
301 optind = 0;
302 while (1) {
303 if (optind >= argc) {
304 if (nb_files == 0 && !print_search_dirs) {
305 if (!s->verbose) help(s);
306 exit(1);
307 } else break;
308 }
309 r = argv[optind++];
310 if (r[0] != '-') {
311 /* add a new file */
312 dynarray_add((void ***)&files, &nb_files, r);
313 if (!multiple_files) {
314 optind--;
315 /* argv[0] will be this file */
316 break;
317 }
318 } else {
319 /* find option in table (match only the first chars */
320 popt = tcc_options;
321 for(;;) {
322 p1 = popt->name;
323 if (p1 == NULL)
324 error("invalid option -- '%s'", r);
325 r1 = r + 1;
326 for(;;) {
327 if (*p1 == '\0')
328 goto option_found;
329 if (*r1 != *p1)
330 break;
331 p1++;
332 r1++;
333 }
334 popt++;
335 }
336 option_found:
337 if (popt->flags & TCC_OPTION_HAS_ARG) {
338 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
339 optarg = r1;
340 } else {
341 if (optind >= argc)
342 error("argument to '%s' is missing", r);
343 optarg = argv[optind++];
344 }
345 } else {
346 if (*r1 != '\0') {
347 help(s);
348 exit(1);
349 }
350 optarg = NULL;
351 }
352
353 switch(popt->index) {
354 case TCC_OPTION_HELP:
355 help(s);
356 exit(1);
357 case TCC_OPTION_I:
358 add_dynarray_path(s, optarg, &(s->include_paths));
359 break;
360 case TCC_OPTION_D:
361 {
362 char *sym, *value;
363 sym = (char *)optarg;
364 value = strchr(sym, '=');
365 if (value) {
366 *value = '\0';
367 value++;
368 }
369 tcc_define_symbol(s, sym, value);
370 }
371 break;
372 case TCC_OPTION_E:
373 s->output_type = TCC_OUTPUT_PREPROCESS;
374 break;
375 case TCC_OPTION_U:
376 tcc_undefine_symbol(s, optarg);
377 break;
378 case TCC_OPTION_L:
379 add_dynarray_path(s, optarg, &(s->library_paths));
380 break;
381 case TCC_OPTION_B:
382 /* set tcc utilities path (mainly for tcc development) */
383 tinycc_path = optarg;
384 break;
385 case TCC_OPTION_l:
386 dynarray_add((void ***)&files, &nb_files, r);
387 nb_libraries++;
388 break;
389 case TCC_OPTION_bench:
390 do_bench = 1;
391 break;
392 #ifdef CONFIG_TCC_BCHECK
393 case TCC_OPTION_b:
394 do_bounds_check = 1;
395 do_debug = 1;
396 break;
397 #endif
398 case TCC_OPTION_g:
399 do_debug = 1;
400 break;
401 case TCC_OPTION_c:
402 multiple_files = 1;
403 s->output_type = TCC_OUTPUT_OBJ;
404 break;
405 case TCC_OPTION_static:
406 s->static_link = 1;
407 break;
408 case TCC_OPTION_shared:
409 s->output_type = TCC_OUTPUT_DLL;
410 break;
411 case TCC_OPTION_o:
412 multiple_files = 1;
413 outfile = optarg;
414 break;
415 case TCC_OPTION_r:
416 /* generate a .o merging several output files */
417 reloc_output = 1;
418 s->output_type = TCC_OUTPUT_OBJ;
419 break;
420 case TCC_OPTION_nostdinc:
421 s->nostdinc = 1;
422 break;
423 case TCC_OPTION_nostdlib:
424 s->nostdlib = 1;
425 break;
426 case TCC_OPTION_print_search_dirs:
427 print_search_dirs = 1;
428 break;
429 case TCC_OPTION_run:
430 {
431 int argc1;
432 char **argv1;
433 argc1 = expand_args(&argv1, optarg);
434 if (argc1 > 0) {
435 parse_args(s, argc1, argv1);
436 }
437 multiple_files = 0;
438 s->output_type = TCC_OUTPUT_MEMORY;
439 }
440 break;
441 case TCC_OPTION_v:
442 if (!s->verbose++) show_version();
443 break;
444 case TCC_OPTION_f:
445 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
446 goto unsupported_option;
447 break;
448 case TCC_OPTION_W:
449 if (tcc_set_warning(s, optarg, 1) < 0 &&
450 s->warn_unsupported)
451 goto unsupported_option;
452 break;
453 case TCC_OPTION_w:
454 s->warn_none = 1;
455 break;
456 case TCC_OPTION_rdynamic:
457 s->rdynamic = 1;
458 break;
459 case TCC_OPTION_Wl:
460 {
461 char *p;
462 if (strstart(optarg, "-Ttext,", &p)) {
463 s->text_addr = strtoul(p, NULL, 16);
464 s->has_text_addr = 1;
465 } else if (strstart(optarg, "--oformat,", &p)) {
466 if (strstart(p, "elf32-", NULL)) {
467 s->output_format = TCC_OUTPUT_FORMAT_ELF;
468 } else if (!strcmp(p, "binary")) {
469 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
470 } else
471 #ifdef TCC_TARGET_COFF
472 if (!strcmp(p, "coff")) {
473 s->output_format = TCC_OUTPUT_FORMAT_COFF;
474 } else
475 #endif
476 {
477 error("target %s not found", p);
478 }
479 } else {
480 error("unsupported linker option '%s'", optarg);
481 }
482 }
483 break;
484 default:
485 if (s->warn_unsupported) {
486 unsupported_option:
487 warning("unsupported option '%s'", r);
488 }
489 break;
490 }
491 }
492 }
493 return optind;
494 }
495
496 int main(int argc, char **argv)
497 {
498 int i;
499 TCCState *s;
500 int nb_objfiles, ret, optind;
501 char objfilename[1024];
502 int64_t start_time = 0;
503
504 s = tcc_new();
505 s->output_type = TCC_OUTPUT_EXE;
506 outfile = NULL;
507 multiple_files = 1;
508 files = NULL;
509 nb_files = 0;
510 nb_libraries = 0;
511 reloc_output = 0;
512 print_search_dirs = 0;
513
514 #ifdef WIN32
515 /* on win32, we suppose the lib and includes are at the location
516 of 'tcc.exe' */
517 {
518 static char path[1024];
519 char *p, *d;
520
521 GetModuleFileNameA(NULL, path, sizeof path);
522 p = d = strlwr(path);
523 while (*d)
524 {
525 if (*d == '\\') *d = '/', p = d;
526 ++d;
527 }
528 *p = '\0';
529 tinycc_path = path;
530 }
531 #else
532 tinycc_path = TINYCC_INSTALLDIR;
533 #endif
534
535 optind = parse_args(s, argc - 1, argv + 1) + 1;
536
537 /* Just enough for the Linux kernel, which is hardwired to use a directory
538 named "include" beneath this output value for the compiler headers.*/
539 if (print_search_dirs) {
540 printf("install: %s/\n", tinycc_path);
541 return 0;
542 }
543
544 nb_objfiles = nb_files - nb_libraries;
545
546 // if outfile provided without other options, we output an executable
547 if (outfile && s->output_type == TCC_OUTPUT_MEMORY)
548 s->output_type = TCC_OUTPUT_EXE;
549
550 // check -c consistency : only single file handled. XXX: checks file type
551 if (s->output_type == TCC_OUTPUT_OBJ && !reloc_output) {
552 /* accepts only a single input file */
553 if (nb_objfiles != 1)
554 error("cannot specify multiple files with -c");
555 if (nb_libraries != 0)
556 error("cannot specify libraries with -c");
557 }
558
559 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
560 if (!outfile) s->outfile = stdout;
561 else {
562 s->outfile = fopen(outfile, "wb");
563 if (!s->outfile) error("could not open '%s'", outfile);
564 }
565 } else if (s->output_type != TCC_OUTPUT_MEMORY) {
566 if (!outfile) {
567 /* compute default outfile name */
568 pstrcpy(objfilename, sizeof(objfilename) - 1,
569 /* strip path */
570 tcc_basename(files[0]));
571 #ifdef TCC_TARGET_PE
572 pe_guess_outfile(objfilename, s->output_type);
573 #else
574 if (s->output_type == TCC_OUTPUT_OBJ && !reloc_output) {
575 char *ext = strrchr(objfilename, '.');
576 if (!ext)
577 goto default_outfile;
578 /* add .o extension */
579 strcpy(ext + 1, "o");
580 } else {
581 default_outfile:
582 pstrcpy(objfilename, sizeof(objfilename), "a.out");
583 }
584 #endif
585 outfile = objfilename;
586 }
587 }
588
589 if (do_bench) {
590 start_time = getclock_us();
591 }
592
593 init_output_type(s);
594
595 /* compile or add each files or library */
596 for(i = 0;i < nb_files; i++) {
597 char *filename;
598
599 next_tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF | TOK_FLAG_BOW;
600
601 filename = files[i];
602 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
603 tcc_add_file_internal(s, filename,
604 AFF_PRINT_ERROR | AFF_PREPROCESS);
605 } else if (filename[0] == '-') {
606 if (tcc_add_library(s, filename + 2) < 0)
607 error("cannot find %s", filename);
608 } else if (tcc_add_file(s, filename) < 0) {
609 ret = 1;
610 goto the_end;
611 }
612 }
613
614 /* free all files */
615 free(files);
616
617 if (do_bench) {
618 double total_time;
619 total_time = (double)(getclock_us() - start_time) / 1000000.0;
620 if (total_time < 0.001)
621 total_time = 0.001;
622 if (total_bytes < 1)
623 total_bytes = 1;
624 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
625 tok_ident - TOK_IDENT, total_lines, total_bytes,
626 total_time, (int)(total_lines / total_time),
627 total_bytes / total_time / 1000000.0);
628 }
629
630 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
631 if (outfile) fclose(s->outfile);
632 ret = 0;
633 } else if (s->output_type == TCC_OUTPUT_MEMORY) {
634 ret = tcc_run(s, argc - optind, argv + optind);
635 } else
636 #ifdef TCC_TARGET_PE
637 if (s->output_type != TCC_OUTPUT_OBJ) {
638 ret = tcc_output_pe(s, outfile);
639 } else
640 #endif
641 {
642 ret = tcc_output_file(s, outfile) ? 1 : 0;
643 }
644 the_end:
645 /* XXX: cannot do it with bound checking because of the malloc hooks */
646 if (!do_bounds_check)
647 tcc_delete(s);
648
649 return ret;
650 }
651
652 #endif