History

ability to specify a type of the input file with the -x switch

Usage example: tcc -xc ex5.cgi From a gcc docs: You can specify the input language explicitly with the -x option: -x language Specify explicitly the language for the following input files (rather than letting the compiler choose a default based on the file name suffix). This option applies to all following input files until the next -x option. Possible values for language are: c c-header c-cpp-output c++ c++-header c++-cpp-output objective-c objective-c-header objective-c-cpp-output objective-c++ objective-c++-header objective-c++-cpp-output assembler assembler-with-cpp ada f77 f77-cpp-input f95 f95-cpp-input java -x none Turn off any specification of a language, so that subsequent files are handled according to their file name suffixes (as they are if -x has not been used at all)

seyko committed 11 years ago

Commit 05364071 parent dcb36585 files · +67 −28Browse files

Unified · Split

5 files
  1. libtcc.c +55 −19
  2. libtcc.h +5 −1
  3. tcc.c +3 −4
  4. tcc.h +1 −1
  5. tccelf.c +3 −3
libtcc.c+55−19
@@ -1138,21 +1138,15 @@ LIBTCCAPI int tcc_add_sysinclude_path(TCCState *s, const char *pathname) · 1137 unmodified lines
11381138 return 0;
11391139 }
11401140
1141−ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags)
1141+ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype)
11421142 {
1143− const char *ext;
11441143 ElfW(Ehdr) ehdr;
11451144 int fd, ret, size;
11461145
1147− /* find source file type with extension */
1148− ext = tcc_fileextension(filename);
1149− if (ext[0])
1150− ext++;
1151−
11521146 parse_flags = 0;
11531147 #ifdef CONFIG_TCC_ASM
11541148 /* if .S file, define __ASSEMBLER__ like gcc does */
1155− if (!strcmp(ext, "S") || !strcmp(ext, "s")) {
1149+ if ((filetype == TCC_FILETYPE_ASM) || (filetype == TCC_FILETYPE_ASM_PP)) {
11561150 tcc_define_symbol(s1, "__ASSEMBLER__", NULL);
11571151 parse_flags = PARSE_FLAG_ASM_FILE;
11581152 }
@@ -1175,20 +1169,20 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 16 unmodified lines
11751169 goto the_end;
11761170 }
11771171
1178− if (!ext[0] || !PATHCMP(ext, "c") || !PATHCMP(ext, "i")) {
1172+ if (filetype == TCC_FILETYPE_C) {
11791173 /* C file assumed */
11801174 ret = tcc_compile(s1);
11811175 goto the_end;
11821176 }
11831177
11841178 #ifdef CONFIG_TCC_ASM
1185− if (!strcmp(ext, "S")) {
1179+ if (filetype == TCC_FILETYPE_ASM_PP) {
11861180 /* non preprocessed assembler */
11871181 ret = tcc_assemble(s1, 1);
11881182 goto the_end;
11891183 }
11901184
1191− if (!strcmp(ext, "s")) {
1185+ if (filetype == TCC_FILETYPE_ASM) {
11921186 /* preprocessed assembler */
11931187 ret = tcc_assemble(s1, 0);
11941188 goto the_end;
@@ -1264,12 +1258,12 @@ the_end: · 69 unmodified lines
12641258 return ret;
12651259 }
12661260
1267−LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
1261+LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype)
12681262 {
12691263 if (s->output_type == TCC_OUTPUT_PREPROCESS)
1270− return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS);
1264+ return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS, filetype);
12711265 else
1272− return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR);
1266+ return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR, filetype);
12731267 }
12741268
12751269 LIBTCCAPI int tcc_add_library_path(TCCState *s, const char *pathname)
@@ -1286,7 +1280,7 @@ static int tcc_add_library_internal(TCCState *s, const char *fmt, · 10 unmodified lines
12861280
12871281 for(i = 0; i < nb_paths; i++) {
12881282 snprintf(buf, sizeof(buf), fmt, paths[i], filename);
1289− if (tcc_add_file_internal(s, buf, flags) == 0)
1283+ if (tcc_add_file_internal(s, buf, flags, TCC_FILETYPE_BINARY) == 0)
12901284 return 0;
12911285 }
12921286 return -1;
@@ -1782,6 +1776,37 @@ static void parse_option_D(TCCState *s1, const char *optarg) · 489 unmodified lines
17821776 tcc_free(sym);
17831777 }
17841778
1779+static void args_parser_add_file(TCCState *s, const char* filename, int filetype)
1780+{
1781+ int len = strlen(filename);
1782+ char *p = tcc_malloc(len + 2);
1783+ if (filetype) {
1784+ *p = filetype;
1785+ }
1786+ else {
1787+ /* use a file extension to detect a filetype */
1788+ const char *ext = tcc_fileextension(filename);
1789+ if (ext[0]) {
1790+ ext++;
1791+ if (!strcmp(ext, "S"))
1792+ *p = TCC_FILETYPE_ASM_PP;
1793+ else
1794+ if (!strcmp(ext, "s"))
1795+ *p = TCC_FILETYPE_ASM;
1796+ else
1797+ if (!PATHCMP(ext, "c") || !PATHCMP(ext, "i"))
1798+ *p = TCC_FILETYPE_C;
1799+ else
1800+ *p = TCC_FILETYPE_BINARY;
1801+ }
1802+ else {
1803+ *p = TCC_FILETYPE_C;
1804+ }
1805+ }
1806+ strcpy(p+1, filename);
1807+ dynarray_add((void ***)&s->files, &s->nb_files, p);
1808+}
1809+
17851810 PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv)
17861811 {
17871812 const TCCOption *popt;
@@ -1789,6 +1814,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 1 unmodified line
17891814 int run = 0;
17901815 int pthread = 0;
17911816 int optind = 0;
1817+ int filetype = 0;
17921818
17931819 /* collect -Wl options for input such as "-Wl,-rpath -Wl,<path>" */
17941820 CString linker_arg;
@@ -1798,8 +1824,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 3 unmodified lines
17981824
17991825 r = argv[optind++];
18001826 if (r[0] != '-' || r[1] == '\0') {
1801− /* add a new file */
1802− dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r));
1827+ args_parser_add_file(s, r, filetype);
18031828 if (run) {
18041829 optind--;
18051830 /* argv[0] will be this file */
@@ -1849,7 +1874,7 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 43 unmodified lines
18491874 tcc_set_lib_path(s, optarg);
18501875 break;
18511876 case TCC_OPTION_l:
1852− dynarray_add((void ***)&s->files, &s->nb_files, tcc_strdup(r));
1877+ args_parser_add_file(s, r, TCC_FILETYPE_BINARY);
18531878 s->nb_libraries++;
18541879 break;
18551880 case TCC_OPTION_pthread:
@@ -1989,10 +2014,21 @@ PUB_FUNC int tcc_parse_args(TCCState *s, int argc, char **argv) · 133 unmodified lines
19892014 case TCC_OPTION_s:
19902015 s->do_strip = 1;
19912016 break;
2017+ case TCC_OPTION_x:
2018+ if (*optarg == 'c')
2019+ filetype = TCC_FILETYPE_C;
2020+ else
2021+ if (*optarg == 'a')
2022+ filetype = TCC_FILETYPE_ASM_PP;
2023+ else
2024+ if (*optarg == 'n')
2025+ filetype = 0;
2026+ else
2027+ tcc_warning("unsupported language '%s'", optarg);
2028+ break;
19922029 case TCC_OPTION_O:
19932030 case TCC_OPTION_pedantic:
19942031 case TCC_OPTION_pipe:
1995− case TCC_OPTION_x:
19962032 /* ignored */
19972033 break;
19982034 default:
libtcc.h+5−1
@@ -48,7 +48,11 @@ LIBTCCAPI void tcc_undefine_symbol(TCCState *s, const char *sym); · 47 unmodified lines
4848 /* compiling */
4949
5050 /* add a file (C file, dll, object, library, ld script). Return -1 if error. */
51−LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename);
51+LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename, int filetype);
52+#define TCC_FILETYPE_BINARY 1
53+#define TCC_FILETYPE_C 2
54+#define TCC_FILETYPE_ASM 3
55+#define TCC_FILETYPE_ASM_PP 4
5256
5357 /* compile a string containing a C source. Return -1 if error. */
5458 LIBTCCAPI int tcc_compile_string(TCCState *s, const char *buf);
tcc.c+3−4
@@ -312,9 +312,8 @@ int main(int argc, char **argv) · 311 unmodified lines
312312
313313 /* compile or add each files or library */
314314 for(i = ret = 0; i < s->nb_files && ret == 0; i++) {
315− const char *filename;
316−
317− filename = s->files[i];
315+ int filetype = *(unsigned char *)s->files[i];
316+ const char *filename = s->files[i] + 1;
318317 if (filename[0] == '-' && filename[1] == 'l') {
319318 if (tcc_add_library(s, filename + 2) < 0) {
320319 tcc_error_noabort("cannot find '%s'", filename);
@@ -323,7 +322,7 @@ int main(int argc, char **argv) · 2 unmodified lines
323322 } else {
324323 if (1 == s->verbose)
325324 printf("-> %s\n", filename);
326− if (tcc_add_file(s, filename) < 0)
325+ if (tcc_add_file(s, filename, filetype) < 0)
327326 ret = 1;
328327 if (!first_file)
329328 first_file = filename;
tcc.h+1−1
@@ -1115,7 +1115,7 @@ ST_FUNC void tcc_open_bf(TCCState *s1, const char *filename, int initlen); · 1114 unmodified lines
11151115 ST_FUNC int tcc_open(TCCState *s1, const char *filename);
11161116 ST_FUNC void tcc_close(void);
11171117
1118−ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags);
1118+ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags, int filetype);
11191119 ST_FUNC int tcc_add_crt(TCCState *s, const char *filename);
11201120 ST_FUNC int tcc_add_dll(TCCState *s, const char *filename, int flags);
11211121
tccelf.c+3−3
@@ -1562,7 +1562,7 @@ static int tcc_add_support(TCCState *s1, const char *filename) · 1561 unmodified lines
15621562 #endif
15631563 ,filename);
15641564
1565− return tcc_add_file(s1, buf);
1565+ return tcc_add_file(s1, buf, TCC_FILETYPE_BINARY);
15661566 }
15671567
15681568 ST_FUNC void tcc_add_bcheck(TCCState *s1)
@@ -1610,7 +1610,7 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 41 unmodified lines
16101610 tcc_add_library(s1, "c");
16111611 #ifdef CONFIG_USE_LIBGCC
16121612 if (!s1->static_link) {
1613− tcc_add_file(s1, TCC_LIBGCC);
1613+ tcc_add_file(s1, TCC_LIBGCC, TCC_FILETYPE_BINARY);
16141614 }
16151615 #endif
16161616 tcc_add_support(s1, "libtcc1.a");
@@ -3366,7 +3366,7 @@ static int ld_add_file(TCCState *s1, const char filename[]) · 1749 unmodified lines
33663366 {
33673367 int ret;
33683368
3369− ret = tcc_add_file_internal(s1, filename, 0);
3369+ ret = tcc_add_file_internal(s1, filename, 0, TCC_FILETYPE_BINARY);
33703370 if (ret)
33713371 ret = tcc_add_dll(s1, filename, 0);
33723372 return ret;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t