Function COMPILED-FILE-P

Author

Sam Steingold

Related

ANSI Common Lisp standard function compile-file.

Abstract

A facility to determine whether a file is a valid compiled file for the specific implementation.

Rationale

Build tools, like defsystem or asdf, have to determine whether a file needs to be recompiled.

Obviously, when the compiled file is older than the source file, recompilation is in order.

Alas, there are other situations when this might be necessary, e.g., when the implementation changes the compiled file format or when two implementations use the same name for their compiled files (.fasl is used by both SBCL and ACL).

Current Practice

Implementation-dependent.

Cost of adoption

Probably tiny: an implementation must be able to check for compiled file validity, so all it takes is to export the necessary functionality, e.g.:

#+clisp
(defun compiled-file-p (file-name)
  (with-open-file (in file-name :direction :input :if-does-not-exist nil)
    (and in (char= #\( (peek-char nil in))
         (let ((form (ignore-errors (read in nil nil))))
           (and (consp form)
                (eq (car form) 'SYSTEM::VERSION)
                (null (nth-value 1 (ignore-errors (eval form)))))))))

Cost of non-adoption

Users will suffer random errors when trying to load invalid binary files.

Specification

Function

(compiled-file-p file-name) ==> valid-p

Returns

true
if the file appears to be a valid compiled file (i.e., exists, is readable, and the contents appears to be valid for this implementation),
false
otherwise.

Implementations are required to inspect the contents (e.g., checking just the pathname type is not sufficient). Although the completeness of the inspection is not required, this function should be able to detect, e.g., file format changes between versions.

Exceptional situations

Examples

(compiled-file-p "foo.lisp") ==> NIL
(compiled-file-p (compile-file "foo.lisp")) ==> T

Reference Implementation

See above.

History

This used to be CLRFI-2 (in 2004).

Notes

The trivial implementation:

(defun compiled-file-p (file-name)
  (not (nth-value 1 (ignore-errors (load file-name)))))

is wrong because,

  1. load may fail even though the file is valid: even when foo.lisp contains calls to error,
    (compiled-file-p (compile-file "foo.lisp"))
    
    should still return T.
  2. this is not side-effect-free, i.e., this may define new functions and macros (or, worse yet, redefine some existing functions and macros or execute some malicious code).