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
;
39 #define TCC_OPTION_HAS_ARG 0x0001
40 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
42 static void help(void)
44 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
45 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
46 " tcc [options...] -run infile [arguments...]\n"
48 " -v display current version, increase verbosity\n"
49 " -c compile only - generate an object file\n"
50 " -o outfile set output filename\n"
51 " -Bdir set tcc internal library and include path\n"
52 " -bench output compilation statistics\n"
53 " -run run compiled source\n"
54 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
55 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
56 " -w disable all warnings\n"
57 "Preprocessor options:\n"
58 " -E preprocess only\n"
59 " -Idir add include path 'dir'\n"
60 " -Dsym[=val] define 'sym' with value 'val'\n"
61 " -Usym undefine 'sym'\n"
63 " -Ldir add library path 'dir'\n"
64 " -llib link with dynamic or static library 'lib'\n"
65 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
66 " -shared generate a shared library\n"
67 " -soname set name for shared library to be used at runtime\n"
68 " -static static linking\n"
69 " -rdynamic export all global symbols to dynamic linker\n"
70 " -r generate (relocatable) object file\n"
72 " -g generate runtime debug info\n"
73 #ifdef CONFIG_TCC_BCHECK
74 " -b compile with built-in memory and bounds checker (implies -g)\n"
76 #ifdef CONFIG_TCC_BACKTRACE
77 " -bt N show N callers in stack traces\n"
80 " -MD generate target dependencies for make\n"
81 " -MF depfile put generated dependencies here\n"
85 typedef struct TCCOption
{
118 TCC_OPTION_print_search_dirs
,
132 static const TCCOption tcc_options
[] = {
133 { "h", TCC_OPTION_HELP
, 0 },
134 { "-help", TCC_OPTION_HELP
, 0 },
135 { "?", TCC_OPTION_HELP
, 0 },
136 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
137 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
138 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
139 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
140 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
141 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
142 { "bench", TCC_OPTION_bench
, 0 },
143 #ifdef CONFIG_TCC_BACKTRACE
144 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
146 #ifdef CONFIG_TCC_BCHECK
147 { "b", TCC_OPTION_b
, 0 },
149 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
150 { "c", TCC_OPTION_c
, 0 },
151 { "static", TCC_OPTION_static
, 0 },
152 { "shared", TCC_OPTION_shared
, 0 },
153 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
154 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
155 { "pedantic", TCC_OPTION_pedantic
, 0},
156 { "pthread", TCC_OPTION_pthread
, 0},
157 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
158 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
159 { "r", TCC_OPTION_r
, 0 },
160 { "s", TCC_OPTION_s
, 0 },
161 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
162 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
163 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
164 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
165 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
166 { "isystem", TCC_OPTION_isystem
, TCC_OPTION_HAS_ARG
},
167 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
168 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
169 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
170 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
171 { "w", TCC_OPTION_w
, 0 },
172 { "pipe", TCC_OPTION_pipe
, 0},
173 { "E", TCC_OPTION_E
, 0},
174 { "MD", TCC_OPTION_MD
, 0},
175 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
176 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
180 static int64_t getclock_us(void)
185 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
188 gettimeofday(&tv
, NULL
);
189 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
193 /* convert 'str' into an array of space separated strings */
194 static int expand_args(char ***pargv
, const char *str
)
203 while (is_space(*str
))
208 while (*str
!= '\0' && !is_space(*str
))
211 arg
= tcc_malloc(len
+ 1);
212 memcpy(arg
, s1
, len
);
214 dynarray_add((void ***)&argv
, &argc
, arg
);
220 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
221 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
224 static int execvp_win32(const char *prog
, char **argv
)
226 int ret
= spawnvp(P_NOWAIT
, prog
, (char const*const*)argv
);
229 cwait(&ret
, ret
, WAIT_CHILD
);
232 #define execvp execvp_win32
234 static void exec_other_tcc(TCCState
*s
, char **argv
, const char *optarg
)
236 char child_path
[4096], *child_name
; const char *target
;
237 switch (atoi(optarg
)) {
238 #ifdef TCC_TARGET_I386
240 case 64: target
= "x86_64";
243 case 32: target
= "i386";
245 pstrcpy(child_path
, sizeof child_path
- 40, argv
[0]);
246 child_name
= tcc_basename(child_path
);
247 strcpy(child_name
, target
);
249 strcat(child_name
, "-win32");
251 strcat(child_name
, "-tcc");
252 if (strcmp(argv
[0], child_path
)) {
254 printf("tcc: using '%s'\n", child_name
), fflush(stdout
);
255 execvp(argv
[0] = child_path
, argv
);
257 tcc_error("'%s' not found", child_name
);
258 case 0: /* ignore -march etc. */
261 tcc_warning("unsupported option \"-m%s\"", optarg
);
266 static void parse_option_D(TCCState
*s1
, const char *optarg
)
268 char *sym
= tcc_strdup(optarg
);
269 char *value
= strchr(sym
, '=');
272 tcc_define_symbol(s1
, sym
, value
);
276 static int parse_args(TCCState
*s
, int argc
, char **argv
)
279 const TCCOption
*popt
;
280 const char *optarg
, *p1
, *r1
;
284 was_pthread
= 0; /* is set if commandline contains -pthread key */
287 while (optind
< argc
) {
290 if (r
[0] != '-' || r
[1] == '\0') {
292 dynarray_add((void ***)&files
, &nb_files
, r
);
293 if (!multiple_files
) {
295 /* argv[0] will be this file */
299 /* find option in table (match only the first chars */
304 tcc_error("invalid option -- '%s'", r
);
317 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
318 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
322 tcc_error("argument to '%s' is missing", r
);
323 optarg
= argv
[optind
++];
331 switch(popt
->index
) {
332 case TCC_OPTION_HELP
:
336 if (tcc_add_include_path(s
, optarg
) < 0)
337 tcc_error("too many include paths");
340 parse_option_D(s
, optarg
);
343 tcc_undefine_symbol(s
, optarg
);
346 tcc_add_library_path(s
, optarg
);
349 /* set tcc utilities path (mainly for tcc development) */
350 tcc_set_lib_path(s
, optarg
);
353 dynarray_add((void ***)&files
, &nb_files
, r
);
356 case TCC_OPTION_pthread
:
358 parse_option_D(s
, "_REENTRANT");
360 case TCC_OPTION_bench
:
363 #ifdef CONFIG_TCC_BACKTRACE
365 tcc_set_num_callers(atoi(optarg
));
368 #ifdef CONFIG_TCC_BCHECK
370 s
->do_bounds_check
= 1;
379 output_type
= TCC_OUTPUT_OBJ
;
381 case TCC_OPTION_static
:
384 case TCC_OPTION_shared
:
385 output_type
= TCC_OUTPUT_DLL
;
387 case TCC_OPTION_soname
:
395 outfile
= tcc_strdup(optarg
);
398 /* generate a .o merging several output files */
400 output_type
= TCC_OUTPUT_OBJ
;
402 case TCC_OPTION_isystem
:
403 tcc_add_sysinclude_path(s
, optarg
);
405 case TCC_OPTION_nostdinc
:
408 case TCC_OPTION_nostdlib
:
411 case TCC_OPTION_print_search_dirs
:
412 print_search_dirs
= 1;
418 argc1
= expand_args(&argv1
, optarg
);
420 parse_args(s
, argc1
, argv1
);
423 output_type
= TCC_OUTPUT_MEMORY
;
427 do ++s
->verbose
; while (*optarg
++ == 'v');
430 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
431 goto unsupported_option
;
434 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
436 goto unsupported_option
;
441 case TCC_OPTION_rdynamic
:
445 if ((r
= (char *) tcc_set_linker(s
, (char *)optarg
, TRUE
)))
446 tcc_error("unsupported linker option '%s'", r
);
449 output_type
= TCC_OUTPUT_PREPROCESS
;
455 deps_outfile
= optarg
;
460 if (s
->warn_unsupported
) {
462 tcc_warning("unsupported option '%s'", r
);
468 /* fixme: these options could be different on your platform */
469 if (was_pthread
&& output_type
!= TCC_OUTPUT_OBJ
) {
470 dynarray_add((void ***)&files
, &nb_files
, "-lpthread");
476 int main(int argc
, char **argv
)
480 int nb_objfiles
, ret
, optind
;
481 int64_t start_time
= 0;
482 const char *default_file
= NULL
;
486 output_type
= TCC_OUTPUT_EXE
;
493 print_search_dirs
= 0;
497 optind
= parse_args(s
, argc
, argv
);
499 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
501 exec_other_tcc(s
, argv
, m_option
);
504 if (print_search_dirs
) {
505 /* enough for Linux kernel */
506 printf("install: %s/\n", s
->tcc_lib_path
);
511 printf("tcc version %s\n", TCC_VERSION
);
513 if (optind
== 0 || nb_files
== 0) {
514 if (optind
&& s
->verbose
)
520 nb_objfiles
= nb_files
- nb_libraries
;
522 /* if outfile provided without other options, we output an
524 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
525 output_type
= TCC_OUTPUT_EXE
;
527 /* check -c consistency : only single file handled. XXX: checks file type */
528 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
529 /* accepts only a single input file */
530 if (nb_objfiles
!= 1)
531 tcc_error("cannot specify multiple files with -c");
532 if (nb_libraries
!= 0)
533 tcc_error("cannot specify libraries with -c");
536 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
540 s
->outfile
= fopen(outfile
, "w");
542 tcc_error("could not open '%s'", outfile
);
547 start_time
= getclock_us();
550 tcc_set_output_type(s
, output_type
);
551 s
->reloc_output
= reloc_output
;
553 /* compile or add each files or library */
554 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
555 const char *filename
;
558 if (filename
[0] == '-' && filename
[1] == 'l') {
559 if (tcc_add_library(s
, filename
+ 2) < 0) {
560 tcc_error_noabort("cannot find %s", filename
);
565 printf("-> %s\n", filename
);
566 if (tcc_add_file(s
, filename
) < 0)
569 default_file
= filename
;
578 tcc_print_stats(s
, getclock_us() - start_time
);
580 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
581 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
582 } else if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
587 outfile
= tcc_default_target(s
, default_file
);
588 ret
= !!tcc_output_file(s
, outfile
);
589 /* dump collected dependencies */
590 if (gen_deps
&& !ret
)
591 tcc_gen_makedeps(s
, outfile
, deps_outfile
);
600 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);