Subject: pkgsrc vs MAKEFLAGS
To: None <tech-pkg@netbsd.org, tech-toolchain@netbsd.org>
From: Simon Gerraty <sjg@juniper.net>
List: tech-pkg
Date: 06/07/2001 13:07:21
Ok, the resent change to make to export command line var assignments
via MAKEFLAGS, causes problems for pkgsrc.

Since re-designing pkgsrc isn't a real option, the following patch to
make and to pkgsrc/mk/bsd.prefs.mk (better in bsd.pkg.mk?) address the
problem.  

Essentially we use .MAKEOVERRIDES to record the command line variable
assignments.  The function ExportMAKEFLAGS is called by main to do the
initial export and by Parse_DoVar when ever .MAKEOVERRIDES is assigned
to with plain '='.

This allows the POSIX behaviour to be implemented, but allows it to be
turned off by putting 

.MAKEOVERRIDES=

in a Makefile.  Gmake uses "MAKEOVERRIDES=" for the same purpose.
Should we use that, or stick to the initial '.' for internally magic
names?   Funky use of .MAKEOVERRIDES is probably not suported.

Any objections?

Thanks
--sjg

Index: bsd.prefs.mk
===================================================================
RCS file: /cvsroot/pkgsrc/mk/bsd.prefs.mk,v
retrieving revision 1.36
diff -u -r1.36 bsd.prefs.mk
--- bsd.prefs.mk	2001/05/17 15:08:17	1.36
+++ bsd.prefs.mk	2001/06/07 19:49:27
@@ -13,6 +13,9 @@
 BSD_PKG_MK=1 
 __PREFIX_SET__:=${PREFIX}
 
+# we want to forget commandline var assignments
+.MAKEOVERRIDES=
+
 .if exists(/usr/bin/uname)
 UNAME=/usr/bin/uname
 .elif exists(/bin/uname)

Index: Makefile
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/Makefile,v
retrieving revision 1.23
diff -u -r1.23 Makefile
--- Makefile	2000/12/30 02:06:42	1.23
+++ Makefile	2001/06/07 20:04:36
@@ -18,3 +18,10 @@
 
 .include <bsd.prog.mk>
 .include <bsd.subdir.mk>
+
+# provide a clue as to what we are using
+BUILD_DATE!= date +%Y%m%d
+MAKE_VERSION:= make-${BUILD_DATE}
+CPPFLAGS_main.o:= "-DMAKE_VERSION=\"${MAKE_VERSION}\""
+CPPFLAGS+= ${CPPFLAGS_${.TARGET}}
+main.o:	${SRCS} ${MAKEFILE}
Index: main.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/main.c,v
retrieving revision 1.67
diff -u -r1.67 main.c
--- main.c	2001/06/01 20:33:37	1.67
+++ main.c	2001/06/07 20:04:36
@@ -393,7 +393,7 @@
 	 */
 	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
 		if (Parse_IsVar(*argv)) {
-			Var_Append(MAKEFLAGS, *argv, VAR_GLOBAL);
+			Var_Append(MAKEOVERRIDES, *argv, VAR_GLOBAL);
 			Parse_DoVar(*argv, VAR_CMD);
 		} else {
 			if (!**argv)
@@ -511,7 +511,7 @@
 	Lst targs;	/* target nodes to create -- passed to Make_Init */
 	Boolean outOfDate = TRUE; 	/* FALSE if all targets up to date */
 	struct stat sb, sa;
-	char *p, *p1, *path, *pathp, *pwd;
+	char *p1, *path, *pathp, *pwd;
 	char mdpath[MAXPATHLEN + 1];
 	char obpath[MAXPATHLEN + 1];
 	char cdpath[MAXPATHLEN + 1];
@@ -711,6 +711,7 @@
 	Var_Set("MAKE", argv[0], VAR_GLOBAL);
 	Var_Set(".MAKE", argv[0], VAR_GLOBAL);
 	Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
+	Var_Set(MAKEOVERRIDES, "", VAR_GLOBAL);
 	Var_Set("MFLAGS", "", VAR_GLOBAL);
 
 	/*
@@ -832,15 +833,7 @@
 	    printf("job_pipe %d %d, maxjobs %d maxlocal %d compat %d\n", job_pipe[0], job_pipe[1], maxJobs,
 	           maxLocal, compatMake);
 
-	/* Install all the flags into the MAKE envariable. */
-	if (((p = Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1)) != NULL) && *p)
-#ifdef POSIX
-		setenv("MAKEFLAGS", p, 1);
-#else
-		setenv("MAKE", p, 1);
-#endif
-	if (p1)
-	    free(p1);
+	ExportMAKEFLAGS(1);		/* initial export */
 
 	Check_Cwd_av(0, NULL, 0);	/* initialize it */
 	
@@ -1659,4 +1652,28 @@
     s = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
     if (s && *s)
 	printf("%s", s);
+}
+
+void
+ExportMAKEFLAGS(first)
+    int first;
+{
+    static int once = 1;
+    char tmp[64];
+    char *s;
+
+    if (once != first)
+	return;
+    once = 0;
+    
+    /* supress duplicates in MAKEOVERRIDES */
+    strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u}", sizeof(tmp));
+    s = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+    if (s && *s) {
+#ifdef POSIX
+	setenv("MAKEFLAGS", s, 1);
+#else
+	setenv("MAKE", s, 1);
+#endif
+    }
 }
Index: make.h
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/make.h,v
retrieving revision 1.34
diff -u -r1.34 make.h
--- make.h	2001/06/01 20:33:37	1.34
+++ make.h	2001/06/07 20:04:36
@@ -367,6 +367,7 @@
 extern char	*progname;	/* The program name */
 
 #define	MAKEFLAGS	".MAKEFLAGS"
+#define	MAKEOVERRIDES	".MAKEOVERRIDES"
 
 /*
  * debug control:
@@ -413,5 +414,6 @@
 char * Check_Cwd_Cmd __P((char *));
 void Check_Cwd __P((char **));
 void PrintOnError __P((char *));
+void ExportMAKEFLAGS __P((int));
 
 #endif /* _MAKE_H_ */
Index: parse.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/parse.c,v
retrieving revision 1.67
diff -u -r1.67 parse.c
--- parse.c	2001/06/02 18:04:44	1.67
+++ parse.c	2001/06/07 20:04:36
@@ -1585,6 +1585,8 @@
 	 * Normal assignment -- just do it.
 	 */
 	Var_Set(line, cp, ctxt);
+	if (strcmp(line, MAKEOVERRIDES) == 0)
+	    ExportMAKEFLAGS(0);		/* re-export MAKEFLAGS */
     }
 }