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