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 void print_paths(const char *msg
, char **paths
, int nb_paths
)
30 printf("%s:\n%s", msg
, nb_paths
? "" : " -\n");
31 for(i
= 0; i
< nb_paths
; i
++)
32 printf(" %s\n", paths
[i
]);
35 static void display_info(TCCState
*s
, int what
)
39 printf("tcc version %s ("
40 #ifdef TCC_TARGET_I386
42 #elif defined TCC_TARGET_X86_64
44 #elif defined TCC_TARGET_C67
46 #elif defined TCC_TARGET_ARM
48 # ifdef TCC_ARM_HARDFLOAT
51 #elif defined TCC_TARGET_ARM64
53 # ifdef TCC_ARM_HARDFLOAT
59 #elif defined(__APPLE__)
60 /* Current Apple OS name as of 2016 */
62 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
64 #elif defined(__DragonFly__)
66 #elif defined(__NetBSD__)
68 #elif defined(__OpenBSD__)
70 #elif defined(__linux__)
73 " Unidentified system"
78 printf("install: %s\n", s
->tcc_lib_path
);
79 /* print_paths("programs", NULL, 0); */
80 print_paths("include", s
->sysinclude_paths
, s
->nb_sysinclude_paths
);
81 print_paths("libraries", s
->library_paths
, s
->nb_library_paths
);
83 print_paths("crt", s
->crt_paths
, s
->nb_crt_paths
);
84 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s
));
90 static void help(void)
92 printf("Tiny C Compiler "TCC_VERSION
" - Copyright (C) 2001-2006 Fabrice Bellard\n"
93 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
94 " tcc [options...] -run infile [arguments...]\n"
96 " -c compile only - generate an object file\n"
97 " -o outfile set output filename\n"
98 " -run run compiled source\n"
99 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
100 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
101 " -w disable all warnings\n"
103 " -vv show included files (as sole argument: show search paths)\n"
104 " -bench show compilation statistics\n"
105 "Preprocessor options:\n"
106 " -Idir add include path 'dir'\n"
107 " -Dsym[=val] define 'sym' with value 'val'\n"
108 " -Usym undefine 'sym'\n"
109 " -E preprocess only\n"
110 " -P[1] no / alternative #line output with -E\n"
111 " -dD -dM output #define directives with -E\n"
113 " -Ldir add library path 'dir'\n"
114 " -llib link with dynamic or static library 'lib'\n"
115 " -r generate (relocatable) object file\n"
116 " -shared generate a shared library\n"
117 " -rdynamic export all global symbols to dynamic linker\n"
118 " -soname set name for shared library to be used at runtime\n"
119 " -static static linking\n"
120 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
121 " -Wl,-opt[=val] set linker option (see manual)\n"
122 "Debugger options:\n"
123 " -g generate runtime debug info\n"
124 #ifdef CONFIG_TCC_BCHECK
125 " -b compile with built-in memory and bounds checker (implies -g)\n"
127 #ifdef CONFIG_TCC_BACKTRACE
128 " -bt N show N callers in stack traces\n"
131 " -x[c|a|n] specify type of the next infile\n"
132 " -nostdinc do not use standard system include paths\n"
133 " -nostdlib do not link with standard crt and libraries\n"
134 " -Bdir use 'dir' as tcc's private library/include path\n"
135 " -MD generate target dependencies for make\n"
136 " -MF depfile put generated dependencies here\n"
137 " -dumpversion print version\n"
138 " - use stdin pipe as infile\n"
139 " @listfile read arguments from listfile\n"
140 "Target specific options:\n"
141 " -m32/64 execute i386/x86-64 cross compiler\n"
142 " -mms-bitfields use MSVC bitfield layout\n"
143 #ifdef TCC_TARGET_ARM
144 " -mfloat-abi hard/softfp on arm\n"
146 #ifdef TCC_TARGET_X86_64
147 " -mno-sse disable floats on x86-64\n"
152 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
153 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
157 static char *str_replace(const char *str
, const char *p
, const char *r
)
166 for (d0
= NULL
;; d0
= tcc_malloc(sl
+ 1)) {
167 for (d
= d0
, s
= str
; s0
= s
, s
= strstr(s
, p
), s
; s
+= pl
) {
169 memcpy(d
, s0
, sl
= s
- s0
), d
+= sl
;
170 memcpy(d
, r
, rl
), d
+= rl
;
181 static int execvp_win32(const char *prog
, char **argv
)
184 /* replace all " by \" */
185 for (p
= argv
; *p
; ++p
)
187 *p
= str_replace(*p
, "\"", "\\\"");
188 ret
= _spawnvp(P_NOWAIT
, prog
, (const char *const*)argv
);
191 cwait(&ret
, ret
, WAIT_CHILD
);
194 #define execvp execvp_win32
196 static void exec_other_tcc(TCCState
*s
, char **argv
, int option
)
198 char child_path
[4096], *a0
= argv
[0]; const char *target
;
203 #ifdef TCC_TARGET_I386
205 case 64: target
= "x86_64";
208 case 32: target
= "i386";
211 l
= tcc_basename(a0
) - a0
;
212 snprintf(child_path
, sizeof child_path
,
219 if (strcmp(a0
, child_path
)) {
221 printf("tcc: using '%s'\n", child_path
+ l
), fflush(stdout
);
222 execvp(argv
[0] = child_path
, argv
);
224 tcc_error("'%s' not found", child_path
+ l
);
228 #define exec_other_tcc(s, argv, option)
231 static void gen_makedeps(TCCState
*s
, const char *target
, const char *filename
)
238 /* compute filename automatically: dir/file.o -> dir/file.d */
239 snprintf(buf
, sizeof buf
, "%.*s.d",
240 (int)(tcc_fileextension(target
) - target
), target
);
245 printf("<- %s\n", filename
);
247 /* XXX return err codes instead of error() ? */
248 depout
= fopen(filename
, "w");
250 tcc_error("could not open '%s'", filename
);
252 fprintf(depout
, "%s: \\\n", target
);
253 for (i
=0; i
<s
->nb_target_deps
; ++i
)
254 fprintf(depout
, " %s \\\n", s
->target_deps
[i
]);
255 fprintf(depout
, "\n");
259 static char *default_outputfile(TCCState
*s
, const char *first_file
)
263 const char *name
= "a";
265 if (first_file
&& strcmp(first_file
, "-"))
266 name
= tcc_basename(first_file
);
267 snprintf(buf
, sizeof(buf
), "%s", name
);
268 ext
= tcc_fileextension(buf
);
270 if (s
->output_type
== TCC_OUTPUT_DLL
)
273 if (s
->output_type
== TCC_OUTPUT_EXE
)
277 if (s
->output_type
== TCC_OUTPUT_OBJ
&& !s
->option_r
&& *ext
)
280 strcpy(buf
, "a.out");
281 return tcc_strdup(buf
);
284 static unsigned getclock_ms(void)
287 return GetTickCount();
290 gettimeofday(&tv
, NULL
);
291 return tv
.tv_sec
*1000 + (tv
.tv_usec
+500)/1000;
295 int main(int argc
, char **argv
)
299 unsigned start_time
= 0;
300 const char *first_file
= NULL
;
304 optind
= tcc_parse_args(s
, argc
- 1, argv
+ 1);
306 tcc_set_environment(s
);
314 exec_other_tcc(s
, argv
, s
->cross_target
);
319 if (s
->nb_files
== 0) {
321 if (s
->print_search_dirs
|| s
->verbose
== 2) {
322 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
329 tcc_error("no input files\n");
332 /* check -c consistency : only single file handled. XXX: checks file type */
333 if (s
->output_type
== TCC_OUTPUT_OBJ
&& !s
->option_r
) {
334 if (s
->nb_libraries
!= 0)
335 tcc_error("cannot specify libraries with -c");
336 /* accepts only a single input file */
337 if (s
->nb_files
!= 1)
338 tcc_error("cannot specify multiple files with -c");
341 if (s
->output_type
== 0)
342 s
->output_type
= TCC_OUTPUT_EXE
;
343 tcc_set_output_type(s
, s
->output_type
);
345 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
349 s
->ppfp
= fopen(s
->outfile
, "w");
351 tcc_error("could not write '%s'", s
->outfile
);
353 } else if (s
->output_type
!= TCC_OUTPUT_OBJ
) {
354 if (s
->option_pthread
)
355 tcc_set_options(s
, "-lpthread");
359 start_time
= getclock_ms();
361 /* compile or add each files or library */
362 for(i
= ret
= 0; i
< s
->nb_files
&& ret
== 0; i
++) {
363 struct filespec
*f
= s
->files
[i
];
364 if (f
->type
>= AFF_TYPE_LIB
) {
365 s
->alacarte_link
= f
->type
== AFF_TYPE_LIB
;
366 if (tcc_add_library_err(s
, f
->name
) < 0)
370 printf("-> %s\n", f
->name
);
371 s
->filetype
= f
->type
;
372 if (tcc_add_file(s
, f
->name
) < 0)
375 first_file
= f
->name
;
377 s
->filetype
= AFF_TYPE_NONE
;
378 s
->alacarte_link
= 1;
381 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
- 1 - optind
, argv
+ 1 + optind
);
392 s
->outfile
= default_outputfile(s
, first_file
);
393 ret
= !!tcc_output_file(s
, s
->outfile
);
394 if (s
->gen_deps
&& !ret
)
395 gen_makedeps(s
, s
->outfile
, s
->deps_outfile
);
400 tcc_print_stats(s
, getclock_ms() - start_time
);