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
21 #if defined NOTALLINONE || defined TCC_USE_LIBTCC
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 const char *outfile
;
34 static int do_bench
= 0;
36 static const char *deps_outfile
;
38 #define TCC_OPTION_HAS_ARG 0x0001
39 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
41 static void help(void)
43 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
44 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
45 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
46 " [-static] [infile1 infile2...] [-run infile args...]\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 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 " -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
{
116 TCC_OPTION_print_search_dirs
,
128 static const TCCOption tcc_options
[] = {
129 { "h", TCC_OPTION_HELP
, 0 },
130 { "-help", TCC_OPTION_HELP
, 0 },
131 { "?", TCC_OPTION_HELP
, 0 },
132 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
133 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
134 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
135 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
136 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
137 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
138 { "bench", TCC_OPTION_bench
, 0 },
139 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
140 #ifdef CONFIG_TCC_BCHECK
141 { "b", TCC_OPTION_b
, 0 },
143 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
144 { "c", TCC_OPTION_c
, 0 },
145 { "static", TCC_OPTION_static
, 0 },
146 { "shared", TCC_OPTION_shared
, 0 },
147 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
148 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
149 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
150 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
151 { "r", TCC_OPTION_r
, 0 },
152 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
153 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
154 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
155 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
156 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
157 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
158 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
159 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
160 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
161 { "w", TCC_OPTION_w
, 0 },
162 { "pipe", TCC_OPTION_pipe
, 0},
163 { "E", TCC_OPTION_E
, 0},
164 { "MD", TCC_OPTION_MD
, 0},
165 { "MF", TCC_OPTION_MF
, TCC_OPTION_HAS_ARG
},
166 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
170 static int64_t getclock_us(void)
175 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
178 gettimeofday(&tv
, NULL
);
179 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
183 /* convert 'str' into an array of space separated strings */
184 static int expand_args(char ***pargv
, const char *str
)
193 while (is_space(*str
))
198 while (*str
!= '\0' && !is_space(*str
))
201 arg
= tcc_malloc(len
+ 1);
202 memcpy(arg
, s1
, len
);
204 dynarray_add((void ***)&argv
, &argc
, arg
);
210 static int parse_args(TCCState
*s
, int argc
, char **argv
)
213 const TCCOption
*popt
;
214 const char *optarg
, *p1
, *r1
;
218 while (optind
< argc
) {
221 if (r
[0] != '-' || r
[1] == '\0') {
223 dynarray_add((void ***)&files
, &nb_files
, r
);
224 if (!multiple_files
) {
226 /* argv[0] will be this file */
230 /* find option in table (match only the first chars */
235 error("invalid option -- '%s'", r
);
248 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
249 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
253 error("argument to '%s' is missing", r
);
254 optarg
= argv
[optind
++];
262 switch(popt
->index
) {
263 case TCC_OPTION_HELP
:
267 if (tcc_add_include_path(s
, optarg
) < 0)
268 error("too many include paths");
273 sym
= (char *)optarg
;
274 value
= strchr(sym
, '=');
279 tcc_define_symbol(s
, sym
, value
);
283 tcc_undefine_symbol(s
, optarg
);
286 tcc_add_library_path(s
, optarg
);
289 /* set tcc utilities path (mainly for tcc development) */
290 tcc_set_lib_path(s
, optarg
);
293 dynarray_add((void ***)&files
, &nb_files
, r
);
296 case TCC_OPTION_bench
:
299 #ifdef CONFIG_TCC_BACKTRACE
301 set_num_callers(atoi(optarg
));
304 #ifdef CONFIG_TCC_BCHECK
306 s
->do_bounds_check
= 1;
315 output_type
= TCC_OUTPUT_OBJ
;
317 case TCC_OPTION_static
:
320 case TCC_OPTION_shared
:
321 output_type
= TCC_OUTPUT_DLL
;
323 case TCC_OPTION_soname
:
331 /* generate a .o merging several output files */
333 output_type
= TCC_OUTPUT_OBJ
;
335 case TCC_OPTION_nostdinc
:
338 case TCC_OPTION_nostdlib
:
341 case TCC_OPTION_print_search_dirs
:
342 print_search_dirs
= 1;
348 argc1
= expand_args(&argv1
, optarg
);
350 parse_args(s
, argc1
, argv1
);
353 output_type
= TCC_OUTPUT_MEMORY
;
358 if (0 == s
->verbose
++)
359 printf("tcc version %s\n", TCC_VERSION
);
360 } while (*optarg
++ == 'v');
363 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
364 goto unsupported_option
;
367 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
369 goto unsupported_option
;
374 case TCC_OPTION_rdynamic
:
379 if ((r
= (char *) tcc_set_linker(s
, (char *)optarg
, TRUE
)))
380 error("unsupported linker option '%s'", r
);
384 output_type
= TCC_OUTPUT_PREPROCESS
;
390 deps_outfile
= optarg
;
395 if (s
->warn_unsupported
) {
397 warning("unsupported option '%s'", r
);
406 int main(int argc
, char **argv
)
410 int nb_objfiles
, ret
, optind
;
411 int64_t start_time
= 0;
415 output_type
= TCC_OUTPUT_EXE
;
422 print_search_dirs
= 0;
425 optind
= parse_args(s
, argc
- 1, argv
+ 1);
426 if (print_search_dirs
) {
427 /* enough for Linux kernel */
428 printf("install: %s/\n", s
->tcc_lib_path
);
431 if (optind
== 0 || nb_files
== 0) {
432 if (optind
&& s
->verbose
)
438 nb_objfiles
= nb_files
- nb_libraries
;
440 /* if outfile provided without other options, we output an
442 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
443 output_type
= TCC_OUTPUT_EXE
;
445 /* check -c consistency : only single file handled. XXX: checks file type */
446 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
447 /* accepts only a single input file */
448 if (nb_objfiles
!= 1)
449 error("cannot specify multiple files with -c");
450 if (nb_libraries
!= 0)
451 error("cannot specify libraries with -c");
455 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
459 s
->outfile
= fopen(outfile
, "w");
461 error("could not open '%s'", outfile
);
466 start_time
= getclock_us();
469 tcc_set_output_type(s
, output_type
);
470 s
->reloc_output
= reloc_output
;
472 /* compile or add each files or library */
473 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
474 const char *filename
;
477 if (filename
[0] == '-' && filename
[1] == 'l') {
478 if (tcc_add_library(s
, filename
+ 2) < 0) {
479 error_noabort("cannot find %s", filename
);
484 printf("-> %s\n", filename
);
485 if (tcc_add_file(s
, filename
) < 0)
495 tcc_print_stats(s
, getclock_us() - start_time
);
497 if (s
->output_type
== TCC_OUTPUT_MEMORY
)
498 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
500 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
504 ret
= tcc_output_file(s
, outfile
? outfile
: tcc_default_target(s
));
508 /* dump collected dependencies */
509 if (gen_deps
&& !ret
)
510 tcc_gen_makedeps(s
, outfile
, deps_outfile
);
518 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);