Subject: make: putting command line vars in MAKEFLAGS
To: None <tech-toolchain@netbsd.org>
From: Simon J. Gerraty <sjg@quick.com.au>
List: tech-toolchain
Date: 05/30/2001 23:17:10
It was mentioned on one of our lists that our make does not propagate
command line variable settings to child makes via MAKEFLAGS.  This is
a bug? I'd been thinking about actually... the diff below seems to
achieve the goal with minimal fuss.

Also, I made ReadMakefile _not_ set MAKEFILE if reading ".depend"
which rather defeats the usefulness of ${MAKEFILE}. Here's a simple
test makefile:

VAR=default

all:
	@echo VAR=${VAR}

rec:
	${MAKE} -f ${MAKEFILE} all

with our current make (and no .depend file to be read):

$ make 
VAR=default
$ make VAR=foo
VAR=foo
$ make VAR=foo rec
...
VAR=default
$

with the diff below, we get VAR=foo as output - which I believe is
more correct.  Oh and the above makefile works even if there is a
.depend file ;-)

The diff below is against bmake, but the change to our make will be
(almost) identical.

Any objections?
--sjg

Index: main.c
===================================================================
RCS file: /share/cvsroot/bmake/main.c,v
retrieving revision 1.15
diff -u -p -r1.15 main.c
--- main.c	2001/05/29 10:03:10	1.15
+++ main.c	2001/05/31 06:13:32
@@ -405,9 +405,10 @@ rearg:	while((c = getopt(argc, argv, OPT
 	 * on the end of the "create" list.
 	 */
 	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
-		if (Parse_IsVar(*argv))
+		if (Parse_IsVar(*argv)) {
+			Var_Append(MAKEFLAGS, *argv, VAR_GLOBAL);
 			Parse_DoVar(*argv, VAR_CMD);
-		else {
+		} else {
 			if (!**argv)
 				Punt("illegal (null) argument.");
 			if (**argv == '-') {
@@ -1022,11 +1023,14 @@ ReadMakefile(p, q)
 	FILE *stream;
 	size_t len = MAXPATHLEN;
 	char *name, *path = emalloc(len);
+	int setMAKEFILE;
 
 	if (!strcmp(fname, "-")) {
 		Parse_File("(stdin)", stdin);
 		Var_Set("MAKEFILE", "", VAR_GLOBAL);
 	} else {
+		setMAKEFILE = strcmp(fname, ".depend");
+		
 		/* if we've chdir'd, rebuild the path name */
 		if (curdir != objdir && *fname != '/') {
 			size_t plen = strlen(curdir) + strlen(fname) + 2;
@@ -1054,7 +1058,9 @@ ReadMakefile(p, q)
 		 * placement of the setting here means it gets set to the last
 		 * makefile specified, as it is set by SysV make.
 		 */
-found:		Var_Set("MAKEFILE", fname, VAR_GLOBAL);
+found:
+		if (setMAKEFILE)
+			Var_Set("MAKEFILE", fname, VAR_GLOBAL);
 		Parse_File(fname, stream);
 		(void)fclose(stream);
 	}