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