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