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