tcc.c: fix an error when you build an object file with '-pthread' key set
[tinycc.git] / tcc.c
blobe0d6386aa7f352788d0dd72b876d5301a223d54a
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_pedantic,
119 TCC_OPTION_pthread,
120 TCC_OPTION_run,
121 TCC_OPTION_v,
122 TCC_OPTION_w,
123 TCC_OPTION_pipe,
124 TCC_OPTION_E,
125 TCC_OPTION_MD,
126 TCC_OPTION_MF,
127 TCC_OPTION_x,
130 static const TCCOption tcc_options[] = {
131 { "h", TCC_OPTION_HELP, 0 },
132 { "-help", TCC_OPTION_HELP, 0 },
133 { "?", TCC_OPTION_HELP, 0 },
134 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
135 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
136 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
137 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
138 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
139 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
140 { "bench", TCC_OPTION_bench, 0 },
141 #ifdef CONFIG_TCC_BACKTRACE
142 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
143 #endif
144 #ifdef CONFIG_TCC_BCHECK
145 { "b", TCC_OPTION_b, 0 },
146 #endif
147 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
148 { "c", TCC_OPTION_c, 0 },
149 { "static", TCC_OPTION_static, 0 },
150 { "shared", TCC_OPTION_shared, 0 },
151 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
152 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
153 { "pedantic", TCC_OPTION_pedantic, 0},
154 { "pthread", TCC_OPTION_pthread, 0},
155 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
156 { "rdynamic", TCC_OPTION_rdynamic, 0 },
157 { "r", TCC_OPTION_r, 0 },
158 { "s", TCC_OPTION_s, 0 },
159 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
160 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
161 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
162 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
163 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
164 { "nostdinc", TCC_OPTION_nostdinc, 0 },
165 { "nostdlib", TCC_OPTION_nostdlib, 0 },
166 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
167 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
168 { "w", TCC_OPTION_w, 0 },
169 { "pipe", TCC_OPTION_pipe, 0},
170 { "E", TCC_OPTION_E, 0},
171 { "MD", TCC_OPTION_MD, 0},
172 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
173 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
174 { NULL },
177 static int64_t getclock_us(void)
179 #ifdef _WIN32
180 struct _timeb tb;
181 _ftime(&tb);
182 return (tb.time * 1000LL + tb.millitm) * 1000LL;
183 #else
184 struct timeval tv;
185 gettimeofday(&tv, NULL);
186 return tv.tv_sec * 1000000LL + tv.tv_usec;
187 #endif
190 /* convert 'str' into an array of space separated strings */
191 static int expand_args(char ***pargv, const char *str)
193 const char *s1;
194 char **argv, *arg;
195 int argc, len;
197 argc = 0;
198 argv = NULL;
199 for(;;) {
200 while (is_space(*str))
201 str++;
202 if (*str == '\0')
203 break;
204 s1 = str;
205 while (*str != '\0' && !is_space(*str))
206 str++;
207 len = str - s1;
208 arg = tcc_malloc(len + 1);
209 memcpy(arg, s1, len);
210 arg[len] = '\0';
211 dynarray_add((void ***)&argv, &argc, arg);
213 *pargv = argv;
214 return argc;
217 #if defined(TCC_TARGET_X86_64) || defined (TCC_TARGET_I386)
219 #ifdef _WIN32
220 #define CAST (const char * const *)
221 #else
222 #define CAST (char * const *)
223 #endif
225 #if defined(TCC_TARGET_X86_64)
226 #define ARG 32
227 #define CHILD "i386"
228 #elif defined (TCC_TARGET_I386)
229 #define ARG 64
230 #define CHILD "x86_64"
231 #endif
233 static char *ssuffix(const char *oldname, const char sep)
235 char *p, *name = tcc_strdup(oldname);
236 p = strchr(name, sep);
237 if (p)
238 return p + 1;
239 /* prefix "win32-" when file extension is present */
240 if (*tcc_fileextension(name)){
241 name = tcc_realloc(name, strlen(oldname + 7));
242 sprintf(name, "win32-%s", oldname);
244 return name;
247 static void exec_other_tcc(TCCState *s, int argc,
248 char **argv,const char *optarg, int optind)
250 char child_path[4096],child_name[4096];
251 char *parent,*child_tcc;
252 int opt = atoi(optarg);
253 if (strlen(argv[0]) > 4000)
254 error("-m%i unsafe path length", ARG);
255 switch (opt) {
256 case ARG + 1: /* oops we called ourselves */
257 error("-m%i cross compiler not installed", ARG);
258 break;
259 case ARG:
261 parent = tcc_basename(argv[0]);
262 child_tcc = ssuffix(parent,'-');
263 sprintf(child_name, CHILD "-%s", child_tcc);
264 tcc_free(child_tcc);
265 if (strcmp(parent, child_name)) {
266 /* child_path = dirname */
267 pstrcpy(child_path, parent - argv[0] + 1, argv[0]);
268 child_tcc = strchr(child_path, 0);
269 strcpy(child_tcc, child_name);
270 if (0 < s->verbose)
271 printf("%s -m%i -> %s\n",
272 parent, ARG, child_path);
273 sprintf(argv[optind],"-m%i", ARG + 1); /* no loop */
274 execvp(child_path, CAST argv);
275 sprintf(child_tcc,"tcc%s",tcc_fileextension(parent));
276 execvp(child_path, CAST argv);
277 error("-m%i cross compiler not found", ARG);
278 } else error("-m%i unsupported configuration", ARG);
280 case 96 ^ ARG : break;
281 case 96 ^ (ARG + 1): break;
282 default:
283 warning("usupported option \"-m%s\"",optarg);
286 #undef CAST
287 #undef ARG
288 #undef CHILD
289 #endif
291 static int parse_args(TCCState *s, int argc, char **argv)
293 int optind;
294 const TCCOption *popt;
295 const char *optarg, *p1, *r1;
296 char *r;
297 int was_pthread;
299 was_pthread = 0; /* is set if commandline contains -pthread key */
301 optind = 0;
302 while (optind < argc) {
304 r = argv[optind++];
305 if (r[0] != '-' || r[1] == '\0') {
306 /* add a new file */
307 dynarray_add((void ***)&files, &nb_files, r);
308 if (!multiple_files) {
309 optind--;
310 /* argv[0] will be this file */
311 break;
313 } else {
314 /* find option in table (match only the first chars */
315 popt = tcc_options;
316 for(;;) {
317 p1 = popt->name;
318 if (p1 == NULL)
319 error("invalid option -- '%s'", r);
320 r1 = r + 1;
321 for(;;) {
322 if (*p1 == '\0')
323 goto option_found;
324 if (*r1 != *p1)
325 break;
326 p1++;
327 r1++;
329 popt++;
331 option_found:
332 if (popt->flags & TCC_OPTION_HAS_ARG) {
333 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
334 optarg = r1;
335 } else {
336 if (optind >= argc)
337 error("argument to '%s' is missing", r);
338 optarg = argv[optind++];
340 } else {
341 if (*r1 != '\0')
342 return 0;
343 optarg = NULL;
346 switch(popt->index) {
347 case TCC_OPTION_HELP:
348 return 0;
350 case TCC_OPTION_I:
351 if (tcc_add_include_path(s, optarg) < 0)
352 error("too many include paths");
353 break;
354 case TCC_OPTION_D:
356 char *sym, *value;
357 sym = (char *)optarg;
358 value = strchr(sym, '=');
359 if (value) {
360 *value = '\0';
361 value++;
363 tcc_define_symbol(s, sym, value);
365 break;
366 case TCC_OPTION_U:
367 tcc_undefine_symbol(s, optarg);
368 break;
369 case TCC_OPTION_L:
370 tcc_add_library_path(s, optarg);
371 break;
372 case TCC_OPTION_B:
373 /* set tcc utilities path (mainly for tcc development) */
374 tcc_set_lib_path(s, optarg);
375 break;
376 case TCC_OPTION_l:
377 dynarray_add((void ***)&files, &nb_files, r);
378 nb_libraries++;
379 break;
380 case TCC_OPTION_pthread:
381 was_pthread = 1;
382 tcc_define_symbol(s, "_REENTRANT", "1");
383 break;
384 case TCC_OPTION_bench:
385 do_bench = 1;
386 break;
387 #ifdef CONFIG_TCC_BACKTRACE
388 case TCC_OPTION_bt:
389 set_num_callers(atoi(optarg));
390 break;
391 #endif
392 #ifdef CONFIG_TCC_BCHECK
393 case TCC_OPTION_b:
394 s->do_bounds_check = 1;
395 s->do_debug = 1;
396 break;
397 #endif
398 case TCC_OPTION_g:
399 s->do_debug = 1;
400 break;
401 case TCC_OPTION_c:
402 multiple_files = 1;
403 output_type = TCC_OUTPUT_OBJ;
404 break;
405 case TCC_OPTION_static:
406 s->static_link = 1;
407 break;
408 case TCC_OPTION_shared:
409 output_type = TCC_OUTPUT_DLL;
410 break;
411 case TCC_OPTION_soname:
412 s->soname = optarg;
413 break;
414 #if defined(TCC_TARGET_X86_64) || defined (TCC_TARGET_I386)
415 case TCC_OPTION_m:
416 exec_other_tcc(s, argc+1, argv-1, optarg, optind);
417 break;
418 #endif
419 case TCC_OPTION_o:
420 multiple_files = 1;
421 outfile = optarg;
422 break;
423 case TCC_OPTION_r:
424 /* generate a .o merging several output files */
425 reloc_output = 1;
426 output_type = TCC_OUTPUT_OBJ;
427 break;
428 case TCC_OPTION_nostdinc:
429 s->nostdinc = 1;
430 break;
431 case TCC_OPTION_nostdlib:
432 s->nostdlib = 1;
433 break;
434 case TCC_OPTION_print_search_dirs:
435 print_search_dirs = 1;
436 break;
437 case TCC_OPTION_run:
439 int argc1;
440 char **argv1;
441 argc1 = expand_args(&argv1, optarg);
442 if (argc1 > 0) {
443 parse_args(s, argc1, argv1);
445 multiple_files = 0;
446 output_type = TCC_OUTPUT_MEMORY;
448 break;
449 case TCC_OPTION_v:
450 do {
451 if (0 == s->verbose++)
452 printf("tcc version %s\n", TCC_VERSION);
453 } while (*optarg++ == 'v');
454 break;
455 case TCC_OPTION_f:
456 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
457 goto unsupported_option;
458 break;
459 case TCC_OPTION_W:
460 if (tcc_set_warning(s, optarg, 1) < 0 &&
461 s->warn_unsupported)
462 goto unsupported_option;
463 break;
464 case TCC_OPTION_w:
465 s->warn_none = 1;
466 break;
467 case TCC_OPTION_rdynamic:
468 s->rdynamic = 1;
469 break;
470 case TCC_OPTION_Wl:
472 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
473 error("unsupported linker option '%s'", r);
475 break;
476 case TCC_OPTION_E:
477 output_type = TCC_OUTPUT_PREPROCESS;
478 break;
479 case TCC_OPTION_MD:
480 gen_deps = 1;
481 break;
482 case TCC_OPTION_MF:
483 deps_outfile = optarg;
484 break;
485 case TCC_OPTION_x:
486 break;
487 default:
488 if (s->warn_unsupported) {
489 unsupported_option:
490 warning("unsupported option '%s'", r);
492 break;
496 /* fixme: these options could be different on your platform */
497 if (was_pthread
498 && output_type != TCC_OUTPUT_OBJ)
500 dynarray_add((void ***)&files, &nb_files, "-lpthread");
501 nb_libraries++;
503 return optind + 1;
506 int main(int argc, char **argv)
508 int i;
509 TCCState *s;
510 int nb_objfiles, ret, optind;
511 int64_t start_time = 0;
513 s = tcc_new();
515 output_type = TCC_OUTPUT_EXE;
516 outfile = NULL;
517 multiple_files = 1;
518 files = NULL;
519 nb_files = 0;
520 nb_libraries = 0;
521 reloc_output = 0;
522 print_search_dirs = 0;
523 ret = 0;
525 optind = parse_args(s, argc - 1, argv + 1);
526 if (print_search_dirs) {
527 /* enough for Linux kernel */
528 printf("install: %s/\n", s->tcc_lib_path);
529 return 0;
531 if (optind == 0 || nb_files == 0) {
532 if (optind && s->verbose)
533 return 0;
534 help();
535 return 1;
538 nb_objfiles = nb_files - nb_libraries;
540 /* if outfile provided without other options, we output an
541 executable */
542 if (outfile && output_type == TCC_OUTPUT_MEMORY)
543 output_type = TCC_OUTPUT_EXE;
545 /* check -c consistency : only single file handled. XXX: checks file type */
546 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
547 /* accepts only a single input file */
548 if (nb_objfiles != 1)
549 error("cannot specify multiple files with -c");
550 if (nb_libraries != 0)
551 error("cannot specify libraries with -c");
555 if (output_type == TCC_OUTPUT_PREPROCESS) {
556 if (!outfile) {
557 s->outfile = stdout;
558 } else {
559 s->outfile = fopen(outfile, "w");
560 if (!s->outfile)
561 error("could not open '%s'", outfile);
565 if (do_bench) {
566 start_time = getclock_us();
569 tcc_set_output_type(s, output_type);
570 s->reloc_output = reloc_output;
572 /* compile or add each files or library */
573 for(i = 0; i < nb_files && ret == 0; i++) {
574 const char *filename;
576 filename = files[i];
577 if (filename[0] == '-' && filename[1] == 'l') {
578 if (tcc_add_library(s, filename + 2) < 0) {
579 error_noabort("cannot find %s", filename);
580 ret = 1;
582 } else {
583 if (1 == s->verbose)
584 printf("-> %s\n", filename);
585 if (tcc_add_file(s, filename) < 0)
586 ret = 1;
590 /* free all files */
591 tcc_free(files);
593 if (0 == ret) {
594 if (do_bench)
595 tcc_print_stats(s, getclock_us() - start_time);
597 if (s->output_type == TCC_OUTPUT_MEMORY)
598 ret = tcc_run(s, argc - optind, argv + optind);
599 else {
600 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
601 if (outfile)
602 fclose(s->outfile);
603 } else {
604 ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
605 ret = ret ? 1 : 0;
608 /* dump collected dependencies */
609 if (gen_deps && !ret)
610 tcc_gen_makedeps(s, outfile, deps_outfile);
614 tcc_delete(s);
616 #ifdef MEM_DEBUG
617 if (do_bench) {
618 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
620 #endif
621 return ret;