lisp-action.lisp 11.9 KB
Newer Older
1 2 3
;;;; -------------------------------------------------------------------------
;;;; Actions to build Common Lisp software

4
(uiop/package:define-package :asdf/lisp-action
5
  (:recycle :asdf/lisp-action :asdf)
6
  (:use :uiop/common-lisp :uiop :asdf/upgrade :asdf/session
7
   :asdf/component :asdf/system :asdf/find-system
8
   :asdf/operation :asdf/action)
9
  (:export
10
   #:try-recompiling
11
   #:cl-source-file #:cl-source-file.cl #:cl-source-file.lsp
12
   #:basic-load-op #:basic-compile-op
13 14
   #:load-op #:prepare-op #:compile-op #:test-op #:load-source-op #:prepare-source-op
   #:call-with-around-compile-hook
15
   #:perform-lisp-compilation #:perform-lisp-load-fasl #:perform-lisp-load-source
16
   #:lisp-compilation-output-files))
17 18 19 20
(in-package :asdf/lisp-action)


;;;; Component classes
21 22
(with-upgradability ()
  (defclass cl-source-file (source-file)
23
    ((type :initform "lisp"))
Robert Goldman's avatar
Robert Goldman committed
24
    (:documentation "Component class for a Common Lisp source file (using type \"lisp\")"))
25
  (defclass cl-source-file.cl (cl-source-file)
26
    ((type :initform "cl"))
Robert Goldman's avatar
Robert Goldman committed
27
    (:documentation "Component class for a Common Lisp source file using type \"cl\""))
28
  (defclass cl-source-file.lsp (cl-source-file)
29
    ((type :initform "lsp"))
Robert Goldman's avatar
Robert Goldman committed
30
    (:documentation "Component class for a Common Lisp source file using type \"lsp\"")))
31 32 33


;;;; Operation classes
34
(with-upgradability ()
35
  (defclass basic-load-op (operation) ()
Robert Goldman's avatar
Robert Goldman committed
36
    (:documentation "Base class for operations that apply the load-time effects of a file"))
37
  (defclass basic-compile-op (operation) ()
Robert Goldman's avatar
Robert Goldman committed
38
    (:documentation "Base class for operations that apply the compile-time effects of a file")))
39

40 41

;;; Our default operations: loading into the current lisp image
42
(with-upgradability ()
43
  (defclass prepare-op (upward-operation sideway-operation)
44
    ((sideway-operation :initform 'load-op :allocation :class))
45
    (:documentation "Load the dependencies for the COMPILE-OP or LOAD-OP of a given COMPONENT."))
46
  (defclass load-op (basic-load-op downward-operation selfward-operation)
47
    ;; NB: even though compile-op depends on prepare-op it is not needed-in-image-p,
48
    ;; so we need to directly depend on prepare-op for its side-effects in the current image.
49 50
    ((selfward-operation :initform '(prepare-op compile-op) :allocation :class))
    (:documentation "Operation for loading the compiled FASL for a Lisp file"))
51
  (defclass compile-op (basic-compile-op downward-operation selfward-operation)
52 53 54
    ((selfward-operation :initform 'prepare-op :allocation :class))
    (:documentation "Operation for compiling a Lisp file to a FASL"))

55

56
  (defclass prepare-source-op (upward-operation sideway-operation)
57 58
    ((sideway-operation :initform 'load-source-op :allocation :class))
    (:documentation "Operation for loading the dependencies of a Lisp file as source."))
59
  (defclass load-source-op (basic-load-op downward-operation selfward-operation)
60 61
    ((selfward-operation :initform 'prepare-source-op :allocation :class))
    (:documentation "Operation for loading a Lisp file as source."))
62

63
  (defclass test-op (selfward-operation)
64 65 66
    ((selfward-operation :initform 'load-op :allocation :class))
    (:documentation "Operation for running the tests for system.
If the tests fail, an error will be signaled.")))
67 68


69
;;;; Methods for prepare-op, compile-op and load-op
70 71

;;; prepare-op
72 73 74 75 76 77 78
(with-upgradability ()
  (defmethod action-description ((o prepare-op) (c component))
    (format nil (compatfmt "~@<loading dependencies of ~3i~_~A~@:>") c))
  (defmethod perform ((o prepare-op) (c component))
    nil)
  (defmethod input-files ((o prepare-op) (s system))
    (if-let (it (system-source-file s)) (list it))))
79 80

;;; compile-op
81 82 83 84 85
(with-upgradability ()
  (defmethod action-description ((o compile-op) (c component))
    (format nil (compatfmt "~@<compiling ~3i~_~A~@:>") c))
  (defmethod action-description ((o compile-op) (c parent-component))
    (format nil (compatfmt "~@<completing compilation for ~3i~_~A~@:>") c))
86
  (defgeneric call-with-around-compile-hook (component thunk)
Robert Goldman's avatar
Robert Goldman committed
87 88 89 90
    (:documentation "A method to be called around the PERFORM'ing of actions that apply the
compile-time side-effects of file (i.e., COMPILE-OP or LOAD-SOURCE-OP). This method can be used
to setup readtables and other variables that control reading, macroexpanding, and compiling, etc.
Note that it will NOT be called around the performing of LOAD-OP."))
91 92 93
  (defmethod call-with-around-compile-hook ((c component) function)
    (call-around-hook (around-compile-hook c) function))
  (defun perform-lisp-compilation (o c)
94
    "Perform the compilation of the Lisp file associated to the specified action (O . C)."
95 96 97
    (let (;; Before 2.26.53, that was unfortunately component-pathname. Now,
          ;; we consult input-files, the first of which should be the one to compile-file
          (input-file (first (input-files o c)))
98
          ;; On some implementations, there are more than one output-file,
99 100 101 102 103 104
          ;; but the first one should always be the primary fasl that gets loaded.
          (outputs (output-files o c)))
      (multiple-value-bind (output warnings-p failure-p)
          (destructuring-bind
              (output-file
               &optional
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
105
                 #+(or clasp ecl mkcl) object-file
106
                 #+clisp lib-file
107 108 109 110 111 112 113
                 warnings-file &rest rest) outputs
            ;; Allow for extra outputs that are not of type warnings-file
            ;; The way we do it is kludgy. In ASDF4, output-files shall not be positional.
            (declare (ignore rest))
            (when warnings-file
              (unless (equal (pathname-type warnings-file) (warnings-file-type))
                (setf warnings-file nil)))
114 115
            (call-with-around-compile-hook
             c #'(lambda (&rest flags)
116 117 118 119 120 121
                   (apply 'compile-file* input-file
                          :output-file output-file
                          :external-format (component-external-format c)
                          :warnings-file warnings-file
                          (append
                           #+clisp (list :lib-file lib-file)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
122
                           #+(or clasp ecl mkcl) (list :object-file object-file)
123
                           flags)))))
124 125 126
        (check-lisp-compile-results output warnings-p failure-p
                                    "~/asdf-action::format-action/" (list (cons o c))))))
  (defun report-file-p (f)
Robert Goldman's avatar
Robert Goldman committed
127
    "Is F a build report file containing, e.g., warnings to check?"
128
    (equalp (pathname-type f) "build-report"))
129
  (defun perform-lisp-warnings-check (o c)
Robert Goldman's avatar
Robert Goldman committed
130
    "Check the warnings associated with the dependencies of an action."
131 132 133 134 135 136 137 138 139 140 141 142 143 144
    (let* ((expected-warnings-files (remove-if-not #'warnings-file-p (input-files o c)))
           (actual-warnings-files (loop :for w :in expected-warnings-files
                                        :when (get-file-stamp w)
                                          :collect w
                                        :else :do (warn "Missing warnings file ~S while ~A"
                                                        w (action-description o c)))))
      (check-deferred-warnings actual-warnings-files)
      (let* ((output (output-files o c))
             (report (find-if #'report-file-p output)))
        (when report
          (with-open-file (s report :direction :output :if-exists :supersede)
            (format s ":success~%"))))))
  (defmethod perform ((o compile-op) (c cl-source-file))
    (perform-lisp-compilation o c))
145
  (defun lisp-compilation-output-files (o c)
Robert Goldman's avatar
Robert Goldman committed
146 147
    "Compute the output-files for compiling the Lisp file for the specified action (O . C),
an OPERATION and a COMPONENT."
148 149
    (let* ((i (first (input-files o c)))
           (f (compile-file-pathname
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
150 151
               i #+clasp :output-type #+ecl :type #+(or clasp ecl) :fasl
               #+mkcl :fasl-p #+mkcl t)))
152
      `(,f ;; the fasl is the primary output, in first position
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
153 154 155
        #+clasp
        ,@(unless nil ;; was (use-ecl-byte-compiler-p)
            `(,(compile-file-pathname i :output-type :object)))
156 157 158 159 160 161
        #+clisp
        ,@`(,(make-pathname :type "lib" :defaults f))
        #+ecl
        ,@(unless (use-ecl-byte-compiler-p)
            `(,(compile-file-pathname i :type :object)))
        #+mkcl
162 163 164
        ,(compile-file-pathname i :fasl-p nil) ;; object file
        ,@(when (and *warnings-file-type* (not (builtin-system-p (component-system c))))
            `(,(make-pathname :type *warnings-file-type* :defaults f))))))
165 166
  (defmethod output-files ((o compile-op) (c cl-source-file))
    (lisp-compilation-output-files o c))
167 168
  (defmethod perform ((o compile-op) (c static-file))
    nil)
169 170

  ;; Performing compile-op on a system will check the deferred warnings for the system
171
  (defmethod perform ((o compile-op) (c system))
172
    (when (and *warnings-file-type* (not (builtin-system-p c)))
173 174
      (perform-lisp-warnings-check o c)))
  (defmethod input-files ((o compile-op) (c system))
175 176 177 178 179 180
    (when (and *warnings-file-type* (not (builtin-system-p c)))
      ;; The most correct way to do it would be to use:
      ;; (traverse-sub-actions o c :other-systems nil :keep-operation 'compile-op :keep-component 'cl-source-file)
      ;; but it's expensive and we don't care too much about file order or ASDF extensions.
      (loop :for sub :in (sub-components c :type 'cl-source-file)
            :nconc (remove-if-not 'warnings-file-p (output-files o sub)))))
181 182 183
  (defmethod output-files ((o compile-op) (c system))
    (when (and *warnings-file-type* (not (builtin-system-p c)))
      (if-let ((pathname (component-pathname c)))
184
        (list (subpathname pathname (coerce-filename c) :type "build-report"))))))
185 186

;;; load-op
187 188 189 190 191
(with-upgradability ()
  (defmethod action-description ((o load-op) (c cl-source-file))
    (format nil (compatfmt "~@<loading FASL for ~3i~_~A~@:>") c))
  (defmethod action-description ((o load-op) (c parent-component))
    (format nil (compatfmt "~@<completing load for ~3i~_~A~@:>") c))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
192 193
  (defmethod action-description ((o load-op) (c component))
    (format nil (compatfmt "~@<loading ~3i~_~A~@:>") c))
194 195 196 197 198 199 200 201 202 203
  (defmethod perform-with-restarts ((o load-op) (c cl-source-file))
    (loop
      (restart-case
          (return (call-next-method))
        (try-recompiling ()
          :report (lambda (s)
                    (format s "Recompile ~a and try loading it again"
                            (component-name c)))
          (perform (find-operation o 'compile-op) c)))))
  (defun perform-lisp-load-fasl (o c)
Robert Goldman's avatar
Robert Goldman committed
204 205
    "Perform the loading of a FASL associated to specified action (O . C),
an OPERATION and a COMPONENT."
206
    (if-let (fasl (first (input-files o c)))
207
      (load* fasl)))
208 209 210
  (defmethod perform ((o load-op) (c cl-source-file))
    (perform-lisp-load-fasl o c))
  (defmethod perform ((o load-op) (c static-file))
211
    nil))
212 213 214 215 216


;;;; prepare-source-op, load-source-op

;;; prepare-source-op
217 218 219 220 221 222 223
(with-upgradability ()
  (defmethod action-description ((o prepare-source-op) (c component))
    (format nil (compatfmt "~@<loading source for dependencies of ~3i~_~A~@:>") c))
  (defmethod input-files ((o prepare-source-op) (s system))
    (if-let (it (system-source-file s)) (list it)))
  (defmethod perform ((o prepare-source-op) (c component))
    nil))
224 225

;;; load-source-op
226
(with-upgradability ()
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
227
  (defmethod action-description ((o load-source-op) (c component))
228 229 230 231
    (format nil (compatfmt "~@<Loading source of ~3i~_~A~@:>") c))
  (defmethod action-description ((o load-source-op) (c parent-component))
    (format nil (compatfmt "~@<Loaded source of ~3i~_~A~@:>") c))
  (defun perform-lisp-load-source (o c)
232
    "Perform the loading of a Lisp file as associated to specified action (O . C)"
233 234
    (call-with-around-compile-hook
     c #'(lambda ()
235 236
           (load* (first (input-files o c))
                  :external-format (component-external-format c)))))
237 238 239 240 241

  (defmethod perform ((o load-source-op) (c cl-source-file))
    (perform-lisp-load-source o c))
  (defmethod perform ((o load-source-op) (c static-file))
    nil))
242 243 244


;;;; test-op
245 246 247 248 249
(with-upgradability ()
  (defmethod perform ((o test-op) (c component))
    nil)
  (defmethod operation-done-p ((o test-op) (c system))
    "Testing a system is _never_ done."
250
    nil))