Module Name:    src
Committed By:   sjg
Date:           Wed Sep  9 17:09:49 UTC 2009

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

Log Message:
We must delay setting .CURDIR and .OBJDIR until after MainParseArgs()
in case -C is used - in which case we should also ignore $PWD.


To generate a diff of this commit:
cvs rdiff -u -r1.173 -r1.174 src/usr.bin/make/main.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.173 src/usr.bin/make/main.c:1.174
--- src/usr.bin/make/main.c:1.173	Tue Sep  8 17:29:20 2009
+++ src/usr.bin/make/main.c	Wed Sep  9 17:09:49 2009
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.173 2009/09/08 17:29:20 sjg Exp $	*/
+/*	$NetBSD: main.c,v 1.174 2009/09/09 17:09:49 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.173 2009/09/08 17:29:20 sjg Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.174 2009/09/09 17:09:49 sjg Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.173 2009/09/08 17:29:20 sjg Exp $");
+__RCSID("$NetBSD: main.c,v 1.174 2009/09/09 17:09:49 sjg Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -180,6 +180,7 @@
 static int		ReadMakefile(const void *, const void *);
 static void		usage(void);
 
+static Boolean		ignorePWD;	/* if we use -C, PWD is meaningless */
 static char curdir[MAXPATHLEN + 1];	/* startup directory */
 static char objdir[MAXPATHLEN + 1];	/* where we chdir'ed to */
 char *progname;				/* the program name */
@@ -392,6 +393,7 @@
 					      strerror(errno));
 				exit(1);
 			}
+			ignorePWD = TRUE;
 			break;
 		case 'D':
 			if (argvalue == NULL || argvalue[0] == 0) goto noarg;
@@ -759,40 +761,6 @@
 		}
 	}
 #endif
-	/*
-	 * Find where we are and take care of PWD for the automounter...
-	 * All this code is so that we know where we are when we start up
-	 * on a different machine with pmake.
-	 */
-	if (getcwd(curdir, MAXPATHLEN) == NULL) {
-		(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
-		exit(2);
-	}
-
-	if (stat(curdir, &sa) == -1) {
-	    (void)fprintf(stderr, "%s: %s: %s.\n",
-		 progname, curdir, strerror(errno));
-	    exit(2);
-	}
-
-	/*
-	 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
-	 * since the value of curdir can very depending on how we got
-	 * here.  Ie sitting at a shell prompt (shell that provides $PWD)
-	 * or via subdir.mk in which case its likely a shell which does
-	 * not provide it.
-	 * So, to stop it breaking this case only, we ignore PWD if
-	 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
-	 */
-	if ((pwd = getenv("PWD")) != NULL && getenv("MAKEOBJDIRPREFIX") == NULL) {
-		const char *makeobjdir = getenv("MAKEOBJDIR");
-
-		if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
-			if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
-			    sa.st_dev == sb.st_dev)
-				(void)strncpy(curdir, pwd, MAXPATHLEN);
-		}
-	}
 
 	/*
 	 * Get the name of this type of MACHINE from utsname
@@ -836,7 +804,6 @@
 	 */
 	Var_Init();		/* Initialize the lists of variables for
 				 * parsing arguments */
-	Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
 	Var_Set("MACHINE", machine, VAR_GLOBAL, 0);
 	Var_Set("MACHINE_ARCH", machine_arch, VAR_GLOBAL, 0);
 #ifdef MAKE_VERSION
@@ -844,32 +811,6 @@
 #endif
 	Var_Set(".newline", "\n", VAR_GLOBAL, 0); /* handy for :@ loops */
 
-	/*
-	 * Find the .OBJDIR.  If MAKEOBJDIRPREFIX, or failing that,
-	 * MAKEOBJDIR is set in the environment, try only that value
-	 * and fall back to .CURDIR if it does not exist.
-	 *
-	 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and
-	 * finally _PATH_OBJDIRPREFIX`pwd`, in that order.  If none
-	 * of these paths exist, just use .CURDIR.
-	 */
-	Dir_Init(curdir);
-	(void)Main_SetObjdir(curdir);
-
-	if ((path = getenv("MAKEOBJDIRPREFIX")) != NULL) {
-		(void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir);
-		(void)Main_SetObjdir(mdpath);
-	} else if ((path = getenv("MAKEOBJDIR")) != NULL) {
-		(void)Main_SetObjdir(path);
-	} else {
-		(void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine);
-		if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) {
-			(void)snprintf(mdpath, MAXPATHLEN, "%s%s", 
-					_PATH_OBJDIRPREFIX, curdir);
-			(void)Main_SetObjdir(mdpath);
-		}
-	}
-
 	create = Lst_Init(FALSE);
 	makefiles = Lst_Init(FALSE);
 	printVars = FALSE;
