78
64
" -bt N show N callers in stack traces\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"
81
70
" -MD generate target dependencies for make\n"
82
71
" -MF depfile put generated dependencies here\n"
86
typedef struct TCCOption {
119
TCC_OPTION_print_search_dirs,
133
static const TCCOption tcc_options[] = {
134
{ "h", TCC_OPTION_HELP, 0 },
135
{ "-help", TCC_OPTION_HELP, 0 },
136
{ "?", TCC_OPTION_HELP, 0 },
137
{ "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
138
{ "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
139
{ "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
140
{ "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
141
{ "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
142
{ "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
143
{ "bench", TCC_OPTION_bench, 0 },
144
#ifdef CONFIG_TCC_BACKTRACE
145
{ "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
147
#ifdef CONFIG_TCC_BCHECK
148
{ "b", TCC_OPTION_b, 0 },
150
{ "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
151
{ "c", TCC_OPTION_c, 0 },
152
{ "static", TCC_OPTION_static, 0 },
153
{ "shared", TCC_OPTION_shared, 0 },
154
{ "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
155
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
156
{ "pedantic", TCC_OPTION_pedantic, 0},
157
{ "pthread", TCC_OPTION_pthread, 0},
158
{ "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
159
{ "rdynamic", TCC_OPTION_rdynamic, 0 },
160
{ "r", TCC_OPTION_r, 0 },
161
{ "s", TCC_OPTION_s, 0 },
162
{ "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
163
{ "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
164
{ "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
165
{ "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
166
{ "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
167
{ "isystem", TCC_OPTION_isystem, TCC_OPTION_HAS_ARG },
168
{ "nostdinc", TCC_OPTION_nostdinc, 0 },
169
{ "nostdlib", TCC_OPTION_nostdlib, 0 },
170
{ "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
171
{ "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
172
{ "w", TCC_OPTION_w, 0 },
173
{ "pipe", TCC_OPTION_pipe, 0},
174
{ "E", TCC_OPTION_E, 0},
175
{ "MD", TCC_OPTION_MD, 0},
176
{ "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
177
{ "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
181
static int64_t getclock_us(void)
186
return (tb.time * 1000LL + tb.millitm) * 1000LL;
189
gettimeofday(&tv, NULL);
190
return tv.tv_sec * 1000000LL + tv.tv_usec;
194
/* convert 'str' into an array of space separated strings */
195
static int expand_args(char ***pargv, const char *str)
204
while (is_space(*str))
209
while (*str != '\0' && !is_space(*str))
212
arg = tcc_malloc(len + 1);
213
memcpy(arg, s1, len);
215
dynarray_add((void ***)&argv, &argc, arg);
221
75
/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
222
76
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
224
78
#include <process.h>
225
79
static int execvp_win32(const char *prog, char **argv)
227
int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv);
81
int ret = spawnvp(P_NOWAIT, prog, (const char *const*)argv);
230
84
cwait(&ret, ret, WAIT_CHILD);
262
116
tcc_warning("unsupported option \"-m%s\"", optarg);
267
static void parse_option_D(TCCState *s1, const char *optarg)
269
char *sym = tcc_strdup(optarg);
270
char *value = strchr(sym, '=');
273
tcc_define_symbol(s1, sym, value);
277
static int parse_args(TCCState *s, int argc, char **argv)
280
const TCCOption *popt;
281
const char *optarg, *p1, *r1;
285
was_pthread = 0; /* is set if commandline contains -pthread key */
287
cstr_new(&linker_arg);
289
while (optind < argc) {
292
if (r[0] != '-' || r[1] == '\0') {
294
dynarray_add((void ***)&files, &nb_files, r);
295
if (!multiple_files) {
297
/* argv[0] will be this file */
301
/* find option in table (match only the first chars */
306
tcc_error("invalid option -- '%s'", r);
319
if (popt->flags & TCC_OPTION_HAS_ARG) {
320
if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
324
tcc_error("argument to '%s' is missing", r);
325
optarg = argv[optind++];
333
switch(popt->index) {
334
case TCC_OPTION_HELP:
338
if (tcc_add_include_path(s, optarg) < 0)
339
tcc_error("too many include paths");
342
parse_option_D(s, optarg);
345
tcc_undefine_symbol(s, optarg);
348
tcc_add_library_path(s, optarg);
351
/* set tcc utilities path (mainly for tcc development) */
352
tcc_set_lib_path(s, optarg);
355
dynarray_add((void ***)&files, &nb_files, r);
358
case TCC_OPTION_pthread:
360
parse_option_D(s, "_REENTRANT");
362
case TCC_OPTION_bench:
365
#ifdef CONFIG_TCC_BACKTRACE
367
tcc_set_num_callers(atoi(optarg));
370
#ifdef CONFIG_TCC_BCHECK
372
s->do_bounds_check = 1;
381
output_type = TCC_OUTPUT_OBJ;
383
case TCC_OPTION_static:
386
case TCC_OPTION_shared:
387
output_type = TCC_OUTPUT_DLL;
389
case TCC_OPTION_soname:
397
outfile = tcc_strdup(optarg);
400
/* generate a .o merging several output files */
402
output_type = TCC_OUTPUT_OBJ;
404
case TCC_OPTION_isystem:
405
tcc_add_sysinclude_path(s, optarg);
407
case TCC_OPTION_nostdinc:
410
case TCC_OPTION_nostdlib:
413
case TCC_OPTION_print_search_dirs:
414
print_search_dirs = 1;
420
argc1 = expand_args(&argv1, optarg);
422
parse_args(s, argc1, argv1);
425
output_type = TCC_OUTPUT_MEMORY;
429
do ++s->verbose; while (*optarg++ == 'v');
432
if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
433
goto unsupported_option;
436
if (tcc_set_warning(s, optarg, 1) < 0 &&
438
goto unsupported_option;
443
case TCC_OPTION_rdynamic:
448
--linker_arg.size, cstr_ccat(&linker_arg, ',');
449
cstr_cat(&linker_arg, optarg);
450
cstr_ccat(&linker_arg, '\0');
453
output_type = TCC_OUTPUT_PREPROCESS;
459
deps_outfile = optarg;
464
if (s->warn_unsupported) {
466
tcc_warning("unsupported option '%s'", r);
472
if (NULL != (r1 = tcc_set_linker(s, (char *) linker_arg.data, TRUE)))
473
tcc_error("unsupported linker option '%s'", r1);
474
/* fixme: these options could be different on your platform */
475
if (was_pthread && output_type != TCC_OUTPUT_OBJ) {
476
dynarray_add((void ***)&files, &nb_files, "-lpthread");
120
#define exec_other_tcc(s, argv, optarg)
123
static void gen_makedeps(TCCState *s, const char *target, const char *filename)
126
char buf[1024], *ext;
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");
139
printf("<- %s\n", filename);
141
/* XXX return err codes instead of error() ? */
142
depout = fopen(filename, "w");
144
tcc_error("could not open '%s'", filename);
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");
153
static char *default_outputfile(TCCState *s, const char *first_file)
157
const char *name = "a";
159
if (first_file && strcmp(first_file, "-"))
160
name = tcc_basename(first_file);
161
pstrcpy(buf, sizeof(buf), name);
162
ext = tcc_fileextension(buf);
164
if (s->output_type == TCC_OUTPUT_DLL)
167
if (s->output_type == TCC_OUTPUT_EXE)
171
if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
172
(s->output_type == TCC_OUTPUT_PREPROCESS) )
176
strcpy(buf, "a.out");
178
return tcc_strdup(buf);
181
static void print_paths(const char *msg, char **paths, int nb_paths)
184
printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
185
for(i = 0; i < nb_paths; i++)
186
printf(" %s\n", paths[i]);
189
static void display_info(TCCState *s, int what)
193
printf("tcc version %s ("
194
#ifdef TCC_TARGET_I386
196
# ifdef TCC_TARGET_PE
199
#elif defined TCC_TARGET_X86_64
201
# ifdef TCC_TARGET_PE
204
#elif defined TCC_TARGET_ARM
206
# ifdef TCC_ARM_HARDFLOAT
209
# ifdef TCC_TARGET_PE
213
#ifndef TCC_TARGET_PE
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", DEFAULT_ELFINTERP(s));
231
static int64_t getclock_us(void)
236
return (tb.time * 1000LL + tb.millitm) * 1000LL;
239
gettimeofday(&tv, NULL);
240
return tv.tv_sec * 1000000LL + tv.tv_usec;
482
244
int main(int argc, char **argv)
486
int nb_objfiles, ret, optind;
247
int ret, optind, i, bench;
487
248
int64_t start_time = 0;
488
const char *default_file = NULL;
249
const char *first_file = NULL;
492
output_type = TCC_OUTPUT_EXE;
499
print_search_dirs = 0;
503
optind = parse_args(s, argc - 1, argv + 1);
505
#if defined TCC_TARGET_X86_64 || defined TCC_TARGET_I386
507
exec_other_tcc(s, argv, m_option);
510
if (print_search_dirs) {
511
/* enough for Linux kernel */
512
printf("install: %s/\n", s->tcc_lib_path);
517
printf("tcc version %s\n", TCC_VERSION);
519
if (optind == 0 || nb_files == 0) {
520
if (optind && s->verbose)
252
s->output_type = TCC_OUTPUT_EXE;
254
optind = tcc_parse_args(s, argc - 1, argv + 1);
255
tcc_set_environment(s);
526
nb_objfiles = nb_files - nb_libraries;
528
/* if outfile provided without other options, we output an
530
if (outfile && output_type == TCC_OUTPUT_MEMORY)
531
output_type = TCC_OUTPUT_EXE;
263
exec_other_tcc(s, argv, s->option_m);
268
if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
269
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
274
if (s->verbose && optind == 1)
277
if (s->nb_files == 0)
278
tcc_error("no input files\n");
533
280
/* check -c consistency : only single file handled. XXX: checks file type */
534
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
281
if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
282
if (s->nb_libraries != 0)
283
tcc_error("cannot specify libraries with -c");
535
284
/* accepts only a single input file */
536
if (nb_objfiles != 1)
285
if (s->nb_files != 1)
537
286
tcc_error("cannot specify multiple files with -c");
538
if (nb_libraries != 0)
539
tcc_error("cannot specify libraries with -c");
542
if (output_type == TCC_OUTPUT_PREPROCESS) {
289
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
546
s->outfile = fopen(outfile, "w");
548
tcc_error("could not open '%s'", outfile);
293
s->ppfp = fopen(s->outfile, "w");
295
tcc_error("could not write '%s'", s->outfile);
553
301
start_time = getclock_us();
556
tcc_set_output_type(s, output_type);
557
s->reloc_output = reloc_output;
303
tcc_set_output_type(s, s->output_type);
559
305
/* compile or add each files or library */
560
for(i = 0; i < nb_files && ret == 0; i++) {
306
for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
561
307
const char *filename;
309
filename = s->files[i];
564
310
if (filename[0] == '-' && filename[1] == 'l') {
565
311
if (tcc_add_library(s, filename + 2) < 0) {
566
tcc_error_noabort("cannot find %s", filename);
312
tcc_error_noabort("cannot find '%s'", filename);