fix-mixed-struct (patch by Pip Cet)
[tinycc.git] / tcc.c
Commit [+]AuthorDateLineData
946b600d bellard2001-11-01 14:48:10 +00001/*
2 * TCC - Tiny C Compiler
3 *
c9c05ca5 bellard2004-10-27 21:38:03 +00004 * Copyright (c) 2001-2004 Fabrice Bellard
946b600d bellard2001-11-01 14:48:10 +00005 *
8f5e44a4 bellard2003-05-24 14:11:17 +00006 * 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.
946b600d bellard2001-11-01 14:48:10 +000010 *
8f5e44a4 bellard2003-05-24 14:11:17 +000011 * 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.
946b600d bellard2001-11-01 14:48:10 +000015 *
8f5e44a4 bellard2003-05-24 14:11:17 +000016 * 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
946b600d bellard2001-11-01 14:48:10 +000019 */
5647993e bellard2003-04-13 19:49:30 +000020
5280293d grischka2011-07-14 18:45:37 +020021#ifdef ONE_SOURCE
9dc9cbf3 grischka2009-05-05 20:18:53 +020022#include "libtcc.c"
5280293d grischka2011-07-14 18:45:37 +020023#else
24#include "tcc.h"
b08dfb82 grischka2009-07-18 22:08:01 +020025#endif
d165e873 grischka2009-04-18 13:17:27 +020026
20074d88 seyko2015-03-03 16:08:33 +030027static void print_paths(const char *msg, char **paths, int nb_paths)
28{
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]);
33}
34
35static void display_info(TCCState *s, int what)
36{
37 switch (what) {
38 case 0:
39 printf("tcc version %s ("
40#ifdef TCC_TARGET_I386
41 "i386"
20074d88 seyko2015-03-03 16:08:33 +030042#elif defined TCC_TARGET_X86_64
43 "x86-64"
859ce5e4 seyko2015-03-03 22:30:24 +030044#elif defined TCC_TARGET_C67
45 "C67"
20074d88 seyko2015-03-03 16:08:33 +030046#elif defined TCC_TARGET_ARM
47 "ARM"
48# ifdef TCC_ARM_HARDFLOAT
49 " Hard Float"
50# endif
20074d88 seyko2015-03-03 16:08:33 +030051#elif defined TCC_TARGET_ARM64
52 "AArch64"
53# ifdef TCC_ARM_HARDFLOAT
54 " Hard Float"
55# endif
20074d88 seyko2015-03-03 16:08:33 +030056#endif
859ce5e4 seyko2015-03-03 22:30:24 +030057#ifdef TCC_TARGET_PE
30df3189 grischka2015-05-09 14:29:39 +020058 " Windows"
859ce5e4 seyko2015-03-03 22:30:24 +030059#else
30df3189 grischka2015-05-09 14:29:39 +020060 " Linux"
20074d88 seyko2015-03-03 16:08:33 +030061#endif
62 ")\n", TCC_VERSION);
63 break;
64 case 1:
2e6626a4 seyko2015-03-03 21:41:22 +030065 printf("install: %s\n", s->tcc_lib_path);
20074d88 seyko2015-03-03 16:08:33 +030066 /* print_paths("programs", NULL, 0); */
20074d88 seyko2015-03-03 16:08:33 +030067 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
30df3189 grischka2015-05-09 14:29:39 +020068 print_paths("libraries", s->library_paths, s->nb_library_paths);
69#ifndef TCC_TARGET_PE
70 print_paths("crt", s->crt_paths, s->nb_crt_paths);
20074d88 seyko2015-03-03 16:08:33 +030071 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
30df3189 grischka2015-05-09 14:29:39 +020072#endif
20074d88 seyko2015-03-03 16:08:33 +030073 break;
74 }
75}
76
30df3189 grischka2015-05-09 14:29:39 +020077static void help(void)
8f64c13c bellard2002-05-13 23:00:17 +000078{
30df3189 grischka2015-05-09 14:29:39 +020079 printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
a7fb00e8 grischka2010-09-08 19:13:36 +020080 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
81 " tcc [options...] -run infile [arguments...]\n"
a50e58e5 bellard2002-07-15 22:17:02 +000082 "General options:\n"
83 " -c compile only - generate an object file\n"
b12e91aa bellard2002-07-24 22:10:59 +000084 " -o outfile set output filename\n"
54bf8c05 grischka2007-11-14 17:34:30 +000085 " -run run compiled source\n"
114883e0 bellard2003-10-17 20:43:47 +000086 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
87 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
9c1106d8 bellard2003-09-19 20:35:50 +000088 " -w disable all warnings\n"
82965594 grischka2013-02-10 20:39:05 +010089 " -v show version\n"
90 " -vv show included files (as sole argument: show search paths)\n"
062efe6a Urs Janssen2013-02-14 08:24:51 +010091 " -dumpversion\n"
82965594 grischka2013-02-10 20:39:05 +010092 " -bench show compilation statistics\n"
a50e58e5 bellard2002-07-15 22:17:02 +000093 "Preprocessor options:\n"
94 " -Idir add include path 'dir'\n"
95 " -Dsym[=val] define 'sym' with value 'val'\n"
96 " -Usym undefine 'sym'\n"
30df3189 grischka2015-05-09 14:29:39 +020097 " -E preprocess only\n"
98 " -P[1] no/alternative output of #line directives with -E\n"
80322ada seyko2015-05-13 12:16:00 +030099 " -dD dump defines\n"
a50e58e5 bellard2002-07-15 22:17:02 +0000100 "Linker options:\n"
9051cc21 bellard2002-07-22 00:20:38 +0000101 " -Ldir add library path 'dir'\n"
53834650 bellard2002-08-18 13:22:55 +0000102 " -llib link with dynamic or static library 'lib'\n"
9036c181 Henry Kroll III2010-11-24 16:42:08 -0800103 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
82965594 grischka2013-02-10 20:39:05 +0100104 " -r generate (relocatable) object file\n"
105 " -rdynamic export all global symbols to dynamic linker\n"
7ac43f6d bellard2002-07-27 23:08:04 +0000106 " -shared generate a shared library\n"
9bcc0b97 grischka2008-03-25 21:04:47 +0000107 " -soname set name for shared library to be used at runtime\n"
b12e91aa bellard2002-07-24 22:10:59 +0000108 " -static static linking\n"
05108a3b grischka2013-02-12 19:13:28 +0100109 " -Wl,-opt[=val] set linker option (see manual)\n"
804d0c8d bellard2002-11-03 00:43:55 +0000110 "Debugger options:\n"
111 " -g generate runtime debug info\n"
112#ifdef CONFIG_TCC_BCHECK
113 " -b compile with built-in memory and bounds checker (implies -g)\n"
114#endif
e9e89ad6 grischka2009-04-18 18:21:38 +0200115#ifdef CONFIG_TCC_BACKTRACE
804d0c8d bellard2002-11-03 00:43:55 +0000116 " -bt N show N callers in stack traces\n"
e9e89ad6 grischka2009-04-18 18:21:38 +0200117#endif
0c928da9 Kirill Smelkov2010-06-20 20:08:12 +0400118 "Misc options:\n"
05108a3b grischka2013-02-12 19:13:28 +0100119 " -nostdinc do not use standard system include paths\n"
120 " -nostdlib do not link with standard crt and libraries\n"
121 " -Bdir use 'dir' as tcc internal library and include path\n"
0c928da9
KS
Kirill Smelkov2010-06-20 20:08:12 +0400122 " -MD generate target dependencies for make\n"
123 " -MF depfile put generated dependencies here\n"
8f64c13c bellard2002-05-13 23:00:17 +0000124 );
125}
126
39a07cca grischka2011-08-06 16:08:33 +0200127/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
128#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
bb7bb37f Henry Kroll III2010-12-06 16:17:20 -0800129#ifdef _WIN32
39a07cca grischka2011-08-06 16:08:33 +0200130#include <process.h>
131static int execvp_win32(const char *prog, char **argv)
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800132{
df0267b2 minux2014-04-11 23:49:53 -0400133 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
39a07cca grischka2011-08-06 16:08:33 +0200134 if (-1 == ret)
135 return ret;
136 cwait(&ret, ret, WAIT_CHILD);
137 exit(ret);
bb7bb37f Henry Kroll III2010-12-06 16:17:20 -0800138}
39a07cca grischka2011-08-06 16:08:33 +0200139#define execvp execvp_win32
140#endif
141static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800142{
39a07cca grischka2011-08-06 16:08:33 +0200143 char child_path[4096], *child_name; const char *target;
144 switch (atoi(optarg)) {
145#ifdef TCC_TARGET_I386
146 case 32: break;
147 case 64: target = "x86_64";
148#else
149 case 64: break;
150 case 32: target = "i386";
151#endif
152 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
153 child_name = tcc_basename(child_path);
154 strcpy(child_name, target);
155#ifdef TCC_TARGET_PE
156 strcat(child_name, "-win32");
157#endif
158 strcat(child_name, "-tcc");
159 if (strcmp(argv[0], child_path)) {
160 if (s->verbose > 0)
161 printf("tcc: using '%s'\n", child_name), fflush(stdout);
162 execvp(argv[0] = child_path, argv);
163 }
bf374a5f grischka2011-08-11 17:07:56 +0200164 tcc_error("'%s' not found", child_name);
39a07cca grischka2011-08-06 16:08:33 +0200165 case 0: /* ignore -march etc. */
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800166 break;
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800167 default:
bf374a5f grischka2011-08-11 17:07:56 +0200168 tcc_warning("unsupported option \"-m%s\"", optarg);
b3be007a
HKI
Henry Kroll III2010-12-08 00:27:06 -0800169 }
170}
05108a3b grischka2013-02-12 19:13:28 +0100171#else
172#define exec_other_tcc(s, argv, optarg)
b3be007a
HKI
Henry Kroll III2010-12-08 00:27:06 -0800173#endif
174
05108a3b grischka2013-02-12 19:13:28 +0100175static void gen_makedeps(TCCState *s, const char *target, const char *filename)
74a24d77 grischka2011-08-11 16:55:30 +0200176{
05108a3b grischka2013-02-12 19:13:28 +0100177 FILE *depout;
178 char buf[1024], *ext;
179 int i;
74a24d77 grischka2011-08-11 16:55:30 +0200180
05108a3b grischka2013-02-12 19:13:28 +0100181 if (!filename) {
182 /* compute filename automatically
183 * dir/file.o -> dir/file.d */
184 pstrcpy(buf, sizeof(buf), target);
185 ext = tcc_fileextension(buf);
186 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
187 filename = buf;
188 }
0a50e6c9 Sergei Trofimovich2011-01-04 11:17:52 +0200189
05108a3b grischka2013-02-12 19:13:28 +0100190 if (s->verbose)
191 printf("<- %s\n", filename);
192
193 /* XXX return err codes instead of error() ? */
194 depout = fopen(filename, "w");
195 if (!depout)
196 tcc_error("could not open '%s'", filename);
197
198 fprintf(depout, "%s : \\\n", target);
199 for (i=0; i<s->nb_target_deps; ++i)
200 fprintf(depout, " %s \\\n", s->target_deps[i]);
201 fprintf(depout, "\n");
202 fclose(depout);
203}
4274c44d grischka2012-04-18 18:32:37 +0200204
05108a3b grischka2013-02-12 19:13:28 +0100205static char *default_outputfile(TCCState *s, const char *first_file)
206{
207 char buf[1024];
208 char *ext;
209 const char *name = "a";
210
211 if (first_file && strcmp(first_file, "-"))
212 name = tcc_basename(first_file);
213 pstrcpy(buf, sizeof(buf), name);
214 ext = tcc_fileextension(buf);
215#ifdef TCC_TARGET_PE
216 if (s->output_type == TCC_OUTPUT_DLL)
217 strcpy(ext, ".dll");
218 else
219 if (s->output_type == TCC_OUTPUT_EXE)
220 strcpy(ext, ".exe");
221 else
222#endif
223 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
224 (s->output_type == TCC_OUTPUT_PREPROCESS) )
225 && *ext)
226 strcpy(ext, ".o");
227 else
228 strcpy(buf, "a.out");
229
230 return tcc_strdup(buf);
231}
f2698687 grischka2008-04-27 18:50:35 +0000232
05108a3b grischka2013-02-12 19:13:28 +0100233static int64_t getclock_us(void)
234{
235#ifdef _WIN32
236 struct _timeb tb;
237 _ftime(&tb);
238 return (tb.time * 1000LL + tb.millitm) * 1000LL;
239#else
240 struct timeval tv;
241 gettimeofday(&tv, NULL);
242 return tv.tv_sec * 1000000LL + tv.tv_usec;
243#endif
bef8703f bellard2003-10-04 21:23:51 +0000244}
245
246int main(int argc, char **argv)
247{
bef8703f bellard2003-10-04 21:23:51 +0000248 TCCState *s;
cf92f7aa seyko2015-05-12 22:02:51 +0300249 int ret, optind, i;
bef8703f bellard2003-10-04 21:23:51 +0000250 int64_t start_time = 0;
251
d165e873 grischka2009-04-18 13:17:27 +0200252 s = tcc_new();
94ae3984 grischka2009-07-18 22:07:33 +0200253
05108a3b grischka2013-02-12 19:13:28 +0100254 optind = tcc_parse_args(s, argc - 1, argv + 1);
cf92f7aa seyko2015-05-12 22:02:51 +0300255
256 if (s->do_bench)
257 start_time = getclock_us();
258
0ad857c8 Andrew Aladjev2013-02-19 14:47:36 +0300259 tcc_set_environment(s);
39a07cca grischka2011-08-06 16:08:33 +0200260
05108a3b grischka2013-02-12 19:13:28 +0100261 if (optind == 0) {
30df3189 grischka2015-05-09 14:29:39 +0200262 help();
05108a3b grischka2013-02-12 19:13:28 +0100263 return 1;
8e40dd90 bellard2003-01-06 20:21:08 +0000264 }
39a07cca grischka2011-08-06 16:08:33 +0200265
05108a3b grischka2013-02-12 19:13:28 +0100266 if (s->option_m)
267 exec_other_tcc(s, argv, s->option_m);
268
39a07cca grischka2011-08-06 16:08:33 +0200269 if (s->verbose)
05108a3b grischka2013-02-12 19:13:28 +0100270 display_info(s, 0);
39a07cca grischka2011-08-06 16:08:33 +0200271
05108a3b grischka2013-02-12 19:13:28 +0100272 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
273 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
274 display_info(s, 1);
275 return 0;
f2698687 grischka2008-04-27 18:50:35 +0000276 }
277
05108a3b grischka2013-02-12 19:13:28 +0100278 if (s->verbose && optind == 1)
279 return 0;
6dd6a026 bellard2002-08-30 22:46:35 +0000280
05108a3b grischka2013-02-12 19:13:28 +0100281 if (s->nb_files == 0)
282 tcc_error("no input files\n");
7b54a53e bellard2002-07-18 00:51:11 +0000283
6dd6a026 bellard2002-08-30 22:46:35 +0000284 /* check -c consistency : only single file handled. XXX: checks file type */
05108a3b grischka2013-02-12 19:13:28 +0100285 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
286 if (s->nb_libraries != 0)
287 tcc_error("cannot specify libraries with -c");
6dd6a026 bellard2002-08-30 22:46:35 +0000288 /* accepts only a single input file */
a13f183e seyko2015-04-12 15:39:48 +0300289 if ((s->nb_files != 1) && s->outfile) {
290 tcc_error("cannot specify multiple files with -c and -o");
291 }
6dd6a026 bellard2002-08-30 22:46:35 +0000292 }
06e0753f seyko2015-05-12 21:32:32 +0300293
05108a3b grischka2013-02-12 19:13:28 +0100294 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
295 if (!s->outfile) {
296 s->ppfp = stdout;
200b58da bellard2006-10-16 19:44:00 +0000297 } else {
05108a3b grischka2013-02-12 19:13:28 +0100298 s->ppfp = fopen(s->outfile, "w");
299 if (!s->ppfp)
300 tcc_error("could not write '%s'", s->outfile);
200b58da bellard2006-10-16 19:44:00 +0000301 }
a50e58e5 bellard2002-07-15 22:17:02 +0000302 }
3c9ab729 bellard2001-12-08 15:04:01 +0000303
05108a3b grischka2013-02-12 19:13:28 +0100304 tcc_set_output_type(s, s->output_type);
6dd6a026 bellard2002-08-30 22:46:35 +0000305
306 /* compile or add each files or library */
05108a3b grischka2013-02-12 19:13:28 +0100307 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
05364072 seyko2015-04-12 15:35:37 +0300308 int filetype = *(unsigned char *)s->files[i];
309 const char *filename = s->files[i] + 1;
93de8d80 Kirill Smelkov2010-06-20 19:18:58 +0400310 if (filename[0] == '-' && filename[1] == 'l') {
67aebdd5 grischka2009-05-11 18:46:02 +0200311 if (tcc_add_library(s, filename + 2) < 0) {
b472d536 seyko2015-04-16 07:30:24 +0300312 tcc_error_noabort("cannot find library 'lib%s'", filename+2);
5342b32e grischka2007-12-19 17:36:42 +0000313 ret = 1;
67aebdd5 grischka2009-05-11 18:46:02 +0200314 }
6dd6a026 bellard2002-08-30 22:46:35 +0000315 } else {
f9181416 grischka2009-05-11 18:45:44 +0200316 if (1 == s->verbose)
a327c7a5 grischka2008-03-31 19:49:14 +0000317 printf("-> %s\n", filename);
06e0753f seyko2015-05-12 21:32:32 +0300318 if (!s->outfile)
319 s->outfile = default_outputfile(s, filename);
05364072 seyko2015-04-12 15:35:37 +0300320 if (tcc_add_file(s, filename, filetype) < 0)
5342b32e grischka2007-12-19 17:36:42 +0000321 ret = 1;
a13f183e seyko2015-04-12 15:39:48 +0300322 else
323 if (s->output_type == TCC_OUTPUT_OBJ) {
a98fd130 seyko2015-05-12 10:58:04 +0300324 ret = !!tcc_output_file(s, s->outfile);
06e0753f seyko2015-05-12 21:32:32 +0300325 if (s->gen_deps && !ret)
326 gen_makedeps(s, s->outfile, s->deps_outfile);
a13f183e seyko2015-04-12 15:39:48 +0300327 if (!ret) {
a13f183e seyko2015-04-12 15:39:48 +0300328 if ((i+1) < s->nb_files) {
329 tcc_delete(s);
330 s = tcc_new();
331 tcc_parse_args(s, argc - 1, argv + 1);
332 tcc_set_environment(s);
333 if (s->output_type != TCC_OUTPUT_OBJ)
334 tcc_error("interlnal error");
335 tcc_set_output_type(s, s->output_type);
336 }
337 }
338 }
6dd6a026 bellard2002-08-30 22:46:35 +0000339 }
7b54a53e bellard2002-07-18 00:51:11 +0000340 }
341
0085c648 grischka2009-07-06 21:10:14 +0200342 if (0 == ret) {
e6f3bf7f grischka2011-08-06 16:08:46 +0200343 if (s->output_type == TCC_OUTPUT_MEMORY) {
a35b3059 grischka2012-03-05 20:15:56 +0100344#ifdef TCC_IS_NATIVE
27d38bf2 grischka2012-05-13 10:21:39 +0200345 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
a35b3059 grischka2012-03-05 20:15:56 +0100346#else
347 tcc_error_noabort("-run is not available in a cross compiler");
05108a3b grischka2013-02-12 19:13:28 +0100348 ret = 1;
a35b3059 grischka2012-03-05 20:15:56 +0100349#endif
06e0753f seyko2015-05-12 21:32:32 +0300350 } else
351 if (s->output_type == TCC_OUTPUT_EXE ||
352 s->output_type == TCC_OUTPUT_DLL)
353 {
05108a3b grischka2013-02-12 19:13:28 +0100354 ret = !!tcc_output_file(s, s->outfile);
05108a3b grischka2013-02-12 19:13:28 +0100355 if (s->gen_deps && !ret)
356 gen_makedeps(s, s->outfile, s->deps_outfile);
bdae4a59 Kirill Smelkov2010-06-16 16:54:24 +0400357 }
0085c648 grischka2009-07-06 21:10:14 +0200358 }
6e2d1a80 bellard2002-01-26 18:05:29 +0000359
cf92f7aa seyko2015-05-12 22:02:51 +0300360 if (s->do_bench)
361 tcc_print_stats(s, getclock_us() - start_time);
362
0085c648 grischka2009-07-06 21:10:14 +0200363 tcc_delete(s);
c62792e6 bellard2002-11-02 14:12:32 +0000364 return ret;
27f6e16b bellard2001-10-27 23:48:39 +0000365}