From 80b1e4e59d0c36ef0988570d6f32f560c1b7e0ff Mon Sep 17 00:00:00 2001
From: Ron Yorston <rmy@pobox.com>
Date: Sun, 16 Jun 2024 11:29:21 +0100
Subject: make: update default rules

The default rules were changed in the 2024 standard:

- Fortran is no longer supported

- The CC macro is set to 'c17' instead of 'c99'

In addition to these changes, CC is set to 'cc' as a non-POSIX
extension:  on my system, at least, there isn't a 'c17' compiler.

Adds 208-224 bytes.
---
 miscutils/make.c | 63 +++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 49 insertions(+), 14 deletions(-)

diff --git a/miscutils/make.c b/miscutils/make.c
index 02f90f805..07e83166b 100644
--- a/miscutils/make.c
+++ b/miscutils/make.c
@@ -271,6 +271,7 @@ struct globals {
 	int lineno;	// Physical line number in file
 	int dispno;	// Line number for display purposes
 	const char *rulepos;
+	int rule_idx;
 #define IF_MAX 10
 	uint8_t clevel;
 	uint8_t cstate[IF_MAX + 1];
@@ -301,6 +302,7 @@ struct globals {
 #define lineno		(G.lineno)
 #define dispno		(G.dispno)
 #define rulepos		(G.rulepos)
+#define rule_idx	(G.rule_idx)
 #define clevel		(G.clevel)
 #define cstate		(G.cstate)
 #define numjobs		(G.numjobs)
@@ -1020,11 +1022,8 @@ dyndep(struct name *np, struct rule *imprule)
 }
 
 #define RULES \
-	".SUFFIXES:.o .c .y .l .a .sh .f\n" \
 	".c.o:\n" \
 	"	$(CC) $(CFLAGS) -c $<\n" \
-	".f.o:\n" \
-	"	$(FC) $(FFLAGS) -c $<\n" \
 	".y.o:\n" \
 	"	$(YACC) $(YFLAGS) $<\n" \
 	"	$(CC) $(CFLAGS) -c y.tab.c\n" \
@@ -1045,23 +1044,28 @@ dyndep(struct name *np, struct rule *imprule)
 	"	$(CC) -c $(CFLAGS) $<\n" \
 	"	$(AR) $(ARFLAGS) $@ $*.o\n" \
 	"	rm -f $*.o\n" \
-	".f.a:\n" \
-	"	$(FC) -c $(FFLAGS) $<\n" \
-	"	$(AR) $(ARFLAGS) $@ $*.o\n" \
-	"	rm -f $*.o\n" \
 	".c:\n" \
 	"	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<\n" \
-	".f:\n" \
-	"	$(FC) $(FFLAGS) $(LDFLAGS) -o $@ $<\n" \
 	".sh:\n" \
 	"	cp $< $@\n" \
 	"	chmod a+x $@\n"
 
+#define RULES_2017 \
+	".SUFFIXES:.o .c .y .l .a .sh .f\n" \
+	".f.o:\n" \
+	"	$(FC) $(FFLAGS) -c $<\n" \
+	".f.a:\n" \
+	"	$(FC) -c $(FFLAGS) $<\n" \
+	"	$(AR) $(ARFLAGS) $@ $*.o\n" \
+	"	rm -f $*.o\n" \
+	".f:\n" \
+	"	$(FC) $(FFLAGS) $(LDFLAGS) -o $@ $<\n"
+
+#define RULES_2024 \
+	".SUFFIXES:.o .c .y .l .a .sh\n"
+
 #define MACROS \
-	"CC=c99\n" \
 	"CFLAGS=-O1\n" \
-	"FC=fort77\n" \
-	"FFLAGS=-O1\n" \
 	"YACC=yacc\n" \
 	"YFLAGS=\n" \
 	"LEX=lex\n" \
@@ -1070,6 +1074,17 @@ dyndep(struct name *np, struct rule *imprule)
 	"ARFLAGS=-rv\n" \
 	"LDFLAGS=\n"
 
+#define MACROS_2017 \
+	"CC=c99\n" \
+	"FC=fort77\n" \
+	"FFLAGS=-O1\n" \
+
+#define MACROS_2024 \
+	"CC=c17\n"
+
+#define MACROS_EXT \
+	"CC=cc\n"
+
 /*
  * Read the built-in rules using a fake fgets-like interface.
  */
@@ -1078,8 +1093,28 @@ getrules(char *s, int size)
 {
 	char *r = s;
 
-	if (rulepos == NULL)
-		rulepos = (RULES MACROS) + (norules ? sizeof(RULES) - 1 : 0);
+	if (rulepos == NULL || *rulepos == '\0') {
+		if (rule_idx == 0) {
+			rulepos = MACROS;
+			rule_idx++;
+		} else if (rule_idx == 1) {
+			if (POSIX_2017)
+				rulepos = MACROS_2017;
+			else if (posix)
+				rulepos = MACROS_2024;
+			else
+				rulepos = MACROS_EXT;
+			rule_idx++;
+		} else if (!norules) {
+			if (rule_idx == 2) {
+				rulepos = POSIX_2017 ? RULES_2017 : RULES_2024;
+				rule_idx++;
+			} else if (rule_idx == 3) {
+				rulepos = RULES;
+				rule_idx++;
+			}
+		}
+	}
 
 	if (*rulepos == '\0')
 		return NULL;
-- 
cgit v1.2.3-55-g6feb