Even on Darwin, tcc should display Linux
[tinycc.git] / tcc.c
blob2cca7a89ebcd66c3a8786d722540caf64d5af118
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 #ifdef ONE_SOURCE
22 #include "libtcc.c"
23 #else
24 #include "tcc.h"
25 #endif
26 #include "tcctools.c"
28 static const char help[] =
29 "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
30 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
31 " tcc [options...] -run infile [arguments...]\n"
32 "General options:\n"
33 " -c compile only - generate an object file\n"
34 " -o outfile set output filename\n"
35 " -run run compiled source\n"
36 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
37 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
38 " -w disable all warnings\n"
39 " -v -vv show version, show search paths or loaded files\n"
40 " -h -hh show this, show more help\n"
41 " -bench show compilation statistics\n"
42 " - use stdin pipe as infile\n"
43 " @listfile read arguments from listfile\n"
44 "Preprocessor options:\n"
45 " -Idir add include path 'dir'\n"
46 " -Dsym[=val] define 'sym' with value 'val'\n"
47 " -Usym undefine 'sym'\n"
48 " -E preprocess only\n"
49 "Linker options:\n"
50 " -Ldir add library path 'dir'\n"
51 " -llib link with dynamic or static library 'lib'\n"
52 " -r generate (relocatable) object file\n"
53 " -shared generate a shared library/dll\n"
54 " -rdynamic export all global symbols to dynamic linker\n"
55 " -soname set name for shared library to be used at runtime\n"
56 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
57 "Debugger options:\n"
58 " -g generate runtime debug info\n"
59 #ifdef CONFIG_TCC_BCHECK
60 " -b compile with built-in memory and bounds checker (implies -g)\n"
61 #endif
62 #ifdef CONFIG_TCC_BACKTRACE
63 " -bt N show N callers in stack traces\n"
64 #endif
65 "Misc. options:\n"
66 " -x[c|a|n] specify type of the next infile\n"
67 " -nostdinc do not use standard system include paths\n"
68 " -nostdlib do not link with standard crt and libraries\n"
69 " -Bdir set tcc's private include/library dir\n"
70 " -MD generate dependency file for make\n"
71 " -MF file specify dependency file name\n"
72 " -m32/64 defer to i386/x86_64 cross compiler\n"
73 "Tools:\n"
74 " create library : tcc -ar [rcsv] lib.a files\n"
75 #ifdef TCC_TARGET_PE
76 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
77 #endif
80 static const char help2[] =
81 "Tiny C Compiler "TCC_VERSION" - More Options\n"
82 "Special options:\n"
83 " -P -P1 with -E: no/alternative #line output\n"
84 " -dD -dM with -E: output #define directives\n"
85 " -pthread same as -D_REENTRANT and -lpthread\n"
86 " -On same as -D__OPTIMIZE__ for n > 0\n"
87 " -Wp,-opt same as -opt\n"
88 " -include file include 'file' above each input file\n"
89 " -isystem dir add 'dir' to system include path\n"
90 " -iwithprefix dir set tcc's private include/library subdir\n"
91 " -static link to static libraries (not recommended)\n"
92 " -dumpversion print version\n"
93 " -print-search-dirs print search paths\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 " -image-base= -Ttext= set base address of executable\n"
121 " -section-alignment= set section alignment in executable\n"
122 #ifdef TCC_TARGET_PE
123 " -file-alignment= set PE file alignment\n"
124 " -stack= set PE stack reserve\n"
125 " -large-address-aware set related PE option\n"
126 " -subsystem=[console gui] set PE subsystem\n"
127 " -oformat=[pe-* binary] set executable output format\n"
128 "Predefined macros:\n"
129 " tcc -E -dM - < nul\n"
130 #else
131 " -rpath= set dynamic library seach path\n"
132 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
133 " -soname= set DT_SONAME elf tag\n"
134 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
135 " -oformat=[elf32/64-* binary] set executable output format\n"
136 " -init= -fini= -as-needed -O (ignored)\n"
137 "Predefined macros:\n"
138 " tcc -E -dM - < /dev/null\n"
139 #endif
140 "See also the manual for more details.\n"
143 static const char version[] =
144 "tcc version "TCC_VERSION" ("
145 #ifdef TCC_TARGET_I386
146 "i386"
147 #elif defined TCC_TARGET_X86_64
148 "x86_64"
149 #elif defined TCC_TARGET_C67
150 "C67"
151 #elif defined TCC_TARGET_ARM
152 "ARM"
153 #elif defined TCC_TARGET_ARM64
154 "AArch64"
155 #endif
156 #ifdef TCC_ARM_HARDFLOAT
157 " Hard Float"
158 #endif
159 #ifdef TCC_TARGET_PE
160 " Windows"
161 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
162 " FreeBSD"
163 #else
164 " Linux"
165 #endif
166 ")\n"
169 static void print_dirs(const char *msg, char **paths, int nb_paths)
171 int i;
172 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
173 for(i = 0; i < nb_paths; i++)
174 printf(" %s\n", paths[i]);
177 static void print_search_dirs(TCCState *s)
179 printf("install: %s\n", s->tcc_lib_path);
180 /* print_dirs("programs", NULL, 0); */
181 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
182 print_dirs("libraries", s->library_paths, s->nb_library_paths);
183 #ifndef TCC_TARGET_PE
184 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
185 printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
186 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
187 #endif
190 static void set_environment(TCCState *s)
192 char * path;
194 path = getenv("C_INCLUDE_PATH");
195 if(path != NULL) {
196 tcc_add_include_path(s, path);
198 path = getenv("CPATH");
199 if(path != NULL) {
200 tcc_add_include_path(s, path);
202 path = getenv("LIBRARY_PATH");
203 if(path != NULL) {
204 tcc_add_library_path(s, path);
208 static char *default_outputfile(TCCState *s, const char *first_file)
210 char buf[1024];
211 char *ext;
212 const char *name = "a";
214 if (first_file && strcmp(first_file, "-"))
215 name = tcc_basename(first_file);
216 snprintf(buf, sizeof(buf), "%s", name);
217 ext = tcc_fileextension(buf);
218 #ifdef TCC_TARGET_PE
219 if (s->output_type == TCC_OUTPUT_DLL)
220 strcpy(ext, ".dll");
221 else
222 if (s->output_type == TCC_OUTPUT_EXE)
223 strcpy(ext, ".exe");
224 else
225 #endif
226 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
227 strcpy(ext, ".o");
228 else
229 strcpy(buf, "a.out");
230 return tcc_strdup(buf);
233 static unsigned getclock_ms(void)
235 #ifdef _WIN32
236 return GetTickCount();
237 #else
238 struct timeval tv;
239 gettimeofday(&tv, NULL);
240 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
241 #endif
244 int main(int argc, char **argv)
246 TCCState *s;
247 int ret, opt, n = 0;
248 unsigned start_time = 0;
249 const char *first_file;
251 redo:
252 s = tcc_new();
253 opt = tcc_parse_args(s, &argc, &argv, 1);
255 if (n == 0) {
256 if (opt == OPT_HELP)
257 return printf(help), 1;
258 if (opt == OPT_HELP2)
259 return printf(help2), 1;
260 if (opt == OPT_M32 || opt == OPT_M64)
261 tcc_tool_cross(s, argv, opt); /* never returns */
262 if (s->verbose)
263 printf(version);
264 if (opt == OPT_AR)
265 return tcc_tool_ar(s, argc, argv);
266 #ifdef TCC_TARGET_PE
267 if (opt == OPT_IMPDEF)
268 return tcc_tool_impdef(s, argc, argv);
269 #endif
270 if (opt == OPT_V)
271 return 0;
272 if (opt == OPT_PRINT_DIRS) {
273 /* initialize search dirs */
274 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
275 print_search_dirs(s);
276 return 0;
279 n = s->nb_files;
280 if (n == 0)
281 tcc_error("no input files\n");
283 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
284 if (!s->outfile) {
285 s->ppfp = stdout;
286 } else {
287 s->ppfp = fopen(s->outfile, "w");
288 if (!s->ppfp)
289 tcc_error("could not write '%s'", s->outfile);
291 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
292 if (s->nb_libraries)
293 tcc_error("cannot specify libraries with -c");
294 if (n > 1 && s->outfile)
295 tcc_error("cannot specify output file with -c many files");
296 } else {
297 if (s->option_pthread)
298 tcc_set_options(s, "-lpthread");
301 if (s->do_bench)
302 start_time = getclock_ms();
305 set_environment(s);
306 if (s->output_type == 0)
307 s->output_type = TCC_OUTPUT_EXE;
308 tcc_set_output_type(s, s->output_type);
310 /* compile or add each files or library */
311 for (first_file = NULL, ret = 0;;) {
312 struct filespec *f = s->files[s->nb_files - n];
313 s->filetype = f->type;
314 s->alacarte_link = f->alacarte;
315 if (f->type == AFF_TYPE_LIB) {
316 if (tcc_add_library_err(s, f->name) < 0)
317 ret = 1;
318 } else {
319 if (1 == s->verbose)
320 printf("-> %s\n", f->name);
321 if (!first_file)
322 first_file = f->name;
323 if (tcc_add_file(s, f->name) < 0)
324 ret = 1;
326 s->filetype = 0;
327 s->alacarte_link = 1;
328 if (ret || --n == 0
329 || (s->output_type == TCC_OUTPUT_OBJ && !s->option_r))
330 break;
333 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
334 if (s->outfile)
335 fclose(s->ppfp);
336 } else if (0 == ret) {
337 if (s->output_type == TCC_OUTPUT_MEMORY) {
338 #ifdef TCC_IS_NATIVE
339 ret = tcc_run(s, argc, argv);
340 #endif
341 } else {
342 if (!s->outfile)
343 s->outfile = default_outputfile(s, first_file);
344 if (tcc_output_file(s, s->outfile))
345 ret = 1;
346 else if (s->gen_deps)
347 gen_makedeps(s, s->outfile, s->deps_outfile);
351 if (s->do_bench && ret == 0 && n == 0)
352 tcc_print_stats(s, getclock_ms() - start_time);
353 tcc_delete(s);
354 if (ret == 0 && n)
355 goto redo; /* compile more files with -c */
356 return ret;