compile-file.lisp 28.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
;;; compile-file.lisp
;;;
;;; Copyright (C) 2004-2006 Peter Graves
;;; $Id$
;;;
;;; This program is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License
;;; as published by the Free Software Foundation; either version 2
;;; of the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
;;;
;;; As a special exception, the copyright holders of this library give you
;;; permission to link this library with independent modules to produce an
;;; executable, regardless of the license terms of these independent
;;; modules, and to copy and distribute the resulting executable under
;;; terms of your choice, provided that you also meet, for each linked
;;; independent module, the terms and conditions of the license of that
;;; module.  An independent module is a module which is not derived from
;;; or based on this library.  If you modify this library, you may extend
;;; this exception to your version of the library, but you are not
;;; obligated to do so.  If you do not wish to do so, delete this
;;; exception statement from your version.

(in-package #:system)

34 35
(require "JVM")
;; (require "COMPILER-ERROR") already made accessible through JVM
36 37 38 39 40 41 42

(defvar *fbound-names*)

(defvar *class-number*)

(defvar *output-file-pathname*)

43 44 45 46 47 48 49 50 51 52 53
(declaim (ftype (function (t) t) compute-classfile-name))
(defun compute-classfile-name (n &optional (output-file-pathname
                                            *output-file-pathname*))
  "Computes the name of the class file associated with number `n'."
  (let ((name
         (%format nil "~A-~D"
                  (substitute #\_ #\.
                              (pathname-name output-file-pathname)) n)))
    (namestring (merge-pathnames (make-pathname :name name :type "cls")
                                 output-file-pathname))))

54 55
(declaim (ftype (function () t) next-classfile-name))
(defun next-classfile-name ()
56
  (compute-classfile-name (incf *class-number*)))
57 58 59 60 61 62 63 64 65 66 67 68 69 70

(defmacro report-error (&rest forms)
  `(handler-case (progn ,@forms)
     (compiler-unsupported-feature-error (condition)
       (fresh-line)
       (%format t "; UNSUPPORTED-FEATURE: ~A~%" condition)
       (values nil condition))))

;; Dummy function. Should never be called.
(defun dummy (&rest ignored)
  (declare (ignore ignored))
  (assert nil))

(declaim (ftype (function (t) t) verify-load))
71 72 73 74 75
;(defun verify-load (classfile)
;  (and classfile
;       (let ((*load-truename* *output-file-pathname*))
;         (report-error
;          (load-compiled-function classfile)))))
76
(defun verify-load (classfile)
77 78
  (declare (ignore classfile))
  t)
79

80 81
(declaim (ftype (function (t) t) process-defconstant))
(defun process-defconstant (form)
82 83 84 85 86 87 88 89
  ;; "If a DEFCONSTANT form appears as a top level form, the compiler
  ;; must recognize that [the] name names a constant variable. An
  ;; implementation may choose to evaluate the value-form at compile
  ;; time, load time, or both. Therefore, users must ensure that the
  ;; initial-value can be evaluated at compile time (regardless of
  ;; whether or not references to name appear in the file) and that
  ;; it always evaluates to the same value."
  (eval form)
90
  (output-form form))
91 92 93 94 95 96 97 98 99 100 101 102 103 104

(declaim (ftype (function (t) t) note-toplevel-form))
(defun note-toplevel-form (form)
  (when *compile-print*
    (fresh-line)
    (princ "; ")
    (let ((*print-length* 2)
          (*print-level* 2)
          (*print-pretty* nil))
      (prin1 form))
    (terpri)))

(declaim (ftype (function (t stream t) t) process-toplevel-form))
(defun process-toplevel-form (form stream compile-time-too)
105 106 107 108 109 110 111 112 113 114 115
  (if (atom form)
      (when compile-time-too
        (eval form))
    (progn
      (let ((operator (%car form)))
        (case operator
          (MACROLET
           (process-toplevel-macrolet form stream compile-time-too)
           (return-from process-toplevel-form))
          ((IN-PACKAGE DEFPACKAGE)
           (note-toplevel-form form)
116
           (setf form (precompiler:precompile-form form nil *compile-file-environment*))
117 118 119
           (eval form)
           ;; Force package prefix to be used when dumping form.
           (let ((*package* +keyword-package+))
120
             (output-form form))
121 122 123 124 125 126 127 128 129 130 131 132 133 134
           (return-from process-toplevel-form))
          ((DEFVAR DEFPARAMETER)
           (note-toplevel-form form)
           (if compile-time-too
               (eval form)
               ;; "If a DEFVAR or DEFPARAMETER form appears as a top level form,
               ;; the compiler must recognize that the name has been proclaimed
               ;; special. However, it must neither evaluate the initial-value
               ;; form nor assign the dynamic variable named NAME at compile
               ;; time."
               (let ((name (second form)))
                 (%defvar name))))
          (DEFCONSTANT
           (note-toplevel-form form)
135
           (process-defconstant form)
136 137 138 139 140 141
           (return-from process-toplevel-form))
          (DEFUN
           (note-toplevel-form form)
           (let* ((name (second form))
                  (block-name (fdefinition-block-name name))
                  (lambda-list (third form))
142 143 144 145 146 147
                  (body (nthcdr 3 form)))
             (jvm::with-saved-compiler-policy
               (multiple-value-bind (body decls doc)
                   (parse-body body)
                 (let* ((expr `(lambda ,lambda-list
                                 ,@decls (block ,block-name ,@body)))
148 149 150 151 152 153 154 155
                        (classfile (next-classfile-name))
                        (result (with-open-file
				    (f classfile
				       :direction :output
				       :element-type '(unsigned-byte 8)
				       :if-exists :supersede)
				  (report-error
				   (jvm:compile-defun name expr nil
156
						      classfile f nil))))
157
                        (compiled-function (verify-load classfile)))
