Module Name:    src
Committed By:   christos
Date:           Tue Jul 16 14:00:53 UTC 2013

Modified Files:
        src/usr.bin/make: main.c make.1 var.c

Log Message:
More gmake compatibility:
1. add -w flag to print Entering and Leaving directory name the the beginning
   and the end of processing.
2. export MAKELEVEL=$((MAKELEVEL + 1)) only in the child environment.
3. when printing error messages, prefix them with the program name [$MAKELEVEL]
   for $MAKELEVEL > 0
4. if $MAKEFLAGS consists only of letters assume it is a set of flags (as
   allowed by posix), convert them to -f -l -a -g -s, so that they get parsed
   properly.
With those fixes gmake -> bmake -> gmake -> bmake etc. works as expected.


To generate a diff of this commit:
cvs rdiff -u -r1.219 -r1.220 src/usr.bin/make/main.c
cvs rdiff -u -r1.218 -r1.219 src/usr.bin/make/make.1
cvs rdiff -u -r1.181 -r1.182 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.219 src/usr.bin/make/main.c:1.220
--- src/usr.bin/make/main.c:1.219	Mon Jul 15 16:33:11 2013
+++ src/usr.bin/make/main.c	Tue Jul 16 10:00:53 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.219 2013/07/15 20:33:11 christos Exp $	*/
+/*	$NetBSD: main.c,v 1.220 2013/07/16 14:00:53 christos Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.219 2013/07/15 20:33:11 christos Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.220 2013/07/16 14:00:53 christos Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@ __COPYRIGHT("@(#) Copyright (c) 1988, 19
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.219 2013/07/15 20:33:11 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.220 2013/07/16 14:00:53 christos Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -128,6 +128,7 @@ __RCSID("$NetBSD: main.c,v 1.219 2013/07
 #include <stdio.h>
 #include <stdlib.h>
 #include <time.h>
+#include <ctype.h>
 
 #include "make.h"
 #include "hash.h"
@@ -163,6 +164,7 @@ Boolean			noRecursiveExecute;	/* -N flag
 Boolean			keepgoing;	/* -k flag */
 Boolean			queryFlag;	/* -q flag */
 Boolean			touchFlag;	/* -t flag */
+Boolean			enterFlag;	/* -w flag */
 Boolean			ignoreErrors;	/* -i flag */
 Boolean			beSilent;	/* -s flag */
 Boolean			oldVars;	/* variable substitution style */
@@ -184,11 +186,44 @@ char curdir[MAXPATHLEN + 1];		/* Startup
 char *progname;				/* the program name */
 char *makeDependfile;
 pid_t myPid;
+int makelevel;
 
 Boolean forceJobs = FALSE;
 
 extern Lst parseIncPath;
 
+/*
+ * For compatibility with the POSIX version of MAKEFLAGS that includes
+ * all the options with out -, convert flags to -f -l -a -g -s.
+ */
+static char *
+explode(const char *flags)
+{
+    size_t len;
+    char *nf, *st;
+    const char *f;
+
+    if (flags == NULL)
+	return NULL;
+
+    for (f = flags; *f; f++)
+	if (!isalpha((unsigned char)*f))
+	    break;
+
+    if (*f)
+	return estrdup(flags);
+
+    len = strlen(flags);
+    st = nf = emalloc(len * 3 + 1);
+    while (*flags) {
+	*nf++ = '-';
+	*nf++ = *flags++;
+	*nf++ = ' ';
+    }
+    *nf = '\0';
+    return st;
+}
+	    
 static void
 parse_debug_options(const char *argvalue)
 {
@@ -341,7 +376,7 @@ MainParseArgs(int argc, char **argv)
 	Boolean inOption, dashDash = FALSE;
 	char found_path[MAXPATHLEN + 1];	/* for searching for sys.mk */
 
-#define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrst"
+#define OPTFLAGS "BC:D:I:J:NST:V:WXd:ef:ij:km:nqrstw"
 /* Can't actually use getopt(3) because rescanning is not portable */
 
 	getopt_def = OPTFLAGS;
@@ -549,6 +584,10 @@ rearg:	
 			touchFlag = TRUE;
 			Var_Append(MAKEFLAGS, "-t", VAR_GLOBAL);
 			break;
+		case 'w':
+			enterFlag = TRUE;
+			Var_Append(MAKEFLAGS, "-w", VAR_GLOBAL);
+			break;
 		case '-':
 			dashDash = TRUE;
 			break;
@@ -941,14 +980,21 @@ main(int argc, char **argv)
 	{
 	    char tmp[64], *ep;
 
-	    snprintf(tmp, sizeof(tmp), "%d",
-	        ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) + 1 : 0);
+	    makelevel = ((ep = getenv(MAKE_LEVEL_ENV)) && *ep) ? atoi(ep) : 0;
+	    if (makelevel < 0)
+		makelevel = 0;
+	    snprintf(tmp, sizeof(tmp), "%d", makelevel);
 	    Var_Set(MAKE_LEVEL, tmp, VAR_GLOBAL, 0);
 	    snprintf(tmp, sizeof(tmp), "%u", myPid);
 	    Var_Set(".MAKE.PID", tmp, VAR_GLOBAL, 0);
 	    snprintf(tmp, sizeof(tmp), "%u", getppid());
 	    Var_Set(".MAKE.PPID", tmp, VAR_GLOBAL, 0);
 	}
