Missied a commit in order to support -v --help the same way as gcc does
[tinycc.git] / tcc.c
blob1913ffc4f2a87d5f9bd2a15eb8f64d2c702db7d1
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 #include "tcc.h"
22 #if ONE_SOURCE
23 # include "libtcc.c"
24 #endif
25 #include "tcctools.c"
27 static const char help[] =
28 "Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
29 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
30 " tcc [options...] -run infile [arguments...]\n"
31 "General options:\n"
32 " -c compile only - generate an object file\n"
33 " -o outfile set output filename\n"
34 " -run run compiled source\n"
35 " -fflag set or reset (with 'no-' prefix) 'flag' (see tcc -hh)\n"
36 " -std=c99 Conform to the ISO 1999 C standard (default).\n"
37 " -std=c11 Conform to the ISO 2011 C standard.\n"
38 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see tcc -hh)\n"
39 " -w disable all warnings\n"
40 " --version -v show version\n"
41 " -vv show search paths or loaded files\n"
42 " -h -hh show this, show more help\n"
43 " -bench show compilation statistics\n"
44 " - use stdin pipe as infile\n"
45 " @listfile read arguments from listfile\n"
46 "Preprocessor options:\n"
47 " -Idir add include path 'dir'\n"
48 " -Dsym[=val] define 'sym' with value 'val'\n"
49 " -Usym undefine 'sym'\n"
50 " -E preprocess only\n"
51 "Linker options:\n"
52 " -Ldir add library path 'dir'\n"
53 " -llib link with dynamic or static library 'lib'\n"
54 " -r generate (relocatable) object file\n"
55 " -shared generate a shared library/dll\n"
56 " -rdynamic export all global symbols to dynamic linker\n"
57 " -soname set name for shared library to be used at runtime\n"
58 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
59 "Debugger options:\n"
60 " -g generate runtime debug info\n"
61 #ifdef CONFIG_TCC_BCHECK
62 " -b compile with built-in memory and bounds checker (implies -g)\n"
63 #endif
64 #ifdef CONFIG_TCC_BACKTRACE
65 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
66 #endif
67 "Misc. options:\n"
68 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
69 " -nostdinc do not use standard system include paths\n"
70 " -nostdlib do not link with standard crt and libraries\n"
71 " -Bdir set tcc's private include/library dir\n"
72 " -MD generate dependency file for make\n"
73 " -MF file specify dependency file name\n"
74 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
75 " -m32/64 defer to i386/x86_64 cross compiler\n"
76 #endif
77 "Tools:\n"
78 " create library : tcc -ar [rcsv] lib.a files\n"
79 #ifdef TCC_TARGET_PE
80 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
81 #endif
84 static const char help2[] =
85 "Tiny C Compiler "TCC_VERSION" - More Options\n"
86 "Special options:\n"
87 " -P -P1 with -E: no/alternative #line output\n"
88 " -dD -dM with -E: output #define directives\n"
89 " -pthread same as -D_REENTRANT and -lpthread\n"
90 " -On same as -D__OPTIMIZE__ for n > 0\n"
91 " -Wp,-opt same as -opt\n"
92 " -include file include 'file' above each input file\n"
93 " -isystem dir add 'dir' to system include path\n"
94 " -static link to static libraries (not recommended)\n"
95 " -dumpversion print version\n"
96 " -print-search-dirs print search paths\n"
97 " -dt with -run/-E: auto-define 'test_...' macros\n"
98 "Ignored options:\n"
99 " --param -pedantic -pipe -s -traditional\n"
100 "-W... warnings:\n"
101 " all turn on some (*) warnings\n"
102 " error stop after first warning\n"
103 " unsupported warn about ignored options, pragmas, etc.\n"
104 " write-strings strings are const\n"
105 " implicit-function-declaration warn for missing prototype (*)\n"
106 "-f[no-]... flags:\n"
107 " unsigned-char default char is unsigned\n"
108 " signed-char default char is signed\n"
109 " common use common section instead of bss\n"
110 " leading-underscore decorate extern symbols\n"
111 " ms-extensions allow anonymous struct in struct\n"
112 " dollars-in-identifiers allow '$' in C symbols\n"
113 "-m... target specific options:\n"
114 " ms-bitfields use MSVC bitfield layout\n"
115 #ifdef TCC_TARGET_ARM
116 " float-abi hard/softfp on arm\n"
117 #endif
118 #ifdef TCC_TARGET_X86_64
119 " no-sse disable floats on x86_64\n"
120 #endif
121 "-Wl,... linker options:\n"
122 " -nostdlib do not link with standard crt/libs\n"
123 " -[no-]whole-archive load lib(s) fully/only as needed\n"
124 " -export-all-symbols same as -rdynamic\n"
125 " -export-dynamic same as -rdynamic\n"
126 " -image-base= -Ttext= set base address of executable\n"
127 " -section-alignment= set section alignment in executable\n"
128 #ifdef TCC_TARGET_PE
129 " -file-alignment= set PE file alignment\n"
130 " -stack= set PE stack reserve\n"
131 " -large-address-aware set related PE option\n"
132 " -subsystem=[console/windows] set PE subsystem\n"
133 " -oformat=[pe-* binary] set executable output format\n"
134 "Predefined macros:\n"
135 " tcc -E -dM - < nul\n"
136 #else
137 " -rpath= set dynamic library search path\n"
138 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
139 " -soname= set DT_SONAME elf tag\n"
140 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
141 " -oformat=[elf32/64-* binary] set executable output format\n"
142 " -init= -fini= -as-needed -O (ignored)\n"
143 "Predefined macros:\n"
144 " tcc -E -dM - < /dev/null\n"
145 #endif
146 "See also the manual for more details.\n"
149 static const char version[] =
150 "tcc version "TCC_VERSION" ("
151 #ifdef TCC_TARGET_I386
152 "i386"
153 #elif defined TCC_TARGET_X86_64
154 "x86_64"
155 #elif defined TCC_TARGET_C67
156 "C67"
157 #elif defined TCC_TARGET_ARM
158 "ARM"
159 #elif defined TCC_TARGET_ARM64
160 "AArch64"
161 #elif defined TCC_TARGET_RISCV64
162 "riscv64"
163 #endif
164 #ifdef TCC_ARM_HARDFLOAT
165 " Hard Float"
166 #endif
167 #ifdef TCC_TARGET_PE
168 " Windows"
169 #elif defined(TCC_TARGET_MACHO)
170 " Darwin"
171 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
172 " FreeBSD"
173 #else
174 " Linux"
175 #endif
176 ")\n"
179 static void print_dirs(const char *msg, char **paths, int nb_paths)
181 int i;
182 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
183 for(i = 0; i < nb_paths; i++)
184 printf(" %s\n", paths[i]);
187 static void print_search_dirs(TCCState *s)
189 printf("install: %s\n", s->tcc_lib_path);
190 /* print_dirs("programs", NULL, 0); */
191 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
192 print_dirs("libraries", s->library_paths, s->nb_library_paths);
193 #ifdef TCC_TARGET_PE
194 printf("libtcc1:\n %s/lib/"TCC_LIBTCC1"\n", s->tcc_lib_path);
195 #else
196 printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
197 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
198 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
199 #endif
202 static void set_environment(TCCState *s)
204 char * path;
206 path = getenv("C_INCLUDE_PATH");
207 if(path != NULL) {
208 tcc_add_sysinclude_path(s, path);
210 path = getenv("CPATH");
211 if(path != NULL) {
212 tcc_add_include_path(s, path);
214 path = getenv("LIBRARY_PATH");
215 if(path != NULL) {
216 tcc_add_library_path(s, path);
220 static char *default_outputfile(TCCState *s, const char *first_file)
222 char buf[1024];
223 char *ext;
224 const char *name = "a";
226 if (first_file && strcmp(first_file, "-"))
227 name = tcc_basename(first_file);
228 snprintf(buf, sizeof(buf), "%s", name);
229 ext = tcc_fileextension(buf);
230 #ifdef TCC_TARGET_PE
231 if (s->output_type == TCC_OUTPUT_DLL)
232 strcpy(ext, ".dll");
233 else
234 if (s->output_type == TCC_OUTPUT_EXE)
235 strcpy(ext, ".exe");
236 else
237 #endif
238 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
239 strcpy(ext, ".o");
240 else
241 strcpy(buf, "a.out");
242 return tcc_strdup(buf);
245 static unsigned getclock_ms(void)
247 #ifdef _WIN32
248 return GetTickCount();
249 #else
250 struct timeval tv;
251 gettimeofday(&tv, NULL);
252 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
253 #endif
256 int main(int argc0, char **argv0)
258 TCCState *s, *s1;
259 int ret, opt, n = 0, t = 0, done;
260 unsigned start_time = 0;
261 const char *first_file;
262 int argc; char **argv;
263 FILE *ppfp = stdout;
265 redo:
266 argc = argc0, argv = argv0;
267 s = s1 = tcc_new();
268 opt = tcc_parse_args(s, &argc, &argv, 1);
270 if (n == 0) {
271 if (opt == OPT_HELP) {
272 fputs(help, stdout);
273 return 0;
275 if (opt == OPT_HELP2) {
276 fputs(help, stdout);
277 return 0;
279 if (opt == OPT_VERBOSE_HELP) {
280 /* simulate gcc -v --help */
281 fputs(help, stdout);
282 fputs(help2, stdout);
283 return 0;
285 if (opt == OPT_M32 || opt == OPT_M64)
286 tcc_tool_cross(s, argv, opt); /* never returns */
287 if (s->verbose)
288 printf(version);
289 if (opt == OPT_AR)
290 return tcc_tool_ar(s, argc, argv);
291 #ifdef TCC_TARGET_PE
292 if (opt == OPT_IMPDEF)
293 return tcc_tool_impdef(s, argc, argv);
294 #endif
295 if (opt == OPT_V)
296 return 0;
297 if (opt == OPT_PRINT_DIRS) {
298 /* initialize search dirs */
299 set_environment(s);
300 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
301 print_search_dirs(s);
302 return 0;
305 if (s->nb_files == 0)
306 tcc_error("no input files\n");
308 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
309 if (s->outfile && 0!=strcmp("-",s->outfile)) {
310 ppfp = fopen(s->outfile, "w");
311 if (!ppfp)
312 tcc_error("could not write '%s'", s->outfile);
314 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
315 if (s->nb_libraries)
316 tcc_error("cannot specify libraries with -c");
317 if (s->nb_files > 1 && s->outfile)
318 tcc_error("cannot specify output file with -c many files");
319 } else {
320 if (s->option_pthread)
321 tcc_set_options(s, "-lpthread");
324 if (s->do_bench)
325 start_time = getclock_ms();
328 set_environment(s);
329 if (s->output_type == 0)
330 s->output_type = TCC_OUTPUT_EXE;
331 tcc_set_output_type(s, s->output_type);
332 s->ppfp = ppfp;
334 if ((s->output_type == TCC_OUTPUT_MEMORY
335 || s->output_type == TCC_OUTPUT_PREPROCESS)
336 && (s->dflag & 16)) { /* -dt option */
337 if (t)
338 s->dflag |= 32;
339 s->run_test = ++t;
340 if (n)
341 --n;
344 /* compile or add each files or library */
345 first_file = NULL, ret = 0;
346 do {
347 struct filespec *f = s->files[n];
348 s->filetype = f->type;
349 if (f->type & AFF_TYPE_LIB) {
350 if (tcc_add_library_err(s, f->name) < 0)
351 ret = 1;
352 } else {
353 if (1 == s->verbose)
354 printf("-> %s\n", f->name);
355 if (!first_file)
356 first_file = f->name;
357 if (tcc_add_file(s, f->name) < 0)
358 ret = 1;
360 done = ret || ++n >= s->nb_files;
361 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
363 if (s->run_test) {
364 t = 0;
365 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
367 } else if (0 == ret) {
368 if (s->output_type == TCC_OUTPUT_MEMORY) {
369 #ifdef TCC_IS_NATIVE
370 ret = tcc_run(s, argc, argv);
371 #endif
372 } else {
373 if (!s->outfile)
374 s->outfile = default_outputfile(s, first_file);
375 if (tcc_output_file(s, s->outfile))
376 ret = 1;
377 else if (s->gen_deps)
378 gen_makedeps(s, s->outfile, s->deps_outfile);
382 if (s->do_bench && done && !(t | ret))
383 tcc_print_stats(s, getclock_ms() - start_time);
384 tcc_delete(s);
385 if (!done)
386 goto redo; /* compile more files with -c */
387 if (t)
388 goto redo; /* run more tests with -dt -run */
389 if (ppfp && ppfp != stdout)
390 fclose(ppfp);
391 return ret;