libtcc: minor adjustments
[tinycc.git] / tcc.c
blobada67a0b04ae9eb6d79cc3c8ea1a98cd8d490f6e
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 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 char *outfile;
34 static int do_bench = 0;
35 static int gen_deps;
36 static const char *deps_outfile;
37 static const char *m_option;
39 #define TCC_OPTION_HAS_ARG 0x0001
40 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
42 static void help(void)
44 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
45 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
46 " tcc [options...] -run infile [arguments...]\n"
47 "General options:\n"
48 " -v display current version, increase verbosity\n"
49 " -c compile only - generate an object file\n"
50 " -o outfile set output filename\n"
51 " -Bdir set tcc internal library and include path\n"
52 " -bench output compilation statistics\n"
53 " -run run compiled source\n"
54 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
55 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
56 " -w disable all warnings\n"
57 "Preprocessor options:\n"
58 " -E preprocess only\n"
59 " -Idir add include path 'dir'\n"
60 " -Dsym[=val] define 'sym' with value 'val'\n"
61 " -Usym undefine 'sym'\n"
62 "Linker options:\n"
63 " -Ldir add library path 'dir'\n"
64 " -llib link with dynamic or static library 'lib'\n"
65 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
66 " -shared generate a shared library\n"
67 " -soname set name for shared library to be used at runtime\n"
68 " -static static linking\n"
69 " -rdynamic export all global symbols to dynamic linker\n"
70 " -r generate (relocatable) object file\n"
71 "Debugger options:\n"
72 " -g generate runtime debug info\n"
73 #ifdef CONFIG_TCC_BCHECK
74 " -b compile with built-in memory and bounds checker (implies -g)\n"
75 #endif
76 #ifdef CONFIG_TCC_BACKTRACE
77 " -bt N show N callers in stack traces\n"
78 #endif
79 "Misc options:\n"
80 " -MD generate target dependencies for make\n"
81 " -MF depfile put generated dependencies here\n"
85 typedef struct TCCOption {
86 const char *name;
87 uint16_t index;
88 uint16_t flags;
89 } TCCOption;
91 enum {
92 TCC_OPTION_HELP,
93 TCC_OPTION_I,
94 TCC_OPTION_D,
95 TCC_OPTION_U,
96 TCC_OPTION_L,
97 TCC_OPTION_B,
98 TCC_OPTION_l,
99 TCC_OPTION_bench,
100 TCC_OPTION_bt,
101 TCC_OPTION_b,
102 TCC_OPTION_g,
103 TCC_OPTION_c,
104 TCC_OPTION_static,
105 TCC_OPTION_shared,
106 TCC_OPTION_soname,
107 TCC_OPTION_o,
108 TCC_OPTION_r,
109 TCC_OPTION_s,
110 TCC_OPTION_Wl,
111 TCC_OPTION_W,
112 TCC_OPTION_O,
113 TCC_OPTION_m,
114 TCC_OPTION_f,
115 TCC_OPTION_isystem,
116 TCC_OPTION_nostdinc,
117 TCC_OPTION_nostdlib,
118 TCC_OPTION_print_search_dirs,
119 TCC_OPTION_rdynamic,
120 TCC_OPTION_pedantic,
121 TCC_OPTION_pthread,
122 TCC_OPTION_run,
123 TCC_OPTION_v,
124 TCC_OPTION_w,
125 TCC_OPTION_pipe,
126 TCC_OPTION_E,
127 TCC_OPTION_MD,
128 TCC_OPTION_MF,
129 TCC_OPTION_x,
132 static const TCCOption tcc_options[] = {
133 { "h", TCC_OPTION_HELP, 0 },
134 { "-help", TCC_OPTION_HELP, 0 },
135 { "?", TCC_OPTION_HELP, 0 },
136 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
137 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
138 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
139 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
140 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
141 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
142 { "bench", TCC_OPTION_bench, 0 },
143 #ifdef CONFIG_TCC_BACKTRACE
144 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
145 #endif
146 #ifdef CONFIG_TCC_BCHECK
147 { "b", TCC_OPTION_b, 0 },
148 #endif
149 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
150 { "c", TCC_OPTION_c, 0 },
151 { "static", TCC_OPTION_static, 0 },
152 { "shared", TCC_OPTION_shared, 0 },
153 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
154 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
155 { "pedantic", TCC_OPTION_pedantic, 0},
156 { "pthread", TCC_OPTION_pthread, 0},
157 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
158 { "rdynamic", TCC_OPTION_rdynamic, 0 },
159 { "r", TCC_OPTION_r, 0 },
160 { "s", TCC_OPTION_s, 0 },
161 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
162 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
163 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
164 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
165 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
166 { "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
167 { "nostdinc", TCC_OPTION_nostdinc, 0 },
168 { "nostdlib", TCC_OPTION_nostdlib, 0 },
169 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
170 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
171 { "w", TCC_OPTION_w, 0 },
172 { "pipe", TCC_OPTION_pipe, 0},
173 { "E", TCC_OPTION_E, 0},
174 { "MD", TCC_OPTION_MD, 0},
175 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
176 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
177 { NULL },
180 static int64_t getclock_us(void)
182 #ifdef _WIN32
183 struct _timeb tb;
184 _ftime(&tb);
185 return (tb.time * 1000LL + tb.millitm) * 1000LL;
186 #else
187 struct timeval tv;
188 gettimeofday(&tv, NULL);
189 return tv.tv_sec * 1000000LL + tv.tv_usec;
190 #endif
193 /* convert 'str' into an array of space separated strings */
194 static int expand_args(char ***pargv, const char *str)
196 const char *s1;
197 char **argv, *arg;
198 int argc, len;
200 argc = 0;
201 argv = NULL;
202 for(;;) {
203 while (is_space(*str))
204 str++;
205 if (*str == '\0')
206 break;
207 s1 = str;
208 while (*str != '\0' && !is_space(*str))
209 str++;
210 len = str - s1;
211 arg = tcc_malloc(len + 1);
212 memcpy(arg, s1, len);
213 arg[len] = '\0';
214 dynarray_add((void ***)&argv, &argc, arg);
216 *pargv = argv;
217 return argc;
220 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
221 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
222 #ifdef _WIN32
223 #include <process.h>
224 static int execvp_win32(const char *prog, char **argv)
226 int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv);
227 if (-1 == ret)
228 return ret;
229 cwait(&ret, ret, WAIT_CHILD);
230 exit(ret);
232 #define execvp execvp_win32
233 #endif
234 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
236 char child_path[4096], *child_name; const char *target;
237 switch (atoi(optarg)) {
238 #ifdef TCC_TARGET_I386
239 case 32: break;
240 case 64: target = "x86_64";
241 #else
242 case 64: break;
243 case 32: target = "i386";
244 #endif
245 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
246 child_name = tcc_basename(child_path);
247 strcpy(child_name, target);
248 #ifdef TCC_TARGET_PE
249 strcat(child_name, "-win32");
250 #endif
251 strcat(child_name, "-tcc");
252 if (strcmp(argv[0], child_path)) {
253 if (s->verbose > 0)
254 printf("tcc: using '%s'\n", child_name), fflush(stdout);
255 execvp(argv[0] = child_path, argv);
257 error("'%s' not found", child_name);
258 case 0: /* ignore -march etc. */
259 break;
260 default:
261 warning("unsupported option \"-m%s\"", optarg);
264 #endif
266 static void parse_option_D(TCCState *s1, const char *optarg)
268 char *sym = tcc_strdup(optarg);
269 char *value = strchr(sym, '=');
270 if (value)
271 *value++ = '\0';
272 tcc_define_symbol(s1, sym, value);
273 tcc_free(sym);
276 static int parse_args(TCCState *s, int argc, char **argv)
278 int optind;
279 const TCCOption *popt;
280 const char *optarg, *p1, *r1;
281 char *r;
282 int was_pthread;
284 was_pthread = 0; /* is set if commandline contains -pthread key */
286 optind = 1;
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:
340 parse_option_D(s, optarg);
341 break;
342 case TCC_OPTION_U:
343 tcc_undefine_symbol(s, optarg);
344 break;
345 case TCC_OPTION_L:
346 tcc_add_library_path(s, optarg);
347 break;
348 case TCC_OPTION_B:
349 /* set tcc utilities path (mainly for tcc development) */
350 tcc_set_lib_path(s, optarg);
351 break;
352 case TCC_OPTION_l:
353 dynarray_add((void ***)&files, &nb_files, r);
354 nb_libraries++;
355 break;
356 case TCC_OPTION_pthread:
357 was_pthread = 1;
358 parse_option_D(s, "_REENTRANT");
359 break;
360 case TCC_OPTION_bench:
361 do_bench = 1;
362 break;
363 #ifdef CONFIG_TCC_BACKTRACE
364 case TCC_OPTION_bt:
365 tcc_set_num_callers(atoi(optarg));
366 break;
367 #endif
368 #ifdef CONFIG_TCC_BCHECK
369 case TCC_OPTION_b:
370 s->do_bounds_check = 1;
371 s->do_debug = 1;
372 break;
373 #endif
374 case TCC_OPTION_g:
375 s->do_debug = 1;
376 break;
377 case TCC_OPTION_c:
378 multiple_files = 1;
379 output_type = TCC_OUTPUT_OBJ;
380 break;
381 case TCC_OPTION_static:
382 s->static_link = 1;
383 break;
384 case TCC_OPTION_shared:
385 output_type = TCC_OUTPUT_DLL;
386 break;
387 case TCC_OPTION_soname:
388 s->soname = optarg;
389 break;
390 case TCC_OPTION_m:
391 m_option = optarg;
392 break;
393 case TCC_OPTION_o:
394 multiple_files = 1;
395 outfile = tcc_strdup(optarg);
396 break;
397 case TCC_OPTION_r:
398 /* generate a .o merging several output files */
399 reloc_output = 1;
400 output_type = TCC_OUTPUT_OBJ;
401 break;
402 case TCC_OPTION_isystem:
403 tcc_add_sysinclude_path(s, optarg);
404 break;
405 case TCC_OPTION_nostdinc:
406 s->nostdinc = 1;
407 break;
408 case TCC_OPTION_nostdlib:
409 s->nostdlib = 1;
410 break;
411 case TCC_OPTION_print_search_dirs:
412 print_search_dirs = 1;
413 break;
414 case TCC_OPTION_run:
416 int argc1;
417 char **argv1;
418 argc1 = expand_args(&argv1, optarg);
419 if (argc1 > 0) {
420 parse_args(s, argc1, argv1);
422 multiple_files = 0;
423 output_type = TCC_OUTPUT_MEMORY;
424 break;
426 case TCC_OPTION_v:
427 do ++s->verbose; while (*optarg++ == 'v');
428 break;
429 case TCC_OPTION_f:
430 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
431 goto unsupported_option;
432 break;
433 case TCC_OPTION_W:
434 if (tcc_set_warning(s, optarg, 1) < 0 &&
435 s->warn_unsupported)
436 goto unsupported_option;
437 break;
438 case TCC_OPTION_w:
439 s->warn_none = 1;
440 break;
441 case TCC_OPTION_rdynamic:
442 s->rdynamic = 1;
443 break;
444 case TCC_OPTION_Wl:
445 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
446 error("unsupported linker option '%s'", r);
447 break;
448 case TCC_OPTION_E:
449 output_type = TCC_OUTPUT_PREPROCESS;
450 break;
451 case TCC_OPTION_MD:
452 gen_deps = 1;
453 break;
454 case TCC_OPTION_MF:
455 deps_outfile = optarg;
456 break;
457 case TCC_OPTION_x:
458 break;
459 default:
460 if (s->warn_unsupported) {
461 unsupported_option:
462 warning("unsupported option '%s'", r);
464 break;
468 /* fixme: these options could be different on your platform */
469 if (was_pthread && output_type != TCC_OUTPUT_OBJ) {
470 dynarray_add((void ***)&files, &nb_files, "-lpthread");
471 nb_libraries++;
473 return optind;
476 int main(int argc, char **argv)
478 int i;
479 TCCState *s;
480 int nb_objfiles, ret, optind;
481 int64_t start_time = 0;
482 const char *default_file = NULL;
484 s = tcc_new();
486 output_type = TCC_OUTPUT_EXE;
487 outfile = NULL;
488 multiple_files = 1;
489 files = NULL;
490 nb_files = 0;
491 nb_libraries = 0;
492 reloc_output = 0;
493 print_search_dirs = 0;
494 m_option = NULL;
495 ret = 0;
497 optind = parse_args(s, argc, argv);
499 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
500 if (m_option)
501 exec_other_tcc(s, argv, m_option);
502 #endif
504 if (print_search_dirs) {
505 /* enough for Linux kernel */
506 printf("install: %s/\n", s->tcc_lib_path);
507 return 0;
510 if (s->verbose)
511 printf("tcc version %s\n", TCC_VERSION);
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");
536 if (output_type == TCC_OUTPUT_PREPROCESS) {
537 if (!outfile) {
538 s->outfile = stdout;
539 } else {
540 s->outfile = fopen(outfile, "w");
541 if (!s->outfile)
542 error("could not open '%s'", outfile);
546 if (do_bench) {
547 start_time = getclock_us();
550 tcc_set_output_type(s, output_type);
551 s->reloc_output = reloc_output;
553 /* compile or add each files or library */
554 for(i = 0; i < nb_files && ret == 0; i++) {
555 const char *filename;
557 filename = files[i];
558 if (filename[0] == '-' && filename[1] == 'l') {
559 if (tcc_add_library(s, filename + 2) < 0) {
560 error_noabort("cannot find %s", filename);
561 ret = 1;
563 } else {
564 if (1 == s->verbose)
565 printf("-> %s\n", filename);
566 if (tcc_add_file(s, filename) < 0)
567 ret = 1;
568 if (!default_file)
569 default_file = filename;
573 /* free all files */
574 tcc_free(files);
576 if (0 == ret) {
577 if (do_bench)
578 tcc_print_stats(s, getclock_us() - start_time);
580 if (s->output_type == TCC_OUTPUT_MEMORY) {
581 ret = tcc_run(s, argc - optind, argv + optind);
582 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
583 if (s->outfile)
584 fclose(s->outfile);
585 } else {
586 if (!outfile)
587 outfile = tcc_default_target(s, default_file);
588 ret = !!tcc_output_file(s, outfile);
589 /* dump collected dependencies */
590 if (gen_deps && !ret)
591 tcc_gen_makedeps(s, outfile, deps_outfile);
595 tcc_delete(s);
596 tcc_free(outfile);
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;