VLA code: minor fix
[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
58 ", mingw"
59#else
60 #ifdef __linux
61 ", Linux"
62 #else
63 ", Unknown"
64 #endif
20074d88 seyko2015-03-03 16:08:33 +030065#endif
66 ")\n", TCC_VERSION);
67 break;
68 case 1:
2e6626a4 seyko2015-03-03 21:41:22 +030069 printf("install: %s\n", s->tcc_lib_path);
20074d88 seyko2015-03-03 16:08:33 +030070 /* print_paths("programs", NULL, 0); */
71 print_paths("crt", s->crt_paths, s->nb_crt_paths);
72 print_paths("libraries", s->library_paths, s->nb_library_paths);
73 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
74 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
75 break;
76 }
77}
78
79static void help(TCCState *s)
8f64c13c bellard2002-05-13 23:00:17 +000080{
20074d88 seyko2015-03-03 16:08:33 +030081 display_info(s, 0);
82 printf("Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
a7fb00e8 grischka2010-09-08 19:13:36 +020083 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
84 " tcc [options...] -run infile [arguments...]\n"
a50e58e5 bellard2002-07-15 22:17:02 +000085 "General options:\n"
86 " -c compile only - generate an object file\n"
b12e91aa bellard2002-07-24 22:10:59 +000087 " -o outfile set output filename\n"
54bf8c05 grischka2007-11-14 17:34:30 +000088 " -run run compiled source\n"
114883e0 bellard2003-10-17 20:43:47 +000089 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
90 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
9c1106d8 bellard2003-09-19 20:35:50 +000091 " -w disable all warnings\n"
82965594 grischka2013-02-10 20:39:05 +010092 " -v show version\n"
93 " -vv show included files (as sole argument: show search paths)\n"
062efe6a Urs Janssen2013-02-14 08:24:51 +010094 " -dumpversion\n"
82965594 grischka2013-02-10 20:39:05 +010095 " -bench show compilation statistics\n"
a50e58e5 bellard2002-07-15 22:17:02 +000096 "Preprocessor options:\n"
200b58da bellard2006-10-16 19:44:00 +000097 " -E preprocess only\n"
a50e58e5 bellard2002-07-15 22:17:02 +000098 " -Idir add include path 'dir'\n"
99 " -Dsym[=val] define 'sym' with value 'val'\n"
100 " -Usym undefine 'sym'\n"
50cdccf3 seyko2015-03-03 14:19:14 +0300101 " -P do not output a #line directive\n"
102 " -P1 use a #line directive in output instead of the gcc style\n"
b7b9f9f5 seyko2015-03-03 14:25:57 +0300103 " -dD put a define directive in the output (inside a comment)\n"
a50e58e5 bellard2002-07-15 22:17:02 +0000104 "Linker options:\n"
9051cc21 bellard2002-07-22 00:20:38 +0000105 " -Ldir add library path 'dir'\n"
53834650 bellard2002-08-18 13:22:55 +0000106 " -llib link with dynamic or static library 'lib'\n"
9036c181 Henry Kroll III2010-11-24 16:42:08 -0800107 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
82965594 grischka2013-02-10 20:39:05 +0100108 " -r generate (relocatable) object file\n"
109 " -rdynamic export all global symbols to dynamic linker\n"
7ac43f6d bellard2002-07-27 23:08:04 +0000110 " -shared generate a shared library\n"
9bcc0b97 grischka2008-03-25 21:04:47 +0000111 " -soname set name for shared library to be used at runtime\n"
b12e91aa bellard2002-07-24 22:10:59 +0000112 " -static static linking\n"
05108a3b grischka2013-02-12 19:13:28 +0100113 " -Wl,-opt[=val] set linker option (see manual)\n"
804d0c8d bellard2002-11-03 00:43:55 +0000114 "Debugger options:\n"
115 " -g generate runtime debug info\n"
116#ifdef CONFIG_TCC_BCHECK
117 " -b compile with built-in memory and bounds checker (implies -g)\n"
118#endif
e9e89ad6 grischka2009-04-18 18:21:38 +0200119#ifdef CONFIG_TCC_BACKTRACE
804d0c8d bellard2002-11-03 00:43:55 +0000120 " -bt N show N callers in stack traces\n"
e9e89ad6 grischka2009-04-18 18:21:38 +0200121#endif
0c928da9 Kirill Smelkov2010-06-20 20:08:12 +0400122 "Misc options:\n"
05108a3b grischka2013-02-12 19:13:28 +0100123 " -nostdinc do not use standard system include paths\n"
124 " -nostdlib do not link with standard crt and libraries\n"
125 " -Bdir use 'dir' as tcc internal library and include path\n"
0c928da9
KS
Kirill Smelkov2010-06-20 20:08:12 +0400126 " -MD generate target dependencies for make\n"
127 " -MF depfile put generated dependencies here\n"
8f64c13c bellard2002-05-13 23:00:17 +0000128 );
129}
130
39a07cca grischka2011-08-06 16:08:33 +0200131/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
132#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
bb7bb37f Henry Kroll III2010-12-06 16:17:20 -0800133#ifdef _WIN32
39a07cca grischka2011-08-06 16:08:33 +0200134#include <process.h>
135static int execvp_win32(const char *prog, char **argv)
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800136{
df0267b2 minux2014-04-11 23:49:53 -0400137 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
39a07cca grischka2011-08-06 16:08:33 +0200138 if (-1 == ret)
139 return ret;
140 cwait(&ret, ret, WAIT_CHILD);
141 exit(ret);
bb7bb37f Henry Kroll III2010-12-06 16:17:20 -0800142}
39a07cca grischka2011-08-06 16:08:33 +0200143#define execvp execvp_win32
144#endif
145static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800146{
39a07cca grischka2011-08-06 16:08:33 +0200147 char child_path[4096], *child_name; const char *target;
148 switch (atoi(optarg)) {
149#ifdef TCC_TARGET_I386
150 case 32: break;
151 case 64: target = "x86_64";
152#else
153 case 64: break;
154 case 32: target = "i386";
155#endif
156 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
157 child_name = tcc_basename(child_path);
158 strcpy(child_name, target);
159#ifdef TCC_TARGET_PE
160 strcat(child_name, "-win32");
161#endif
162 strcat(child_name, "-tcc");
163 if (strcmp(argv[0], child_path)) {
164 if (s->verbose > 0)
165 printf("tcc: using '%s'\n", child_name), fflush(stdout);
166 execvp(argv[0] = child_path, argv);
167 }
bf374a5f grischka2011-08-11 17:07:56 +0200168 tcc_error("'%s' not found", child_name);
39a07cca grischka2011-08-06 16:08:33 +0200169 case 0: /* ignore -march etc. */
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800170 break;
b3be007a Henry Kroll III2010-12-08 00:27:06 -0800171 default:
bf374a5f grischka2011-08-11 17:07:56 +0200172 tcc_warning("unsupported option \"-m%s\"", optarg);
b3be007a
HKI
Henry Kroll III2010-12-08 00:27:06 -0800173 }
174}
05108a3b grischka2013-02-12 19:13:28 +0100175#else
176#define exec_other_tcc(s, argv, optarg)
b3be007a
HKI
Henry Kroll III2010-12-08 00:27:06 -0800177#endif
178
05108a3b grischka2013-02-12 19:13:28 +0100179static void gen_makedeps(TCCState *s, const char *target, const char *filename)
74a24d77 grischka2011-08-11 16:55:30 +0200180{
05108a3b grischka2013-02-12 19:13:28 +0100181 FILE *depout;
182 char buf[1024], *ext;
183 int i;
74a24d77 grischka2011-08-11 16:55:30 +0200184
05108a3b grischka2013-02-12 19:13:28 +0100185 if (!filename) {
186 /* compute filename automatically
187 * dir/file.o -> dir/file.d */
188 pstrcpy(buf, sizeof(buf), target);
189 ext = tcc_fileextension(buf);
190 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
191 filename = buf;
192 }
0a50e6c9 Sergei Trofimovich2011-01-04 11:17:52 +0200193
05108a3b grischka2013-02-12 19:13:28 +0100194 if (s->verbose)
195 printf("<- %s\n", filename);
196
197 /* XXX return err codes instead of error() ? */
198 depout = fopen(filename, "w");
199 if (!depout)
200 tcc_error("could not open '%s'", filename);
201
202 fprintf(depout, "%s : \\\n", target);
203 for (i=0; i<s->nb_target_deps; ++i)
204 fprintf(depout, " %s \\\n", s->target_deps[i]);
205 fprintf(depout, "\n");
206 fclose(depout);
207}
4274c44d grischka2012-04-18 18:32:37 +0200208
05108a3b grischka2013-02-12 19:13:28 +0100209static char *default_outputfile(TCCState *s, const char *first_file)
210{
211 char buf[1024];
212 char *ext;
213 const char *name = "a";
214
215 if (first_file && strcmp(first_file, "-"))
216 name = tcc_basename(first_file);
217 pstrcpy(buf, sizeof(buf), name);
218 ext = tcc_fileextension(buf);
219#ifdef TCC_TARGET_PE
220 if (s->output_type == TCC_OUTPUT_DLL)
221 strcpy(ext, ".dll");
222 else
223 if (s->output_type == TCC_OUTPUT_EXE)
224 strcpy(ext, ".exe");
225 else
226#endif
227 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
228 (s->output_type == TCC_OUTPUT_PREPROCESS) )
229 && *ext)
230 strcpy(ext, ".o");
231 else
232 strcpy(buf, "a.out");
233
234 return tcc_strdup(buf);
235}
f2698687 grischka2008-04-27 18:50:35 +0000236
05108a3b grischka2013-02-12 19:13:28 +0100237static int64_t getclock_us(void)
238{
239#ifdef _WIN32
240 struct _timeb tb;
241 _ftime(&tb);
242 return (tb.time * 1000LL + tb.millitm) * 1000LL;
243#else
244 struct timeval tv;
245 gettimeofday(&tv, NULL);
246 return tv.tv_sec * 1000000LL + tv.tv_usec;
247#endif
bef8703f bellard2003-10-04 21:23:51 +0000248}
249
250int main(int argc, char **argv)
251{
bef8703f bellard2003-10-04 21:23:51 +0000252 TCCState *s;
05108a3b grischka2013-02-12 19:13:28 +0100253 int ret, optind, i, bench;
bef8703f bellard2003-10-04 21:23:51 +0000254 int64_t start_time = 0;
05108a3b grischka2013-02-12 19:13:28 +0100255 const char *first_file = NULL;
bef8703f bellard2003-10-04 21:23:51 +0000256
d165e873 grischka2009-04-18 13:17:27 +0200257 s = tcc_new();
94ae3984 grischka2009-07-18 22:07:33 +0200258
05108a3b grischka2013-02-12 19:13:28 +0100259 optind = tcc_parse_args(s, argc - 1, argv + 1);
0ad857c8 Andrew Aladjev2013-02-19 14:47:36 +0300260 tcc_set_environment(s);
39a07cca grischka2011-08-06 16:08:33 +0200261
05108a3b grischka2013-02-12 19:13:28 +0100262 if (optind == 0) {
20074d88 seyko2015-03-03 16:08:33 +0300263 help(s);
05108a3b grischka2013-02-12 19:13:28 +0100264 return 1;
8e40dd90 bellard2003-01-06 20:21:08 +0000265 }
39a07cca grischka2011-08-06 16:08:33 +0200266
05108a3b grischka2013-02-12 19:13:28 +0100267 if (s->option_m)
268 exec_other_tcc(s, argv, s->option_m);
269
39a07cca grischka2011-08-06 16:08:33 +0200270 if (s->verbose)
05108a3b grischka2013-02-12 19:13:28 +0100271 display_info(s, 0);
39a07cca grischka2011-08-06 16:08:33 +0200272
05108a3b grischka2013-02-12 19:13:28 +0100273 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
274 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
275 display_info(s, 1);
276 return 0;
f2698687 grischka2008-04-27 18:50:35 +0000277 }
278
05108a3b grischka2013-02-12 19:13:28 +0100279 if (s->verbose && optind == 1)
280 return 0;
6dd6a026 bellard2002-08-30 22:46:35 +0000281
05108a3b grischka2013-02-12 19:13:28 +0100282 if (s->nb_files == 0)
283 tcc_error("no input files\n");
7b54a53e bellard2002-07-18 00:51:11 +0000284
6dd6a026 bellard2002-08-30 22:46:35 +0000285 /* check -c consistency : only single file handled. XXX: checks file type */
05108a3b grischka2013-02-12 19:13:28 +0100286 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
287 if (s->nb_libraries != 0)
288 tcc_error("cannot specify libraries with -c");
6dd6a026 bellard2002-08-30 22:46:35 +0000289 /* accepts only a single input file */
a13f183e seyko2015-04-12 15:39:48 +0300290 if ((s->nb_files != 1) && s->outfile) {
291 tcc_error("cannot specify multiple files with -c and -o");
292 }
6dd6a026 bellard2002-08-30 22:46:35 +0000293 }
294
05108a3b grischka2013-02-12 19:13:28 +0100295 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
296 if (!s->outfile) {
297 s->ppfp = stdout;
200b58da bellard2006-10-16 19:44:00 +0000298 } else {
05108a3b grischka2013-02-12 19:13:28 +0100299 s->ppfp = fopen(s->outfile, "w");
300 if (!s->ppfp)
301 tcc_error("could not write '%s'", s->outfile);
200b58da bellard2006-10-16 19:44:00 +0000302 }
a50e58e5 bellard2002-07-15 22:17:02 +0000303 }
3c9ab729 bellard2001-12-08 15:04:01 +0000304
05108a3b grischka2013-02-12 19:13:28 +0100305 bench = s->do_bench;
306 if (bench)
9620fd18 bellard2002-11-18 21:46:44 +0000307 start_time = getclock_us();
9620fd18 bellard2002-11-18 21:46:44 +0000308
05108a3b grischka2013-02-12 19:13:28 +0100309 tcc_set_output_type(s, s->output_type);
6dd6a026 bellard2002-08-30 22:46:35 +0000310
311 /* compile or add each files or library */
05108a3b grischka2013-02-12 19:13:28 +0100312 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
05364072 seyko2015-04-12 15:35:37 +0300313 int filetype = *(unsigned char *)s->files[i];
314 const char *filename = s->files[i] + 1;
93de8d80 Kirill Smelkov2010-06-20 19:18:58 +0400315 if (filename[0] == '-' && filename[1] == 'l') {
67aebdd5 grischka2009-05-11 18:46:02 +0200316 if (tcc_add_library(s, filename + 2) < 0) {
b472d536 seyko2015-04-16 07:30:24 +0300317 tcc_error_noabort("cannot find library 'lib%s'", filename+2);
5342b32e grischka2007-12-19 17:36:42 +0000318 ret = 1;
67aebdd5 grischka2009-05-11 18:46:02 +0200319 }
6dd6a026 bellard2002-08-30 22:46:35 +0000320 } else {
f9181416 grischka2009-05-11 18:45:44 +0200321 if (1 == s->verbose)
a327c7a5 grischka2008-03-31 19:49:14 +0000322 printf("-> %s\n", filename);
05364072 seyko2015-04-12 15:35:37 +0300323 if (tcc_add_file(s, filename, filetype) < 0)
5342b32e grischka2007-12-19 17:36:42 +0000324 ret = 1;
a13f183e seyko2015-04-12 15:39:48 +0300325 else
326 if (s->output_type == TCC_OUTPUT_OBJ) {
327 const char *outfile = s->outfile;
328 if (!outfile)
329 outfile = default_outputfile(s, filename);
330 ret = !!tcc_output_file(s, outfile);
331 if (!ret) {
332 /* dump collected dependencies */
333 if (s->gen_deps)
334 gen_makedeps(s, outfile, s->deps_outfile);
335 if ((i+1) < s->nb_files) {
336 tcc_delete(s);
337 s = tcc_new();
338 tcc_parse_args(s, argc - 1, argv + 1);
339 tcc_set_environment(s);
340 if (s->output_type != TCC_OUTPUT_OBJ)
341 tcc_error("interlnal error");
342 tcc_set_output_type(s, s->output_type);
343 }
344 }
345 }
346 else
05108a3b grischka2013-02-12 19:13:28 +0100347 if (!first_file)
348 first_file = filename;
6dd6a026 bellard2002-08-30 22:46:35 +0000349 }
7b54a53e bellard2002-07-18 00:51:11 +0000350 }
351
0085c648 grischka2009-07-06 21:10:14 +0200352 if (0 == ret) {
05108a3b grischka2013-02-12 19:13:28 +0100353 if (bench)
0085c648 grischka2009-07-06 21:10:14 +0200354 tcc_print_stats(s, getclock_us() - start_time);
355
e6f3bf7f grischka2011-08-06 16:08:46 +0200356 if (s->output_type == TCC_OUTPUT_MEMORY) {
a35b3059 grischka2012-03-05 20:15:56 +0100357#ifdef TCC_IS_NATIVE
27d38bf2 grischka2012-05-13 10:21:39 +0200358 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
a35b3059 grischka2012-03-05 20:15:56 +0100359#else
360 tcc_error_noabort("-run is not available in a cross compiler");
05108a3b grischka2013-02-12 19:13:28 +0100361 ret = 1;
a35b3059 grischka2012-03-05 20:15:56 +0100362#endif
e6f3bf7f grischka2011-08-06 16:08:46 +0200363 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
364 if (s->outfile)
05108a3b grischka2013-02-12 19:13:28 +0100365 fclose(s->ppfp);
a13f183e seyko2015-04-12 15:39:48 +0300366 } else if (s->output_type != TCC_OUTPUT_OBJ) {
05108a3b grischka2013-02-12 19:13:28 +0100367 if (!s->outfile)
368 s->outfile = default_outputfile(s, first_file);
369 ret = !!tcc_output_file(s, s->outfile);
0c928da9 Kirill Smelkov2010-06-20 20:08:12 +0400370 /* dump collected dependencies */
05108a3b grischka2013-02-12 19:13:28 +0100371 if (s->gen_deps && !ret)
372 gen_makedeps(s, s->outfile, s->deps_outfile);
bdae4a59 Kirill Smelkov2010-06-16 16:54:24 +0400373 }
0085c648 grischka2009-07-06 21:10:14 +0200374 }
6e2d1a80 bellard2002-01-26 18:05:29 +0000375
0085c648 grischka2009-07-06 21:10:14 +0200376 tcc_delete(s);
05108a3b grischka2013-02-12 19:13:28 +0100377 if (bench)
378 tcc_memstats();
c62792e6 bellard2002-11-02 14:12:32 +0000379 return ret;
27f6e16b bellard2001-10-27 23:48:39 +0000380}