Accept concatenated strings in attributes
[tinycc.git] / tcc.c
blobcbef5d3bdf1062b9743d6479cbff486efca1af9a
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 " -mms-bitfields use bitfield alignment consistent with MSVC\n"
101 " -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
102 " -w disable all warnings\n"
103 " -v show version\n"
104 " -vv show included files (as sole argument: show search paths)\n"
105 " -dumpversion\n"
106 " -bench show compilation statistics\n"
107 " -xc -xa specify type of the next infile\n"
108 " - use stdin pipe as infile\n"
109 " @listfile read arguments from listfile\n"
110 "Preprocessor options:\n"
111 " -Idir add include path 'dir'\n"
112 " -Dsym[=val] define 'sym' with value 'val'\n"
113 " -Usym undefine 'sym'\n"
114 " -E preprocess only\n"
115 " -P[1] no / alternative #line output with -E\n"
116 " -dD -dM output #define directives with -E\n"
117 "Linker options:\n"
118 " -Ldir add library path 'dir'\n"
119 " -llib link with dynamic or static library 'lib'\n"
120 " -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
121 " -r generate (relocatable) object file\n"
122 " -rdynamic export all global symbols to dynamic linker\n"
123 " -shared generate a shared library\n"
124 " -soname set name for shared library to be used at runtime\n"
125 " -static static linking\n"
126 " -Wl,-opt[=val] set linker option (see manual)\n"
127 "Debugger options:\n"
128 " -g generate runtime debug info\n"
129 #ifdef CONFIG_TCC_BCHECK
130 " -b compile with built-in memory and bounds checker (implies -g)\n"
131 #endif
132 #ifdef CONFIG_TCC_BACKTRACE
133 " -bt N show N callers in stack traces\n"
134 #endif
135 "Misc options:\n"
136 " -nostdinc do not use standard system include paths\n"
137 " -nostdlib do not link with standard crt and libraries\n"
138 " -Bdir use 'dir' as tcc internal library and include path\n"
139 " -MD generate target dependencies for make\n"
140 " -MF depfile put generated dependencies here\n"
144 /* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
145 #if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
146 #ifdef _WIN32
147 #include <process.h>
148 static int execvp_win32(const char *prog, char **argv)
150 int ret = _spawnvp(P_NOWAIT, prog, (const char *const*)argv);
151 if (-1 == ret)
152 return ret;
153 cwait(&ret, ret, WAIT_CHILD);
154 exit(ret);
156 #define execvp execvp_win32
157 #endif
158 static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
160 char child_path[4096], *child_name; const char *target;
161 switch (atoi(optarg)) {
162 #ifdef TCC_TARGET_I386
163 case 32: break;
164 case 64: target = "x86_64";
165 #else
166 case 64: break;
167 case 32: target = "i386";
168 #endif
169 pstrcpy(child_path, sizeof child_path - 40, argv[0]);
170 child_name = tcc_basename(child_path);
171 strcpy(child_name, target);
172 #ifdef TCC_TARGET_PE
173 strcat(child_name, "-win32");
174 #endif
175 strcat(child_name, "-tcc");
176 if (strcmp(argv[0], child_path)) {
177 if (s->verbose > 0)
178 printf("tcc: using '%s'\n", child_name), fflush(stdout);
179 execvp(argv[0] = child_path, argv);
181 tcc_error("'%s' not found", child_name);
182 case 0: /* ignore -march etc. */
183 break;
184 default:
185 tcc_warning("unsupported option \"-m%s\"", optarg);
188 #else
189 #define exec_other_tcc(s, argv, optarg)
190 #endif
192 static void gen_makedeps(TCCState *s, const char *target, const char *filename)
194 FILE *depout;
195 char buf[1024], *ext;
196 int i;
198 if (!filename) {
199 /* compute filename automatically
200 * dir/file.o -> dir/file.d */
201 pstrcpy(buf, sizeof(buf), target);
202 ext = tcc_fileextension(buf);
203 pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
204 filename = buf;
207 if (s->verbose)
208 printf("<- %s\n", filename);
210 /* XXX return err codes instead of error() ? */
211 depout = fopen(filename, "w");
212 if (!depout)
213 tcc_error("could not open '%s'", filename);
215 fprintf(depout, "%s: \\\n", target);
216 for (i=0; i<s->nb_target_deps; ++i)
217 fprintf(depout, " %s \\\n", s->target_deps[i]);
218 fprintf(depout, "\n");
219 fclose(depout);
222 static char *default_outputfile(TCCState *s, const char *first_file)
224 char buf[1024];
225 char *ext;
226 const char *name = "a";
228 if (first_file && strcmp(first_file, "-"))
229 name = tcc_basename(first_file);
230 pstrcpy(buf, sizeof(buf), name);
231 ext = tcc_fileextension(buf);
232 #ifdef TCC_TARGET_PE
233 if (s->output_type == TCC_OUTPUT_DLL)
234 strcpy(ext, ".dll");
235 else
236 if (s->output_type == TCC_OUTPUT_EXE)
237 strcpy(ext, ".exe");
238 else
239 #endif
240 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r && *ext)
241 strcpy(ext, ".o");
242 else
243 strcpy(buf, "a.out");
245 return tcc_strdup(buf);
248 static unsigned getclock_ms(void)
250 #ifdef _WIN32
251 return GetTickCount();
252 #else
253 struct timeval tv;
254 gettimeofday(&tv, NULL);
255 return tv.tv_sec*1000 + (tv.tv_usec+500)/1000;
256 #endif
259 int main(int argc, char **argv)
261 TCCState *s;
262 int ret, optind, i;
263 unsigned start_time = 0;
264 const char *first_file = NULL;
266 s = tcc_new();
268 optind = tcc_parse_args(s, argc - 1, argv + 1);
270 tcc_set_environment(s);
272 if (optind == 0) {
273 help();
274 return 1;
277 if (s->option_m)
278 exec_other_tcc(s, argv, s->option_m);
280 if (s->verbose)
281 display_info(s, 0);
283 if (s->nb_files == 0) {
284 if (optind == 1) {
285 if (s->print_search_dirs || s->verbose == 2) {
286 tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
287 display_info(s, 1);
288 return 1;
290 if (s->verbose)
291 return 1;
293 tcc_error("no input files\n");
296 /* check -c consistency : only single file handled. XXX: checks file type */
297 if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
298 if (s->nb_libraries != 0)
299 tcc_error("cannot specify libraries with -c");
300 /* accepts only a single input file */
301 if (s->nb_files != 1)
302 tcc_error("cannot specify multiple files with -c");
305 if (s->output_type == 0)
306 s->output_type = TCC_OUTPUT_EXE;
307 tcc_set_output_type(s, s->output_type);
309 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
310 if (!s->outfile) {
311 s->ppfp = stdout;
312 } else {
313 s->ppfp = fopen(s->outfile, "w");
314 if (!s->ppfp)
315 tcc_error("could not write '%s'", s->outfile);
317 } else if (s->output_type != TCC_OUTPUT_OBJ) {
318 if (s->option_pthread)
319 tcc_set_options(s, "-lpthread");
322 if (s->do_bench)
323 start_time = getclock_ms();
325 /* compile or add each files or library */
326 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
327 struct filespec *f = s->files[i];
328 if (f->type >= AFF_TYPE_LIB) {
329 s->alacarte_link = f->type == AFF_TYPE_LIB;
330 if (tcc_add_library_err(s, f->name) < 0)
331 ret = 1;
332 } else {
333 if (1 == s->verbose)
334 printf("-> %s\n", f->name);
335 s->filetype = f->type;
336 if (tcc_add_file(s, f->name) < 0)
337 ret = 1;
338 if (!first_file)
339 first_file = f->name;
341 s->filetype = AFF_TYPE_NONE;
342 s->alacarte_link = 1;
345 if (s->output_type == TCC_OUTPUT_PREPROCESS) {
346 if (s->outfile)
347 fclose(s->ppfp);
349 } else if (0 == ret) {
350 if (s->output_type == TCC_OUTPUT_MEMORY) {
351 #ifdef TCC_IS_NATIVE
352 ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
353 #endif
354 } else {
355 if (!s->outfile)
356 s->outfile = default_outputfile(s, first_file);
357 ret = !!tcc_output_file(s, s->outfile);
358 if (s->gen_deps && !ret)
359 gen_makedeps(s, s->outfile, s->deps_outfile);
363 if (s->do_bench)
364 tcc_print_stats(s, getclock_ms() - start_time);
365 tcc_delete(s);
366 return ret;