fix to allow build tcc by build-tcc.bat
[tinycc.git] / tcc.c
blob5815adee4ed2942541d8f8ff5afba7878369ff9e
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 ", mingw"
59 #else
60 #ifdef __linux
61 ", Linux"
62 #else
63 ", Unknown"
64 #endif
65 #endif
66 ")\n", TCC_VERSION);
67 break;
68 case 1:
69 printf("install: %s\n", s->tcc_lib_path);
70 /* print_paths("programs", NULL, 0); */
71 print_paths("crt", s->crt_paths, s->nb_crt_paths);
72 print_paths("libraries", s->library_paths, s->nb_library_paths);
73 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
74 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
75 break;
79 static void help(TCCState *s)
81 display_info(s, 0);
82 printf("Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
83 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
84 " tcc [options...] -run infile [arguments...]\n"
85 "General options:\n"
86 " -c compile only - generate an object file\n"
87 " -o outfile set output filename\n"
88 " -run run compiled source\n"
89 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
90 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
91 " -w disable all warnings\n"
92 " -v show version\n"
93 " -vv show included files (as sole argument: show search paths)\n"
94 " -dumpversion\n"
95 " -bench show compilation statistics\n"
96 "Preprocessor options:\n"
97 " -E preprocess only\n"
98 " -Idir add include path 'dir'\n"
99 " -Dsym[=val] define 'sym' with value 'val'\n"
100 " -Usym undefine 'sym'\n"
101 " -P do not output a #line directive\n"
102 " -P1 use a #line directive in output instead of the gcc style\n"
103 " -dD put a define directive in the output (inside a comment)\n"
104 "Linker options:\n"
105 " -Ldir add library path 'dir'\n"
106 " -llib link with dynamic or static library 'lib'\n"
107 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
108 " -r generate (relocatable) object file\n"
109 " -rdynamic export all global symbols to dynamic linker\n"
110 " -shared generate a shared library\n"
111 " -soname set name for shared library to be used at runtime\n"
112 " -static static linking\n"
113 " -Wl,-opt[=val] set linker option (see manual)\n"
114 "Debugger options:\n"
115 " -g generate runtime debug info\n"
116 #ifdef CONFIG_TCC_BCHECK
117 " -b compile with built-in memory and bounds checker (implies -g)\n"
118 #endif
119 #ifdef CONFIG_TCC_BACKTRACE
120 " -bt N show N callers in stack traces\n"
121 #endif
122 "Misc options:\n"
123 " -nostdinc do not use standard system include paths\n"
124 " -nostdlib do not link with standard crt and libraries\n"
125 " -Bdir use 'dir' as tcc internal library and include path\n"
126 " -MD generate target dependencies for make\n"
127 " -MF depfile put generated dependencies here\n"
131 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
132 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
133 #ifdef _WIN32
134 #include <process.h>
135 static int execvp_win32(const char *prog, char **argv)
137 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
138 if (-1 == ret)
139 return ret;
140 cwait(&ret, ret, WAIT_CHILD);
141 exit(ret);
143 #define execvp execvp_win32
144 #endif
145 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
147 char child_path[4096], *child_name; const char *target;
148 switch (atoi(optarg)) {
149 #ifdef TCC_TARGET_I386
150 case 32: break;
151 case 64: target = "x86_64";
152 #else
153 case 64: break;
154 case 32: target = "i386";
155 #endif
156 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
157 child_name = tcc_basename(child_path);
158 strcpy(child_name, target);
159 #ifdef TCC_TARGET_PE
160 strcat(child_name, "-win32");
161 #endif
162 strcat(child_name, "-tcc");
163 if (strcmp(argv[0], child_path)) {
164 if (s->verbose > 0)
165 printf("tcc: using '%s'\n", child_name), fflush(stdout);
166 execvp(argv[0] = child_path, argv);
168 tcc_error("'%s' not found", child_name);
169 case 0: /* ignore -march etc. */
170 break;
171 default:
172 tcc_warning("unsupported option \"-m%s\"", optarg);
175 #else
176 #define exec_other_tcc(s, argv, optarg)
177 #endif
179 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
181 FILE *depout;
182 char buf[1024], *ext;
183 int i;
185 if (!filename) {
186 /* compute filename automatically
187 * dir/file.o -> dir/file.d */
188 pstrcpy(buf, sizeof(buf), target);
189 ext = tcc_fileextension(buf);
190 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
191 filename = buf;
194 if (s->verbose)
195 printf("<- %s\n", filename);
197 /* XXX return err codes instead of error() ? */
198 depout = fopen(filename, "w");
199 if (!depout)
200 tcc_error("could not open '%s'", filename);
202 fprintf(depout, "%s : \\\n", target);
203 for (i=0; i<s->nb_target_deps; ++i)
204 fprintf(depout, " %s \\\n", s->target_deps[i]);
205 fprintf(depout, "\n");
206 fclose(depout);
209 static char *default_outputfile(TCCState *s, const char *first_file)
211 char buf[1024];
212 char *ext;
213 const char *name = "a";
215 if (first_file && strcmp(first_file, "-"))
216 name = tcc_basename(first_file);
217 pstrcpy(buf, sizeof(buf), name);
218 ext = tcc_fileextension(buf);
219 #ifdef TCC_TARGET_PE
220 if (s->output_type == TCC_OUTPUT_DLL)
221 strcpy(ext, ".dll");
222 else
223 if (s->output_type == TCC_OUTPUT_EXE)
224 strcpy(ext, ".exe");
225 else
226 #endif
227 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
228 (s->output_type == TCC_OUTPUT_PREPROCESS) )
229 && *ext)
230 strcpy(ext, ".o");
231 else
232 strcpy(buf, "a.out");
234 return tcc_strdup(buf);
237 static int64_t getclock_us(void)
239 #ifdef _WIN32
240 struct _timeb tb;
241 _ftime(&tb);
242 return (tb.time * 1000LL + tb.millitm) * 1000LL;
243 #else
244 struct timeval tv;
245 gettimeofday(&tv, NULL);
246 return tv.tv_sec * 1000000LL + tv.tv_usec;
247 #endif
250 int main(int argc, char **argv)
252 TCCState *s;
253 int ret, optind, i, bench;
254 int64_t start_time = 0;
255 const char *first_file = NULL;
257 s = tcc_new();
259 optind = tcc_parse_args(s, argc - 1, argv + 1);
260 tcc_set_environment(s);
262 if (optind == 0) {
263 help(s);
264 return 1;
267 if (s->output_type == 0)
268 s->output_type = TCC_OUTPUT_EXE;
270 if (s->option_m)
271 exec_other_tcc(s, argv, s->option_m);
273 if (s->verbose)
274 display_info(s, 0);
276 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
277 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
278 display_info(s, 1);
279 return 0;
282 if (s->verbose && optind == 1)
283 return 0;
285 if (s->nb_files == 0)
286 tcc_error("no input files\n");
288 /* check -c consistency : only single file handled. XXX: checks file type */
289 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
290 if (s->nb_libraries != 0)
291 tcc_error("cannot specify libraries with -c");
292 /* accepts only a single input file */
293 if (s->nb_files != 1)
294 tcc_error("cannot specify multiple files with -c");
297 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
298 if (!s->outfile) {
299 s->ppfp = stdout;
300 } else {
301 s->ppfp = fopen(s->outfile, "w");
302 if (!s->ppfp)
303 tcc_error("could not write '%s'", s->outfile);
307 bench = s->do_bench;
308 if (bench)
309 start_time = getclock_us();
311 tcc_set_output_type(s, s->output_type);
313 /* compile or add each files or library */
314 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
315 const char *filename;
317 filename = s->files[i];
318 if (filename[0] == '-' && filename[1] == 'l') {
319 if (tcc_add_library(s, filename + 2) < 0) {
320 tcc_error_noabort("cannot find '%s'", filename);
321 ret = 1;
323 } else {
324 if (1 == s->verbose)
325 printf("-> %s\n", filename);
326 if (tcc_add_file(s, filename) < 0)
327 ret = 1;
328 if (!first_file)
329 first_file = filename;
333 if (0 == ret) {
334 if (bench)
335 tcc_print_stats(s, getclock_us() - start_time);
337 if (s->output_type == TCC_OUTPUT_MEMORY) {
338 #ifdef TCC_IS_NATIVE
339 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
340 #else
341 tcc_error_noabort("-run is not available in a cross compiler");
342 ret = 1;
343 #endif
344 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
345 if (s->outfile)
346 fclose(s->ppfp);
347 } else {
348 if (!s->outfile)
349 s->outfile = default_outputfile(s, first_file);
350 ret = !!tcc_output_file(s, s->outfile);
351 /* dump collected dependencies */
352 if (s->gen_deps && !ret)
353 gen_makedeps(s, s->outfile, s->deps_outfile);
357 tcc_delete(s);
358 if (bench)
359 tcc_memstats();
360 return ret;