tcc.h: Extend search path for include, lib and crt.
[tinycc.git] / tcc.c
blobce1945a19ce3de80180e5172a58a5b9014c717e8
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 " -v --version 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 " -C keep comments (not yet implemented)\n"
52 "Linker options:\n"
53 " -Ldir add library path 'dir'\n"
54 " -llib link with dynamic or static library 'lib'\n"
55 " -r generate (relocatable) object file\n"
56 " -shared generate a shared library/dll\n"
57 " -rdynamic export all global symbols to dynamic linker\n"
58 " -soname set name for shared library to be used at runtime\n"
59 " -Wl,-opt[=val] set linker option (see tcc -hh)\n"
60 "Debugger options:\n"
61 " -g generate runtime debug info\n"
62 #ifdef CONFIG_TCC_BCHECK
63 " -b compile with built-in memory and bounds checker (implies -g)\n"
64 #endif
65 #ifdef CONFIG_TCC_BACKTRACE
66 " -bt[N] link with backtrace (stack dump) support [show max N callers]\n"
67 #endif
68 "Misc. options:\n"
69 " -x[c|a|b|n] specify type of the next infile (C,ASM,BIN,NONE)\n"
70 " -nostdinc do not use standard system include paths\n"
71 " -nostdlib do not link with standard crt and libraries\n"
72 " -Bdir set tcc's private include/library dir\n"
73 " -M[M]D generate make dependency file [ignore system files]\n"
74 " -M[M] as above but no other output\n"
75 " -MF file specify dependency file name\n"
76 #if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
77 " -m32/64 defer to i386/x86_64 cross compiler\n"
78 #endif
79 "Tools:\n"
80 " create library : tcc -ar [rcsv] lib.a [files]\n"
81 #ifdef TCC_TARGET_PE
82 " create def file : tcc -impdef lib.dll [-v] [-o lib.def]\n"
83 #endif
86 static const char help2[] =
87 "Tiny C Compiler "TCC_VERSION" - More Options\n"
88 "Special options:\n"
89 " -P -P1 with -E: no/alternative #line output\n"
90 " -dD -dM with -E: output #define directives\n"
91 " -pthread same as -D_REENTRANT and -lpthread\n"
92 " -On same as -D__OPTIMIZE__ for n > 0\n"
93 " -Wp,-opt same as -opt\n"
94 " -include file include 'file' above each input file\n"
95 " -isystem dir add 'dir' to system include path\n"
96 " -static link to static libraries (not recommended)\n"
97 " -dumpversion print version\n"
98 " -print-search-dirs print search paths\n"
99 " -dt with -run/-E: auto-define 'test_...' macros\n"
100 "Ignored options:\n"
101 " -arch -C --param -pedantic -pipe -s -traditional\n"
102 "-W[no-]... warnings:\n"
103 " all turn on some (*) warnings\n"
104 " error[=warning] stop after warning (any or specified)\n"
105 " write-strings strings are const\n"
106 " unsupported warn about ignored options, pragmas, etc.\n"
107 " implicit-function-declaration warn for missing prototype (*)\n"
108 " discarded-qualifiers warn when const is dropped (*)\n"
109 "-f[no-]... flags:\n"
110 " unsigned-char default char is unsigned\n"
111 " signed-char default char is signed\n"
112 " common use common section instead of bss\n"
113 " leading-underscore decorate extern symbols\n"
114 " ms-extensions allow anonymous struct in struct\n"
115 " dollars-in-identifiers allow '$' in C symbols\n"
116 " test-coverage create code coverage code\n"
117 "-m... target specific options:\n"
118 " ms-bitfields use MSVC bitfield layout\n"
119 #ifdef TCC_TARGET_ARM
120 " float-abi hard/softfp on arm\n"
121 #endif
122 #ifdef TCC_TARGET_X86_64
123 " no-sse disable floats on x86_64\n"
124 #endif
125 "-Wl,... linker options:\n"
126 " -nostdlib do not link with standard crt/libs\n"
127 " -[no-]whole-archive load lib(s) fully/only as needed\n"
128 " -export-all-symbols same as -rdynamic\n"
129 " -export-dynamic same as -rdynamic\n"
130 " -image-base= -Ttext= set base address of executable\n"
131 " -section-alignment= set section alignment in executable\n"
132 #ifdef TCC_TARGET_PE
133 " -file-alignment= set PE file alignment\n"
134 " -stack= set PE stack reserve\n"
135 " -large-address-aware set related PE option\n"
136 " -subsystem=[console/windows] set PE subsystem\n"
137 " -oformat=[pe-* binary] set executable output format\n"
138 "Predefined macros:\n"
139 " tcc -E -dM - < nul\n"
140 #else
141 " -rpath= set dynamic library search path\n"
142 " -enable-new-dtags set DT_RUNPATH instead of DT_RPATH\n"
143 " -soname= set DT_SONAME elf tag\n"
144 " -Bsymbolic set DT_SYMBOLIC elf tag\n"
145 " -oformat=[elf32/64-* binary] set executable output format\n"
146 " -init= -fini= -as-needed -O (ignored)\n"
147 "Predefined macros:\n"
148 " tcc -E -dM - < /dev/null\n"
149 #endif
150 "See also the manual for more details.\n"
153 static const char version[] =
154 "tcc version "TCC_VERSION
155 #ifdef TCC_GITHASH
156 " "TCC_GITHASH
157 #endif
158 " ("
159 #ifdef TCC_TARGET_I386
160 "i386"
161 #elif defined TCC_TARGET_X86_64
162 "x86_64"
163 #elif defined TCC_TARGET_C67
164 "C67"
165 #elif defined TCC_TARGET_ARM
166 "ARM"
167 # ifdef TCC_ARM_EABI
168 " eabi"
169 # ifdef TCC_ARM_HARDFLOAT
170 "hf"
171 # endif
172 # endif
173 #elif defined TCC_TARGET_ARM64
174 "AArch64"
175 #elif defined TCC_TARGET_RISCV64
176 "riscv64"
177 #endif
178 #ifdef TCC_TARGET_PE
179 " Windows"
180 #elif defined(TCC_TARGET_MACHO)
181 " Darwin"
182 #elif TARGETOS_FreeBSD || TARGETOS_FreeBSD_kernel
183 " FreeBSD"
184 #elif TARGETOS_OpenBSD
185 " OpenBSD"
186 #elif TARGETOS_NetBSD
187 " NetBSD"
188 #else
189 " Linux"
190 #endif
191 ")\n"
194 static void print_dirs(const char *msg, char **paths, int nb_paths)
196 int i;
197 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
198 for(i = 0; i < nb_paths; i++)
199 printf(" %s\n", paths[i]);
202 static void print_search_dirs(TCCState *s)
204 printf("install: %s\n", s->tcc_lib_path);
205 /* print_dirs("programs", NULL, 0); */
206 print_dirs("include", s->sysinclude_paths, s->nb_sysinclude_paths);
207 print_dirs("libraries", s->library_paths, s->nb_library_paths);
208 #ifdef TCC_TARGET_PE
209 printf("libtcc1:\n %s/lib/"TCC_LIBTCC1"\n", s->tcc_lib_path);
210 #else
211 printf("libtcc1:\n %s/"TCC_LIBTCC1"\n", s->tcc_lib_path);
212 print_dirs("crt", s->crt_paths, s->nb_crt_paths);
213 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
214 #endif
217 static void set_environment(TCCState *s)
219 char * path;
221 path = getenv("C_INCLUDE_PATH");
222 if(path != NULL) {
223 tcc_add_sysinclude_path(s, path);
225 path = getenv("CPATH");
226 if(path != NULL) {
227 tcc_add_include_path(s, path);
229 path = getenv("LIBRARY_PATH");
230 if(path != NULL) {
231 tcc_add_library_path(s, path);
235 static char *default_outputfile(TCCState *s, const char *first_file)
237 char buf[1024];
238 char *ext;
239 const char *name = "a";
241 if (first_file && strcmp(first_file, "-"))
242 name = tcc_basename(first_file);
243 snprintf(buf, sizeof(buf), "%s", name);
244 ext = tcc_fileextension(buf);
245 #ifdef TCC_TARGET_PE
246 if (s->output_type == TCC_OUTPUT_DLL)
247 strcpy(ext, ".dll");
248 else
249 if (s->output_type == TCC_OUTPUT_EXE)
250 strcpy(ext, ".exe");
251 else
252 #endif
253 if ((s->just_deps || s->output_type == TCC_OUTPUT_OBJ) && !s->option_r && *ext)
254 strcpy(ext, ".o");
255 else
256 strcpy(buf, "a.out");
257 return tcc_strdup(buf);
260 static unsigned getclock_ms(void)
262 #ifdef _WIN32
263 return GetTickCount();
264 #else
265 struct timeval tv;
266 gettimeofday(&tv, NULL);
267 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
268 #endif
271 int main(int argc0, char **argv0)
273 TCCState *s, *s1;
274 int ret, opt, n = 0, t = 0, done;
275 unsigned start_time = 0, end_time = 0;
276 const char *first_file;
277 int argc; char **argv;
278 FILE *ppfp = stdout;
280 redo:
281 argc = argc0, argv = argv0;
282 s = s1 = tcc_new();
283 opt = tcc_parse_args(s, &argc, &argv, 1);
285 if (n == 0) {
286 if (opt == OPT_HELP) {
287 fputs(help, stdout);
288 if (!s->verbose)
289 return 0;
290 ++opt;
292 if (opt == OPT_HELP2) {
293 fputs(help2, stdout);
294 return 0;
296 if (opt == OPT_M32 || opt == OPT_M64)
297 tcc_tool_cross(s, argv, opt); /* never returns */
298 if (s->verbose)
299 printf(version);
300 if (opt == OPT_AR)
301 return tcc_tool_ar(s, argc, argv);
302 #ifdef TCC_TARGET_PE
303 if (opt == OPT_IMPDEF)
304 return tcc_tool_impdef(s, argc, argv);
305 #endif
306 if (opt == OPT_V)
307 return 0;
308 if (opt == OPT_PRINT_DIRS) {
309 /* initialize search dirs */
310 set_environment(s);
311 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
312 print_search_dirs(s);
313 return 0;
316 if (s->nb_files == 0)
317 tcc_error("no input files");
319 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
320 if (s->outfile && 0!=strcmp("-",s->outfile)) {
321 ppfp = fopen(s->outfile, "w");
322 if (!ppfp)
323 tcc_error("could not write '%s'", s->outfile);
325 } else if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
326 if (s->nb_libraries)
327 tcc_error("cannot specify libraries with -c");
328 if (s->nb_files > 1 && s->outfile)
329 tcc_error("cannot specify output file with -c many files");
332 if (s->do_bench)
333 start_time = getclock_ms();
336 set_environment(s);
337 if (s->output_type == 0)
338 s->output_type = TCC_OUTPUT_EXE;
339 tcc_set_output_type(s, s->output_type);
340 s->ppfp = ppfp;
342 if ((s->output_type == TCC_OUTPUT_MEMORY
343 || s->output_type == TCC_OUTPUT_PREPROCESS)
344 && (s->dflag & 16)) { /* -dt option */
345 if (t)
346 s->dflag |= 32;
347 s->run_test = ++t;
348 if (n)
349 --n;
352 /* compile or add each files or library */
353 first_file = NULL, ret = 0;
354 do {
355 struct filespec *f = s->files[n];
356 s->filetype = f->type;
357 if (f->type & AFF_TYPE_LIB) {
358 if (tcc_add_library_err(s, f->name) < 0)
359 ret = 1;
360 } else {
361 if (1 == s->verbose)
362 printf("-> %s\n", f->name);
363 if (!first_file)
364 first_file = f->name;
365 if (tcc_add_file(s, f->name) < 0)
366 ret = 1;
368 done = ret || ++n >= s->nb_files;
369 } while (!done && (s->output_type != TCC_OUTPUT_OBJ || s->option_r));
371 if (s->do_bench)
372 end_time = getclock_ms();
374 if (s->run_test) {
375 t = 0;
376 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
378 } else if (0 == ret) {
379 if (s->output_type == TCC_OUTPUT_MEMORY) {
380 #ifdef TCC_IS_NATIVE
381 ret = tcc_run(s, argc, argv);
382 #endif
383 } else {
384 if (!s->outfile)
385 s->outfile = default_outputfile(s, first_file);
386 if (!s->just_deps && tcc_output_file(s, s->outfile))
387 ret = 1;
388 else if (s->gen_deps)
389 gen_makedeps(s, s->outfile, s->deps_outfile);
393 if (done && 0 == t && 0 == ret && s->do_bench)
394 tcc_print_stats(s, end_time - start_time);
396 tcc_delete(s);
397 if (!done)
398 goto redo; /* compile more files with -c */
399 if (t)
400 goto redo; /* run more tests with -dt -run */
402 if (ppfp && ppfp != stdout)
403 fclose(ppfp);
404 return ret;