libtcc: Allow multiple options for -Wl separated with ','
[tinycc.git] / tcc.c
blob06537735c3ab21286678250d4314a976b89c824a
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 /* convert 'str' into an array of space separated strings */
175 static int expand_args(char ***pargv, const char *str)
177 const char *s1;
178 char **argv, *arg;
179 int argc, len;
181 argc = 0;
182 argv = NULL;
183 for(;;) {
184 while (is_space(*str))
185 str++;
186 if (*str == '\0')
187 break;
188 s1 = str;
189 while (*str != '\0' && !is_space(*str))
190 str++;
191 len = str - s1;
192 arg = tcc_malloc(len + 1);
193 memcpy(arg, s1, len);
194 arg[len] = '\0';
195 dynarray_add((void ***)&argv, &argc, arg);
197 *pargv = argv;
198 return argc;
201 static int parse_args(TCCState *s, int argc, char **argv)
203 int optind;
204 const TCCOption *popt;
205 const char *optarg, *p1, *r1;
206 char *r;
208 optind = 0;
209 while (optind < argc) {
211 r = argv[optind++];
212 if (r[0] != '-' || r[1] == '\0') {
213 /* add a new file */
214 dynarray_add((void ***)&files, &nb_files, r);
215 if (!multiple_files) {
216 optind--;
217 /* argv[0] will be this file */
218 break;
220 } else {
221 /* find option in table (match only the first chars */
222 popt = tcc_options;
223 for(;;) {
224 p1 = popt->name;
225 if (p1 == NULL)
226 error("invalid option -- '%s'", r);
227 r1 = r + 1;
228 for(;;) {
229 if (*p1 == '\0')
230 goto option_found;
231 if (*r1 != *p1)
232 break;
233 p1++;
234 r1++;
236 popt++;
238 option_found:
239 if (popt->flags & TCC_OPTION_HAS_ARG) {
240 if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
241 optarg = r1;
242 } else {
243 if (optind >= argc)
244 error("argument to '%s' is missing", r);
245 optarg = argv[optind++];
247 } else {
248 if (*r1 != '\0')
249 return 0;
250 optarg = NULL;
253 switch(popt->index) {
254 case TCC_OPTION_HELP:
255 return 0;
257 case TCC_OPTION_I:
258 if (tcc_add_include_path(s, optarg) < 0)
259 error("too many include paths");
260 break;
261 case TCC_OPTION_D:
263 char *sym, *value;
264 sym = (char *)optarg;
265 value = strchr(sym, '=');
266 if (value) {
267 *value = '\0';
268 value++;
270 tcc_define_symbol(s, sym, value);
272 break;
273 case TCC_OPTION_U:
274 tcc_undefine_symbol(s, optarg);
275 break;
276 case TCC_OPTION_L:
277 tcc_add_library_path(s, optarg);
278 break;
279 case TCC_OPTION_B:
280 /* set tcc utilities path (mainly for tcc development) */
281 tcc_set_lib_path(s, optarg);
282 break;
283 case TCC_OPTION_l:
284 dynarray_add((void ***)&files, &nb_files, r);
285 nb_libraries++;
286 break;
287 case TCC_OPTION_bench:
288 do_bench = 1;
289 break;
290 #ifdef CONFIG_TCC_BACKTRACE
291 case TCC_OPTION_bt:
292 set_num_callers(atoi(optarg));
293 break;
294 #endif
295 #ifdef CONFIG_TCC_BCHECK
296 case TCC_OPTION_b:
297 s->do_bounds_check = 1;
298 s->do_debug = 1;
299 break;
300 #endif
301 case TCC_OPTION_g:
302 s->do_debug = 1;
303 break;
304 case TCC_OPTION_c:
305 multiple_files = 1;
306 output_type = TCC_OUTPUT_OBJ;
307 break;
308 case TCC_OPTION_static:
309 s->static_link = 1;
310 break;
311 case TCC_OPTION_shared:
312 output_type = TCC_OUTPUT_DLL;
313 break;
314 case TCC_OPTION_soname:
315 s->soname = optarg;
316 break;
317 case TCC_OPTION_o:
318 multiple_files = 1;
319 outfile = optarg;
320 break;
321 case TCC_OPTION_r:
322 /* generate a .o merging several output files */
323 reloc_output = 1;
324 output_type = TCC_OUTPUT_OBJ;
325 break;
326 case TCC_OPTION_nostdinc:
327 s->nostdinc = 1;
328 break;
329 case TCC_OPTION_nostdlib:
330 s->nostdlib = 1;
331 break;
332 case TCC_OPTION_print_search_dirs:
333 print_search_dirs = 1;
334 break;
335 case TCC_OPTION_run:
337 int argc1;
338 char **argv1;
339 argc1 = expand_args(&argv1, optarg);
340 if (argc1 > 0) {
341 parse_args(s, argc1, argv1);
343 multiple_files = 0;
344 output_type = TCC_OUTPUT_MEMORY;
346 break;
347 case TCC_OPTION_v:
348 do {
349 if (0 == s->verbose++)
350 printf("tcc version %s\n", TCC_VERSION);
351 } while (*optarg++ == 'v');
352 break;
353 case TCC_OPTION_f:
354 if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
355 goto unsupported_option;
356 break;
357 case TCC_OPTION_W:
358 if (tcc_set_warning(s, optarg, 1) < 0 &&
359 s->warn_unsupported)
360 goto unsupported_option;
361 break;
362 case TCC_OPTION_w:
363 s->warn_none = 1;
364 break;
365 case TCC_OPTION_rdynamic:
366 s->rdynamic = 1;
367 break;
368 case TCC_OPTION_Wl:
370 if ((r = (char *)tcc_set_linker(s, optarg, TRUE)))
371 error("unsupported linker option '%s'", r);
373 break;
374 case TCC_OPTION_E:
375 output_type = TCC_OUTPUT_PREPROCESS;
376 break;
377 case TCC_OPTION_x:
378 break;
379 default:
380 if (s->warn_unsupported) {
381 unsupported_option:
382 warning("unsupported option '%s'", r);
384 break;
388 return optind + 1;
391 int main(int argc, char **argv)
393 int i;
394 TCCState *s;
395 int nb_objfiles, ret, optind;
396 char objfilename[1024];
397 int64_t start_time = 0;
399 s = tcc_new();
401 output_type = TCC_OUTPUT_EXE;
402 outfile = NULL;
403 multiple_files = 1;
404 files = NULL;
405 nb_files = 0;
406 nb_libraries = 0;
407 reloc_output = 0;
408 print_search_dirs = 0;
409 ret = 0;
411 optind = parse_args(s, argc - 1, argv + 1);
412 if (print_search_dirs) {
413 /* enough for Linux kernel */
414 printf("install: %s/\n", s->tcc_lib_path);
415 return 0;
417 if (optind == 0 || nb_files == 0) {
418 if (optind && s->verbose)
419 return 0;
420 help();
421 return 1;
424 nb_objfiles = nb_files - nb_libraries;
426 /* if outfile provided without other options, we output an
427 executable */
428 if (outfile && output_type == TCC_OUTPUT_MEMORY)
429 output_type = TCC_OUTPUT_EXE;
431 /* check -c consistency : only single file handled. XXX: checks file type */
432 if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
433 /* accepts only a single input file */
434 if (nb_objfiles != 1)
435 error("cannot specify multiple files with -c");
436 if (nb_libraries != 0)
437 error("cannot specify libraries with -c");
441 if (output_type == TCC_OUTPUT_PREPROCESS) {
442 if (!outfile) {
443 s->outfile = stdout;
444 } else {
445 s->outfile = fopen(outfile, "w");
446 if (!s->outfile)
447 error("could not open '%s", outfile);
449 } else if (output_type != TCC_OUTPUT_MEMORY) {
450 if (!outfile) {
451 /* compute default outfile name */
452 char *ext;
453 const char *name =
454 strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
455 pstrcpy(objfilename, sizeof(objfilename), name);
456 ext = tcc_fileextension(objfilename);
457 #ifdef TCC_TARGET_PE
458 if (output_type == TCC_OUTPUT_DLL)
459 strcpy(ext, ".dll");
460 else
461 if (output_type == TCC_OUTPUT_EXE)
462 strcpy(ext, ".exe");
463 else
464 #endif
465 if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
466 strcpy(ext, ".o");
467 else
468 pstrcpy(objfilename, sizeof(objfilename), "a.out");
469 outfile = objfilename;
473 if (do_bench) {
474 start_time = getclock_us();
477 tcc_set_output_type(s, output_type);
479 /* compile or add each files or library */
480 for(i = 0; i < nb_files && ret == 0; i++) {
481 const char *filename;
483 filename = files[i];
484 if (filename[0] == '-' && filename[1]) {
485 if (tcc_add_library(s, filename + 2) < 0) {
486 error_noabort("cannot find %s", filename);
487 ret = 1;
489 } else {
490 if (1 == s->verbose)
491 printf("-> %s\n", filename);
492 if (tcc_add_file(s, filename) < 0)
493 ret = 1;
497 /* free all files */
498 tcc_free(files);
500 if (0 == ret) {
501 if (do_bench)
502 tcc_print_stats(s, getclock_us() - start_time);
504 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
505 if (outfile)
506 fclose(s->outfile);
507 } else if (s->output_type == TCC_OUTPUT_MEMORY)
508 ret = tcc_run(s, argc - optind, argv + optind);
509 else
510 ret = tcc_output_file(s, outfile) ? 1 : 0;
513 tcc_delete(s);
515 #ifdef MEM_DEBUG
516 if (do_bench) {
517 printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
519 #endif
520 return ret;