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