Module Name:    src
Committed By:   sjg
Date:           Fri Mar 25 21:16:04 UTC 2022

Modified Files:
        src/usr.bin/make: make.1 parse.c

Log Message:
Include posix.mk when .POSIX: first encountered

Since .POSIX: is required to be the first non-comment line
in a Makefile, including ${MAKE_POSIX_MK} or whatever _PATH_POSIX_MK is
set to at this point is equivalent to an extension of sys.mk

This is a minimal change that can allow a better approximation of
POSIX compliance

Reviewed by: rillig


To generate a diff of this commit:
cvs rdiff -u -r1.305 -r1.306 src/usr.bin/make/make.1
cvs rdiff -u -r1.667 -r1.668 src/usr.bin/make/parse.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/make.1
diff -u src/usr.bin/make/make.1:1.305 src/usr.bin/make/make.1:1.306
--- src/usr.bin/make/make.1:1.305	Wed Feb  9 21:09:24 2022
+++ src/usr.bin/make/make.1	Fri Mar 25 21:16:04 2022
@@ -1,4 +1,4 @@
-.\"	$NetBSD: make.1,v 1.305 2022/02/09 21:09:24 rillig Exp $
+.\"	$NetBSD: make.1,v 1.306 2022/03/25 21:16:04 sjg 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 January 28, 2022
+.Dd March 24, 2022
 .Dt MAKE 1
 .Os
 .Sh NAME
@@ -2289,6 +2289,18 @@ The suffix must have been previously dec
 Apply the
 .Ic .PHONY
 attribute to any specified sources.
+.It Ic .POSIX
+This should be the first non-comment line in a Makefile.
+It results in the variable
+.Va %POSIX
+being defined with the value
+.Ql 1003.2 .
+The first time
+.Ic .POSIX
+is encountered, the makefile
+.Ql posix.mk
+will be included if possible,
+to provide POSIX compatible default rules.
 .It Ic .PRECIOUS
 Apply the
 .Ic .PRECIOUS

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.667 src/usr.bin/make/parse.c:1.668
--- src/usr.bin/make/parse.c:1.667	Thu Mar  3 19:55:27 2022
+++ src/usr.bin/make/parse.c	Fri Mar 25 21:16:04 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.667 2022/03/03 19:55:27 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.668 2022/03/25 21:16:04 sjg Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.667 2022/03/03 19:55:27 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.668 2022/03/25 21:16:04 sjg Exp $");
 
 /*
  * A file being read.
@@ -1115,6 +1115,121 @@ ClearPaths(SearchPathList *paths)
 	Dir_SetPATH();
 }
 
+/*
+ * Handle one of the .[-ds]include directives by remembering the current file
+ * and pushing the included file on the stack.  After the included file has
+ * finished, parsing continues with the including file; see Parse_PushInput
+ * and ParseEOF.
+ *
+ * System includes are looked up in sysIncPath, any other includes are looked
+ * up in the parsedir and then in the directories specified by the -I command
+ * line options.
+ */
+static void
+IncludeFile(const char *file, bool isSystem, bool depinc, bool silent)
+{
+	Buffer buf;
+	char *fullname;		/* full pathname of file */
+	char *newName;
+	char *slash, *incdir;
+	int fd;
+	int i;
+
+	fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
+
+	if (fullname == NULL && !isSystem) {
+		/*
+		 * Include files contained in double-quotes are first searched
+		 * relative to the including file's location. We don't want to
+		 * cd there, of course, so we just tack on the old file's
+		 * leading path components and call Dir_FindFile to see if
+		 * we can locate the file.
+		 */
+
+		incdir = bmake_strdup(CurFile()->name.str);
+		slash = strrchr(incdir, '/');
+		if (slash != NULL) {
+			*slash = '\0';
+			/*
+			 * Now do lexical processing of leading "../" on the
+			 * filename.
+			 */
+			for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
+				slash = strrchr(incdir + 1, '/');
+				if (slash == NULL || strcmp(slash, "/..") == 0)
+					break;
+				*slash = '\0';
+			}
+			newName = str_concat3(incdir, "/", file + i);
+			fullname = Dir_FindFile(newName, parseIncPath);
+			if (fullname == NULL)
+				fullname = Dir_FindFile(newName,
+				    &dirSearchPath);
+			free(newName);
+		}
+		free(incdir);
+
+		if (fullname == NULL) {
+			/*
+			 * Makefile wasn't found in same directory as included
+			 * makefile.
+			 *
+			 * Search for it first on the -I search path, then on
+			 * the .PATH search path, if not found in a -I
+			 * directory. If we have a suffix-specific path, we
+			 * should use that.
+			 */
+			const char *suff;
+			SearchPath *suffPath = NULL;
+
+			if ((suff = strrchr(file, '.')) != NULL) {
+				suffPath = Suff_GetPath(suff);
+				if (suffPath != NULL)
+					fullname = Dir_FindFile(file, suffPath);
+			}
+			if (fullname == NULL) {
+				fullname = Dir_FindFile(file, parseIncPath);
+				if (fullname == NULL)
+					fullname = Dir_FindFile(file,
+					    &dirSearchPath);
+			}
+		}
+	}
+
+	/* Looking for a system file or file still not found */
+	if (fullname == NULL) {
+		/*
+		 * Look for it on the system path
+		 */
+		SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
+		    ? defSysIncPath : sysIncPath;
+		fullname = Dir_FindFile(file, path);
+	}
+
+	if (fullname == NULL) {
+		if (!silent)
+			Parse_Error(PARSE_FATAL, "Could not find %s", file);
+		return;
+	}
+
+	/* Actually open the file... */
+	fd = open(fullname, O_RDONLY);
+	if (fd == -1) {
+		if (!silent)
+			Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
+		free(fullname);
+		return;
+	}
+
+	buf = loadfile(fullname, fd);
+	(void)close(fd);
+
+	Parse_PushInput(fullname, 1, 0, buf, NULL);
+	if (depinc)
+		doing_depend = depinc;	/* only turn it on */
+	free(fullname);
+}
+
 /* Handle a "dependency" line like '.SPECIAL:' without any sources. */
 static void
 HandleDependencySourcesEmpty(ParseSpecial special, SearchPathList *paths)
