-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtarget.lisp
1979 lines (1702 loc) · 63.1 KB
/
target.lisp
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
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(defpackage :overlord/target
(:use
:cl
:alexandria
:serapeum
:local-time
:uiop/filesystem
:uiop/pathname
;; The build logic.
:overlord/redo
;; What we need to implement.
:overlord/target-protocol
;; Target tables.
:overlord/target-table
;; Timestamps.
:overlord/stamp
;; Digests.
:overlord/digest
;; Resettable global state.
:overlord/global-state
;; Types common to the project.
:overlord/types
;; Special variables.
:overlord/specials
;; Utilities.
:overlord/util
;; The disk cache.
:overlord/cache
;; ASDF interface.
:overlord/asdf
;; How to infer the base for the current package.
:overlord/base
;; Logging.
:overlord/message
;; Running shell commands.
:cmd
;; The database.
:overlord/db
;; Freezing the state of the Lisp image.
:overlord/freeze
;; Oracles.
:overlord/oracle)
(:import-from :overlord/kernel
:with-meta-kernel)
(:import-from :named-readtables :in-readtable)
(:import-from :fset)
(:import-from :trivia
:match :ematch :let-match1 :multiple-value-ematch
:multiple-value-match)
(:import-from :trivial-file-size
:file-size-in-octets)
;; Portability shim for "global" or "static" vars. They have global
;; scope, but cannot be rebound.
(:import-from :global-vars
:define-global-var
:define-global-parameter)
(:import-from :cl-murmurhash
:murmurhash)
(:import-from :uiop
:implementation-identifier
:with-temporary-file
:rename-file-overwriting-target
:parse-unix-namestring)
(:import-from :overlord/kernel
:nproc)
(:import-from :overlord/build-env
:build-env-bound?
:claim-file* :claim-files*
:temp-prereqs
:temp-prereqsne)
;; Shadow for style.
(:shadow
:if ;Always ternary.
:if-let ;Ditto.
:cond ;Require exhaustive.
:set ;Use symbol-value.
:defclass ;Force checking slot types.
:typecase ;Use typecase-of instead.
:etypecase ;Use etypecase-of instead.
:ctypecase ;Use ctypecase-of instead.
:case :ecase :ccase
:file-write-date ;Use file-mtime instead.
:pathname ;Use ensure-pathname.
:multiple-value-bind ;Use receive.
:defmethod ;Require a generic function
)
(:export
;; Defining and building targets.
:deftask
:define-target-task
:defconfig
:define-target-config
:var-target
:define-target-var
:define-var-once
:file-target
:ensure-absolute
:extension
:change-pathname-type
:defpattern
:find-pattern
:build
:build-package
:run
:depends-on
:depends-on*
:depends-on-all
:depends-on-all*
:depends-not
:depends-not-all
:use :use-all
:ensure-target-recorded
:task
:pattern
:merge-input-defaults
:merge-output-defaults
:pattern.input-defaults
:pattern.output-defaults
:pattern-name
:pattern-build
:pattern-ref
:pattern-from
:pattern-into
:make-pattern
:define-script
:pattern-ref-static-inputs
:pattern-ref-outputs
:clear-package-prereqs
:list-package-prereqs
:directory-exists
:path
:file
:ensure-file-target-pathname
:canonicalize-pathname-for-readability))
(in-package :overlord/target)
(in-readtable :standard)
;;; Shadows and preferred alternatives.
(deftype pathname ()
'cl:pathname)
;;; This would be nice, but it confuses CCL: it thinks our `pathname'
;;; is a built-in type and won't compile the above `deftype' form.
;; (setf (find-class 'pathname)
;; (find-class 'cl:pathname))
;;; Conditionals should always be exhaustive.
(defmacro if (test then else)
"Like `cl:if', but require two branches."
`(cl:if ,test ,then ,else))
(defmacro if-let (bindings then else)
"Like `alexandria:if-let', but require two branches."
`(alexandria:if-let ,bindings ,then ,else))
(defmacro cond (&whole whole &body clauses)
"Like `cl:cond', but require a default clause."
(unless (and clauses (eql (car (lastcar clauses)) 't))
(warn "Non-exhaustive COND: ~s" whole))
`(cl:cond ,@clauses))
(defmacro defclass (name supers &body (slots . options))
"Like `cl:defclass', but try to force slot types to be checked.
Works for SBCL, at least."
`(locally (declare (optimize (safety 3)))
(cl:defclass ,name ,supers
,slots
,@options)))
(defun check-generic-function (name)
"Check that NAME is bound to a generic function."
(unless (and (fboundp name)
(typep (fdefinition name)
'generic-function))
(error "No generic function for ~a" name)))
(defmacro defmethod (name &body body)
"Like `cl:defmethod', but raise an error is NAME is not already
bound as a generic function."
`(progn
(check-generic-function ',name)
(cl:defmethod ,name ,@body)))
;;; Auxiliary functions for Redo.
;;; Define properties for targets in the database.
(defconst nonexist :nonexist)
(defconst prereqs :prereqs)
(defconst prereqsne :prereqsne)
(defconst stamp :stamp)
(defconst build-time :build-time)
(defun saved-prereq (x &optional (stamp (target-stamp x)))
"Make a saved prereq object."
(cons x (assure stamp stamp)))
(defmethod saved-prereq-target (p) (car p))
(defmethod saved-prereq-stamp (p) (cdr p))
(defun current-parent ()
"The current parent. If we are building, it is the target being
built; otherwise it is the current package."
(or (first *parents*)
*package*))
(defmethod record-prereq (target &aux (parent (current-parent)))
(record-parent-prereq parent target))
(defmethod record-prereq ((target null))
(error 'not-a-target :designator target))
(defmethod record-prereq ((target symbol))
(let ((target (maybe-delay-symbol target)))
(if (symbolp target)
(call-next-method)
(record-prereq target))))
(defgeneric record-parent-prereq (parent target)
(:documentation "Record TARGET as a prerequisite of PARENT.")
(:method ((parent package) target)
(declare (ignore target)))
(:method (parent target)
(withf (temp-prereqs parent)
target
(target-stamp target))))
(defmethod record-prereqne (target &aux (parent (current-parent)))
(record-parent-prereqne parent target))
(defmethod record-prereqne ((target symbol))
(let ((target (maybe-delay-symbol target)))
(if (symbolp target)
(call-next-method)
(record-prereqne target))))
(defun record-parent-prereqne (parent target)
(withf (temp-prereqsne parent) target))
(defmethod target-in-db? (target)
(and (not *force*)
(has-prop? target
prereqs
prereqsne)))
(defmethod target-in-db? ((target package)) t)
(defmethod clear-temp-prereqs (target)
(setf (temp-prereqs target)
(fset:empty-map)))
(defmethod clear-temp-prereqsne (target)
(setf (temp-prereqsne target)
(fset:empty-set)))
(defmethod save-temp-prereqs (target)
(let ((map (temp-prereqs target)))
(setf (prop target prereqs) map)
(clear-temp-prereqs target)))
(defmethod save-temp-prereqsne (target)
(let ((set (temp-prereqsne target)))
(setf (prop target prereqsne) set)
(clear-temp-prereqsne target)))
(defmethod target-saved-prereqs (target)
(let ((map (prop target prereqs)))
(and (typep map 'fset:map)
(collecting
(fset:do-map (k v map)
(collect (saved-prereq k v)))))))
(defun (setf target-saved-prereqs) (value target)
(setf (prop target prereqs)
(assure fset:map value)))
(defmethod target-saved-prereqsne (target)
(let ((set (prop target prereqsne)))
(and (typep set 'fset:set)
(fset:convert 'list set))))
(defun (setf target-saved-prereqsne) (value target)
(setf (prop target prereqsne)
(assure fset:set value)))
(defmethod target-build-time (target)
(prop target build-time 0))
(defmethod (setf target-build-time) (value target)
(check-type value (integer 0 *))
(setf (prop target build-time) value))
(-> build-time-from-file (t pathname) (integer 0 *))
(defun build-time-from-file (target file)
"Get the build time for TARGET from the database, but if there is no
recorded build time, fall back to using the size of FILE.
This heuristic ensures that, in the absence of other information,
larger files will be built before smaller files."
(declare (file-pathname file))
(build-time-from-files target (list file)))
(defun build-time-from-files (target files)
"Like `build-time-from-file', but the size is a sum over FILES."
(let* ((unknown :unknown)
(build-time (prop target build-time unknown)))
(if (eql build-time unknown)
(reduce #'+ files :key (op (or (file-size-in-octets _) 0)))
build-time)))
;;; Simple types.
(defmethods root-target (target)
(:method target-exists? (target)
t)
(:method record-parent-prereq (target child)
(declare (ignore child)))
(:method target-in-db? (target)
t)
(:method target-timestamp (target)
never)
(:method hash-target (target)
(load-time-value (sxhash root-target)))
(:method hash-friendly? (target)
t)
(:method call-with-target-locked (target fn)
(funcall fn))
(:method target-saved-prereqs (target)
(mapcar (op (saved-prereq _1 (target-stamp _1)))
(list-all-packages)))
(:method target-build-script (target)
(trivial-task target))
(:method target-static-prereqs (target)
(list-all-packages))
(:method target-node-label (target)
(progn "everything")))
(defmethods trivial-prereq (target)
(:method record-prereq (target))
(:method target-in-db? (target)
t)
(:method target-exists? (target)
t)
(:method target-timestamp (target)
far-future)
(:method hash-target (target)
(load-time-value (sxhash trivial-prereq)))
(:method hash-friendly? (target)
t)
(:method call-with-target-locked (target fn)
(funcall fn))
(:method target-build-time (target)
0)
(:method (setf target-build-time) (value target)
(declare (ignore value))
(values))
(:method target-build-script (target)
(trivial-task target))
;; Shouldn't happen
(:method target-node-label (target)
(progn "TRIVIAL TARGET")))
(defmethods impossible-prereq (target)
(:method record-prereqne (target)
(declare (ignore target)))
(:method target-in-db? (target) t)
(:method target-exists? (target)
nil)
(:method target-timestamp (target)
never)
(:method hash-target (target)
(load-time-value (sxhash impossible-prereq)))
(:method hash-friendly? (target)
t)
(:method call-with-target-locked (target fn)
(funcall fn))
(:method target-build-time (target)
0)
(:method (setf target-build-time) (value target)
(declare (ignore value)))
(:method target-build-script (target)
(trivial-task target))
;;; Shouldn't happen either.
(:method target-node-label (target)
(progn "IMPOSSIBLE TARGET")))
;;; Types.
(defcondition not-a-target (overlord-error)
((designator :initarg :designator))
(:report (lambda (c s)
(with-slots (designator) c
(format s "Not a target: ~a" designator)))))
(defgeneric load-form-slot-names (self)
(:method-combination append))
(defclass externalizable ()
()
(:documentation "A class that can be externalized. Subclasses
inherit a method on `make-load-form', and need only specialize
`load-form-slot-names' (using the `append' method combination)."))
(defmethod make-load-form ((self externalizable) &optional env)
(make-load-form-saving-slots self
:slot-names (load-form-slot-names self)
:environment env))
(defmethod load-form-slot-names append ((self externalizable))
nil)
(defclass ref (externalizable)
((name
:reader ref.name
:type t
:initarg :name))
(:documentation "Base class for different kinds of by-name references."))
(defmethods ref (self name)
(:method initialize-instance :after (self &key &allow-other-keys)
(unless (slot-boundp self 'name)
(error* "No name")))
(:method load-form-slot-names append (self)
'(name))
(:method print-object (self stream)
(if (not *print-escape*)
(print-unreadable-object (self stream :type t)
(format stream "~a" name))
(call-next-method)))
(:method fset:compare (self (other ref))
(fset:compare-slots self other #'class-name-of #'ref.name))
(:method hash-target (self)
(dx-sxhash
(list (class-name-of self)
(ref.name self)))))
(fset:define-cross-type-compare-methods ref)
(defclass directory-exists (ref)
((name
:initarg :path
:reader directory-exists.path
:type (and absolute-pathname directory-pathname)))
(:documentation "A reference to a directory."))
(defun directory-exists (name)
"Wrap NAME as a directory reference."
(etypecase-of (or string directory-pathname relative-pathname) name
(relative-pathname (directory-exists (ensure-absolute name)))
(string (directory-exists (ensure-pathname name :want-pathname t)))
(directory-pathname (make 'directory-exists :path name))))
(defmethods directory-exists (target (path name))
(:method target-exists? (target)
(~> path
(resolve-target)
directory-exists-p))
(:method target-timestamp (target)
(let* ((dir path)
(dir (resolve-target dir)))
(if (directory-exists-p dir)
far-future
never)))
(:method (setf target-timestamp) (timestamp target)
(declare (ignore timestamp))
(if (directory-exists-p path)
;; TODO Ditto.
(error* "Cannot set directory timestamps (yet).")
(ensure-directories-exist path)))
(:method resolve-target (target &optional base)
(if (absolute-pathname-p path)
target
(directory-exists
(assure tame-pathname
(resolve-file target :base (or base (base)))))))
(:method target= (target (y directory-exists))
(pathname-equal path (directory-exists.path y)))
(:method target-build-script (target)
(let ((dir path))
#+sbcl (declare (notinline task))
(task target
(lambda ()
(let ((dir (resolve-target dir)))
(ensure-directories-exist dir)))
trivial-prereq
nil)))
(:method target-node-label (target)
(fmt "directory ~a" path))
(:method hash-target (target)
(dx-sxhash (list 'directory-exists path)))
(:method print-object (target stream)
(if (not *print-escape*)
(call-next-method)
(format stream "~a~s"
(read-eval-prefix target stream)
`(directory-exists ,path))))
(:method call-with-target-locked (target fn)
"Lock the directory (file), not target."
(claim-file* target path)
(call-with-target-locked path fn))
;; The time it takes to create a directory -- not worth measuring.
(:method target-build-time (target) 0)
(:method (setf target-build-time) (value target)
(declare (ignore value))))
(defclass file-digest-ref (ref)
((name :type pathname
:initarg :file
:reader file-digest-ref.file)))
(defun file-digest-ref (file)
(make 'file-digest-ref :file (ensure-pathname file)))
(defmethods file-digest-ref (target (file name))
(:method target-stamp (target)
(file-stamp/hash file))
(:method target-exists? (target)
(file-exists-p file))
(:method target= (target (other file-digest-ref))
(pathname-equal file (file-digest-ref.file other)))
(:method hash-target (target)
(dx-sxhash (list 'file-digest-ref file)))
(:method resolve-target (target &optional base)
(if (absolute-pathname-p file) target
(make 'file-digest-ref
:file (merge-pathnames* file (or base (base))))))
(:method target-timestamp (target)
(target-timestamp file))
(:method (setf target-timestamp) (value target)
(setf (target-timestamp file) value))
(:method target-build-script (target)
(target-build-script file))
(:method target-node-label (target)
(target-node-label file))
(:method print-object (target stream)
(if (not *print-escape*)
(call-next-method)
(format stream "~a~s"
(read-eval-prefix target stream)
`(file-digest-ref ,file))))
(:method call-with-target-locked (target fn)
(claim-file* target file)
(call-with-target-locked file fn))
(:method target-build-time (target)
(build-time-from-file target file)))
(defclass pattern-ref (ref)
;; Note that the pattern slot has a silly type: a pattern ref can be
;; either a symbol or an instance of `pattern', which is not yet
;; defined. Being able to directly pass in patterns will be useful
;; later when we bootstrap support for languages.
((pattern
:initarg :pattern
:type (or symbol standard-object delayed-symbol)
:reader pattern-ref-pattern)
(name
:initarg :inputs
:type (list-of pathname)
:reader pattern-ref-static-inputs
:reader target-static-prereqs)
(outputs
:type (list-of pathname)
:initarg :outputs
:reader pattern-ref-outputs)
(base
:type pathname
:reader pattern-ref-base
:initform (base)))
(:default-initargs
:inputs '()
:outputs '()))
;;; Re. merge-*-defaults. Originally I was planning on a DSL, but
;;; pathnames seems to work, as long as we're careful about how we
;;; merge them. The order is important: we merge the *provided* inputs
;;; and outputs into the *defaults*, rather than vice-versa.
(defgeneric merge-input-defaults (pattern inputs)
(:method (pattern inputs)
(let ((defaults (pattern.input-defaults pattern)))
(collecting
(dolist (input inputs)
(let ((input
(if (stringp input)
(resolve-file input)
input)))
(dolist (default defaults)
;; Here we want to preserve the host of the provided
;; input, so we use uiop:merge-pathnames*.
(collect (merge-pathnames* default input)))))))))
(defgeneric merge-output-defaults (pattern inputs)
(:method (pattern inputs)
(let* ((defaults (pattern.output-defaults pattern))
(defaults (mapcar #'resolve-file defaults)))
(collecting
(dolist (input inputs)
(let ((input
(if (stringp input)
;; Not resolve-file; default should be able to
;; override path.
(parse-unix-namestring input)
input)))
(dolist (default defaults)
;; We want to be able to redirect to the output to a
;; different host, so we use good old cl:merge-pathnames.
(collect (merge-pathnames default input)))))))))
(defmethods pattern-ref (self (inputs name) outputs pattern base)
(:method initialize-instance :after (self &key (merge t))
(unless (or inputs outputs)
(error* "~
A pattern ref needs either outputs OR at least one input (or both)."))
(when merge
(let* ((*base* base)
(pattern (find-pattern pattern)))
(setf inputs
(merge-input-defaults pattern inputs))
(setf outputs
(merge-output-defaults pattern inputs))))
(when (equal inputs outputs)
(error* "Invalid pattern ref: inputs and outputs are the same."))
(unless outputs
(error* "Cannot determine outputs for ~a.
You must either provide a list of outputs, or provide a list of inputs from which the outputs can be default."
self)))
(:method print-object (self stream)
(let ((pattern-name
(if (typep pattern '(or delayed-symbol symbol))
pattern
(pattern-name pattern))))
(if *print-escape*
(let* ((name (maybe-delay-symbol pattern-name))
(name-form
(if (symbolp name)
`(quote ,name)
name)))
(format stream "~a~s"
(read-eval-prefix self stream)
`(make 'pattern-ref
:pattern ,name-form
:inputs ',inputs
:outputs ',outputs
:merge nil)))
(print-unreadable-object (self stream :type t)
(format stream "~a ~a -> ~a"
pattern-name
inputs
outputs)))))
(:method load-form-slot-names append (self)
'(pattern inputs outputs))
(:method fset:compare (self (other pattern-ref))
(fset:compare-slots self other
#'pattern-ref-static-inputs
#'pattern-ref-outputs
#'pattern-ref-pattern))
(:method target-stamp (self)
(multiple-file-stamp outputs))
(:method resolve-target (self &optional (base base))
(if (and (every #'absolute-pathname-p inputs)
(every #'absolute-pathname-p outputs))
self
(make 'pattern-ref
:merge nil
:pattern (pattern-ref-pattern self)
:inputs (mapcar (op (ensure-absolute _ :base base)) inputs)
:outputs (mapcar (op (ensure-absolute _ :base base)) outputs))))
(:method target= (self (other pattern-ref))
;; Remember order is significant.
(and (equal inputs (pattern-ref-static-inputs other))
(equal outputs (pattern-ref-outputs other))
;; Might be a delayed symbol.
(target= pattern (pattern-ref-pattern other))))
(:method hash-target (self)
(dx-sxhash
(list 'pattern-ref
inputs
outputs)))
(:method target-build-script (self)
(let ((pattern (find-pattern pattern)))
(task self
(lambda ()
(depends-on inputs)
(pattern-build pattern inputs outputs))
(pattern.script pattern)
base)))
(:method call-with-target-locked (self fn)
(claim-files* self outputs)
;; Always take locks in the same global order (to prevent
;; deadlocks).
(call-with-targets-locked outputs fn))
(:method target-node-label (self)
(fmt "~{~a~^, ~}"
(mapcar #'native-namestring outputs)))
(:method delete-target (self)
(apply #'delete-targets outputs))
(:method target-build-time (self)
(build-time-from-files self inputs)))
(defun pattern-ref (inputs pattern)
"Make a pattern reference."
(pattern-from inputs pattern))
(defun pattern-from (inputs pattern)
(make 'pattern-ref
:pattern pattern
:inputs (ensure-list inputs)))
(defun pattern-into (outputs pattern)
(make 'pattern-ref
:pattern pattern
:outputs (ensure-list outputs)))
(defun pattern (inputs name)
(pattern-ref inputs name))
(defun make-pattern (pattern-name
&key (inputs nil inputs-supplied?)
(outputs nil outputs-supplied?))
(unless (or inputs-supplied? outputs-supplied?)
(error* "You must supply either or both of INPUTS or OUTPUTS."))
(make 'pattern-ref
:pattern pattern-name
:inputs (ensure-list inputs)
:outputs (ensure-list outputs)))
;;; NB Figure out whether this actually replaces all possible uses of
;;; ifcreate. (It replaces the original use case, resolving files, but
;;; not all possible use cases.)
(defgeneric relative-file-truename (target))
(defclass relative-file-target (externalizable)
((path
:type relative-pathname
:initarg :path)))
(defmethods relative-file-target (self path)
(:method load-form-slot-names append (self)
'(path))
(:method target-stamp (self)
(let ((truename (relative-file-truename self)))
(if (not (file-exists-p truename))
never
(resolved-file truename
(target-stamp truename)))))
(:method target-exists? (self)
(file-exists-p (relative-file-truename self)))
(:method target= (self (other relative-file-target))
(equal (relative-file-truename self)
(relative-file-truename other)))
(:method target-build-script (self)
(target-build-script (relative-file-truename self)))
(:method call-with-locked-target (self fn)
(let ((file (relative-file-truename self)))
(call-with-locked-target file fn)))
(:method target-build-time (self)
(let ((file (relative-file-truename self)))
(build-time-from-file self file))))
(defclass system-resource (relative-file-target)
((system
:type string
:initarg :system
:reader system-resource.system))
(:documentation "Depend on a system-relative resource.
The natural (Redo-ish) thing to do here would be to depend directly on
the resolved file, but also add non-existent prerequisites for any
possible path to $system.asd that might, in the future, supersede its
current location.
However, the rules ASDF uses to resolve system locations (see §8.1 in
the ASDF manual) are far too flexible for the enumeration of possible
locations to be practical.
Since exhaustive enumeration is out, we use a different approach. We
use a special kind of stamp that stores the resolved pathname. If the
newly resolved pathname agrees with the stored one, then the metadata
from that file is used. But if they disagree, then the dependency is
treated as out-of-date, regardless of file metadata."))
(defmethods system-resource (self system path)
(:method print-object (self stream)
(if (not *print-escape*)
(print-unreadable-object (self stream :type t)
(format stream "~a ~a" system path))
(progn
(write-string (read-eval-prefix self stream) stream)
(format stream "~s"
`(system-resource ,system ,path)))))
(:method relative-file-truename (self)
(asdf:system-relative-pathname
(system-resource.system self)
path))
(:method hash-target (self)
(dx-sxhash (list 'system-resource system path)))
(:method target-node-label (self)
(fmt "resource ~a in system ~a" path system)))
(defun system-resource (system path)
(make 'system-resource
:system (string-downcase system)
:path (assure relative-pathname
(ensure-pathname path :want-pathname t))))
(deftype target ()
;; NB Not allowing lists of targets as targets is a conscious
;; decision. It would make things much more complicated. In
;; particular, there would no longer be a single timestamp for a
;; target, because the proper timestamp to use for a list of targets
;; would depend on whether it was being depended on (in which case
;; we want the /newest/ timestamp) or doing the depending (in which
;; case we want the /oldest/ timestamp).
'(not list))
(defconstructor task
"A task."
(target target)
(thunk function)
(script target)
(base (or null directory-pathname)))
(defmethod target-build-script :around ((target t))
(check-not-frozen)
(assure task
(call-next-method)))
;;; Manipulating targets.
(define-global-state *symbol-timestamps* (make-hash-table :size 1024))
(declaim (type hash-table *symbol-timestamps*))
(defun pathname-exists? (path)
(etypecase-of (or string pathname) path
(string (pathname-exists? (ensure-pathname path :want-pathname t)))
(pathname
(or (file-exists-p path)
(directory-exists-p path)))))
(defmethod target-exists? ((target package))
t)
(defmethod target-exists? ((target symbol))
(boundp target))
(defmethod target-exists? ((target delayed-symbol))
(target-exists? (force-symbol target)))
(defmethod target-exists? ((target cl:pathname))
(pathname-exists? (resolve-target target)))
(defmethod target-timestamp ((target package))
never)
(defmethod target-timestamp ((target symbol))
(if (boundp target)
(let ((now (now)))
(ensure2 (gethash target *symbol-timestamps*)
now))
never))
(defmethod target-timestamp ((target delayed-symbol))
(target-timestamp (force-symbol target)))
(defmethod target-timestamp ((target cl:pathname))
(if (pathname-exists? target)
(file-mtime target)
never))
(defmethod (setf target-timestamp) :before (timestamp target)
(declare (ignore target timestamp))
(check-not-frozen))
(defmethod (setf target-timestamp) (timestamp (target delayed-symbol))
(let ((target (force-symbol target)))
(setf (target-timestamp target) timestamp)))
(defmethod (setf target-timestamp) (timestamp (target symbol))
;; Configurations need to set the timestamp while unbound
#+(or) (unless (boundp target)
(error* "Trying to set timestamp for unbound symbol ~s"
target))
(setf (gethash target *symbol-timestamps*)
(assure stamp timestamp)))
(defmethod (setf target-timestamp) (timestamp (target cl:pathname))
(declare (ignore timestamp))
(if (pathname-exists? target)
;; TODO There must be some portable way to do this.
(error* "Cannot set pathname timestamps (yet).")
(open target :direction :probe :if-does-not-exist :create)))
(defun touch-target (target &optional (date (now)))
(setf (target-timestamp target) date))
(defun delete-file-or-directory (p)
(if (directory-pathname-p p)
(delete-directory-tree p)
(delete-file-if-exists p)))
(defmethod resolve-target ((target cl:pathname) &optional base)
(if (absolute-pathname-p target) target
(let ((path (merge-pathnames* target (or base (base)))))
(if (wild-pathname-p path)
(directory* path)
path))))
(defmethod target= ((x delayed-symbol) y)
(declare (ignore y))
;; If Y is not a symbol, or a delayed symbol, the answer is no.
nil)
(defmethod target= (x (y delayed-symbol))
(target= y x))
(defmethod target= ((x delayed-symbol) (y symbol))
(target= x (delay-symbol y)))
(defmethod target= ((x symbol) (y delayed-symbol))
(target= y x))
(defmethod target= ((x delayed-symbol) (y delayed-symbol))
(fset:equal? x y))
(defmethod target= ((x cl:pathname) (y cl:pathname))
(pathname-equal x y))
(defun deduplicate-targets (targets &key (key #'identity))
;; (test-chamber:with-experiment
;; (:class 'test-chamber:noisy-experiment
;; :test (op (set-equal _ _ :test #'target=))
;; :enabled nil)
;; (remove-duplicates targets :test #'target=))
(collecting
(fbind key
(let ((table (make-target-table :size (length targets))))
(do-each (target targets)
(let ((count
(incf
(ensure2 (target-table-ref table (key target))
0))))
(when (= count 1)
(collect target))))))))
;;; Locking targets.
(defmethod call-with-target-locked ((target delayed-symbol) fn)
(let ((target (force-symbol target)))
(call-with-target-locked target fn)))
(defmethod call-with-target-locked ((target cl:pathname) fn)
(let ((resolved (resolve-target target)))
(if (equal target resolved)
(call-next-method)
(call-with-target-locked target fn))))
;;; Targets not worth metering.
(defmethod target-build-time ((target delayed-symbol))
(target-build-time (force-symbol target)))
(defmethod (setf target-build-time) (value (target delayed-symbol))
(setf (target-build-time (force-symbol target)) value))
(defmethod target-build-time ((target cl:pathname))