lisp-build.lisp 14.5 KB
Newer Older
1
2
3
;;;; -------------------------------------------------------------------------
;;;; Support to build (compile and load) Lisp files

4
(asdf/package:define-package :asdf/lisp-build
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
5
  (:recycle :asdf/interface :asdf :asdf/lisp-build)
6
7
  (:use :common-lisp :asdf/package :asdf/compatibility :asdf/utility
        :asdf/pathname :asdf/stream :asdf/os :asdf/image)
8
  (:export
9
   ;; Variables
10
   #:*compile-file-warnings-behaviour* #:*compile-file-failure-behaviour*
11
   #:*output-translation-function*
12
13
14
15
16
   #:*optimization-settings* #:*previous-optimization-settings*
   #:*uninteresting-compiler-conditions* #:*uninteresting-loader-conditions*
   #:*deferred-warnings*
   ;; Functions & Macros
   #:get-optimization-settings #:proclaim-optimization-settings
17
18
   #:call-with-muffled-compiler-conditions #:with-muffled-compiler-conditions
   #:call-with-muffled-loader-conditions #:with-muffled-loader-conditions
19
   #:call-with-asdf-compilation-unit #:with-asdf-compilation-unit
20
   #:current-lisp-file-pathname #:lispize-pathname #:compile-file-type #:call-around-hook
21
   #:compile-file* #:compile-file-pathname*
22
   #:load* #:load-from-string #:combine-fasls)
23
  (:intern #:defaults #:failure-p #:warnings-p #:s #:y #:body))
24
25
26
27
28
29
30
31
32
33
34
35
36
(in-package :asdf/lisp-build)

(defvar *compile-file-warnings-behaviour*
  (or #+clisp :ignore :warn)
  "How should ASDF react if it encounters a warning when compiling a file?
Valid values are :error, :warn, and :ignore.")

(defvar *compile-file-failure-behaviour*
  (or #+(or mkcl sbcl) :error #+clisp :ignore :warn)
  "How should ASDF react if it encounters a failure (per the ANSI spec of COMPILE-FILE)
when compiling a file?  Valid values are :error, :warn, and :ignore.
Note that ASDF ALWAYS raises an error if it fails to create an output file when compiling.")

37
38
39
40
41

;;; Optimization settings

(defvar *optimization-settings* nil)
(defvar *previous-optimization-settings* nil)
42
(defun* get-optimization-settings ()
43
44
45
46
47
48
49
50
51
52
53
54
55
  "Get current compiler optimization settings, ready to PROCLAIM again"
  (let ((settings '(speed space safety debug compilation-speed #+(or cmu scl) c::brevity)))
    #-(or clisp clozure cmu ecl sbcl scl)
    (warn "xcvb-driver::get-optimization-settings does not support your implementation. Please help me fix that.")
    #.`(loop :for x :in settings
         ,@(or #+clozure '(:for v :in '(ccl::*nx-speed* ccl::*nx-space* ccl::*nx-safety* ccl::*nx-debug* ccl::*nx-cspeed*))
               #+ecl '(:for v :in '(c::*speed* c::*space* c::*safety* c::*debug*))
               #+(or cmu scl) '(:for f :in '(c::cookie-speed c::cookie-space c::cookie-safety c::cookie-debug c::cookie-cspeed c::cookie-brevity)))
         :for y = (or #+clisp (gethash x system::*optimize*)
                      #+(or clozure ecl) (symbol-value v)
                      #+(or cmu scl) (funcall f c::*default-cookie*)
                      #+sbcl (cdr (assoc x sb-c::*policy*)))
         :when y :collect (list x y))))
56
(defun* proclaim-optimization-settings ()
57
58
59
60
61
62
63
64
65
  "Proclaim the optimization settings in *OPTIMIZATION-SETTINGS*"
  (proclaim `(optimize ,@*optimization-settings*))
  (let ((settings (get-optimization-settings)))
    (unless (equal *previous-optimization-settings* settings)
      (setf *previous-optimization-settings* settings))))


;;; Condition control

66
(defvar *uninteresting-compiler-conditions*
67
68
69
70
71
72
73
74
75
  (append
   #+sbcl
   '(sb-c::simple-compiler-note
     "&OPTIONAL and &KEY found in the same lambda list: ~S"
     sb-int:package-at-variance
     sb-kernel:uninteresting-redefinition
     sb-kernel:undefined-alien-style-warning
     sb-ext:implicit-generic-function-warning
     sb-kernel:lexical-environment-too-complex
76
77
78
79
80
81
     "Couldn't grovel for ~A (unknown to the C compiler)."
     ;; BEWARE: the below four are controversial to include here.
     sb-kernel:redefinition-with-defun
     sb-kernel:redefinition-with-defgeneric
     sb-kernel:redefinition-with-defmethod
     sb-kernel::redefinition-with-defmacro) ; not exported by old SBCLs
82
   ;;#+clozure '(ccl:compiler-warning)
83
84
   '("No generic function ~S present when encountering macroexpansion of defmethod. Assuming it will be an instance of standard-generic-function.")) ;; from closer2mop
  "Conditions that may be skipped while compiling")
85

86
(defvar *uninteresting-loader-conditions*
87
88
89
90
  (append
   '("Overwriting already existing readtable ~S." ;; from named-readtables
     #(#:finalizers-off-warning :asdf-finalizers)) ;; from asdf-finalizers
   #+clisp '(clos::simple-gf-replacing-method-warning))
91
  "Additional conditions that may be skipped while loading")
92
93
94
95
96
97

(defvar *deferred-warnings* ()
  "Warnings the handling of which is deferred until the end of the compilation unit")

;;;; ----- Filtering conditions while building -----

98
99
(defun* call-with-muffled-compiler-conditions (thunk)
  (call-with-muffled-conditions
100
    thunk *uninteresting-compiler-conditions*))
101
(defmacro with-muffled-compiler-conditions ((&optional) &body body)
102
  "Run BODY where uninteresting compiler conditions are muffled"
103
104
105
  `(call-with-muffled-compiler-conditions #'(lambda () ,@body)))
(defun* call-with-muffled-loader-conditions (thunk)
  (call-with-muffled-conditions
106
   thunk (append *uninteresting-compiler-conditions* *uninteresting-loader-conditions*)))
107
(defmacro with-muffled-loader-conditions ((&optional) &body body)
108
  "Run BODY where uninteresting compiler and additional loader conditions are muffled"
109
  `(call-with-muffled-loader-conditions #'(lambda () ,@body)))
110
111
112

(defun* save-forward-references (forward-references)
  ;; TODO: replace with stuff in POIU
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
  "Save forward reference conditions so they may be issued at a latter time,
possibly in a different process."
  #+sbcl
  (loop :for w :in sb-c::*undefined-warnings*
    :for kind = (sb-c::undefined-warning-kind w) ; :function :variable :type
    :for name = (sb-c::undefined-warning-name w)
    :for symbol = (cond
                    ((consp name)
                     (unless (eq kind :function)
                       (error "unrecognized warning ~S not a function?" w))
                     (ecase (car name)
                       ((setf)
                        (assert (and (consp (cdr name)) (null (cddr name))) ())
				  (setf kind :setf-function)
                        (second name))
                       ((sb-pcl::slot-accessor)
                        (assert (eq :global (second name)))
                        (assert (eq 'boundp (fourth name)))
                        (assert (null (nthcdr 4 name)))
                        (setf kind :sb-pcl-global-boundp-slot-accessor)
                        (third name))))
                    (t
                     (assert (member kind '(:function :variable :type)) ())
                     name))
    :for symbol-name = (symbol-name symbol)
    :for package-name = (package-name (symbol-package symbol))
    :collect `(:undefined ,symbol-name ,package-name ,kind) :into undefined-warnings
    :finally (setf *deferred-warnings* undefined-warnings
                   sb-c::*undefined-warnings* nil))
  (when forward-references
    (with-open-file (s forward-references :direction :output :if-exists :supersede)
      (write *deferred-warnings* :stream s :pretty t :readably t)
      (terpri s))))

147
(defun* call-with-asdf-compilation-unit (thunk &key forward-references)
148
149
150
151
  (with-compilation-unit (:override t)
    (let ((*deferred-warnings* ())
          #+sbcl (sb-c::*undefined-warnings* nil))
      (multiple-value-prog1
152
          (with-muffled-compiler-conditions ()
153
154
155
156
157
158
159
160
161
162
163
            (funcall thunk))
        (save-forward-references forward-references)))))

(defmacro with-asdf-compilation-unit ((&key forward-references) &body body)
  "Like WITH-COMPILATION-UNIT, but saving forward-reference issues
for processing later (possibly in a different process)."
  `(call-with-xcvb-compilation-unit #'(lambda () ,@body) :forward-references ,forward-references))


;;; from ASDF

164
165
166
(defun* current-lisp-file-pathname ()
  (or *compile-file-pathname* *load-pathname*))

167
168
169
(defun* lispize-pathname (input-file)
  (make-pathname :type "lisp" :defaults input-file))

170
(defun* compile-file-type (&rest keys)
171
172
  "pathname TYPE for lisp FASt Loading files"
  (declare (ignorable keys))
173
  #-(or ecl mkcl) (load-time-value (pathname-type (compile-file-pathname "foo.lisp")))
174
  #+(or ecl mkcl) (pathname-type (apply 'compile-file-pathname "foo" keys)))
175

176
(defun* call-around-hook (hook function)
177
  (call-function (or hook 'funcall) function))
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
(defun* compile-file* (input-file &rest keys
                                  &key compile-check output-file #+(or ecl mkcl) object-file
                                  &allow-other-keys)
  "This function provides a portable wrapper around COMPILE-FILE.
It ensures that the OUTPUT-FILE value is only returned and
the file only actually created if the compilation was successful,
even though your implementation may not do that, and including
an optional call to an user-provided consistency check function COMPILE-CHECK;
it will call this function if not NIL at the end of the compilation
with the arguments sent to COMPILE-FILE*, except with :OUTPUT-FILE TMP-FILE
where TMP-FILE is the name of a temporary output-file.
It also checks two flags (with legacy british spelling from ASDF1),
*COMPILE-FILE-FAILURE-BEHAVIOUR* and *COMPILE-FILE-WARNINGS-BEHAVIOUR*
with appropriate implementation-dependent defaults,
and if a failure (respectively warnings) are reported by COMPILE-FILE
with consider it an error unless the respective behaviour flag
is one of :SUCCESS :WARN :IGNORE.
On ECL or MKCL, it creates both the linkable object and loadable fasl files.
On implementations that erroneously do not recognize standard keyword arguments,
it will filter them appropriately."
199
  (let* ((keywords (remove-keys
200
201
                    `(:compile-check
                      #+gcl<2.7 ,@'(:external-format :print :verbose)) keys))
202
         (output-file (apply 'compile-file-pathname* input-file :output-file output-file keywords))
203
204
         #+ecl
         (object-file
205
           (unless (use-ecl-byte-compiler-p)
206
207
208
209
210
             (or object-file
                 (compile-file-pathname output-file :type :object))))
         #+mkcl
         (object-file
           (or object-file
211
212
               (compile-file-pathname output-file :fasl-p nil)))
         (tmp-file (tmpize-pathname output-file)))
213
    (multiple-value-bind (output-truename warnings-p failure-p)
214
        (or #-(or ecl mkcl) (apply 'compile-file input-file :output-file tmp-file keywords)
215
            #+ecl (apply 'compile-file input-file :output-file
216
                         (if object-file
217
218
                             (list* object-file :system-p t keywords)
                             (list* output-file keywords)))
219
            #+mkcl (apply 'compile-file input-file :output-file object-file :fasl-p nil keywords))
220
      (cond
221
222
223
224
225
226
227
        ((and output-truename
              (flet ((check-flag (flag behaviour)
                       (or (not flag) (member behaviour '(:success :warn :ignore)))))
                (and (check-flag failure-p *compile-file-failure-behaviour*)
                     (check-flag warnings-p *compile-file-warnings-behaviour*)))
              (progn
                #+(or ecl mkcl)
228
                (when (and #+ecl object-file)
229
230
231
232
233
234
                  (setf output-truename
                        (compiler::build-fasl
                         tmp-file #+ecl :lisp-files #+mkcl :lisp-object-files
                                  (list object-file))))
                (or (not compile-check)
                    (apply compile-check input-file :output-file tmp-file keywords))))
235
236
237
         (delete-file-if-exists output-file)
         (when output-truename
           (rename-file-overwriting-target output-truename output-file)
238
           (setf output-truename (truename output-file))))
239
240
        (t ;; error or failed check
         (delete-file-if-exists output-truename)
241
         (setf output-truename nil)))
242
243
244
      (values output-truename warnings-p failure-p))))

(defun* compile-file-pathname* (input-file &rest keys &key output-file &allow-other-keys)
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
245
246
247
248
249
  (let* ((keys
           (remove-keys `(#+(and allegro (not (version>= 8 2))) :external-format
                            ,@(unless output-file '(:output-file))) keys)))
    (if (absolute-pathname-p output-file)
        ;; what cfp should be doing, w/ mp* instead of mp
250
        (let* ((type (pathname-type (apply 'compile-file-type keys)))
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
251
252
253
               (defaults (make-pathname
                          :type type :defaults (merge-pathnames* input-file))))
          (merge-pathnames* output-file defaults))
254
        (funcall *output-translation-function*
Francois-Rene Rideau's avatar
Francois-Rene Rideau committed
255
                 (apply 'compile-file-pathname input-file keys)))))
256

257
(defun* load* (x &rest keys &key &allow-other-keys)
258
  (etypecase x
259
    ((or pathname string #-(or gcl<2.7 clozure allegro) stream)
260
     (apply 'load x
261
            #-gcl<2.7 keys #+gcl<2.7 (remove-key :external-format keys)))
262
263
264
265
266
    #-(or gcl<2.7 clozure allegro)
    ;; GCL 2.6 can't load from a string-input-stream
    ;; ClozureCL 1.6 can only load from file input stream
    ;; Allegro 5, I don't remember but it must have been broken when I tested.
    (stream ;; make do this way
267
268
269
270
     (let ((*package* *package*)
           (*readtable* *readtable*)
           (*load-pathname* nil)
           (*load-truename* nil))
271
272
273
274
275
276
277
       (eval-input x)))))

(defun* load-from-string (string)
  "Portably read and evaluate forms from a STRING."
  (with-input-from-string (s string) (load* s)))

;;; Links FASLs together
278
279
(defun* combine-fasls (inputs output)
  #-(or allegro clisp clozure cmu lispworks sbcl scl xcl)
280
281
  (error "~A does not support ~S~%inputs ~S~%output  ~S"
         (implementation-type) 'combine-fasls inputs output)
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  #+clozure (ccl:fasl-concatenate output inputs :if-exists :supersede)
  #+(or allegro clisp cmu sbcl scl xcl) (concatenate-files inputs output)
  #+lispworks
  (let (fasls)
    (unwind-protect
         (progn
           (loop :for i :in inputs
                 :for n :from 1
                 :for f = (add-pathname-suffix
                           output (format nil "-FASL~D" n))
                 :do #-lispworks-personal-edition (lispworks:copy-file i f)
                     #+lispworks-personal-edition (concatenate-files (list i) f)
                     (push f fasls))
           (ignore-errors (lispworks:delete-system :fasls-to-concatenate))
           (eval `(scm:defsystem :fasls-to-concatenate
                    (:default-pathname ,(pathname-directory-pathname output))
                    :members
                    ,(loop :for f :in (reverse fasls)
                           :collect `(,(namestring f) :load-only t))))
           (scm:concatenate-system output :fasls-to-concatenate))
      (loop :for f :in fasls :do (ignore-errors (delete-file f)))
      (ignore-errors (lispworks:delete-system :fasls-to-concatenate)))))