Make 112_backtrace/bcheck_123 more robust
[tinycc.git] / tcc.c
blob34bfa0043479b5570ac2b669ad19c1e4de2eb625
1 /*
2 * TCC - Tiny C Compiler
3 *
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 #include "tcc.h"
22 #if ONE_SOURCE
23 # include "libtcc.c"
24 #endif
25 #include "tcctools.c"
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 [arguments...]\n"
31 "General options:\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 -vv show version, show search paths or loaded files\n"
41 " -h -hh show this, show more help\n"
42 " -bench show compilation statistics\n"
43 " - use stdin pipe as infile\n"
44 " @listfile read arguments from listfile\n"
45 "Preprocessor options:\n"
46 " -Idir add include path 'dir'\n"
47 " -Dsym[=val] define 'sym' with value 'val'\n"
48 " -Usym undefine 'sym'\n"
49 " -E preprocess only\n"
50 "Linker options:\n"
51 " -Ldir add library path 'dir'\n"
52 " -llib link with dynamic or static library 'lib'\n"
53 " -r generate (relocatable) object file\n"
54 " -shared generate a shared library/dll\n"
55 " -rdynamic export all global symbols to dynamic linker\n"
56 " -soname set name for shared library to be used at runtime\n"
57 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
58 "Debugger options:\n"
59 " -g generate runtime debug info\n"
60 #ifdef CONFIG_TCC_BCHECK
61 " -b compile with built-in memory and bounds checker (implies -g)\n"
62 #endif
63 #ifdef CONFIG_TCC_BACKTRACE
64 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
65 #endif
66 "Misc. options:\n"
67 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
68 " -nostdinc do not use standard system include paths\n"
69 " -nostdlib do not link with standard crt and libraries\n"
70 " -Bdir set tcc's private include/library dir\n"
71 " -MD generate dependency file for make\n"
72 " -MF file specify dependency file name\n"
73 " -m32/64 defer to i386/x86_64 cross compiler\n"
74 "Tools:\n"
75 " create library : tcc -ar [rcsv] lib.a files\n"
76 #ifdef TCC_TARGET_PE
77 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
78 #endif
81 static const char help2[] =
82 "Tiny C Compiler "TCC_VERSION" - More Options\n"
83 "Special options:\n"
84 " -P -P1 with -E: no/alternative #line output\n"
85 " -dD -dM with -E: output #define directives\n"
86 " -pthread same as -D_REENTRANT and -lpthread\n"
87 " -On same as -D__OPTIMIZE__ for n > 0\n"
88 " -Wp,-opt same as -opt\n"
89 " -include file include 'file' above each input file\n"
90 " -isystem dir add 'dir' to system include path\n"
91 " -static link to static libraries (not recommended)\n"
92 " -dumpversion print version\n"
93 " -print-search-dirs print search paths\n"
94 " -dt with -run/-E: auto-define 'test_...' macros\n"
95 "Ignored options:\n"
96 " --param -pedantic -pipe -s -traditional\n"
97 "-W... warnings:\n"
98 " all turn on some (*) warnings\n"
99 " error stop after first warning\n"
100 " unsupported warn about ignored options, pragmas, etc.\n"
101 " write-strings strings are const\n"
102 " implicit-function-declaration warn for missing prototype (*)\n"
103 "-f[no-]... flags:\n"
104 " unsigned-char default char is unsigned\n"
105 " signed-char default char is signed\n"
106 " common use common section instead of bss\n"
107 " leading-underscore decorate extern symbols\n"
108 " ms-extensions allow anonymous struct in struct\n"
109 " dollars-in-identifiers allow '$' in C symbols\n"
110 "-m... target specific options:\n"
111 " ms-bitfields use MSVC bitfield layout\n"
112 #ifdef TCC_TARGET_ARM
113 " float-abi hard/softfp on arm\n"
114 #endif
115 #ifdef TCC_TARGET_X86_64
116 " no-sse disable floats on x86_64\n"
117 #endif
118 "-Wl,... linker options:\n"
119 " -nostdlib do not link with standard crt/libs\n"
120 " -[no-]whole-archive load lib(s) fully/only as needed\n"
121 " -export-all-symbols same as -rdynamic\n"
122 " -export-dynamic same as -rdynamic\n"
123 " -image-base= -Ttext= set base address of executable\n"
124 " -section-alignment= set section alignment in executable\n"
125 #ifdef TCC_TARGET_PE
126 " -file-alignment= set PE file alignment\n"
127 " -stack= set PE stack reserve\n"
128 " -large-address-aware set related PE option\n"
129 " -subsystem=[console/windows] set PE subsystem\n"
130 " -oformat=[pe-* binary] set executable output format\n"
131 "Predefined macros:\n"
132 " tcc -E -dM - < nul\n"
133 #else
134 " -rpath= set dynamic library search path\n"
135 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
136 " -soname= set DT_SONAME elf tag\n"
137 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
138 " -oformat=[elf32/64-* binary] set executable output format\n"
139 " -init= -fini= -as-needed -O (ignored)\n"
140 "Predefined macros:\n"
141 " tcc -E -dM - < /dev/null\n"
142 #endif
143 "See also the manual for more details.\n"
146 static const char version[] =
147 "tcc version "TCC_VERSION" ("
148 #ifdef TCC_TARGET_I386
149 "i386"
150 #elif defined TCC_TARGET_X86_64
151 "x86_64"
152 #elif defined TCC_TARGET_C67
153 "C67"
154 #elif defined TCC_TARGET_ARM
155 "ARM"
156 #elif defined TCC_TARGET_ARM64
157 "AArch64"
158 #endif
159 #ifdef TCC_ARM_HARDFLOAT
160 " Hard Float"
161 #endif
162 #ifdef TCC_TARGET_PE
163 " Windows"
164 #elif defined(TCC_TARGET_MACHO)
165 " Darwin"
166 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
167 " FreeBSD"
168 #else
169 " Linux"
170 #endif
171 ")\n"
174 static void print_dirs(const char *msg, char **paths, int nb_paths)
176 int i;
177 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
178 for(i = 0; i < nb_paths; i++)
179 printf(" %s\n", paths[i]);
182 static void print_search_dirs(TCCState *s)
184 printf("install: %s\n", s->tcc_lib_path);
185 /* print_dirs("programs", NULL, 0); */
186 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
187 print_dirs("libraries", s->library_paths, s->nb_library_paths);
188 #ifdef TCC_TARGET_PE
189 printf("libtcc1:\n %s/lib/"TCC_LIBTCC1"\n", s->tcc_lib_path);
190 #else
191 printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
192 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
193 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
194 #endif
197 static void set_environment(TCCState *s)
199 char * path;
201 path = getenv("C_INCLUDE_PATH");
202 if(path != NULL) {
203 tcc_add_sysinclude_path(s, path);
205 path = getenv("CPATH");
206 if(path != NULL) {
207 tcc_add_include_path(s, path);
209 path = getenv("LIBRARY_PATH");
210 if(path != NULL) {
211 tcc_add_library_path(s, path);
215 static char *default_outputfile(TCCState *s, const char *first_file)
217 char buf[1024];
218 char *ext;
219 const char *name = "a";
221 if (first_file && strcmp(first_file, "-"))
222 name = tcc_basename(first_file);
223 snprintf(buf, sizeof(buf), "%s", name);
224 ext = tcc_fileextension(buf);
225 #ifdef TCC_TARGET_PE
226 if (s->output_type == TCC_OUTPUT_DLL)
227 strcpy(ext, ".dll");
228 else
229 if (s->output_type == TCC_OUTPUT_EXE)
230 strcpy(ext, ".exe");
231 else
232 #endif
233 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
234 strcpy(ext, ".o");
235 else
236 strcpy(buf, "a.out");
237 return tcc_strdup(buf);
240 static unsigned getclock_ms(void)
242 #ifdef _WIN32
243 return GetTickCount();
244 #else
245 struct timeval tv;
246 gettimeofday(&tv, NULL);
247 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
248 #endif
251 int main(int argc0, char **argv0)
253 TCCState *s, *s1;
254 int ret, opt, n = 0, t = 0, done;
255 unsigned start_time = 0;
256 const char *first_file;
257 int argc; char **argv;
258 FILE *ppfp = stdout;
260 redo:
261 argc = argc0, argv = argv0;
262 s = s1 = tcc_new();
263 opt = tcc_parse_args(s, &argc, &argv, 1);
265 if (n == 0) {
266 if (opt == OPT_HELP)
267 return fputs(help, stdout), 0;
268 if (opt == OPT_HELP2)
269 return fputs(help2, stdout), 0;
270 if (opt == OPT_M32 || opt == OPT_M64)
271 tcc_tool_cross(s, argv, opt); /* never returns */
272 if (s->verbose)
273 printf(version);
274 if (opt == OPT_AR)
275 return tcc_tool_ar(s, argc, argv);
276 #ifdef TCC_TARGET_PE
277 if (opt == OPT_IMPDEF)
278 return tcc_tool_impdef(s, argc, argv);
279 #endif
280 if (opt == OPT_V)
281 return 0;
282 if (opt == OPT_PRINT_DIRS) {
283 /* initialize search dirs */
284 set_environment(s);
285 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
286 print_search_dirs(s);
287 return 0;
290 if (s->nb_files == 0)
291 tcc_error("no input files\n");
293 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
294 if (s->outfile && 0!=strcmp("-",s->outfile)) {
295 ppfp = fopen(s->outfile, "w");
296 if (!ppfp)
297 tcc_error("could not write '%s'", s->outfile);
299 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
300 if (s->nb_libraries)
301 tcc_error("cannot specify libraries with -c");
302 if (s->nb_files > 1 && s->outfile)
303 tcc_error("cannot specify output file with -c many files");
304 } else {
305 if (s->option_pthread)
306 tcc_set_options(s, "-lpthread");
309 if (s->do_bench)
310 start_time = getclock_ms();
313 set_environment(s);
314 if (s->output_type == 0)
315 s->output_type = TCC_OUTPUT_EXE;
316 tcc_set_output_type(s, s->output_type);
317 s->ppfp = ppfp;
319 if ((s->output_type == TCC_OUTPUT_MEMORY
320 || s->output_type == TCC_OUTPUT_PREPROCESS)
321 && (s->dflag & 16)) { /* -dt option */
322 if (t)
323 s->dflag |= 32;
324 s->run_test = ++t;
325 if (n)
326 --n;
329 /* compile or add each files or library */
330 first_file = NULL, ret = 0;
331 do {
332 struct filespec *f = s->files[n];
333 s->filetype = f->type;
334 if (f->type & AFF_TYPE_LIB) {
335 if (tcc_add_library_err(s, f->name) < 0)
336 ret = 1;
337 } else {
338 if (1 == s->verbose)
339 printf("-> %s\n", f->name);
340 if (!first_file)
341 first_file = f->name;
342 if (tcc_add_file(s, f->name) < 0)
343 ret = 1;
345 done = ret || ++n >= s->nb_files;
346 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
348 if (s->run_test) {
349 t = 0;
350 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
352 } else if (0 == ret) {
353 if (s->output_type == TCC_OUTPUT_MEMORY) {
354 #ifdef TCC_IS_NATIVE
355 ret = tcc_run(s, argc, argv);
356 #endif
357 } else {
358 if (!s->outfile)
359 s->outfile = default_outputfile(s, first_file);
360 if (tcc_output_file(s, s->outfile))
361 ret = 1;
362 else if (s->gen_deps)
363 gen_makedeps(s, s->outfile, s->deps_outfile);
367 if (s->do_bench && done && !(t | ret))
368 tcc_print_stats(s, getclock_ms() - start_time);
369 tcc_delete(s);
370 if (!done)
371 goto redo; /* compile more files with -c */
372 if (t)
373 goto redo; /* run more tests with -dt -run */
374 if (ppfp && ppfp != stdout)
375 fclose(ppfp);
376 return ret;