libtcc: cleanup the 'gen_makedeps' stuff
[tinycc.git] / tcc.c
blobdaf302b356e1a604340dd42cb7529002fd5bc118
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 int parse_args(TCCState *s, int argc, char **argv)
268 int optind;
269 const TCCOption *popt;
270 const char *optarg, *p1, *r1;
271 char *r;
272 int was_pthread;
274 was_pthread = 0; /* is set if commandline contains -pthread key */
276 optind = 1;
277 while (optind < argc) {
279 r = argv[optind++];
280 if (r[0] != '-' || r[1] == '\0') {
281 /* add a new file */
282 dynarray_add((void ***)&files, &nb_files, r);
283 if (!multiple_files) {
284 optind--;
285 /* argv[0] will be this file */
286 break;
288 } else {
289 /* find option in table (match only the first chars */
290 popt = tcc_options;
291 for(;;) {
292 p1 = popt->name;
293 if (p1 == NULL)
294 error("invalid option -- '%s'", r);
295 r1 = r + 1;
296 for(;;) {
297 if (*p1 == '\0')
298 goto option_found;
299 if (*r1 != *p1)
300 break;
301 p1++;
302 r1++;
304 popt++;
306 option_found:
307 if (popt->flags & TCC_OPTION_HAS_ARG) {
308 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
309 optarg = r1;
310 } else {
311 if (optind >= argc)
312 error("argument to '%s' is missing", r);
313 optarg = argv[optind++];
315 } else {
316 if (*r1 != '\0')
317 return 0;
318 optarg = NULL;
321 switch(popt->index) {
322 case TCC_OPTION_HELP:
323 return 0;
325 case TCC_OPTION_I:
326 if (tcc_add_include_path(s, optarg) < 0)
327 error("too many include paths");
328 break;
329 case TCC_OPTION_D:
331 char *sym, *value;
332 sym = (char *)optarg;
333 value = strchr(sym, '=');
334 if (value) {
335 *value = '\0';
336 value++;
338 tcc_define_symbol(s, sym, value);
340 break;
341 case TCC_OPTION_U:
342 tcc_undefine_symbol(s, optarg);
343 break;
344 case TCC_OPTION_L:
345 tcc_add_library_path(s, optarg);
346 break;
347 case TCC_OPTION_B:
348 /* set tcc utilities path (mainly for tcc development) */
349 tcc_set_lib_path(s, optarg);
350 /* append /include and add it as -isystem, as gcc does */
351 r = tcc_strdup(optarg);
352 r = tcc_realloc(r, strlen(r) + 8 + 1);
353 strcat(r, "/include");
354 tcc_add_sysinclude_path(s, r);
355 tcc_free(r);
356 break;
357 case TCC_OPTION_l:
358 dynarray_add((void ***)&files, &nb_files, r);
359 nb_libraries++;
360 break;
361 case TCC_OPTION_pthread:
362 was_pthread = 1;
363 tcc_define_symbol(s, "_REENTRANT", "1");
364 break;
365 case TCC_OPTION_bench:
366 do_bench = 1;
367 break;
368 #ifdef CONFIG_TCC_BACKTRACE
369 case TCC_OPTION_bt:
370 set_num_callers(atoi(optarg));
371 break;
372 #endif
373 #ifdef CONFIG_TCC_BCHECK
374 case TCC_OPTION_b:
375 s->do_bounds_check = 1;
376 s->do_debug = 1;
377 break;
378 #endif
379 case TCC_OPTION_g:
380 s->do_debug = 1;
381 break;
382 case TCC_OPTION_c:
383 multiple_files = 1;
384 output_type = TCC_OUTPUT_OBJ;
385 break;
386 case TCC_OPTION_static:
387 s->static_link = 1;
388 break;
389 case TCC_OPTION_shared:
390 output_type = TCC_OUTPUT_DLL;
391 break;
392 case TCC_OPTION_soname:
393 s->soname = optarg;
394 break;
395 case TCC_OPTION_m:
396 m_option = optarg;
397 break;
398 case TCC_OPTION_o:
399 multiple_files = 1;
400 outfile = tcc_strdup(optarg);
401 break;
402 case TCC_OPTION_r:
403 /* generate a .o merging several output files */
404 reloc_output = 1;
405 output_type = TCC_OUTPUT_OBJ;
406 break;
407 case TCC_OPTION_isystem:
408 tcc_add_sysinclude_path(s, optarg);
409 break;
410 case TCC_OPTION_nostdinc:
411 s->nostdinc = 1;
412 break;
413 case TCC_OPTION_nostdlib:
414 s->nostdlib = 1;
415 break;
416 case TCC_OPTION_print_search_dirs:
417 print_search_dirs = 1;
418 break;
419 case TCC_OPTION_run:
421 int argc1;
422 char **argv1;
423 argc1 = expand_args(&argv1, optarg);
424 if (argc1 > 0) {
425 parse_args(s, argc1, argv1);
427 multiple_files = 0;
428 output_type = TCC_OUTPUT_MEMORY;
430 break;
431 case TCC_OPTION_v:
432 do ++s->verbose; while (*optarg++ == 'v');
433 break;
434 case TCC_OPTION_f:
435 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
436 goto unsupported_option;
437 break;
438 case TCC_OPTION_W:
439 if (tcc_set_warning(s, optarg, 1) < 0 &&
440 s->warn_unsupported)
441 goto unsupported_option;
442 break;
443 case TCC_OPTION_w:
444 s->warn_none = 1;
445 break;
446 case TCC_OPTION_rdynamic:
447 s->rdynamic = 1;
448 break;
449 case TCC_OPTION_Wl:
451 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
452 error("unsupported linker option '%s'", r);
454 break;
455 case TCC_OPTION_E:
456 output_type = TCC_OUTPUT_PREPROCESS;
457 break;
458 case TCC_OPTION_MD:
459 gen_deps = 1;
460 break;
461 case TCC_OPTION_MF:
462 deps_outfile = optarg;
463 break;
464 case TCC_OPTION_x:
465 break;
466 default:
467 if (s->warn_unsupported) {
468 unsupported_option:
469 warning("unsupported option '%s'", r);
471 break;
475 /* fixme: these options could be different on your platform */
476 if (was_pthread
477 && output_type != TCC_OUTPUT_OBJ)
479 dynarray_add((void ***)&files, &nb_files, "-lpthread");
480 nb_libraries++;
482 return optind;
485 int main(int argc, char **argv)
487 int i;
488 TCCState *s;
489 int nb_objfiles, ret, optind;
490 int64_t start_time = 0;
491 const char *default_file = NULL;
493 s = tcc_new();
495 output_type = TCC_OUTPUT_EXE;
496 outfile = NULL;
497 multiple_files = 1;
498 files = NULL;
499 nb_files = 0;
500 nb_libraries = 0;
501 reloc_output = 0;
502 print_search_dirs = 0;
503 m_option = NULL;
504 ret = 0;
506 optind = parse_args(s, argc, argv);
508 #if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
509 if (m_option)
510 exec_other_tcc(s, argv, m_option);
511 #endif
513 if (print_search_dirs) {
514 /* enough for Linux kernel */
515 printf("install: %s/\n", s->tcc_lib_path);
516 return 0;
519 if (s->verbose)
520 printf("tcc version %s\n", TCC_VERSION);
522 if (optind == 0 || nb_files == 0) {
523 if (optind && s->verbose)
524 return 0;
525 help();
526 return 1;
529 nb_objfiles = nb_files - nb_libraries;
531 /* if outfile provided without other options, we output an
532 executable */
533 if (outfile && output_type == TCC_OUTPUT_MEMORY)
534 output_type = TCC_OUTPUT_EXE;
536 /* check -c consistency : only single file handled. XXX: checks file type */
537 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
538 /* accepts only a single input file */
539 if (nb_objfiles != 1)
540 error("cannot specify multiple files with -c");
541 if (nb_libraries != 0)
542 error("cannot specify libraries with -c");
545 if (output_type == TCC_OUTPUT_PREPROCESS) {
546 if (!outfile) {
547 s->outfile = stdout;
548 } else {
549 s->outfile = fopen(outfile, "w");
550 if (!s->outfile)
551 error("could not open '%s'", outfile);
555 if (do_bench) {
556 start_time = getclock_us();
559 tcc_set_output_type(s, output_type);
560 s->reloc_output = reloc_output;
562 /* compile or add each files or library */
563 for(i = 0; i < nb_files && ret == 0; i++) {
564 const char *filename;
566 filename = files[i];
567 if (filename[0] == '-' && filename[1] == 'l') {
568 if (tcc_add_library(s, filename + 2) < 0) {
569 error_noabort("cannot find %s", filename);
570 ret = 1;
572 } else {
573 if (1 == s->verbose)
574 printf("-> %s\n", filename);
575 if (tcc_add_file(s, filename) < 0)
576 ret = 1;
577 if (!default_file)
578 default_file = filename;
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 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
592 if (s->outfile)
593 fclose(s->outfile);
594 } else {
595 if (!outfile)
596 outfile = tcc_default_target(s, default_file);
597 ret = !!tcc_output_file(s, outfile);
598 /* dump collected dependencies */
599 if (gen_deps && !ret)
600 tcc_gen_makedeps(s, outfile, deps_outfile);
604 tcc_delete(s);
605 tcc_free(outfile);
607 #ifdef MEM_DEBUG
608 if (do_bench) {
609 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
611 #endif
612 return ret;