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