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