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