summaryrefslogtreecommitdiff
path: root/appl/cmd/mash/make.b
blob: 4d566c006373cc5d10ed0360f08bfa32b8e4198d (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
implement Mashbuiltin;

#
#	"make" builtin, defines:
#
#	depends	- print dependencies
#	make		- make-like command
#	match	- print details of rule matches
#	rules		- print rules
#

include	"mash.m";
include	"mashparse.m";

verbose:	con 0;	# debug output

mashlib:	Mashlib;

Cmd, Env, Item, Stab:	import mashlib;
Depend, Rule, Target:	import mashlib;
sys, bufio, hash:		import mashlib;

Iobuf:	import bufio;

#
#	Interface to catch the use as a command.
#
init(nil: ref Draw->Context, args: list of string)
{
	raise "fail: " + hd args + " not loaded";
}

#
#	Used by whatis.
#
name(): string
{
	return "make";
}

#
#	Install commands.
#
mashinit(nil: list of string, lib: Mashlib, this: Mashbuiltin, e: ref Env)
{
	mashlib = lib;
	e.defbuiltin("depends", this);
	e.defbuiltin("make", this);
	e.defbuiltin("match", this);
	e.defbuiltin("rules", this);
}

#
#	Execute a builtin.
#
mashcmd(e: ref Env, l: list of string)
{
	s := hd l;
	l = tl l;
	case s {
	"depends" =>
		out := e.outfile();
		if (out == nil)
			return;
		if (l == nil)
			alldeps(out);
		else
			depends(out, l);
		out.close();
	"make" =>
		domake(e, l);
	"match" =>
		domatch(e, l);
	"rules" =>
		out := e.outfile();
		if (out == nil)
			return;
		if (l == nil)
			allrules(out);
		else
			rules(out, l);
		out.close();
	}
}

#
#	Node states.
#
SUnknown, SNoexist, SExist, SStale, SMade, SDir, SDirload
	: con iota;

#
#	Node flags.
#
#	FMark	- marked as in progress
#
FMark
	: con 1 << iota;

Node: adt
{
	name:	string;
	state:		int;
	flags:		int;
	mtime:	int;
};

#
#	Step in implicit chain.
#
Step:	type (ref Rule, array of string, ref Node);

#
#	Implicit match.
#
Match: adt
{
	node:	ref Node;
	path:		list of Step;
};

NSIZE:	con 127;	# node hash size
DSIZE:	con 32;	# number of dir entries for read

ntab:		array of list of ref Node;	# node hash table

initnodes()
{
	ntab = array[NSIZE] of list of ref Node;
}

#
#	Find node for a pathname.
#
getnode(s: string): ref Node
{
	h := hash->fun1(s, NSIZE);
	for (l := ntab[h]; l != nil; l = tl l) {
		n := hd l;
		if (n.name == s)
			return n;
	}
	r := ref Node(s, SUnknown, 0, 0);
	ntab[h] = r :: ntab[h];
	return r;
}

#
#	Make a pathname from a dir and an entry.
#
mkpath(d, s: string): string
{
	if (d == ".")
		return s;
	else if (d == "/")
		return "/" + s;
	else
		return d + "/" + s;
}

#
#	Load a directory.
#
loaddir(s: string)
{
	if (verbose)
		sys->print("loaddir %s\n", s);
	fd := sys->open(s, Sys->OREAD);
	if (fd == nil)
		return;
	for (;;) {
		(c, dbuf) := sys->dirread(fd);
		if(c <= 0)
			break;
		for (i := 0; i < c; i++) {
			n := getnode(mkpath(s, dbuf[i].name));
			if (dbuf[i].mode & Sys->DMDIR)
				n.state = SDir;
			else
				n.state = SExist;
			n.mtime = dbuf[i].mtime;
		}
	}
}

#
#	Load a file.  Get its node, maybe stat it or loaddir.
#
loadfile(s: string): ref Node
{
	n := getnode(s);
	if (n.state == SUnknown) {
		if (verbose)
			sys->print("stat %s\n", s);
		(ok, d) := sys->stat(s);
		if (ok >= 0) {
			n.mtime = d.mtime;
			if (d.mode & Sys->DMDIR) {
				loaddir(s);
				n.state = SDirload;
			} else
				n.state = SExist;
		} else
			n.state = SNoexist;
	} else if (n.state == SDir) {
		loaddir(s);
		n.state = SDirload;
	}
	return n;
}

#
#	Get the node for a file and load the directories in its path.
#
getfile(s: string): ref Node
{
	d: string;
	n := len s;
	while (n >= 2 && s[0:2] == "./") {
		n -= 2;
		s = s[2:];
	}
	if (n > 0 && s[0] == '/') {
		d = "/";
		s = s[1:];
	} else
		d = ".";
	(nil, l) := sys->tokenize(s, "/");
	for (;;) {
		w := loadfile(d);
		if (l == nil)
			return w;
		s = hd l;
		l = tl l;
		d = mkpath(d, s);
	}
}

#
#	If a dependency rule makes more than one target propogate SMade.
#
propagate(l: list of string)
{
	if (tl l == nil)
		return ;
	while (l != nil) {
		s := hd l;
		if (verbose)
			sys->print("propogate to %s\n", s);
		getfile(s).state = SMade;
		l = tl l;
	}
}

#
#	Try to make a node, or mark it as stale.
#	Return -1 on (reported) error, 0 on fail, 1 on success.
#
explicit(e: ref Env, t: ref Target, n: ref Node): int
{
	d: ref Depend;
	for (l := t.depends; l != nil ; l = tl l) {
		if ((hd l).op != Cnop) {
			if (d != nil) {
				e.report(sys->sprint("make: too many rules for %s", t.target));
				return -1;
			}
			d = hd l;
		}
	}
	for (l = t.depends; l != nil ; l = tl l) {
		for (u := (hd l).depends; u != nil; u = tl u) {
			s := hd u;
			m := getfile(s);
			x := make(e, m, s);
			if (x < 0) {
				sys->print("don't know how to make %s\n", s);
				return x;
			}
			if (m.state == SMade || m.mtime > n.mtime) {
				if (verbose)
					sys->print("%s makes %s stale\n", s, t.target);
				n.state = SStale;
			}
		}
	}
	if (d != nil) {
		if (n.state == SNoexist || n.state == SStale) {
			if (verbose)
				sys->print("build %s with explicit rule\n", t.target);
			e = e.copy();
			e.flags |= mashlib->EEcho | Mashlib->ERaise;
			e.flags &= ~mashlib->EInter;
			d.cmd.xeq(e);
			propagate(d.targets);
			n.state = SMade;
		} else if (verbose)
			sys->print("%s up to date\n", t.target);
		return 1;
	}
	return 0;
}

#
#	Report multiple implicit chains of equal length.
#
multimatch(e: ref Env, n: ref Node, l: list of Match)
{
	e.report(sys->sprint("%d rules match for %s", len l, n.name));
	f := e.stderr;
	while (l != nil) {
		m := hd l;
		sys->fprint(f, "%s", m.node.name);
		for (p := m.path; p != nil; p = tl p) {
			(nil, nil, t) := hd p;
			sys->fprint(f, " -> %s", t.name);
		}
		sys->fprint(f, "\n");
		l = tl l;
	}
}

cycle(e: ref Env, n: ref Node)
{
	e.report(sys->sprint("make: cycle in dependencies for target %s", n.name));
}

#
#	Mark the nodes in an implicit chain.
#
markchain(e: ref Env, l: list of Step): int
{
	while (tl l != nil) {
		(nil, nil, n) := hd l;
		if (n.flags & FMark) {
			cycle(e, n);
			return 0;
		}
		n.flags |= FMark;
		l = tl l;
	}
	return 1;
}

#
#	Unmark the nodes in an implicit chain.
#
unmarkchain(l: list of Step): int
{
	while (tl l != nil) {
		(nil, nil, n) := hd l;
		n.flags &= ~FMark;
		l = tl l;
	}
	return 1;
}

#
#	Execute an implicit rule chain.
#
xeqmatch(e: ref Env, b, n: ref Node, l: list of Step): int
{
	if (!markchain(e, l))
		return -1;
	if (verbose)
		sys->print("making %s for implicit rule chain\n", n.name);
	e.args = nil;
	x := make(e, n, n.name);
	if (x < 0) {
		sys->print("don't know how to make %s\n", n.name);
		return x;
	}
	if (n.state == SMade || n.mtime > b.mtime || b.state == SStale) {
		e = e.copy();
		e.flags |= mashlib->EEcho | Mashlib->ERaise;
		e.flags &= ~mashlib->EInter;
		for (;;) {
			(r, a, t) := hd l;
			if (verbose)
				sys->print("making %s with implicit rule\n", t.name);
			e.args = a;
			r.cmd.xeq(e);
			t.state = SMade;
			l = tl l;
			if (l == nil)
				break;
			t.flags &= ~FMark;
		}
	} else
		unmarkchain(l);
	return 1;
}

#
#	Find the shortest implicit rule chain.
#
implicit(e: ref Env, base: ref Node): int
{
	win, lose: list of Match;
	l: list of ref Rule;
	cand := Match(base, nil) :: nil;
	do {
		# cand - list of candidate chains
		# lose - list of extended chains that lose
		# win	 - list of extended chains that win
		lose = nil;
	match:
		# for each candidate
		for (c := cand; c != nil; c = tl c) {
			(b, x) := hd c;
			s := b.name;
			# find rules that match end of chain
			m := mashlib->rulematch(s);
			l = nil;
			# exclude rules already in the chain
		exclude:
			for (n := m; n != nil; n = tl n) {
				r := hd n;
				for (y := x; y != nil; y = tl y) {
					(u, nil, nil) := hd y;
					if (u == r)
						continue exclude;
				}
				l = r :: l;
			}
			if (l == nil)
				continue match;
			(nil, t) := sys->tokenize(s, "/");
			# for each new rule that matched
			for (n = l; n != nil; n = tl n) {
				r := hd n;
				a := r.matches(t);
				if (a == nil) {
					e.report("rule match cock up");
					return -1;
				}
				a[0] = s;
				e.args = a;
				# eval rhs
				(v, nil, nil) := r.rhs.ieval2(e);
				if (v == nil)
					continue;
				y := (r, a, b) :: x;
				z := getfile(v);
				# winner or loser
				if (z.state != SNoexist || Target.find(v) != nil)
					win = (z, y) :: win;
				else
					lose = (z, y) :: lose;
			}
		}
		# winner should be unique
		if (win != nil) {
			if (tl win != nil) {
				multimatch(e, base, win);
				return -1;
			} else {
				(a, p) := hd win;
				return xeqmatch(e, base, a, p);
			}
		}
		# losers are candidates in next round
		cand = lose;
	} while (cand != nil);
	return 0;
}

#
#	Make a node (recursive).
#	Return -1 on (reported) error, 0 on fail, 1 on success.
#
make(e: ref Env, n: ref Node, s: string): int
{
	if (n == nil)
		n = getfile(s);
	if (verbose)
		sys->print("making %s\n", n.name);
	if (n.state == SMade)
		return 1;
	if (n.flags & FMark) {
		cycle(e, n);
		return -1;
	}
	n.flags |= FMark;
	t := Target.find(s);
	if (t != nil) {
		x := explicit(e, t, n);
		if (x != 0) {
			n.flags &= ~FMark;
			return x;
		}
	}
	x := implicit(e, n);
	n.flags &= ~FMark;
	if (x != 0)
		return x;
	if (n.state == SExist)
		return 0;
	return -1;
}

makelevel:	int = 0;	# count recursion

#
#	Make driver routine.  Maybe initialize and handle exceptions.
#
domake(e: ref Env, l: list of string)
{
	if ((e.flags & mashlib->ETop) == 0) {
		e.report("make not at top level");
		return;
	}
	inited := 0;
	if (makelevel > 0)
		inited = 1;
	makelevel++;
	if (l == nil)
		l = "default" :: nil;
	while (l != nil) {
		s := hd l;
		l = tl l;
		if (s[0] == '-') {
			case s {
			"-clear" =>
				mashlib->initdep();
			* =>
				e.report("make: unknown option: " + s);
			}
		} else {
			if (!inited) {
				initnodes();
				inited = 1;
			}
			{
				if (make(e, nil, s) < 0) {
					sys->print("don't know how to make %s\n", s);
					raise "fail: make error";
				}
			}exception x{
			mashlib->FAILPAT =>
				makelevel--;
				raise x;
			}
		}
	}
	makelevel--;
}

#
#	Print dependency/rule command.
#
prcmd(out: ref Iobuf, op: int, c: ref Cmd)
{
	if (op == Clistgroup)
		out.putc(':');
	if (c != nil) {
		out.puts("{ ");
		out.puts(c.text());
		out.puts(" }");
	} else
		out.puts("{}");
}

#
#	Print details of rule matches.
#
domatch(e: ref Env, l: list of string)
{
	out := e.outfile();
	if (out == nil)
		return;
	e = e.copy();
	while (l != nil) {
		s := hd l;
		out.puts(sys->sprint("%s:\n", s));
		m := mashlib->rulematch(s);
		(nil, t) := sys->tokenize(s, "/");
		while (m != nil) {
			r := hd m;
			out.puts(sys->sprint("\tlhs %s\n", r.lhs.text));
			a := r.matches(t);
			if (a != nil) {
				a[0] = s;
				n := len a;
				for (i := 0; i < n; i++)
					out.puts(sys->sprint("\t$%d '%s'\n", i, a[i]));
				e.args = a;
				(v, w, nil) := r.rhs.ieval2(e);
				if (v != nil)
					out.puts(sys->sprint("\trhs '%s'\n", v));
				else
					out.puts(sys->sprint("\trhs list %d\n", len w));
				if (r.cmd != nil) {
					out.putc('\t');
					prcmd(out, r.op, r.cmd);
					out.puts(";\n");
				}
			} else
				out.puts("\tcock up\n");
			m = tl m;
		}
		l = tl l;
	}
	out.close();
}

#
#	Print word list.
#
prwords(out: ref Iobuf, l: list of string, pre: int)
{
	while (l != nil) {
		if (pre)
			out.putc(' ');
		out.puts(mashlib->quote(hd l));
		if (!pre)
			out.putc(' ');
		l = tl l;
	}
}

#
#	Print dependency.
#
prdep(out: ref Iobuf, d: ref Depend)
{
	prwords(out, d.targets, 0);
	out.putc(':');
	prwords(out, d.depends, 1);
	if (d.op != Cnop) {
		out.putc(' ');
		prcmd(out, d.op, d.cmd);
	}
	out.puts(";\n");
}

#
#	Print all dependencies, avoiding duplicates.
#
alldep(out: ref Iobuf, d: ref Depend, pass: int)
{
	case pass {
	0 =>
		d.mark = 0;
	1 =>
		if (!d.mark) {
			prdep(out, d);
			d.mark = 1;
		}
	}
}

#
#	Print all dependencies.
#
alldeps(out: ref Iobuf)
{
	a := mashlib->dephash;
	n := len a;
	for (p := 0; p < 2; p++)
		for (i := 0; i < n; i++)
			for (l := a[i]; l != nil; l = tl l)
				for (d := (hd l).depends; d != nil; d = tl d)
					alldep(out, hd d, p);
}

#
#	Print dependencies.
#
depends(out: ref Iobuf, l: list of string)
{
	while (l != nil) {
		s := hd l;
		out.puts(s);
		out.puts(":\n");
		t := Target.find(s);
		if (t != nil) {
			for (d := t.depends; d != nil; d = tl d)
				prdep(out, hd d);
		}
		l = tl l;
	}
}

#
#	Print rule.
#
prrule(out: ref Iobuf, r: ref Rule)
{
	out.puts(r.lhs.text);
	out.puts(" :~ ");
	out.puts(r.rhs.text());
	out.putc(' ');
	prcmd(out, r.op, r.cmd);
	out.puts(";\n");
}

#
#	Print all rules.
#
allrules(out: ref Iobuf)
{
	for (l := mashlib->rules; l != nil; l = tl l)
		prrule(out, hd l);
}

#
#	Print matching rules.
#
rules(out: ref Iobuf, l: list of string)
{
	while (l != nil) {
		s := hd l;
		out.puts(s);
		out.puts(":\n");
		r := mashlib->rulematch(s);
		while (r != nil) {
			prrule(out, hd r);
			r = tl r;
		}
		l = tl l;
	}
}