commit 5c285d7

shrub  ·  2026-04-28 10:46:25 +0000 UTC
parent 5043282
define builtins in a less stupid way like pdpmake
1 files changed,  +70, -129
+70, -129
  1@@ -1,6 +1,7 @@
  2 #include "posix.h"
  3 
  4 #include <unistd.h>
  5+#include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 
  9@@ -30,65 +31,6 @@ sufactive(const struct SuffixList *list, const char *suf)
 10 	return sufidx(list, suf) >= 0;
 11 }
 12 
 13-static void
 14-addimprecipe(struct RecipeList *recipes, const char *line)
 15-{
 16-	addrecipe(recipes, line);
 17-}
 18-
 19-static void
 20-addimpsingle(struct SufRules *rules, const char *from, const char *const *recipev, size_t nrecipe)
 21-{
 22-	size_t i, k;
 23-	struct SingleSufRule *sr;
 24-
 25-	for (i = 0; i < rules->nsinglesuf; i++) {
 26-		if (strcmp(rules->singlesuf[i].from, from) == 0) {
 27-			freerecipes(&rules->singlesuf[i].recipes);
 28-			memset(&rules->singlesuf[i].recipes, 0, sizeof(rules->singlesuf[i].recipes));
 29-			for (k = 0; k < nrecipe; k++)
 30-				addimprecipe(&rules->singlesuf[i].recipes, recipev[k]);
 31-			return;
 32-		}
 33-	}
 34-	rules->singlesuf = xrealloc(rules->singlesuf,
 35-	                            (rules->nsinglesuf + 1) * sizeof(rules->singlesuf[0]));
 36-	sr = &rules->singlesuf[rules->nsinglesuf++];
 37-	memset(sr, 0, sizeof(*sr));
 38-	sr->from = xstrdup(from);
 39-	for (k = 0; k < nrecipe; k++)
 40-		addimprecipe(&sr->recipes, recipev[k]);
 41-}
 42-
 43-static void
 44-addimpdouble(struct SufRules *rules,
 45-             const char *from,
 46-             const char *to,
 47-             const char *const *recipev,
 48-             size_t nrecipe)
 49-{
 50-	size_t i, k;
 51-	struct SufRule *sr;
 52-
 53-	for (i = 0; i < rules->nsufs; i++) {
 54-		if (strcmp(rules->sufs[i].from, from) == 0 &&
 55-		    strcmp(rules->sufs[i].to, to) == 0) {
 56-			freerecipes(&rules->sufs[i].recipes);
 57-			memset(&rules->sufs[i].recipes, 0, sizeof(rules->sufs[i].recipes));
 58-			for (k = 0; k < nrecipe; k++)
 59-				addimprecipe(&rules->sufs[i].recipes, recipev[k]);
 60-			return;
 61-		}
 62-	}
 63-	rules->sufs = xrealloc(rules->sufs, (rules->nsufs + 1) * sizeof(rules->sufs[0]));
 64-	sr = &rules->sufs[rules->nsufs++];
 65-	memset(sr, 0, sizeof(*sr));
 66-	sr->from = xstrdup(from);
 67-	sr->to = xstrdup(to);
 68-	for (k = 0; k < nrecipe; k++)
 69-		addimprecipe(&sr->recipes, recipev[k]);
 70-}
 71-
 72 static int
 73 pathorargetexists(const struct Graph *graph, const char *name)
 74 {
 75@@ -212,6 +154,71 @@ expandauto(const char *s, const struct Target *t, const char *stem)
 76 	return out;
 77 }
 78 
 79+#define IMPRULES_COMMON \
 80+	".SUFFIXES: .o .c .f .y .l .sh\n" \
 81+	".c:\n" \
 82+	"\t$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<\n" \
 83+	".f:\n" \
 84+	"\t$(FC) $(FFLAGS) $(LDFLAGS) -o $@ $<\n" \
 85+	".sh:\n" \
 86+	"\tcp $< $@\n" \
 87+	"\tchmod a+x $@\n" \
 88+	".y.o:\n" \
 89+	"\t$(YACC) $(YFLAGS) $<\n" \
 90+	"\t$(CC) $(CPPFLAGS) $(CFLAGS) -c y.tab.c\n" \
 91+	"\trm -f y.tab.c\n" \
 92+	"\tmv y.tab.o $@\n" \
 93+	".l.o:\n" \
 94+	"\t$(LEX) $(LFLAGS) $<\n" \
 95+	"\t$(CC) $(CPPFLAGS) $(CFLAGS) -c lex.yy.c\n" \
 96+	"\trm -f lex.yy.c\n" \
 97+	"\tmv lex.yy.o $@\n" \
 98+	".y.c:\n" \
 99+	"\t$(YACC) $(YFLAGS) $<\n" \
