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