Makefile for Windows native tcc handles recent UNICODE support
[tinycc.git] / tcc.c
blobc5a1f0e27c75f643626b6ae58fd58799b2d425e8
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 #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
56 #endif
57 #ifdef TCC_TARGET_PE
58 " Windows"
59 #elif defined(__APPLE__)
60 /* Current Apple OS name as of 2016 */
61 " macOS"
62 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
63 " FreeBSD"
64 #elif defined(__DragonFly__)
65 " DragonFly BSD"
66 #elif defined(__NetBSD__)
67 " NetBSD"
68 #elif defined(__OpenBSD__)
69 " OpenBSD"
70 #elif defined(__linux__)
71 " Linux"
72 #else
73 " Unidentified system"
74 #endif
75 ")\n", TCC_VERSION);
76 break;
77 case 1:
78 printf("install: %s\n", s->tcc_lib_path);
79 /* print_paths("programs", NULL, 0); */
80 print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
81 print_paths("libraries", s->library_paths, s->nb_library_paths);
82 #ifndef TCC_TARGET_PE
83 print_paths("crt", s->crt_paths, s->nb_crt_paths);
84 printf("elfinterp:\n %s\n", DEFAULT_ELFINTERP(s));
85 #endif
86 break;
90 static void help(void)
92 printf("Tiny C Compiler "TCC_VERSION" - Copyright (C) 2001-2006 Fabrice Bellard\n"
93 "Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
94 " tcc [options...] -run infile [arguments...]\n"
95 "General options:\n"
96 " -c compile only - generate an object file\n"
97 " -o outfile set output filename\n"
98 " -run run compiled source\n"
99 " -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
100 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
101 " -w disable all warnings\n"
102 " -v show version\n"
103 " -vv show included files (as sole argument: show search paths)\n"
104 " -bench show compilation statistics\n"
105 "Preprocessor options:\n"
106 " -Idir add include path 'dir'\n"
107 " -Dsym[=val] define 'sym' with value 'val'\n"
108 " -Usym undefine 'sym'\n"
109 " -E preprocess only\n"
110 " -P[1] no / alternative #line output with -E\n"
111 " -dD -dM output #define directives with -E\n"
112 " -include file include file above each input file\n"
113 "Linker options:\n"
114 " -Ldir add library path 'dir'\n"
115 " -llib link with dynamic or static library 'lib'\n"
116 " -r generate (relocatable) object file\n"
117 " -shared generate a shared library\n"
118 " -rdynamic export all global symbols to dynamic linker\n"
119 " -soname set name for shared library to be used at runtime\n"
120 " -static static linking\n"
121 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
122 " -Wl,-opt[=val] set linker option (see manual)\n"
123 "Debugger options:\n"
124 " -g generate runtime debug info\n"
125 #ifdef CONFIG_TCC_BCHECK
126 " -b compile with built-in memory and bounds checker (implies -g)\n"
127 #endif
128 #ifdef CONFIG_TCC_BACKTRACE
129 " -bt N show N callers in stack traces\n"
130 #endif
131 "Misc options:\n"
132 " -x[c|a|n] specify type of the next infile\n"
133 " -nostdinc do not use standard system include paths\n"
134 " -nostdlib do not link with standard crt and libraries\n"
135 " -Bdir use 'dir' as tcc's private library/include path\n"
136 " -MD generate target dependencies for make\n"
137 " -MF depfile put generated dependencies here\n"
138 " -dumpversion print version\n"
139 " - use stdin pipe as infile\n"
140 " @listfile read arguments from listfile\n"
141 "Target specific options:\n"
142 " -m32/64 execute i386/x86-64 cross compiler\n"
143 " -mms-bitfields use MSVC bitfield layout\n"
144 #ifdef TCC_TARGET_ARM
145 " -mfloat-abi hard/softfp on arm\n"
146 #endif
147 #ifdef TCC_TARGET_X86_64
148 " -mno-sse disable floats on x86-64\n"
149 #endif
153 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
154 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
155 #ifdef _WIN32
156 #include <process.h>
158 static char *str_replace(const char *str, const char *p, const char *r)
160 const char *s, *s0;
161 char *d, *d0;
162 int sl, pl, rl;
164 sl = strlen(str);
165 pl = strlen(p);
166 rl = strlen(r);
167 for (d0 = NULL;; d0 = tcc_malloc(sl + 1)) {
168 for (d = d0, s = str; s0 = s, s = strstr(s, p), s; s += pl) {
169 if (d) {
170 memcpy(d, s0, sl = s - s0), d += sl;
171 memcpy(d, r, rl), d += rl;
172 } else
173 sl += rl - pl;
175 if (d) {
176 strcpy(d, s0);
177 return d0;
182 static int execvp_win32(const char *prog, char **argv)
184 int ret; char **p;
185 /* replace all " by \" */
186 for (p = argv; *p; ++p)
187 if (strchr(*p, '"'))
188 *p = str_replace(*p, "\"", "\\\"");
189 ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
190 if (-1 == ret)
191 return ret;
192 cwait(&ret, ret, WAIT_CHILD);
193 exit(ret);
195 #define execvp execvp_win32
196 #endif
197 static void exec_other_tcc(TCCState *s, char **argv, int option)
199 char child_path[4096], *a0 = argv[0]; const char *target;
200 int l;
202 switch (option) {
204 #ifdef TCC_TARGET_I386
205 case 32: break;
206 case 64: target = "x86_64";
207 #else
208 case 64: break;
209 case 32: target = "i386";
211 #endif
212 l = tcc_basename(a0) - a0;
213 snprintf(child_path, sizeof child_path,
214 #ifdef TCC_TARGET_PE
215 "%.*s%s-win32-tcc"
216 #else
217 "%.*s%s-tcc"
218 #endif
219 , l, a0, target);
220 if (strcmp(a0, child_path)) {
221 if (s->verbose > 0)
222 printf("tcc: using '%s'\n", child_path + l), fflush(stdout);
223 execvp(argv[0] = child_path, argv);
225 tcc_error("'%s' not found", child_path + l);
228 #else
229 #define exec_other_tcc(s, argv, option)
230 #endif
232 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
234 FILE *depout;
235 char buf[1024];
236 int i;
238 if (!filename) {
239 /* compute filename automatically: dir/file.o -> dir/file.d */
240 snprintf(buf, sizeof buf, "%.*s.d",
241 (int)(tcc_fileextension(target) - target), target);
242 filename = buf;
245 if (s->verbose)
246 printf("<- %s\n", filename);
248 /* XXX return err codes instead of error() ? */
249 depout = fopen(filename, "w");
250 if (!depout)
251 tcc_error("could not open '%s'", filename);
253 fprintf(depout, "%s: \\\n", target);
254 for (i=0; i<s->nb_target_deps; ++i)
255 fprintf(depout, " %s \\\n", s->target_deps[i]);
256 fprintf(depout, "\n");
257 fclose(depout);
260 static char *default_outputfile(TCCState *s, const char *first_file)
262 char buf[1024];
263 char *ext;
264 const char *name = "a";
266 if (first_file && strcmp(first_file, "-"))
267 name = tcc_basename(first_file);
268 snprintf(buf, sizeof(buf), "%s", name);
269 ext = tcc_fileextension(buf);
270 #ifdef TCC_TARGET_PE
271 if (s->output_type == TCC_OUTPUT_DLL)
272 strcpy(ext, ".dll");
273 else
274 if (s->output_type == TCC_OUTPUT_EXE)
275 strcpy(ext, ".exe");
276 else
277 #endif
278 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
279 strcpy(ext, ".o");
280 else
281 strcpy(buf, "a.out");
282 return tcc_strdup(buf);
285 static unsigned getclock_ms(void)
287 #ifdef _WIN32
288 return GetTickCount();
289 #else
290 struct timeval tv;
291 gettimeofday(&tv, NULL);
292 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
293 #endif
296 int main(int argc, char **argv)
298 TCCState *s;
299 int ret, optind, i;
300 unsigned start_time = 0;
301 const char *first_file = NULL;
303 s = tcc_new();
305 optind = tcc_parse_args(s, argc - 1, argv + 1);
307 tcc_set_environment(s);
309 if (optind == 0) {
310 help();
311 return 1;
314 if (s->cross_target)
315 exec_other_tcc(s, argv, s->cross_target);
317 if (s->verbose)
318 display_info(s, 0);
320 if (s->nb_files == 0) {
321 if (optind == 1) {
322 if (s->print_search_dirs || s->verbose == 2) {
323 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
324 display_info(s, 1);
325 return 1;
327 if (s->verbose)
328 return 1;
330 tcc_error("no input files\n");
333 /* check -c consistency : only single file handled. XXX: checks file type */
334 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
335 if (s->nb_libraries != 0)
336 tcc_error("cannot specify libraries with -c");
337 /* accepts only a single input file */
338 if (s->nb_files != 1)
339 tcc_error("cannot specify multiple files with -c");
342 if (s->output_type == 0)
343 s->output_type = TCC_OUTPUT_EXE;
344 tcc_set_output_type(s, s->output_type);
346 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
347 if (!s->outfile) {
348 s->ppfp = stdout;
349 } else {
350 s->ppfp = fopen(s->outfile, "w");
351 if (!s->ppfp)
352 tcc_error("could not write '%s'", s->outfile);
354 } else if (s->output_type != TCC_OUTPUT_OBJ) {
355 if (s->option_pthread)
356 tcc_set_options(s, "-lpthread");
359 if (s->do_bench)
360 start_time = getclock_ms();
362 /* compile or add each files or library */
363 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
364 struct filespec *f = s->files[i];
365 if (f->type >= AFF_TYPE_LIB) {
366 s->alacarte_link = f->type == AFF_TYPE_LIB;
367 if (tcc_add_library_err(s, f->name) < 0)
368 ret = 1;
369 } else {
370 if (1 == s->verbose)
371 printf("-> %s\n", f->name);
372 s->filetype = f->type;
373 if (tcc_add_file(s, f->name) < 0)
374 ret = 1;
375 if (!first_file)
376 first_file = f->name;
378 s->filetype = AFF_TYPE_NONE;
379 s->alacarte_link = 1;
382 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
383 if (s->outfile)
384 fclose(s->ppfp);
386 } else if (0 == ret) {
387 if (s->output_type == TCC_OUTPUT_MEMORY) {
388 #ifdef TCC_IS_NATIVE
389 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
390 #endif
391 } else {
392 if (!s->outfile)
393 s->outfile = default_outputfile(s, first_file);
394 ret = !!tcc_output_file(s, s->outfile);
395 if (s->gen_deps && !ret)
396 gen_makedeps(s, s->outfile, s->deps_outfile);
400 if (s->do_bench)
401 tcc_print_stats(s, getclock_ms() - start_time);
402 tcc_delete(s);
403 return ret;