100+	"\tmv y.tab.c $@\n" \
101+	".l.c:\n" \
102+	"\t$(LEX) $(LFLAGS) $<\n" \
103+	"\tmv lex.yy.c $@\n"
104+
105+#define IMPRULES_POSIX \
106+	IMPRULES_COMMON \
107+	".c.o:\n" \
108+	"\t$(CC) $(CFLAGS) -c $<\n" \
109+	".f.o:\n" \
110+	"\t$(FC) $(FFLAGS) -c $<\n"
111+
112+#define IMPRULES_GNU \
113+	IMPRULES_COMMON \
114+	".c.o:\n" \
115+	"\t$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<\n" \
116+	".f.o:\n" \
117+	"\t$(FC) $(FFLAGS) -c -o $@ $<\n"
118+
119+static int
120+loadimprules(struct SufRules *rules, const char *src)
121+{
122+	struct Ast ast;
123+	size_t i;
124+	int rc;
125+
126+	rc = parse("<built-in-rules>", src, &ast);
127+	if (rc < 0)
128+		return -1;
129+	for (i = 0; i < ast.n; i++) {
130+		struct Node *node = &ast.v[i];
131+
132+		if (node->kind != NODE_RULE)
133+			continue;
134+		if (issufrule(&node->data.rule)) {
135+			addsufs(&rules->active, &node->data.rule.prereqs);
136+			continue;
137+		}
138+		(void)collectsufrule(rules, &node->data.rule);
139+	}
140+	freeast(&ast);
141+	return 0;
142+}
143+
144 void
145 seedenv(struct Env *env, int posix, int envoverride)
146 {
147@@ -262,76 +269,10 @@ seedenv(struct Env *env, int posix, int envoverride)
148 void
149 imprules(struct SufRules *rules, int posix)
150 {
151-	static const char *const c_bin[] = {
152-	    "$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $<",
153-	};
154-	static const char *const f_bin[] = {
155-	    "$(FC) $(FFLAGS) $(LDFLAGS) -o $@ $<",
156-	};
157-	static const char *const sh_bin[] = {
158-	    "cp $< $@",
159-	    "chmod a+x $@",
160-	};
161-	static const char *const c_o[] = {
162-	    "$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<",
163-	};
164-	static const char *const c_o_posix[] = {
165-	    "$(CC) $(CPPFLAGS) $(CFLAGS) -c $<",
166-	};
167-	static const char *const f_o[] = {
168-	    "$(FC) $(FFLAGS) -c -o $@ $<",
169-	};
170-	static const char *const f_o_posix[] = {
171-	    "$(FC) $(FFLAGS) -c $<",
172-	};
173-	static const char *const y_o[] = {
174-	    "$(YACC) $(YFLAGS) $<",
175-	    "$(CC) $(CPPFLAGS) $(CFLAGS) -c y.tab.c",
176-	    "rm -f y.tab.c",
177-	    "mv y.tab.o $@",
178-	};
179-	static const char *const l_o[] = {
180-	    "$(LEX) $(LFLAGS) $<",
181-	    "$(CC) $(CPPFLAGS) $(CFLAGS) -c lex.yy.c",
182-	    "rm -f lex.yy.c",
183-	    "mv lex.yy.o $@",
184-	};
185-	static const char *const y_c[] = {
186-	    "$(YACC) $(YFLAGS) $<",
187-	    "mv y.tab.c $@",
188-	};
189-	static const char *const l_c[] = {
190-	    "$(LEX) $(LFLAGS) $<",
191-	    "mv lex.yy.c $@",
192-	};
193-	static const char *const suffixes[] = {
194-	    ".o",
195-	    ".c",
196-	    ".f",
197-	    ".y",
198-	    ".l",
199-	    ".sh",
200-	};
201-	struct StrList list;
202-
203-	memset(&list, 0, sizeof(list));
204-	list.v = (char **)suffixes;
205-	list.n = sizeof(suffixes) / sizeof(suffixes[0]);
206-	addsufs(&rules->active, &list);
207-
208-	addimpsingle(rules, ".c", c_bin, sizeof(c_bin) / sizeof(c_bin[0]));
209-	addimpsingle(rules, ".f", f_bin, sizeof(f_bin) / sizeof(f_bin[0]));
210-	addimpsingle(rules, ".sh", sh_bin, sizeof(sh_bin) / sizeof(sh_bin[0]));
211-	addimpdouble(rules, ".c", ".o",
212-	             posix ? c_o_posix : c_o,
213-	             posix ? sizeof(c_o_posix) / sizeof(c_o_posix[0]) : sizeof(c_o) / sizeof(c_o[0]));
214-	addimpdouble(rules, ".f", ".o",
215-	             posix ? f_o_posix : f_o,
216-	             posix ? sizeof(f_o_posix) / sizeof(f_o_posix[0]) : sizeof(f_o) / sizeof(f_o[0]));
217-	addimpdouble(rules, ".y", ".o", y_o, sizeof(y_o) / sizeof(y_o[0]));
218-	addimpdouble(rules, ".l", ".o", l_o, sizeof(l_o) / sizeof(l_o[0]));
219-	addimpdouble(rules, ".y", ".c", y_c, sizeof(y_c) / sizeof(y_c[0]));
220-	addimpdouble(rules, ".l", ".c", l_c, sizeof(l_c) / sizeof(l_c[0]));
221+	if (loadimprules(rules, posix ? IMPRULES_POSIX : IMPRULES_GNU) < 0) {
222+		fprintf(stderr, "failed to load built-in implicit rules\n");
223+		exit(1);
224+	}
225 }
226 
227 int