OSDN Git Service

2003-01-29 Joel Sherrill <joel@OARcorp.com>
[pf3gnuchains/gcc-fork.git] / gcc / mkdeps.c
index 14935b3..0c573cd 100644 (file)
@@ -22,6 +22,8 @@ Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "mkdeps.h"
 
 /* Keep this structure local to this file, so clients don't find it
@@ -188,11 +190,11 @@ deps_add_default_target (d, tgt)
     deps_add_target (d, "-", 1);
   else
     {
-#ifndef OBJECT_SUFFIX
-# define OBJECT_SUFFIX ".o"
+#ifndef TARGET_OBJECT_SUFFIX
+# define TARGET_OBJECT_SUFFIX ".o"
 #endif
-      char *start = basename (tgt);
-      char *o = (char *) alloca (strlen (start) + strlen (OBJECT_SUFFIX) + 1);
+      const char *start = lbasename (tgt);
+      char *o = (char *) alloca (strlen (start) + strlen (TARGET_OBJECT_SUFFIX) + 1);
       char *suffix;
 
       strcpy (o, start);
@@ -200,7 +202,7 @@ deps_add_default_target (d, tgt)
       suffix = strrchr (o, '.');
       if (!suffix)
         suffix = o + strlen (o);
-      strcpy (suffix, OBJECT_SUFFIX);
+      strcpy (suffix, TARGET_OBJECT_SUFFIX);
       
       deps_add_target (d, o, 1);
     }
@@ -289,3 +291,77 @@ deps_phony_targets (d, fp)
       putc ('\n', fp);
     }
 }
+
+/* Write out a deps buffer to a file, in a form that can be read back
+   with deps_restore.  Returns nonzero on error, in which case the
+   error number will be in errno.  */
+
+int
+deps_save (deps, f)
+     struct deps *deps;
+     FILE *f;
+{
+  unsigned int i;
+
+  /* The cppreader structure contains makefile dependences.  Write out this
+     structure.  */
+
+  /* The number of dependences.  */
+  if (fwrite (&deps->ndeps, sizeof (deps->ndeps), 1, f) != 1)
+      return -1;
+  /* The length of each dependence followed by the string.  */
+  for (i = 0; i < deps->ndeps; i++)
+    {
+      size_t num_to_write = strlen (deps->depv[i]);
+      if (fwrite (&num_to_write, sizeof (size_t), 1, f) != 1)
+          return -1;
+      if (fwrite (deps->depv[i], num_to_write, 1, f) != 1)
+          return -1;
+    }
+
+  return 0;
+}
+
+/* Read back dependency information written with deps_save into
+   the deps buffer.  The third argument may be NULL, in which case
+   the dependency information is just skipped, or it may be a filename,
+   in which case that filename is skipped.  */
+
+int
+deps_restore (deps, fd, self)
+     struct deps *deps;
+     FILE *fd;
+     const char *self;
+{
+  unsigned int i, count;
+  size_t num_to_read;
+  size_t buf_size = 512;
+  char *buf = (char *) xmalloc (buf_size);
+
+  /* Number of dependences.  */
+  if (fread (&count, 1, sizeof (count), fd) != sizeof (count))
+    return -1;
+
+  /* The length of each dependence string, followed by the string.  */
+  for (i = 0; i < count; i++)
+    {
+      /* Read in # bytes in string.  */
+      if (fread (&num_to_read, 1, sizeof (size_t), fd) != sizeof (size_t))
+       return -1;
+      if (buf_size < num_to_read + 1)
+       {
+         buf_size = num_to_read + 1 + 127;
+         buf = xrealloc (buf, buf_size);
+       }
+      if (fread (buf, 1, num_to_read, fd) != num_to_read)
+       return -1;
+      buf[num_to_read] = '\0';
+
+      /* Generate makefile dependencies from .pch if -nopch-deps.  */ 
+      if (self != NULL && strcmp (buf, self) != 0)
+        deps_add_dep (deps, buf);
+    }
+
+  free (buf);
+  return 0;
+}