2 * TCC - Tiny C Compiler
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
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
;
34 static int do_bench
= 0;
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"
49 " -v display current version, increase verbosity\n"
50 " -c compile only - generate an object file\n"
51 " -o outfile set output filename\n"
52 " -Bdir set tcc internal library and include path\n"
53 " -bench output compilation statistics\n"
54 " -run run compiled source\n"
55 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
56 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
57 " -w disable all warnings\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"
64 " -Ldir add library path 'dir'\n"
65 " -llib link with dynamic or static library 'lib'\n"
66 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
67 " -shared generate a shared library\n"
68 " -soname set name for shared library to be used at runtime\n"
69 " -static static linking\n"
70 " -rdynamic export all global symbols to dynamic linker\n"
71 " -r generate (relocatable) object file\n"
73 " -g generate runtime debug info\n"
74 #ifdef CONFIG_TCC_BCHECK
75 " -b compile with built-in memory and bounds checker (implies -g)\n"
77 #ifdef CONFIG_TCC_BACKTRACE
78 " -bt N show N callers in stack traces\n"
81 " -MD generate target dependencies for make\n"
82 " -MF depfile put generated dependencies here\n"
86 typedef struct TCCOption
{
119 TCC_OPTION_print_search_dirs
,
133 static const TCCOption tcc_options
[] = {
134 { "h", TCC_OPTION_HELP
, 0 },
135 { "-help", TCC_OPTION_HELP
, 0 },
136 { "?", TCC_OPTION_HELP
, 0 },
137 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
138 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
139 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
140 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
141 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
142 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
143 { "bench", TCC_OPTION_bench
, 0 },
144 #ifdef CONFIG_TCC_BACKTRACE
145 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
147 #ifdef CONFIG_TCC_BCHECK
148 { "b", TCC_OPTION_b
, 0 },
150 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
151 { "c", TCC_OPTION_c
, 0 },
152 { "static", TCC_OPTION_static
, 0 },
153 { "shared", TCC_OPTION_shared
, 0 },
154 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
155 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
156 { "pedantic", TCC_OPTION_pedantic
, 0},
157 { "pthread", TCC_OPTION_pthread
, 0},
158 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
159 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
160 { "r", TCC_OPTION_r
, 0 },
161 { "s", TCC_OPTION_s
, 0 },
162 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
163 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
164 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
165 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
166 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
167 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
168 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
169 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
170 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
171 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
172 { "w", TCC_OPTION_w
, 0 },
173 { "pipe", TCC_OPTION_pipe
, 0},
174 { "E", TCC_OPTION_E
, 0},
175 { "MD", TCC_OPTION_MD
, 0},
176 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
177 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
181 static int64_t getclock_us(void)
186 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
189 gettimeofday(&tv
, NULL
);
190 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
194 /* convert 'str' into an array of space separated strings */
195 static int expand_args(char ***pargv
, const char *str
)
204 while (is_space(*str
))
209 while (*str
!= '\0' && !is_space(*str
))
212 arg
= tcc_malloc(len
+ 1);
213 memcpy(arg
, s1
, len
);
215 dynarray_add((void ***)&argv
, &argc
, arg
);
221 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
222 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
225 static int execvp_win32(const char *prog
, char **argv
)
227 int ret
= spawnvp(P_NOWAIT
, prog
, (char const*const*)argv
);
230 cwait(&ret
, ret
, WAIT_CHILD
);
233 #define execvp execvp_win32
235 static void exec_other_tcc(TCCState
*s
, char **argv
, const char *optarg
)
237 char child_path
[4096], *child_name
; const char *target
;
238 switch (atoi(optarg
)) {
239 #ifdef TCC_TARGET_I386
241 case 64: target
= "x86_64";
244 case 32: target
= "i386";
246 pstrcpy(child_path
, sizeof child_path
- 40, argv
[0]);
247 child_name
= tcc_basename(child_path
);
248 strcpy(child_name
, target
);
250 strcat(child_name
, "-win32");
252 strcat(child_name
, "-tcc");
253 if (strcmp(argv
[0], child_path
)) {
255 printf("tcc: using '%s'\n", child_name
), fflush(stdout
);
256 execvp(argv
[0] = child_path
, argv
);
258 tcc_error("'%s' not found", child_name
);
259 case 0: /* ignore -march etc. */
262 tcc_warning("unsupported option \"-m%s\"", optarg
);
267 static void parse_option_D(TCCState
*s1
, const char *optarg
)
269 char *sym
= tcc_strdup(optarg
);
270 char *value
= strchr(sym
, '=');
273 tcc_define_symbol(s1
, sym
, value
);
277 static int parse_args(TCCState
*s
, int argc
, char **argv
)
280 const TCCOption
*popt
;
281 const char *optarg
, *p1
, *r1
;
285 was_pthread
= 0; /* is set if commandline contains -pthread key */
287 cstr_new(&linker_arg
);
289 while (optind
< argc
) {
292 if (r
[0] != '-' || r
[1] == '\0') {
294 dynarray_add((void ***)&files
, &nb_files
, r
);
295 if (!multiple_files
) {
297 /* argv[0] will be this file */
301 /* find option in table (match only the first chars */
306 tcc_error("invalid option -- '%s'", r
);
319 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
320 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
324 tcc_error("argument to '%s' is missing", r
);
325 optarg
= argv
[optind
++];
333 switch(popt
->index
) {
334 case TCC_OPTION_HELP
:
338 if (tcc_add_include_path(s
, optarg
) < 0)
339 tcc_error("too many include paths");
342 parse_option_D(s
, optarg
);
345 tcc_undefine_symbol(s
, optarg
);
348 tcc_add_library_path(s
, optarg
);
351 /* set tcc utilities path (mainly for tcc development) */
352 tcc_set_lib_path(s
, optarg
);
355 dynarray_add((void ***)&files
, &nb_files
, r
);
358 case TCC_OPTION_pthread
:
360 parse_option_D(s
, "_REENTRANT");
362 case TCC_OPTION_bench
:
365 #ifdef CONFIG_TCC_BACKTRACE
367 tcc_set_num_callers(atoi(optarg
));
370 #ifdef CONFIG_TCC_BCHECK
372 s
->do_bounds_check
= 1;
381 output_type
= TCC_OUTPUT_OBJ
;
383 case TCC_OPTION_static
:
386 case TCC_OPTION_shared
:
387 output_type
= TCC_OUTPUT_DLL
;
389 case TCC_OPTION_soname
:
397 outfile
= tcc_strdup(optarg
);
400 /* generate a .o merging several output files */
402 output_type
= TCC_OUTPUT_OBJ
;
404 case TCC_OPTION_isystem
:
405 tcc_add_sysinclude_path(s
, optarg
);
407 case TCC_OPTION_nostdinc
:
410 case TCC_OPTION_nostdlib
:
413 case TCC_OPTION_print_search_dirs
:
414 print_search_dirs
= 1;
420 argc1
= expand_args(&argv1
, optarg
);
422 parse_args(s
, argc1
, argv1
);
425 output_type
= TCC_OUTPUT_MEMORY
;
429 do ++s
->verbose
; while (*optarg
++ == 'v');
432 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
433 goto unsupported_option
;
436 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
438 goto unsupported_option
;
443 case TCC_OPTION_rdynamic
:
448 --linker_arg
.size
, cstr_ccat(&linker_arg
, ',');
449 cstr_cat(&linker_arg
, optarg
);
450 cstr_ccat(&linker_arg
, '\0');
453 output_type
= TCC_OUTPUT_PREPROCESS
;
459 deps_outfile
= optarg
;
464 if (s
->warn_unsupported
) {
466 tcc_warning("unsupported option '%s'", r
);
472 if (NULL
!= (r1
= tcc_set_linker(s
, (char *) linker_arg
.data
, TRUE
)))
473 tcc_error("unsupported linker option '%s'", r1
);
474 /* fixme: these options could be different on your platform */
475 if (was_pthread
&& output_type
!= TCC_OUTPUT_OBJ
) {
476 dynarray_add((void ***)&files
, &nb_files
, "-lpthread");
482 int main(int argc
, char **argv
)
486 int nb_objfiles
, ret
, optind
;
487 int64_t start_time
= 0;
488 const char *default_file
= NULL
;
492 output_type
= TCC_OUTPUT_EXE
;
499 print_search_dirs
= 0;
503 optind
= parse_args(s
, argc
- 1, argv
+ 1);
505 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
507 exec_other_tcc(s
, argv
, m_option
);
510 if (print_search_dirs
) {
511 /* enough for Linux kernel */
512 printf("install: %s/\n", s
->tcc_lib_path
);
517 printf("tcc version %s\n", TCC_VERSION
);
519 if (optind
== 0 || nb_files
== 0) {
520 if (optind
&& s
->verbose
)
526 nb_objfiles
= nb_files
- nb_libraries
;
528 /* if outfile provided without other options, we output an
530 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
531 output_type
= TCC_OUTPUT_EXE
;
533 /* check -c consistency : only single file handled. XXX: checks file type */
534 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
535 /* accepts only a single input file */
536 if (nb_objfiles
!= 1)
537 tcc_error("cannot specify multiple files with -c");
538 if (nb_libraries
!= 0)
539 tcc_error("cannot specify libraries with -c");
542 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
546 s
->outfile
= fopen(outfile
, "w");
548 tcc_error("could not open '%s'", outfile
);
553 start_time
= getclock_us();
556 tcc_set_output_type(s
, output_type
);
557 s
->reloc_output
= reloc_output
;
559 /* compile or add each files or library */
560 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
561 const char *filename
;
564 if (filename
[0] == '-' && filename
[1] == 'l') {
565 if (tcc_add_library(s
, filename
+ 2) < 0) {
566 tcc_error_noabort("cannot find %s", filename
);
571 printf("-> %s\n", filename
);
572 if (tcc_add_file(s
, filename
) < 0)
575 default_file
= filename
;
584 tcc_print_stats(s
, getclock_us() - start_time
);
586 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
588 ret
= tcc_run(s
, argc
- 1 - optind
, argv
+ 1 + optind
);
590 tcc_error_noabort("-run is not available in a cross compiler");
592 } else if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
597 outfile
= tcc_default_target(s
, default_file
);
598 ret
= !!tcc_output_file(s
, outfile
);
599 /* dump collected dependencies */
600 if (gen_deps
&& !ret
)
601 tcc_gen_makedeps(s
, outfile
, deps_outfile
);
606 cstr_free(&linker_arg
);
611 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);