utility.lisp 11.1 KB
Newer Older
1
2
3
;;;; -------------------------------------------------------------------------
;;;; General Purpose Utilities for ASDF

4
5
(asdf/package:define-package :asdf/utility
  (:recycle :asdf/utility :asdf)
6
  (:use :common-lisp :asdf/package :asdf/compatibility)
7
  (:export
8
9
10
   ;; magic helper to define debugging functions:
   #:asdf-debug #:load-asdf-debug-utility #:*asdf-debug-utility*
   #:undefine-function #:undefine-functions #:defun* #:defgeneric* ;; (un)defining functions
11
   #:if-bind ;; basic flow control
12
   #:while-collecting #:appendf #:length=n-p #:remove-keys #:remove-keyword ;; lists and plists
13
14
15
   #:emptyp ;; sequences
   #:first-char #:last-char #:split-string ;; strings
   #:string-prefix-p #:string-enclosed-p #:string-suffix-p
16
17
18
19
   #:find-class* ;; CLOS
   #:stamp< #:stamp<= #:earlier-stamp #:stamps-earliest #:earliest-stamp ;; stamps
   #:later-stamp #:stamps-latest #:latest-stamp #:latest-stamp-f
   #:list-to-hash-set ;; hash-table
20
   #:ensure-function #:call-function #:call-functions #:register-hook-function ;; functions
21
   #:eval-string #:load-string #:load-stream
22
   #:parse-version #:unparse-version #:version-compatible-p)) ;; version
23
24
(in-package :asdf/utility)

25
26
27
28
29
;;;; Defining functions in a way compatible with hot-upgrade:
;; DEFUN* and DEFGENERIC* use FMAKUNBOUND to delete any previous fdefinition,
;; thus replacing the function without warning or error
;; even if the signature and/or generic-ness of the function has changed.
;; For a generic function, this invalidates any previous DEFMETHOD.
30
31
32
(eval-when (:load-toplevel :compile-toplevel :execute)
  (defun undefine-function (function-spec)
    (cond
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
33
34
35
36
37
38
39
      ((symbolp function-spec)
       #+clisp
       (let ((f (and (fboundp function-spec) (fdefinition function-spec))))
         (when (typep f 'clos:standard-generic-function)
           (loop :for m :in (clos:generic-function-methods f)
                 :do (remove-method f m))))
       (fmakunbound function-spec))
40
41
42
43
44
45
46
      ((and (consp function-spec) (eq (car function-spec) 'setf)
            (consp (cdr function-spec)) (null (cddr function-spec)))
       #-(or gcl<2.7) (fmakunbound function-spec))
      (t (error "bad function spec ~S" function-spec))))
  (defun undefine-functions (function-spec-list)
    (map () 'undefine-function function-spec-list)))

47
48
49
50
(macrolet
    ((defdef (def* def)
       `(defmacro ,def* (name formals &rest rest)
          `(progn
51
             #-clisp ;; clisp is unhappy about fmakunbound.
52
             (undefine-function ',name)
53
             #-gcl ; gcl 2.7.0 notinline functions lose secondary return values :-(
54
55
             ,@(when (and #+ecl (symbolp name)) ; fails for setf functions on ecl
                 `((declaim (notinline ,name))))
56
57
58
59
             (,',def ,name ,formals ,@rest)))))
  (defdef defgeneric* defgeneric)
  (defdef defun* defun))

60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

;;; Magic debugging help. See contrib/debug.lisp
(defvar *asdf-debug-utility*
  '(symbol-call :asdf :system-relative-pathname :asdf "contrib/debug.lisp")
  "form that evaluates to the pathname to your favorite debugging utilities")

(defmacro asdf-debug (&optional package utility-file)
  `(eval-when (:compile-toplevel :load-toplevel :execute)
     (load-asdf-debug-utility ',package ',utility-file)))

(defun* load-asdf-debug-utility (&optional package utility-file)
  (let* ((*package* (if package (find-package package) *package*))
         (keyword (read-from-string
                   (format nil ":DBG-~:@(~A~)" (package-name *package*)))))
    (unless (member keyword *features*)
      (let* ((utility-file (or utility-file *asdf-debug-utility*))
             (file (ignore-errors (probe-file (eval utility-file)))))
        (if file (load file)
            (error "Failed to locate debug utility file: ~S" utility-file))))))


81
;;; Flow control
82
83
84
(defmacro if-bind ((var test) then &optional else)
  `(let ((,var ,test)) (if ,var ,then ,else)))

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

;;; List manipulation
(defmacro while-collecting ((&rest collectors) &body body)
  "COLLECTORS should be a list of names for collections.  A collector
defines a function that, when applied to an argument inside BODY, will
add its argument to the corresponding collection.  Returns multiple values,
a list for each collection, in order.
   E.g.,
\(while-collecting \(foo bar\)
           \(dolist \(x '\(\(a 1\) \(b 2\) \(c 3\)\)\)
             \(foo \(first x\)\)
             \(bar \(second x\)\)\)\)
Returns two values: \(A B C\) and \(1 2 3\)."
  (let ((vars (mapcar #'(lambda (x) (gensym (symbol-name x))) collectors))
        (initial-values (mapcar (constantly nil) collectors)))
    `(let ,(mapcar #'list vars initial-values)
       (flet ,(mapcar #'(lambda (c v) `(,c (x) (push x ,v) (values))) collectors vars)
         ,@body
         (values ,@(mapcar #'(lambda (v) `(reverse ,v)) vars))))))

(define-modify-macro appendf (&rest args)
  append "Append onto list") ;; only to be used on short lists.

(defun* length=n-p (x n) ;is it that (= (length x) n) ?
  (check-type n (integer 0 *))
  (loop
    :for l = x :then (cdr l)
    :for i :downfrom n :do
    (cond
      ((zerop i) (return (null l)))
      ((not (consp l)) (return nil)))))

;;; Keyword argument lists
(defun* remove-keys (key-names args)
  (loop :for (name val) :on args :by #'cddr
    :unless (member (symbol-name name) key-names
                    :key #'symbol-name :test 'equal)
    :append (list name val)))

(defun* remove-keyword (key args)
  (loop :for (k v) :on args :by #'cddr
    :unless (eq k key)
    :append (list k v)))

129
;;; Sequences
130
(defun* emptyp (x)
131
132
133
  "Predicate that is true for an empty sequence"
  (or (null x) (and (vectorp x) (zerop (length x)))))

134
;;; Strings
135

136
137
138
139
140
141
142
143
144
145
146
147
(defun* first-char (s)
  (and (stringp s) (plusp (length s)) (char s 0)))

(defun* last-char (s)
  (and (stringp s) (plusp (length s)) (char s (1- (length s)))))

(defun* split-string (string &key max (separator '(#\Space #\Tab)))
  "Split STRING into a list of components separated by
any of the characters in the sequence SEPARATOR.
If MAX is specified, then no more than max(1,MAX) components will be returned,
starting the separation from the end, e.g. when called with arguments
 \"a.b.c.d.e\" :max 3 :separator \".\" it will return (\"a.b.c\" \"d\" \"e\")."
148
  (block ()
149
150
    (let ((list nil) (words 0) (end (length string)))
      (flet ((separatorp (char) (find char separator))
151
             (done () (return (cons (subseq string 0 end) list))))
152
153
154
155
156
157
158
159
160
161
        (loop
          :for start = (if (and max (>= words (1- max)))
                           (done)
                           (position-if #'separatorp string :end end :from-end t)) :do
          (when (null start)
            (done))
          (push (subseq string (1+ start) end) list)
          (incf words)
          (setf end start))))))

162
(defun* string-prefix-p (prefix string)
163
164
165
166
167
168
169
  "Does STRING begin with PREFIX?"
  (let* ((x (string prefix))
         (y (string string))
         (lx (length x))
         (ly (length y)))
    (and (<= lx ly) (string= x y :end2 lx))))

170
(defun* string-suffix-p (string suffix)
171
172
173
174
175
176
177
  "Does STRING end with SUFFIX?"
  (let* ((x (string string))
         (y (string suffix))
         (lx (length x))
         (ly (length y)))
    (and (<= ly lx) (string= x y :start1 (- lx ly)))))

178
(defun* string-enclosed-p (prefix string suffix)
179
180
181
182
  "Does STRING begin with PREFIX and end with SUFFIX?"
  (and (string-prefix-p prefix string)
       (string-suffix-p string suffix)))

183
184
185
186
187
188
189
190

;;; CLOS
(defun* find-class* (x &optional (errorp t) environment)
  (etypecase x
    ((or standard-class built-in-class) x)
    #+gcl<2.7 (keyword nil)
    (symbol (find-class x errorp environment))))

191
192
193

;;; stamps: a REAL or boolean where NIL=-infinity, T=+infinity
(deftype stamp () '(or real boolean))
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(defun* stamp< (x y)
  (etypecase x
    (null (and y t))
    ((eql t) nil)
    (real (etypecase y
            (null nil)
            ((eql t) t)
            (real (< x y))))))
;;(defun* stamps< (list) (loop :for y :in list :for x = nil :then y :always (stamp< x y)))
;;(defun* stamp*< (&rest list) (stamps< list))
(defun* stamp<= (x y) (not (stamp< y x)))
(defun* earlier-stamp (x y) (if (stamp< x y) x y))
(defun* stamps-earliest (list) (reduce 'earlier-stamp list :initial-value t))
(defun* earliest-stamp (&rest list) (stamps-earliest list))
(defun* later-stamp (x y) (if (stamp< x y) y x))
(defun* stamps-latest (list) (reduce 'later-stamp list :initial-value nil))
(defun* latest-stamp (&rest list) (stamps-latest list))
(define-modify-macro latest-stamp-f (&rest stamps) latest-stamp)

213

214
215
216
217
;;; Hash-tables
(defun* list-to-hash-set (list &aux (h (make-hash-table :test 'equal)))
  (dolist (x list h) (setf (gethash x h) t)))

218

219
;;; Code execution
220
221
222
223
(defun* eval-string (string)
  "Evaluate a form read from a string."
  (eval (read-from-string string)))

224
(defun* ensure-function (fun &key (package :cl))
225
  (etypecase fun
226
    ((or boolean keyword character number pathname) (constantly fun))
227
228
229
230
    ((or function symbol) fun)
    (cons (eval `(function ,fun)))
    (string (eval `(function ,(with-standard-io-syntax
                                (let ((*package* (find-package package)))
231
                                  (read-from-string fun))))))))
232

233
234
(defun* call-function (function-spec &rest arguments)
  (apply (ensure-function function-spec) arguments))
235
236

(defun* call-functions (function-specs)
237
  (map () 'call-function function-specs))
238

239
240
241
242
(defun* register-hook-function (variable hook &optional (now t))
  (pushnew hook (symbol-value variable))
  (when now (call-function hook)))

243
244
245
246
247
248
249
250

;;; Version handling
(defun* parse-version (string &optional on-error)
  "Parse a version string as a series of natural integers separated by dots.
Return a (non-null) list of integers if the string is valid, NIL otherwise.
If on-error is error, warn, or designates a function of compatible signature,
the function is called with an explanation of what is wrong with the argument.
NB: ignores leading zeroes, and so doesn't distinguish between 2.003 and 2.3"
251
252
253
254
255
256
257
258
259
260
261
  (block nil
   (unless (stringp string)
     (call-function on-error "~S: ~S is not a string" 'parse-version string)
     (return))
   (unless (loop :for prev = nil :then c :for c :across string
                 :always (or (digit-char-p c)
                             (and (eql c #\.) prev (not (eql prev #\.))))
                 :finally (return (and c (digit-char-p c))))
     (call-function on-error "~S: ~S doesn't follow asdf version numbering convention"
                    'parse-version string)
     (return))
262
263
   (mapcar #'parse-integer (split-string string :separator "."))))

264
265
266
(defun* unparse-version (version-list)
  (format nil "~{~D~^.~}" version-list))

267
268
269
270
271
(defun* version-compatible-p (provided-version required-version)
  "Is the provided version a compatible substitution for the required-version?
If major versions differ, it's not compatible.
If they are equal, then any later version is compatible,
with later being determined by a lexicographical comparison of minor numbers."
272
273
  (let ((x (parse-version provided-version 'warn))
        (y (parse-version required-version 'warn)))
274
275
276
277
278
279
280
281
282
    (labels ((bigger (x y)
               (cond ((not y) t)
                     ((not x) nil)
                     ((> (car x) (car y)) t)
                     ((= (car x) (car y))
                      (bigger (cdr x) (cdr y))))))
      (and x y (= (car x) (car y))
           (or (not (cdr y)) (bigger (cdr x) (cdr y)))))))