A partial reverse for commit eda2c756edc4dca004ba217d5bf361235dd9de1f
[tinycc.git] / tcc.c
blobce03d3b3cbd235fd783682e9f705570e6d2d86a6
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 help(void)
29 printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
30 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
31 " tcc [options...] -run infile [arguments...]\n"
32 "General options:\n"
33 " -c compile only - generate an object file\n"
34 " -o outfile set output filename\n"
35 " -run run compiled source\n"
36 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
37 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
38 " -w disable all warnings\n"
39 " -v show version\n"
40 " -vv show included files (as sole argument: show search paths)\n"
41 " -dumpversion\n"
42 " -bench show compilation statistics\n"
43 "Preprocessor options:\n"
44 " -E preprocess only\n"
45 " -Idir add include path 'dir'\n"
46 " -Dsym[=val] define 'sym' with value 'val'\n"
47 " -Usym undefine 'sym'\n"
48 " -P do not output a #line directive\n"
49 " -P1 use a #line directive in output instead of the gcc style\n"
50 " -dD put a define directive in the output (inside a comment)\n"
51 "Linker options:\n"
52 " -Ldir add library path 'dir'\n"
53 " -llib link with dynamic or static library 'lib'\n"
54 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
55 " -r generate (relocatable) object file\n"
56 " -rdynamic export all global symbols to dynamic linker\n"
57 " -shared generate a shared library\n"
58 " -soname set name for shared library to be used at runtime\n"
59 " -static static linking\n"
60 " -Wl,-opt[=val] set linker option (see manual)\n"
61 "Debugger options:\n"
62 " -g generate runtime debug info\n"
63 #ifdef CONFIG_TCC_BCHECK
64 " -b compile with built-in memory and bounds checker (implies -g)\n"
65 #endif
66 #ifdef CONFIG_TCC_BACKTRACE
67 " -bt N show N callers in stack traces\n"
68 #endif
69 "Misc options:\n"
70 " -nostdinc do not use standard system include paths\n"
71 " -nostdlib do not link with standard crt and libraries\n"
72 " -Bdir use 'dir' as tcc internal library and include path\n"
73 " -MD generate target dependencies for make\n"
74 " -MF depfile put generated dependencies here\n"
78 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
79 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
80 #ifdef _WIN32
81 #include <process.h>
82 static int execvp_win32(const char *prog, char **argv)
84 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
85 if (-1 == ret)
86 return ret;
87 cwait(&ret, ret, WAIT_CHILD);
88 exit(ret);
90 #define execvp execvp_win32
91 #endif
92 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
94 char child_path[4096], *child_name; const char *target;
95 switch (atoi(optarg)) {
96 #ifdef TCC_TARGET_I386
97 case 32: break;
98 case 64: target = "x86_64";
99 #else
100 case 64: break;
101 case 32: target = "i386";
102 #endif
103 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
104 child_name = tcc_basename(child_path);
105 strcpy(child_name, target);
106 #ifdef TCC_TARGET_PE
107 strcat(child_name, "-win32");
108 #endif
109 strcat(child_name, "-tcc");
110 if (strcmp(argv[0], child_path)) {
111 if (s->verbose > 0)
112 printf("tcc: using '%s'\n", child_name), fflush(stdout);
113 execvp(argv[0] = child_path, argv);
115 tcc_error("'%s' not found", child_name);
116 case 0: /* ignore -march etc. */
117 break;
118 default:
119 tcc_warning("unsupported option \"-m%s\"", optarg);
122 #else
123 #define exec_other_tcc(s, argv, optarg)
124 #endif
126 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
128 FILE *depout;
129 char buf[1024], *ext;
130 int i;
132 if (!filename) {
133 /* compute filename automatically
134 * dir/file.o -> dir/file.d */
135 pstrcpy(buf, sizeof(buf), target);
136 ext = tcc_fileextension(buf);
137 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
138 filename = buf;
141 if (s->verbose)
142 printf("<- %s\n", filename);
144 /* XXX return err codes instead of error() ? */
145 depout = fopen(filename, "w");
146 if (!depout)
147 tcc_error("could not open '%s'", filename);
149 fprintf(depout, "%s : \\\n", target);
150 for (i=0; i<s->nb_target_deps; ++i)
151 fprintf(depout, " %s \\\n", s->target_deps[i]);
152 fprintf(depout, "\n");
153 fclose(depout);
156 static char *default_outputfile(TCCState *s, const char *first_file)
158 char buf[1024];
159 char *ext;
160 const char *name = "a";
162 if (first_file && strcmp(first_file, "-"))
163 name = tcc_basename(first_file);
164 pstrcpy(buf, sizeof(buf), name);
165 ext = tcc_fileextension(buf);
166 #ifdef TCC_TARGET_PE
167 if (s->output_type == TCC_OUTPUT_DLL)
168 strcpy(ext, ".dll");
169 else
170 if (s->output_type == TCC_OUTPUT_EXE)
171 strcpy(ext, ".exe");
172 else
173 #endif
174 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
175 (s->output_type == TCC_OUTPUT_PREPROCESS) )
176 && *ext)
177 strcpy(ext, ".o");
178 else
179 strcpy(buf, "a.out");
181 return tcc_strdup(buf);
184 static void print_paths(const char *msg, char **paths, int nb_paths)
186 int i;
187 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
188 for(i = 0; i < nb_paths; i++)
189 printf(" %s\n", paths[i]);
192 static void display_info(TCCState *s, int what)
194 switch (what) {
195 case 0:
196 printf("tcc version %s ("
197 #ifdef TCC_TARGET_I386
198 "i386"
199 # ifdef TCC_TARGET_PE
200 " Win32"
201 # endif
202 #elif defined TCC_TARGET_X86_64
203 "x86-64"
204 # ifdef TCC_TARGET_PE
205 " Win64"
206 # endif
207 #elif defined TCC_TARGET_ARM
208 "ARM"
209 #elif defined TCC_TARGET_ARM64
210 "AArch64"
211 # ifdef TCC_ARM_HARDFLOAT
212 " Hard Float"
213 # endif
214 # ifdef TCC_TARGET_PE
215 " WinCE"
216 # endif
217 #endif
218 #ifndef TCC_TARGET_PE
219 # ifdef __linux
220 " Linux"
221 # endif
222 #endif
223 ")\n", TCC_VERSION);
224 break;
225 case 1:
226 printf("install: %s/\n", s->tcc_lib_path);
227 /* print_paths("programs", NULL, 0); */
228 print_paths("crt", s->crt_paths, s->nb_crt_paths);
229 print_paths("libraries", s->library_paths, s->nb_library_paths);
230 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
231 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
232 break;
236 static int64_t getclock_us(void)
238 #ifdef _WIN32
239 struct _timeb tb;
240 _ftime(&tb);
241 return (tb.time * 1000LL + tb.millitm) * 1000LL;
242 #else
243 struct timeval tv;
244 gettimeofday(&tv, NULL);
245 return tv.tv_sec * 1000000LL + tv.tv_usec;
246 #endif
249 int main(int argc, char **argv)
251 TCCState *s;
252 int ret, optind, i, bench;
253 int64_t start_time = 0;
254 const char *first_file = NULL;
256 s = tcc_new();
258 optind = tcc_parse_args(s, argc - 1, argv + 1);
259 tcc_set_environment(s);
261 if (optind == 0) {
262 help();
263 return 1;
266 if (s->output_type == 0)
267 s->output_type = TCC_OUTPUT_EXE;
269 if (s->option_m)
270 exec_other_tcc(s, argv, s->option_m);
272 if (s->verbose)
273 display_info(s, 0);
275 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
276 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
277 display_info(s, 1);
278 return 0;
281 if (s->verbose && optind == 1)
282 return 0;
284 if (s->nb_files == 0)
285 tcc_error("no input files\n");
287 /* check -c consistency : only single file handled. XXX: checks file type */
288 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
289 if (s->nb_libraries != 0)
290 tcc_error("cannot specify libraries with -c");
291 /* accepts only a single input file */
292 if (s->nb_files != 1)
293 tcc_error("cannot specify multiple files with -c");
296 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
297 if (!s->outfile) {
298 s->ppfp = stdout;
299 } else {
300 s->ppfp = fopen(s->outfile, "w");
301 if (!s->ppfp)
302 tcc_error("could not write '%s'", s->outfile);
306 bench = s->do_bench;
307 if (bench)
308 start_time = getclock_us();
310 tcc_set_output_type(s, s->output_type);
311 if (s->output_type == TCC_OUTPUT_PREPROCESS)
312 print_defines();
314 /* compile or add each files or library */
315 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
316 const char *filename;
318 filename = s->files[i];
319 if (filename[0] == '-' && filename[1] == 'l') {
320 if (tcc_add_library(s, filename + 2) < 0) {
321 tcc_error_noabort("cannot find '%s'", filename);
322 ret = 1;
324 } else {
325 if (1 == s->verbose)
326 printf("-> %s\n", filename);
327 if (tcc_add_file(s, filename) < 0)
328 ret = 1;
329 if (!first_file)
330 first_file = filename;
334 if (0 == ret) {
335 if (bench)
336 tcc_print_stats(s, getclock_us() - start_time);
338 if (s->output_type == TCC_OUTPUT_MEMORY) {
339 #ifdef TCC_IS_NATIVE
340 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
341 #else
342 tcc_error_noabort("-run is not available in a cross compiler");
343 ret = 1;
344 #endif
345 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
346 if (s->outfile)
347 fclose(s->ppfp);
348 } else {
349 if (!s->outfile)
350 s->outfile = default_outputfile(s, first_file);
351 ret = !!tcc_output_file(s, s->outfile);
352 /* dump collected dependencies */
353 if (s->gen_deps && !ret)
354 gen_makedeps(s, s->outfile, s->deps_outfile);
358 tcc_delete(s);
359 if (bench)
360 tcc_memstats();
361 return ret;