tcc: add -m32 option to x86_64 cross compilers
[tinycc.git] / tcc.c
blob692d355fd463eecf31906a2951469518d8f9f301
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 #if defined NOTALLINONE || defined TCC_USE_LIBTCC
22 #include "tcc.h"
23 #else
24 #include "libtcc.c"
25 #endif
27 static char **files;
28 static int nb_files, nb_libraries;
29 static int multiple_files;
30 static int print_search_dirs;
31 static int output_type;
32 static int reloc_output;
33 static const char *outfile;
34 static int do_bench = 0;
35 static int gen_deps;
36 static const char *deps_outfile;
38 #define TCC_OPTION_HAS_ARG 0x0001
39 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
41 static void help(void)
43 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
44 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
45 " tcc [options...] -run infile [arguments...]\n"
46 "General options:\n"
47 " -v display current version, increase verbosity\n"
48 " -c compile only - generate an object file\n"
49 " -o outfile set output filename\n"
50 " -Bdir set tcc internal library path\n"
51 " -bench output compilation statistics\n"
52 " -run run compiled source\n"
53 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
54 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
55 " -w disable all warnings\n"
56 "Preprocessor options:\n"
57 " -E preprocess only\n"
58 " -Idir add include path 'dir'\n"
59 " -Dsym[=val] define 'sym' with value 'val'\n"
60 " -Usym undefine 'sym'\n"
61 "Linker options:\n"
62 " -Ldir add library path 'dir'\n"
63 " -llib link with dynamic or static library 'lib'\n"
64 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
65 " -shared generate a shared library\n"
66 " -soname set name for shared library to be used at runtime\n"
67 " -static static linking\n"
68 " -rdynamic export all global symbols to dynamic linker\n"
69 " -r generate (relocatable) object file\n"
70 "Debugger options:\n"
71 " -g generate runtime debug info\n"
72 #ifdef CONFIG_TCC_BCHECK
73 " -b compile with built-in memory and bounds checker (implies -g)\n"
74 #endif
75 #ifdef CONFIG_TCC_BACKTRACE
76 " -bt N show N callers in stack traces\n"
77 #endif
78 "Misc options:\n"
79 " -MD generate target dependencies for make\n"
80 " -MF depfile put generated dependencies here\n"
84 typedef struct TCCOption {
85 const char *name;
86 uint16_t index;
87 uint16_t flags;
88 } TCCOption;
90 enum {
91 TCC_OPTION_HELP,
92 TCC_OPTION_I,
93 TCC_OPTION_D,
94 TCC_OPTION_U,
95 TCC_OPTION_L,
96 TCC_OPTION_B,
97 TCC_OPTION_l,
98 TCC_OPTION_bench,
99 TCC_OPTION_bt,
100 TCC_OPTION_b,
101 TCC_OPTION_g,
102 TCC_OPTION_c,
103 TCC_OPTION_static,
104 TCC_OPTION_shared,
105 TCC_OPTION_soname,
106 TCC_OPTION_o,
107 TCC_OPTION_r,
108 TCC_OPTION_s,
109 TCC_OPTION_Wl,
110 TCC_OPTION_W,
111 TCC_OPTION_O,
112 TCC_OPTION_m,
113 TCC_OPTION_f,
114 TCC_OPTION_nostdinc,
115 TCC_OPTION_nostdlib,
116 TCC_OPTION_print_search_dirs,
117 TCC_OPTION_rdynamic,
118 TCC_OPTION_pthread,
119 TCC_OPTION_run,
120 TCC_OPTION_v,
121 TCC_OPTION_w,
122 TCC_OPTION_pipe,
123 TCC_OPTION_E,
124 TCC_OPTION_MD,
125 TCC_OPTION_MF,
126 TCC_OPTION_x,
129 static const TCCOption tcc_options[] = {
130 { "h", TCC_OPTION_HELP, 0 },
131 { "-help", TCC_OPTION_HELP, 0 },
132 { "?", TCC_OPTION_HELP, 0 },
133 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
134 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
135 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
136 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
137 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
138 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
139 { "bench", TCC_OPTION_bench, 0 },
140 #ifdef CONFIG_TCC_BACKTRACE
141 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
142 #endif
143 #ifdef CONFIG_TCC_BCHECK
144 { "b", TCC_OPTION_b, 0 },
145 #endif
146 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
147 { "c", TCC_OPTION_c, 0 },
148 { "static", TCC_OPTION_static, 0 },
149 { "shared", TCC_OPTION_shared, 0 },
150 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
151 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
152 { "pthread", TCC_OPTION_pthread, 0},
153 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
154 { "rdynamic", TCC_OPTION_rdynamic, 0 },
155 { "r", TCC_OPTION_r, 0 },
156 { "s", TCC_OPTION_s, 0 },
157 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
158 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
159 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
160 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
161 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
162 { "nostdinc", TCC_OPTION_nostdinc, 0 },
163 { "nostdlib", TCC_OPTION_nostdlib, 0 },
164 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
165 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
166 { "w", TCC_OPTION_w, 0 },
167 { "pipe", TCC_OPTION_pipe, 0},
168 { "E", TCC_OPTION_E, 0},
169 { "MD", TCC_OPTION_MD, 0},
170 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
171 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
172 { NULL },
175 static int64_t getclock_us(void)
177 #ifdef _WIN32
178 struct _timeb tb;
179 _ftime(&tb);
180 return (tb.time * 1000LL + tb.millitm) * 1000LL;
181 #else
182 struct timeval tv;
183 gettimeofday(&tv, NULL);
184 return tv.tv_sec * 1000000LL + tv.tv_usec;
185 #endif
188 /* convert 'str' into an array of space separated strings */
189 static int expand_args(char ***pargv, const char *str)
191 const char *s1;
192 char **argv, *arg;
193 int argc, len;
195 argc = 0;
196 argv = NULL;
197 for(;;) {
198 while (is_space(*str))
199 str++;
200 if (*str == '\0')
201 break;
202 s1 = str;
203 while (*str != '\0' && !is_space(*str))
204 str++;
205 len = str - s1;
206 arg = tcc_malloc(len + 1);
207 memcpy(arg, s1, len);
208 arg[len] = '\0';
209 dynarray_add((void ***)&argv, &argc, arg);
211 *pargv = argv;
212 return argc;
215 void exec_other_tcc(TCCState *s, int argc,char **argv,int optarg)
217 #ifdef TCC_TARGET_X86_64
218 #define WIN32_CAST
219 char arg[20]="i386-tcc";
220 char sep='/',*tcc;
221 if (32 == optarg) {/* -m32 launches 32 bit tcc */
222 #ifdef TCC_TARGET_PE
223 #ifdef _WIN32
224 #define WIN32_CAST (const char**)(const char *)
225 strcpy(arg,"tcc.exe");
226 sep='\\';
227 #else
228 strcpy(arg,"i386-win32-tcc");
229 #endif
230 #endif
231 if (!(tcc=strrchr(argv[0],sep)))
232 tcc=argv[0];
233 else tcc++;
234 if (0<s->verbose) printf("%s->%s\n",tcc,arg);
235 if (strcmp(tcc,arg)) {
236 execvp(arg, WIN32_CAST (argv));
237 error("cross compiler not found!");
238 exit(1);
239 #undef WIN32_CAST
240 } else warning("-m32 infinite loop prevented");
241 } else warning("usupported option \"-m%s\"",optarg);
242 #endif
245 static int parse_args(TCCState *s, int argc, char **argv)
247 int optind;
248 const TCCOption *popt;
249 const char *optarg, *p1, *r1;
250 char *r;
252 optind = 0;
253 while (optind < argc) {
255 r = argv[optind++];
256 if (r[0] != '-' || r[1] == '\0') {
257 /* add a new file */
258 dynarray_add((void ***)&files, &nb_files, r);
259 if (!multiple_files) {
260 optind--;
261 /* argv[0] will be this file */
262 break;
264 } else {
265 /* find option in table (match only the first chars */
266 popt = tcc_options;
267 for(;;) {
268 p1 = popt->name;
269 if (p1 == NULL)
270 error("invalid option -- '%s'", r);
271 r1 = r + 1;
272 for(;;) {
273 if (*p1 == '\0')
274 goto option_found;
275 if (*r1 != *p1)
276 break;
277 p1++;
278 r1++;
280 popt++;
282 option_found:
283 if (popt->flags & TCC_OPTION_HAS_ARG) {
284 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
285 optarg = r1;
286 } else {
287 if (optind >= argc)
288 error("argument to '%s' is missing", r);
289 optarg = argv[optind++];
291 } else {
292 if (*r1 != '\0')
293 return 0;
294 optarg = NULL;
297 switch(popt->index) {
298 case TCC_OPTION_HELP:
299 return 0;
301 case TCC_OPTION_I:
302 if (tcc_add_include_path(s, optarg) < 0)
303 error("too many include paths");
304 break;
305 case TCC_OPTION_D:
307 char *sym, *value;
308 sym = (char *)optarg;
309 value = strchr(sym, '=');
310 if (value) {
311 *value = '\0';
312 value++;
314 tcc_define_symbol(s, sym, value);
316 break;
317 case TCC_OPTION_U:
318 tcc_undefine_symbol(s, optarg);
319 break;
320 case TCC_OPTION_L:
321 tcc_add_library_path(s, optarg);
322 break;
323 case TCC_OPTION_B:
324 /* set tcc utilities path (mainly for tcc development) */
325 tcc_set_lib_path(s, optarg);
326 break;
327 case TCC_OPTION_l:
328 dynarray_add((void ***)&files, &nb_files, r);
329 nb_libraries++;
330 break;
331 case TCC_OPTION_pthread:
332 /* fixme: these options could be different on your platform */
333 if(output_type != TCC_OUTPUT_OBJ){
334 dynarray_add((void ***)&files, &nb_files, "-lpthread");
335 nb_libraries++;
337 tcc_define_symbol(s, "_REENTRANT", "1");
338 break;
339 case TCC_OPTION_bench:
340 do_bench = 1;
341 break;
342 #ifdef CONFIG_TCC_BACKTRACE
343 case TCC_OPTION_bt:
344 set_num_callers(atoi(optarg));
345 break;
346 #endif
347 #ifdef CONFIG_TCC_BCHECK
348 case TCC_OPTION_b:
349 s->do_bounds_check = 1;
350 s->do_debug = 1;
351 break;
352 #endif
353 case TCC_OPTION_g:
354 s->do_debug = 1;
355 break;
356 case TCC_OPTION_c:
357 multiple_files = 1;
358 output_type = TCC_OUTPUT_OBJ;
359 break;
360 case TCC_OPTION_static:
361 s->static_link = 1;
362 break;
363 case TCC_OPTION_shared:
364 output_type = TCC_OUTPUT_DLL;
365 break;
366 case TCC_OPTION_soname:
367 s->soname = optarg;
368 break;
369 case TCC_OPTION_m:
370 exec_other_tcc(s,argc+1,argv-1,atoi(optarg));
371 break;
372 case TCC_OPTION_o:
373 multiple_files = 1;
374 outfile = optarg;
375 break;
376 case TCC_OPTION_r:
377 /* generate a .o merging several output files */
378 reloc_output = 1;
379 output_type = TCC_OUTPUT_OBJ;
380 break;
381 case TCC_OPTION_nostdinc:
382 s->nostdinc = 1;
383 break;
384 case TCC_OPTION_nostdlib:
385 s->nostdlib = 1;
386 break;
387 case TCC_OPTION_print_search_dirs:
388 print_search_dirs = 1;
389 break;
390 case TCC_OPTION_run:
392 int argc1;
393 char **argv1;
394 argc1 = expand_args(&argv1, optarg);
395 if (argc1 > 0) {
396 parse_args(s, argc1, argv1);
398 multiple_files = 0;
399 output_type = TCC_OUTPUT_MEMORY;
401 break;
402 case TCC_OPTION_v:
403 do {
404 if (0 == s->verbose++)
405 printf("tcc version %s\n", TCC_VERSION);
406 } while (*optarg++ == 'v');
407 break;
408 case TCC_OPTION_f:
409 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
410 goto unsupported_option;
411 break;
412 case TCC_OPTION_W:
413 if (tcc_set_warning(s, optarg, 1) < 0 &&
414 s->warn_unsupported)
415 goto unsupported_option;
416 break;
417 case TCC_OPTION_w:
418 s->warn_none = 1;
419 break;
420 case TCC_OPTION_rdynamic:
421 s->rdynamic = 1;
422 break;
423 case TCC_OPTION_Wl:
425 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
426 error("unsupported linker option '%s'", r);
428 break;
429 case TCC_OPTION_E:
430 output_type = TCC_OUTPUT_PREPROCESS;
431 break;
432 case TCC_OPTION_MD:
433 gen_deps = 1;
434 break;
435 case TCC_OPTION_MF:
436 deps_outfile = optarg;
437 break;
438 case TCC_OPTION_x:
439 break;
440 default:
441 if (s->warn_unsupported) {
442 unsupported_option:
443 warning("unsupported option '%s'", r);
445 break;
449 return optind + 1;
452 int main(int argc, char **argv)
454 int i;
455 TCCState *s;
456 int nb_objfiles, ret, optind;
457 int64_t start_time = 0;
459 s = tcc_new();
461 output_type = TCC_OUTPUT_EXE;
462 outfile = NULL;
463 multiple_files = 1;
464 files = NULL;
465 nb_files = 0;
466 nb_libraries = 0;
467 reloc_output = 0;
468 print_search_dirs = 0;
469 ret = 0;
471 optind = parse_args(s, argc - 1, argv + 1);
472 if (print_search_dirs) {
473 /* enough for Linux kernel */
474 printf("install: %s/\n", s->tcc_lib_path);
475 return 0;
477 if (optind == 0 || nb_files == 0) {
478 if (optind && s->verbose)
479 return 0;
480 help();
481 return 1;
484 nb_objfiles = nb_files - nb_libraries;
486 /* if outfile provided without other options, we output an
487 executable */
488 if (outfile && output_type == TCC_OUTPUT_MEMORY)
489 output_type = TCC_OUTPUT_EXE;
491 /* check -c consistency : only single file handled. XXX: checks file type */
492 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
493 /* accepts only a single input file */
494 if (nb_objfiles != 1)
495 error("cannot specify multiple files with -c");
496 if (nb_libraries != 0)
497 error("cannot specify libraries with -c");
501 if (output_type == TCC_OUTPUT_PREPROCESS) {
502 if (!outfile) {
503 s->outfile = stdout;
504 } else {
505 s->outfile = fopen(outfile, "w");
506 if (!s->outfile)
507 error("could not open '%s'", outfile);
511 if (do_bench) {
512 start_time = getclock_us();
515 tcc_set_output_type(s, output_type);
516 s->reloc_output = reloc_output;
518 /* compile or add each files or library */
519 for(i = 0; i < nb_files && ret == 0; i++) {
520 const char *filename;
522 filename = files[i];
523 if (filename[0] == '-' && filename[1] == 'l') {
524 if (tcc_add_library(s, filename + 2) < 0) {
525 error_noabort("cannot find %s", filename);
526 ret = 1;
528 } else {
529 if (1 == s->verbose)
530 printf("-> %s\n", filename);
531 if (tcc_add_file(s, filename) < 0)
532 ret = 1;
536 /* free all files */
537 tcc_free(files);
539 if (0 == ret) {
540 if (do_bench)
541 tcc_print_stats(s, getclock_us() - start_time);
543 if (s->output_type == TCC_OUTPUT_MEMORY)
544 ret = tcc_run(s, argc - optind, argv + optind);
545 else {
546 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
547 if (outfile)
548 fclose(s->outfile);
549 } else {
550 ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
551 ret = ret ? 1 : 0;
554 /* dump collected dependencies */
555 if (gen_deps && !ret)
556 tcc_gen_makedeps(s, outfile, deps_outfile);
560 tcc_delete(s);
562 #ifdef MEM_DEBUG
563 if (do_bench) {
564 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
566 #endif
567 return ret;