+;;;; tools to compile SBCL sources to create the cross-compiler
+
+;;; a wrapper for compilation/assembly, used mostly to centralize
+;;; the procedure for finding full filenames from "stems"
+;;;
+;;; Compile the source file whose basic name is STEM, using some
+;;; standard-for-the-SBCL-build-process procedures to generate the
+;;; full pathnames of source file and object file. Return the pathname
+;;; of the object file for STEM.
+;;;
+;;; STEM and FLAGS are as per DO-STEMS-AND-FLAGS. MODE is one of
+;;; :HOST-COMPILE and :TARGET-COMPILE.
+(defun compile-stem (stem flags mode)
+
+ (let* (;; KLUDGE: Note that this CONCATENATE 'STRING stuff is not The Common
+ ;; Lisp Way, although it works just fine for common UNIX environments.
+ ;; Should it come to pass that the system is ported to environments
+ ;; where version numbers and so forth become an issue, it might become
+ ;; urgent to rewrite this using the fancy Common Lisp PATHNAME
+ ;; machinery instead of just using strings. In the absence of such a
+ ;; port, it might or might be a good idea to do the rewrite.
+ ;; -- WHN 19990815
+ (src (stem-source-path stem))
+ (obj (stem-object-path stem flags mode))
+ (tmp-obj (concatenate 'string obj "-tmp"))
+
+ (compile-file (ecase mode
+ (:host-compile #'compile-file)
+ (:target-compile (if (find :assem flags)
+ *target-assemble-file*
+ *target-compile-file*))))
+ (trace-file (find :trace-file flags))
+ (ignore-failure-p (find :ignore-failure-p flags)))
+ (declare (type function compile-file))
+
+ (ensure-directories-exist obj :verbose t)
+
+ ;; We're about to set about building a new object file. First, we
+ ;; delete any preexisting object file in order to avoid confusing
+ ;; ourselves later should we happen to bail out of compilation
+ ;; with an error.
+ (when (probe-file obj)
+ (delete-file obj))
+
+ ;; Original comment:
+ ;;
+ ;; Work around a bug in CLISP 1999-01-08 #'COMPILE-FILE: CLISP
+ ;; mangles relative pathnames passed as :OUTPUT-FILE arguments,
+ ;; but works OK with absolute pathnames.
+ ;;
+ ;; following discussion on cmucl-imp 2002-07
+ ;; "COMPILE-FILE-PATHNAME", it would seem safer to deal with
+ ;; absolute pathnames all the time; it is no longer clear that the
+ ;; original behaviour in CLISP was wrong or that the current
+ ;; behaviour is right; and in any case absolutifying the pathname
+ ;; insulates us against changes of behaviour. -- CSR, 2002-08-09
+ (setf tmp-obj
+ ;; (Note that this idiom is taken from the ANSI
+ ;; documentation for TRUENAME.)
+ (with-open-file (stream tmp-obj
+ :direction :output
+ ;; Compilation would overwrite the
+ ;; temporary object anyway and overly
+ ;; strict implementations default
+ ;; to :ERROR.
+ :if-exists :supersede)
+ (close stream)
+ (truename stream)))
+ ;; and some compilers (e.g. OpenMCL) will complain if they're
+ ;; asked to write over a file that exists already (and isn't
+ ;; recognizeably a fasl file), so
+ (when (probe-file tmp-obj)
+ (delete-file tmp-obj))
+
+ ;; Try to use the compiler to generate a new temporary object file.
+ (flet ((report-recompile-restart (stream)
+ (format stream "Recompile file ~S" src))
+ (report-continue-restart (stream)
+ (format stream "Continue, using possibly bogus file ~S" obj)))
+ (tagbody
+ retry-compile-file
+ (multiple-value-bind (output-truename warnings-p failure-p)
+ (if trace-file
+ (funcall compile-file src :output-file tmp-obj
+ :trace-file t :allow-other-keys t)
+ (funcall compile-file src :output-file tmp-obj))
+ (declare (ignore warnings-p))
+ (cond ((not output-truename)
+ (error "couldn't compile ~S" src))
+ (failure-p
+ (if ignore-failure-p
+ (warn "ignoring FAILURE-P return value from compilation of ~S"
+ src)
+ (unwind-protect
+ (restart-case
+ (error "FAILURE-P was set when creating ~S."
+ obj)
+ (recompile ()
+ :report report-recompile-restart
+ (go retry-compile-file))
+ (continue ()
+ :report report-continue-restart
+ (setf failure-p nil)))
+ ;; Don't leave failed object files lying around.
+ (when (and failure-p (probe-file tmp-obj))
+ (delete-file tmp-obj)
+ (format t "~&deleted ~S~%" tmp-obj)))))
+ ;; Otherwise: success, just fall through.
+ (t nil)))))
+
+ ;; If we get to here, compilation succeeded, so it's OK to rename
+ ;; the temporary output file to the permanent object file.
+ (rename-file-a-la-unix tmp-obj obj)
+
+ ;; nice friendly traditional return value
+ (pathname obj)))
+(compile 'compile-stem)