2001-11-01 17:48:10 +03:00
|
|
|
/*
|
|
|
|
* TCC - Tiny C Compiler
|
|
|
|
*
|
2004-10-28 01:38:03 +04:00
|
|
|
* Copyright (c) 2001-2004 Fabrice Bellard
|
2001-11-01 17:48:10 +03:00
|
|
|
*
|
2003-05-24 18:11:17 +04: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 17:48:10 +03:00
|
|
|
*
|
2003-05-24 18:11:17 +04: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 17:48:10 +03:00
|
|
|
*
|
2003-05-24 18:11:17 +04: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 17:48:10 +03:00
|
|
|
*/
|
2003-04-13 23:49:30 +04:00
|
|
|
|
2011-07-14 20:45:37 +04:00
|
|
|
#ifdef ONE_SOURCE
|
2009-05-05 22:18:53 +04:00
|
|
|
#include "libtcc.c"
|
2011-07-14 20:45:37 +04:00
|
|
|
#else
|
|
|
|
#include "tcc.h"
|
2009-07-19 00:08:01 +04:00
|
|
|
#endif
|
2009-04-18 15:17:27 +04:00
|
|
|
|
2009-12-20 03:53:49 +03:00
|
|
|
static void help(void)
|
2002-05-14 03:00:17 +04:00
|
|
|
{
|
2006-10-16 23:44:00 +04:00
|
|
|
printf("tcc version " TCC_VERSION " - Tiny C Compiler - Copyright (C) 2001-2006 Fabrice Bellard\n"
|
2010-09-08 21:13:36 +04:00
|
|
|
"Usage: tcc [options...] [-o outfile] [-c] infile(s)...\n"
|
|
|
|
" tcc [options...] -run infile [arguments...]\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
"General options:\n"
|
|
|
|
" -c compile only - generate an object file\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
" -o outfile set output filename\n"
|
2007-11-14 20:34:30 +03:00
|
|
|
" -run run compiled source\n"
|
2003-10-18 00:43:47 +04: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 00:35:50 +04:00
|
|
|
" -w disable all warnings\n"
|
2013-02-10 23:39:05 +04:00
|
|
|
" -v show version\n"
|
|
|
|
" -vv show included files (as sole argument: show search paths)\n"
|
2013-02-14 11:24:51 +04:00
|
|
|
" -dumpversion\n"
|
2013-02-10 23:39:05 +04:00
|
|
|
" -bench show compilation statistics\n"
|
2002-07-16 02:17:02 +04:00
|
|
|
"Preprocessor options:\n"
|
2006-10-16 23:44:00 +04:00
|
|
|
" -E preprocess only\n"
|
2002-07-16 02:17:02 +04: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 04:20:38 +04:00
|
|
|
" -Ldir add library path 'dir'\n"
|
2002-08-18 17:22:55 +04:00
|
|
|
" -llib link with dynamic or static library 'lib'\n"
|
2010-11-25 03:42:08 +03:00
|
|
|
" -pthread link with -lpthread and -D_REENTRANT (POSIX Linux)\n"
|
2013-02-10 23:39:05 +04:00
|
|
|
" -r generate (relocatable) object file\n"
|
|
|
|
" -rdynamic export all global symbols to dynamic linker\n"
|
2002-07-28 03:08:04 +04:00
|
|
|
" -shared generate a shared library\n"
|
2008-03-26 00:04:47 +03:00
|
|
|
" -soname set name for shared library to be used at runtime\n"
|
2002-07-25 02:10:59 +04:00
|
|
|
" -static static linking\n"
|
2013-02-12 22:13:28 +04:00
|
|
|
" -Wl,-opt[=val] set linker option (see manual)\n"
|
2002-11-03 03:43:55 +03: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-18 20:21:38 +04:00
|
|
|
#ifdef CONFIG_TCC_BACKTRACE
|
2002-11-03 03:43:55 +03:00
|
|
|
" -bt N show N callers in stack traces\n"
|
2009-04-18 20:21:38 +04:00
|
|
|
#endif
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
"Misc options:\n"
|
2013-02-12 22:13:28 +04:00
|
|
|
" -nostdinc do not use standard system include paths\n"
|
|
|
|
" -nostdlib do not link with standard crt and libraries\n"
|
|
|
|
" -Bdir use 'dir' as tcc internal library and include path\n"
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
" -MD generate target dependencies for make\n"
|
|
|
|
" -MF depfile put generated dependencies here\n"
|
2013-04-19 03:40:48 +04:00
|
|
|
" -norunsrc Do not compile the file which is the first argument after -run.\n"
|
2002-05-14 03:00:17 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-08-06 18:08:33 +04:00
|
|
|
/* re-execute the i386/x86_64 cross-compilers with tcc -m32/-m64: */
|
|
|
|
#if defined TCC_TARGET_I386 || defined TCC_TARGET_X86_64
|
2010-12-07 03:17:20 +03:00
|
|
|
#ifdef _WIN32
|
2011-08-06 18:08:33 +04:00
|
|
|
#include <process.h>
|
|
|
|
static int execvp_win32(const char *prog, char **argv)
|
2010-12-08 11:27:06 +03:00
|
|
|
{
|
2011-08-06 18:08:33 +04:00
|
|
|
int ret = spawnvp(P_NOWAIT, prog, (char const*const*)argv);
|
|
|
|
if (-1 == ret)
|
|
|
|
return ret;
|
|
|
|
cwait(&ret, ret, WAIT_CHILD);
|
|
|
|
exit(ret);
|
2010-12-07 03:17:20 +03:00
|
|
|
}
|
2011-08-06 18:08:33 +04:00
|
|
|
#define execvp execvp_win32
|
|
|
|
#endif
|
|
|
|
static void exec_other_tcc(TCCState *s, char **argv, const char *optarg)
|
2010-12-08 11:27:06 +03:00
|
|
|
{
|
2011-08-06 18:08:33 +04:00
|
|
|
char child_path[4096], *child_name; const char *target;
|
|
|
|
switch (atoi(optarg)) {
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
case 32: break;
|
|
|
|
case 64: target = "x86_64";
|
|
|
|
#else
|
|
|
|
case 64: break;
|
|
|
|
case 32: target = "i386";
|
|
|
|
#endif
|
|
|
|
pstrcpy(child_path, sizeof child_path - 40, argv[0]);
|
|
|
|
child_name = tcc_basename(child_path);
|
|
|
|
strcpy(child_name, target);
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
strcat(child_name, "-win32");
|
|
|
|
#endif
|
|
|
|
strcat(child_name, "-tcc");
|
|
|
|
if (strcmp(argv[0], child_path)) {
|
|
|
|
if (s->verbose > 0)
|
|
|
|
printf("tcc: using '%s'\n", child_name), fflush(stdout);
|
|
|
|
execvp(argv[0] = child_path, argv);
|
|
|
|
}
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("'%s' not found", child_name);
|
2011-08-06 18:08:33 +04:00
|
|
|
case 0: /* ignore -march etc. */
|
2010-12-08 11:27:06 +03:00
|
|
|
break;
|
|
|
|
default:
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_warning("unsupported option \"-m%s\"", optarg);
|
2010-12-08 11:27:06 +03:00
|
|
|
}
|
|
|
|
}
|
2013-02-12 22:13:28 +04:00
|
|
|
#else
|
|
|
|
#define exec_other_tcc(s, argv, optarg)
|
2010-12-08 11:27:06 +03:00
|
|
|
#endif
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static void gen_makedeps(TCCState *s, const char *target, const char *filename)
|
2011-08-11 18:55:30 +04:00
|
|
|
{
|
2013-02-12 22:13:28 +04:00
|
|
|
FILE *depout;
|
|
|
|
char buf[1024], *ext;
|
|
|
|
int i;
|
2011-08-11 18:55:30 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!filename) {
|
|
|
|
/* compute filename automatically
|
|
|
|
* dir/file.o -> dir/file.d */
|
|
|
|
pstrcpy(buf, sizeof(buf), target);
|
|
|
|
ext = tcc_fileextension(buf);
|
|
|
|
pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
|
|
|
|
filename = buf;
|
|
|
|
}
|
2011-01-04 12:17:52 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->verbose)
|
|
|
|
printf("<- %s\n", filename);
|
|
|
|
|
|
|
|
/* XXX return err codes instead of error() ? */
|
|
|
|
depout = fopen(filename, "w");
|
|
|
|
if (!depout)
|
|
|
|
tcc_error("could not open '%s'", filename);
|
|
|
|
|
|
|
|
fprintf(depout, "%s : \\\n", target);
|
|
|
|
for (i=0; i<s->nb_target_deps; ++i)
|
|
|
|
fprintf(depout, " %s \\\n", s->target_deps[i]);
|
|
|
|
fprintf(depout, "\n");
|
|
|
|
fclose(depout);
|
|
|
|
}
|
2012-04-18 20:32:37 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static char *default_outputfile(TCCState *s, const char *first_file)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
char *ext;
|
|
|
|
const char *name = "a";
|
|
|
|
|
|
|
|
if (first_file && strcmp(first_file, "-"))
|
|
|
|
name = tcc_basename(first_file);
|
|
|
|
pstrcpy(buf, sizeof(buf), name);
|
|
|
|
ext = tcc_fileextension(buf);
|
|
|
|
#ifdef TCC_TARGET_PE
|
|
|
|
if (s->output_type == TCC_OUTPUT_DLL)
|
|
|
|
strcpy(ext, ".dll");
|
|
|
|
else
|
|
|
|
if (s->output_type == TCC_OUTPUT_EXE)
|
|
|
|
strcpy(ext, ".exe");
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
if (( (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) ||
|
|
|
|
(s->output_type == TCC_OUTPUT_PREPROCESS) )
|
|
|
|
&& *ext)
|
|
|
|
strcpy(ext, ".o");
|
|
|
|
else
|
|
|
|
strcpy(buf, "a.out");
|
|
|
|
|
|
|
|
return tcc_strdup(buf);
|
|
|
|
}
|
2008-04-27 22:50:35 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static void print_paths(const char *msg, char **paths, int nb_paths)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
printf("%s:\n%s", msg, nb_paths ? "" : " -\n");
|
|
|
|
for(i = 0; i < nb_paths; i++)
|
|
|
|
printf(" %s\n", paths[i]);
|
|
|
|
}
|
2008-04-27 22:50:35 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
static void display_info(TCCState *s, int what)
|
|
|
|
{
|
|
|
|
switch (what) {
|
|
|
|
case 0:
|
|
|
|
printf("tcc version %s ("
|
|
|
|
#ifdef TCC_TARGET_I386
|
|
|
|
"i386"
|
|
|
|
# ifdef TCC_TARGET_PE
|
|
|
|
" Win32"
|
|
|
|
# endif
|
|
|
|
#elif defined TCC_TARGET_X86_64
|
|
|
|
"x86-64"
|
|
|
|
# ifdef TCC_TARGET_PE
|
|
|
|
" Win64"
|
|
|
|
# endif
|
|
|
|
#elif defined TCC_TARGET_ARM
|
|
|
|
"ARM"
|
|
|
|
# ifdef TCC_ARM_HARDFLOAT
|
|
|
|
" Hard Float"
|
|
|
|
# endif
|
|
|
|
# ifdef TCC_TARGET_PE
|
|
|
|
" WinCE"
|
|
|
|
# endif
|
2009-04-18 20:21:38 +04:00
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
#ifndef TCC_TARGET_PE
|
|
|
|
# ifdef __linux
|
|
|
|
" Linux"
|
|
|
|
# endif
|
2002-07-27 19:38:21 +04:00
|
|
|
#endif
|
2013-02-12 22:13:28 +04:00
|
|
|
")\n", TCC_VERSION);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
printf("install: %s/\n", s->tcc_lib_path);
|
|
|
|
/* print_paths("programs", NULL, 0); */
|
|
|
|
print_paths("crt", s->crt_paths, s->nb_crt_paths);
|
|
|
|
print_paths("libraries", s->library_paths, s->nb_library_paths);
|
|
|
|
print_paths("include", s->sysinclude_paths, s->nb_sysinclude_paths);
|
2013-02-14 20:43:24 +04:00
|
|
|
printf("elfinterp:\n %s\n", CONFIG_TCC_ELFINTERP);
|
2013-02-12 22:13:28 +04:00
|
|
|
break;
|
2011-01-04 12:17:52 +03:00
|
|
|
}
|
2013-02-12 22:13:28 +04: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
|
2003-10-05 01:23:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
TCCState *s;
|
2013-02-12 22:13:28 +04:00
|
|
|
int ret, optind, i, bench;
|
2003-10-05 01:23:51 +04:00
|
|
|
int64_t start_time = 0;
|
2013-02-12 22:13:28 +04:00
|
|
|
const char *first_file = NULL;
|
2003-10-05 01:23:51 +04:00
|
|
|
|
2009-04-18 15:17:27 +04:00
|
|
|
s = tcc_new();
|
2013-02-12 22:13:28 +04:00
|
|
|
s->output_type = TCC_OUTPUT_EXE;
|
2009-07-19 00:07:33 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
optind = tcc_parse_args(s, argc - 1, argv + 1);
|
2013-02-19 15:47:36 +04:00
|
|
|
tcc_set_environment(s);
|
2011-08-06 18:08:33 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (optind == 0) {
|
|
|
|
help();
|
|
|
|
return 1;
|
2003-01-06 23:21:08 +03:00
|
|
|
}
|
2011-08-06 18:08:33 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->option_m)
|
|
|
|
exec_other_tcc(s, argv, s->option_m);
|
|
|
|
|
2011-08-06 18:08:33 +04:00
|
|
|
if (s->verbose)
|
2013-02-12 22:13:28 +04:00
|
|
|
display_info(s, 0);
|
2011-08-06 18:08:33 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->print_search_dirs || (s->verbose == 2 && optind == 1)) {
|
|
|
|
tcc_set_output_type(s, TCC_OUTPUT_MEMORY);
|
|
|
|
display_info(s, 1);
|
|
|
|
return 0;
|
2008-04-27 22:50:35 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->verbose && optind == 1)
|
|
|
|
return 0;
|
2002-08-31 02:46:35 +04:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->nb_files == 0)
|
|
|
|
tcc_error("no input files\n");
|
2002-07-18 04:51:11 +04:00
|
|
|
|
2002-08-31 02:46:35 +04:00
|
|
|
/* check -c consistency : only single file handled. XXX: checks file type */
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->output_type == TCC_OUTPUT_OBJ && !s->option_r) {
|
|
|
|
if (s->nb_libraries != 0)
|
|
|
|
tcc_error("cannot specify libraries with -c");
|
2002-08-31 02:46:35 +04:00
|
|
|
/* accepts only a single input file */
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->nb_files != 1)
|
2011-08-11 19:07:56 +04:00
|
|
|
tcc_error("cannot specify multiple files with -c");
|
2002-08-31 02:46:35 +04:00
|
|
|
}
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
if (!s->outfile) {
|
|
|
|
s->ppfp = stdout;
|
2006-10-16 23:44:00 +04:00
|
|
|
} else {
|
2013-02-12 22:13:28 +04:00
|
|
|
s->ppfp = fopen(s->outfile, "w");
|
|
|
|
if (!s->ppfp)
|
|
|
|
tcc_error("could not write '%s'", s->outfile);
|
2006-10-16 23:44:00 +04:00
|
|
|
}
|
2002-07-16 02:17:02 +04:00
|
|
|
}
|
2001-12-08 18:04:01 +03:00
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
bench = s->do_bench;
|
|
|
|
if (bench)
|
2002-11-19 00:46:44 +03:00
|
|
|
start_time = getclock_us();
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
tcc_set_output_type(s, s->output_type);
|
2002-08-31 02:46:35 +04:00
|
|
|
|
|
|
|
/* compile or add each files or library */
|
2013-02-12 22:13:28 +04:00
|
|
|
for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
|
2002-08-31 02:46:35 +04:00
|
|
|
const char *filename;
|
|
|
|
|
2013-02-12 22:13:28 +04:00
|
|
|
filename = s->files[i];
|
2010-06-20 19:18:58 +04:00
|
|
|
if (filename[0] == '-' && filename[1] == 'l') {
|
2009-05-11 20:46:02 +04:00
|
|
|
if (tcc_add_library(s, filename + 2) < 0) {
|
2013-02-12 22:13:28 +04:00
|
|
|
tcc_error_noabort("cannot find '%s'", filename);
|
2007-12-19 20:36:42 +03:00
|
|
|
ret = 1;
|
2009-05-11 20:46:02 +04:00
|
|
|
}
|
2002-08-31 02:46:35 +04:00
|
|
|
} else {
|
2009-05-11 20:45:44 +04:00
|
|
|
if (1 == s->verbose)
|
2008-03-31 23:49:14 +04:00
|
|
|
printf("-> %s\n", filename);
|
2007-12-19 20:36:42 +03:00
|
|
|
if (tcc_add_file(s, filename) < 0)
|
|
|
|
ret = 1;
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!first_file)
|
|
|
|
first_file = filename;
|
2002-08-31 02:46:35 +04:00
|
|
|
}
|
2002-07-18 04:51:11 +04:00
|
|
|
}
|
|
|
|
|
2009-07-06 23:10:14 +04:00
|
|
|
if (0 == ret) {
|
2013-02-12 22:13:28 +04:00
|
|
|
if (bench)
|
2009-07-06 23:10:14 +04:00
|
|
|
tcc_print_stats(s, getclock_us() - start_time);
|
|
|
|
|
2011-08-06 18:08:46 +04:00
|
|
|
if (s->output_type == TCC_OUTPUT_MEMORY) {
|
2012-03-05 23:15:56 +04:00
|
|
|
#ifdef TCC_IS_NATIVE
|
2012-05-13 12:21:39 +04:00
|
|
|
ret = tcc_run(s, argc - 1 - optind, argv + 1 + optind);
|
2012-03-05 23:15:56 +04:00
|
|
|
#else
|
|
|
|
tcc_error_noabort("-run is not available in a cross compiler");
|
2013-02-12 22:13:28 +04:00
|
|
|
ret = 1;
|
2012-03-05 23:15:56 +04:00
|
|
|
#endif
|
2011-08-06 18:08:46 +04:00
|
|
|
} else if (s->output_type == TCC_OUTPUT_PREPROCESS) {
|
|
|
|
if (s->outfile)
|
2013-02-12 22:13:28 +04:00
|
|
|
fclose(s->ppfp);
|
2011-08-06 18:08:46 +04:00
|
|
|
} else {
|
2013-02-12 22:13:28 +04:00
|
|
|
if (!s->outfile)
|
|
|
|
s->outfile = default_outputfile(s, first_file);
|
|
|
|
ret = !!tcc_output_file(s, s->outfile);
|
tcc: Draft suppoprt for -MD/-MF options
In build systems, this is used to automatically collect target
dependencies, e.g.
---- 8< (hello.c) ----
#include "hello.h"
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
$ tcc -MD -c hello.c # -> hello.o, hello.d
$ cat hello.d
hello.o : \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
NOTE: gcc supports -MD only for .c -> .o, but in tcc, we generate
dependencies for whatever action is being taken. E.g. for .c -> exe, the
result will be:
$ tcc -MD -o hello hello.c # -> hello, hello.d
hello: \
/usr/lib/crt1.o \
/usr/lib/crti.o \
hello.c \
hello.h \
/usr/include/stdio.h \
/usr/include/features.h \
/usr/include/bits/predefs.h \
/usr/include/sys/cdefs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs.h \
/usr/include/bits/wordsize.h \
/usr/include/gnu/stubs-32.h \
/home/kirr/local/tcc/lib/tcc/include/stddef.h \
/usr/include/bits/types.h \
/usr/include/bits/wordsize.h \
/usr/include/bits/typesizes.h \
/usr/include/libio.h \
/usr/include/_G_config.h \
/usr/include/wchar.h \
/home/kirr/local/tcc/lib/tcc/include/stdarg.h \
/usr/include/bits/stdio_lim.h \
/usr/include/bits/sys_errlist.h \
/usr/lib/libc.so \
/lib/libc.so.6 \
/usr/lib/ld-linux.so.2 \
/lib/ld-linux.so.2 \
/usr/lib/libc_nonshared.a \
/lib/libc.so.6 \
/usr/lib/libc_nonshared.a \
/home/kirr/local/tcc/lib/tcc/libtcc1.a \
/usr/lib/crtn.o \
So tcc dependency generator is a bit more clever than one used in gcc :)
Also, I've updated TODO and Changelog (in not-yet-released section).
v2:
(Taking inputs from grischka and me myself)
- put code to generate deps file into a function.
- used tcc_fileextension() instead of open-coding
- generate deps only when compilation/preprocessing was successful
v3:
- use pstrcpy instead of snprintf(buf, sizeof(buf), "%s", ...)
2010-06-20 20:08:12 +04:00
|
|
|
/* dump collected dependencies */
|
2013-02-12 22:13:28 +04:00
|
|
|
if (s->gen_deps && !ret)
|
|
|
|
gen_makedeps(s, s->outfile, s->deps_outfile);
|
2010-06-16 16:54:24 +04:00
|
|
|
}
|
2009-07-06 23:10:14 +04:00
|
|
|
}
|
2002-01-26 21:05:29 +03:00
|
|
|
|
2009-07-06 23:10:14 +04:00
|
|
|
tcc_delete(s);
|
2013-02-12 22:13:28 +04:00
|
|
|
if (bench)
|
|
|
|
tcc_memstats();
|
2002-11-02 17:12:32 +03:00
|
|
|
return ret;
|
2001-10-28 02:48:39 +03:00
|
|
|
}
|