1

Is there a way to tell if the current *.elc files were not compiled with the current Emacs?

I am trying to detect an error where emacs is updated, but old .elc files are left in the user elpa directory and they are not compatible.

3
  • What do you mean by "the user elpa file"?
    – Drew
    Commented Dec 30, 2020 at 18:00
  • 2
    The file header of a .elc file tells you what Emacs build it was created with. Does that help?
    – Drew
    Commented Dec 30, 2020 at 18:02
  • @Drew I edited the question to indicate I mean in the user elpa directory, .e.g. where packages installed by the user usually go. Commented Dec 30, 2020 at 19:06

1 Answer 1

1

Based on the hint from @Drew about the file header, this seems to work:

(defun elc-consistent-p (elc)
  "Return non-nil if ELC is consistent with current Emacs.
ELC is the path to a .elc file.
That means it was compiled with the same emacs major and minor
version as the current emacs."
  (string= (with-temp-buffer
         (insert-file-contents elc)
         (goto-char (point-min))
         (re-search-forward ";;; in Emacs version \\([0-9]\\{2\\}\\.[0-9]+\\)"
                nil t)
         (match-string 1))
       (format "%s.%s" emacs-major-version emacs-minor-version)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.