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
25 static int64_t getclock_us(void)
30 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
33 gettimeofday(&tv
, NULL
);
34 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
40 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
41 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
42 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
43 " [-static] [infile1 infile2...] [-run infile args...]\n"
46 " -v display current version, increase verbosity\n"
47 " -c compile only - generate an object file\n"
48 " -o outfile set output filename\n"
49 " -Bdir set tcc internal library path\n"
50 " -bench output compilation statistics\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 "Preprocessor options:\n"
56 " -E preprocess only\n"
57 " -Idir add include path 'dir'\n"
58 " -Dsym[=val] define 'sym' with value 'val'\n"
59 " -Usym undefine 'sym'\n"
61 " -Ldir add library path 'dir'\n"
62 " -llib link with dynamic or static library 'lib'\n"
63 " -shared generate a shared library\n"
64 " -soname set name for shared library to be used at runtime\n"
65 " -static static linking\n"
66 " -rdynamic export all global symbols to dynamic linker\n"
67 " -r generate (relocatable) object file\n"
69 " -g generate runtime debug info\n"
70 #ifdef CONFIG_TCC_BCHECK
71 " -b compile with built-in memory and bounds checker (implies -g)\n"
73 #ifdef CONFIG_TCC_BACKTRACE
74 " -bt N show N callers in stack traces\n"
79 #define TCC_OPTION_HAS_ARG 0x0001
80 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
82 typedef struct TCCOption
{
113 TCC_OPTION_print_search_dirs
,
122 static const TCCOption tcc_options
[] = {
123 { "h", TCC_OPTION_HELP
, 0 },
124 { "?", TCC_OPTION_HELP
, 0 },
125 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
126 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
127 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
128 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
129 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
130 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
131 { "bench", TCC_OPTION_bench
, 0 },
132 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
133 #ifdef CONFIG_TCC_BCHECK
134 { "b", TCC_OPTION_b
, 0 },
136 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
137 { "c", TCC_OPTION_c
, 0 },
138 { "static", TCC_OPTION_static
, 0 },
139 { "shared", TCC_OPTION_shared
, 0 },
140 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
141 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
142 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
143 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
144 { "r", TCC_OPTION_r
, 0 },
145 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
146 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
147 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
148 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
149 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
150 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
151 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
152 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
153 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
154 { "w", TCC_OPTION_w
, 0 },
155 { "pipe", TCC_OPTION_pipe
, 0},
156 { "E", TCC_OPTION_E
, 0},
160 /* convert 'str' into an array of space separated strings */
161 static int expand_args(char ***pargv
, const char *str
)
170 while (is_space(*str
))
175 while (*str
!= '\0' && !is_space(*str
))
178 arg
= tcc_malloc(len
+ 1);
179 memcpy(arg
, s1
, len
);
181 dynarray_add((void ***)&argv
, &argc
, arg
);
188 static int nb_files
, nb_libraries
;
189 static int multiple_files
;
190 static int print_search_dirs
;
191 static int output_type
;
192 static int reloc_output
;
193 static const char *outfile
;
195 int parse_args(TCCState
*s
, int argc
, char **argv
)
198 const TCCOption
*popt
;
199 const char *optarg
, *p1
, *r1
;
203 while (optind
< argc
) {
206 if (r
[0] != '-' || r
[1] == '\0') {
208 dynarray_add((void ***)&files
, &nb_files
, r
);
209 if (!multiple_files
) {
211 /* argv[0] will be this file */
215 /* find option in table (match only the first chars */
220 error("invalid option -- '%s'", r
);
233 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
234 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
238 error("argument to '%s' is missing", r
);
239 optarg
= argv
[optind
++];
247 switch(popt
->index
) {
248 case TCC_OPTION_HELP
:
252 if (tcc_add_include_path(s
, optarg
) < 0)
253 error("too many include paths");
258 sym
= (char *)optarg
;
259 value
= strchr(sym
, '=');
264 tcc_define_symbol(s
, sym
, value
);
268 tcc_undefine_symbol(s
, optarg
);
271 tcc_add_library_path(s
, optarg
);
274 /* set tcc utilities path (mainly for tcc development) */
275 tcc_set_lib_path(s
, optarg
);
278 dynarray_add((void ***)&files
, &nb_files
, r
);
281 case TCC_OPTION_bench
:
284 #ifdef CONFIG_TCC_BACKTRACE
286 num_callers
= atoi(optarg
);
289 #ifdef CONFIG_TCC_BCHECK
300 output_type
= TCC_OUTPUT_OBJ
;
302 case TCC_OPTION_static
:
305 case TCC_OPTION_shared
:
306 output_type
= TCC_OUTPUT_DLL
;
308 case TCC_OPTION_soname
:
316 /* generate a .o merging several output files */
318 output_type
= TCC_OUTPUT_OBJ
;
320 case TCC_OPTION_nostdinc
:
323 case TCC_OPTION_nostdlib
:
326 case TCC_OPTION_print_search_dirs
:
327 print_search_dirs
= 1;
333 argc1
= expand_args(&argv1
, optarg
);
335 parse_args(s
, argc1
, argv1
);
338 output_type
= TCC_OUTPUT_MEMORY
;
344 printf("tcc version %s\n", TCC_VERSION
);
345 } while (*optarg
++ == 'v');
348 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
349 goto unsupported_option
;
352 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
354 goto unsupported_option
;
359 case TCC_OPTION_rdynamic
:
365 if (strstart(optarg
, "-Ttext,", &p
)) {
366 s
->text_addr
= strtoul(p
, NULL
, 16);
367 s
->has_text_addr
= 1;
368 } else if (strstart(optarg
, "--oformat,", &p
)) {
369 if (strstart(p
, "elf32-", NULL
)) {
370 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
371 } else if (!strcmp(p
, "binary")) {
372 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
374 #ifdef TCC_TARGET_COFF
375 if (!strcmp(p
, "coff")) {
376 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
380 error("target %s not found", p
);
383 error("unsupported linker option '%s'", optarg
);
388 output_type
= TCC_OUTPUT_PREPROCESS
;
391 if (s
->warn_unsupported
) {
393 warning("unsupported option '%s'", r
);
402 int main(int argc
, char **argv
)
406 int nb_objfiles
, ret
, optind
;
407 char objfilename
[1024];
408 int64_t start_time
= 0;
412 tcc_set_lib_path_w32(s
);
414 output_type
= TCC_OUTPUT_EXE
;
421 print_search_dirs
= 0;
424 optind
= parse_args(s
, argc
- 1, argv
+ 1);
425 if (print_search_dirs
) {
426 /* enough for Linux kernel */
427 printf("install: %s/\n", tcc_lib_path
);
430 if (optind
== 0 || nb_files
== 0) {
431 if (optind
&& verbose
)
437 nb_objfiles
= nb_files
- nb_libraries
;
439 /* if outfile provided without other options, we output an
441 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
442 output_type
= TCC_OUTPUT_EXE
;
444 /* check -c consistency : only single file handled. XXX: checks file type */
445 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
446 /* accepts only a single input file */
447 if (nb_objfiles
!= 1)
448 error("cannot specify multiple files with -c");
449 if (nb_libraries
!= 0)
450 error("cannot specify libraries with -c");
454 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
458 s
->outfile
= fopen(outfile
, "w");
460 error("could not open '%s", outfile
);
462 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
464 /* compute default outfile name */
467 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
468 pstrcpy(objfilename
, sizeof(objfilename
), name
);
469 ext
= tcc_fileextension(objfilename
);
471 if (output_type
== TCC_OUTPUT_DLL
)
474 if (output_type
== TCC_OUTPUT_EXE
)
478 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
481 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
482 outfile
= objfilename
;
487 start_time
= getclock_us();
490 tcc_set_output_type(s
, output_type
);
492 /* compile or add each files or library */
493 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
494 const char *filename
;
497 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
498 if (tcc_add_file_internal(s
, filename
,
499 AFF_PRINT_ERROR
| AFF_PREPROCESS
) < 0)
501 } else if (filename
[0] == '-' && filename
[1]) {
502 if (tcc_add_library(s
, filename
+ 2) < 0)
503 error("cannot find %s", filename
);
506 printf("-> %s\n", filename
);
507 if (tcc_add_file(s
, filename
) < 0)
520 total_time
= (double)(getclock_us() - start_time
) / 1000000.0;
521 if (total_time
< 0.001)
525 printf("%d idents, %d lines, %d bytes, %0.3f s, %d lines/s, %0.1f MB/s\n",
526 tok_ident
- TOK_IDENT
, total_lines
, total_bytes
,
527 total_time
, (int)(total_lines
/ total_time
),
528 total_bytes
/ total_time
/ 1000000.0);
531 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
534 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
535 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
537 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
539 /* XXX: cannot do it with bound checking because of the malloc hooks */
540 if (!do_bounds_check
)
545 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);