History

Merge branch 'kirr/tcc-MD' into mob

Hello up there. I'm trying to change gcc to tcc for faster develpment on slow machines, and the first obstacle that turned out was lack of dependency generation on tcc side. Attached patches try to fix that. Thanks, Kirill * tcc-MD: .gitignore += tags tcc: Explicitly require -l<lib> for libraries Document what tcc_fileextension does tcc -E: Let output_default be <file>.o instead of a.out tcc: Draft suppoprt for -MD/-MF options tcc: Refactor "compute default outfile name" into libtcc function Add input files/libs and reloc_output switch to TCCState tcc: Fix typo in error (it's '%s', not '%s) chmod a-x i386-gen.c .gitignore += *.o *.a .cvsignore -> .gitignore

Kirill Smelkov committed 16 years ago

Commit 9775aea2 parents 3ba37e1 2fe52109 files · +150 −34Browse files

Unified · Split

9 files
  1. .gitignore +4 −1
  2. Changelog +4 −0
  3. TODO +0 −1
  4. i386-gen.c +0 −0
  5. libtcc.c +81 −0
  6. libtcc.h +14 −0
  7. tcc.c +32 −31
  8. tcc.h +12 −1
  9. tccpp.c +3 −0
.gitignorerenamed from .cvsignore+4−1
@@ -1,3 +1,5 @@
1+*.o
2+*.a
13 tcc_g
24 tcc
35 tc2.c
@@ -34,4 +36,5 @@ tcc.pod · 30 unmodified lines
3436 config.h
3537 config.mak
3638 config.texi
37−tests
39+tests
40+tags
Changelog+4−0
@@ -1,3 +1,7 @@
1+not released:
2+
3+- Add support for -MD/-MF (automatically generate dependencies for make)
4+
15 version 0.9.25:
26
37 - first support for x86-64 target (Shinichiro Hamaji)
TODO+0−1
@@ -46,7 +46,6 @@ Missing features: · 45 unmodified lines
4646 - disable-asm and disable-bcheck options
4747 - __builtin_expect()
4848 - improve '-E' option.
49−- add '-MD' option
5049 - atexit (Nigel Horne)
5150 - packed attribute
5251 - C99: add variable size arrays (gcc 3.2 testsuite issue)
i386-gen.c+0−0

File mode changed from 100755 to 100644.

