libtcc: filetype cleanup

- does not change signature of tcc_add_file
master
grischka 2016-10-01 20:46:16 +02:00
parent 8637c1d0ad
commit acac35c125
5 changed files with 58 additions and 55 deletions

View File

@ -1296,16 +1296,35 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname)
return 0; return 0;
} }
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype) ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
{ {
ElfW(Ehdr) ehdr; ElfW(Ehdr) ehdr;
int fd, ret, size; int fd, ret, size;
int filetype = flags & 0x0F;
if (filetype == 0) {
/* use a file extension to detect a filetype */
const char *ext = tcc_fileextension(filename);
if (ext[0]) {
ext++;
if (!strcmp(ext, "S"))
filetype = AFF_TYPE_ASMPP;
else if (!strcmp(ext, "s"))
filetype = AFF_TYPE_ASM;
else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
filetype = AFF_TYPE_C;
else
filetype = AFF_TYPE_BIN;
} else {
filetype = AFF_TYPE_C;
}
}
parse_flags = 0; parse_flags = 0;
#ifdef CONFIG_TCC_ASM #ifdef CONFIG_TCC_ASM
/* if .S file, define __ASSEMBLER__ like gcc does */ /* if .S file, define __ASSEMBLER__ like gcc does */
if (filetype == TCC_FILETYPE_ASM if (filetype == AFF_TYPE_ASM
|| filetype == TCC_FILETYPE_ASM_PP) { || filetype == AFF_TYPE_ASMPP) {
tcc_define_symbol(s1, "__ASSEMBLER__", NULL); tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
parse_flags = PARSE_FLAG_ASM_FILE; parse_flags = PARSE_FLAG_ASM_FILE;
} }
@ -1328,20 +1347,20 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags,
goto the_end; goto the_end;
} }
if (filetype == TCC_FILETYPE_C) { if (filetype == AFF_TYPE_C) {
/* C file assumed */ /* C file assumed */
ret = tcc_compile(s1); ret = tcc_compile(s1);
goto the_end; goto the_end;
} }
#ifdef CONFIG_TCC_ASM #ifdef CONFIG_TCC_ASM
if (filetype == TCC_FILETYPE_ASM_PP) { if (filetype == AFF_TYPE_ASMPP) {
/* non preprocessed assembler */ /* non preprocessed assembler */
ret = tcc_assemble(s1, 1); ret = tcc_assemble(s1, 1);
goto the_end; goto the_end;
} }
if (filetype == TCC_FILETYPE_ASM) { if (filetype == AFF_TYPE_ASM) {
/* preprocessed assembler */ /* preprocessed assembler */
ret = tcc_assemble(s1, 0); ret = tcc_assemble(s1, 0);
goto the_end; goto the_end;
@ -1417,12 +1436,12 @@ the_end:
return ret; return ret;
} }
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype) LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
{ {
if (s->output_type == TCC_OUTPUT_PREPROCESS) if (s->output_type == TCC_OUTPUT_PREPROCESS)
return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS, filetype); return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS | s->filetype);
else else
return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR, filetype); return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | s->filetype);
} }
LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname) LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
@ -1439,7 +1458,7 @@ static int tcc_add_library_internal(TCCState *s, const char *fmt,
for(i = 0; i < nb_paths; i++) { for(i = 0; i < nb_paths; i++) {
snprintf(buf, sizeof(buf), fmt, paths[i], filename); snprintf(buf, sizeof(buf), fmt, paths[i], filename);
if (tcc_add_file_internal(s, buf, flags, TCC_FILETYPE_BINARY) == 0) if (tcc_add_file_internal(s, buf, flags | AFF_TYPE_BIN) == 0)
return 0; return 0;
} }
return -1; return -1;
@ -2050,25 +2069,6 @@ static void parse_option_D(TCCState *s1, const char *optarg)
static void args_parser_add_file(TCCState *s, const char* filename, int filetype) static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
{ {
struct filespec *f = tcc_malloc(sizeof *f + strlen(filename)); struct filespec *f = tcc_malloc(sizeof *f + strlen(filename));
if (filetype == 0) {
/* use a file extension to detect a filetype */
const char *ext = tcc_fileextension(filename);
if (ext[0]) {
ext++;
if (!strcmp(ext, "S"))
filetype = TCC_FILETYPE_ASM_PP;
else if (!strcmp(ext, "s"))
filetype = TCC_FILETYPE_ASM;
else if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
filetype = TCC_FILETYPE_C;
else
filetype = TCC_FILETYPE_BINARY;
} else {
filetype = TCC_FILETYPE_C;
}
}
f->type = filetype; f->type = filetype;
strcpy(f->name, filename); strcpy(f->name, filename);
dynarray_add((void ***)&s->files, &s->nb_files, f); dynarray_add((void ***)&s->files, &s->nb_files, f);
@ -2098,7 +2098,6 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
const char *optarg, *r; const char *optarg, *r;
int optind = 0; int optind = 0;
int run = 0; int run = 0;
int filetype = 0;
int x; int x;
char buf[1024]; char buf[1024];
@ -2114,7 +2113,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
} }
if (r[0] != '-' || r[1] == '\0') { if (r[0] != '-' || r[1] == '\0') {
args_parser_add_file(s, r, filetype); args_parser_add_file(s, r, s->filetype);
if (run) { if (run) {
optind--; optind--;
/* argv[0] will be this file */ /* argv[0] will be this file */
@ -2163,7 +2162,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
tcc_set_lib_path(s, optarg); tcc_set_lib_path(s, optarg);
break; break;
case TCC_OPTION_l: case TCC_OPTION_l:
args_parser_add_file(s, optarg, 'l'); args_parser_add_file(s, optarg, AFF_TYPE_LIB);
s->nb_libraries++; s->nb_libraries++;
break; break;
case TCC_OPTION_pthread: case TCC_OPTION_pthread:
@ -2310,13 +2309,11 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
break; break;
case TCC_OPTION_x: case TCC_OPTION_x:
if (*optarg == 'c') if (*optarg == 'c')
filetype = TCC_FILETYPE_C; s->filetype = AFF_TYPE_C;
else else if (*optarg == 'a')
if (*optarg == 'a') s->filetype = AFF_TYPE_ASMPP;
filetype = TCC_FILETYPE_ASM_PP; else if (*optarg == 'n')
else s->filetype = AFF_TYPE_NONE;
if (*optarg == 'n')
filetype = 0;
else else
tcc_warning("unsupported language '%s'", optarg); tcc_warning("unsupported language '%s'", optarg);
break; break;

View File

@ -48,11 +48,7 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym);
/* compiling */ /* compiling */
/* add a file (C file, dll, object, library, ld script). Return -1 if error. */ /* add a file (C file, dll, object, library, ld script). Return -1 if error. */
LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype); LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename);
#define TCC_FILETYPE_BINARY 'b'
#define TCC_FILETYPE_C 'c'
#define TCC_FILETYPE_ASM 's'
#define TCC_FILETYPE_ASM_PP 'S'
/* compile a string containing a C source. Return -1 if error. */ /* compile a string containing a C source. Return -1 if error. */
LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);

6
tcc.c
View File

@ -311,14 +311,16 @@ int main(int argc, char **argv)
/* compile or add each files or library */ /* compile or add each files or library */
for(i = ret = 0; i < s->nb_files && ret == 0; i++) { for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
struct filespec *f = s->files[i]; struct filespec *f = s->files[i];
if (f->type == 'l') { if (f->type == AFF_TYPE_LIB) {
if (tcc_add_library_err(s, f->name) < 0) if (tcc_add_library_err(s, f->name) < 0)
ret = 1; ret = 1;
} else { } else {
if (1 == s->verbose) if (1 == s->verbose)
printf("-> %s\n", f->name); printf("-> %s\n", f->name);
if (tcc_add_file(s, f->name, f->type) < 0) s->filetype = f->type;
if (tcc_add_file(s, f->name) < 0)
ret = 1; ret = 1;
s->filetype = AFF_TYPE_NONE;
if (!first_file) if (!first_file)
first_file = f->name; first_file = f->name;
} }

19
tcc.h
View File

@ -755,6 +755,7 @@ struct TCCState {
struct filespec **files; /* files seen on command line */ struct filespec **files; /* files seen on command line */
int nb_files; /* number thereof */ int nb_files; /* number thereof */
int nb_libraries; /* number of libs thereof */ int nb_libraries; /* number of libs thereof */
int filetype;
char *outfile; /* output filename */ char *outfile; /* output filename */
char *option_m; /* only -m32/-m64 handled */ char *option_m; /* only -m32/-m64 handled */
int print_search_dirs; /* option */ int print_search_dirs; /* option */
@ -1070,10 +1071,6 @@ ST_DATA int tcc_ext;
/* XXX: get rid of this ASAP */ /* XXX: get rid of this ASAP */
ST_DATA struct TCCState *tcc_state; ST_DATA struct TCCState *tcc_state;
#define AFF_PRINT_ERROR 0x0001 /* print error if file not found */
#define AFF_REFERENCED_DLL 0x0002 /* load a referenced dll from another dll */
#define AFF_PREPROCESS 0x0004 /* preprocess file */
/* public functions currently used by the tcc main function */ /* public functions currently used by the tcc main function */
ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s); ST_FUNC char *pstrcpy(char *buf, int buf_size, const char *s);
ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s); ST_FUNC char *pstrcat(char *buf, int buf_size, const char *s);
@ -1144,7 +1141,19 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen);
ST_FUNC int tcc_open(TCCState *s1, const char *filename); ST_FUNC int tcc_open(TCCState *s1, const char *filename);
ST_FUNC void tcc_close(void); ST_FUNC void tcc_close(void);
ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype); ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
/* flags: */
#define AFF_PRINT_ERROR 0x10 /* print error if file not found */
#define AFF_REFERENCED_DLL 0x20 /* load a referenced dll from another dll */
#define AFF_PREPROCESS 0x40 /* preprocess file */
/* combined with: */
#define AFF_TYPE_NONE 0
#define AFF_TYPE_C 1
#define AFF_TYPE_ASM 2
#define AFF_TYPE_ASMPP 3
#define AFF_TYPE_BIN 4
#define AFF_TYPE_LIB 5
ST_FUNC int tcc_add_crt(TCCState *s, const char *filename); ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
#ifndef TCC_TARGET_PE #ifndef TCC_TARGET_PE

