tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] Makefile dependencies with -M


From: Joshua Phillips
Subject: [Tinycc-devel] Makefile dependencies with -M
Date: Sun, 09 Sep 2007 18:02:34 +0100
User-agent: Icedove 1.5.0.12 (X11/20070607)

Since I like tcc being more or less self-contained I thought it'd be
nice to be able to use tcc -M to generate makefile dependencies instead
of 'makedepend', so here it is.

Implements -M, -MM and -MT options, a la gcc.

diff -r 6c9a8e33bad9 Makefile
--- a/Makefile  Sun Sep 09 16:03:10 2007 +0100
+++ b/Makefile  Sun Sep 09 17:53:20 2007 +0100
@@ -3,7 +3,7 @@
 #
 include config.mak
 
-CFLAGS+=-g -Wall -fsigned-char -Os
+CFLAGS+=-g -Wall -fsigned-char
 ifndef CONFIG_WIN32
 LIBS=-lm
 ifndef CONFIG_NOLDL
diff -r 6c9a8e33bad9 tcc.c
--- a/tcc.c     Sun Sep 09 16:03:10 2007 +0100
+++ b/tcc.c     Sun Sep 09 17:53:20 2007 +0100
@@ -1969,6 +1969,12 @@ static void pragma_parse(TCCState *s1)
             skip(')');
         }
     }
+}
+
+/* print a makefile dependency (-M) */
+static void print_depend(TCCState *s1, const char *prerequisite)
+{
+    fprintf(s1->outfile, "%s: %s\n", s1->makefile_target, prerequisite);
 }
 
 /* is_bof is true if first non space token at beginning of file */
@@ -2080,8 +2086,12 @@ static void preprocess(int is_bof)
                 if (f) {
                     if (tok == TOK_INCLUDE_NEXT)
                         tok = TOK_INCLUDE;
-                    else
+                    else {
+                        if (s1->makefile_depends)
+                            /* print makefile dependency */
+                            print_depend(s1, buf1);
                         goto found;
+                    }
                 }
             }
             if (s1->include_stack_ptr >= s1->include_stack + 
INCLUDE_STACK_SIZE)
@@ -2101,8 +2111,12 @@ static void preprocess(int is_bof)
                 if (f) {
                     if (tok == TOK_INCLUDE_NEXT)
                         tok = TOK_INCLUDE;
-                    else
+                    else {
+                        if (s1->makefile_depends == 1)
+                            /* print makefile dependency */
+                            print_depend(s1, buf1);
                         goto found;
+                    }
                 }
             }
             error("include file '%s' not found", buf);
@@ -2115,8 +2129,8 @@ static void preprocess(int is_bof)
             pstrcpy(f->inc_filename, sizeof(f->inc_filename), buf);
             /* push current file in stack */
             /* XXX: fix current line init */
-                       if (s1->include_stack_ptr + 1 == s1->include_stack + 
INCLUDE_STACK_SIZE)
-                               error("#include nested too deeply");
+            if (s1->include_stack_ptr + 1 == s1->include_stack + 
INCLUDE_STACK_SIZE)
+                error("#include nested too deeply");
             *s1->include_stack_ptr++ = file;
             file = f;
             /* add include file debug info */