libtcc.c+81−0
@@ -168,6 +168,10 @@ PUB_FUNC char *tcc_basename(const char *name) · 167 unmodified lines
168168 return p;
169169 }
170170
171+/* extract extension part of a file
172+ *
173+ * (if no extension, return pointer to end-of-string)
174+ */
171175 PUB_FUNC char *tcc_fileextension (const char *name)
172176 {
173177 char *b = tcc_basename(name);
@@ -1038,6 +1042,11 @@ LIBTCCAPI void tcc_delete(TCCState *s1) · 864 unmodified lines
10381042 dynarray_reset(&s1->sysinclude_paths, &s1->nb_sysinclude_paths);
10391043
10401044 tcc_free(s1->tcc_lib_path);
1045+
1046+ dynarray_reset(&s1->input_files, &s1->nb_input_files);
1047+ dynarray_reset(&s1->input_libs, &s1->nb_input_libs);
1048+ dynarray_reset(&s1->target_deps, &s1->nb_target_deps);
1049+
10411050 #ifdef HAVE_SELINUX
10421051 munmap (s1->write_mem, s1->mem_size);
10431052 munmap (s1->runtime_mem, s1->mem_size);
@@ -1088,6 +1097,10 @@ ST_FUNC int tcc_add_file_internal(TCCState *s1, const char *filename, int flags) · 44 unmodified lines
10881097 goto the_end;
10891098 }
10901099
1100+ /* update target deps */
1101+ dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1102+ tcc_strdup(filename));
1103+
10911104 if (flags & AFF_PREPROCESS) {
10921105 ret = tcc_preprocess(s1);
10931106 goto the_end;
@@ -1184,6 +1197,8 @@ the_end: · 90 unmodified lines
11841197
11851198 LIBTCCAPI int tcc_add_file(TCCState *s, const char *filename)
11861199 {
1200+ dynarray_add((void ***)&s->input_files, &s->nb_input_files, tcc_strdup(filename));
1201+
11871202 if (s->output_type == TCC_OUTPUT_PREPROCESS)
11881203 return tcc_add_file_internal(s, filename, AFF_PRINT_ERROR | AFF_PREPROCESS);
11891204 else
@@ -1220,6 +1235,8 @@ LIBTCCAPI int tcc_add_library(TCCState *s, const char *libraryname) · 30 unmodified lines
12201235 {
12211236 char buf[1024];
12221237 int i;
1238+
1239+ dynarray_add((void ***)&s->input_libs, &s->nb_input_libs, tcc_strdup(libraryname));
12231240
12241241 /* first we look for the dynamic library if not static linking */
12251242 if (!s->static_link) {
@@ -1549,3 +1566,67 @@ PUB_FUNC void set_num_callers(int n) · 323 unmodified lines
15491566 num_callers = n;
15501567 #endif
15511568 }
1569+
1570+
1571+LIBTCCAPI const char *tcc_default_target(TCCState *s)
1572+{
1573+ /* FIXME will break in multithreaded case */
1574+ static char outfile_default[1024];
1575+
1576+ char *ext;
1577+ const char *name =
1578+ strcmp(s->input_files[0], "-") == 0 ? "a"
1579+ : tcc_basename(s->input_files[0]);
1580+ pstrcpy(outfile_default, sizeof(outfile_default), name);
1581+ ext = tcc_fileextension(outfile_default);
1582+#ifdef TCC_TARGET_PE
1583+ if (s->output_type == TCC_OUTPUT_DLL)
1584+ strcpy(ext, ".dll");
1585+ else
1586+ if (s->output_type == TCC_OUTPUT_EXE)
1587+ strcpy(ext, ".exe");
1588+ else
1589+#endif
1590+ if (( (s->output_type == TCC_OUTPUT_OBJ && !s->reloc_output) ||
1591+ (s->output_type == TCC_OUTPUT_PREPROCESS) )
1592+ && *ext)
1593+ strcpy(ext, ".o");
1594+ else
1595+ pstrcpy(outfile_default, sizeof(outfile_default), "a.out");
1596+
1597+ return outfile_default;
1598+}
1599+
1600+
1601+LIBTCCAPI void tcc_gen_makedeps(TCCState *s, const char *target, const char *filename)
1602+{
1603+ FILE *depout;
1604+ char buf[1024], *ext;
1605+ int i;
1606+
1607+ if (!target)
1608+ target = tcc_default_target(s);
1609+
1610+ if (!filename) {
1611+ /* compute filename automatically
1612+ * dir/file.o -> dir/file.d */
1613+ pstrcpy(buf, sizeof(buf), target);
1614+ ext = tcc_fileextension(buf);
1615+ pstrcpy(ext, sizeof(buf) - (ext-buf), ".d");
1616+ filename = buf;
1617+ }
1618+
1619+ if (s->verbose)
1620+ printf("<- %s\n", filename);
1621+
1622+ /* XXX return err codes instead of error() ? */
1623+ depout = fopen(filename, "w");
1624+ if (!depout)
1625+ error("could not open '%s'", filename);
1626+
1627+ fprintf(depout, "%s : \\\n", target);
1628+ for (i=0; i<s->nb_target_deps; ++i)
1629+ fprintf(depout, "\t%s \\\n", s->target_deps[i]);
1630+ fprintf(depout, "\n");
1631+ fclose(depout);
1632+}
libtcc.h+14−0
@@ -103,6 +103,20 @@ LIBTCCAPI void *tcc_get_symbol(TCCState *s, const char *name); · 102 unmodified lines
103103 /* set CONFIG_TCCDIR at runtime */
104104 LIBTCCAPI void tcc_set_lib_path(TCCState *s, const char *path);
105105
106+
107+/*****************************/
108+/* Miscellaneous */
109+
110+/* Get default target filename for this compilation */
111+LIBTCCAPI const char *tcc_default_target(TCCState *s);
112+
113+/* Generate make dependencies for target and store them into file
114+ *
115+ * !target - use default target name
116+ * !filename - use (target.o -> target.d)
117+ */
118+LIBTCCAPI void tcc_gen_makedeps(TCCState *s, const char *target, const char *filename);
119+
106120 #ifdef __cplusplus
107121 }
108122 #endif
tcc.c+32−31
@@ -32,6 +32,8 @@ static int output_type; · 31 unmodified lines
3232 static int reloc_output;
3333 static const char *outfile;
3434 static int do_bench = 0;
35+static int gen_deps;
36+static const char *deps_outfile;
3537
3638 #define TCC_OPTION_HAS_ARG 0x0001
3739 #define TCC_OPTION_NOSEP 0x0002 /* cannot have space before option and arg */
@@ -74,6 +76,9 @@ static void help(void) · 36 unmodified lines
7476 #ifdef CONFIG_TCC_BACKTRACE
7577 " -bt N show N callers in stack traces\n"
7678 #endif
79+ "Misc options:\n"
80+ " -MD generate target dependencies for make\n"
81+ " -MF depfile put generated dependencies here\n"
7782 );
7883 }
7984
@@ -115,6 +120,8 @@ enum { · 35 unmodified lines
115120 TCC_OPTION_w,
116121 TCC_OPTION_pipe,
117122 TCC_OPTION_E,
123+ TCC_OPTION_MD,
124+ TCC_OPTION_MF,
118125 TCC_OPTION_x,
119126 };
120127
@@ -154,6 +161,8 @@ static const TCCOption tcc_options[] = { · 33 unmodified lines
154161 { "w", TCC_OPTION_w, 0 },
155162 { "pipe", TCC_OPTION_pipe, 0},
156163 { "E", TCC_OPTION_E, 0},
164+ { "MD", TCC_OPTION_MD, 0},
165+ { "MF", TCC_OPTION_MF, TCC_OPTION_HAS_ARG },
157166 { "x", TCC_OPTION_x, TCC_OPTION_HAS_ARG },
158167 { NULL },
159168 };
@@ -374,6 +383,12 @@ static int parse_args(TCCState *s, int argc, char **argv) · 214 unmodified lines
374383 case TCC_OPTION_E:
375384 output_type = TCC_OUTPUT_PREPROCESS;
376385 break;
386+ case TCC_OPTION_MD:
387+ gen_deps = 1;
388+ break;
389+ case TCC_OPTION_MF:
390+ deps_outfile = optarg;
391+ break;
377392 case TCC_OPTION_x:
378393 break;
379394 default:
@@ -393,7 +408,6 @@ int main(int argc, char **argv) · 13 unmodified lines
393408 int i;
394409 TCCState *s;
395410 int nb_objfiles, ret, optind;
396− char objfilename[1024];
397411 int64_t start_time = 0;
398412
399413 s = tcc_new();
@@ -444,29 +458,7 @@ int main(int argc, char **argv) · 44 unmodified lines
444458 } else {
445459 s->outfile = fopen(outfile, "w");
446460 if (!s->outfile)
447− error("could not open '%s", outfile);
448− }
449− } else if (output_type != TCC_OUTPUT_MEMORY) {
450− if (!outfile) {
451− /* compute default outfile name */
452− char *ext;
453− const char *name =
454− strcmp(files[0], "-") == 0 ? "a" : tcc_basename(files[0]);
455− pstrcpy(objfilename, sizeof(objfilename), name);
456− ext = tcc_fileextension(objfilename);
457−#ifdef TCC_TARGET_PE
458− if (output_type == TCC_OUTPUT_DLL)
459− strcpy(ext, ".dll");
460− else
461− if (output_type == TCC_OUTPUT_EXE)
462− strcpy(ext, ".exe");
463− else
464−#endif
465− if (output_type == TCC_OUTPUT_OBJ && !reloc_output && *ext)
466− strcpy(ext, ".o");
467− else
468− pstrcpy(objfilename, sizeof(objfilename), "a.out");
469− outfile = objfilename;
461+ error("could not open '%s'", outfile);
470462 }
471463 }
472464
@@ -475,13 +467,14 @@ int main(int argc, char **argv) · 2 unmodified lines
475467 }
476468
477469 tcc_set_output_type(s, output_type);
470+ s->reloc_output = reloc_output;
478471
479472 /* compile or add each files or library */
480473 for(i = 0; i < nb_files && ret == 0; i++) {
481474 const char *filename;
482475
483476 filename = files[i];
484− if (filename[0] == '-' && filename[1]) {
477+ if (filename[0] == '-' && filename[1] == 'l') {
485478 if (tcc_add_library(s, filename + 2) < 0) {
486479 error_noabort("cannot find %s", filename);
487480 ret = 1;
@@ -501,13 +494,21 @@ int main(int argc, char **argv) · 13 unmodified lines
501494 if (do_bench)
502495 tcc_print_stats(s, getclock_us() - start_time);
503496
504− if (s->output_type == TCC_OUTPUT_PREPROCESS) {
505− if (outfile)
506− fclose(s->outfile);
507− } else if (s->output_type == TCC_OUTPUT_MEMORY)
497+ if (s->output_type == TCC_OUTPUT_MEMORY)
508498 ret = tcc_run(s, argc - optind, argv + optind);
509− else
510− ret = tcc_output_file(s, outfile) ? 1 : 0;
499+ else {
500+ if (s->output_type == TCC_OUTPUT_PREPROCESS) {
501+ if (outfile)
502+ fclose(s->outfile);
503+ } else {
504+ ret = tcc_output_file(s, outfile ? outfile : tcc_default_target(s));
505+ ret = ret ? 1 : 0;
506+ }
507+
508+ /* dump collected dependencies */
509+ if (gen_deps && !ret)
510+ tcc_gen_makedeps(s, outfile, deps_outfile);
511+ }
511512 }
512513
513514 tcc_delete(s);
tcc.h+12−1
@@ -397,7 +397,8 @@ typedef struct ASMOperand { · 396 unmodified lines
397397 #endif
398398
399399 struct TCCState {
400− int output_type;
400+ unsigned output_type : 8;
401+ unsigned reloc_output : 1;
401402
402403 BufferedFile **include_stack_ptr;
403404 int *ifdef_stack_ptr;
@@ -518,6 +519,16 @@ struct TCCState { · 114 unmodified lines
518519 /* output file for preprocessing */
519520 FILE *outfile;
520521
522+ /* input files and libraries for this compilation */
523+ char **input_files;
524+ int nb_input_files;
525+ char **input_libs;
526+ int nb_input_libs;
527+
528+ /* automatically collected dependencies for this compilation */
529+ char **target_deps;
530+ int nb_target_deps;
531+
521532 /* for tcc_relocate */
522533 int runtime_added;
523534 void *runtime_mem;
tccpp.c+3−0
@@ -1491,6 +1491,9 @@ ST_FUNC void preprocess(int is_bof) · 1490 unmodified lines
14911491 #ifdef INC_DEBUG
14921492 printf("%s: including %s\n", file->filename, buf1);
14931493 #endif
1494+ /* update target deps */
1495+ dynarray_add((void ***)&s1->target_deps, &s1->nb_target_deps,
1496+ tcc_strdup(buf1));
14941497
14951498 /* XXX: fix current line init */
14961499 /* push current file in stack */

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