+	if (makelevel > 0) {
+		char pn[1024];
+		snprintf(pn, sizeof(pn), "%s[%d]", progname, makelevel);
+		progname = estrdup(pn);
+	}
 	Job_SetPrefix();
 
 #ifdef USE_META
@@ -960,7 +1006,9 @@ main(int argc, char **argv)
 	 * in a different format).
 	 */
 #ifdef POSIX
-	Main_ParseArgLine(getenv("MAKEFLAGS"));
+	p1 = explode(getenv("MAKEFLAGS"));
+	Main_ParseArgLine(p1);
+	free(p1);
 #else
 	Main_ParseArgLine(getenv("MAKE"));
 #endif
@@ -977,6 +1025,9 @@ main(int argc, char **argv)
 
 	MainParseArgs(argc, argv);
 
+	if (enterFlag)
+		printf("%s: Entering directory `%s'\n", progname, curdir);
+
 	/*
 	 * Verify that cwd is sane.
 	 */
@@ -1293,6 +1344,9 @@ main(int argc, char **argv)
 
 	Trace_Log(MAKEEND, 0);
 
+	if (enterFlag)
+		printf("%s: Leaving directory `%s'\n", progname, curdir);
+
 	Suff_End();
         Targ_End();
 	Arch_End();
@@ -1721,8 +1775,12 @@ execError(const char *af, const char *av
 static void
 usage(void)
 {
+	char *p;
+	if ((p = strchr(progname, '[')) != NULL)
+	    *p = '\0';
+
 	(void)fprintf(stderr,
-"usage: %s [-BeikNnqrstWX] \n\
+"usage: %s [-BeikNnqrstWwX] \n\
             [-C directory] [-D variable] [-d flags] [-f makefile]\n\
             [-I directory] [-J private] [-j max_jobs] [-m directory] [-T file]\n\
             [-V variable] [variable=value] [target ...]\n", progname);

Index: src/usr.bin/make/make.1
diff -u src/usr.bin/make/make.1:1.218 src/usr.bin/make/make.1:1.219
--- src/usr.bin/make/make.1:1.218	Wed Jun 26 16:20:36 2013
+++ src/usr.bin/make/make.1	Tue Jul 16 10:00:53 2013
@@ -1,4 +1,4 @@
-.\"	$NetBSD: make.1,v 1.218 2013/06/26 20:20:36 agc Exp $
+.\"	$NetBSD: make.1,v 1.219 2013/07/16 14:00:53 christos Exp $
 .\"
 .\" Copyright (c) 1990, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"	from: @(#)make.1	8.4 (Berkeley) 3/19/94
 .\"
-.Dd June 24, 2013
+.Dd July 15, 2013
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -37,7 +37,7 @@
 .Nd maintain program dependencies
 .Sh SYNOPSIS
 .Nm
-.Op Fl BeikNnqrstWX
+.Op Fl BeikNnqrstWwX
 .Op Fl C Ar directory
 .Op Fl D Ar variable
 .Op Fl d Ar flags
@@ -209,6 +209,8 @@ Force the
 option to print raw values of variables.
 .It Ar v
 Print debugging information about variable assignment.
+.It Ar w
+Print entering and leaving directory messages, pre and post processing.
 .It Ar x
 Run shell commands with
 .Fl x

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.181 src/usr.bin/make/var.c:1.182
--- src/usr.bin/make/var.c:1.181	Mon Jul 15 16:33:11 2013
+++ src/usr.bin/make/var.c	Tue Jul 16 10:00:53 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.181 2013/07/15 20:33:11 christos Exp $	*/
+/*	$NetBSD: var.c,v 1.182 2013/07/16 14:00:53 christos Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.181 2013/07/15 20:33:11 christos Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.182 2013/07/16 14:00:53 christos Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.181 2013/07/15 20:33:11 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.182 2013/07/16 14:00:53 christos Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -139,6 +139,7 @@ __RCSID("$NetBSD: var.c,v 1.181 2013/07/
 #include    "dir.h"
 #include    "job.h"
 
+extern int makelevel;
 /*
  * This lets us tell if we have replaced the original environ
  * (which we cannot free).
@@ -657,6 +658,15 @@ Var_ExportVars(void)
     char *val;
     int n;
 
+    /*
+     * Several make's support this sort of mechanism for tracking
+     * recursion - but each uses a different name.
+     * We allow the makefiles to update MAKELEVEL and ensure
+     * children see a correctly incremented value.
+     */
+    snprintf(tmp, sizeof(tmp), "%d", makelevel + 1);
+    setenv(MAKE_LEVEL_ENV, tmp, 1);
+
     if (VAR_EXPORTED_NONE == var_exportedVars)
 	return;
 
@@ -953,15 +963,6 @@ Var_Set(const char *name, const char *va
 
 	Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
     }
-    /*
-     * Another special case.
-     * Several make's support this sort of mechanism for tracking
-     * recursion - but each uses a different name.
-     * We allow the makefiles to update .MAKE.LEVEL and ensure
-     * children see a correctly incremented value.
-     */
-    if (ctxt == VAR_GLOBAL && strcmp(MAKE_LEVEL, name) == 0)
-	setenv(MAKE_LEVEL_ENV, val, 1);
 	
  out:
     if (expanded_name != NULL)

Reply via email to