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 #define TCC_OPTION_HAS_ARG 0x0001
37 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
39 static void help(void)
41 printf("tcc version " TCC_VERSION
" - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
42 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
43 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
44 " [-static] [infile1 infile2...] [-run infile args...]\n"
47 " -v display current version, increase verbosity\n"
48 " -c compile only - generate an object file\n"
49 " -o outfile set output filename\n"
50 " -Bdir set tcc internal library path\n"
51 " -bench output compilation statistics\n"
52 " -run run compiled source\n"
53 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
54 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
55 " -w disable all warnings\n"
56 "Preprocessor options:\n"
57 " -E preprocess only\n"
58 " -Idir add include path 'dir'\n"
59 " -Dsym[=val] define 'sym' with value 'val'\n"
60 " -Usym undefine 'sym'\n"
62 " -Ldir add library path 'dir'\n"
63 " -llib link with dynamic or static library 'lib'\n"
64 " -shared generate a shared library\n"
65 " -soname set name for shared library to be used at runtime\n"
66 " -static static linking\n"
67 " -rdynamic export all global symbols to dynamic linker\n"
68 " -r generate (relocatable) object file\n"
70 " -g generate runtime debug info\n"
71 #ifdef CONFIG_TCC_BCHECK
72 " -b compile with built-in memory and bounds checker (implies -g)\n"
74 #ifdef CONFIG_TCC_BACKTRACE
75 " -bt N show N callers in stack traces\n"
80 typedef struct TCCOption
{
111 TCC_OPTION_print_search_dirs
,
121 static const TCCOption tcc_options
[] = {
122 { "h", TCC_OPTION_HELP
, 0 },
123 { "?", TCC_OPTION_HELP
, 0 },
124 { "I", TCC_OPTION_I
, TCC_OPTION_HAS_ARG
},
125 { "D", TCC_OPTION_D
, TCC_OPTION_HAS_ARG
},
126 { "U", TCC_OPTION_U
, TCC_OPTION_HAS_ARG
},
127 { "L", TCC_OPTION_L
, TCC_OPTION_HAS_ARG
},
128 { "B", TCC_OPTION_B
, TCC_OPTION_HAS_ARG
},
129 { "l", TCC_OPTION_l
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
130 { "bench", TCC_OPTION_bench
, 0 },
131 { "bt", TCC_OPTION_bt
, TCC_OPTION_HAS_ARG
},
132 #ifdef CONFIG_TCC_BCHECK
133 { "b", TCC_OPTION_b
, 0 },
135 { "g", TCC_OPTION_g
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
136 { "c", TCC_OPTION_c
, 0 },
137 { "static", TCC_OPTION_static
, 0 },
138 { "shared", TCC_OPTION_shared
, 0 },
139 { "soname", TCC_OPTION_soname
, TCC_OPTION_HAS_ARG
},
140 { "o", TCC_OPTION_o
, TCC_OPTION_HAS_ARG
},
141 { "run", TCC_OPTION_run
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
142 { "rdynamic", TCC_OPTION_rdynamic
, 0 },
143 { "r", TCC_OPTION_r
, 0 },
144 { "Wl,", TCC_OPTION_Wl
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
145 { "W", TCC_OPTION_W
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
146 { "O", TCC_OPTION_O
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
147 { "m", TCC_OPTION_m
, TCC_OPTION_HAS_ARG
},
148 { "f", TCC_OPTION_f
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
149 { "nostdinc", TCC_OPTION_nostdinc
, 0 },
150 { "nostdlib", TCC_OPTION_nostdlib
, 0 },
151 { "print-search-dirs", TCC_OPTION_print_search_dirs
, 0 },
152 { "v", TCC_OPTION_v
, TCC_OPTION_HAS_ARG
| TCC_OPTION_NOSEP
},
153 { "w", TCC_OPTION_w
, 0 },
154 { "pipe", TCC_OPTION_pipe
, 0},
155 { "E", TCC_OPTION_E
, 0},
156 { "x", TCC_OPTION_x
, TCC_OPTION_HAS_ARG
},
160 static int64_t getclock_us(void)
165 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
168 gettimeofday(&tv
, NULL
);
169 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
173 static int strstart(const char *str
, const char *val
, const char **ptr
)
189 /* convert 'str' into an array of space separated strings */
190 static int expand_args(char ***pargv
, const char *str
)
199 while (is_space(*str
))
204 while (*str
!= '\0' && !is_space(*str
))
207 arg
= tcc_malloc(len
+ 1);
208 memcpy(arg
, s1
, len
);
210 dynarray_add((void ***)&argv
, &argc
, arg
);
216 static int parse_args(TCCState
*s
, int argc
, char **argv
)
219 const TCCOption
*popt
;
220 const char *optarg
, *p1
, *r1
;
224 while (optind
< argc
) {
227 if (r
[0] != '-' || r
[1] == '\0') {
229 dynarray_add((void ***)&files
, &nb_files
, r
);
230 if (!multiple_files
) {
232 /* argv[0] will be this file */
236 /* find option in table (match only the first chars */
241 error("invalid option -- '%s'", r
);
254 if (popt
->flags
& TCC_OPTION_HAS_ARG
) {
255 if (*r1
!= '\0' || (popt
->flags
& TCC_OPTION_NOSEP
)) {
259 error("argument to '%s' is missing", r
);
260 optarg
= argv
[optind
++];
268 switch(popt
->index
) {
269 case TCC_OPTION_HELP
:
273 if (tcc_add_include_path(s
, optarg
) < 0)
274 error("too many include paths");
279 sym
= (char *)optarg
;
280 value
= strchr(sym
, '=');
285 tcc_define_symbol(s
, sym
, value
);
289 tcc_undefine_symbol(s
, optarg
);
292 tcc_add_library_path(s
, optarg
);
295 /* set tcc utilities path (mainly for tcc development) */
296 tcc_set_lib_path(s
, optarg
);
299 dynarray_add((void ***)&files
, &nb_files
, r
);
302 case TCC_OPTION_bench
:
305 #ifdef CONFIG_TCC_BACKTRACE
307 set_num_callers(atoi(optarg
));
310 #ifdef CONFIG_TCC_BCHECK
312 s
->do_bounds_check
= 1;
321 output_type
= TCC_OUTPUT_OBJ
;
323 case TCC_OPTION_static
:
326 case TCC_OPTION_shared
:
327 output_type
= TCC_OUTPUT_DLL
;
329 case TCC_OPTION_soname
:
337 /* generate a .o merging several output files */
339 output_type
= TCC_OUTPUT_OBJ
;
341 case TCC_OPTION_nostdinc
:
344 case TCC_OPTION_nostdlib
:
347 case TCC_OPTION_print_search_dirs
:
348 print_search_dirs
= 1;
354 argc1
= expand_args(&argv1
, optarg
);
356 parse_args(s
, argc1
, argv1
);
359 output_type
= TCC_OUTPUT_MEMORY
;
364 if (0 == s
->verbose
++)
365 printf("tcc version %s\n", TCC_VERSION
);
366 } while (*optarg
++ == 'v');
369 if (tcc_set_flag(s
, optarg
, 1) < 0 && s
->warn_unsupported
)
370 goto unsupported_option
;
373 if (tcc_set_warning(s
, optarg
, 1) < 0 &&
375 goto unsupported_option
;
380 case TCC_OPTION_rdynamic
:
386 if (strstart(optarg
, "-Ttext,", &p
)) {
387 s
->text_addr
= strtoul(p
, NULL
, 16);
388 s
->has_text_addr
= 1;
389 } else if (strstart(optarg
, "--section-alignment,", &p
)) {
390 s
->section_align
= strtoul(p
, NULL
, 16);
391 } else if (strstart(optarg
, "--image-base,", &p
)) {
392 s
->text_addr
= strtoul(p
, NULL
, 16);
393 s
->has_text_addr
= 1;
395 } else if (strstart(optarg
, "--file-alignment,", &p
)) {
396 s
->pe_file_align
= strtoul(p
, NULL
, 16);
397 } else if (strstart(optarg
, "--subsystem,", &p
)) {
398 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
399 if (!strcmp(p
, "native"))
401 else if (!strcmp(p
, "console"))
403 else if (!strcmp(p
, "gui"))
405 else if (!strcmp(p
, "posix"))
407 else if (!strcmp(p
, "efiapp"))
408 s
->pe_subsystem
= 10;
409 else if (!strcmp(p
, "efiboot"))
410 s
->pe_subsystem
= 11;
411 else if (!strcmp(p
, "efiruntime"))
412 s
->pe_subsystem
= 12;
413 else if (!strcmp(p
, "efirom"))
414 s
->pe_subsystem
= 13;
415 #elif defined(TCC_TARGET_ARM)
416 if (!strcmp(p
, "wince"))
420 error("invalid subsystem '%s'", p
);
423 } else if (strstart(optarg
, "--oformat,", &p
)) {
424 #if defined(TCC_TARGET_PE)
425 if (strstart(p
, "pe-", NULL
)) {
427 #if defined(TCC_TARGET_X86_64)
428 if (strstart(p
, "elf64-", NULL
)) {
430 if (strstart(p
, "elf32-", NULL
)) {
433 s
->output_format
= TCC_OUTPUT_FORMAT_ELF
;
434 } else if (!strcmp(p
, "binary")) {
435 s
->output_format
= TCC_OUTPUT_FORMAT_BINARY
;
437 #ifdef TCC_TARGET_COFF
438 if (!strcmp(p
, "coff")) {
439 s
->output_format
= TCC_OUTPUT_FORMAT_COFF
;
443 error("target %s not found", p
);
445 } else if (strstart(optarg
, "-rpath=", &p
)) {
448 error("unsupported linker option '%s'", optarg
);
453 output_type
= TCC_OUTPUT_PREPROCESS
;
458 if (s
->warn_unsupported
) {
460 warning("unsupported option '%s'", r
);
469 int main(int argc
, char **argv
)
473 int nb_objfiles
, ret
, optind
;
474 char objfilename
[1024];
475 int64_t start_time
= 0;
479 output_type
= TCC_OUTPUT_EXE
;
486 print_search_dirs
= 0;
489 optind
= parse_args(s
, argc
- 1, argv
+ 1);
490 if (print_search_dirs
) {
491 /* enough for Linux kernel */
492 printf("install: %s/\n", s
->tcc_lib_path
);
495 if (optind
== 0 || nb_files
== 0) {
496 if (optind
&& s
->verbose
)
502 nb_objfiles
= nb_files
- nb_libraries
;
504 /* if outfile provided without other options, we output an
506 if (outfile
&& output_type
== TCC_OUTPUT_MEMORY
)
507 output_type
= TCC_OUTPUT_EXE
;
509 /* check -c consistency : only single file handled. XXX: checks file type */
510 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
) {
511 /* accepts only a single input file */
512 if (nb_objfiles
!= 1)
513 error("cannot specify multiple files with -c");
514 if (nb_libraries
!= 0)
515 error("cannot specify libraries with -c");
519 if (output_type
== TCC_OUTPUT_PREPROCESS
) {
523 s
->outfile
= fopen(outfile
, "w");
525 error("could not open '%s", outfile
);
527 } else if (output_type
!= TCC_OUTPUT_MEMORY
) {
529 /* compute default outfile name */
532 strcmp(files
[0], "-") == 0 ? "a" : tcc_basename(files
[0]);
533 pstrcpy(objfilename
, sizeof(objfilename
), name
);
534 ext
= tcc_fileextension(objfilename
);
536 if (output_type
== TCC_OUTPUT_DLL
)
539 if (output_type
== TCC_OUTPUT_EXE
)
543 if (output_type
== TCC_OUTPUT_OBJ
&& !reloc_output
&& *ext
)
546 pstrcpy(objfilename
, sizeof(objfilename
), "a.out");
547 outfile
= objfilename
;
552 start_time
= getclock_us();
555 tcc_set_output_type(s
, output_type
);
557 /* compile or add each files or library */
558 for(i
= 0; i
< nb_files
&& ret
== 0; i
++) {
559 const char *filename
;
562 if (filename
[0] == '-' && filename
[1]) {
563 if (tcc_add_library(s
, filename
+ 2) < 0) {
564 error_noabort("cannot find %s", filename
);
569 printf("-> %s\n", filename
);
570 if (tcc_add_file(s
, filename
) < 0)
580 tcc_print_stats(s
, getclock_us() - start_time
);
582 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
585 } else if (s
->output_type
== TCC_OUTPUT_MEMORY
)
586 ret
= tcc_run(s
, argc
- optind
, argv
+ optind
);
588 ret
= tcc_output_file(s
, outfile
) ? 1 : 0;
595 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size
, mem_max_size
);