Rev 9284 | Rev 9782 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
145 | halyavin | 1 | /* |
2 | * TCC - Tiny C Compiler |
||
6441 | siemargl | 3 | * |
145 | halyavin | 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 | |||
6429 | siemargl | 21 | #ifdef ONE_SOURCE |
22 | #include "libtcc.c" |
||
145 | halyavin | 23 | #else |
6429 | siemargl | 24 | #include "tcc.h" |
145 | halyavin | 25 | #endif |
609 | andrew_pro | 26 | |
6429 | siemargl | 27 | static void print_paths(const char *msg, char **paths, int nb_paths) |
28 | { |
||
145 | halyavin | 29 | int i; |
6429 | siemargl | 30 | printf("%s:\n%s", msg, nb_paths ? "" : " -\n"); |
31 | for(i = 0; i < nb_paths; i++) |
||
32 | printf(" %s\n", paths[i]); |
||
145 | halyavin | 33 | } |
609 | andrew_pro | 34 | |
6429 | siemargl | 35 | static void display_info(TCCState *s, int what) |
145 | halyavin | 36 | { |
6429 | siemargl | 37 | switch (what) { |
38 | case 0: |
||
39 | printf("tcc version %s (" |
||
145 | halyavin | 40 | #ifdef TCC_TARGET_I386 |
6429 | siemargl | 41 | "i386" |
42 | #elif defined TCC_TARGET_X86_64 |
||
43 | "x86-64" |
||
44 | #elif defined TCC_TARGET_C67 |
||
45 | "C67" |
||
46 | #elif defined TCC_TARGET_ARM |
||
47 | "ARM" |
||
48 | # ifdef TCC_ARM_HARDFLOAT |
||
49 | " Hard Float" |
||
50 | # endif |
||
51 | #elif defined TCC_TARGET_ARM64 |
||
52 | "AArch64" |
||
53 | # ifdef TCC_ARM_HARDFLOAT |
||
54 | " Hard Float" |
||
55 | # endif |
||
145 | halyavin | 56 | #endif |
6429 | siemargl | 57 | #ifdef TCC_TARGET_PE |
58 | " Windows" |
||
59 | #elif defined TCC_TARGET_MEOS |
||
60 | " KolibriOS" |
||
9305 | Coldy | 61 | #ifdef TCC_TARGET_KX |
62 | "/KX extension" |
||
63 | #endif |
||
145 | halyavin | 64 | #else |
6429 | siemargl | 65 | " Linux" |
145 | halyavin | 66 | #endif |
6429 | siemargl | 67 | ")\n", TCC_VERSION); |
145 | halyavin | 68 | break; |
6429 | siemargl | 69 | case 1: |
70 | printf("install: %s\n", s->tcc_lib_path); |
||
71 | /* print_paths("programs", NULL, 0); */ |
||
72 | print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths); |
||
73 | print_paths("libraries", s->library_paths, s->nb_library_paths); |
||
74 | #ifndef TCC_TARGET_PE |
||
75 | print_paths("crt", s->crt_paths, s->nb_crt_paths); |
||
76 | printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s)); |
||
145 | halyavin | 77 | #endif |
78 | break; |
||
79 | } |
||
80 | } |
||
81 | |||
6429 | siemargl | 82 | static void help(void) |
145 | halyavin | 83 | { |
6429 | siemargl | 84 | printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n" |
85 | "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n" |
||
86 | " tcc [options...] -run infile [arguments...]\n" |
||
87 | "General options:\n" |
||
88 | " -c compile only - generate an object file\n" |
||
89 | " -o outfile set output filename\n" |
||
90 | " -run run compiled source\n" |
||
91 | " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n" |
||
92 | " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n" |
||
93 | " -w disable all warnings\n" |
||
94 | " -v show version\n" |
||
95 | " -vv show included files (as sole argument: show search paths)\n" |
||
96 | " -dumpversion\n" |
||
97 | " -bench show compilation statistics\n" |
||
98 | " -xc -xa specify type of the next infile\n" |
||
99 | " - use stdin pipe as infile\n" |
||
100 | " @listfile read line separated arguments from 'listfile'\n" |
||
101 | "Preprocessor options:\n" |
||
102 | " -Idir add include path 'dir'\n" |
||
103 | " -Dsym[=val] define 'sym' with value 'val'\n" |
||
104 | " -Usym undefine 'sym'\n" |
||
105 | " -E preprocess only\n" |
||
106 | " -P[1] no/alternative output of #line directives\n" |
||
107 | " -d{D|M} dump defines\n" |
||
108 | " -C keep comments\n" |
||
109 | "Linker options:\n" |
||
110 | " -Ldir add library path 'dir'\n" |
||
111 | " -llib link with dynamic or static library 'lib'\n" |
||
112 | " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n" |
||
113 | " -r generate (relocatable) object file\n" |
||
114 | " -rdynamic export all global symbols to dynamic linker\n" |
||
115 | " -shared generate a shared library\n" |
||
116 | " -soname set name for shared library to be used at runtime\n" |
||
117 | " -static static linking\n" |
||
118 | " -Wl,-opt[=val] set linker option (see manual)\n" |
||
6441 | siemargl | 119 | " -stack=size set PE/KOS32 stack size\n" |
6429 | siemargl | 120 | "Debugger options:\n" |
121 | " -g generate runtime debug info\n" |
||
145 | halyavin | 122 | #ifdef CONFIG_TCC_BCHECK |
6429 | siemargl | 123 | " -b compile with built-in memory and bounds checker (implies -g)\n" |
145 | halyavin | 124 | #endif |
6429 | siemargl | 125 | #ifdef CONFIG_TCC_BACKTRACE |
126 | " -bt N show N callers in stack traces\n" |
||
145 | halyavin | 127 | #endif |
6429 | siemargl | 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" |
||
9284 | Coldy | 134 | #if defined(TCC_TARGET_MEOS) && !defined (TCC_TARGET_KX) |
8157 | Boppan | 135 | "For KolibriOS only:\n" |
136 | " -nobss do not emit BSS section into file\n" |
||
9284 | Coldy | 137 | #endif |
6429 | siemargl | 138 | ); |
145 | halyavin | 139 | } |
140 | |||
6429 | siemargl | 141 | /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */ |
142 | #if (defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64) && !defined(TCC_TARGET_MEOS) |
||
143 | #ifdef _WIN32 |
||
144 | #include |
||
145 | static int execvp_win32(const char *prog, char **argv) |
||
145 | halyavin | 146 | { |
6429 | siemargl | 147 | int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv); |
148 | if (-1 == ret) |
||
149 | return ret; |
||
150 | cwait(&ret, ret, WAIT_CHILD); |
||
151 | exit(ret); |
||
145 | halyavin | 152 | } |
6429 | siemargl | 153 | #define execvp execvp_win32 |
145 | halyavin | 154 | #endif |
6429 | siemargl | 155 | static void exec_other_tcc(TCCState *s, char **argv, const char *optarg) |
145 | halyavin | 156 | { |
6429 | siemargl | 157 | char child_path[4096], *child_name; const char *target; |
158 | switch (atoi(optarg)) { |
||
145 | halyavin | 159 | #ifdef TCC_TARGET_I386 |
6429 | siemargl | 160 | case 32: break; |
161 | case 64: target = "x86_64"; |
||
145 | halyavin | 162 | #else |
6429 | siemargl | 163 | case 64: break; |
164 | case 32: target = "i386"; |
||
145 | halyavin | 165 | #endif |
6429 | siemargl | 166 | pstrcpy(child_path, sizeof child_path - 40, argv[0]); |
167 | child_name = tcc_basename(child_path); |
||
168 | strcpy(child_name, target); |
||
169 | #ifdef TCC_TARGET_PE |
||
170 | strcat(child_name, "-win32"); |
||
171 | #elif defined TCC_TARGET_MEOS |
||
172 | strcat(child_name, "-kos32"); |
||
145 | halyavin | 173 | #endif |
6429 | siemargl | 174 | strcat(child_name, "-tcc"); |
175 | if (strcmp(argv[0], child_path)) { |
||
176 | if (s->verbose > 0) |
||
177 | printf("tcc: using '%s'\n", child_name), fflush(stdout); |
||
178 | execvp(argv[0] = child_path, argv); |
||
145 | halyavin | 179 | } |
6429 | siemargl | 180 | tcc_error("'%s' not found", child_name); |
181 | case 0: /* ignore -march etc. */ |
||
145 | halyavin | 182 | break; |
183 | default: |
||
6429 | siemargl | 184 | tcc_warning("unsupported option \"-m%s\"", optarg); |
145 | halyavin | 185 | } |
186 | } |
||
187 | #else |
||
6429 | siemargl | 188 | #define exec_other_tcc(s, argv, optarg) |
145 | halyavin | 189 | #endif |
190 | |||
6429 | siemargl | 191 | static void gen_makedeps(TCCState *s, const char *target, const char *filename) |
145 | halyavin | 192 | { |
6429 | siemargl | 193 | FILE *depout; |
194 | char buf[1024], *ext; |
||
145 | halyavin | 195 | int i; |
196 | |||
6429 | siemargl | 197 | if (!filename) { |
198 | /* compute filename automatically |
||
199 | * dir/file.o -> dir/file.d */ |
||
200 | pstrcpy(buf, sizeof(buf), target); |
||
201 | ext = tcc_fileextension(buf); |
||
202 | pstrcpy(ext, sizeof(buf) - (ext-buf), ".d"); |
||
203 | filename = buf; |
||
145 | halyavin | 204 | } |
205 | |||
6429 | siemargl | 206 | if (s->verbose) |
207 | printf("<- %s\n", filename); |
||
145 | halyavin | 208 | |
6429 | siemargl | 209 | /* XXX return err codes instead of error() ? */ |
210 | depout = fopen(filename, "w"); |
||
211 | if (!depout) |
||
212 | tcc_error("could not open '%s'", filename); |
||
145 | halyavin | 213 | |
6429 | siemargl | 214 | fprintf(depout, "%s : \\\n", target); |
215 | for (i=0; i |
||
216 | fprintf(depout, " %s \\\n", s->target_deps[i]); |
||
217 | fprintf(depout, "\n"); |
||
218 | fclose(depout); |
||
145 | halyavin | 219 | } |
220 | |||
6429 | siemargl | 221 | static char *default_outputfile(TCCState *s, const char *first_file) |
145 | halyavin | 222 | { |
6429 | siemargl | 223 | char buf[1024]; |
224 | char *ext; |
||
225 | const char *name = "a"; |
||
145 | halyavin | 226 | |
6429 | siemargl | 227 | if (first_file && strcmp(first_file, "-")) |
228 | name = tcc_basename(first_file); |
||
229 | pstrcpy(buf, sizeof(buf), name); |
||
230 | ext = tcc_fileextension(buf); |
||
145 | halyavin | 231 | #ifdef TCC_TARGET_PE |
6429 | siemargl | 232 | if (s->output_type == TCC_OUTPUT_DLL) |
233 | strcpy(ext, ".dll"); |
||
234 | else |
||
235 | if (s->output_type == TCC_OUTPUT_EXE) |
||
236 | strcpy(ext, ".exe"); |
||
237 | else |
||
145 | halyavin | 238 | #endif |
6429 | siemargl | 239 | if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) || |
240 | (s->output_type == TCC_OUTPUT_PREPROCESS) ) |
||
241 | && *ext) |
||
242 | strcpy(ext, ".o"); |
||
145 | halyavin | 243 | else |
6429 | siemargl | 244 | strcpy(buf, "a.out"); |
145 | halyavin | 245 | |
6429 | siemargl | 246 | return tcc_strdup(buf); |
145 | halyavin | 247 | } |
248 | |||
249 | static int64_t getclock_us(void) |
||
250 | { |
||
6429 | siemargl | 251 | #ifdef _WIN32 |
252 | LARGE_INTEGER frequency, t1; |
||
253 | QueryPerformanceFrequency(&frequency); |
||
254 | QueryPerformanceCounter(&t1); |
||
255 | return t1.QuadPart * 1000000LL / frequency.QuadPart; |
||
145 | halyavin | 256 | #else |
257 | struct timeval tv; |
||
258 | gettimeofday(&tv, NULL); |
||
259 | return tv.tv_sec * 1000000LL + tv.tv_usec; |
||
260 | #endif |
||
261 | } |
||
262 | |||
6429 | siemargl | 263 | int main(int argc, char **argv) |
145 | halyavin | 264 | { |
6429 | siemargl | 265 | TCCState *s; |
266 | int ret, optind, i; |
||
267 | int64_t start_time = 0; |
||
145 | halyavin | 268 | |
6429 | siemargl | 269 | s = tcc_new(); |
145 | halyavin | 270 | |
6429 | siemargl | 271 | optind = tcc_parse_args(s, argc - 1, argv + 1); |
145 | halyavin | 272 | |
6429 | siemargl | 273 | if (s->do_bench) |
274 | start_time = getclock_us(); |
||
145 | halyavin | 275 | |
6429 | siemargl | 276 | tcc_set_environment(s); |
145 | halyavin | 277 | |
6429 | siemargl | 278 | if (optind == 0) { |
279 | help(); |
||
280 | return 1; |
||
145 | halyavin | 281 | } |
282 | |||
6429 | siemargl | 283 | if (s->option_m) |
284 | exec_other_tcc(s, argv, s->option_m); |
||
145 | halyavin | 285 | |
6429 | siemargl | 286 | if (s->verbose) |
287 | display_info(s, 0); |
||
145 | halyavin | 288 | |
6429 | siemargl | 289 | if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) { |
290 | tcc_set_output_type(s, TCC_OUTPUT_MEMORY); |
||
291 | display_info(s, 1); |
||
292 | return 0; |
||
145 | halyavin | 293 | } |
294 | |||
6429 | siemargl | 295 | if (s->verbose && optind == 1) |
145 | halyavin | 296 | return 0; |
297 | |||
6429 | siemargl | 298 | if (s->nb_files == 0) |
299 | tcc_error("no input files\n"); |
||
145 | halyavin | 300 | |
6429 | siemargl | 301 | /* check -c consistency : only single file handled. XXX: checks file type */ |
302 | if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) { |
||
303 | if (s->nb_libraries != 0) |
||
304 | tcc_error("cannot specify libraries with -c"); |
||
145 | halyavin | 305 | /* accepts only a single input file */ |
6429 | siemargl | 306 | if ((s->nb_files != 1) && s->outfile) { |
307 | tcc_error("cannot specify multiple files with -c and -o"); |
||
145 | halyavin | 308 | } |
309 | } |
||
310 | |||
6429 | siemargl | 311 | tcc_set_output_type(s, s->output_type); |
145 | halyavin | 312 | |
313 | /* compile or add each files or library */ |
||
6429 | siemargl | 314 | for(i = ret = 0; i < s->nb_files && ret == 0; i++) { |
315 | int filetype = *(unsigned char *)s->files[i]; |
||
316 | const char *filename = s->files[i] + 1; |
||
317 | if (filename[0] == '-' && filename[1] == 'l') { |
||
318 | if (tcc_add_library(s, filename + 2) < 0) { |
||
319 | /* don't fail on -lm as it's harmless to skip math lib */ |
||
320 | if (strcmp(filename + 2, "m")) { |
||
321 | tcc_error_noabort("cannot find library 'lib%s'", filename + 2); |
||
322 | ret = 1; |
||
323 | } |
||
324 | } |
||
145 | halyavin | 325 | } else { |
6429 | siemargl | 326 | if (1 == s->verbose) |
327 | printf("-> %s\n", filename); |
||
328 | if (!s->outfile) |
||
329 | s->outfile = default_outputfile(s, filename); |
||
330 | if (tcc_add_file(s, filename, filetype) < 0) |
||
145 | halyavin | 331 | ret = 1; |
6429 | siemargl | 332 | else |
333 | if (s->output_type == TCC_OUTPUT_OBJ) { |
||
334 | ret = !!tcc_output_file(s, s->outfile); |
||
335 | if (s->gen_deps && !ret) |
||
336 | gen_makedeps(s, s->outfile, s->deps_outfile); |
||
337 | if (!ret) { |
||
338 | if ((i+1) < s->nb_files) { |
||
339 | tcc_delete(s); |
||
340 | s = tcc_new(); |
||
341 | tcc_parse_args(s, argc - 1, argv + 1); |
||
342 | tcc_set_environment(s); |
||
343 | if (s->output_type != TCC_OUTPUT_OBJ) |
||
344 | tcc_error("internal error"); |
||
345 | tcc_set_output_type(s, s->output_type); |
||
346 | } |
||
347 | } |
||
145 | halyavin | 348 | } |
349 | } |
||
350 | } |
||
351 | |||
6429 | siemargl | 352 | if (0 == ret) { |
353 | if (s->output_type == TCC_OUTPUT_MEMORY) { |
||
354 | #ifdef TCC_IS_NATIVE |
||
355 | ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind); |
||
609 | andrew_pro | 356 | #else |
6429 | siemargl | 357 | tcc_error_noabort("-run is not available in a cross compiler"); |
358 | ret = 1; |
||
145 | halyavin | 359 | #endif |
6429 | siemargl | 360 | } else |
361 | if (s->output_type == TCC_OUTPUT_EXE || |
||
362 | s->output_type == TCC_OUTPUT_DLL) |
||
363 | { |
||
364 | ret = !!tcc_output_file(s, s->outfile); |
||
365 | if (s->gen_deps && !ret) |
||
366 | gen_makedeps(s, s->outfile, s->deps_outfile); |
||
367 | } |
||
145 | halyavin | 368 | } |
369 | |||
6429 | siemargl | 370 | if (s->do_bench) |
371 | tcc_print_stats(s, getclock_us() - start_time); |
||
372 | |||
373 | tcc_delete(s); |
||
145 | halyavin | 374 | return ret; |
375 | }>>>>->> |