Subject: Re: latest make in -current broken?
To: None <apb@cequrux.com>
From: Simon J. Gerraty <sjg@quick.com.au>
List: current-users
Date: 06/09/2001 16:04:12
> > I originally used that, but it broke the use of :O:u for duplicate
> > supression.   But of coures I'm not using that now.  Yes :Q would be
> > better.   Thanks.
> 
> Hmm.  I suspect that teaching brk_string() about backslash
> quoting would be the right thing to fix :O and :u

Actually, I've taken a different approach altogether.
Previously I found that just ${.MAKEOVERRIDES:Q} did not quite do the
right thing - which is why I went to 'quotes' but that's a losing
game.

So, now I simply append the name of the vars to .MAKEOVERRIDES
and in ExportMAKEFLAGS use ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}
which will do duplicate suppression on the names, and then :Q quote
the values.  The result seems correct.  Given:

# $Id: varcmd,v 1.1 2001/06/06 07:07:27 sjg Exp sjg $
#
# Test behaviour of recursive make and vars set on command line.

FU=fu
FOO?=foo
.if !empty(.TARGETS)
TAG=${.TARGETS}
.endif
TAG?=default

all:	one

show:
	@echo "${TAG} FU=<v>${FU}</v> FOO=<v>${FOO}</v>"

one:	show
	${MAKE} -f ${MAKEFILE} FU=bar FOO=goo two

two:	show
	${MAKE} -f ${MAKEFILE} three

three:	show
	@echo MAKEFLAGS=${MAKEFLAGS}

four:	show
	${MAKE} -f ${MAKEFILE} two

$ make -dx -f /users/sjg/make-tests/varcmd FOO="one two" FU="This is' it" four
+ echo four FU=<v>This is' it</v> FOO=<v>one two</v>
four FU=<v>This is' it</v> FOO=<v>one two</v>
/var/obj/u3/NetBSD/current/src/usr.bin/make/make -f /users/sjg/make-tests/varcmd two
+ echo two FU=<v>This is' it</v> FOO=<v>one two</v>
two FU=<v>This is' it</v> FOO=<v>one two</v>
/var/obj/u3/NetBSD/current/src/usr.bin/make/make -f /users/sjg/make-tests/varcmd three
+ echo three FU=<v>This is' it</v> FOO=<v>one two</v>
three FU=<v>This is' it</v> FOO=<v>one two</v>
+ echo MAKEFLAGS= -d x FOO=one two FU=This is' it
MAKEFLAGS= -d x FOO=one two FU=This is' it

Patch is:

Index: main.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/main.c,v
retrieving revision 1.69
diff -u -p -r1.69 main.c
--- main.c	2001/06/09 05:57:31	1.69
+++ main.c	2001/06/09 22:54:20
@@ -1665,8 +1665,9 @@ ExportMAKEFLAGS(first)
 	return;
     once = 0;
     
-    strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES}", sizeof(tmp));
-    s = Var_Subst(NULL, tmp, VAR_GLOBAL, 0);
+    strncpy(tmp, "${.MAKEFLAGS} ${.MAKEOVERRIDES:O:u:@v@$v=${$v:Q}@}",
+	    sizeof(tmp));
+    s = Var_Subst(NULL, tmp, VAR_CMD, 0);
     if (s && *s) {
 #ifdef POSIX
 	setenv("MAKEFLAGS", s, 1);
Index: var.c
===================================================================
RCS file: /cvsroot/basesrc/usr.bin/make/var.c,v
retrieving revision 1.63
diff -u -p -r1.63 var.c
--- var.c	2001/06/09 05:57:31	1.63
+++ var.c	2001/06/09 22:54:25
@@ -468,43 +468,10 @@ Var_Set (name, val, ctxt)
      * to the environment (as per POSIX standard)
      */
     if (ctxt == VAR_CMD) {
-	char tmp[256];
-	char *exp;
-	char *s;
-	int nbytes;
-	
+
 	setenv(name, val, 1);
 
-	/*
-	 * Add name='val' to .MAKEOVERRIDES
-	 * We actually want the equivalent of:
-	 * .MAKEOVERRIDES:= ${.MAKEOVERRIDES:Nname=*} name='val'
-	 * clearing the previous value for name is important, since
-	 * doing simple duplicate suppression does not handle:
-	 * $ make FOO=goo
-	 * which might run a sub-make with FOO=boo
-	 * the commands from that sub-make should see just FOO=boo.
-	 */
-	exp = 0;
-	if ((nbytes = snprintf(tmp, sizeof(tmp), "${%s:N%s=*} %s='%s'",
-			       MAKEOVERRIDES, name, name, val))
-	    < sizeof(tmp)) {
-	    exp = tmp;
-	} else {
-	    /* alloca is handy, but not everyone has it. */
-	    if ((exp = malloc(nbytes + 1)))
-		snprintf(exp, nbytes + 1, "${%s:N%s=*} %s='%s'",
-			       MAKEOVERRIDES, name, name, val);
-	}
-	if (exp) {
-	    s = Var_Subst(NULL, exp, VAR_GLOBAL, 0);
-	    if (s) {
-		    Var_Set(MAKEOVERRIDES, s, VAR_GLOBAL);
-		    free(s);
-	    }
-	    if (exp != tmp)
-		free(exp);
-	}
+	Var_Append(MAKEOVERRIDES, name, VAR_GLOBAL);
     }
     if (name != cp)
 	free(name);