1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
#include "mk.h"
static Node *applyrules(char *, char *);
static void togo(Node *);
static int vacuous(Node *);
static Node *newnode(char *);
static void trace(char *, Arc *);
static void cyclechk(Node *);
static void ambiguous(Node *);
static void attribute(Node *);
Node *
graph(char *target)
{
Node *node;
char *cnt;
cnt = rulecnt();
node = applyrules(target, cnt);
free(cnt);
cyclechk(node);
node->flags |= PROBABLE; /* make sure it doesn't get deleted */
vacuous(node);
ambiguous(node);
attribute(node);
return(node);
}
static Node *
applyrules(char *target, char *cnt)
{
Symtab *sym;
Node *node;
Rule *r;
Arc head, *a = &head;
Word *w;
char stem[NAMEBLOCK], buf[NAMEBLOCK];
Resub rmatch[NREGEXP];
/* print("applyrules(%lux='%s')\n", target, target); */
sym = symlook(target, S_NODE, 0);
if(sym)
return sym->u.ptr;
target = strdup(target);
node = newnode(target);
head.n = 0;
head.next = 0;
sym = symlook(target, S_TARGET, 0);
memset((char*)rmatch, 0, sizeof(rmatch));
for(r = sym? sym->u.ptr:0; r; r = r->chain){
if(r->attr&META) continue;
if(strcmp(target, r->target)) continue;
if((!r->recipe || !*r->recipe) && (!r->tail || !r->tail->s || !*r->tail->s)) continue; /* no effect; ignore */
if(cnt[r->rule] >= nreps) continue;
cnt[r->rule]++;
node->flags |= PROBABLE;
/* if(r->attr&VIR)
* node->flags |= VIRTUAL;
* if(r->attr&NOREC)
* node->flags |= NORECIPE;
* if(r->attr&DEL)
* node->flags |= DELETE;
*/
if(!r->tail || !r->tail->s || !*r->tail->s) {
a->next = newarc((Node *)0, r, "", rmatch);
a = a->next;
} else
for(w = r->tail; w; w = w->next){
a->next = newarc(applyrules(w->s, cnt), r, "", rmatch);
a = a->next;
}
cnt[r->rule]--;
head.n = node;
}
for(r = metarules; r; r = r->next){
if((!r->recipe || !*r->recipe) && (!r->tail || !r->tail->s || !*r->tail->s)) continue; /* no effect; ignore */
if ((r->attr&NOVIRT) && a != &head && (a->r->attr&VIR))
continue;
if(r->attr®EXP){
stem[0] = 0;
patrule = r;
memset((char*)rmatch, 0, sizeof(rmatch));
if(regexec(r->pat, node->name, rmatch, NREGEXP) == 0)
continue;
} else {
if(!match(node->name, r->target, stem, r->shellt)) continue;
}
if(cnt[r->rule] >= nreps) continue;
cnt[r->rule]++;
/* if(r->attr&VIR)
* node->flags |= VIRTUAL;
* if(r->attr&NOREC)
* node->flags |= NORECIPE;
* if(r->attr&DEL)
* node->flags |= DELETE;
*/
if(!r->tail || !r->tail->s || !*r->tail->s) {
a->next = newarc((Node *)0, r, stem, rmatch);
a = a->next;
} else
for(w = r->tail; w; w = w->next){
if(r->attr®EXP)
regsub(w->s, buf, sizeof buf, rmatch, NREGEXP);
else
subst(stem, w->s, buf);
a->next = newarc(applyrules(buf, cnt), r, stem, rmatch);
a = a->next;
}
cnt[r->rule]--;
}
a->next = node->prereqs;
node->prereqs = head.next;
return(node);
}
static void
togo(Node *node)
{
Arc *la, *a;
/* delete them now */
la = 0;
for(a = node->prereqs; a; la = a, a = a->next)
if(a->flag&TOGO){
if(a == node->prereqs)
node->prereqs = a->next;
else
la->next = a->next, a = la;
}
}
static int
vacuous(Node *node)
{
Arc *la, *a;
int vac = !(node->flags&PROBABLE);
if(node->flags&READY)
return(node->flags&VACUOUS);
node->flags |= READY;
for(a = node->prereqs; a; a = a->next)
if(a->n && vacuous(a->n) && (a->r->attr&META))
a->flag |= TOGO;
else
vac = 0;
/* if a rule generated arcs that DON'T go; no others from that rule go */
for(a = node->prereqs; a; a = a->next)
if((a->flag&TOGO) == 0)
for(la = node->prereqs; la; la = la->next)
if((la->flag&TOGO) && (la->r == a->r)){
la->flag &= ~TOGO;
}
togo(node);
if(vac)
node->flags |= VACUOUS;
return(vac);
}
static Node *
newnode(char *name)
{
register Node *node;
node = (Node *)Malloc(sizeof(Node));
symlook(name, S_NODE, (void *)node);
node->name = name;
node->time = timeof(name, 0);
node->prereqs = 0;
node->flags = node->time? PROBABLE : 0;
node->next = 0;
return(node);
}
void
dumpn(char *s, Node *n)
{
char buf[1024];
Arc *a;
snprint(buf, sizeof buf, "%s ", (*s == ' ')? s:"");
Bprint(&bout, "%s%s@%ld: time=%ld flags=0x%x next=%ld\n",
s, n->name, n, n->time, n->flags, n->next);
for(a = n->prereqs; a; a = a->next)
dumpa(buf, a);
}
static void
trace(char *s, Arc *a)
{
fprint(2, "\t%s", s);
while(a){
fprint(2, " <-(%s:%d)- %s", a->r->file, a->r->line,
a->n? a->n->name:"");
if(a->n){
for(a = a->n->prereqs; a; a = a->next)
if(*a->r->recipe) break;
} else
a = 0;
}
fprint(2, "\n");
}
static void
cyclechk(Node *n)
{
Arc *a;
if((n->flags&CYCLE) && n->prereqs){
fprint(2, "mk: cycle in graph detected at target %s\n", n->name);
Exit();
}
n->flags |= CYCLE;
for(a = n->prereqs; a; a = a->next)
if(a->n)
cyclechk(a->n);
n->flags &= ~CYCLE;
}
static void
ambiguous(Node *n)
{
Arc *a;
Rule *r = 0;
Arc *la;
int bad = 0;
la = 0;
for(a = n->prereqs; a; a = a->next){
if(a->n)
ambiguous(a->n);
if(*a->r->recipe == 0) continue;
if(r == 0)
r = a->r, la = a;
else{
if(r->recipe != a->r->recipe){
if((r->attr&META) && !(a->r->attr&META)){
la->flag |= TOGO;
r = a->r, la = a;
} else if(!(r->attr&META) && (a->r->attr&META)){
a->flag |= TOGO;
continue;
}
}
if(r->recipe != a->r->recipe){
if(bad == 0){
fprint(2, "mk: ambiguous recipes for %s:\n", n->name);
bad = 1;
trace(n->name, la);
}
trace(n->name, a);
}
}
}
if(bad)
Exit();
togo(n);
}
static void
attribute(Node *n)
{
register Arc *a;
for(a = n->prereqs; a; a = a->next){
if(a->r->attr&VIR)
n->flags |= VIRTUAL;
if(a->r->attr&NOREC)
n->flags |= NORECIPE;
if(a->r->attr&DEL)
n->flags |= DELETE;
if(a->n)
attribute(a->n);
}
if(n->flags&VIRTUAL)
n->time = 0;
}
|