Update NEWS; explain Windows system requirements
[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
143 ;;; Some feature combinations simply don't work, and sometimes don't
144 ;;; fail until quite a ways into the build.  Pick off the more obvious
145 ;;; combinations now, and provide a description of what the actual
146 ;;; failure is (not always obvious from when the build fails).
147 (let ((feature-compatability-tests
148        '(("(and sb-thread (not gencgc))"
149           ":SB-THREAD requires :GENCGC")
150          ("(and sb-thread (not (or ppc x86 x86-64)))"
151           ":SB-THREAD not supported on selected architecture")
152          ("(and gencgc cheneygc)"
153           ":GENCGC and :CHENEYGC are incompatible")
154          ("(and cheneygc (not (or alpha hppa mips ppc sparc)))"
155           ":CHENEYGC not supported on selected architecture")
156          ("(and gencgc (not (or sparc ppc x86 x86-64)))"
157           ":GENCGC not supported on selected architecture")
158          ("(not (or gencgc cheneygc))"
159           "One of :GENCGC or :CHENEYGC must be enabled")
160          ("(and win32 (not (and sb-thread
161                                 sb-safepoint sb-thruption sb-wtimer
162                                 sb-dynamic-core)))"
163           ":SB-WIN32 requires :SB-THREAD and related features")
164          ("(and sb-dynamic-core (not (and linkage-table sb-thread)))"
165           ;; Subtle memory corruption follows when sb-dynamic-core is
166           ;; active, and non-threaded allocation routines have not been
167           ;; updated to take the additional indirection into account.
168           ;; Let's avoid this unusual combination.
169           ":SB-DYNAMIC-CORE requires :LINKAGE-TABLE and :SB-THREAD")
170          ("(or (and alpha (or hppa mips ppc sparc x86 x86-64))
171                (and hppa (or mips ppc sparc x86 x86-64))
172                (and mips (or ppc sparc x86 x86-64))
173                (and ppc (or sparc x86 x86-64))
174                (and sparc (or x86 x86-64))
175                (and x86 x86-64))"
176           "More than one architecture selected")))
177       (failed-test-descriptions nil))
178   (dolist (test feature-compatability-tests)
179     (let ((*features* *shebang-features*))
180       (when (read-from-string (concatenate 'string "#+" (first test) "T NIL"))
181         (push (second test) failed-test-descriptions))))
182   (when failed-test-descriptions
183     (error "Feature compatibility check failed, ~S"
184            failed-test-descriptions)))
185 \f
186 ;;;; cold-init-related PACKAGE and SYMBOL tools
187
188 ;;; Once we're done with possibly ANSIfying the COMMON-LISP package,
189 ;;; it's probably a mistake if we change it (beyond changing the
190 ;;; values of special variables such as *** and +, anyway). Set up
191 ;;; machinery to warn us when/if we change it.
192 ;;;
193 ;;; All code depending on this is itself dependent on #!+SB-SHOW.
194 #!+sb-show
195 (progn
196   (load "src/cold/snapshot.lisp")
197   (defvar *cl-snapshot* (take-snapshot "COMMON-LISP")))
198 \f
199 ;;;; master list of source files and their properties
200
201 ;;; flags which can be used to describe properties of source files
202 (defparameter
203   *expected-stem-flags*
204   '(;; meaning: This file is not to be compiled when building the
205     ;; cross-compiler which runs on the host ANSI Lisp. ("not host
206     ;; code", i.e. does not execute on host -- but may still be
207     ;; cross-compiled by the host, so that it executes on the target)
208     :not-host
209     ;; meaning: This file is not to be compiled as part of the target
210     ;; SBCL. ("not target code" -- but still presumably host code,
211     ;; used to support the cross-compilation process)
212     :not-target
213     ;; meaning: The #'COMPILE-STEM argument :TRACE-FILE should be T.
214     ;; When the compiler is SBCL's COMPILE-FILE or something like it,
215     ;; compiling "foo.lisp" will generate "foo.trace" which contains lots
216     ;; of exciting low-level information about representation selection,
217     ;; VOPs used by the compiler, and bits of assembly.
218     :trace-file
219     ;; meaning: This file is to be processed with the SBCL assembler,
220     ;; not COMPILE-FILE. (Note that this doesn't make sense unless
221     ;; :NOT-HOST is also set, since the SBCL assembler doesn't exist
222     ;; while the cross-compiler is being built in the host ANSI Lisp.)
223     :assem
224     ;; meaning: The #'COMPILE-STEM argument called :IGNORE-FAILURE-P
225     ;; should be true. (This is a KLUDGE: I'd like to get rid of it.
226     ;; For now, it exists so that compilation can proceed through the
227     ;; legacy warnings in src/compiler/x86/array.lisp, which I've
228     ;; never figured out but which were apparently acceptable in CMU
229     ;; CL. Eventually, it would be great to just get rid of all
230     ;; warnings and remove support for this flag. -- WHN 19990323)
231     :ignore-failure-p))
232
233 (defparameter *stems-and-flags* (read-from-file "build-order.lisp-expr"))
234
235 (defmacro do-stems-and-flags ((stem flags) &body body)
236   (let ((stem-and-flags (gensym "STEM-AND-FLAGS")))
237     `(dolist (,stem-and-flags *stems-and-flags*)
238        (let ((,stem (first ,stem-and-flags))
239              (,flags (rest ,stem-and-flags)))
240          ,@body))))
241
242 ;;; Given a STEM, remap the path component "/target/" to a suitable
243 ;;; target directory.
244 (defun stem-remap-target (stem)
245   (let ((position (search "/target/" stem)))
246     (if position
247       (concatenate 'string
248                    (subseq stem 0 (1+ position))
249                    #!+x86 "x86"
250                    #!+x86-64 "x86-64"
251                    #!+sparc "sparc"
252                    #!+ppc "ppc"
253                    #!+mips "mips"
254                    #!+alpha "alpha"
255                    #!+hppa "hppa"
256                    (subseq stem (+ position 7)))
257       stem)))
258 (compile 'stem-remap-target)
259
260 ;;; Determine the source path for a stem.
261 (defun stem-source-path (stem)
262   (concatenate 'string "" (stem-remap-target stem) ".lisp"))
263 (compile 'stem-source-path)
264
265 ;;; Determine the object path for a stem/flags/mode combination.
266 (defun stem-object-path (stem flags mode)
267   (multiple-value-bind
268         (obj-prefix obj-suffix)
269       (ecase mode
270         (:host-compile
271          ;; On some xc hosts, it's impossible to LOAD a fasl file unless it
272          ;; has the same extension that the host uses for COMPILE-FILE
273          ;; output, so we have to be careful to use the xc host's preferred
274          ;; extension.
275          (values *host-obj-prefix*
276                  (concatenate 'string "."
277                               (pathname-type (compile-file-pathname stem)))))
278         (:target-compile (values *target-obj-prefix*
279                                  (if (find :assem flags)
280                                      *target-assem-obj-suffix*
281                                      *target-obj-suffix*))))
282     (concatenate 'string obj-prefix (stem-remap-target stem) obj-suffix)))
283 (compile 'stem-object-path)
284
285 ;;; Check for stupid typos in FLAGS list keywords.
286 (let ((stems (make-hash-table :test 'equal)))
287   (do-stems-and-flags (stem flags)
288     ;; We do duplicate stem comparison based on the object path in
289     ;; order to cover the case of stems with an :assem flag, which
290     ;; have two entries but separate object paths for each.  KLUDGE:
291     ;; We have to bind *target-obj-prefix* here because it's normally
292     ;; set up later in the build process and we don't actually care
293     ;; what it is so long as it doesn't change while we're checking
294     ;; for duplicate stems.
295     (let* ((*target-obj-prefix* "")
296            (object-path (stem-object-path stem flags :target-compile)))
297       (if (gethash object-path stems)
298           (error "duplicate stem ~S in *STEMS-AND-FLAGS*" stem)
299           (setf (gethash object-path stems) t)))
300     ;; FIXME: We should make sure that the :assem flag is only used
301     ;; when paired with :not-host.
302     (let ((set-difference (set-difference flags *expected-stem-flags*)))
303       (when set-difference
304         (error "found unexpected flag(s) in *STEMS-AND-FLAGS*: ~S"
305                set-difference)))))
306 \f
307 ;;;; tools to compile SBCL sources to create the cross-compiler
308
309 ;;; a wrapper for compilation/assembly, used mostly to centralize
310 ;;; the procedure for finding full filenames from "stems"
311 ;;;
312 ;;; Compile the source file whose basic name is STEM, using some
313 ;;; standard-for-the-SBCL-build-process procedures to generate the
314 ;;; full pathnames of source file and object file. Return the pathname
315 ;;; of the object file for STEM.
316 ;;;
317 ;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS.  MODE is one of
318 ;;; :HOST-COMPILE and :TARGET-COMPILE.
319 (defun compile-stem (stem flags mode)
320
321   (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
322          ;; Lisp Way, although it works just fine for common UNIX environments.
323          ;; Should it come to pass that the system is ported to environments
324          ;; where version numbers and so forth become an issue, it might become
325          ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
326          ;; machinery instead of just using strings. In the absence of such a
327          ;; port, it might or might be a good idea to do the rewrite.
328          ;; -- WHN 19990815
329          (src (stem-source-path stem))
330          (obj (stem-object-path stem flags mode))
331          (tmp-obj (concatenate 'string obj "-tmp"))
332
333          (compile-file (ecase mode
334                          (:host-compile #'compile-file)
335                          (:target-compile (if (find :assem flags)
336                                               *target-assemble-file*
337                                               *target-compile-file*))))
338          (trace-file (find :trace-file flags))
339          (ignore-failure-p (find :ignore-failure-p flags)))
340     (declare (type function compile-file))
341
342     (ensure-directories-exist obj :verbose t)
343
344     ;; We're about to set about building a new object file. First, we
345     ;; delete any preexisting object file in order to avoid confusing
346     ;; ourselves later should we happen to bail out of compilation
347     ;; with an error.
348     (when (probe-file obj)
349       (delete-file obj))
350
351     ;; Original comment:
352     ;;
353     ;;   Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
354     ;;   mangles relative pathnames passed as :OUTPUT-FILE arguments,
355     ;;   but works OK with absolute pathnames.
356     ;;
357     ;; following discussion on cmucl-imp 2002-07
358     ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
359     ;; absolute pathnames all the time; it is no longer clear that the
360     ;; original behaviour in CLISP was wrong or that the current
361     ;; behaviour is right; and in any case absolutifying the pathname
362     ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
363     (setf tmp-obj
364           ;; (Note that this idiom is taken from the ANSI
365           ;; documentation for TRUENAME.)
366           (with-open-file (stream tmp-obj
367                                   :direction :output
368                                   ;; Compilation would overwrite the
369                                   ;; temporary object anyway and overly
370                                   ;; strict implementations default
371                                   ;; to :ERROR.
372                                   :if-exists :supersede)
373             (close stream)
374             (truename stream)))
375     ;; and some compilers (e.g. OpenMCL) will complain if they're
376     ;; asked to write over a file that exists already (and isn't
377     ;; recognizeably a fasl file), so
378     (when (probe-file tmp-obj)
379       (delete-file tmp-obj))
380
381     ;; Try to use the compiler to generate a new temporary object file.
382     (flet ((report-recompile-restart (stream)
383              (format stream "Recompile file ~S" src))
384            (report-continue-restart (stream)
385              (format stream "Continue, using possibly bogus file ~S" obj)))
386       (tagbody
387        retry-compile-file
388          (multiple-value-bind (output-truename warnings-p failure-p)
389             (if trace-file
390                 (funcall compile-file src :output-file tmp-obj
391                          :trace-file t :allow-other-keys t)
392                 (funcall compile-file src :output-file tmp-obj))
393            (declare (ignore warnings-p))
394            (cond ((not output-truename)
395                   (error "couldn't compile ~S" src))
396                  (failure-p
397                   (if ignore-failure-p
398                       (warn "ignoring FAILURE-P return value from compilation of ~S"
399                             src)
400                       (unwind-protect
401                            (restart-case
402                                (error "FAILURE-P was set when creating ~S."
403                                       obj)
404                              (recompile ()
405                                :report report-recompile-restart
406                                (go retry-compile-file))
407                              (continue ()
408                                :report report-continue-restart
409                                (setf failure-p nil)))
410                         ;; Don't leave failed object files lying around.
411                         (when (and failure-p (probe-file tmp-obj))
412                           (delete-file tmp-obj)
413                           (format t "~&deleted ~S~%" tmp-obj)))))
414                  ;; Otherwise: success, just fall through.
415                  (t nil)))))
416
417     ;; If we get to here, compilation succeeded, so it's OK to rename
418     ;; the temporary output file to the permanent object file.
419     (rename-file-a-la-unix tmp-obj obj)
420
421     ;; nice friendly traditional return value
422     (pathname obj)))
423 (compile 'compile-stem)
424
425 ;;; Execute function FN in an environment appropriate for compiling the
426 ;;; cross-compiler's source code in the cross-compilation host.
427 (defun in-host-compilation-mode (fn)
428   (declare (type function fn))
429   (let ((*features* (cons :sb-xc-host *features*))
430         ;; the CROSS-FLOAT-INFINITY-KLUDGE, as documented in
431         ;; base-target-features.lisp-expr:
432         (*shebang-features* (set-difference *shebang-features*
433                                             '(:sb-propagate-float-type
434                                               :sb-propagate-fun-type))))
435     (with-additional-nickname ("SB-XC" "SB!XC")
436       (funcall fn))))
437 (compile 'in-host-compilation-mode)
438
439 ;;; Process a file as source code for the cross-compiler, compiling it
440 ;;; (if necessary) in the appropriate environment, then loading it
441 ;;; into the cross-compilation host Common lisp.
442 (defun host-cload-stem (stem flags)
443   (let ((compiled-filename (in-host-compilation-mode
444                             (lambda ()
445                               (compile-stem stem flags :host-compile)))))
446     (load compiled-filename)))
447 (compile 'host-cload-stem)
448
449 ;;; like HOST-CLOAD-STEM, except that we don't bother to compile
450 (defun host-load-stem (stem flags)
451   (load (stem-object-path stem flags :host-compile)))
452 (compile 'host-load-stem)
453 \f
454 ;;;; tools to compile SBCL sources to create object files which will
455 ;;;; be used to create the target SBCL .core file
456
457 ;;; Run the cross-compiler on a file in the source directory tree to
458 ;;; produce a corresponding file in the target object directory tree.
459 (defun target-compile-stem (stem flags)
460   (funcall *in-target-compilation-mode-fn*
461            (lambda ()
462              (compile-stem stem flags :target-compile))))
463 (compile 'target-compile-stem)
464
465 ;;; (This function is not used by the build process, but is intended
466 ;;; for interactive use when experimenting with the system. It runs
467 ;;; the cross-compiler on test files with arbitrary filenames, not
468 ;;; necessarily in the source tree, e.g. in "/tmp".)
469 (defun target-compile-file (filename)
470   (funcall *in-target-compilation-mode-fn*
471            (lambda ()
472              (funcall *target-compile-file* filename))))
473 (compile 'target-compile-file)