tests: OOT build fixes etc.
[tinycc.git] / tcc.c
blob2ffbde7c5de2de0ce82271e95b637601c5c58177
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 #ifdef ONE_SOURCE
22 #include "libtcc.c"
23 #else
24 #include "tcc.h"
25 #endif
27 static void print_paths(const char *msg, char **paths, int nb_paths)
29 int i;
30 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
31 for(i = 0; i < nb_paths; i++)
32 printf(" %s\n", paths[i]);
35 static void display_info(TCCState *s, int what)
37 switch (what) {
38 case 0:
39 printf("tcc version %s ("
40 #ifdef TCC_TARGET_I386
41 "i386"
42 #elif defined TCC_TARGET_X86_64
43 "x86-64"
44 #elif defined TCC_TARGET_C67
45 "C67"
46 #elif defined TCC_TARGET_ARM
47 "ARM"
48 # ifdef TCC_ARM_HARDFLOAT
49 " Hard Float"
50 # endif
51 #elif defined TCC_TARGET_ARM64
52 "AArch64"
53 # ifdef TCC_ARM_HARDFLOAT
54 " Hard Float"
55 # endif
56 #endif
57 #ifdef TCC_TARGET_PE
58 " Windows"
59 #elif defined(__APPLE__)
60 /* Current Apple OS name as of 2016 */
61 " macOS"
62 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
63 " FreeBSD"
64 #elif defined(__DragonFly__)
65 " DragonFly BSD"
66 #elif defined(__NetBSD__)
67 " NetBSD"
68 #elif defined(__OpenBSD__)
69 " OpenBSD"
70 #elif defined(__linux__)
71 " Linux"
72 #else
73 " Unidentified system"
74 #endif
75 ")\n", TCC_VERSION);
76 break;
77 case 1:
78 printf("install: %s\n", s->tcc_lib_path);
79 /* print_paths("programs", NULL, 0); */
80 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
81 print_paths("libraries", s->library_paths, s->nb_library_paths);
82 #ifndef TCC_TARGET_PE
83 print_paths("crt", s->crt_paths, s->nb_crt_paths);
84 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
85 #endif
86 break;
90 static void help(void)
92 printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
93 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
94 " tcc [options...] -run infile [arguments...]\n"
95 "General options:\n"
96 " -c compile only - generate an object file\n"
97 " -o outfile set output filename\n"
98 " -run run compiled source\n"
99 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
100 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
101 " -w disable all warnings\n"
102 " -v show version\n"
103 " -vv show included files (as sole argument: show search paths)\n"
104 " -bench show compilation statistics\n"
105 "Preprocessor options:\n"
106 " -Idir add include path 'dir'\n"
107 " -Dsym[=val] define 'sym' with value 'val'\n"
108 " -Usym undefine 'sym'\n"
109 " -E preprocess only\n"
110 " -P[1] no / alternative #line output with -E\n"
111 " -dD -dM output #define directives with -E\n"
112 "Linker options:\n"
113 " -Ldir add library path 'dir'\n"
114 " -llib link with dynamic or static library 'lib'\n"
115 " -r generate (relocatable) object file\n"
116 " -shared generate a shared library\n"
117 " -rdynamic export all global symbols to dynamic linker\n"
118 " -soname set name for shared library to be used at runtime\n"
119 " -static static linking\n"
120 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
121 " -Wl,-opt[=val] set linker option (see manual)\n"
122 "Debugger options:\n"
123 " -g generate runtime debug info\n"
124 #ifdef CONFIG_TCC_BCHECK
125 " -b compile with built-in memory and bounds checker (implies -g)\n"
126 #endif
127 #ifdef CONFIG_TCC_BACKTRACE
128 " -bt N show N callers in stack traces\n"
129 #endif
130 "Misc options:\n"
131 " -x[c|a|n] specify type of the next infile\n"
132 " -nostdinc do not use standard system include paths\n"
133 " -nostdlib do not link with standard crt and libraries\n"
134 " -Bdir use 'dir' as tcc's private library/include path\n"
135 " -MD generate target dependencies for make\n"
136 " -MF depfile put generated dependencies here\n"
137 " -dumpversion print version\n"
138 " - use stdin pipe as infile\n"
139 " @listfile read arguments from listfile\n"
140 "Target specific options:\n"
141 " -m32/64 execute i386/x86-64 cross compiler\n"
142 " -mms-bitfields use MSVC bitfield layout\n"
143 #ifdef TCC_TARGET_ARM
144 " -mfloat-abi hard/softfp on arm\n"
145 #endif
146 #ifdef TCC_TARGET_X86_64
147 " -mno-sse disable floats on x86-64\n"
148 #endif
152 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
153 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
154 #ifdef _WIN32
155 #include <process.h>
156 static int execvp_win32(const char *prog, char **argv)
158 int ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
159 if (-1 == ret)
160 return ret;
161 cwait(&ret, ret, WAIT_CHILD);
162 exit(ret);
164 #define execvp execvp_win32
165 #endif
166 static void exec_other_tcc(TCCState *s, char **argv, int option)
168 char child_path[4096], *child_name; const char *target;
169 switch (option) {
170 #ifdef TCC_TARGET_I386
171 case 32: break;
172 case 64: target = "x86_64";
173 #else
174 case 64: break;
175 case 32: target = "i386";
176 #endif
177 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
178 child_name = tcc_basename(child_path);
179 strcpy(child_name, target);
180 #ifdef TCC_TARGET_PE
181 strcat(child_name, "-win32");
182 #endif
183 strcat(child_name, "-tcc");
184 if (strcmp(argv[0], child_path)) {
185 if (s->verbose > 0)
186 printf("tcc: using '%s'\n", child_name), fflush(stdout);
187 execvp(argv[0] = child_path, argv);
189 tcc_error("'%s' not found", child_name);
192 #else
193 #define exec_other_tcc(s, argv, option)
194 #endif
196 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
198 FILE *depout;
199 char buf[1024], *ext;
200 int i;
202 if (!filename) {
203 /* compute filename automatically
204 * dir/file.o -> dir/file.d */
205 pstrcpy(buf, sizeof(buf), target);
206 ext = tcc_fileextension(buf);
207 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
208 filename = buf;
211 if (s->verbose)
212 printf("<- %s\n", filename);
214 /* XXX return err codes instead of error() ? */
215 depout = fopen(filename, "w");
216 if (!depout)
217 tcc_error("could not open '%s'", filename);
219 fprintf(depout, "%s: \\\n", target);
220 for (i=0; i<s->nb_target_deps; ++i)
221 fprintf(depout, " %s \\\n", s->target_deps[i]);
222 fprintf(depout, "\n");
223 fclose(depout);
226 static char *default_outputfile(TCCState *s, const char *first_file)
228 char buf[1024];
229 char *ext;
230 const char *name = "a";
232 if (first_file && strcmp(first_file, "-"))
233 name = tcc_basename(first_file);
234 pstrcpy(buf, sizeof(buf), name);
235 ext = tcc_fileextension(buf);
236 #ifdef TCC_TARGET_PE
237 if (s->output_type == TCC_OUTPUT_DLL)
238 strcpy(ext, ".dll");
239 else
240 if (s->output_type == TCC_OUTPUT_EXE)
241 strcpy(ext, ".exe");
242 else
243 #endif
244 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
245 strcpy(ext, ".o");
246 else
247 strcpy(buf, "a.out");
249 return tcc_strdup(buf);
252 static unsigned getclock_ms(void)
254 #ifdef _WIN32
255 return GetTickCount();
256 #else
257 struct timeval tv;
258 gettimeofday(&tv, NULL);
259 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
260 #endif
263 int main(int argc, char **argv)
265 TCCState *s;
266 int ret, optind, i;
267 unsigned start_time = 0;
268 const char *first_file = NULL;
270 s = tcc_new();
272 optind = tcc_parse_args(s, argc - 1, argv + 1);
274 tcc_set_environment(s);
276 if (optind == 0) {
277 help();
278 return 1;
281 if (s->cross_target)
282 exec_other_tcc(s, argv, s->cross_target);
284 if (s->verbose)
285 display_info(s, 0);
287 if (s->nb_files == 0) {
288 if (optind == 1) {
289 if (s->print_search_dirs || s->verbose == 2) {
290 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
291 display_info(s, 1);
292 return 1;
294 if (s->verbose)
295 return 1;
297 tcc_error("no input files\n");
300 /* check -c consistency : only single file handled. XXX: checks file type */
301 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
302 if (s->nb_libraries != 0)
303 tcc_error("cannot specify libraries with -c");
304 /* accepts only a single input file */
305 if (s->nb_files != 1)
306 tcc_error("cannot specify multiple files with -c");
309 if (s->output_type == 0)
310 s->output_type = TCC_OUTPUT_EXE;
311 tcc_set_output_type(s, s->output_type);
313 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
314 if (!s->outfile) {
315 s->ppfp = stdout;
316 } else {
317 s->ppfp = fopen(s->outfile, "w");
318 if (!s->ppfp)
319 tcc_error("could not write '%s'", s->outfile);
321 } else if (s->output_type != TCC_OUTPUT_OBJ) {
322 if (s->option_pthread)
323 tcc_set_options(s, "-lpthread");
326 if (s->do_bench)
327 start_time = getclock_ms();
329 /* compile or add each files or library */
330 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
331 struct filespec *f = s->files[i];
332 if (f->type >= AFF_TYPE_LIB) {
333 s->alacarte_link = f->type == AFF_TYPE_LIB;
334 if (tcc_add_library_err(s, f->name) < 0)
335 ret = 1;
336 } else {
337 if (1 == s->verbose)
338 printf("-> %s\n", f->name);
339 s->filetype = f->type;
340 if (tcc_add_file(s, f->name) < 0)
341 ret = 1;
342 if (!first_file)
343 first_file = f->name;
345 s->filetype = AFF_TYPE_NONE;
346 s->alacarte_link = 1;
349 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
350 if (s->outfile)
351 fclose(s->ppfp);
353 } else if (0 == ret) {
354 if (s->output_type == TCC_OUTPUT_MEMORY) {
355 #ifdef TCC_IS_NATIVE
356 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
357 #endif
358 } else {
359 if (!s->outfile)
360 s->outfile = default_outputfile(s, first_file);
361 ret = !!tcc_output_file(s, s->outfile);
362 if (s->gen_deps && !ret)
363 gen_makedeps(s, s->outfile, s->deps_outfile);
367 if (s->do_bench)
368 tcc_print_stats(s, getclock_ms() - start_time);
369 tcc_delete(s);
370 return ret;