tcc: add option -s for gcc compatibility (ignored)
[tinycc.git] / tcc.c
blob2f9f6bb857b886c0b37dc2ef1b171b3df749f165
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 static int parse_args(TCCState *s, int argc, char **argv)
217 int optind;
218 const TCCOption *popt;
219 const char *optarg, *p1, *r1;
220 char *r;
222 optind = 0;
223 while (optind < argc) {
225 r = argv[optind++];
226 if (r[0] != '-' || r[1] == '\0') {
227 /* add a new file */
228 dynarray_add((void ***)&files, &nb_files, r);
229 if (!multiple_files) {
230 optind--;
231 /* argv[0] will be this file */
232 break;
234 } else {
235 /* find option in table (match only the first chars */
236 popt = tcc_options;
237 for(;;) {
238 p1 = popt->name;
239 if (p1 == NULL)
240 error("invalid option -- '%s'", r);
241 r1 = r + 1;
242 for(;;) {
243 if (*p1 == '\0')
244 goto option_found;
245 if (*r1 != *p1)
246 break;
247 p1++;
248 r1++;
250 popt++;
252 option_found:
253 if (popt->flags & TCC_OPTION_HAS_ARG) {
254 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
255 optarg = r1;
256 } else {
257 if (optind >= argc)
258 error("argument to '%s' is missing", r);
259 optarg = argv[optind++];
261 } else {
262 if (*r1 != '\0')
263 return 0;
264 optarg = NULL;
267 switch(popt->index) {
268 case TCC_OPTION_HELP:
269 return 0;
271 case TCC_OPTION_I:
272 if (tcc_add_include_path(s, optarg) < 0)
273 error("too many include paths");
274 break;
275 case TCC_OPTION_D:
277 char *sym, *value;
278 sym = (char *)optarg;
279 value = strchr(sym, '=');
280 if (value) {
281 *value = '\0';
282 value++;
284 tcc_define_symbol(s, sym, value);
286 break;
287 case TCC_OPTION_U:
288 tcc_undefine_symbol(s, optarg);
289 break;
290 case TCC_OPTION_L:
291 tcc_add_library_path(s, optarg);
292 break;
293 case TCC_OPTION_B:
294 /* set tcc utilities path (mainly for tcc development) */
295 tcc_set_lib_path(s, optarg);
296 break;
297 case TCC_OPTION_l:
298 dynarray_add((void ***)&files, &nb_files, r);
299 nb_libraries++;
300 break;
301 case TCC_OPTION_pthread:
302 /* fixme: these options could be different on your platform */
303 if(output_type != TCC_OUTPUT_OBJ){
304 dynarray_add((void ***)&files, &nb_files, "-lpthread");
305 nb_libraries++;
307 tcc_define_symbol(s, "_REENTRANT", "1");
308 break;
309 case TCC_OPTION_bench:
310 do_bench = 1;
311 break;
312 #ifdef CONFIG_TCC_BACKTRACE
313 case TCC_OPTION_bt:
314 set_num_callers(atoi(optarg));
315 break;
316 #endif
317 #ifdef CONFIG_TCC_BCHECK
318 case TCC_OPTION_b:
319 s->do_bounds_check = 1;
320 s->do_debug = 1;
321 break;
322 #endif
323 case TCC_OPTION_g:
324 s->do_debug = 1;
325 break;
326 case TCC_OPTION_c:
327 multiple_files = 1;
328 output_type = TCC_OUTPUT_OBJ;
329 break;
330 case TCC_OPTION_static:
331 s->static_link = 1;
332 break;
333 case TCC_OPTION_shared:
334 output_type = TCC_OUTPUT_DLL;
335 break;
336 case TCC_OPTION_soname:
337 s->soname = optarg;
338 break;
339 case TCC_OPTION_o:
340 multiple_files = 1;
341 outfile = optarg;
342 break;
343 case TCC_OPTION_r:
344 /* generate a .o merging several output files */
345 reloc_output = 1;
346 output_type = TCC_OUTPUT_OBJ;
347 break;
348 case TCC_OPTION_nostdinc:
349 s->nostdinc = 1;
350 break;
351 case TCC_OPTION_s:
352 break;
353 case TCC_OPTION_nostdlib:
354 s->nostdlib = 1;
355 break;
356 case TCC_OPTION_print_search_dirs:
357 print_search_dirs = 1;
358 break;
359 case TCC_OPTION_run:
361 int argc1;
362 char **argv1;
363 argc1 = expand_args(&argv1, optarg);
364 if (argc1 > 0) {
365 parse_args(s, argc1, argv1);
367 multiple_files = 0;
368 output_type = TCC_OUTPUT_MEMORY;
370 break;
371 case TCC_OPTION_v:
372 do {
373 if (0 == s->verbose++)
374 printf("tcc version %s\n", TCC_VERSION);
375 } while (*optarg++ == 'v');
376 break;
377 case TCC_OPTION_f:
378 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
379 goto unsupported_option;
380 break;
381 case TCC_OPTION_W:
382 if (tcc_set_warning(s, optarg, 1) < 0 &&
383 s->warn_unsupported)
384 goto unsupported_option;
385 break;
386 case TCC_OPTION_w:
387 s->warn_none = 1;
388 break;
389 case TCC_OPTION_rdynamic:
390 s->rdynamic = 1;
391 break;
392 case TCC_OPTION_Wl:
394 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
395 error("unsupported linker option '%s'", r);
397 break;
398 case TCC_OPTION_E:
399 output_type = TCC_OUTPUT_PREPROCESS;
400 break;
401 case TCC_OPTION_MD:
402 gen_deps = 1;
403 break;
404 case TCC_OPTION_MF:
405 deps_outfile = optarg;
406 break;
407 case TCC_OPTION_x:
408 break;
409 default:
410 if (s->warn_unsupported) {
411 unsupported_option:
412 warning("unsupported option '%s'", r);
414 break;
418 return optind + 1;
421 int main(int argc, char **argv)
423 int i;
424 TCCState *s;
425 int nb_objfiles, ret, optind;
426 int64_t start_time = 0;
428 s = tcc_new();
430 output_type = TCC_OUTPUT_EXE;
431 outfile = NULL;
432 multiple_files = 1;
433 files = NULL;
434 nb_files = 0;
435 nb_libraries = 0;
436 reloc_output = 0;
437 print_search_dirs = 0;
438 ret = 0;
440 optind = parse_args(s, argc - 1, argv + 1);
441 if (print_search_dirs) {
442 /* enough for Linux kernel */
443 printf("install: %s/\n", s->tcc_lib_path);
444 return 0;
446 if (optind == 0 || nb_files == 0) {
447 if (optind && s->verbose)
448 return 0;
449 help();
450 return 1;
453 nb_objfiles = nb_files - nb_libraries;
455 /* if outfile provided without other options, we output an
456 executable */
457 if (outfile && output_type == TCC_OUTPUT_MEMORY)
458 output_type = TCC_OUTPUT_EXE;
460 /* check -c consistency : only single file handled. XXX: checks file type */
461 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
462 /* accepts only a single input file */
463 if (nb_objfiles != 1)
464 error("cannot specify multiple files with -c");
465 if (nb_libraries != 0)
466 error("cannot specify libraries with -c");
470 if (output_type == TCC_OUTPUT_PREPROCESS) {
471 if (!outfile) {
472 s->outfile = stdout;
473 } else {
474 s->outfile = fopen(outfile, "w");
475 if (!s->outfile)
476 error("could not open '%s'", outfile);
480 if (do_bench) {
481 start_time = getclock_us();
484 tcc_set_output_type(s, output_type);
485 s->reloc_output = reloc_output;
487 /* compile or add each files or library */
488 for(i = 0; i < nb_files && ret == 0; i++) {
489 const char *filename;
491 filename = files[i];
492 if (filename[0] == '-' && filename[1] == 'l') {
493 if (tcc_add_library(s, filename + 2) < 0) {
494 error_noabort("cannot find %s", filename);
495 ret = 1;
497 } else {
498 if (1 == s->verbose)
499 printf("-> %s\n", filename);
500 if (tcc_add_file(s, filename) < 0)
501 ret = 1;
505 /* free all files */
506 tcc_free(files);
508 if (0 == ret) {
509 if (do_bench)
510 tcc_print_stats(s, getclock_us() - start_time);
512 if (s->output_type == TCC_OUTPUT_MEMORY)
513 ret = tcc_run(s, argc - optind, argv + optind);
514 else {
515 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
516 if (outfile)
517 fclose(s->outfile);
518 } else {
519 ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
520 ret = ret ? 1 : 0;
523 /* dump collected dependencies */
524 if (gen_deps && !ret)
525 tcc_gen_makedeps(s, outfile, deps_outfile);
529 tcc_delete(s);
531 #ifdef MEM_DEBUG
532 if (do_bench) {
533 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
535 #endif
536 return ret;