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