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