Add support for --help
[tinycc.git] / tcc.c
blob418c2ec42a511bf5267adb2aac3542f8baadf418
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;
36 #define TCC_OPTION_HAS_ARG 0x0001
37 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
39 static void help(void)
41 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
42 "usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
43 " [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
44 " [-static] [infile1 infile2...] [-run infile args...]\n"
45 "\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 " -shared generate a shared library\n"
65 " -soname set name for shared library to be used at runtime\n"
66 " -static static linking\n"
67 " -rdynamic export all global symbols to dynamic linker\n"
68 " -r generate (relocatable) object file\n"
69 "Debugger options:\n"
70 " -g generate runtime debug info\n"
71 #ifdef CONFIG_TCC_BCHECK
72 " -b compile with built-in memory and bounds checker (implies -g)\n"
73 #endif
74 #ifdef CONFIG_TCC_BACKTRACE
75 " -bt N show N callers in stack traces\n"
76 #endif
80 typedef struct TCCOption {
81 const char *name;
82 uint16_t index;
83 uint16_t flags;
84 } TCCOption;
86 enum {
87 TCC_OPTION_HELP,
88 TCC_OPTION_I,
89 TCC_OPTION_D,
90 TCC_OPTION_U,
91 TCC_OPTION_L,
92 TCC_OPTION_B,
93 TCC_OPTION_l,
94 TCC_OPTION_bench,
95 TCC_OPTION_bt,
96 TCC_OPTION_b,
97 TCC_OPTION_g,
98 TCC_OPTION_c,
99 TCC_OPTION_static,
100 TCC_OPTION_shared,
101 TCC_OPTION_soname,
102 TCC_OPTION_o,
103 TCC_OPTION_r,
104 TCC_OPTION_Wl,
105 TCC_OPTION_W,
106 TCC_OPTION_O,
107 TCC_OPTION_m,
108 TCC_OPTION_f,
109 TCC_OPTION_nostdinc,
110 TCC_OPTION_nostdlib,
111 TCC_OPTION_print_search_dirs,
112 TCC_OPTION_rdynamic,
113 TCC_OPTION_run,
114 TCC_OPTION_v,
115 TCC_OPTION_w,
116 TCC_OPTION_pipe,
117 TCC_OPTION_E,
118 TCC_OPTION_x,
121 static const TCCOption tcc_options[] = {
122 { "h", TCC_OPTION_HELP, 0 },
123 { "-help", TCC_OPTION_HELP, 0 },
124 { "?", TCC_OPTION_HELP, 0 },
125 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
126 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
127 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
128 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
129 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
130 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
131 { "bench", TCC_OPTION_bench, 0 },
132 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
133 #ifdef CONFIG_TCC_BCHECK
134 { "b", TCC_OPTION_b, 0 },
135 #endif
136 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
137 { "c", TCC_OPTION_c, 0 },
138 { "static", TCC_OPTION_static, 0 },
139 { "shared", TCC_OPTION_shared, 0 },
140 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
141 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
142 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
143 { "rdynamic", TCC_OPTION_rdynamic, 0 },
144 { "r", TCC_OPTION_r, 0 },
145 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
146 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
147 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
148 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
149 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
150 { "nostdinc", TCC_OPTION_nostdinc, 0 },
151 { "nostdlib", TCC_OPTION_nostdlib, 0 },
152 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
153 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
154 { "w", TCC_OPTION_w, 0 },
155 { "pipe", TCC_OPTION_pipe, 0},
156 { "E", TCC_OPTION_E, 0},
157 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
158 { NULL },
161 static int64_t getclock_us(void)
163 #ifdef _WIN32
164 struct _timeb tb;
165 _ftime(&tb);
166 return (tb.time * 1000LL + tb.millitm) * 1000LL;
167 #else
168 struct timeval tv;
169 gettimeofday(&tv, NULL);
170 return tv.tv_sec * 1000000LL + tv.tv_usec;
171 #endif
174 static int strstart(const char *str, const char *val, const char **ptr)
176 const char *p, *q;
177 p = str;
178 q = val;
179 while (*q != '\0') {
180 if (*p != *q)
181 return 0;
182 p++;
183 q++;
185 if (ptr)
186 *ptr = p;
187 return 1;
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 static int parse_args(TCCState *s, int argc, char **argv)
219 int optind;
220 const TCCOption *popt;
221 const char *optarg, *p1, *r1;
222 char *r;
224 optind = 0;
225 while (optind < argc) {
227 r = argv[optind++];
228 if (r[0] != '-' || r[1] == '\0') {
229 /* add a new file */
230 dynarray_add((void ***)&files, &nb_files, r);
231 if (!multiple_files) {
232 optind--;
233 /* argv[0] will be this file */
234 break;
236 } else {
237 /* find option in table (match only the first chars */
238 popt = tcc_options;
239 for(;;) {
240 p1 = popt->name;
241 if (p1 == NULL)
242 error("invalid option -- '%s'", r);
243 r1 = r + 1;
244 for(;;) {
245 if (*p1 == '\0')
246 goto option_found;
247 if (*r1 != *p1)
248 break;
249 p1++;
250 r1++;
252 popt++;
254 option_found:
255 if (popt->flags & TCC_OPTION_HAS_ARG) {
256 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
257 optarg = r1;
258 } else {
259 if (optind >= argc)
260 error("argument to '%s' is missing", r);
261 optarg = argv[optind++];
263 } else {
264 if (*r1 != '\0')
265 return 0;
266 optarg = NULL;
269 switch(popt->index) {
270 case TCC_OPTION_HELP:
271 return 0;
273 case TCC_OPTION_I:
274 if (tcc_add_include_path(s, optarg) < 0)
275 error("too many include paths");
276 break;
277 case TCC_OPTION_D:
279 char *sym, *value;
280 sym = (char *)optarg;
281 value = strchr(sym, '=');
282 if (value) {
283 *value = '\0';
284 value++;
286 tcc_define_symbol(s, sym, value);
288 break;
289 case TCC_OPTION_U:
290 tcc_undefine_symbol(s, optarg);
291 break;
292 case TCC_OPTION_L:
293 tcc_add_library_path(s, optarg);
294 break;
295 case TCC_OPTION_B:
296 /* set tcc utilities path (mainly for tcc development) */
297 tcc_set_lib_path(s, optarg);
298 break;
299 case TCC_OPTION_l:
300 dynarray_add((void ***)&files, &nb_files, r);
301 nb_libraries++;
302 break;
303 case TCC_OPTION_bench:
304 do_bench = 1;
305 break;
306 #ifdef CONFIG_TCC_BACKTRACE
307 case TCC_OPTION_bt:
308 set_num_callers(atoi(optarg));
309 break;
310 #endif
311 #ifdef CONFIG_TCC_BCHECK
312 case TCC_OPTION_b:
313 s->do_bounds_check = 1;
314 s->do_debug = 1;
315 break;
316 #endif
317 case TCC_OPTION_g:
318 s->do_debug = 1;
319 break;
320 case TCC_OPTION_c:
321 multiple_files = 1;
322 output_type = TCC_OUTPUT_OBJ;
323 break;
324 case TCC_OPTION_static:
325 s->static_link = 1;
326 break;
327 case TCC_OPTION_shared:
328 output_type = TCC_OUTPUT_DLL;
329 break;
330 case TCC_OPTION_soname:
331 s->soname = optarg;
332 break;
333 case TCC_OPTION_o:
334 multiple_files = 1;
335 outfile = optarg;
336 break;
337 case TCC_OPTION_r:
338 /* generate a .o merging several output files */
339 reloc_output = 1;
340 output_type = TCC_OUTPUT_OBJ;
341 break;
342 case TCC_OPTION_nostdinc:
343 s->nostdinc = 1;
344 break;
345 case TCC_OPTION_nostdlib:
346 s->nostdlib = 1;
347 break;
348 case TCC_OPTION_print_search_dirs:
349 print_search_dirs = 1;
350 break;
351 case TCC_OPTION_run:
353 int argc1;
354 char **argv1;
355 argc1 = expand_args(&argv1, optarg);
356 if (argc1 > 0) {
357 parse_args(s, argc1, argv1);
359 multiple_files = 0;
360 output_type = TCC_OUTPUT_MEMORY;
362 break;
363 case TCC_OPTION_v:
364 do {
365 if (0 == s->verbose++)
366 printf("tcc version %s\n", TCC_VERSION);
367 } while (*optarg++ == 'v');
368 break;
369 case TCC_OPTION_f:
370 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
371 goto unsupported_option;
372 break;
373 case TCC_OPTION_W:
374 if (tcc_set_warning(s, optarg, 1) < 0 &&
375 s->warn_unsupported)
376 goto unsupported_option;
377 break;
378 case TCC_OPTION_w:
379 s->warn_none = 1;
380 break;
381 case TCC_OPTION_rdynamic:
382 s->rdynamic = 1;
383 break;
384 case TCC_OPTION_Wl:
386 const char *p;
387 if (strstart(optarg, "-Ttext,", &p)) {
388 s->text_addr = strtoul(p, NULL, 16);
389 s->has_text_addr = 1;
390 } else if (strstart(optarg, "-Bsymbolic", &p)) {
391 s->symbolic = TRUE;
392 } else if (strstart(optarg, "--section-alignment,", &p)) {
393 s->section_align = strtoul(p, NULL, 16);
394 } else if (strstart(optarg, "--image-base,", &p)) {
395 s->text_addr = strtoul(p, NULL, 16);
396 s->has_text_addr = 1;
397 #ifdef TCC_TARGET_PE
398 } else if (strstart(optarg, "--file-alignment,", &p)) {
399 s->pe_file_align = strtoul(p, NULL, 16);
400 } else if (strstart(optarg, "--subsystem,", &p)) {
401 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
402 if (!strcmp(p, "native"))
403 s->pe_subsystem = 1;
404 else if (!strcmp(p, "console"))
405 s->pe_subsystem = 3;
406 else if (!strcmp(p, "gui"))
407 s->pe_subsystem = 2;
408 else if (!strcmp(p, "posix"))
409 s->pe_subsystem = 7;
410 else if (!strcmp(p, "efiapp"))
411 s->pe_subsystem = 10;
412 else if (!strcmp(p, "efiboot"))
413 s->pe_subsystem = 11;
414 else if (!strcmp(p, "efiruntime"))
415 s->pe_subsystem = 12;
416 else if (!strcmp(p, "efirom"))
417 s->pe_subsystem = 13;
418 #elif defined(TCC_TARGET_ARM)
419 if (!strcmp(p, "wince"))
420 s->pe_subsystem = 9;
421 #endif
422 else {
423 error("invalid subsystem '%s'", p);
425 #endif
426 } else if (strstart(optarg, "--oformat,", &p)) {
427 #if defined(TCC_TARGET_PE)
428 if (strstart(p, "pe-", NULL)) {
429 #else
430 #if defined(TCC_TARGET_X86_64)
431 if (strstart(p, "elf64-", NULL)) {
432 #else
433 if (strstart(p, "elf32-", NULL)) {
434 #endif
435 #endif
436 s->output_format = TCC_OUTPUT_FORMAT_ELF;
437 } else if (!strcmp(p, "binary")) {
438 s->output_format = TCC_OUTPUT_FORMAT_BINARY;
439 } else
440 #ifdef TCC_TARGET_COFF
441 if (!strcmp(p, "coff")) {
442 s->output_format = TCC_OUTPUT_FORMAT_COFF;
443 } else
444 #endif
446 error("target %s not found", p);
448 } else if (strstart(optarg, "-rpath=", &p)) {
449 s->rpath = p;
450 } else {
451 error("unsupported linker option '%s'", optarg);
454 break;
455 case TCC_OPTION_E:
456 output_type = TCC_OUTPUT_PREPROCESS;
457 break;
458 case TCC_OPTION_x:
459 break;
460 default:
461 if (s->warn_unsupported) {
462 unsupported_option:
463 warning("unsupported option '%s'", r);
465 break;
469 return optind + 1;
472 int main(int argc, char **argv)
474 int i;
475 TCCState *s;
476 int nb_objfiles, ret, optind;
477 char objfilename[1024];
478 int64_t start_time = 0;
480 s = tcc_new();
482 output_type = TCC_OUTPUT_EXE;
483 outfile = NULL;
484 multiple_files = 1;
485 files = NULL;
486 nb_files = 0;
487 nb_libraries = 0;
488 reloc_output = 0;
489 print_search_dirs = 0;
490 ret = 0;
492 optind = parse_args(s, argc - 1, argv + 1);
493 if (print_search_dirs) {
494 /* enough for Linux kernel */
495 printf("install: %s/\n", s->tcc_lib_path);
496 return 0;
498 if (optind == 0 || nb_files == 0) {
499 if (optind && s->verbose)
500 return 0;
501 help();
502 return 1;
505 nb_objfiles = nb_files - nb_libraries;
507 /* if outfile provided without other options, we output an
508 executable */
509 if (outfile && output_type == TCC_OUTPUT_MEMORY)
510 output_type = TCC_OUTPUT_EXE;
512 /* check -c consistency : only single file handled. XXX: checks file type */
513 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
514 /* accepts only a single input file */
515 if (nb_objfiles != 1)
516 error("cannot specify multiple files with -c");
517 if (nb_libraries != 0)
518 error("cannot specify libraries with -c");
522 if (output_type == TCC_OUTPUT_PREPROCESS) {
523 if (!outfile) {
524 s->outfile = stdout;
525 } else {
526 s->outfile = fopen(outfile, "w");
527 if (!s->outfile)
528 error("could not open '%s", outfile);
530 } else if (output_type != TCC_OUTPUT_MEMORY) {
531 if (!outfile) {
532 /* compute default outfile name */
533 char *ext;
534 const char *name =
535 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
536 pstrcpy(objfilename, sizeof(objfilename), name);
537 ext = tcc_fileextension(objfilename);
538 #ifdef TCC_TARGET_PE
539 if (output_type == TCC_OUTPUT_DLL)
540 strcpy(ext, ".dll");
541 else
542 if (output_type == TCC_OUTPUT_EXE)
543 strcpy(ext, ".exe");
544 else
545 #endif
546 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
547 strcpy(ext, ".o");
548 else
549 pstrcpy(objfilename, sizeof(objfilename), "a.out");
550 outfile = objfilename;
554 if (do_bench) {
555 start_time = getclock_us();
558 tcc_set_output_type(s, output_type);
560 /* compile or add each files or library */
561 for(i = 0; i < nb_files && ret == 0; i++) {
562 const char *filename;
564 filename = files[i];
565 if (filename[0] == '-' && filename[1]) {
566 if (tcc_add_library(s, filename + 2) < 0) {
567 error_noabort("cannot find %s", filename);
568 ret = 1;
570 } else {
571 if (1 == s->verbose)
572 printf("-> %s\n", filename);
573 if (tcc_add_file(s, filename) < 0)
574 ret = 1;
578 /* free all files */
579 tcc_free(files);
581 if (0 == ret) {
582 if (do_bench)
583 tcc_print_stats(s, getclock_us() - start_time);
585 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
586 if (outfile)
587 fclose(s->outfile);
588 } else if (s->output_type == TCC_OUTPUT_MEMORY)
589 ret = tcc_run(s, argc - optind, argv + optind);
590 else
591 ret = tcc_output_file(s, outfile) ? 1 : 0;
594 tcc_delete(s);
596 #ifdef MEM_DEBUG
597 if (do_bench) {
598 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
600 #endif
601 return ret;