tcc: -m32 prefix "win32-" when file extension is present
[tinycc.git] / tcc.c
blob8fdf68ccc22178c381c87d3c032f06159c7277ff
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 #if defined(TCC_TARGET_X86_64) || defined (TCC_TARGET_I386)
217 #ifdef _WIN32
218 #define CAST (const char * const *)
219 #else
220 #define CAST (char * const *)
221 #endif
223 #if defined(TCC_TARGET_X86_64)
224 #define ARG 32
225 #define CHILD "i386"
226 #elif defined (TCC_TARGET_I386)
227 #define ARG 64
228 #define CHILD "x86_64"
229 #endif
231 static char *ssuffix(const char *oldname, const char sep)
233 char *p, *name = tcc_strdup(oldname);
234 p = strchr(name, sep);
235 if (p)
236 return p + 1;
237 /* prefix "win32-" when file extension is present */
238 if (*tcc_fileextension(name)){
239 name = tcc_realloc(name, strlen(oldname + 7));
240 sprintf(name, "win32-%s", oldname);
242 return name;
245 static void exec_other_tcc(TCCState *s, int argc,
246 char **argv,const char *optarg, int optind)
248 char child_path[4096],child_name[4096];
249 char *parent,*child_tcc;
250 int opt = atoi(optarg);
251 if (strlen(argv[0]) > 4000)
252 error("-m%i unsafe path length", ARG);
253 switch (opt) {
254 case ARG + 1: /* oops we called ourselves */
255 error("-m%i cross compiler not installed", ARG);
256 break;
257 case ARG:
259 parent = tcc_basename(argv[0]);
260 child_tcc = ssuffix(parent,'-');
261 sprintf(child_name, CHILD "-%s", child_tcc);
262 tcc_free(child_tcc);
263 if (strcmp(parent, child_name)) {
264 /* child_path = dirname */
265 pstrcpy(child_path, parent - argv[0] + 1, argv[0]);
266 child_tcc = strchr(child_path, 0);
267 strcpy(child_tcc, child_name);
268 if (0 < s->verbose)
269 printf("%s -m%i -> %s\n",
270 parent, ARG, child_path);
271 sprintf(argv[optind],"-m%i", ARG + 1); /* no loop */
272 execvp(child_path, CAST argv);
273 sprintf(child_tcc,"tcc%s",tcc_fileextension(parent));
274 execvp(child_path, CAST argv);
275 error("-m%i cross compiler not found", ARG);
276 } else error("-m%i unsupported configuration", ARG);
278 case 96 ^ ARG : break;
279 case 96 ^ (ARG + 1): break;
280 default:
281 warning("usupported option \"-m%s\"",optarg);
284 #undef CAST
285 #undef ARG
286 #undef CHILD
287 #endif
289 static int parse_args(TCCState *s, int argc, char **argv)
291 int optind;
292 const TCCOption *popt;
293 const char *optarg, *p1, *r1;
294 char *r;
296 optind = 0;
297 while (optind < argc) {
299 r = argv[optind++];
300 if (r[0] != '-' || r[1] == '\0') {
301 /* add a new file */
302 dynarray_add((void ***)&files, &nb_files, r);
303 if (!multiple_files) {
304 optind--;
305 /* argv[0] will be this file */
306 break;
308 } else {
309 /* find option in table (match only the first chars */
310 popt = tcc_options;
311 for(;;) {
312 p1 = popt->name;
313 if (p1 == NULL)
314 error("invalid option -- '%s'", r);
315 r1 = r + 1;
316 for(;;) {
317 if (*p1 == '\0')
318 goto option_found;
319 if (*r1 != *p1)
320 break;
321 p1++;
322 r1++;
324 popt++;
326 option_found:
327 if (popt->flags & TCC_OPTION_HAS_ARG) {
328 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
329 optarg = r1;
330 } else {
331 if (optind >= argc)
332 error("argument to '%s' is missing", r);
333 optarg = argv[optind++];
335 } else {
336 if (*r1 != '\0')
337 return 0;
338 optarg = NULL;
341 switch(popt->index) {
342 case TCC_OPTION_HELP:
343 return 0;
345 case TCC_OPTION_I:
346 if (tcc_add_include_path(s, optarg) < 0)
347 error("too many include paths");
348 break;
349 case TCC_OPTION_D:
351 char *sym, *value;
352 sym = (char *)optarg;
353 value = strchr(sym, '=');
354 if (value) {
355 *value = '\0';
356 value++;
358 tcc_define_symbol(s, sym, value);
360 break;
361 case TCC_OPTION_U:
362 tcc_undefine_symbol(s, optarg);
363 break;
364 case TCC_OPTION_L:
365 tcc_add_library_path(s, optarg);
366 break;
367 case TCC_OPTION_B:
368 /* set tcc utilities path (mainly for tcc development) */
369 tcc_set_lib_path(s, optarg);
370 break;
371 case TCC_OPTION_l:
372 dynarray_add((void ***)&files, &nb_files, r);
373 nb_libraries++;
374 break;
375 case TCC_OPTION_pthread:
376 /* fixme: these options could be different on your platform */
377 if(output_type != TCC_OUTPUT_OBJ){
378 dynarray_add((void ***)&files, &nb_files, "-lpthread");
379 nb_libraries++;
381 tcc_define_symbol(s, "_REENTRANT", "1");
382 break;
383 case TCC_OPTION_bench:
384 do_bench = 1;
385 break;
386 #ifdef CONFIG_TCC_BACKTRACE
387 case TCC_OPTION_bt:
388 set_num_callers(atoi(optarg));
389 break;
390 #endif
391 #ifdef CONFIG_TCC_BCHECK
392 case TCC_OPTION_b:
393 s->do_bounds_check = 1;
394 s->do_debug = 1;
395 break;
396 #endif
397 case TCC_OPTION_g:
398 s->do_debug = 1;
399 break;
400 case TCC_OPTION_c:
401 multiple_files = 1;
402 output_type = TCC_OUTPUT_OBJ;
403 break;
404 case TCC_OPTION_static:
405 s->static_link = 1;
406 break;
407 case TCC_OPTION_shared:
408 output_type = TCC_OUTPUT_DLL;
409 break;
410 case TCC_OPTION_soname:
411 s->soname = optarg;
412 break;
413 #if defined(TCC_TARGET_X86_64) || defined (TCC_TARGET_I386)
414 case TCC_OPTION_m:
415 exec_other_tcc(s, argc+1, argv-1, optarg, optind);
416 break;
417 #endif
418 case TCC_OPTION_o:
419 multiple_files = 1;
420 outfile = optarg;
421 break;
422 case TCC_OPTION_r:
423 /* generate a .o merging several output files */
424 reloc_output = 1;
425 output_type = TCC_OUTPUT_OBJ;
426 break;
427 case TCC_OPTION_nostdinc:
428 s->nostdinc = 1;
429 break;
430 case TCC_OPTION_nostdlib:
431 s->nostdlib = 1;
432 break;
433 case TCC_OPTION_print_search_dirs:
434 print_search_dirs = 1;
435 break;
436 case TCC_OPTION_run:
438 int argc1;
439 char **argv1;
440 argc1 = expand_args(&argv1, optarg);
441 if (argc1 > 0) {
442 parse_args(s, argc1, argv1);
444 multiple_files = 0;
445 output_type = TCC_OUTPUT_MEMORY;
447 break;
448 case TCC_OPTION_v:
449 do {
450 if (0 == s->verbose++)
451 printf("tcc version %s\n", TCC_VERSION);
452 } while (*optarg++ == 'v');
453 break;
454 case TCC_OPTION_f:
455 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
456 goto unsupported_option;
457 break;
458 case TCC_OPTION_W:
459 if (tcc_set_warning(s, optarg, 1) < 0 &&
460 s->warn_unsupported)
461 goto unsupported_option;
462 break;
463 case TCC_OPTION_w:
464 s->warn_none = 1;
465 break;
466 case TCC_OPTION_rdynamic:
467 s->rdynamic = 1;
468 break;
469 case TCC_OPTION_Wl:
471 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
472 error("unsupported linker option '%s'", r);
474 break;
475 case TCC_OPTION_E:
476 output_type = TCC_OUTPUT_PREPROCESS;
477 break;
478 case TCC_OPTION_MD:
479 gen_deps = 1;
480 break;
481 case TCC_OPTION_MF:
482 deps_outfile = optarg;
483 break;
484 case TCC_OPTION_x:
485 break;
486 default:
487 if (s->warn_unsupported) {
488 unsupported_option:
489 warning("unsupported option '%s'", r);
491 break;
495 return optind + 1;
498 int main(int argc, char **argv)
500 int i;
501 TCCState *s;
502 int nb_objfiles, ret, optind;
503 int64_t start_time = 0;
505 s = tcc_new();
507 output_type = TCC_OUTPUT_EXE;
508 outfile = NULL;
509 multiple_files = 1;
510 files = NULL;
511 nb_files = 0;
512 nb_libraries = 0;
513 reloc_output = 0;
514 print_search_dirs = 0;
515 ret = 0;
517 optind = parse_args(s, argc - 1, argv + 1);
518 if (print_search_dirs) {
519 /* enough for Linux kernel */
520 printf("install: %s/\n", s->tcc_lib_path);
521 return 0;
523 if (optind == 0 || nb_files == 0) {
524 if (optind && s->verbose)
525 return 0;
526 help();
527 return 1;
530 nb_objfiles = nb_files - nb_libraries;
532 /* if outfile provided without other options, we output an
533 executable */
534 if (outfile && output_type == TCC_OUTPUT_MEMORY)
535 output_type = TCC_OUTPUT_EXE;
537 /* check -c consistency : only single file handled. XXX: checks file type */
538 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
539 /* accepts only a single input file */
540 if (nb_objfiles != 1)
541 error("cannot specify multiple files with -c");
542 if (nb_libraries != 0)
543 error("cannot specify libraries with -c");
547 if (output_type == TCC_OUTPUT_PREPROCESS) {
548 if (!outfile) {
549 s->outfile = stdout;
550 } else {
551 s->outfile = fopen(outfile, "w");
552 if (!s->outfile)
553 error("could not open '%s'", outfile);
557 if (do_bench) {
558 start_time = getclock_us();
561 tcc_set_output_type(s, output_type);
562 s->reloc_output = reloc_output;
564 /* compile or add each files or library */
565 for(i = 0; i < nb_files && ret == 0; i++) {
566 const char *filename;
568 filename = files[i];
569 if (filename[0] == '-' && filename[1] == 'l') {
570 if (tcc_add_library(s, filename + 2) < 0) {
571 error_noabort("cannot find %s", filename);
572 ret = 1;
574 } else {
575 if (1 == s->verbose)
576 printf("-> %s\n", filename);
577 if (tcc_add_file(s, filename) < 0)
578 ret = 1;
582 /* free all files */
583 tcc_free(files);
585 if (0 == ret) {
586 if (do_bench)
587 tcc_print_stats(s, getclock_us() - start_time);
589 if (s->output_type == TCC_OUTPUT_MEMORY)
590 ret = tcc_run(s, argc - optind, argv + optind);
591 else {
592 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
593 if (outfile)
594 fclose(s->outfile);
595 } else {
596 ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
597 ret = ret ? 1 : 0;
600 /* dump collected dependencies */
601 if (gen_deps && !ret)
602 tcc_gen_makedeps(s, outfile, deps_outfile);
606 tcc_delete(s);
608 #ifdef MEM_DEBUG
609 if (do_bench) {
610 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
612 #endif
613 return ret;