158
		   (declare (ignore result))
159 160
                   (cond
                     (compiled-function
161 162
                      (setf form
                            `(fset ',name
163
                                   (proxy-preloaded-function ',name ,(file-namestring classfile))
164 165
                                   ,*source-position*
                                   ',lambda-list
166 167 168 169 170 171 172
                                   ,doc))
                      (when compile-time-too
                        (fset name compiled-function)))
                     (t
                      ;; FIXME Should be a warning or error of some sort...
                      (format *error-output*
                              "; Unable to compile function ~A~%" name)
173 174 175
                      (let ((precompiled-function
                             (precompiler:precompile-form expr nil
                                              *compile-file-environment*)))
176 177 178 179 180 181 182 183 184
                        (setf form
                              `(fset ',name
                                     ,precompiled-function
                                     ,*source-position*
                                     ',lambda-list
                                     ,doc)))
                      (when compile-time-too
                        (eval form)))))
                 (when (and (symbolp name) (eq (get name '%inline) 'INLINE))
185
                 ;; FIXME Need to support SETF functions too!
186 187 188
                   (setf (inline-expansion name)
                         (jvm::generate-inline-expansion block-name
                                                         lambda-list body))
189 190
                   (output-form `(setf (inline-expansion ',name)
                                       ',(inline-expansion name))))))
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
             (push name jvm::*functions-defined-in-current-file*)
             (note-name-defined name)
             ;; If NAME is not fbound, provide a dummy definition so that
             ;; getSymbolFunctionOrDie() will succeed when we try to verify that
             ;; functions defined later in the same file can be loaded correctly.
             (unless (fboundp name)
               (setf (fdefinition name) #'dummy)
               (push name *fbound-names*))))
          ((DEFGENERIC DEFMETHOD)
           (note-toplevel-form form)
           (note-name-defined (second form))
           (let ((*compile-print* nil))
             (process-toplevel-form (macroexpand-1 form *compile-file-environment*)
                                    stream compile-time-too))
             (return-from process-toplevel-form))
          (DEFMACRO
           (note-toplevel-form form)
           (let ((name (second form)))
             (eval form)
             (let* ((expr (function-lambda-expression (macro-function name)))
211 212 213 214 215 216 217
                    (classfile (next-classfile-name)))
	       (with-open-file
		   (f classfile
		      :direction :output
		      :element-type '(unsigned-byte 8)
		      :if-exists :supersede)
		 (ignore-errors
218
		   (jvm:compile-defun nil expr nil classfile f nil)))
219 220 221 222 223 224 225 226
               (if (null (verify-load classfile))
                   ;; FIXME error or warning
                   (format *error-output* "; Unable to compile macro ~A~%" name)
                 (progn
                   (setf form
                         (if (special-operator-p name)
                             `(put ',name 'macroexpand-macro
                                   (make-macro ',name
227 228
                                               (proxy-preloaded-function
                                                '(macro-function ,name)
229 230 231
                                                ,(file-namestring classfile))))
                             `(fset ',name
                                    (make-macro ',name
232 233
                                                (proxy-preloaded-function
                                                 '(macro-function ,name)
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                                                 ,(file-namestring classfile)))
                                    ,*source-position*
                                    ',(third form)))))))))
          (DEFTYPE
           (note-toplevel-form form)
           (eval form))
          (EVAL-WHEN
           (multiple-value-bind (ct lt e)
               (parse-eval-when-situations (cadr form))
             (let ((new-compile-time-too (or ct (and compile-time-too e)))
                   (body (cddr form)))
               (if lt
                   (process-toplevel-progn body stream new-compile-time-too)
                 (when new-compile-time-too
                   (eval `(progn ,@body)))))
           (return-from process-toplevel-form)))
          (LOCALLY
           ;; FIXME Need to handle special declarations too!
252
           (jvm::with-saved-compiler-policy
253 254 255
             (multiple-value-bind (forms decls)
                 (parse-body (cdr form) nil)
               (process-optimization-declarations decls)
256
               (let* ((jvm::*visible-variables* jvm::*visible-variables*)
257 258
                      (specials (jvm::process-declarations-for-vars (cdr form)
                                                                    nil nil)))
259
                 (dolist (special specials)
260
                   (push special jvm::*visible-variables*))
261
                 (process-toplevel-progn forms stream compile-time-too))
262 263 264 265 266 267 268 269
               (return-from process-toplevel-form))))
          (PROGN
           (process-toplevel-progn (cdr form) stream compile-time-too)
           (return-from process-toplevel-form))
          (DECLARE
           (compiler-style-warn "Misplaced declaration: ~S" form))
          (t
           (when (and (symbolp operator)
270
                      (macro-function operator *compile-file-environment*))
271 272 273 274 275 276 277 278
             (note-toplevel-form form)
             ;; Note that we want MACROEXPAND-1 and not MACROEXPAND here, in
             ;; case the form being expanded expands into something that needs
             ;; special handling by PROCESS-TOPLEVEL-FORM (e.g. DEFMACRO).
             (let ((*compile-print* nil))
               (process-toplevel-form (macroexpand-1 form *compile-file-environment*)
                                      stream compile-time-too))
             (return-from process-toplevel-form))
279

280
           (cond ((eq operator 'QUOTE)
281 282
;;;                      (setf form (precompiler:precompile-form form nil
;;;                                                  *compile-file-environment*))
283 284 285 286
                  (when compile-time-too
                    (eval form))
                  (return-from process-toplevel-form))
                 ((eq operator 'PUT)
287
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
288
                 ((eq operator 'COMPILER-DEFSTRUCT)
289
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
290
                 ((eq operator 'PROCLAIM)
291
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
292 293 294 295
                 ((and (memq operator '(EXPORT REQUIRE PROVIDE SHADOW))
                       (or (keywordp (second form))
                           (and (listp (second form))
                                (eq (first (second form)) 'QUOTE))))
296
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
297
                 ((eq operator 'IMPORT)
298
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*))
299 300
                  ;; Make sure package prefix is printed when symbols are imported.
                  (let ((*package* +keyword-package+))
301
                    (output-form form))
302 303 304 305 306 307 308 309
                  (when compile-time-too
                    (eval form))
                  (return-from process-toplevel-form))
                 ((and (eq operator '%SET-FDEFINITION)
                       (eq (car (second form)) 'QUOTE)
                       (consp (third form))
                       (eq (%car (third form)) 'FUNCTION)
                       (symbolp (cadr (third form))))
310
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
311 312 313 314 315 316
;;;                     ((memq operator '(LET LET*))
;;;                      (let ((body (cddr form)))
;;;                        (if (dolist (subform body nil)
;;;                              (when (and (consp subform) (eq (%car subform) 'DEFUN))
;;;                                (return t)))
;;;                            (setf form (convert-toplevel-form form))
317
;;;                            (setf form (precompiler:precompile-form form nil)))))
318 319 320 321 322
                 ((eq operator 'mop::ensure-method)
                  (setf form (convert-ensure-method form)))
                 ((and (symbolp operator)
                       (not (special-operator-p operator))
                       (null (cdr form)))
323
                  (setf form (precompiler:precompile-form form nil *compile-file-environment*)))
324
                 (t
325
;;;                      (setf form (precompiler:precompile-form form nil))
326
                  (note-toplevel-form form)
327
                  (setf form (convert-toplevel-form form nil)))))))))
328
  (when (consp form)
329
    (output-form form))
330 331 332 333 334 335 336
  ;; Make sure the compiled-function loader knows where
  ;; to load the compiled functions. Note that this trickery
  ;; was already used in verify-load before I used it,
  ;; however, binding *load-truename* isn't fully compliant, I think.
  (let ((*load-truename* *output-file-pathname*))
    (when compile-time-too
      (eval form))))
337 338 339 340 341

(declaim (ftype (function (t) t) convert-ensure-method))
(defun convert-ensure-method (form)
  (c-e-m-1 form :function)
  (c-e-m-1 form :fast-function)
342
  (precompiler:precompile-form form nil *compile-file-environment*))
343 344 345 346 347 348 349 350

(declaim (ftype (function (t t) t) c-e-m-1))
(defun c-e-m-1 (form key)
  (let* ((tail (cddr form))
         (function-form (getf tail key)))
    (when (and function-form (consp function-form)
               (eq (%car function-form) 'FUNCTION))
      (let ((lambda-expression (cadr function-form)))
351
        (jvm::with-saved-compiler-policy
352 353 354 355 356 357 358 359
          (let* ((classfile (next-classfile-name))
                 (result
		  (with-open-file
		      (f classfile
			 :direction :output
			 :element-type '(unsigned-byte 8)
			 :if-exists :supersede)
		    (report-error
360
		     (jvm:compile-defun nil lambda-expression nil classfile f nil))))
361
                 (compiled-function (verify-load classfile)))
362
	    (declare (ignore result))
363 364 365 366 367 368 369
            (cond (compiled-function
                   (setf (getf tail key)
                         `(load-compiled-function ,(file-namestring classfile))))
                  (t
                   ;; FIXME This should be a warning or error of some sort...
                   (format *error-output* "; Unable to compile method~%")))))))))

370 371 372 373 374
(declaim (ftype (function (t) t) simple-toplevel-form-p))
(defun simple-toplevel-form-p (form)
  "Returns NIL if the form is too complex to become an
interpreted toplevel form, non-NIL if it is 'simple enough'."
  (and (consp form)
375 376 377 378 379 380
             (every #'(lambda (arg)
                        (or (and (atom arg)
                                 (not (and (symbolp arg)
                                           (symbol-macro-p arg))))
                            (and (consp arg)
                                 (eq 'QUOTE (car arg)))))
381 382
              (cdr form))))

383 384
(declaim (ftype (function (t t) t) convert-toplevel-form))
(defun convert-toplevel-form (form declare-inline)
385 386 387 388
  (when (or (simple-toplevel-form-p form)
            (and (eq (car form) 'SETQ)
                 ;; for SETQ, look at the evaluated part
                 (simple-toplevel-form-p (third form))))
389 390 391 392 393 394 395 396
    ;; single form with simple or constant arguments
    ;; Without this exception, toplevel function calls
    ;; will be compiled into lambdas which get compiled to
    ;; compiled-functions. Those need to be loaded.
    ;; Conclusion: Top level interpreting the function call
    ;;  and its arguments may be (and should be) more efficient.
    (return-from convert-toplevel-form
      (precompiler:precompile-form form nil *compile-file-environment*)))
397
  (let* ((expr `(lambda () ,form))
398 399 400 401 402 403 404
         (classfile (next-classfile-name))
         (result
	  (with-open-file
	      (f classfile
		 :direction :output
		 :element-type '(unsigned-byte 8)
		 :if-exists :supersede)
405 406
	    (report-error (jvm:compile-defun nil expr nil classfile
                                             f declare-inline))))
407
         (compiled-function (verify-load classfile)))
408
    (declare (ignore result))
409 410 411
    (setf form
          (if compiled-function
              `(funcall (load-compiled-function ,(file-namestring classfile)))
412
              (precompiler:precompile-form form nil *compile-file-environment*)))))
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447


(defun process-toplevel-macrolet (form stream compile-time-too)
  (let ((*compile-file-environment* (make-environment *compile-file-environment*)))
    (dolist (definition (cadr form))
      (environment-add-macro-definition *compile-file-environment*
                                        (car definition)
                                        (make-macro (car definition)
                                                    (make-expander-for-macrolet definition))))
    (dolist (body-form (cddr form))
      (process-toplevel-form body-form stream compile-time-too))))

(declaim (ftype (function (t stream t) t) process-toplevel-progn))
(defun process-toplevel-progn (forms stream compile-time-too)
  (dolist (form forms)
    (process-toplevel-form form stream compile-time-too)))

;;; Adapted from SBCL.
;;; Parse an EVAL-WHEN situations list, returning three flags,
;;; (VALUES COMPILE-TOPLEVEL LOAD-TOPLEVEL EXECUTE), indicating
;;; the types of situations present in the list.
(defun parse-eval-when-situations (situations)
  (when (or (not (listp situations))
	    (set-difference situations
			    '(:compile-toplevel
			      compile
			      :load-toplevel
			      load
			      :execute
			      eval)))
    (error "Bad EVAL-WHEN situation list: ~S." situations))
  (values (intersection '(:compile-toplevel compile) situations)
	  (intersection '(:load-toplevel load) situations)
	  (intersection '(:execute eval) situations)))

448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

(defvar *binary-fasls* nil)
(defvar *forms-for-output* nil)
(defvar *fasl-stream* nil)

(defun output-form (form)
  (if *binary-fasls*
      (push form *forms-for-output*)
      (progn
        (dump-form form *fasl-stream*)
        (%stream-terpri *fasl-stream*))))

(defun finalize-fasl-output ()
  (when *binary-fasls*
    (let ((*package* (find-package :keyword))
          (*double-colon-package-separators* T))
      (dump-form (convert-toplevel-form (list* 'PROGN
                                               (nreverse *forms-for-output*))
                                        t)
                 *fasl-stream*))
    (%stream-terpri *fasl-stream*)))

470 471 472 473 474 475
(defun compile-file (input-file
                     &key
                     output-file
                     ((:verbose *compile-verbose*) *compile-verbose*)
                     ((:print *compile-print*) *compile-print*)
                     external-format)
476
  (declare (ignore external-format))    ; FIXME
477 478 479 480 481 482 483 484 485 486 487 488
  (unless (or (and (probe-file input-file) (not (file-directory-p input-file)))
              (pathname-type input-file))
    (let ((pathname (merge-pathnames (make-pathname :type "lisp") input-file)))
      (when (probe-file pathname)
        (setf input-file pathname))))
  (setf output-file (if output-file
                        (merge-pathnames output-file *default-pathname-defaults*)
                        (compile-file-pathname input-file)))
  (let* ((*output-file-pathname* output-file)
         (type (pathname-type output-file))
         (temp-file (merge-pathnames (make-pathname :type (concatenate 'string type "-tmp"))
                                     output-file))
489 490
         (temp-file2 (merge-pathnames (make-pathname :type (concatenate 'string type "-tmp2"))
                                     output-file))
491 492
         (warnings-p nil)
         (failure-p nil))
493 494 495 496 497 498 499 500 501 502 503
    (with-open-file (in input-file :direction :input)
      (let* ((*compile-file-pathname* (pathname in))
             (*compile-file-truename* (truename in))
             (*source* *compile-file-truename*)
             (*class-number* 0)
             (namestring (namestring *compile-file-truename*))
             (start (get-internal-real-time))
             elapsed)
        (when *compile-verbose*
          (format t "; Compiling ~A ...~%" namestring))
        (with-compilation-unit ()
504
          (with-open-file (out temp-file
505 506
                               :direction :output :if-exists :supersede
                               :external-format *fasl-external-format*)
507
            (let ((*readtable* *readtable*)
508 509
                  (*read-default-float-format* *read-default-float-format*)
                  (*read-base* *read-base*)
510 511
                  (*package* *package*)
                  (jvm::*functions-defined-in-current-file* '())
512
                  (*fbound-names* '())
513 514 515
                  (*fasl-anonymous-package* (%make-package))
                  (*fasl-stream* out)
                  *forms-for-output*)
516
              (jvm::with-saved-compiler-policy
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
                  (jvm::with-file-compilation
                    (handler-bind ((style-warning #'(lambda (c)
                                                      (setf warnings-p t)
                                                      ;; let outer handlers
                                                      ;; do their thing
                                                      (signal c)
                                                      ;; prevent the next
                                                      ;; handler from running:
                                                      ;; we're a WARNING subclass
                                                      (continue)))
                                   ((or warning
                                        compiler-error) #'(lambda (c)
                                        (declare (ignore c))
                                        (setf warnings-p t
                                              failure-p t))))
                      (loop
                         (let* ((*source-position* (file-position in))
                                (jvm::*source-line-number* (stream-line-number in))
                                (form (read in nil in))
                                (*compiler-error-context* form))
                           (when (eq form in)
                             (return))
                           (process-toplevel-form form out nil))))
                    (finalize-fasl-output)
                    (dolist (name *fbound-names*)
                      (fmakunbound name)))))))
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
        (with-open-file (in temp-file :direction :input)
          (with-open-file (out temp-file2 :direction :output
                               :if-does-not-exist :create
                               :if-exists :supersede)
            ;; write header
            (write "; -*- Mode: Lisp -*-" :escape nil :stream out)
            (%stream-terpri out)
            (let ((*package* (find-package '#:cl))
                  (count-sym (gensym)))
              (write (list 'init-fasl :version *fasl-version*)
                     :stream out)
              (%stream-terpri out)
              (write (list 'setq '*source* *compile-file-truename*)
                     :stream out)
              (%stream-terpri out)
              (dump-form `(dotimes (,count-sym ,*class-number*)
                            (function-preload
560 561
                             (%format nil "~A-~D.cls" 
                                      ,(substitute #\_ #\. (pathname-name output-file))
562 563 564 565 566 567 568 569 570
                                      (1+ ,count-sym)))) out)
              (%stream-terpri out))


            ;; copy remaining content
            (loop for line = (read-line in nil :eof)
               while (not (eq line :eof))
               do (write-line line out))))
        (delete-file temp-file)
571
	(remove-zip-cache-entry output-file) ;; Necessary under windows
572
        (rename-file temp-file2 output-file)
573 574

        (when *compile-file-zip*
575
          (let* ((type ;; Don't use ".zip", it'll result in an extension
576
                  ;;  with a dot, which is rejected by NAMESTRING
577 578 579 580 581
                  (%format nil "~A~A" (pathname-type output-file) "-zip"))
                 (zipfile (namestring
                           (merge-pathnames (make-pathname :type type)
                                            output-file)))
                 (pathnames ()))
582
            (dotimes (i *class-number*)
583
              (let* ((pathname (compute-classfile-name (1+ i))))
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
                (when (probe-file pathname)
                  (push pathname pathnames))))
            (setf pathnames (nreverse pathnames))
            (let ((load-file (merge-pathnames (make-pathname :type "_")
                                              output-file)))
              (rename-file output-file load-file)
              (push load-file pathnames))
            (zip zipfile pathnames)
            (dolist (pathname pathnames)
              (let ((truename (probe-file pathname)))
                (when truename
                  (delete-file truename))))
            (rename-file zipfile output-file)))

        (setf elapsed (/ (- (get-internal-real-time) start) 1000.0))
        (when *compile-verbose*
600 601
          (format t "~&; Wrote ~A (~A seconds)~%"
                  (namestring output-file) elapsed))))
602 603
    (values (truename output-file) warnings-p failure-p)))

604 605
(defun compile-file-if-needed (input-file &rest allargs &key force-compile
                               &allow-other-keys)
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  (setf input-file (truename input-file))
  (cond (force-compile
         (remf allargs :force-compile)
         (apply 'compile-file input-file allargs))
        (t
         (let* ((source-write-time (file-write-date input-file))
                (output-file       (or (getf allargs :output-file)
                                       (compile-file-pathname input-file)))
                (target-write-time (and (probe-file output-file)
                                        (file-write-date output-file))))
           (if (or (null target-write-time)
                   (<= target-write-time source-write-time))
               (apply 'compile-file input-file allargs)
               output-file)))))

(provide 'compile-file)