View File

@ -1573,8 +1573,7 @@ static int tcc_add_support(TCCState *s1, const char *filename)
"C67" "C67"
#endif #endif
,filename); ,filename);
return tcc_add_file(s1, buf);
return tcc_add_file(s1, buf, TCC_FILETYPE_BINARY);
} }
ST_FUNC void tcc_add_bcheck(TCCState *s1) ST_FUNC void tcc_add_bcheck(TCCState *s1)
@ -1624,7 +1623,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1)
tcc_add_library(s1, "c"); tcc_add_library(s1, "c");
#ifdef CONFIG_USE_LIBGCC #ifdef CONFIG_USE_LIBGCC
if (!s1->static_link) { if (!s1->static_link) {
tcc_add_file(s1, TCC_LIBGCC, TCC_FILETYPE_BINARY); tcc_add_file(s1, TCC_LIBGCC);
} }
#endif #endif
tcc_add_support(s1, "libtcc1.a"); tcc_add_support(s1, "libtcc1.a");
@ -3385,7 +3384,7 @@ static int ld_add_file(TCCState *s1, const char filename[])
{ {
int ret; int ret;
ret = tcc_add_file_internal(s1, filename, 0, TCC_FILETYPE_BINARY); ret = tcc_add_file_internal(s1, filename, AFF_TYPE_BIN);
if (ret) if (ret)
ret = tcc_add_dll(s1, filename, 0); ret = tcc_add_dll(s1, filename, 0);
return ret; return ret;