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
65 printf("install: %s\n", s
->tcc_lib_path
);
66 /* print_paths("programs", NULL, 0); */
67 print_paths("include", s
->sysinclude_paths
, s
->nb_sysinclude_paths
);
68 print_paths("libraries", s
->library_paths
, s
->nb_library_paths
);
70 print_paths("crt", s
->crt_paths
, s
->nb_crt_paths
);
71 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s
));
77 static void help(void)
79 printf("Tiny C Compiler "TCC_VERSION
" - Copyright (C) 2001-2006 Fabrice Bellard\n"
80 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
81 " tcc [options...] -run infile [arguments...]\n"
83 " -c compile only - generate an object file\n"
84 " -o outfile set output filename\n"
85 " -run run compiled source\n"
86 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
87 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
88 " -w disable all warnings\n"
90 " -vv show included files (as sole argument: show search paths)\n"
92 " -bench show compilation statistics\n"
93 " -xc -xa specify type of the next infile\n"
94 " - use stdin pipe as infile\n"
95 "Preprocessor options:\n"
96 " -Idir add include path 'dir'\n"
97 " -Dsym[=val] define 'sym' with value 'val'\n"
98 " -Usym undefine 'sym'\n"
99 " -E preprocess only\n"
100 " -P[1] no/alternative output of #line directives with -E\n"
101 " -dD dump defines\n"
103 " -Ldir add library path 'dir'\n"
104 " -llib link with dynamic or static library 'lib'\n"
105 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
106 " -r generate (relocatable) object file\n"
107 " -rdynamic export all global symbols to dynamic linker\n"
108 " -shared generate a shared library\n"
109 " -soname set name for shared library to be used at runtime\n"
110 " -static static linking\n"
111 " -Wl,-opt[=val] set linker option (see manual)\n"
112 "Debugger options:\n"
113 " -g generate runtime debug info\n"
114 #ifdef CONFIG_TCC_BCHECK
115 " -b compile with built-in memory and bounds checker (implies -g)\n"
117 #ifdef CONFIG_TCC_BACKTRACE
118 " -bt N show N callers in stack traces\n"
121 " -nostdinc do not use standard system include paths\n"
122 " -nostdlib do not link with standard crt and libraries\n"
123 " -Bdir use 'dir' as tcc internal library and include path\n"
124 " -MD generate target dependencies for make\n"
125 " -MF depfile put generated dependencies here\n"
129 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
130 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
133 static int execvp_win32(const char *prog
, char **argv
)
135 int ret
= spawnvp(P_NOWAIT
, prog
, (const char *const*)argv
);
138 cwait(&ret
, ret
, WAIT_CHILD
);
141 #define execvp execvp_win32
143 static void exec_other_tcc(TCCState
*s
, char **argv
, const char *optarg
)
145 char child_path
[4096], *child_name
; const char *target
;
146 switch (atoi(optarg
)) {
147 #ifdef TCC_TARGET_I386
149 case 64: target
= "x86_64";
152 case 32: target
= "i386";
154 pstrcpy(child_path
, sizeof child_path
- 40, argv
[0]);
155 child_name
= tcc_basename(child_path
);
156 strcpy(child_name
, target
);
158 strcat(child_name
, "-win32");
160 strcat(child_name
, "-tcc");
161 if (strcmp(argv
[0], child_path
)) {
163 printf("tcc: using '%s'\n", child_name
), fflush(stdout
);
164 execvp(argv
[0] = child_path
, argv
);
166 tcc_error("'%s' not found", child_name
);
167 case 0: /* ignore -march etc. */
170 tcc_warning("unsupported option \"-m%s\"", optarg
);
174 #define exec_other_tcc(s, argv, optarg)
177 static void gen_makedeps(TCCState
*s
, const char *target
, const char *filename
)
180 char buf
[1024], *ext
;
184 /* compute filename automatically
185 * dir/file.o -> dir/file.d */
186 pstrcpy(buf
, sizeof(buf
), target
);
187 ext
= tcc_fileextension(buf
);
188 pstrcpy(ext
, sizeof(buf
) - (ext
-buf
), ".d");
193 printf("<- %s\n", filename
);
195 /* XXX return err codes instead of error() ? */
196 depout
= fopen(filename
, "w");
198 tcc_error("could not open '%s'", filename
);
200 fprintf(depout
, "%s : \\\n", target
);
201 for (i
=0; i
<s
->nb_target_deps
; ++i
)
202 fprintf(depout
, " %s \\\n", s
->target_deps
[i
]);
203 fprintf(depout
, "\n");
207 static char *default_outputfile(TCCState
*s
, const char *first_file
)
211 const char *name
= "a";
213 if (first_file
&& strcmp(first_file
, "-"))
214 name
= tcc_basename(first_file
);
215 pstrcpy(buf
, sizeof(buf
), name
);
216 ext
= tcc_fileextension(buf
);
218 if (s
->output_type
== TCC_OUTPUT_DLL
)
221 if (s
->output_type
== TCC_OUTPUT_EXE
)
225 if (( (s
->output_type
== TCC_OUTPUT_OBJ
&& !s
->option_r
) ||
226 (s
->output_type
== TCC_OUTPUT_PREPROCESS
) )
230 strcpy(buf
, "a.out");
232 return tcc_strdup(buf
);
235 static int64_t getclock_us(void)
240 return (tb
.time
* 1000LL + tb
.millitm
) * 1000LL;
243 gettimeofday(&tv
, NULL
);
244 return tv
.tv_sec
* 1000000LL + tv
.tv_usec
;
248 int main(int argc
, char **argv
)
252 int64_t start_time
= 0;
256 optind
= tcc_parse_args(s
, argc
- 1, argv
+ 1);
259 start_time
= getclock_us();
261 tcc_set_environment(s
);
269 exec_other_tcc(s
, argv
, s
->option_m
);
274 if (s
->print_search_dirs
|| (s
->verbose
== 2 && optind
== 1)) {
275 tcc_set_output_type(s
, TCC_OUTPUT_MEMORY
);
280 if (s
->verbose
&& optind
== 1)
283 if (s
->nb_files
== 0)
284 tcc_error("no input files\n");
286 /* check -c consistency : only single file handled. XXX: checks file type */
287 if (s
->output_type
== TCC_OUTPUT_OBJ
&& !s
->option_r
) {
288 if (s
->nb_libraries
!= 0)
289 tcc_error("cannot specify libraries with -c");
290 /* accepts only a single input file */
291 if ((s
->nb_files
!= 1) && s
->outfile
) {
292 tcc_error("cannot specify multiple files with -c and -o");
296 if (s
->output_type
== TCC_OUTPUT_PREPROCESS
) {
300 s
->ppfp
= fopen(s
->outfile
, "w");
302 tcc_error("could not write '%s'", s
->outfile
);
306 tcc_set_output_type(s
, s
->output_type
);
308 /* compile or add each files or library */
309 for(i
= ret
= 0; i
< s
->nb_files
&& ret
== 0; i
++) {
310 int filetype
= *(unsigned char *)s
->files
[i
];
311 const char *filename
= s
->files
[i
] + 1;
312 if (filename
[0] == '-' && filename
[1] == 'l') {
313 if (tcc_add_library(s
, filename
+ 2) < 0) {
314 tcc_error_noabort("cannot find library 'lib%s'", filename
+2);
319 printf("-> %s\n", filename
);
321 s
->outfile
= default_outputfile(s
, filename
);
322 if (tcc_add_file(s
, filename
, filetype
) < 0)
325 if (s
->output_type
== TCC_OUTPUT_OBJ
) {
326 ret
= !!tcc_output_file(s
, s
->outfile
);
327 if (s
->gen_deps
&& !ret
)
328 gen_makedeps(s
, s
->outfile
, s
->deps_outfile
);
330 if ((i
+1) < s
->nb_files
) {
333 tcc_parse_args(s
, argc
- 1, argv
+ 1);
334 tcc_set_environment(s
);
335 if (s
->output_type
!= TCC_OUTPUT_OBJ
)
336 tcc_error("interlnal error");
337 tcc_set_output_type(s
, s
->output_type
);
345 if (s
->output_type
== TCC_OUTPUT_MEMORY
) {
347 ret
= tcc_run(s
, argc
- 1 - optind
, argv
+ 1 + optind
);
349 tcc_error_noabort("-run is not available in a cross compiler");
353 if (s
->output_type
== TCC_OUTPUT_EXE
||
354 s
->output_type
== TCC_OUTPUT_DLL
)
356 ret
= !!tcc_output_file(s
, s
->outfile
);
357 if (s
->gen_deps
&& !ret
)
358 gen_makedeps(s
, s
->outfile
, s
->deps_outfile
);
363 tcc_print_stats(s
, getclock_us() - start_time
);