@@ -1138,6 +1253,23 @@ HandleDependencySourcesEmpty(ParseSpecia
 #ifdef POSIX
 	case SP_POSIX:
 		Global_Set("%POSIX", "1003.2");
+		{
+			static bool first_posix = true;
+
+			/*
+			 * Since .POSIX: should be the first
+			 * operative line in a makefile,
+			 * if '-r' flag is used, no default rules have
+			 * been read yet, in which case 'posix.mk' can
+			 * be a substiute for 'sys.mk'.
+			 * If '-r' is not used, then 'posix.mk' acts
+			 * as an extension of 'sys.mk'.
+			 */
+			if (first_posix) {
+				first_posix = false;
+				IncludeFile("posix.mk", true, false, true);
+			}
+		}
 		break;
 #endif
 	default:
@@ -1823,120 +1955,6 @@ Parse_AddIncludeDir(const char *dir)
 	(void)SearchPath_Add(parseIncPath, dir);
 }
 
-/*
- * Handle one of the .[-ds]include directives by remembering the current file
- * and pushing the included file on the stack.  After the included file has
- * finished, parsing continues with the including file; see Parse_PushInput
- * and ParseEOF.
- *
- * System includes are looked up in sysIncPath, any other includes are looked
- * up in the parsedir and then in the directories specified by the -I command
- * line options.
- */
-static void
-IncludeFile(const char *file, bool isSystem, bool depinc, bool silent)
-{
-	Buffer buf;
-	char *fullname;		/* full pathname of file */
-	char *newName;
-	char *slash, *incdir;
-	int fd;
-	int i;
-
-	fullname = file[0] == '/' ? bmake_strdup(file) : NULL;
-
-	if (fullname == NULL && !isSystem) {
-		/*
-		 * Include files contained in double-quotes are first searched
-		 * relative to the including file's location. We don't want to
-		 * cd there, of course, so we just tack on the old file's
-		 * leading path components and call Dir_FindFile to see if
-		 * we can locate the file.
-		 */
-
-		incdir = bmake_strdup(CurFile()->name.str);
-		slash = strrchr(incdir, '/');
-		if (slash != NULL) {
-			*slash = '\0';
-			/*
-			 * Now do lexical processing of leading "../" on the
-			 * filename.
-			 */
-			for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
-				slash = strrchr(incdir + 1, '/');
-				if (slash == NULL || strcmp(slash, "/..") == 0)
-					break;
-				*slash = '\0';
-			}
-			newName = str_concat3(incdir, "/", file + i);
-			fullname = Dir_FindFile(newName, parseIncPath);
-			if (fullname == NULL)
-				fullname = Dir_FindFile(newName,
-				    &dirSearchPath);
-			free(newName);
-		}
-		free(incdir);
-
-		if (fullname == NULL) {
-			/*
-			 * Makefile wasn't found in same directory as included
-			 * makefile.
-			 *
-			 * Search for it first on the -I search path, then on
-			 * the .PATH search path, if not found in a -I
-			 * directory. If we have a suffix-specific path, we
-			 * should use that.
-			 */
-			const char *suff;
-			SearchPath *suffPath = NULL;
-
-			if ((suff = strrchr(file, '.')) != NULL) {
-				suffPath = Suff_GetPath(suff);
-				if (suffPath != NULL)
-					fullname = Dir_FindFile(file, suffPath);
-			}
-			if (fullname == NULL) {
-				fullname = Dir_FindFile(file, parseIncPath);
-				if (fullname == NULL)
-					fullname = Dir_FindFile(file,
-					    &dirSearchPath);
-			}
-		}
-	}
-
-	/* Looking for a system file or file still not found */
-	if (fullname == NULL) {
-		/*
-		 * Look for it on the system path
-		 */
-		SearchPath *path = Lst_IsEmpty(&sysIncPath->dirs)
-		    ? defSysIncPath : sysIncPath;
-		fullname = Dir_FindFile(file, path);
-	}
-
-	if (fullname == NULL) {
-		if (!silent)
-			Parse_Error(PARSE_FATAL, "Could not find %s", file);
-		return;
-	}
-
-	/* Actually open the file... */
-	fd = open(fullname, O_RDONLY);
-	if (fd == -1) {
-		if (!silent)
-			Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
-		free(fullname);
-		return;
-	}
-
-	buf = loadfile(fullname, fd);
-	(void)close(fd);
-
-	Parse_PushInput(fullname, 1, 0, buf, NULL);
-	if (depinc)
-		doing_depend = depinc;	/* only turn it on */
-	free(fullname);
-}
 
 /*
  * Parse a directive like '.include' or '.-include'.

Reply via email to