Added a gcc preprocessor options -P, -P1
[tinycc.git] / tcc.c
blob7b191147b593106665581ed892dabb205ba444cf
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 "Linker options:\n"
51 " -Ldir add library path 'dir'\n"
52 " -llib link with dynamic or static library 'lib'\n"
53 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
54 " -r generate (relocatable) object file\n"
55 " -rdynamic export all global symbols to dynamic linker\n"
56 " -shared generate a shared library\n"
57 " -soname set name for shared library to be used at runtime\n"
58 " -static static linking\n"
59 " -Wl,-opt[=val] set linker option (see manual)\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 show N callers in stack traces\n"
67 #endif
68 "Misc options:\n"
69 " -nostdinc do not use standard system include paths\n"
70 " -nostdlib do not link with standard crt and libraries\n"
71 " -Bdir use 'dir' as tcc internal library and include path\n"
72 " -MD generate target dependencies for make\n"
73 " -MF depfile put generated dependencies here\n"
77 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
78 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
79 #ifdef _WIN32
80 #include <process.h>
81 static int execvp_win32(const char *prog, char **argv)
83 int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
84 if (-1 == ret)
85 return ret;
86 cwait(&ret, ret, WAIT_CHILD);
87 exit(ret);
89 #define execvp execvp_win32
90 #endif
91 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
93 char child_path[4096], *child_name; const char *target;
94 switch (atoi(optarg)) {
95 #ifdef TCC_TARGET_I386
96 case 32: break;
97 case 64: target = "x86_64";
98 #else
99 case 64: break;
100 case 32: target = "i386";
101 #endif
102 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
103 child_name = tcc_basename(child_path);
104 strcpy(child_name, target);
105 #ifdef TCC_TARGET_PE
106 strcat(child_name, "-win32");
107 #endif
108 strcat(child_name, "-tcc");
109 if (strcmp(argv[0], child_path)) {
110 if (s->verbose > 0)
111 printf("tcc: using '%s'\n", child_name), fflush(stdout);
112 execvp(argv[0] = child_path, argv);
114 tcc_error("'%s' not found", child_name);
115 case 0: /* ignore -march etc. */
116 break;
117 default:
118 tcc_warning("unsupported option \"-m%s\"", optarg);
121 #else
122 #define exec_other_tcc(s, argv, optarg)
123 #endif
125 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
127 FILE *depout;
128 char buf[1024], *ext;
129 int i;
131 if (!filename) {
132 /* compute filename automatically
133 * dir/file.o -> dir/file.d */
134 pstrcpy(buf, sizeof(buf), target);
135 ext = tcc_fileextension(buf);
136 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
137 filename = buf;
140 if (s->verbose)
141 printf("<- %s\n", filename);
143 /* XXX return err codes instead of error() ? */
144 depout = fopen(filename, "w");
145 if (!depout)
146 tcc_error("could not open '%s'", filename);
148 fprintf(depout, "%s : \\\n", target);
149 for (i=0; i<s->nb_target_deps; ++i)
150 fprintf(depout, " %s \\\n", s->target_deps[i]);
151 fprintf(depout, "\n");
152 fclose(depout);
155 static char *default_outputfile(TCCState *s, const char *first_file)
157 char buf[1024];
158 char *ext;
159 const char *name = "a";
161 if (first_file && strcmp(first_file, "-"))
162 name = tcc_basename(first_file);
163 pstrcpy(buf, sizeof(buf), name);
164 ext = tcc_fileextension(buf);
165 #ifdef TCC_TARGET_PE
166 if (s->output_type == TCC_OUTPUT_DLL)
167 strcpy(ext, ".dll");
168 else
169 if (s->output_type == TCC_OUTPUT_EXE)
170 strcpy(ext, ".exe");
171 else
172 #endif
173 if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
174 (s->output_type == TCC_OUTPUT_PREPROCESS) )
175 && *ext)
176 strcpy(ext, ".o");
177 else
178 strcpy(buf, "a.out");
180 return tcc_strdup(buf);
183 static void print_paths(const char *msg, char **paths, int nb_paths)
185 int i;
186 printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
187 for(i = 0; i < nb_paths; i++)
188 printf(" %s\n", paths[i]);
191 static void display_info(TCCState *s, int what)
193 switch (what) {
194 case 0:
195 printf("tcc version %s ("
196 #ifdef TCC_TARGET_I386
197 "i386"
198 # ifdef TCC_TARGET_PE
199 " Win32"
200 # endif
201 #elif defined TCC_TARGET_X86_64
202 "x86-64"
203 # ifdef TCC_TARGET_PE
204 " Win64"
205 # endif
206 #elif defined TCC_TARGET_ARM
207 "ARM"
208 #elif defined TCC_TARGET_ARM64
209 "AArch64"
210 # ifdef TCC_ARM_HARDFLOAT
211 " Hard Float"
212 # endif
213 # ifdef TCC_TARGET_PE
214 " WinCE"
215 # endif
216 #endif
217 #ifndef TCC_TARGET_PE
218 # ifdef __linux
219 " Linux"
220 # endif
221 #endif
222 ")\n", TCC_VERSION);
223 break;
224 case 1:
225 printf("install: %s/\n", s->tcc_lib_path);
226 /* print_paths("programs", NULL, 0); */
227 print_paths("crt", s->crt_paths, s->nb_crt_paths);
228 print_paths("libraries", s->library_paths, s->nb_library_paths);
229 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
230 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
231 break;
235 static int64_t getclock_us(void)
237 #ifdef _WIN32
238 struct _timeb tb;
239 _ftime(&tb);
240 return (tb.time * 1000LL + tb.millitm) * 1000LL;
241 #else
242 struct timeval tv;
243 gettimeofday(&tv, NULL);
244 return tv.tv_sec * 1000000LL + tv.tv_usec;
245 #endif
248 int main(int argc, char **argv)
250 TCCState *s;
251 int ret, optind, i, bench;
252 int64_t start_time = 0;
253 const char *first_file = NULL;
255 s = tcc_new();
257 optind = tcc_parse_args(s, argc - 1, argv + 1);
258 tcc_set_environment(s);
260 if (optind == 0) {
261 help();
262 return 1;
265 if (s->output_type == 0)
266 s->output_type = TCC_OUTPUT_EXE;
268 if (s->option_m)
269 exec_other_tcc(s, argv, s->option_m);
271 if (s->verbose)
272 display_info(s, 0);
274 if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
275 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
276 display_info(s, 1);
277 return 0;
280 if (s->verbose && optind == 1)
281 return 0;
283 if (s->nb_files == 0)
284 tcc_error("no input files\n");
286 /* check -c consistency : only single file handled. XXX: checks file type */
287 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
288 if (s->nb_libraries != 0)
289 tcc_error("cannot specify libraries with -c");
290 /* accepts only a single input file */
291 if (s->nb_files != 1)
292 tcc_error("cannot specify multiple files with -c");
295 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
296 if (!s->outfile) {
297 s->ppfp = stdout;
298 } else {
299 s->ppfp = fopen(s->outfile, "w");
300 if (!s->ppfp)
301 tcc_error("could not write '%s'", s->outfile);
305 bench = s->do_bench;
306 if (bench)
307 start_time = getclock_us();
309 tcc_set_output_type(s, s->output_type);
311 /* compile or add each files or library */
312 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
313 const char *filename;
315 filename = s->files[i];
316 if (filename[0] == '-' && filename[1] == 'l') {
317 if (tcc_add_library(s, filename + 2) < 0) {
318 tcc_error_noabort("cannot find '%s'", filename);
319 ret = 1;
321 } else {
322 if (1 == s->verbose)
323 printf("-> %s\n", filename);
324 if (tcc_add_file(s, filename) < 0)
325 ret = 1;
326 if (!first_file)
327 first_file = filename;
331 if (0 == ret) {
332 if (bench)
333 tcc_print_stats(s, getclock_us() - start_time);
335 if (s->output_type == TCC_OUTPUT_MEMORY) {
336 #ifdef TCC_IS_NATIVE
337 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
338 #else
339 tcc_error_noabort("-run is not available in a cross compiler");
340 ret = 1;
341 #endif
342 } else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
343 if (s->outfile)
344 fclose(s->ppfp);
345 } else {
346 if (!s->outfile)
347 s->outfile = default_outputfile(s, first_file);
348 ret = !!tcc_output_file(s, s->outfile);
349 /* dump collected dependencies */
350 if (s->gen_deps && !ret)
351 gen_makedeps(s, s->outfile, s->deps_outfile);
355 tcc_delete(s);
356 if (bench)
357 tcc_memstats();
358 return ret;