Improve multiarch detection
[tinycc.git] / tcc.c
blobf4ff3f84167ccbcfa3c8fe8559f226ec6853d4cc
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 help(void)
29 printf("tcc version " TCC_VERSION " - Tiny C Compiler - 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 man page)\n"
37 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
38 " -w disable all warnings\n"
39 " -v show version\n"
40 " -vv show included files (as sole argument: show search paths)\n"
41 " -bench show compilation statistics\n"
42 "Preprocessor options:\n"
43 " -E preprocess only\n"
44 " -Idir add include path 'dir'\n"
45 " -Dsym[=val] define 'sym' with value 'val'\n"
46 " -Usym undefine 'sym'\n"
47 "Linker options:\n"
48 " -Ldir add library path 'dir'\n"
49 " -llib link with dynamic or static library 'lib'\n"
50 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
51 " -r generate (relocatable) object file\n"
52 " -rdynamic export all global symbols to dynamic linker\n"
53 " -shared generate a shared library\n"
54 " -soname set name for shared library to be used at runtime\n"
55 " -static static linking\n"
56 " -Wl,-opt[=val] set linker option (see manual)\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 " -nostdinc do not use standard system include paths\n"
67 " -nostdlib do not link with standard crt and libraries\n"
68 " -Bdir use 'dir' as tcc internal library and include path\n"
69 " -MD generate target dependencies for make\n"
70 " -MF depfile put generated dependencies here\n"
74 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
75 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
76 #ifdef _WIN32
77 #include <process.h>
78 static int execvp_win32(const char *prog, char **argv)
80 int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv);
81 if (-1 == ret)
82 return ret;
83 cwait(&ret, ret, WAIT_CHILD);
84 exit(ret);
86 #define execvp execvp_win32
87 #endif
88 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
90 char child_path[4096], *child_name; const char *target;
91 switch (atoi(optarg)) {
92 #ifdef TCC_TARGET_I386
93 case 32: break;
94 case 64: target = "x86_64";
95 #else
96 case 64: break;
97 case 32: target = "i386";
98 #endif
99 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
100 child_name = tcc_basename(child_path);
101 strcpy(child_name, target);
102 #ifdef TCC_TARGET_PE
103 strcat(child_name, "-win32");
104 #endif
105 strcat(child_name, "-tcc");
106 if (strcmp(argv[0], child_path)) {
107 if (s->verbose > 0)
108 printf("tcc: using '%s'\n", child_name), fflush(stdout);
109 execvp(argv[0] = child_path, argv);
111 tcc_error("'%s' not found", child_name);
112 case 0: /* ignore -march etc. */
113 break;
114 default:
115 tcc_warning("unsupported option \"-m%s\"", optarg);
118 #else
119 #define exec_other_tcc(s, argv, optarg)
120 #endif
122 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
124 FILE *depout;
125 char buf[1024], *ext;
126 int i;
128 if (!filename) {
129 /* compute filename automatically
130 * dir/file.o -> dir/file.d */
131 pstrcpy(buf, sizeof(buf), target);
132 ext = tcc_fileextension(buf);
133 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
134 filename = buf;
137 if (s->verbose)
138 printf("<- %s\n", filename);
140 /* XXX return err codes instead of error() ? */
141 depout = fopen(filename, "w");
142 if (!depout)
143 tcc_error("could not open '%s'", filename);
145 fprintf(depout, "%s : \\\n", target);
146 for (i=0; i<s->nb_target_deps; ++i)
147 fprintf(depout, " %s \\\n", s->target_deps[i]);
148 fprintf(depout, "\n");
149 fclose(depout);
152 static char *default_outputfile(TCCState *s, const char *first_file)
154 char buf[1024];
155 char *ext;
156 const char *name = "a";
158 if (first_file && strcmp(first_file, "-"))
159 name = tcc_basename(first_file);
160 pstrcpy(buf, sizeof(buf), name);
161 ext = tcc_fileextension(buf);
162 #ifdef TCC_TARGET_PE
163 if (s->output_type == TCC_OUTPUT_DLL)
164 strcpy(ext, ".dll");
165 else
166 if (s->output_type == TCC_OUTPUT_EXE)
167 strcpy(ext, ".exe");
168 else
169 #endif
170 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
171 (s->output_type == TCC_OUTPUT_PREPROCESS) )
172 && *ext)
173 strcpy(ext, ".o");
174 else
175 strcpy(buf, "a.out");
177 return tcc_strdup(buf);
180 static void print_paths(const char *msg, char **paths, int nb_paths)
182 int i;
183 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
184 for(i = 0; i < nb_paths; i++)
185 printf(" %s\n", paths[i]);
188 static void display_info(TCCState *s, int what)
190 switch (what) {
191 case 0:
192 printf("tcc version %s ("
193 #ifdef TCC_TARGET_I386
194 "i386"
195 # ifdef TCC_TARGET_PE
196 " Win32"
197 # endif
198 #elif defined TCC_TARGET_X86_64
199 "x86-64"
200 # ifdef TCC_TARGET_PE
201 " Win64"
202 # endif
203 #elif defined TCC_TARGET_ARM
204 "ARM"
205 # ifdef TCC_ARM_HARDFLOAT
206 " Hard Float"
207 # endif
208 # ifdef TCC_TARGET_PE
209 " WinCE"
210 # endif
211 #endif
212 #ifndef TCC_TARGET_PE
213 # ifdef __linux
214 " Linux"
215 # endif
216 #endif
217 ")\n", TCC_VERSION);
218 break;
219 case 1:
220 printf("install: %s/\n", s->tcc_lib_path);
221 /* print_paths("programs", NULL, 0); */
222 print_paths("crt", s->crt_paths, s->nb_crt_paths);
223 print_paths("libraries", s->library_paths, s->nb_library_paths);
224 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
225 break;
229 static int64_t getclock_us(void)
231 #ifdef _WIN32
232 struct _timeb tb;
233 _ftime(&tb);
234 return (tb.time * 1000LL + tb.millitm) * 1000LL;
235 #else
236 struct timeval tv;
237 gettimeofday(&tv, NULL);
238 return tv.tv_sec * 1000000LL + tv.tv_usec;
239 #endif
242 int main(int argc, char **argv)
244 TCCState *s;
245 int ret, optind, i, bench;
246 int64_t start_time = 0;
247 const char *first_file = NULL;
249 s = tcc_new();
250 s->output_type = TCC_OUTPUT_EXE;
252 optind = tcc_parse_args(s, argc - 1, argv + 1);
254 if (optind == 0) {
255 help();
256 return 1;
259 if (s->option_m)
260 exec_other_tcc(s, argv, s->option_m);
262 if (s->verbose)
263 display_info(s, 0);
265 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
266 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
267 display_info(s, 1);
268 return 0;
271 if (s->verbose && optind == 1)
272 return 0;
274 if (s->nb_files == 0)
275 tcc_error("no input files\n");
277 /* check -c consistency : only single file handled. XXX: checks file type */
278 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
279 if (s->nb_libraries != 0)
280 tcc_error("cannot specify libraries with -c");
281 /* accepts only a single input file */
282 if (s->nb_files != 1)
283 tcc_error("cannot specify multiple files with -c");
286 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
287 if (!s->outfile) {
288 s->ppfp = stdout;
289 } else {
290 s->ppfp = fopen(s->outfile, "w");
291 if (!s->ppfp)
292 tcc_error("could not write '%s'", s->outfile);
296 bench = s->do_bench;
297 if (bench)
298 start_time = getclock_us();
300 tcc_set_output_type(s, s->output_type);
302 /* compile or add each files or library */
303 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
304 const char *filename;
306 filename = s->files[i];
307 if (filename[0] == '-' && filename[1] == 'l') {
308 if (tcc_add_library(s, filename + 2) < 0) {
309 tcc_error_noabort("cannot find '%s'", filename);
310 ret = 1;
312 } else {
313 if (1 == s->verbose)
314 printf("-> %s\n", filename);
315 if (tcc_add_file(s, filename) < 0)
316 ret = 1;
317 if (!first_file)
318 first_file = filename;
322 if (0 == ret) {
323 if (bench)
324 tcc_print_stats(s, getclock_us() - start_time);
326 if (s->output_type == TCC_OUTPUT_MEMORY) {
327 #ifdef TCC_IS_NATIVE
328 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
329 #else
330 tcc_error_noabort("-run is not available in a cross compiler");
331 ret = 1;
332 #endif
333 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
334 if (s->outfile)
335 fclose(s->ppfp);
336 } else {
337 if (!s->outfile)
338 s->outfile = default_outputfile(s, first_file);
339 ret = !!tcc_output_file(s, s->outfile);
340 /* dump collected dependencies */
341 if (s->gen_deps && !ret)
342 gen_makedeps(s, s->outfile, s->deps_outfile);
346 tcc_delete(s);
347 if (bench)
348 tcc_memstats();
349 return ret;