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
27 static const char help
[] =
28 "Tiny C Compiler "TCC_VERSION
" - Copyright (C) 2001-2006 Fabrice Bellard\n"
29 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
30 " tcc [options...] -run infile (or --) [arguments...]\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -run run compiled source\n"
35 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
36 " -std=c99 Conform to the ISO 1999 C standard (default).\n"
37 " -std=c11 Conform to the ISO 2011 C standard.\n"
38 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
39 " -w disable all warnings\n"
40 " -v --version show version\n"
41 " -vv show search paths or loaded files\n"
42 " -h -hh show this, show more help\n"
43 " -bench show compilation statistics\n"
44 " - use stdin pipe as infile\n"
45 " @listfile read arguments from listfile\n"
46 "Preprocessor options:\n"
47 " -Idir add include path 'dir'\n"
48 " -Dsym[=val] define 'sym' with value 'val'\n"
49 " -Usym undefine 'sym'\n"
50 " -E preprocess only\n"
52 " -Ldir add library path 'dir'\n"
53 " -llib link with dynamic or static library 'lib'\n"
54 " -r generate (relocatable) object file\n"
55 " -shared generate a shared library/dll\n"
56 " -rdynamic export all global symbols to dynamic linker\n"
57 " -soname set name for shared library to be used at runtime\n"
58 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
60 " -g generate stab runtime debug info\n"
61 " -gdwarf[-x] generate dwarf runtime debug info\n"
63 " -g.pdb create .pdb debug database\n"
65 #ifdef CONFIG_TCC_BCHECK
66 " -b compile with built-in memory and bounds checker (implies -g)\n"
68 #ifdef CONFIG_TCC_BACKTRACE
69 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
72 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
73 " -nostdinc do not use standard system include paths\n"
74 " -nostdlib do not link with standard crt and libraries\n"
75 " -Bdir set tcc's private include/library dir\n"
76 " -M[M]D generate make dependency file [ignore system files]\n"
77 " -M[M] as above but no other output\n"
78 " -MF file specify dependency file name\n"
79 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
80 " -m32/64 defer to i386/x86_64 cross compiler\n"
83 " create library : tcc -ar [crstvx] lib [files]\n"
85 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
89 static const char help2
[] =
90 "Tiny C Compiler "TCC_VERSION
" - More Options\n"
92 " -P -P1 with -E: no/alternative #line output\n"
93 " -dD -dM with -E: output #define directives\n"
94 " -pthread same as -D_REENTRANT and -lpthread\n"
95 " -On same as -D__OPTIMIZE__ for n > 0\n"
96 " -Wp,-opt same as -opt\n"
97 " -include file include 'file' above each input file\n"
98 " -isystem dir add 'dir' to system include path\n"
99 " -static link to static libraries (not recommended)\n"
100 " -dumpversion print version\n"
101 " -print-search-dirs print search paths\n"
102 " -dt with -run/-E: auto-define 'test_...' macros\n"
104 " -arch -C --param -pedantic -pipe -s -traditional\n"
105 "-W[no-]... warnings:\n"
106 " all turn on some (*) warnings\n"
107 " error[=warning] stop after warning (any or specified)\n"
108 " write-strings strings are const\n"
109 " unsupported warn about ignored options, pragmas, etc.\n"
110 " implicit-function-declaration warn for missing prototype (*)\n"
111 " discarded-qualifiers warn when const is dropped (*)\n"
112 "-f[no-]... flags:\n"
113 " unsigned-char default char is unsigned\n"
114 " signed-char default char is signed\n"
115 " common use common section instead of bss\n"
116 " leading-underscore decorate extern symbols\n"
117 " ms-extensions allow anonymous struct in struct\n"
118 " dollars-in-identifiers allow '$' in C symbols\n"
119 " test-coverage create code coverage code\n"
120 "-m... target specific options:\n"
121 " ms-bitfields use MSVC bitfield layout\n"
122 #ifdef TCC_TARGET_ARM
123 " float-abi hard/softfp on arm\n"
125 #ifdef TCC_TARGET_X86_64
126 " no-sse disable floats on x86_64\n"
128 "-Wl,... linker options:\n"
129 " -nostdlib do not link with standard crt/libs\n"
130 " -[no-]whole-archive load lib(s) fully/only as needed\n"
131 " -export-all-symbols same as -rdynamic\n"
132 " -export-dynamic same as -rdynamic\n"
133 " -image-base= -Ttext= set base address of executable\n"
134 " -section-alignment= set section alignment in executable\n"
136 " -file-alignment= set PE file alignment\n"
137 " -stack= set PE stack reserve\n"
138 " -large-address-aware set related PE option\n"
139 " -subsystem=[console/windows] set PE subsystem\n"
140 " -oformat=[pe-* binary] set executable output format\n"
141 "Predefined macros:\n"
142 " tcc -E -dM - < nul\n"
144 " -rpath= set dynamic library search path\n"
145 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
146 " -soname= set DT_SONAME elf tag\n"
147 #if defined(TCC_TARGET_MACHO)
148 " -install_name= set DT_SONAME elf tag (soname macOS alias)\n"
150 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
151 " -oformat=[elf32/64-* binary] set executable output format\n"
152 " -init= -fini= -Map= -as-needed -O (ignored)\n"
153 "Predefined macros:\n"
154 " tcc -E -dM - < /dev/null\n"
156 "See also the manual for more details.\n"
159 static const char version
[] =
160 "tcc version "TCC_VERSION
165 #ifdef TCC_TARGET_I386
167 #elif defined TCC_TARGET_X86_64
169 #elif defined TCC_TARGET_C67
171 #elif defined TCC_TARGET_ARM
175 # ifdef TCC_ARM_HARDFLOAT
179 #elif defined TCC_TARGET_ARM64
181 #elif defined TCC_TARGET_RISCV64
186 #elif defined(TCC_TARGET_MACHO)
188 #elif TARGETOS_FreeBSD || TARGETOS_FreeBSD_kernel
190 #elif TARGETOS_OpenBSD
192 #elif TARGETOS_NetBSD
200 static void print_dirs(const char *msg
, char **paths
, int nb_paths
)
203 printf("%s:\n%s", msg
, nb_paths
? "" : " -\n");
204 for(i
= 0; i
< nb_paths
; i
++)
205 printf(" %s\n", paths
[i
]);
208 static void print_search_dirs(TCCState
*s
)
210 printf("install: %s\n", s
->tcc_lib_path
);
211 /* print_dirs("programs", NULL, 0); */
212 print_dirs("include", s
->sysinclude_paths
, s
->nb_sysinclude_paths
);
213 print_dirs("libraries", s
->library_paths
, s
->nb_library_paths
);
214 printf("libtcc1:\n %s/%s\n", s
->library_paths
[0], CONFIG_TCC_CROSSPREFIX TCC_LIBTCC1
);
215 #if !defined TCC_TARGET_PE && !defined TCC_TARGET_MACHO
216 print_dirs("crt", s
->crt_paths
, s
->nb_crt_paths
);
217 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s
));
221 static void set_environment(TCCState
*s
)
225 path
= getenv("C_INCLUDE_PATH");
227 tcc_add_sysinclude_path(s
, path
);
229 path
= getenv("CPATH");
231 tcc_add_include_path(s
, path
);
233 path
= getenv("LIBRARY_PATH");
235 tcc_add_library_path(s
, path
);
239 static char *default_outputfile(TCCState
*s
, const char *first_file
)
243 const char *name
= "a";
245 if (first_file
&& strcmp(first_file
, "-"))
246 name
= tcc_basename(first_file
);
247 snprintf(buf
, sizeof(buf
), "%s", name
);
248 ext
= tcc_fileextension(buf
);
250 if (s
->output_type
== TCC_OUTPUT_DLL
)
253 if (s
->output_type
== TCC_OUTPUT_EXE
)
257 if ((s
->just_deps
|| s
->output_type
== TCC_OUTPUT_OBJ
) && !s
->option_r
&& *ext
)
260 strcpy(buf
, "a.out");
261 return tcc_strdup(buf
);
264 static unsigned getclock_ms(void)
267 return GetTickCount();
270 gettimeofday(&tv
, NULL
);
271 return tv
.tv_sec
*1000 + (tv
.tv_usec
+500)/1000;
275 int main(int argc0
, char **argv0
)
278 int ret
, opt
, n
= 0, t
= 0, done
;
279 unsigned start_time
= 0, end_time
= 0;
280 const char *first_file
;
281 int argc
; char **argv
;
285 argc
= argc0
, argv
= argv0
;
287 #ifdef CONFIG_TCC_SWITCHES /* predefined options */
288 tcc_set_options(s
, CONFIG_TCC_SWITCHES
);
290 opt
= tcc_parse_args(s
, &argc
, &argv
, 1);
295 if (opt
== OPT_HELP
) {
301 if (opt
== OPT_HELP2
) {
302 fputs(help2
, stdout
);
305 if (opt
== OPT_M32
|| opt
== OPT_M64
)
306 return tcc_tool_cross(s
, argv
, opt
);
308 printf("%s", version
);
310 return tcc_tool_ar(s
, argc
, argv
);
312 if (opt
== OPT_IMPDEF
)
313 return tcc_tool_impdef(s
, argc
, argv
);
317 if (opt
== OPT_PRINT_DIRS
) {
318 /* initialize search dirs */
320 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
321 print_search_dirs(s
);
325 if (s
->nb_files
== 0) {
326 tcc_error_noabort("no input files");
327 } else if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
328 if (s
->outfile
&& 0!=strcmp("-",s
->outfile
)) {
329 ppfp
= fopen(s
->outfile
, "wb");
331 tcc_error_noabort("could not write '%s'", s
->outfile
);
333 } else if (s
->output_type
== TCC_OUTPUT_OBJ
&& !s
->option_r
) {
335 tcc_error_noabort("cannot specify libraries with -c");
336 else if (s
->nb_files
> 1 && s
->outfile
)
337 tcc_error_noabort("cannot specify output file with -c many files");
342 start_time
= getclock_ms();
346 if (s
->output_type
== 0)
347 s
->output_type
= TCC_OUTPUT_EXE
;
348 tcc_set_output_type(s
, s
->output_type
);
351 if ((s
->output_type
== TCC_OUTPUT_MEMORY
352 || s
->output_type
== TCC_OUTPUT_PREPROCESS
)
353 && (s
->dflag
& 16)) { /* -dt option */
361 /* compile or add each files or library */
364 struct filespec
*f
= s
->files
[n
];
365 s
->filetype
= f
->type
;
366 if (f
->type
& AFF_TYPE_LIB
) {
367 ret
= tcc_add_library_err(s
, f
->name
);
370 printf("-> %s\n", f
->name
);
372 first_file
= f
->name
;
373 ret
= tcc_add_file(s
, f
->name
);
375 done
= ret
|| ++n
>= s
->nb_files
;
376 } while (!done
&& (s
->output_type
!= TCC_OUTPUT_OBJ
|| s
->option_r
));
379 end_time
= getclock_ms();
383 } else if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
385 } else if (0 == ret
) {
386 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
388 ret
= tcc_run(s
, argc
, argv
);
392 s
->outfile
= default_outputfile(s
, first_file
);
393 if (!s
->just_deps
&& tcc_output_file(s
, s
->outfile
))
395 else if (s
->gen_deps
)
396 gen_makedeps(s
, s
->outfile
, s
->deps_outfile
);
402 done
= 0; /* run more tests with -dt -run */
403 else if (s
->nb_errors
)
405 else if (n
< s
->nb_files
)
406 done
= 0; /* compile more files with -c */
407 else if (s
->do_bench
)
408 tcc_print_stats(s
, end_time
- start_time
);
412 if (ppfp
&& ppfp
!= stdout
)