From acac35c12597eb678c4a0866f79a6aeb9171dd09 Mon Sep 17 00:00:00 2001 From: grischka Date: Sat, 1 Oct 2016 20:46:16 +0200 Subject: [PATCH] libtcc: filetype cleanup - does not change signature of tcc_add_file --- libtcc.c | 75 +++++++++++++++++++++++++++++++--------------------------------- libtcc.h | 6 +----- tcc.c | 6 ++++-- tcc.h | 19 +++++++++++----- tccelf.c | 7 +++--- 5 files changed, 58 insertions(+), 55 deletions(-) diff --git a/libtcc.c b/libtcc.c index 3a349549..c092a04a 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1296,16 +1296,35 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) 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; 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; #ifdef CONFIG_TCC_ASM /* if .S file, define __ASSEMBLER__ like gcc does */ - if (filetype == TCC_FILETYPE_ASM - || filetype == TCC_FILETYPE_ASM_PP) { + if (filetype == AFF_TYPE_ASM + || filetype == AFF_TYPE_ASMPP) { tcc_define_symbol(s1, "__ASSEMBLER__", NULL); 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; } - if (filetype == TCC_FILETYPE_C) { + if (filetype == AFF_TYPE_C) { /* C file assumed */ ret = tcc_compile(s1); goto the_end; } #ifdef CONFIG_TCC_ASM - if (filetype == TCC_FILETYPE_ASM_PP) { + if (filetype == AFF_TYPE_ASMPP) { /* non preprocessed assembler */ ret = tcc_assemble(s1, 1); goto the_end; } - if (filetype == TCC_FILETYPE_ASM) { + if (filetype == AFF_TYPE_ASM) { /* preprocessed assembler */ ret = tcc_assemble(s1, 0); goto the_end; @@ -1417,12 +1436,12 @@ the_end: 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) - 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 - 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) @@ -1439,7 +1458,7 @@ static int tcc_add_library_internal(TCCState *s, const char *fmt, for(i = 0; i < nb_paths; i++) { 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 -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) { 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; strcpy(f->name, filename); 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; int optind = 0; int run = 0; - int filetype = 0; int x; 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') { - args_parser_add_file(s, r, filetype); + args_parser_add_file(s, r, s->filetype); if (run) { optind--; /* 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); break; case TCC_OPTION_l: - args_parser_add_file(s, optarg, 'l'); + args_parser_add_file(s, optarg, AFF_TYPE_LIB); s->nb_libraries++; break; case TCC_OPTION_pthread: @@ -2310,13 +2309,11 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) break; case TCC_OPTION_x: if (*optarg == 'c') - filetype = TCC_FILETYPE_C; - else - if (*optarg == 'a') - filetype = TCC_FILETYPE_ASM_PP; - else - if (*optarg == 'n') - filetype = 0; + s->filetype = AFF_TYPE_C; + else if (*optarg == 'a') + s->filetype = AFF_TYPE_ASMPP; + else if (*optarg == 'n') + s->filetype = AFF_TYPE_NONE; else tcc_warning("unsupported language '%s'", optarg); break; diff --git a/libtcc.h b/libtcc.h index ad081fc6..799ffd89 100644 --- a/libtcc.h +++ b/libtcc.h @@ -48,11 +48,7 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); /* compiling */ /* 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); -#define TCC_FILETYPE_BINARY 'b' -#define TCC_FILETYPE_C 'c' -#define TCC_FILETYPE_ASM 's' -#define TCC_FILETYPE_ASM_PP 'S' +LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename); /* compile a string containing a C source. Return -1 if error. */ LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf); diff --git a/tcc.c b/tcc.c index 730e1e4c..0e516d1e 100644 --- a/tcc.c +++ b/tcc.c @@ -311,14 +311,16 @@ int main(int argc, char **argv) /* compile or add each files or library */ for(i = ret = 0; i < s->nb_files && ret == 0; 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) ret = 1; } else { if (1 == s->verbose) 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; + s->filetype = AFF_TYPE_NONE; if (!first_file) first_file = f->name; } diff --git a/tcc.h b/tcc.h index 5f504bde..d16fea80 100644 --- a/tcc.h +++ b/tcc.h @@ -755,6 +755,7 @@ struct TCCState { struct filespec **files; /* files seen on command line */ int nb_files; /* number thereof */ int nb_libraries; /* number of libs thereof */ + int filetype; char *outfile; /* output filename */ char *option_m; /* only -m32/-m64 handled */ int print_search_dirs; /* option */ @@ -1070,10 +1071,6 @@ ST_DATA int tcc_ext; /* XXX: get rid of this ASAP */ 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 */ ST_FUNC char *pstrcpy(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 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); #ifndef TCC_TARGET_PE diff --git a/tccelf.c b/tccelf.c index c4f08334..d47027b8 100644 --- a/tccelf.c +++ b/tccelf.c @@ -1573,8 +1573,7 @@ static int tcc_add_support(TCCState *s1, const char *filename) "C67" #endif ,filename); - - return tcc_add_file(s1, buf, TCC_FILETYPE_BINARY); + return tcc_add_file(s1, buf); } 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"); #ifdef CONFIG_USE_LIBGCC if (!s1->static_link) { - tcc_add_file(s1, TCC_LIBGCC, TCC_FILETYPE_BINARY); + tcc_add_file(s1, TCC_LIBGCC); } #endif tcc_add_support(s1, "libtcc1.a"); @@ -3385,7 +3384,7 @@ static int ld_add_file(TCCState *s1, const char filename[]) { 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) ret = tcc_add_dll(s1, filename, 0); return ret; -- 2.11.4.GIT