tcc: Linux -pthread option sets -D_REENTRANT and -lpthread
[tinycc.git] / tcc.c
blob969e0227c65164be562177a674cc9edf2b2bfaea
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_Wl,
109 TCC_OPTION_W,
110 TCC_OPTION_O,
111 TCC_OPTION_m,
112 TCC_OPTION_f,
113 TCC_OPTION_nostdinc,
114 TCC_OPTION_nostdlib,
115 TCC_OPTION_print_search_dirs,
116 TCC_OPTION_rdynamic,
117 TCC_OPTION_pthread,
118 TCC_OPTION_run,
119 TCC_OPTION_v,
120 TCC_OPTION_w,
121 TCC_OPTION_pipe,
122 TCC_OPTION_E,
123 TCC_OPTION_MD,
124 TCC_OPTION_MF,
125 TCC_OPTION_x,
128 static const TCCOption tcc_options[] = {
129 { "h", TCC_OPTION_HELP, 0 },
130 { "-help", TCC_OPTION_HELP, 0 },
131 { "?", TCC_OPTION_HELP, 0 },
132 { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
133 { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
134 { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
135 { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
136 { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
137 { "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
138 { "bench", TCC_OPTION_bench, 0 },
139 #ifdef CONFIG_TCC_BACKTRACE
140 { "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
141 #endif
142 #ifdef CONFIG_TCC_BCHECK
143 { "b", TCC_OPTION_b, 0 },
144 #endif
145 { "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
146 { "c", TCC_OPTION_c, 0 },
147 { "static", TCC_OPTION_static, 0 },
148 { "shared", TCC_OPTION_shared, 0 },
149 { "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
150 { "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
151 { "pthread", TCC_OPTION_pthread, 0},
152 { "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
153 { "rdynamic", TCC_OPTION_rdynamic, 0 },
154 { "r", TCC_OPTION_r, 0 },
155 { "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
156 { "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
157 { "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
158 { "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
159 { "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
160 { "nostdinc", TCC_OPTION_nostdinc, 0 },
161 { "nostdlib", TCC_OPTION_nostdlib, 0 },
162 { "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
163 { "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
164 { "w", TCC_OPTION_w, 0 },
165 { "pipe", TCC_OPTION_pipe, 0},
166 { "E", TCC_OPTION_E, 0},
167 { "MD", TCC_OPTION_MD, 0},
168 { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
169 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
170 { NULL },
173 static int64_t getclock_us(void)
175 #ifdef _WIN32
176 struct _timeb tb;
177 _ftime(&tb);
178 return (tb.time * 1000LL + tb.millitm) * 1000LL;
179 #else
180 struct timeval tv;
181 gettimeofday(&tv, NULL);
182 return tv.tv_sec * 1000000LL + tv.tv_usec;
183 #endif
186 /* convert 'str' into an array of space separated strings */
187 static int expand_args(char ***pargv, const char *str)
189 const char *s1;
190 char **argv, *arg;
191 int argc, len;
193 argc = 0;
194 argv = NULL;
195 for(;;) {
196 while (is_space(*str))
197 str++;
198 if (*str == '\0')
199 break;
200 s1 = str;
201 while (*str != '\0' && !is_space(*str))
202 str++;
203 len = str - s1;
204 arg = tcc_malloc(len + 1);
205 memcpy(arg, s1, len);
206 arg[len] = '\0';
207 dynarray_add((void ***)&argv, &argc, arg);
209 *pargv = argv;
210 return argc;
213 static int parse_args(TCCState *s, int argc, char **argv)
215 int optind;
216 const TCCOption *popt;
217 const char *optarg, *p1, *r1;
218 char *r;
220 optind = 0;
221 while (optind < argc) {
223 r = argv[optind++];
224 if (r[0] != '-' || r[1] == '\0') {
225 /* add a new file */
226 dynarray_add((void ***)&files, &nb_files, r);
227 if (!multiple_files) {
228 optind--;
229 /* argv[0] will be this file */
230 break;
232 } else {
233 /* find option in table (match only the first chars */
234 popt = tcc_options;
235 for(;;) {
236 p1 = popt->name;
237 if (p1 == NULL)
238 error("invalid option -- '%s'", r);
239 r1 = r + 1;
240 for(;;) {
241 if (*p1 == '\0')
242 goto option_found;
243 if (*r1 != *p1)
244 break;
245 p1++;
246 r1++;
248 popt++;
250 option_found:
251 if (popt->flags & TCC_OPTION_HAS_ARG) {
252 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
253 optarg = r1;
254 } else {
255 if (optind >= argc)
256 error("argument to '%s' is missing", r);
257 optarg = argv[optind++];
259 } else {
260 if (*r1 != '\0')
261 return 0;
262 optarg = NULL;
265 switch(popt->index) {
266 case TCC_OPTION_HELP:
267 return 0;
269 case TCC_OPTION_I:
270 if (tcc_add_include_path(s, optarg) < 0)
271 error("too many include paths");
272 break;
273 case TCC_OPTION_D:
275 char *sym, *value;
276 sym = (char *)optarg;
277 value = strchr(sym, '=');
278 if (value) {
279 *value = '\0';
280 value++;
282 tcc_define_symbol(s, sym, value);
284 break;
285 case TCC_OPTION_U:
286 tcc_undefine_symbol(s, optarg);
287 break;
288 case TCC_OPTION_L:
289 tcc_add_library_path(s, optarg);
290 break;
291 case TCC_OPTION_B:
292 /* set tcc utilities path (mainly for tcc development) */
293 tcc_set_lib_path(s, optarg);
294 break;
295 case TCC_OPTION_l:
296 dynarray_add((void ***)&files, &nb_files, r);
297 nb_libraries++;
298 break;
299 case TCC_OPTION_pthread:
300 /* fixme: these options could be different on your platform */
301 dynarray_add((void ***)&files, &nb_files, "-lpthread");
302 nb_libraries++;
303 tcc_define_symbol(s, "_REENTRANT", "1");
304 break;
305 case TCC_OPTION_bench:
306 do_bench = 1;
307 break;
308 #ifdef CONFIG_TCC_BACKTRACE
309 case TCC_OPTION_bt:
310 set_num_callers(atoi(optarg));
311 break;
312 #endif
313 #ifdef CONFIG_TCC_BCHECK
314 case TCC_OPTION_b:
315 s->do_bounds_check = 1;
316 s->do_debug = 1;
317 break;
318 #endif
319 case TCC_OPTION_g:
320 s->do_debug = 1;
321 break;
322 case TCC_OPTION_c:
323 multiple_files = 1;
324 output_type = TCC_OUTPUT_OBJ;
325 break;
326 case TCC_OPTION_static:
327 s->static_link = 1;
328 break;
329 case TCC_OPTION_shared:
330 output_type = TCC_OUTPUT_DLL;
331 break;
332 case TCC_OPTION_soname:
333 s->soname = optarg;
334 break;
335 case TCC_OPTION_o:
336 multiple_files = 1;
337 outfile = optarg;
338 break;
339 case TCC_OPTION_r:
340 /* generate a .o merging several output files */
341 reloc_output = 1;
342 output_type = TCC_OUTPUT_OBJ;
343 break;
344 case TCC_OPTION_nostdinc:
345 s->nostdinc = 1;
346 break;
347 case TCC_OPTION_nostdlib:
348 s->nostdlib = 1;
349 break;
350 case TCC_OPTION_print_search_dirs:
351 print_search_dirs = 1;
352 break;
353 case TCC_OPTION_run:
355 int argc1;
356 char **argv1;
357 argc1 = expand_args(&argv1, optarg);
358 if (argc1 > 0) {
359 parse_args(s, argc1, argv1);
361 multiple_files = 0;
362 output_type = TCC_OUTPUT_MEMORY;
364 break;
365 case TCC_OPTION_v:
366 do {
367 if (0 == s->verbose++)
368 printf("tcc version %s\n", TCC_VERSION);
369 } while (*optarg++ == 'v');
370 break;
371 case TCC_OPTION_f:
372 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
373 goto unsupported_option;
374 break;
375 case TCC_OPTION_W:
376 if (tcc_set_warning(s, optarg, 1) < 0 &&
377 s->warn_unsupported)
378 goto unsupported_option;
379 break;
380 case TCC_OPTION_w:
381 s->warn_none = 1;
382 break;
383 case TCC_OPTION_rdynamic:
384 s->rdynamic = 1;
385 break;
386 case TCC_OPTION_Wl:
388 if ((r = (char *) tcc_set_linker(s, (char *)optarg, TRUE)))
389 error("unsupported linker option '%s'", r);
391 break;
392 case TCC_OPTION_E:
393 output_type = TCC_OUTPUT_PREPROCESS;
394 break;
395 case TCC_OPTION_MD:
396 gen_deps = 1;
397 break;
398 case TCC_OPTION_MF:
399 deps_outfile = optarg;
400 break;
401 case TCC_OPTION_x:
402 break;
403 default:
404 if (s->warn_unsupported) {
405 unsupported_option:
406 warning("unsupported option '%s'", r);
408 break;
412 return optind + 1;
415 int main(int argc, char **argv)
417 int i;
418 TCCState *s;
419 int nb_objfiles, ret, optind;
420 int64_t start_time = 0;
422 s = tcc_new();
424 output_type = TCC_OUTPUT_EXE;
425 outfile = NULL;
426 multiple_files = 1;
427 files = NULL;
428 nb_files = 0;
429 nb_libraries = 0;
430 reloc_output = 0;
431 print_search_dirs = 0;
432 ret = 0;
434 optind = parse_args(s, argc - 1, argv + 1);
435 if (print_search_dirs) {
436 /* enough for Linux kernel */
437 printf("install: %s/\n", s->tcc_lib_path);
438 return 0;
440 if (optind == 0 || nb_files == 0) {
441 if (optind && s->verbose)
442 return 0;
443 help();
444 return 1;
447 nb_objfiles = nb_files - nb_libraries;
449 /* if outfile provided without other options, we output an
450 executable */
451 if (outfile && output_type == TCC_OUTPUT_MEMORY)
452 output_type = TCC_OUTPUT_EXE;
454 /* check -c consistency : only single file handled. XXX: checks file type */
455 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
456 /* accepts only a single input file */
457 if (nb_objfiles != 1)
458 error("cannot specify multiple files with -c");
459 if (nb_libraries != 0)
460 error("cannot specify libraries with -c");
464 if (output_type == TCC_OUTPUT_PREPROCESS) {
465 if (!outfile) {
466 s->outfile = stdout;
467 } else {
468 s->outfile = fopen(outfile, "w");
469 if (!s->outfile)
470 error("could not open '%s'", outfile);
474 if (do_bench) {
475 start_time = getclock_us();
478 tcc_set_output_type(s, output_type);
479 s->reloc_output = reloc_output;
481 /* compile or add each files or library */
482 for(i = 0; i < nb_files && ret == 0; i++) {
483 const char *filename;
485 filename = files[i];
486 if (filename[0] == '-' && filename[1] == 'l') {
487 if (tcc_add_library(s, filename + 2) < 0) {
488 error_noabort("cannot find %s", filename);
489 ret = 1;
491 } else {
492 if (1 == s->verbose)
493 printf("-> %s\n", filename);
494 if (tcc_add_file(s, filename) < 0)
495 ret = 1;
499 /* free all files */
500 tcc_free(files);
502 if (0 == ret) {
503 if (do_bench)
504 tcc_print_stats(s, getclock_us() - start_time);
506 if (s->output_type == TCC_OUTPUT_MEMORY)
507 ret = tcc_run(s, argc - optind, argv + optind);
508 else {
509 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
510 if (outfile)
511 fclose(s->outfile);
512 } else {
513 ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
514 ret = ret ? 1 : 0;
517 /* dump collected dependencies */
518 if (gen_deps && !ret)
519 tcc_gen_makedeps(s, outfile, deps_outfile);
523 tcc_delete(s);
525 #ifdef MEM_DEBUG
526 if (do_bench) {
527 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
529 #endif
530 return ret;