Generate buildno.h in the intermediate tree instead of in the source tree
Modified: trunk/reactos/Makefile
Modified: trunk/reactos/tools/buildno/buildno.cpp

Modified: trunk/reactos/Makefile
--- trunk/reactos/Makefile	2006-01-30 21:46:14 UTC (rev 57)
+++ trunk/reactos/Makefile	2006-01-30 21:47:18 UTC (rev 58)
@@ -337,7 +337,7 @@
 
 NTOSKRNL_MC = ntoskrnl$(SEP)ntoskrnl.mc
 KERNEL32_MC = lib$(SEP)kernel32$(SEP)kernel32.mc
-BUILDNO_H = include$(SEP)reactos$(SEP)buildno.h
+BUILDNO_H = $(INTERMEDIATE_)include$(SEP)reactos$(SEP)buildno.h
 BUGCODES_H = include$(SEP)reactos$(SEP)bugcodes.h
 BUGCODES_RC = ntoskrnl$(SEP)bugcodes.rc
 ERRCODES_H = include$(SEP)reactos$(SEP)errcodes.h

Modified: trunk/reactos/tools/buildno/buildno.cpp
--- trunk/reactos/tools/buildno/buildno.cpp	2006-01-30 21:46:14 UTC (rev 57)
+++ trunk/reactos/tools/buildno/buildno.cpp	2006-01-30 21:47:18 UTC (rev 58)
@@ -1,5 +1,4 @@
-/* $Id$
- *
+/*
  * buildno - Generate the build number for ReactOS
  *
  * Copyright (c) 1999,2000 Emanuele Aliberti
@@ -23,10 +22,18 @@
  * 	Added -p option to make it simply print the
  * 	version number, but skip buildno.h generation.
  */
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include <sys/stat.h>
+#ifdef WIN32
+#include <direct.h>
+#define ros_mkdir(dir, mode) _mkdir(dir)
+#else
+#define ros_mkdir(dir, mode) mkdir((dir), (mode))
+#endif
 #include "version.h"
 #include "xml.h"
 
@@ -37,7 +44,7 @@
 static char * filename = "";
 
 #ifdef DBG
-void
+static void
 tm_dump (const char *tag, struct tm * t)
 {
 	printf ("%s->tm_sec   = %d\n", tag, t->tm_sec);
@@ -52,7 +59,54 @@
 }
 #endif
 
-void
+static int
+create_dir_tree (char *filename)
+{
+  char *dir, *p, sep;
+
+  dir = (char *) malloc(strlen(filename) + 1);
+  if (NULL == dir)
+    {
+      fprintf(stderr, "%s: Out of memory\n", argv0);
+      return -1;
+    }
+
+  strcpy(dir, filename);
+  p = dir;
+
+  /* Until no more path components left */
+  while ('\0' != *p)
+    {
+      /* Find end of path component */
+      while ('\0' != *p && '/' != *p && '\\' != *p)
+        {
+          p++;
+        }
+
+      /* Is this a directory? */
+      if ('\0' != *p)
+        {
+          /* Save separator and make directory name nul terminated */
+          sep = *p;
+          *p = '\0';
+          if (0 != ros_mkdir(dir, 0777) && EEXIST != errno)
+            {
+              fprintf(stderr, "%s: Failed to create output dir %s\n", argv0,
+                      dir);
+              free(dir);
+              return -1;
+            }
+          /* Repair path and continue */
+          *p++ = sep;
+        }
+    }
+
+  free(dir);
+
+  return 0;
+}
+
+static int
 write_h (int build, char *buildstr)
 {
   FILE	*h = NULL;
@@ -151,26 +205,38 @@
 	  if (memcmp(s1, orig, length) == 0)
 	    {
 	      fclose(h);
-	      return;
+	      return 0;
 	    }
 	}
       fclose(h);
     }
 
   h = fopen (filename, "wb");
-  if (!h) 
+  if (NULL == h) 
     {
-      fprintf (stderr,
-	       "%s: can not create file \"%s\"!\n",
-	       argv0,
-	       filename);
-      return;
+      /* Perhaps we're missing a parent dir, try to create the directory tree */
+      if (0 != create_dir_tree (filename))
+        {
+          return -1;
+        }
+      h = fopen (filename, "wb");
+      if (NULL == h) 
+        {
+          /* Still fails, give up */
+          fprintf (stderr,
+                   "%s: can not create file \"%s\"!\n",
+                   argv0,
+                   filename);
+          return -1;
+        }
     }
   fwrite(s1, 1, strlen(s1), h);
   fclose (h);
+
+  return 0;
 }
 
-char *
+static char *
 GetRev(void)
 {
   static char Unknown[] = "UNKNOWN";
@@ -240,7 +306,7 @@
 }
 
 
-void
+static void
 usage (void)
 {
 	fprintf (
@@ -357,7 +423,10 @@
 	 */
 	if (! print_only)
 	{
-		write_h (build, buildstr);
+		if (0 != write_h (build, buildstr))
+		{
+			exit(EXIT_FAILURE);
+		}
 	}
 	else
 	{