0.8.12.18: Rearranging COMPILER-ERROR protocol
[sbcl.git] / src / cold / shared.lisp
1 ;;;; stuff which is not specific to any particular build phase, but
2 ;;;; used by most of them
3 ;;;;
4 ;;;; Note: It's specifically not used when bootstrapping PCL, because
5 ;;;; we do SAVE-LISP after that, and we don't want to save extraneous
6 ;;;; bootstrapping machinery into the frozen image which will
7 ;;;; subsequently be used as the mother of all Lisp sessions.
8
9 ;;;; This software is part of the SBCL system. See the README file for
10 ;;;; more information.
11 ;;;;
12 ;;;; This software is derived from the CMU CL system, which was
13 ;;;; written at Carnegie Mellon University and released into the
14 ;;;; public domain. The software is in the public domain and is
15 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
16 ;;;; files for more information.
17
18 ;;; SB-COLD holds stuff used to build the initial SBCL core file
19 ;;; (including not only the final construction of the core file, but
20 ;;; also the preliminary steps like e.g. building the cross-compiler
21 ;;; and running the cross-compiler to produce target FASL files).
22 (defpackage "SB-COLD" (:use "CL"))
23
24 (in-package "SB-COLD")
25
26 ;;; FIXME: This is embarassing -- SBCL violates SBCL style-package
27 ;;; locks on the host lisp. Rather then find and fix all the cases
28 ;;; right now, let's just remain self-hosting. The problems at least
29 ;;; involve a few defvars and local macros with names in the CL
30 ;;; package.
31 #+sbcl
32 (let ((plp (find-symbol PACKAGE-LOCKED-P :sb-ext)))
33   (when (and plp (fboundp plp))
34     (dolist (p (list-all-packages))
35       (sb-ext::unlock-package p))))
36
37 ;;; prefixes for filename stems when cross-compiling. These are quite arbitrary
38 ;;; (although of course they shouldn't collide with anything we don't want to
39 ;;; write over). In particular, they can be either relative path names (e.g.
40 ;;; "host-objects/" or absolute pathnames (e.g. "/tmp/sbcl-xc-host-objects/").
41 ;;;
42 ;;; The cross-compilation process will force the creation of these directories
43 ;;; by executing CL:ENSURE-DIRECTORIES-EXIST (on the xc host Common Lisp).
44 (defvar *host-obj-prefix*)
45 (defvar *target-obj-prefix*)
46
47 ;;; suffixes for filename stems when cross-compiling
48 (defvar *host-obj-suffix* 
49   (or
50    ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
51    ;; has the same extension that the host uses for COMPILE-FILE
52    ;; output, so we have to be careful to use the xc host's preferred
53    ;; extension.
54    ;;
55    ;; FIXME: This is a little ugly and annoying to maintain. And
56    ;; there's very likely some way to rearrange the build process so
57    ;; that we never explicitly refer to host object file suffixes,
58    ;; only to the result of CL:COMPILE-FILE-PATHNAME.
59    #+lispworks ".ufsl" ; as per Lieven Marchand sbcl-devel 2002-02-01
60    #+(and openmcl (not darwin)) ".pfsl"
61    #+(and openmcl darwin) ".dfsl"
62    ;; On most xc hosts, any old extension works, so we use an
63    ;; arbitrary one.
64    ".lisp-obj"))
65 (defvar *target-obj-suffix*
66   ;; Target fasl files are LOADed (actually only quasi-LOADed, in
67   ;; GENESIS) only by SBCL code, and it doesn't care about particular
68   ;; extensions, so we can use something arbitrary.
69   ".lisp-obj")
70
71 ;;; a function of one functional argument, which calls its functional argument
72 ;;; in an environment suitable for compiling the target. (This environment
73 ;;; includes e.g. a suitable *FEATURES* value.)
74 (declaim (type function *in-target-compilation-mode-fn*))
75 (defvar *in-target-compilation-mode-fn*)
76
77 ;;; a function with the same calling convention as CL:COMPILE-FILE, to be
78 ;;; used to translate ordinary Lisp source files into target object files
79 (declaim (type function *target-compile-file*))
80 (defvar *target-compile-file*)
81
82 ;;; designator for a function with the same calling convention as
83 ;;; SB-C:ASSEMBLE-FILE, to be used to translate assembly files into target
84 ;;; object files
85 (defvar *target-assemble-file*)
86 \f
87 ;;;; some tools
88
89 ;;; Take the file named X and make it into a file named Y. Sorta like
90 ;;; UNIX, and unlike Common Lisp's bare RENAME-FILE, we don't allow
91 ;;; information from the original filename to influence the final
92 ;;; filename. (The reason that it's only sorta like UNIX is that in
93 ;;; UNIX "mv foo bar/" will work, but the analogous
94 ;;; (RENAME-FILE-A-LA-UNIX "foo" "bar/") should fail.)
95 ;;;
96 ;;; (This is a workaround for the weird behavior of Debian CMU CL
97 ;;; 2.4.6, where (RENAME-FILE "dir/x" "dir/y") tries to create a file
98 ;;; called "dir/dir/y". If that behavior goes away, then we should be
99 ;;; able to get rid of this function and use plain RENAME-FILE in the
100 ;;; COMPILE-STEM function above. -- WHN 19990321
101 (defun rename-file-a-la-unix (x y)
102
103   (let ((path    ;; (Note that the TRUENAME expression here is lifted from an
104                  ;; example in the ANSI spec for TRUENAME.)
105          (with-open-file (stream y :direction :output)
106            (close stream)
107            ;; From the ANSI spec: "In this case, the file is closed
108            ;; when the truename is tried, so the truename
109            ;; information is reliable."
110            (truename stream))))
111     (delete-file path)
112     (rename-file x path)))
113 (compile 'rename-file-a-la-unix)
114
115 ;;; a wrapper for compilation/assembly, used mostly to centralize
116 ;;; the procedure for finding full filenames from "stems"
117 ;;;
118 ;;; Compile the source file whose basic name is STEM, using some
119 ;;; standard-for-the-SBCL-build-process procedures to generate the
120 ;;; full pathnames of source file and object file. Return the pathname
121 ;;; of the object file for STEM. Several &KEY arguments are accepted:
122 ;;;   :SRC-PREFIX, :SRC-SUFFIX =
123 ;;;      strings to be concatenated to STEM to produce source filename
124 ;;;   :OBJ-PREFIX, :OBJ-SUFFIX =
125 ;;;      strings to be concatenated to STEM to produce object filename
126 ;;;   :TMP-OBJ-SUFFIX-SUFFIX =
127 ;;;      string to be appended to the name of an object file to produce 
128 ;;;      the name of a temporary object file
129 ;;;   :COMPILE-FILE, :IGNORE-FAILURE-P =
130 ;;;     :COMPILE-FILE is a function to use for compiling the file
131 ;;;     (with the same calling conventions as ANSI CL:COMPILE-FILE).
132 ;;;     If the third return value (FAILURE-P) of this function is
133 ;;;     true, a continuable error will be signalled, unless
134 ;;;     :IGNORE-FAILURE-P is set, in which case only a warning will be
135 ;;;     signalled.
136 (defun compile-stem (stem
137                      &key
138                      (obj-prefix "")
139                      (obj-suffix (error "missing OBJ-SUFFIX"))
140                      (tmp-obj-suffix-suffix "-tmp")
141                      (src-prefix "")
142                      (src-suffix ".lisp")
143                      (compile-file #'compile-file)
144                      ignore-failure-p)
145
146   (declare (type function compile-file))
147
148   (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
149          ;; Lisp Way, although it works just fine for common UNIX environments.
150          ;; Should it come to pass that the system is ported to environments
151          ;; where version numbers and so forth become an issue, it might become
152          ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
153          ;; machinery instead of just using strings. In the absence of such a
154          ;; port, it might or might be a good idea to do the rewrite.
155          ;; -- WHN 19990815
156          (src (concatenate 'string src-prefix stem src-suffix))
157          (obj (concatenate 'string obj-prefix stem obj-suffix))
158          (tmp-obj (concatenate 'string obj tmp-obj-suffix-suffix)))
159
160     (ensure-directories-exist obj :verbose t)
161
162     ;; We're about to set about building a new object file. First, we
163     ;; delete any preexisting object file in order to avoid confusing
164     ;; ourselves later should we happen to bail out of compilation
165     ;; with an error.
166     (when (probe-file obj)
167       (delete-file obj))
168
169     ;; Original comment:
170     ;;
171     ;;   Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
172     ;;   mangles relative pathnames passed as :OUTPUT-FILE arguments,
173     ;;   but works OK with absolute pathnames.
174     ;;
175     ;; following discussion on cmucl-imp 2002-07
176     ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
177     ;; absolute pathnames all the time; it is no longer clear that the
178     ;; original behaviour in CLISP was wrong or that the current
179     ;; behaviour is right; and in any case absolutifying the pathname
180     ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
181     (setf tmp-obj
182           ;; (Note that this idiom is taken from the ANSI
183           ;; documentation for TRUENAME.)
184           (with-open-file (stream tmp-obj :direction :output)
185             (close stream)
186             (truename stream)))
187     ;; and some compilers (e.g. OpenMCL) will complain if they're
188     ;; asked to write over a file that exists already (and isn't
189     ;; recognizeably a fasl file), so
190     (when (probe-file tmp-obj)
191       (delete-file tmp-obj))
192
193     ;; Try to use the compiler to generate a new temporary object file.
194     (flet ((report-recompile-restart (stream)
195              (format stream "Recompile file ~S" src))
196            (report-continue-restart (stream)
197              (format stream "Continue, using possibly bogus file ~S" obj)))
198       (tagbody
199        retry-compile-file
200          (multiple-value-bind (output-truename warnings-p failure-p)
201              (funcall compile-file src :output-file tmp-obj)
202            (declare (ignore warnings-p))
203            (cond ((not output-truename)
204                   (error "couldn't compile ~S" src))
205                  (failure-p
206                   (if ignore-failure-p
207                       (warn "ignoring FAILURE-P return value from compilation of ~S"
208                             src)
209                       (unwind-protect
210                            (restart-case
211                                (error "FAILURE-P was set when creating ~S."
212                                       obj)
213                              (recompile ()
214                                :report report-recompile-restart
215                                (go retry-compile-file))
216                              (continue ()
217                                :report report-continue-restart
218                                (setf failure-p nil)))
219                         ;; Don't leave failed object files lying around.
220                         (when (and failure-p (probe-file tmp-obj))
221                           (delete-file tmp-obj)
222                           (format t "~&deleted ~S~%" tmp-obj)))))
223                  ;; Otherwise: success, just fall through.
224                  (t nil)))))
225
226     ;; If we get to here, compilation succeeded, so it's OK to rename
227     ;; the temporary output file to the permanent object file.
228     (rename-file-a-la-unix tmp-obj obj)
229
230     ;; nice friendly traditional return value
231     (pathname obj)))
232 (compile 'compile-stem)
233
234 ;;; other miscellaneous tools
235 (load "src/cold/read-from-file.lisp")
236 (load "src/cold/rename-package-carefully.lisp")
237 (load "src/cold/with-stuff.lisp")
238
239 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
240 (load "src/cold/ansify.lisp")
241 \f
242 ;;;; special read-macros for building the cold system (and even for
243 ;;;; building some of our tools for building the cold system)
244
245 (load "src/cold/shebang.lisp")
246
247 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
248 ;;; not in general the same as the *FEATURES* set for the host Lisp.
249 ;;; In order to refer to target features specifically, we refer to
250 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
251 ;;; readmacros instead of the ordinary #+ and #- readmacros.
252 (setf *shebang-features*
253       (let* ((default-features
254                (append (read-from-file "base-target-features.lisp-expr")
255                        (read-from-file "local-target-features.lisp-expr")))
256              (customizer-file-name "customize-target-features.lisp")
257              (customizer (if (probe-file customizer-file-name)
258                              (compile nil 
259                                       (read-from-file customizer-file-name))
260                              #'identity)))
261         (funcall customizer default-features)))
262 (let ((*print-length* nil)
263       (*print-level* nil))
264   (format t
265           "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
266           *shebang-features*))
267
268 (defvar *shebang-backend-subfeatures*
269   (let* ((default-subfeatures nil)
270          (customizer-file-name "customize-backend-subfeatures.lisp")
271          (customizer (if (probe-file customizer-file-name)
272                          (compile nil 
273                                   (read-from-file customizer-file-name))
274                          #'identity)))
275     (funcall customizer default-subfeatures)))
276 (let ((*print-length* nil)
277       (*print-level* nil))
278   (format t
279           "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
280           *shebang-backend-subfeatures*))
281 \f
282 ;;;; cold-init-related PACKAGE and SYMBOL tools
283
284 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
285 ;;; it's probably a mistake if we change it (beyond changing the
286 ;;; values of special variables such as *** and +, anyway). Set up
287 ;;; machinery to warn us when/if we change it.
288 ;;;
289 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
290 #!+sb-show
291 (progn
292   (load "src/cold/snapshot.lisp")
293   (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
294 \f
295 ;;;; master list of source files and their properties
296
297 ;;; flags which can be used to describe properties of source files
298 (defparameter
299   *expected-stem-flags*
300   '(;; meaning: This file is not to be compiled when building the
301     ;; cross-compiler which runs on the host ANSI Lisp. ("not host
302     ;; code", i.e. does not execute on host -- but may still be
303     ;; cross-compiled by the host, so that it executes on the target)
304     :not-host
305     ;; meaning: This file is not to be compiled as part of the target
306     ;; SBCL. ("not target code" -- but still presumably host code,
307     ;; used to support the cross-compilation process)
308     :not-target
309     ;; meaning: This file is to be processed with the SBCL assembler,
310     ;; not COMPILE-FILE. (Note that this doesn't make sense unless
311     ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
312     ;; while the cross-compiler is being built in the host ANSI Lisp.)
313     :assem
314     ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
315     ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
316     ;; For now, it exists so that compilation can proceed through the
317     ;; legacy warnings in src/compiler/x86/array.lisp, which I've
318     ;; never figured out but which were apparently acceptable in CMU
319     ;; CL. Eventually, it would be great to just get rid of all
320     ;; warnings and remove support for this flag. -- WHN 19990323)
321     :ignore-failure-p))
322
323 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
324
325 (defmacro do-stems-and-flags ((stem flags) &body body)
326   (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
327     `(dolist (,stem-and-flags *stems-and-flags*)
328        (let ((,stem (first ,stem-and-flags))
329              (,flags (rest ,stem-and-flags)))
330          ,@body))))
331
332 ;;; Check for stupid typos in FLAGS list keywords.
333 (let ((stems (make-hash-table :test 'equal)))
334   (do-stems-and-flags (stem flags)
335     (if (gethash stem stems)
336       (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
337       (setf (gethash stem stems) t))
338     (let ((set-difference (set-difference flags *expected-stem-flags*)))
339       (when set-difference
340         (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
341                set-difference)))))
342 \f
343 ;;;; tools to compile SBCL sources to create the cross-compiler
344
345 ;;; Execute function FN in an environment appropriate for compiling the
346 ;;; cross-compiler's source code in the cross-compilation host.
347 (defun in-host-compilation-mode (fn)
348   (declare (type function fn))
349   (let ((*features* (cons :sb-xc-host *features*))
350         ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
351         ;; base-target-features.lisp-expr:
352         (*shebang-features* (set-difference *shebang-features*
353                                             '(:sb-propagate-float-type
354                                               :sb-propagate-fun-type))))
355     (with-additional-nickname ("SB-XC" "SB!XC")
356       (funcall fn))))
357 (compile 'in-host-compilation-mode)
358
359 ;;; Process a file as source code for the cross-compiler, compiling it
360 ;;; (if necessary) in the appropriate environment, then loading it
361 ;;; into the cross-compilation host Common lisp.
362 (defun host-cload-stem (stem &key ignore-failure-p)
363   (let ((compiled-filename (in-host-compilation-mode
364                             (lambda ()
365                               (compile-stem
366                                stem
367                                :obj-prefix *host-obj-prefix*
368                                :obj-suffix *host-obj-suffix*
369                                :compile-file #'cl:compile-file
370                                :ignore-failure-p ignore-failure-p)))))
371     (load compiled-filename)))
372 (compile 'host-cload-stem)
373
374 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
375 (defun host-load-stem (stem &key ignore-failure-p)
376   (declare (ignore ignore-failure-p)) ; (It's only relevant when
377   ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
378   ;; construct complete filenames from stems in here as well as in
379   ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
380   ;; 19990815
381   (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
382 (compile 'host-load-stem)
383 \f
384 ;;;; tools to compile SBCL sources to create object files which will
385 ;;;; be used to create the target SBCL .core file
386
387 ;;; Run the cross-compiler on a file in the source directory tree to
388 ;;; produce a corresponding file in the target object directory tree.
389 (defun target-compile-stem (stem &key assem-p ignore-failure-p)
390   (funcall *in-target-compilation-mode-fn*
391            (lambda ()
392              (compile-stem stem
393                            :obj-prefix *target-obj-prefix*
394                            :obj-suffix *target-obj-suffix*
395                            :ignore-failure-p ignore-failure-p
396                            :compile-file (if assem-p
397                                              *target-assemble-file*
398                                              *target-compile-file*)))))
399 (compile 'target-compile-stem)
400
401 ;;; (This function is not used by the build process, but is intended
402 ;;; for interactive use when experimenting with the system. It runs
403 ;;; the cross-compiler on test files with arbitrary filenames, not
404 ;;; necessarily in the source tree, e.g. in "/tmp".)
405 (defun target-compile-file (filename)
406   (funcall *in-target-compilation-mode-fn*
407            (lambda ()
408              (funcall *target-compile-file* filename))))
409 (compile 'target-compile-file)