tcc --help: update option summary
[tinycc.git] / tcc.c
blob7fabe8b7928e81f2ed19b4ab5778cdd26260d44e
1 /*
2 * TCC - Tiny C Compiler
3 *
4 * Copyright (c) 2001-2004 Fabrice Bellard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #ifdef ONE_SOURCE
22 #include "libtcc.c"
23 #else
24 #include "tcc.h"
25 #endif
27 static char **files;
28 static int nb_files, nb_libraries;
29 static int multiple_files;
30 static int print_search_dirs;
31 static int output_type;
32 static int reloc_output;
33 static char *outfile;
34 static int do_bench = 0;
35 static int gen_deps;
36 static const char *deps_outfile;
37 static const char *m_option;
38 static CString linker_arg;
40 #define TCC_OPTION_HAS_ARG 0x0001
41 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
43 static void help(void)
45 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
46 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
47 " tcc [options...] -run infile [arguments...]\n"
48 "General options:\n"
49 " -c compile only - generate an object file\n"
50 " -o outfile set output filename\n"
51 " -run run compiled source\n"
52 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
53 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
54 " -w disable all warnings\n"
55 " -v show version\n"
56 " -vv show included files (as sole argument: show search paths)\n"
57 " -bench show compilation statistics\n"
58 "Preprocessor options:\n"
59 " -E preprocess only\n"
60 " -Idir add include path 'dir'\n"
61 " -Dsym[=val] define 'sym' with value 'val'\n"
62 " -Usym undefine 'sym'\n"
63 " -nostdinc do not use standard system include paths\n"
64 "Linker options:\n"
65 " -Ldir add library path 'dir'\n"
66 " -llib link with dynamic or static library 'lib'\n"
67 " -Bdir use 'dir' as tcc internal library and include path\n"
68 " -nostdlib do not link with standard crt and libraries\n"
69 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
70 " -r generate (relocatable) object file\n"
71 " -rdynamic export all global symbols to dynamic linker\n"
72 " -shared generate a shared library\n"
73 " -soname set name for shared library to be used at runtime\n"
74 " -static static linking\n"
75 " -Wl,opt[=val] set linker option 'opt' (see manual)\n"
76 "Debugger options:\n"
77 " -g generate runtime debug info\n"
78 #ifdef CONFIG_TCC_BCHECK
79 " -b compile with built-in memory and bounds checker (implies -g)\n"
80 #endif
81 #ifdef CONFIG_TCC_BACKTRACE
82 " -bt N show N callers in stack traces\n"
83 #endif
84 "Misc options:\n"
85 " -MD generate target dependencies for make\n"
86 " -MF depfile put generated dependencies here\n"
90 typedef struct TCCOption {
91 const char *name;
92 uint16_t index;
93 uint16_t flags;
94 } TCCOption;
96 enum {
97 TCC_OPTION_HELP,
98 TCC_OPTION_I,
99 TCC_OPTION_D,
100 TCC_OPTION_U,
101 TCC_OPTION_L,
102 TCC_OPTION_B,
103 TCC_OPTION_l,
104 TCC_OPTION_bench,
105 TCC_OPTION_bt,
106 TCC_OPTION_b,
107 TCC_OPTION_g,
108 TCC_OPTION_c,
109 TCC_OPTION_static,
110 TCC_OPTION_shared,
111 TCC_OPTION_soname,
112 TCC_OPTION_o,
113 TCC_OPTION_r,
114 TCC_OPTION_s,
115 TCC_OPTION_Wl,
116 TCC_OPTION_W,
117 TCC_OPTION_O,
118 TCC_OPTION_m,
119 TCC_OPTION_f,
120 TCC_OPTION_isystem,
121 TCC_OPTION_nostdinc,
122 TCC_OPTION_nostdlib,
123 TCC_OPTION_print_search_dirs,
124 TCC_OPTION_rdynamic,
125 TCC_OPTION_pedantic,
126 TCC_OPTION_pthread,
127 TCC_OPTION_run,
128 TCC_OPTION_v,
129 TCC_OPTION_w,
130 TCC_OPTION_pipe,
131 TCC_OPTION_E,
132 TCC_OPTION_MD,
133 TCC_OPTION_MF,
134 TCC_OPTION_x,
137 static const TCCOption tcc_options[] = {
138 { "h", TCC_OPTION_HELP, 0 },
139 { "-help", TCC_OPTION_HELP, 0 },
140 { "?", TCC_OPTION_HELP, 0 },
141 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
142 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
143 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
144 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
145 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
146 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
147 { "bench", TCC_OPTION_bench, 0 },
148 #ifdef CONFIG_TCC_BACKTRACE
149 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
150 #endif
151 #ifdef CONFIG_TCC_BCHECK
152 { "b", TCC_OPTION_b, 0 },
153 #endif
154 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
155 { "c", TCC_OPTION_c, 0 },
156 { "static", TCC_OPTION_static, 0 },
157 { "shared", TCC_OPTION_shared, 0 },
158 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
159 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
160 { "pedantic", TCC_OPTION_pedantic, 0},
161 { "pthread", TCC_OPTION_pthread, 0},
162 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
163 { "rdynamic", TCC_OPTION_rdynamic, 0 },
164 { "r", TCC_OPTION_r, 0 },
165 { "s", TCC_OPTION_s, 0 },
166 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
167 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
168 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
169 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
170 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
171 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
172 { "nostdinc", TCC_OPTION_nostdinc, 0 },
173 { "nostdlib", TCC_OPTION_nostdlib, 0 },
174 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
175 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
176 { "w", TCC_OPTION_w, 0 },
177 { "pipe", TCC_OPTION_pipe, 0},
178 { "E", TCC_OPTION_E, 0},
179 { "MD", TCC_OPTION_MD, 0},
180 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
181 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
182 { NULL, 0, 0 },
185 static int64_t getclock_us(void)
187 #ifdef _WIN32
188 struct _timeb tb;
189 _ftime(&tb);
190 return (tb.time * 1000LL + tb.millitm) * 1000LL;
191 #else
192 struct timeval tv;
193 gettimeofday(&tv, NULL);
194 return tv.tv_sec * 1000000LL + tv.tv_usec;
195 #endif
198 /* convert 'str' into an array of space separated strings */
199 static int expand_args(char ***pargv, const char *str)
201 const char *s1;
202 char **argv, *arg;
203 int argc, len;
205 argc = 0;
206 argv = NULL;
207 for(;;) {
208 while (is_space(*str))
209 str++;
210 if (*str == '\0')
211 break;
212 s1 = str;
213 while (*str != '\0' && !is_space(*str))
214 str++;
215 len = str - s1;
216 arg = tcc_malloc(len + 1);
217 memcpy(arg, s1, len);
218 arg[len] = '\0';
219 dynarray_add((void ***)&argv, &argc, arg);
221 *pargv = argv;
222 return argc;
225 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
226 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
227 #ifdef _WIN32
228 #include <process.h>
229 static int execvp_win32(const char *prog, char **argv)
231 int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv);
232 if (-1 == ret)
233 return ret;
234 cwait(&ret, ret, WAIT_CHILD);
235 exit(ret);
237 #define execvp execvp_win32
238 #endif
239 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
241 char child_path[4096], *child_name; const char *target;
242 switch (atoi(optarg)) {
243 #ifdef TCC_TARGET_I386
244 case 32: break;
245 case 64: target = "x86_64";
246 #else
247 case 64: break;
248 case 32: target = "i386";
249 #endif
250 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
251 child_name = tcc_basename(child_path);
252 strcpy(child_name, target);
253 #ifdef TCC_TARGET_PE
254 strcat(child_name, "-win32");
255 #endif
256 strcat(child_name, "-tcc");
257 if (strcmp(argv[0], child_path)) {
258 if (s->verbose > 0)
259 printf("tcc: using '%s'\n", child_name), fflush(stdout);
260 execvp(argv[0] = child_path, argv);
262 tcc_error("'%s' not found", child_name);
263 case 0: /* ignore -march etc. */
264 break;
265 default:
266 tcc_warning("unsupported option \"-m%s\"", optarg);
269 #endif
271 static void parse_option_D(TCCState *s1, const char *optarg)
273 char *sym = tcc_strdup(optarg);
274 char *value = strchr(sym, '=');
275 if (value)
276 *value++ = '\0';
277 tcc_define_symbol(s1, sym, value);
278 tcc_free(sym);
281 static int parse_args(TCCState *s, int argc, char **argv)
283 int optind;
284 const TCCOption *popt;
285 const char *optarg, *p1, *r1;
286 char *r;
287 int was_pthread;
289 was_pthread = 0; /* is set if commandline contains -pthread key */
290 optind = 0;
291 cstr_new(&linker_arg);
293 while (optind < argc) {
295 r = argv[optind++];
296 if (r[0] != '-' || r[1] == '\0') {
297 /* add a new file */
298 dynarray_add((void ***)&files, &nb_files, r);
299 if (!multiple_files) {
300 optind--;
301 /* argv[0] will be this file */
302 break;
304 } else {
305 /* find option in table (match only the first chars */
306 popt = tcc_options;
307 for(;;) {
308 p1 = popt->name;
309 if (p1 == NULL)
310 tcc_error("invalid option -- '%s'", r);
311 r1 = r + 1;
312 for(;;) {
313 if (*p1 == '\0')
314 goto option_found;
315 if (*r1 != *p1)
316 break;
317 p1++;
318 r1++;
320 popt++;
322 option_found:
323 if (popt->flags & TCC_OPTION_HAS_ARG) {
324 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
325 optarg = r1;
326 } else {
327 if (optind >= argc)
328 tcc_error("argument to '%s' is missing", r);
329 optarg = argv[optind++];
331 } else {
332 if (*r1 != '\0')
333 return 0;
334 optarg = NULL;
337 switch(popt->index) {
338 case TCC_OPTION_HELP:
339 return 0;
341 case TCC_OPTION_I:
342 if (tcc_add_include_path(s, optarg) < 0)
343 tcc_error("too many include paths");
344 break;
345 case TCC_OPTION_D:
346 parse_option_D(s, optarg);
347 break;
348 case TCC_OPTION_U:
349 tcc_undefine_symbol(s, optarg);
350 break;
351 case TCC_OPTION_L:
352 tcc_add_library_path(s, optarg);
353 break;
354 case TCC_OPTION_B:
355 /* set tcc utilities path (mainly for tcc development) */
356 tcc_set_lib_path(s, optarg);
357 break;
358 case TCC_OPTION_l:
359 dynarray_add((void ***)&files, &nb_files, r);
360 nb_libraries++;
361 break;
362 case TCC_OPTION_pthread:
363 was_pthread = 1;
364 parse_option_D(s, "_REENTRANT");
365 break;
366 case TCC_OPTION_bench:
367 do_bench = 1;
368 break;
369 #ifdef CONFIG_TCC_BACKTRACE
370 case TCC_OPTION_bt:
371 tcc_set_num_callers(atoi(optarg));
372 break;
373 #endif
374 #ifdef CONFIG_TCC_BCHECK
375 case TCC_OPTION_b:
376 s->do_bounds_check = 1;
377 s->do_debug = 1;
378 break;
379 #endif
380 case TCC_OPTION_g:
381 s->do_debug = 1;
382 break;
383 case TCC_OPTION_c:
384 multiple_files = 1;
385 output_type = TCC_OUTPUT_OBJ;
386 break;
387 case TCC_OPTION_static:
388 s->static_link = 1;
389 break;
390 case TCC_OPTION_shared:
391 output_type = TCC_OUTPUT_DLL;
392 break;
393 case TCC_OPTION_soname:
394 s->soname = optarg;
395 break;
396 case TCC_OPTION_m:
397 m_option = optarg;
398 break;
399 case TCC_OPTION_o:
400 multiple_files = 1;
401 outfile = tcc_strdup(optarg);
402 break;
403 case TCC_OPTION_r:
404 /* generate a .o merging several output files */
405 reloc_output = 1;
406 output_type = TCC_OUTPUT_OBJ;
407 break;
408 case TCC_OPTION_isystem:
409 tcc_add_sysinclude_path(s, optarg);
410 break;
411 case TCC_OPTION_nostdinc:
412 s->nostdinc = 1;
413 break;
414 case TCC_OPTION_nostdlib:
415 s->nostdlib = 1;
416 break;
417 case TCC_OPTION_print_search_dirs:
418 print_search_dirs = 1;
419 break;
420 case TCC_OPTION_run:
422 int argc1;
423 char **argv1;
424 argc1 = expand_args(&argv1, optarg);
425 if (argc1 > 0) {
426 parse_args(s, argc1, argv1);
428 multiple_files = 0;
429 output_type = TCC_OUTPUT_MEMORY;
430 break;
432 case TCC_OPTION_v:
433 do ++s->verbose; while (*optarg++ == 'v');
434 break;
435 case TCC_OPTION_f:
436 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
437 goto unsupported_option;
438 break;
439 case TCC_OPTION_W:
440 if (tcc_set_warning(s, optarg, 1) < 0 &&
441 s->warn_unsupported)
442 goto unsupported_option;
443 break;
444 case TCC_OPTION_w:
445 s->warn_none = 1;
446 break;
447 case TCC_OPTION_rdynamic:
448 s->rdynamic = 1;
449 break;
450 case TCC_OPTION_Wl:
451 if (linker_arg.size)
452 --linker_arg.size, cstr_ccat(&linker_arg, ',');
453 cstr_cat(&linker_arg, optarg);
454 cstr_ccat(&linker_arg, '\0');
455 break;
456 case TCC_OPTION_E:
457 output_type = TCC_OUTPUT_PREPROCESS;
458 break;
459 case TCC_OPTION_MD:
460 gen_deps = 1;
461 break;
462 case TCC_OPTION_MF:
463 deps_outfile = optarg;
464 break;
465 case TCC_OPTION_O:
466 case TCC_OPTION_pedantic:
467 case TCC_OPTION_pipe:
468 case TCC_OPTION_s:
469 case TCC_OPTION_x:
470 /* ignored */
471 break;
472 default:
473 if (s->warn_unsupported) {
474 unsupported_option:
475 tcc_warning("unsupported option '%s'", r);
477 break;
481 if (NULL != (r1 = tcc_set_linker(s, (char *) linker_arg.data, 1)))
482 tcc_error("unsupported linker option '%s'", r1);
483 /* fixme: these options could be different on your platform */
484 if (was_pthread && output_type != TCC_OUTPUT_OBJ) {
485 dynarray_add((void ***)&files, &nb_files, "-lpthread");
486 nb_libraries++;
488 return optind;
491 int main(int argc, char **argv)
493 int i;
494 TCCState *s;
495 int nb_objfiles, ret, optind;
496 int64_t start_time = 0;
497 const char *default_file = NULL;
499 s = tcc_new();
501 output_type = TCC_OUTPUT_EXE;
502 outfile = NULL;
503 multiple_files = 1;
504 files = NULL;
505 nb_files = 0;
506 nb_libraries = 0;
507 reloc_output = 0;
508 print_search_dirs = 0;
509 m_option = NULL;
510 ret = 0;
512 optind = parse_args(s, argc - 1, argv + 1);
514 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
515 if (m_option)
516 exec_other_tcc(s, argv, m_option);
517 #endif
519 if (print_search_dirs) {
520 psd:
521 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
522 tcc_display_info(s, 1);
523 return 0;
526 if (s->verbose)
527 tcc_display_info(s, 0);
529 if (optind == 0 || nb_files == 0) {
530 if (optind && s->verbose) {
531 if (s->verbose == 2)
532 goto psd;
533 return 0;
535 help();
536 return 1;
539 nb_objfiles = nb_files - nb_libraries;
541 /* if outfile provided without other options, we output an
542 executable */
543 if (outfile && output_type == TCC_OUTPUT_MEMORY)
544 output_type = TCC_OUTPUT_EXE;
546 /* check -c consistency : only single file handled. XXX: checks file type */
547 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
548 /* accepts only a single input file */
549 if (nb_objfiles != 1)
550 tcc_error("cannot specify multiple files with -c");
551 if (nb_libraries != 0)
552 tcc_error("cannot specify libraries with -c");
555 if (output_type == TCC_OUTPUT_PREPROCESS) {
556 if (!outfile) {
557 s->outfile = stdout;
558 } else {
559 s->outfile = fopen(outfile, "w");
560 if (!s->outfile)
561 tcc_error("could not open '%s'", outfile);
565 if (do_bench) {
566 start_time = getclock_us();
569 tcc_set_output_type(s, output_type);
570 s->reloc_output = reloc_output;
572 /* compile or add each files or library */
573 for(i = 0; i < nb_files && ret == 0; i++) {
574 const char *filename;
576 filename = files[i];
577 if (filename[0] == '-' && filename[1] == 'l') {
578 if (tcc_add_library(s, filename + 2) < 0) {
579 tcc_error_noabort("cannot find %s", filename);
580 ret = 1;
582 } else {
583 if (1 == s->verbose)
584 printf("-> %s\n", filename);
585 if (tcc_add_file(s, filename) < 0)
586 ret = 1;
587 if (!default_file)
588 default_file = filename;
592 /* free all files */
593 tcc_free(files);
595 if (0 == ret) {
596 if (do_bench)
597 tcc_print_stats(s, getclock_us() - start_time);
599 if (s->output_type == TCC_OUTPUT_MEMORY) {
600 #ifdef TCC_IS_NATIVE
601 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
602 #else
603 tcc_error_noabort("-run is not available in a cross compiler");
604 #endif
605 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
606 if (s->outfile)
607 fclose(s->outfile);
608 } else {
609 if (!outfile)
610 outfile = tcc_default_target(s, default_file);
611 ret = !!tcc_output_file(s, outfile);
612 /* dump collected dependencies */
613 if (gen_deps && !ret)
614 tcc_gen_makedeps(s, outfile, deps_outfile);
618 tcc_delete(s);
619 cstr_free(&linker_arg);
620 tcc_free(outfile);
622 #ifdef MEM_DEBUG
623 if (do_bench) {
624 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
626 #endif
627 return ret;