some -bench fixes
[tinycc.git] / tcc.c
blobac820ce477d16f010552f90f9fc982f390d05579
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
27 static void print_paths(const char *msg, char **paths, int nb_paths)
29 int i;
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)
37 switch (what) {
38 case 0:
39 printf("tcc version %s ("
40 #ifdef TCC_TARGET_I386
41 "i386"
42 #elif defined TCC_TARGET_X86_64
43 "x86-64"
44 #elif defined TCC_TARGET_C67
45 "C67"
46 #elif defined TCC_TARGET_ARM
47 "ARM"
48 # ifdef TCC_ARM_HARDFLOAT
49 " Hard Float"
50 # endif
51 #elif defined TCC_TARGET_ARM64
52 "AArch64"
53 # ifdef TCC_ARM_HARDFLOAT
54 " Hard Float"
55 # endif
56 #endif
57 #ifdef TCC_TARGET_PE
58 " Windows"
59 #else
60 " Linux"
61 #endif
62 ")\n", TCC_VERSION);
63 break;
64 case 1:
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);
69 #ifndef TCC_TARGET_PE
70 print_paths("crt", s->crt_paths, s->nb_crt_paths);
71 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
72 #endif
73 break;
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"
82 "General options:\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"
89 " -v show version\n"
90 " -vv show included files (as sole argument: show search paths)\n"
91 " -dumpversion\n"
92 " -bench show compilation statistics\n"
93 "Preprocessor options:\n"
94 " -Idir add include path 'dir'\n"
95 " -Dsym[=val] define 'sym' with value 'val'\n"
96 " -Usym undefine 'sym'\n"
97 " -E preprocess only\n"
98 " -P[1] no/alternative output of #line directives with -E\n"
99 "Linker options:\n"
100 " -Ldir add library path 'dir'\n"
101 " -llib link with dynamic or static library 'lib'\n"
102 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
103 " -r generate (relocatable) object file\n"
104 " -rdynamic export all global symbols to dynamic linker\n"
105 " -shared generate a shared library\n"
106 " -soname set name for shared library to be used at runtime\n"
107 " -static static linking\n"
108 " -Wl,-opt[=val] set linker option (see manual)\n"
109 "Debugger options:\n"
110 " -g generate runtime debug info\n"
111 #ifdef CONFIG_TCC_BCHECK
112 " -b compile with built-in memory and bounds checker (implies -g)\n"
113 #endif
114 #ifdef CONFIG_TCC_BACKTRACE
115 " -bt N show N callers in stack traces\n"
116 #endif
117 "Misc options:\n"
118 " -nostdinc do not use standard system include paths\n"
119 " -nostdlib do not link with standard crt and libraries\n"
120 " -Bdir use 'dir' as tcc internal library and include path\n"
121 " -MD generate target dependencies for make\n"
122 " -MF depfile put generated dependencies here\n"
126 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
127 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
128 #ifdef _WIN32
129 #include <process.h>
130 static int execvp_win32(const char *prog, char **argv)
132 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
133 if (-1 == ret)
134 return ret;
135 cwait(&ret, ret, WAIT_CHILD);
136 exit(ret);
138 #define execvp execvp_win32
139 #endif
140 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
142 char child_path[4096], *child_name; const char *target;
143 switch (atoi(optarg)) {
144 #ifdef TCC_TARGET_I386
145 case 32: break;
146 case 64: target = "x86_64";
147 #else
148 case 64: break;
149 case 32: target = "i386";
150 #endif
151 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
152 child_name = tcc_basename(child_path);
153 strcpy(child_name, target);
154 #ifdef TCC_TARGET_PE
155 strcat(child_name, "-win32");
156 #endif
157 strcat(child_name, "-tcc");
158 if (strcmp(argv[0], child_path)) {
159 if (s->verbose > 0)
160 printf("tcc: using '%s'\n", child_name), fflush(stdout);
161 execvp(argv[0] = child_path, argv);
163 tcc_error("'%s' not found", child_name);
164 case 0: /* ignore -march etc. */
165 break;
166 default:
167 tcc_warning("unsupported option \"-m%s\"", optarg);
170 #else
171 #define exec_other_tcc(s, argv, optarg)
172 #endif
174 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
176 FILE *depout;
177 char buf[1024], *ext;
178 int i;
180 if (!filename) {
181 /* compute filename automatically
182 * dir/file.o -> dir/file.d */
183 pstrcpy(buf, sizeof(buf), target);
184 ext = tcc_fileextension(buf);
185 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
186 filename = buf;
189 if (s->verbose)
190 printf("<- %s\n", filename);
192 /* XXX return err codes instead of error() ? */
193 depout = fopen(filename, "w");
194 if (!depout)
195 tcc_error("could not open '%s'", filename);
197 fprintf(depout, "%s : \\\n", target);
198 for (i=0; i<s->nb_target_deps; ++i)
199 fprintf(depout, " %s \\\n", s->target_deps[i]);
200 fprintf(depout, "\n");
201 fclose(depout);
204 static char *default_outputfile(TCCState *s, const char *first_file)
206 char buf[1024];
207 char *ext;
208 const char *name = "a";
210 if (first_file && strcmp(first_file, "-"))
211 name = tcc_basename(first_file);
212 pstrcpy(buf, sizeof(buf), name);
213 ext = tcc_fileextension(buf);
214 #ifdef TCC_TARGET_PE
215 if (s->output_type == TCC_OUTPUT_DLL)
216 strcpy(ext, ".dll");
217 else
218 if (s->output_type == TCC_OUTPUT_EXE)
219 strcpy(ext, ".exe");
220 else
221 #endif
222 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
223 (s->output_type == TCC_OUTPUT_PREPROCESS) )
224 && *ext)
225 strcpy(ext, ".o");
226 else
227 strcpy(buf, "a.out");
229 return tcc_strdup(buf);
232 static int64_t getclock_us(void)
234 #ifdef _WIN32
235 struct _timeb tb;
236 _ftime(&tb);
237 return (tb.time * 1000LL + tb.millitm) * 1000LL;
238 #else
239 struct timeval tv;
240 gettimeofday(&tv, NULL);
241 return tv.tv_sec * 1000000LL + tv.tv_usec;
242 #endif
245 int main(int argc, char **argv)
247 TCCState *s;
248 int ret, optind, i;
249 int64_t start_time = 0;
251 s = tcc_new();
253 optind = tcc_parse_args(s, argc - 1, argv + 1);
255 if (s->do_bench)
256 start_time = getclock_us();
258 tcc_set_environment(s);
260 if (optind == 0) {
261 help();
262 return 1;
265 if (s->option_m)
266 exec_other_tcc(s, argv, s->option_m);
268 if (s->verbose)
269 display_info(s, 0);
271 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
272 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
273 display_info(s, 1);
274 return 0;
277 if (s->verbose && optind == 1)
278 return 0;
280 if (s->nb_files == 0)
281 tcc_error("no input files\n");
283 /* check -c consistency : only single file handled. XXX: checks file type */
284 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
285 if (s->nb_libraries != 0)
286 tcc_error("cannot specify libraries with -c");
287 /* accepts only a single input file */
288 if ((s->nb_files != 1) && s->outfile) {
289 tcc_error("cannot specify multiple files with -c and -o");
293 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
294 if (!s->outfile) {
295 s->ppfp = stdout;
296 } else {
297 s->ppfp = fopen(s->outfile, "w");
298 if (!s->ppfp)
299 tcc_error("could not write '%s'", s->outfile);
303 tcc_set_output_type(s, s->output_type);
305 /* compile or add each files or library */
306 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
307 int filetype = *(unsigned char *)s->files[i];
308 const char *filename = s->files[i] + 1;
309 if (filename[0] == '-' && filename[1] == 'l') {
310 if (tcc_add_library(s, filename + 2) < 0) {
311 tcc_error_noabort("cannot find library 'lib%s'", filename+2);
312 ret = 1;
314 } else {
315 if (1 == s->verbose)
316 printf("-> %s\n", filename);
317 if (!s->outfile)
318 s->outfile = default_outputfile(s, filename);
319 if (tcc_add_file(s, filename, filetype) < 0)
320 ret = 1;
321 else
322 if (s->output_type == TCC_OUTPUT_OBJ) {
323 ret = !!tcc_output_file(s, s->outfile);
324 if (s->gen_deps && !ret)
325 gen_makedeps(s, s->outfile, s->deps_outfile);
326 if (!ret) {
327 if ((i+1) < s->nb_files) {
328 tcc_delete(s);
329 s = tcc_new();
330 tcc_parse_args(s, argc - 1, argv + 1);
331 tcc_set_environment(s);
332 if (s->output_type != TCC_OUTPUT_OBJ)
333 tcc_error("interlnal error");
334 tcc_set_output_type(s, s->output_type);
341 if (0 == ret) {
342 if (s->output_type == TCC_OUTPUT_MEMORY) {
343 #ifdef TCC_IS_NATIVE
344 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
345 #else
346 tcc_error_noabort("-run is not available in a cross compiler");
347 ret = 1;
348 #endif
349 } else
350 if (s->output_type == TCC_OUTPUT_EXE ||
351 s->output_type == TCC_OUTPUT_DLL)
353 ret = !!tcc_output_file(s, s->outfile);
354 if (s->gen_deps && !ret)
355 gen_makedeps(s, s->outfile, s->deps_outfile);
359 if (s->do_bench)
360 tcc_print_stats(s, getclock_us() - start_time);
362 tcc_delete(s);
363 return ret;