@@ -8596,10 +8610,13 @@ static int tcc_preprocess(TCCState *s1)
 {
     Sym *define_start;
     int last_is_space;
+    int silent;
 
     preprocess_init(s1);
 
     define_start = define_stack;
+
+    silent = s1->makefile_depends ? 1 : 0;
 
     ch = file->buf_ptr[0];
     tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
@@ -8610,10 +8627,11 @@ static int tcc_preprocess(TCCState *s1)
     for(;;) {
         if (tok == TOK_EOF)
             break;
-        if (!last_is_space) {
+        if (!last_is_space && !silent) {
             fputc(' ', s1->outfile);
         }
-        fputs(get_tok_str(tok, &tokc), s1->outfile);
+        if (!silent)
+            fputs(get_tok_str(tok, &tokc), s1->outfile);
         if (tok == TOK_LINEFEED) {
             last_is_space = 1;
             /* XXX: suppress that hack */
@@ -9411,6 +9429,9 @@ enum {
     TCC_OPTION_I,
     TCC_OPTION_D,
     TCC_OPTION_E,
+    TCC_OPTION_M,
+    TCC_OPTION_MM,
+    TCC_OPTION_MT,
     TCC_OPTION_U,
     TCC_OPTION_L,
     TCC_OPTION_B,
@@ -9445,6 +9466,9 @@ static const TCCOption tcc_options[] = {
     { "I", TCC_OPTION_I, TCC_OPTION_HAS_ARG },
     { "D", TCC_OPTION_D, TCC_OPTION_HAS_ARG },
     { "E", TCC_OPTION_E, 0 },
+    { "MT", TCC_OPTION_MT, TCC_OPTION_HAS_ARG },
+    { "MM", TCC_OPTION_MM, 0 },
+    { "M", TCC_OPTION_M, 0 },
     { "U", TCC_OPTION_U, TCC_OPTION_HAS_ARG },
     { "L", TCC_OPTION_L, TCC_OPTION_HAS_ARG },
     { "B", TCC_OPTION_B, TCC_OPTION_HAS_ARG },
@@ -9589,8 +9613,20 @@ int parse_args(TCCState *s, int argc, ch
                     tcc_define_symbol(s, sym, value);
                 }
                 break;
+            case TCC_OPTION_M:
+                s->makefile_depends = 1;
+                goto option_e;
+            case TCC_OPTION_MM:
+                s->makefile_depends = 2;
+                goto option_e;
             case TCC_OPTION_E:
+            option_e:
                 output_type = TCC_OUTPUT_PREPROCESS;
+                break;
+            case TCC_OPTION_MT:
+                if (s->makefile_target)
+                    tcc_free(s->makefile_target);
+                s->makefile_target = tcc_strdup(optarg);
                 break;
             case TCC_OPTION_U:
                 tcc_undefine_symbol(s, optarg);
@@ -9721,6 +9757,7 @@ int main(int argc, char **argv)
     int i;
     TCCState *s;
     int nb_objfiles, ret, optind;
+    int got_make_target;
     char objfilename[1024];
     int64_t start_time = 0;
 
@@ -9794,16 +9831,16 @@ int main(int argc, char **argv)
 #else
             if (output_type == TCC_OUTPUT_OBJ && !reloc_output) {
                 char *ext = strrchr(objfilename, '.');
-            if (!ext)
-                goto default_outfile;
+                if (!ext)
+                    goto default_outfile;
                 /* add .o extension */
-            strcpy(ext + 1, "o");
-        } else {
-        default_outfile:
-            pstrcpy(objfilename, sizeof(objfilename), "a.out");
-        }
+                strcpy(ext + 1, "o");
+            } else {
+            default_outfile:
+                pstrcpy(objfilename, sizeof(objfilename), "a.out");
+            }
 #endif
-        outfile = objfilename;
+            outfile = objfilename;
         }
     }
 
@@ -9812,6 +9849,7 @@ int main(int argc, char **argv)
     }
 
     tcc_set_output_type(s, output_type);
+    got_make_target = s->makefile_target ? 1 : 0;
 
     /* compile or add each files or library */
     for(i = 0;i < nb_files; i++) {
@@ -9821,6 +9859,21 @@ int main(int argc, char **argv)
 
         filename = files[i];
         if (output_type == TCC_OUTPUT_PREPROCESS) {
+            if (s->makefile_depends && !got_make_target){
+                /* guess output file name for makefile dependencies */
+                if (s->makefile_target)
+                    tcc_free(s->makefile_target);
+                char *p;
+                p = tcc_basename(filename);
+                s->makefile_target = tcc_malloc(strlen(p) + 2);
+                strcpy(s->makefile_target, p);
+                p = strrchr(s->makefile_target, '.');
+                if (p)
+                    strcpy(p + 1, "o");
+                else
+                    strcat(s->makefile_target, ".o");
+            }
+            print_depend(s, filename);
             tcc_add_file_internal(s, filename,
                                   AFF_PRINT_ERROR | AFF_PREPROCESS);
         } else if (filename[0] == '-') {
diff -r 6c9a8e33bad9 tcc.h
--- a/tcc.h     Sun Sep 09 16:03:10 2007 +0100
+++ b/tcc.h     Sun Sep 09 17:53:20 2007 +0100
@@ -336,6 +336,9 @@ struct TCCState {
     int nb_sysinclude_paths;
     CachedInclude **cached_includes;
     int nb_cached_includes;
+
+    int makefile_depends; /* if true, generate makefile dependencies on stdout 
(1=-M option, 2=-MM option) */
+    char *makefile_target;
 
     char **library_paths;
     int nb_library_paths;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]