2001-11-01 22:48:10 +08:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
|
|
|
*
|
2004-10-28 05:38:03 +08:00
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
2001-11-01 22:48:10 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
2001-11-01 22:48:10 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
2001-11-01 22:48:10 +08:00
|
|
|
*
|
2003-05-24 22:11:17 +08:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2001-11-01 22:48:10 +08:00
|
|
|
*/
|
2003-04-14 03:49:30 +08:00
|
|
|
|
2009-07-19 04:08:01 +08:00
|
|
|
#ifdef TCC_USE_LIBTCC
|
|
|
|
#include "tcc.h"
|
|
|
|
#else
|
2009-05-06 02:18:53 +08:00
|
|
|
#include "libtcc.c"
|
2009-07-19 04:08:01 +08:00
|
|
|
#endif
|
2009-04-18 19:17:27 +08:00
|
|
|
|
2002-05-14 07:00:17 +08:00
|
|
|
void help(void)
|
|
|
|
{
|
2006-10-17 03:44:00 +08:00
|
|
|
printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
|
2003-04-14 05:54:03 +08:00
|
|
|
"usage: tcc [-v] [-c] [-o outfile] [-Bdir] [-bench] [-Idir] [-Dsym[=val]] [-Usym]\n"
|
2008-03-26 05:04:47 +08:00
|
|
|
" [-Wwarn] [-g] [-b] [-bt N] [-Ldir] [-llib] [-shared] [-soname name]\n"
|
|
|
|
" [-static] [infile1 infile2...] [-run infile args...]\n"
|
2002-05-14 07:00:17 +08:00
|
|
|
"\n"
|
2002-07-16 06:17:02 +08:00
|
|
|
"General options:\n"
|
2008-04-01 03:49:14 +08:00
|
|
|
" -v display current version, increase verbosity\n"
|
2002-07-16 06:17:02 +08:00
|
|
|
" -c compile only - generate an object file\n"
|
2002-07-25 06:10:59 +08:00
|
|
|
" -o outfile set output filename\n"
|
|
|
|
" -Bdir set tcc internal library path\n"
|
|
|
|
" -bench output compilation statistics\n"
|
2007-11-15 01:34:30 +08:00
|
|
|
" -run run compiled source\n"
|
2003-10-18 04:43:47 +08:00
|
|
|
" -fflag set or reset (with 'no-' prefix) 'flag' (see man page)\n"
|
|
|
|
" -Wwarning set or reset (with 'no-' prefix) 'warning' (see man page)\n"
|
2003-09-20 04:35:50 +08:00
|
|
|
" -w disable all warnings\n"
|
2002-07-16 06:17:02 +08:00
|
|
|
"Preprocessor options:\n"
|
2006-10-17 03:44:00 +08:00
|
|
|
" -E preprocess only\n"
|
2002-07-16 06:17:02 +08:00
|
|
|
" -Idir add include path 'dir'\n"
|
|
|
|
" -Dsym[=val] define 'sym' with value 'val'\n"
|
|
|
|
" -Usym undefine 'sym'\n"
|
|
|
|
"Linker options:\n"
|
2002-07-22 08:20:38 +08:00
|
|
|
" -Ldir add library path 'dir'\n"
|
2002-08-18 21:22:55 +08:00
|
|
|
" -llib link with dynamic or static library 'lib'\n"
|
2002-07-28 07:08:04 +08:00
|
|
|
" -shared generate a shared library\n"
|
2008-03-26 05:04:47 +08:00
|
|
|
" -soname set name for shared library to be used at runtime\n"
|
2002-07-25 06:10:59 +08:00
|
|
|
" -static static linking\n"
|
2003-05-25 00:21:09 +08:00
|
|
|
" -rdynamic export all global symbols to dynamic linker\n"
|
2007-12-20 01:36:42 +08:00
|
|
|
" -r generate (relocatable) object file\n"
|
2002-11-03 08:43:55 +08:00
|
|
|
"Debugger options:\n"
|
|
|
|
" -g generate runtime debug info\n"
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
" -b compile with built-in memory and bounds checker (implies -g)\n"
|
|
|
|
#endif
|
2009-04-19 00:21:38 +08:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2002-11-03 08:43:55 +08:00
|
|
|
" -bt N show N callers in stack traces\n"
|
2009-04-19 00:21:38 +08:00
|
|
|
#endif
|
2002-05-14 07:00:17 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2009-05-06 02:30:39 +08:00
|
|
|
static char **files;
|
|
|
|
static int nb_files, nb_libraries;
|
|
|
|
static int multiple_files;
|
|
|
|
static int print_search_dirs;
|
|
|
|
static int output_type;
|
|
|
|
static int reloc_output;
|
|
|
|
static const char *outfile;
|
|
|
|
static int do_bench = 0;
|
|
|
|
|
2003-01-07 04:21:08 +08:00
|
|
|
#define TCC_OPTION_HAS_ARG 0x0001
|
|
|
|
#define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
|
|
|
|
|
|
|
|
typedef struct TCCOption {
|
|
|
|
const char *name;
|
|
|
|
uint16_t index;
|
|
|
|
uint16_t flags;
|
|
|
|
} TCCOption;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
TCC_OPTION_HELP,
|
|
|
|
TCC_OPTION_I,
|
|
|
|
TCC_OPTION_D,
|
|
|
|
TCC_OPTION_U,
|
|
|
|
TCC_OPTION_L,
|
|
|
|
TCC_OPTION_B,
|
|
|
|
TCC_OPTION_l,
|
|
|
|
TCC_OPTION_bench,
|
|
|
|
TCC_OPTION_bt,
|
|
|
|
TCC_OPTION_b,
|
|
|
|
TCC_OPTION_g,
|
|
|
|
TCC_OPTION_c,
|
|
|
|
TCC_OPTION_static,
|
|
|
|
TCC_OPTION_shared,
|
2008-03-26 05:04:47 +08:00
|
|
|
TCC_OPTION_soname,
|
2003-01-07 04:21:08 +08:00
|
|
|
TCC_OPTION_o,
|
|
|
|
TCC_OPTION_r,
|
2004-10-18 08:18:20 +08:00
|
|
|
TCC_OPTION_Wl,
|
2003-01-07 04:21:08 +08:00
|
|
|
TCC_OPTION_W,
|
|
|
|
TCC_OPTION_O,
|
|
|
|
TCC_OPTION_m,
|
|
|
|
TCC_OPTION_f,
|
|
|
|
TCC_OPTION_nostdinc,
|
2003-04-27 19:45:01 +08:00
|
|
|
TCC_OPTION_nostdlib,
|
2003-01-07 04:21:08 +08:00
|
|
|
TCC_OPTION_print_search_dirs,
|
|
|
|
TCC_OPTION_rdynamic,
|
2003-04-14 05:54:03 +08:00
|
|
|
TCC_OPTION_run,
|
|
|
|
TCC_OPTION_v,
|
2003-09-20 04:35:50 +08:00
|
|
|
TCC_OPTION_w,
|
2004-12-17 06:19:28 +08:00
|
|
|
TCC_OPTION_pipe,
|
2006-10-17 03:44:00 +08:00
|
|
|
TCC_OPTION_E,
|
2009-06-17 08:11:27 +08:00
|
|
|
TCC_OPTION_x,
|
2003-01-07 04:21:08 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static const TCCOption tcc_options[] = {
|
|
|
|
{ "h", TCC_OPTION_HELP, 0 },
|
|
|
|
{ "?", TCC_OPTION_HELP, 0 },
|
|
|
|
{ "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "l", TCC_OPTION_l, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "bench", TCC_OPTION_bench, 0 },
|
|
|
|
{ "bt", TCC_OPTION_bt, TCC_OPTION_HAS_ARG },
|
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
|
|
|
{ "b", TCC_OPTION_b, 0 },
|
|
|
|
#endif
|
2004-12-17 06:19:28 +08:00
|
|
|
{ "g", TCC_OPTION_g, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ "c", TCC_OPTION_c, 0 },
|
|
|
|
{ "static", TCC_OPTION_static, 0 },
|
|
|
|
{ "shared", TCC_OPTION_shared, 0 },
|
2008-03-26 05:04:47 +08:00
|
|
|
{ "soname", TCC_OPTION_soname, TCC_OPTION_HAS_ARG },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ "o", TCC_OPTION_o, TCC_OPTION_HAS_ARG },
|
2003-10-05 05:23:51 +08:00
|
|
|
{ "run", TCC_OPTION_run, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2003-05-24 23:46:29 +08:00
|
|
|
{ "rdynamic", TCC_OPTION_rdynamic, 0 },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ "r", TCC_OPTION_r, 0 },
|
2004-10-18 08:18:20 +08:00
|
|
|
{ "Wl,", TCC_OPTION_Wl, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ "W", TCC_OPTION_W, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "O", TCC_OPTION_O, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "m", TCC_OPTION_m, TCC_OPTION_HAS_ARG },
|
|
|
|
{ "f", TCC_OPTION_f, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
|
|
|
{ "nostdinc", TCC_OPTION_nostdinc, 0 },
|
2003-04-27 19:45:01 +08:00
|
|
|
{ "nostdlib", TCC_OPTION_nostdlib, 0 },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ "print-search-dirs", TCC_OPTION_print_search_dirs, 0 },
|
2008-04-01 03:49:14 +08:00
|
|
|
{ "v", TCC_OPTION_v, TCC_OPTION_HAS_ARG | TCC_OPTION_NOSEP },
|
2003-09-20 04:35:50 +08:00
|
|
|
{ "w", TCC_OPTION_w, 0 },
|
2004-12-17 06:19:28 +08:00
|
|
|
{ "pipe", TCC_OPTION_pipe, 0},
|
2006-10-17 03:44:00 +08:00
|
|
|
{ "E", TCC_OPTION_E, 0},
|
2009-06-17 08:11:27 +08:00
|
|
|
{ "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
|
2003-01-07 04:21:08 +08:00
|
|
|
{ NULL },
|
|
|
|
};
|
|
|
|
|
2009-05-06 02:30:39 +08:00
|
|
|
static int64_t getclock_us(void)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
struct _timeb tb;
|
|
|
|
_ftime(&tb);
|
|
|
|
return (tb.time * 1000LL + tb.millitm) * 1000LL;
|
|
|
|
#else
|
|
|
|
struct timeval tv;
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
return tv.tv_sec * 1000000LL + tv.tv_usec;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static int strstart(const char *str, const char *val, const char **ptr)
|
|
|
|
{
|
|
|
|
const char *p, *q;
|
|
|
|
p = str;
|
|
|
|
q = val;
|
|
|
|
while (*q != '\0') {
|
|
|
|
if (*p != *q)
|
|
|
|
return 0;
|
|
|
|
p++;
|
|
|
|
q++;
|
|
|
|
}
|
|
|
|
if (ptr)
|
|
|
|
*ptr = p;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-10-05 05:23:51 +08:00
|
|
|
/* convert 'str' into an array of space separated strings */
|
|
|
|
static int expand_args(char ***pargv, const char *str)
|
2002-05-14 07:00:17 +08:00
|
|
|
{
|
2003-10-05 05:23:51 +08:00
|
|
|
const char *s1;
|
|
|
|
char **argv, *arg;
|
|
|
|
int argc, len;
|
2003-01-07 04:21:08 +08:00
|
|
|
|
2003-10-05 05:23:51 +08:00
|
|
|
argc = 0;
|
|
|
|
argv = NULL;
|
|
|
|
for(;;) {
|
|
|
|
while (is_space(*str))
|
|
|
|
str++;
|
|
|
|
if (*str == '\0')
|
|
|
|
break;
|
|
|
|
s1 = str;
|
|
|
|
while (*str != '\0' && !is_space(*str))
|
|
|
|
str++;
|
|
|
|
len = str - s1;
|
|
|
|
arg = tcc_malloc(len + 1);
|
|
|
|
memcpy(arg, s1, len);
|
|
|
|
arg[len] = '\0';
|
|
|
|
dynarray_add((void ***)&argv, &argc, arg);
|
|
|
|
}
|
|
|
|
*pargv = argv;
|
|
|
|
return argc;
|
|
|
|
}
|
|
|
|
|
|
|
|
int parse_args(TCCState *s, int argc, char **argv)
|
|
|
|
{
|
|
|
|
int optind;
|
|
|
|
const TCCOption *popt;
|
|
|
|
const char *optarg, *p1, *r1;
|
|
|
|
char *r;
|
2002-05-14 07:00:17 +08:00
|
|
|
|
2003-10-05 05:23:51 +08:00
|
|
|
optind = 0;
|
2008-04-28 02:50:35 +08:00
|
|
|
while (optind < argc) {
|
|
|
|
|
2002-08-31 06:46:35 +08:00
|
|
|
r = argv[optind++];
|
2008-04-01 02:42:56 +08:00
|
|
|
if (r[0] != '-' || r[1] == '\0') {
|
2002-08-31 06:46:35 +08:00
|
|
|
/* add a new file */
|
|
|
|
dynarray_add((void ***)&files, &nb_files, r);
|
|
|
|
if (!multiple_files) {
|
|
|
|
optind--;
|
|
|
|
/* argv[0] will be this file */
|
|
|
|
break;
|
|
|
|
}
|
2003-01-07 04:21:08 +08:00
|
|
|
} else {
|
|
|
|
/* find option in table (match only the first chars */
|
|
|
|
popt = tcc_options;
|
|
|
|
for(;;) {
|
|
|
|
p1 = popt->name;
|
|
|
|
if (p1 == NULL)
|
|
|
|
error("invalid option -- '%s'", r);
|
|
|
|
r1 = r + 1;
|
|
|
|
for(;;) {
|
|
|
|
if (*p1 == '\0')
|
|
|
|
goto option_found;
|
|
|
|
if (*r1 != *p1)
|
|
|
|
break;
|
|
|
|
p1++;
|
|
|
|
r1++;
|
|
|
|
}
|
|
|
|
popt++;
|
2002-08-31 06:46:35 +08:00
|
|
|
}
|
2003-01-07 04:21:08 +08:00
|
|
|
option_found:
|
|
|
|
if (popt->flags & TCC_OPTION_HAS_ARG) {
|
|
|
|
if (*r1 != '\0' || (popt->flags & TCC_OPTION_NOSEP)) {
|
|
|
|
optarg = r1;
|
|
|
|
} else {
|
|
|
|
if (optind >= argc)
|
|
|
|
error("argument to '%s' is missing", r);
|
|
|
|
optarg = argv[optind++];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (*r1 != '\0')
|
2008-04-28 02:50:35 +08:00
|
|
|
return 0;
|
2003-01-07 04:21:08 +08:00
|
|
|
optarg = NULL;
|
2002-05-14 07:00:17 +08:00
|
|
|
}
|
2003-01-07 04:21:08 +08:00
|
|
|
|
|
|
|
switch(popt->index) {
|
|
|
|
case TCC_OPTION_HELP:
|
2008-04-28 02:50:35 +08:00
|
|
|
return 0;
|
|
|
|
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_I:
|
|
|
|
if (tcc_add_include_path(s, optarg) < 0)
|
|
|
|
error("too many include paths");
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_D:
|
|
|
|
{
|
|
|
|
char *sym, *value;
|
|
|
|
sym = (char *)optarg;
|
|
|
|
value = strchr(sym, '=');
|
|
|
|
if (value) {
|
|
|
|
*value = '\0';
|
|
|
|
value++;
|
|
|
|
}
|
|
|
|
tcc_define_symbol(s, sym, value);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_U:
|
|
|
|
tcc_undefine_symbol(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_L:
|
|
|
|
tcc_add_library_path(s, optarg);
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_B:
|
|
|
|
/* set tcc utilities path (mainly for tcc development) */
|
2009-04-18 19:17:27 +08:00
|
|
|
tcc_set_lib_path(s, optarg);
|
2003-01-07 04:21:08 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_l:
|
|
|
|
dynarray_add((void ***)&files, &nb_files, r);
|
|
|
|
nb_libraries++;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_bench:
|
|
|
|
do_bench = 1;
|
|
|
|
break;
|
2009-04-19 00:21:38 +08:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_bt:
|
|
|
|
num_callers = atoi(optarg);
|
|
|
|
break;
|
2009-04-19 00:21:38 +08:00
|
|
|
#endif
|
2002-03-04 06:45:55 +08:00
|
|
|
#ifdef CONFIG_TCC_BCHECK
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_b:
|
2009-05-12 00:45:44 +08:00
|
|
|
s->do_bounds_check = 1;
|
|
|
|
s->do_debug = 1;
|
2003-01-07 04:21:08 +08:00
|
|
|
break;
|
2002-07-27 23:38:21 +08:00
|
|
|
#endif
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_g:
|
2009-05-12 00:45:44 +08:00
|
|
|
s->do_debug = 1;
|
2003-01-07 04:21:08 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_c:
|
|
|
|
multiple_files = 1;
|
|
|
|
output_type = TCC_OUTPUT_OBJ;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_static:
|
|
|
|
s->static_link = 1;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_shared:
|
|
|
|
output_type = TCC_OUTPUT_DLL;
|
|
|
|
break;
|
2008-03-26 05:04:47 +08:00
|
|
|
case TCC_OPTION_soname:
|
|
|
|
s->soname = optarg;
|
|
|
|
break;
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_o:
|
|
|
|
multiple_files = 1;
|
|
|
|
outfile = optarg;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_r:
|
|
|
|
/* generate a .o merging several output files */
|
|
|
|
reloc_output = 1;
|
|
|
|
output_type = TCC_OUTPUT_OBJ;
|
|
|
|
break;
|
|
|
|
case TCC_OPTION_nostdinc:
|
|
|
|
s->nostdinc = 1;
|
|
|
|
break;
|
2003-04-27 19:45:01 +08:00
|
|
|
case TCC_OPTION_nostdlib:
|
|
|
|
s->nostdlib = 1;
|
|
|
|
break;
|
2003-01-07 04:21:08 +08:00
|
|
|
case TCC_OPTION_print_search_dirs:
|
|
|
|
print_search_dirs = 1;
|
|
|
|
break;
|
2003-04-14 05:54:03 +08:00
|
|
|
case TCC_OPTION_run:
|
2003-10-05 05:23:51 +08:00
|
|
|
{
|
|
|
|
int argc1;
|
|
|
|
char **argv1;
|
|
|
|
argc1 = expand_args(&argv1, optarg);
|
|
|
|
if (argc1 > 0) {
|
|
|
|
parse_args(s, argc1, argv1);
|
|
|
|
}
|
|
|
|
multiple_files = 0;
|
|
|
|
output_type = TCC_OUTPUT_MEMORY;
|
|
|
|
}
|
2003-04-14 05:54:03 +08:00
|
|
|
break;
|
|
|
|
case TCC_OPTION_v:
|
2008-04-01 03:49:14 +08:00
|
|
|
do {
|
2009-05-12 00:45:44 +08:00
|
|
|
if (0 == s->verbose++)
|
2008-04-01 03:49:14 +08:00
|
|
|
printf("tcc version %s\n", TCC_VERSION);
|
|
|
|
} while (*optarg++ == 'v');
|
|
|
|
break;
|
2003-10-18 04:43:47 +08:00
|
|
|
case TCC_OPTION_f:
|
|
|
|
if (tcc_set_flag(s, optarg, 1) < 0 && s->warn_unsupported)
|
|
|
|
goto unsupported_option;
|
|
|
|
break;
|
2003-04-27 04:49:46 +08:00
|
|
|
case TCC_OPTION_W:
|
2003-10-18 04:43:47 +08:00
|
|
|
if (tcc_set_warning(s, optarg, 1) < 0 &&
|
|
|
|
s->warn_unsupported)
|
|
|
|
goto unsupported_option;
|
2003-04-27 04:49:46 +08:00
|
|
|
break;
|
2003-09-20 04:35:50 +08:00
|
|
|
case TCC_OPTION_w:
|
2003-10-04 21:04:47 +08:00
|
|
|
s->warn_none = 1;
|
2003-09-20 04:35:50 +08:00
|
|
|
break;
|
2003-05-24 23:46:29 +08:00
|
|
|
case TCC_OPTION_rdynamic:
|
|
|
|
s->rdynamic = 1;
|
|
|
|
break;
|
2004-10-18 08:18:20 +08:00
|
|
|
case TCC_OPTION_Wl:
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
if (strstart(optarg, "-Ttext,", &p)) {
|
|
|
|
s->text_addr = strtoul(p, NULL, 16);
|
|
|
|
s->has_text_addr = 1;
|
2009-08-27 16:12:13 +08:00
|
|
|
} else if (strstart(optarg, "--section-alignment,", &p)) {
|
|
|
|
s->section_align = strtoul(p, NULL, 16);
|
|
|
|
} else if (strstart(optarg, "--image-base,", &p)) {
|
|
|
|
s->text_addr = strtoul(p, NULL, 16);
|
|
|
|
s->has_text_addr = 1;
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
} else if (strstart(optarg, "--file-alignment,", &p)) {
|
|
|
|
s->pe_file_align = strtoul(p, NULL, 16);
|
|
|
|
} else if (strstart(optarg, "--subsystem,", &p)) {
|
|
|
|
#if defined(TCC_TARGET_I386) || defined(TCC_TARGET_X86_64)
|
|
|
|
if (!strcmp(p, "native"))
|
|
|
|
s->pe_subsystem = 1;
|
|
|
|
else if (!strcmp(p, "console"))
|
|
|
|
s->pe_subsystem = 3;
|
|
|
|
else if (!strcmp(p, "gui"))
|
|
|
|
s->pe_subsystem = 2;
|
|
|
|
else if (!strcmp(p, "posix"))
|
|
|
|
s->pe_subsystem = 7;
|
|
|
|
else if (!strcmp(p, "efiapp"))
|
|
|
|
s->pe_subsystem = 10;
|
|
|
|
else if (!strcmp(p, "efiboot"))
|
|
|
|
s->pe_subsystem = 11;
|
|
|
|
else if (!strcmp(p, "efiruntime"))
|
|
|
|
s->pe_subsystem = 12;
|
|
|
|
else if (!strcmp(p, "efirom"))
|
|
|
|
s->pe_subsystem = 13;
|
2009-08-27 16:25:28 +08:00
|
|
|
#elif defined(TCC_TARGET_ARM)
|
|
|
|
if (!strcmp(p, "wince"))
|
|
|
|
s->pe_subsystem = 9;
|
2009-08-27 16:12:13 +08:00
|
|
|
#endif
|
|
|
|
else {
|
|
|
|
error("invalid subsystem '%s'", p);
|
|
|
|
}
|
|
|
|
#endif
|
2004-10-24 06:52:05 +08:00
|
|
|
} else if (strstart(optarg, "--oformat,", &p)) {
|
2009-08-27 16:12:13 +08:00
|
|
|
#if defined(TCC_TARGET_PE)
|
|
|
|
if (strstart(p, "pe-", NULL)) {
|
|
|
|
#else
|
|
|
|
#if defined(TCC_TARGET_X86_64)
|
|
|
|
if (strstart(p, "elf64-", NULL)) {
|
|
|
|
#else
|
2004-10-24 06:52:05 +08:00
|
|
|
if (strstart(p, "elf32-", NULL)) {
|
2009-08-27 16:12:13 +08:00
|
|
|
#endif
|
|
|
|
#endif
|
2004-10-24 06:52:05 +08:00
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_ELF;
|
|
|
|
} else if (!strcmp(p, "binary")) {
|
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_BINARY;
|
|
|
|
} else
|
|
|
|
#ifdef TCC_TARGET_COFF
|
|
|
|
if (!strcmp(p, "coff")) {
|
|
|
|
s->output_format = TCC_OUTPUT_FORMAT_COFF;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
error("target %s not found", p);
|
|
|
|
}
|
2009-12-20 05:08:15 +08:00
|
|
|
} else if (strstart(optarg, "-rpath=", &p)) {
|
|
|
|
s->rpath = p;
|
2004-10-18 08:18:20 +08:00
|
|
|
} else {
|
2004-10-24 06:52:05 +08:00
|
|
|
error("unsupported linker option '%s'", optarg);
|
2004-10-18 08:18:20 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2006-10-17 03:44:00 +08:00
|
|
|
case TCC_OPTION_E:
|
|
|
|
output_type = TCC_OUTPUT_PREPROCESS;
|
|
|
|
break;
|
2009-06-17 08:11:27 +08:00
|
|
|
case TCC_OPTION_x:
|
|
|
|
break;
|
2003-01-07 04:21:08 +08:00
|
|
|
default:
|
2003-04-27 04:49:46 +08:00
|
|
|
if (s->warn_unsupported) {
|
|
|
|
unsupported_option:
|
|
|
|
warning("unsupported option '%s'", r);
|
|
|
|
}
|
2003-01-07 04:21:08 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-11-11 10:53:01 +08:00
|
|
|
}
|
|
|
|
}
|
2008-04-28 02:50:35 +08:00
|
|
|
return optind + 1;
|
2003-10-05 05:23:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
TCCState *s;
|
|
|
|
int nb_objfiles, ret, optind;
|
|
|
|
char objfilename[1024];
|
|
|
|
int64_t start_time = 0;
|
|
|
|
|
2009-04-18 19:17:27 +08:00
|
|
|
s = tcc_new();
|
2009-07-19 04:07:33 +08:00
|
|
|
|
2003-10-05 05:23:51 +08:00
|
|
|
output_type = TCC_OUTPUT_EXE;
|
|
|
|
outfile = NULL;
|
|
|
|
multiple_files = 1;
|
|
|
|
files = NULL;
|
|
|
|
nb_files = 0;
|
|
|
|
nb_libraries = 0;
|
|
|
|
reloc_output = 0;
|
|
|
|
print_search_dirs = 0;
|
2007-12-20 01:36:42 +08:00
|
|
|
ret = 0;
|
2003-10-05 05:23:51 +08:00
|
|
|
|
2008-04-28 02:50:35 +08:00
|
|
|
optind = parse_args(s, argc - 1, argv + 1);
|
2003-01-07 04:21:08 +08:00
|
|
|
if (print_search_dirs) {
|
|
|
|
/* enough for Linux kernel */
|
2009-05-12 00:45:44 +08:00
|
|
|
printf("install: %s/\n", s->tcc_lib_path);
|
2003-01-07 04:21:08 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2008-04-28 02:50:35 +08:00
|
|
|
if (optind == 0 || nb_files == 0) {
|
2009-05-12 00:45:44 +08:00
|
|
|
if (optind && s->verbose)
|
2008-04-28 02:50:35 +08:00
|
|
|
return 0;
|
|
|
|
help();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-08-31 06:46:35 +08:00
|
|
|
nb_objfiles = nb_files - nb_libraries;
|
|
|
|
|
2002-07-22 08:20:38 +08:00
|
|
|
/* if outfile provided without other options, we output an
|
|
|
|
executable */
|
|
|
|
if (outfile && output_type == TCC_OUTPUT_MEMORY)
|
|
|
|
output_type = TCC_OUTPUT_EXE;
|
2002-07-18 08:51:11 +08:00
|
|
|
|
2002-08-31 06:46:35 +08:00
|
|
|
/* check -c consistency : only single file handled. XXX: checks file type */
|
|
|
|
if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
|
|
|
|
/* accepts only a single input file */
|
|
|
|
if (nb_objfiles != 1)
|
|
|
|
error("cannot specify multiple files with -c");
|
|
|
|
if (nb_libraries != 0)
|
|
|
|
error("cannot specify libraries with -c");
|
|
|
|
}
|
|
|
|
|
2006-10-17 03:44:00 +08:00
|
|
|
|
|
|
|
if (output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
if (!outfile) {
|
|
|
|
s->outfile = stdout;
|
|
|
|
} else {
|
2007-12-20 01:36:42 +08:00
|
|
|
s->outfile = fopen(outfile, "w");
|
2006-10-17 03:44:00 +08:00
|
|
|
if (!s->outfile)
|
|
|
|
error("could not open '%s", outfile);
|
|
|
|
}
|
|
|
|
} else if (output_type != TCC_OUTPUT_MEMORY) {
|
2005-04-17 21:15:54 +08:00
|
|
|
if (!outfile) {
|
2007-12-14 03:07:19 +08:00
|
|
|
/* compute default outfile name */
|
2007-12-20 01:36:42 +08:00
|
|
|
char *ext;
|
2008-04-01 02:42:56 +08:00
|
|
|
const char *name =
|
|
|
|
strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
|
|
|
|
pstrcpy(objfilename, sizeof(objfilename), name);
|
2007-12-20 01:36:42 +08:00
|
|
|
ext = tcc_fileextension(objfilename);
|
2005-04-17 21:15:54 +08:00
|
|
|
#ifdef TCC_TARGET_PE
|
2007-12-20 01:36:42 +08:00
|
|
|
if (output_type == TCC_OUTPUT_DLL)
|
|
|
|
strcpy(ext, ".dll");
|
|
|
|
else
|
|
|
|
if (output_type == TCC_OUTPUT_EXE)
|
|
|
|
strcpy(ext, ".exe");
|
|
|
|
else
|
2005-04-17 21:15:54 +08:00
|
|
|
#endif
|
2007-12-20 01:36:42 +08:00
|
|
|
if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
|
|
|
|
strcpy(ext, ".o");
|
|
|
|
else
|
|
|
|
pstrcpy(objfilename, sizeof(objfilename), "a.out");
|
2007-11-15 01:34:30 +08:00
|
|
|
outfile = objfilename;
|
2005-04-17 21:15:54 +08:00
|
|
|
}
|
2002-07-16 06:17:02 +08:00
|
|
|
}
|
2001-12-08 23:04:01 +08:00
|
|
|
|
2002-11-19 05:46:44 +08:00
|
|
|
if (do_bench) {
|
|
|
|
start_time = getclock_us();
|
|
|
|
}
|
|
|
|
|
2002-08-31 06:46:35 +08:00
|
|
|
tcc_set_output_type(s, output_type);
|
|
|
|
|
|
|
|
/* compile or add each files or library */
|
2007-12-20 01:36:42 +08:00
|
|
|
for(i = 0; i < nb_files && ret == 0; i++) {
|
2002-08-31 06:46:35 +08:00
|
|
|
const char *filename;
|
|
|
|
|
|
|
|
filename = files[i];
|
2009-05-12 00:46:02 +08:00
|
|
|
if (filename[0] == '-' && filename[1]) {
|
|
|
|
if (tcc_add_library(s, filename + 2) < 0) {
|
|
|
|
error_noabort("cannot find %s", filename);
|
2007-12-20 01:36:42 +08:00
|
|
|
ret = 1;
|
2009-05-12 00:46:02 +08:00
|
|
|
}
|
2002-08-31 06:46:35 +08:00
|
|
|
} else {
|
2009-05-12 00:45:44 +08:00
|
|
|
if (1 == s->verbose)
|
2008-04-01 03:49:14 +08:00
|
|
|
printf("-> %s\n", filename);
|
2007-12-20 01:36:42 +08:00
|
|
|
if (tcc_add_file(s, filename) < 0)
|
|
|
|
ret = 1;
|
2002-08-31 06:46:35 +08:00
|
|
|
}
|
2002-07-18 08:51:11 +08:00
|
|
|
}
|
|
|
|
|
2002-11-02 22:12:32 +08:00
|
|
|
/* free all files */
|
|
|
|
tcc_free(files);
|
|
|
|
|
2009-07-07 03:10:14 +08:00
|
|
|
if (0 == ret) {
|
|
|
|
if (do_bench)
|
|
|
|
tcc_print_stats(s, getclock_us() - start_time);
|
|
|
|
|
|
|
|
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
if (outfile)
|
|
|
|
fclose(s->outfile);
|
|
|
|
} else if (s->output_type == TCC_OUTPUT_MEMORY)
|
|
|
|
ret = tcc_run(s, argc - optind, argv + optind);
|
|
|
|
else
|
|
|
|
ret = tcc_output_file(s, outfile) ? 1 : 0;
|
|
|
|
}
|
2002-01-27 02:05:29 +08:00
|
|
|
|
2009-07-07 03:10:14 +08:00
|
|
|
tcc_delete(s);
|
2002-11-02 22:12:32 +08:00
|
|
|
|
|
|
|
#ifdef MEM_DEBUG
|
|
|
|
if (do_bench) {
|
|
|
|
printf("memory: %d bytes, max = %d bytes\n", mem_cur_size, mem_max_size);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return ret;
|
2001-10-28 07:48:39 +08:00
|
|
|
}
|