Lemon now deletes its output on failure.
authorRyan C. Gordon <icculus@icculus.org>
Fri, 12 Feb 2010 13:40:25 -0500
changeset 829 7b3c3ae9e5a9
parent 828 32b964418dcc
child 830 b15b3c3b5c4a
Lemon now deletes its output on failure. This is to fix the build system. Previously, a Makefile would abort when lemon fails, but on the next run it would see a newer (incorrect/incomplete) output file, and assume it succeeded previously, with disastrous effects for later steps in the build process.
misc/lemon.c
--- a/misc/lemon.c	Tue Feb 09 03:28:04 2010 -0500
+++ b/misc/lemon.c	Fri Feb 12 13:40:25 2010 -0500
@@ -40,6 +40,27 @@
 #define MAXRHS 1000
 #endif
 
+#if __MOJOSHADER__
+static const char **made_files = NULL;
+static int made_files_count = 0;
+static void lemon_exit(const int status)
+{
+    /* if we failed, delete (most) files we made, to unconfuse build tools. */
+    int i;
+    for (i = 0; i < made_files_count; i++) {
+        if (status != 0) {
+            remove(made_files[i]);
+        }
+        free((void *) made_files[i]);
+    }
+    free(made_files);
+    made_files_count = 0;
+    made_files = NULL;
+    exit(status);
+}
+#define exit(x) lemon_exit(x)
+#endif
+
 static char *msort(char*,char**,int(*)(const char*,const char*));
 
 /*
@@ -2833,6 +2854,22 @@
     lemp->errorcnt++;
     return 0;
   }
+#if __MOJOSHADER__
+  /* don't include .out files: this is debug information, and you don't want
+     it deleted if there was an error you need to track down. */
+  if(( *mode=='w' ) && (strcmp(suffix, ".out") != 0)){
+    const char **ptr = (const char **)
+        realloc(made_files, sizeof (const char **) * (made_files_count + 1));
+    char *fname = strdup(lemp->outname);
+    if ((ptr == NULL) || (fname == NULL)) {
+        free(ptr);
+        free(fname);
+        memory_error();
+    }
+    made_files = ptr;
+    made_files[made_files_count++] = fname;
+  }
+#endif
   return fp;
 }