95f125ac1bb4260aa34ac98891455c6615518f4d
[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    #+(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 :direction :output)
174             (close stream)
175             (truename stream)))
176     ;; and some compilers (e.g. OpenMCL) will complain if they're
177     ;; asked to write over a file that exists already (and isn't
178     ;; recognizeably a fasl file), so
179     (when (probe-file tmp-obj)
180       (delete-file tmp-obj))
181
182     ;; Try to use the compiler to generate a new temporary object file.
183     (flet ((report-recompile-restart (stream)
184              (format stream "Recompile file ~S" src))
185            (report-continue-restart (stream)
186              (format stream "Continue, using possibly bogus file ~S" obj)))
187       (tagbody
188        retry-compile-file
189          (multiple-value-bind (output-truename warnings-p failure-p)
190              (funcall compile-file src :output-file tmp-obj)
191            (declare (ignore warnings-p))
192            (cond ((not output-truename)
193                   (error "couldn't compile ~S" src))
194                  (failure-p
195                   (if ignore-failure-p
196                       (warn "ignoring FAILURE-P return value from compilation of ~S"
197                             src)
198                       (unwind-protect
199                            (restart-case
200                                (error "FAILURE-P was set when creating ~S."
201                                       obj)
202                              (recompile ()
203                                :report report-recompile-restart
204                                (go retry-compile-file))
205                              (continue ()
206                                :report report-continue-restart
207                                (setf failure-p nil)))
208                         ;; Don't leave failed object files lying around.
209                         (when (and failure-p (probe-file tmp-obj))
210                           (delete-file tmp-obj)
211                           (format t "~&deleted ~S~%" tmp-obj)))))
212                  ;; Otherwise: success, just fall through.
213                  (t nil)))))
214
215     ;; If we get to here, compilation succeeded, so it's OK to rename
216     ;; the temporary output file to the permanent object file.
217     (rename-file-a-la-unix tmp-obj obj)
218
219     ;; nice friendly traditional return value
220     (pathname obj)))
221 (compile 'compile-stem)
222
223 ;;; other miscellaneous tools
224 (load "src/cold/read-from-file.lisp")
225 (load "src/cold/rename-package-carefully.lisp")
226 (load "src/cold/with-stuff.lisp")
227
228 ;;; Try to minimize/conceal any non-standardness of the host Common Lisp.
229 (load "src/cold/ansify.lisp")
230 \f
231 ;;;; special read-macros for building the cold system (and even for
232 ;;;; building some of our tools for building the cold system)
233
234 (load "src/cold/shebang.lisp")
235
236 ;;; When cross-compiling, the *FEATURES* set for the target Lisp is
237 ;;; not in general the same as the *FEATURES* set for the host Lisp.
238 ;;; In order to refer to target features specifically, we refer to
239 ;;; *SHEBANG-FEATURES* instead of *FEATURES*, and use the #!+ and #!-
240 ;;; readmacros instead of the ordinary #+ and #- readmacros.
241 (setf *shebang-features*
242       (let* ((default-features
243                (append (read-from-file "base-target-features.lisp-expr")
244                        (read-from-file "local-target-features.lisp-expr")))
245              (customizer-file-name "customize-target-features.lisp")
246              (customizer (if (probe-file customizer-file-name)
247                              (compile nil 
248                                       (read-from-file customizer-file-name))
249                              #'identity)))
250         (funcall customizer default-features)))
251 (let ((*print-length* nil)
252       (*print-level* nil))
253   (format t
254           "target features *SHEBANG-FEATURES*=~@<~S~:>~%"
255           *shebang-features*))
256
257 (defvar *shebang-backend-subfeatures*
258   (let* ((default-subfeatures nil)
259          (customizer-file-name "customize-backend-subfeatures.lisp")
260          (customizer (if (probe-file customizer-file-name)
261                          (compile nil 
262                                   (read-from-file customizer-file-name))
263                          #'identity)))
264     (funcall customizer default-subfeatures)))
265 (let ((*print-length* nil)
266       (*print-level* nil))
267   (format t
268           "target backend-subfeatures *SHEBANG-BACKEND-FEATURES*=~@<~S~:>~%"
269           *shebang-backend-subfeatures*))
270 \f
271 ;;;; cold-init-related PACKAGE and SYMBOL tools
272
273 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
274 ;;; it's probably a mistake if we change it (beyond changing the
275 ;;; values of special variables such as *** and +, anyway). Set up
276 ;;; machinery to warn us when/if we change it.
277 ;;;
278 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
279 #!+sb-show
280 (progn
281   (load "src/cold/snapshot.lisp")
282   (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
283 \f
284 ;;;; master list of source files and their properties
285
286 ;;; flags which can be used to describe properties of source files
287 (defparameter
288   *expected-stem-flags*
289   '(;; meaning: This file is not to be compiled when building the
290     ;; cross-compiler which runs on the host ANSI Lisp. ("not host
291     ;; code", i.e. does not execute on host -- but may still be
292     ;; cross-compiled by the host, so that it executes on the target)
293     :not-host
294     ;; meaning: This file is not to be compiled as part of the target
295     ;; SBCL. ("not target code" -- but still presumably host code,
296     ;; used to support the cross-compilation process)
297     :not-target
298     ;; meaning: This file is to be processed with the SBCL assembler,
299     ;; not COMPILE-FILE. (Note that this doesn't make sense unless
300     ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
301     ;; while the cross-compiler is being built in the host ANSI Lisp.)
302     :assem
303     ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
304     ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
305     ;; For now, it exists so that compilation can proceed through the
306     ;; legacy warnings in src/compiler/x86/array.lisp, which I've
307     ;; never figured out but which were apparently acceptable in CMU
308     ;; CL. Eventually, it would be great to just get rid of all
309     ;; warnings and remove support for this flag. -- WHN 19990323)
310     :ignore-failure-p))
311
312 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
313
314 (defmacro do-stems-and-flags ((stem flags) &body body)
315   (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
316     `(dolist (,stem-and-flags *stems-and-flags*)
317        (let ((,stem (first ,stem-and-flags))
318              (,flags (rest ,stem-and-flags)))
319          ,@body))))
320
321 ;;; Check for stupid typos in FLAGS list keywords.
322 (let ((stems (make-hash-table :test 'equal)))
323   (do-stems-and-flags (stem flags)
324     (if (gethash stem stems)
325       (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
326       (setf (gethash stem stems) t))
327     (let ((set-difference (set-difference flags *expected-stem-flags*)))
328       (when set-difference
329         (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
330                set-difference)))))
331 \f
332 ;;;; tools to compile SBCL sources to create the cross-compiler
333
334 ;;; Execute function FN in an environment appropriate for compiling the
335 ;;; cross-compiler's source code in the cross-compilation host.
336 (defun in-host-compilation-mode (fn)
337   (declare (type function fn))
338   (let ((*features* (cons :sb-xc-host *features*))
339         ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
340         ;; base-target-features.lisp-expr:
341         (*shebang-features* (set-difference *shebang-features*
342                                             '(:sb-propagate-float-type
343                                               :sb-propagate-fun-type))))
344     (with-additional-nickname ("SB-XC" "SB!XC")
345       (funcall fn))))
346 (compile 'in-host-compilation-mode)
347
348 ;;; Process a file as source code for the cross-compiler, compiling it
349 ;;; (if necessary) in the appropriate environment, then loading it
350 ;;; into the cross-compilation host Common lisp.
351 (defun host-cload-stem (stem &key ignore-failure-p)
352   (let ((compiled-filename (in-host-compilation-mode
353                             (lambda ()
354                               (compile-stem
355                                stem
356                                :obj-prefix *host-obj-prefix*
357                                :obj-suffix *host-obj-suffix*
358                                :compile-file #'cl:compile-file
359                                :ignore-failure-p ignore-failure-p)))))
360     (load compiled-filename)))
361 (compile 'host-cload-stem)
362
363 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
364 (defun host-load-stem (stem &key ignore-failure-p)
365   (declare (ignore ignore-failure-p)) ; (It's only relevant when
366   ;; compiling.) KLUDGE: It's untidy to have the knowledge of how to
367   ;; construct complete filenames from stems in here as well as in
368   ;; COMPILE-STEM. It should probably be factored out somehow. -- WHN
369   ;; 19990815
370   (load (concatenate 'simple-string *host-obj-prefix* stem *host-obj-suffix*)))
371 (compile 'host-load-stem)
372 \f
373 ;;;; tools to compile SBCL sources to create object files which will
374 ;;;; be used to create the target SBCL .core file
375
376 ;;; Run the cross-compiler on a file in the source directory tree to
377 ;;; produce a corresponding file in the target object directory tree.
378 (defun target-compile-stem (stem &key assem-p ignore-failure-p)
379   (funcall *in-target-compilation-mode-fn*
380            (lambda ()
381              (compile-stem stem
382                            :obj-prefix *target-obj-prefix*
383                            :obj-suffix *target-obj-suffix*
384                            :ignore-failure-p ignore-failure-p
385                            :compile-file (if assem-p
386                                              *target-assemble-file*
387                                              *target-compile-file*)))))
388 (compile 'target-compile-stem)
389
390 ;;; (This function is not used by the build process, but is intended
391 ;;; for interactive use when experimenting with the system. It runs
392 ;;; the cross-compiler on test files with arbitrary filenames, not
393 ;;; necessarily in the source tree, e.g. in "/tmp".)
394 (defun target-compile-file (filename)
395   (funcall *in-target-compilation-mode-fn*
396            (lambda ()
397              (funcall *target-compile-file* filename))))
398 (compile 'target-compile-file)