@@ -889,7 +830,7 @@
 	maxJobs = DEFMAXLOCAL;		/* Set default local max concurrency */
 	maxJobTokens = maxJobs;
 	compatMake = FALSE;		/* No compat mode */
-
+	ignorePWD = FALSE;
 
 	/*
 	 * Initialize the parsing, directory and variable modules to prepare
@@ -943,6 +884,70 @@
 	MainParseArgs(argc, argv);
 
 	/*
+	 * Find where we are (now) and take care of PWD for the automounter...
+	 * All this code is so that we know where we are when we start up
+	 * on a different machine with pmake.
+	 */
+	if (getcwd(curdir, MAXPATHLEN) == NULL) {
+		(void)fprintf(stderr, "%s: %s.\n", progname, strerror(errno));
+		exit(2);
+	}
+
+	if (stat(curdir, &sa) == -1) {
+	    (void)fprintf(stderr, "%s: %s: %s.\n",
+		 progname, curdir, strerror(errno));
+	    exit(2);
+	}
+
+	/*
+	 * Overriding getcwd() with $PWD totally breaks MAKEOBJDIRPREFIX
+	 * since the value of curdir can vary depending on how we got
+	 * here.  Ie sitting at a shell prompt (shell that provides $PWD)
+	 * or via subdir.mk in which case its likely a shell which does
+	 * not provide it.
+	 * So, to stop it breaking this case only, we ignore PWD if
+	 * MAKEOBJDIRPREFIX is set or MAKEOBJDIR contains a transform.
+	 */
+	if (!ignorePWD &&
+	    (pwd = getenv("PWD")) != NULL &&
+	    getenv("MAKEOBJDIRPREFIX") == NULL) {
+		const char *makeobjdir = getenv("MAKEOBJDIR");
+
+		if (makeobjdir == NULL || !strchr(makeobjdir, '$')) {
+			if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino &&
+			    sa.st_dev == sb.st_dev)
+				(void)strncpy(curdir, pwd, MAXPATHLEN);
+		}
+	}
+	Var_Set(".CURDIR", curdir, VAR_GLOBAL, 0);
+
+	/*
+	 * Find the .OBJDIR.  If MAKEOBJDIRPREFIX, or failing that,
+	 * MAKEOBJDIR is set in the environment, try only that value
+	 * and fall back to .CURDIR if it does not exist.
+	 *
+	 * Otherwise, try _PATH_OBJDIR.MACHINE, _PATH_OBJDIR, and
+	 * finally _PATH_OBJDIRPREFIX`pwd`, in that order.  If none
+	 * of these paths exist, just use .CURDIR.
+	 */
+	Dir_Init(curdir);
+	(void)Main_SetObjdir(curdir);
+
+	if ((path = getenv("MAKEOBJDIRPREFIX")) != NULL) {
+		(void)snprintf(mdpath, MAXPATHLEN, "%s%s", path, curdir);
+		(void)Main_SetObjdir(mdpath);
+	} else if ((path = getenv("MAKEOBJDIR")) != NULL) {
+		(void)Main_SetObjdir(path);
+	} else {
+		(void)snprintf(mdpath, MAXPATHLEN, "%s.%s", _PATH_OBJDIR, machine);
+		if (!Main_SetObjdir(mdpath) && !Main_SetObjdir(_PATH_OBJDIR)) {
+			(void)snprintf(mdpath, MAXPATHLEN, "%s%s", 
+					_PATH_OBJDIRPREFIX, curdir);
+			(void)Main_SetObjdir(mdpath);
+		}
+	}
+
+	/*
 	 * Be compatible if user did not specify -j and did not explicitly
 	 * turned compatibility on
 	 */

Reply via email to