Subject: make: print useful info on error.
To: None <tech-toolchain@netbsd.org>
From: Simon J. Gerraty <sjg@quick.com.au>
List: tech-toolchain
Date: 05/26/2001 23:29:38
Below are some of the changes I've recently made to bmake (autoconf'd
version of netbsd make), that I think would be useful to add to our
make.

In particular, I find it very handy if make reports .CURDIR when it
bails.  With the diffs below, instead of just:

*** Error code 1

Stop.

we get:


*** Error code 1

Stop.
bmake: stopped in /u0/share/arch/NetBSD/i386/src/sjg/bin/bmake


This makes finding the problem when a build of something like /usr/src
blows up much simpler.

The extra goop in PrintOnError() lets you put say:

MAKE_PRINT_VAR_ON_ERROR=MAKE_VERSION .MAKE .OBJDIR 

and the above output becomes:

*** Error code 1

Stop.
bmake: stopped in /u0/share/arch/NetBSD/i386/src/sjg/bin/bmake
MAKE_VERSION='bmake-3.1.6 20010526'
.MAKE='/tmp/b/bmake'
.OBJDIR='/u0/share/arch/NetBSD/i386/src/sjg/bin/bmake'

Doing the :@ loop in PrintOnError makes it easy to add a newline b/w
vars, (the setting of .newline containing  "\n" works too but...).

Comments?

--sjg

Index: compat.c
===================================================================
RCS file: /share/cvsroot/bmake/compat.c,v
retrieving revision 1.7
diff -u -p -r1.7 compat.c
--- compat.c	2001/05/12 07:27:31	1.7
+++ compat.c	2001/05/26 22:56:43
@@ -483,7 +483,7 @@ CompatMake (gnp, pgnp)
 	} else if (keepgoing) {
 	    pgn->flags &= ~REMAKE;
 	} else {
-	    printf ("\n\nStop.\n");
+	    PrintOnError("\n\nStop.");
 	    exit (1);
 	}
     } else if (gn->made == ERROR) {
@@ -577,9 +577,9 @@ Compat_Run(targs)
 	gn = Targ_FindNode(".BEGIN", TARG_NOCREATE);
 	if (gn != NILGNODE) {
 	    Lst_ForEach(gn->commands, CompatRunCommand, (ClientData)gn);
-            if (gn->made == ERROR) {
-                printf("\n\nStop.\n");
-                exit(1);
+	    if (gn->made == ERROR) {
+		PrintOnError("\n\nStop.");
+		exit(1);
             }
 	}
     }
Index: main.c
===================================================================
RCS file: /share/cvsroot/bmake/main.c,v
retrieving revision 1.12
diff -u -p -r1.12 main.c
--- main.c	2001/03/05 21:22:27	1.12
+++ main.c	2001/05/27 06:16:55
@@ -643,6 +643,10 @@ main(argc, argv)
 	Var_Set(".CURDIR", curdir, VAR_GLOBAL);
 	Var_Set("MACHINE", machine, VAR_GLOBAL);
 	Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL);
+#ifdef MAKE_VERSION
+	Var_Set("MAKE_VERSION", MAKE_VERSION, VAR_GLOBAL);
+#endif
+	Var_Set(".newline", "\n", VAR_GLOBAL);
 
 	/*
 	 * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
@@ -1442,6 +1450,8 @@ Fatal(va_alist)
 	(void)fprintf(stderr, "\n");
 	(void)fflush(stderr);
 
+	PrintOnError(NULL);
+	
 	if (DEBUG(GRAPH2))
 		Targ_PrintGraph(2);
 	Trace_Log(MAKEERROR, 0);
@@ -1484,6 +1494,8 @@ Punt(va_alist)
 	(void)fprintf(stderr, "\n");
 	(void)fflush(stderr);
 
+	PrintOnError(NULL);
+	
 	DieHorribly();
 }
 
@@ -1624,3 +1636,22 @@ PrintAddr(a, b)
     printf("%lx ", (unsigned long) a);
     return b ? 0 : 0;
 }
+
+void
+PrintOnError(s)
+     char *s;
+{
+    char tmp[64];
+	
+    if (s)
+	    printf("%s", s);
+	
+    printf("\n%s: stopped in %s\n", progname, curdir);
+    strncpy(tmp, "${MAKE_PRINT_VAR_ON_ERROR:@v@$v='${$v}'\n@}",
+	    sizeof(tmp) - 1);
+    s = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+    if (s && *s)
+	printf("%s", s);
+}
+
+		
Index: make.h
===================================================================
RCS file: /share/cvsroot/bmake/make.h,v
retrieving revision 1.9
diff -u -p -r1.9 make.h
--- make.h	2001/03/05 21:22:28	1.9
+++ make.h	2001/05/26 22:58:53
@@ -429,5 +429,6 @@ void Make_DoAllVar __P((GNode *));
 Boolean Make_Run __P((Lst));
 char * Check_Cwd_Cmd __P((char *));
 void Check_Cwd __P((char **));
+void PrintOnError __P((char *));
 
 #endif /* _MAKE_H_ */
Index: parse.c
===================================================================
RCS file: /share/cvsroot/bmake/parse.c,v
retrieving revision 1.7
diff -u -p -r1.7 parse.c
--- parse.c	2001/03/05 21:22:28	1.7
+++ parse.c	2001/05/27 06:03:28
@@ -2725,6 +2725,7 @@ Parse_File(name, stream)
 	(void)fprintf(stderr,
 	    "%s: Fatal errors encountered -- cannot continue\n",
 	    progname);
+	PrintOnError(NULL);
 	exit (1);
     }
 }
Index: var.c
===================================================================
RCS file: /share/cvsroot/bmake/var.c,v
retrieving revision 1.9
diff -u -p -r1.9 var.c
--- var.c	2001/05/12 07:27:32	1.9
+++ var.c	2001/05/27 05:55:00
@@ -1243,15 +1243,16 @@ VarLoopExpand (word, addSpace, buf, loop
 {
     VarLoop_t	*loop = (VarLoop_t *) loopp;
     char *s;
+    int slen;
     
     Var_Set(loop->tvar, word, loop->ctxt);
     s = Var_Subst(NULL, loop->str, loop->ctxt, loop->err);
     if (s != NULL && *s != '\0') {
-	if (addSpace)
+	if (addSpace && *s != '\n')
 	    Buf_AddByte(buf, ' ');
 	Buf_AddBytes(buf, strlen(s), (Byte *)s);
+	addSpace = (slen > 0 && s[slen - 1] != '\n');
 	free(s);
-	addSpace = TRUE;
     }
     return addSpace;
 }