Delete output files if this is a failed run.
Otherwise, the fail will stop a Makefile from progressing, but if you
immediately run the build again, Make will think the output files are up to
date, since they are newer (albeit incomplete/incorrect).
FossilOrigin-Name: e38c08d9cdeb0476ac1a77cd3f29f547a8205835
diff --git a/tool/lemon.c b/tool/lemon.c
index 122ab97..4a3bfa1 100644
--- a/tool/lemon.c
+++ b/tool/lemon.c
@@ -34,6 +34,24 @@
#define MAXRHS 1000
#endif
+static const char **made_files = NULL;
+static int made_files_count = 0;
+static int successful_exit = 0;
+static void LemonAtExit(void)
+{
+ /* if we failed, delete (most) files we made, to unconfuse build tools. */
+ int i;
+ for (i = 0; i < made_files_count; i++) {
+ if (!successful_exit) {
+ remove(made_files[i]);
+ }
+ free((void *) made_files[i]);
+ }
+ free(made_files);
+ made_files_count = 0;
+ made_files = NULL;
+}
+
static char *msort(char*,char**,int(*)(const char*,const char*));
/*
@@ -1470,6 +1488,8 @@
int exitcode;
struct lemon lem;
+ atexit(LemonAtExit);
+
OptInit(argv,options,stderr);
if( version ){
printf("Lemon version 1.0\n");
@@ -1576,6 +1596,7 @@
/* return 0 on success, 1 on failure. */
exitcode = ((lem.errorcnt > 0) || (lem.nconflict != lem.nexpected)) ? 1 : 0;
+ successful_exit = (exitcode == 0);
exit(exitcode);
return (exitcode);
}
@@ -2831,6 +2852,24 @@
lemp->errorcnt++;
return 0;
}
+
+ /* Add files we create to a list, so we can delete them if we fail. This
+ ** is to keep makefiles from getting confused. We don't include .out files,
+ ** though: 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;
+ }
return fp;
}