1 ;;; async-bytecomp.el --- Async functions to compile elisp files async
3 ;; Copyright (C) 2014 John Wiegley
4 ;; Copyright (C) 2014 Thierry Volpiatto
6 ;; Authors: John Wiegley <jwiegley@gmail.com>
7 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
9 ;; Keywords: dired async byte-compile
10 ;; X-URL: https://github.com/jwiegley/dired-async
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ;; General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This package provide the `async-byte-recompile-directory' function
30 ;; which allows, as the name says to recompile a directory outside of
31 ;; your running emacs.
32 ;; The benefit is your files will be compiled in a clean environment without
33 ;; the old *.el files loaded.
34 ;; Among other things, this fix a bug in package.el which recompile
35 ;; the new files in the current environment with the old files loaded, creating
36 ;; errors in most packages after upgrades.
38 ;; NB: This package is advicing the function `package--compile'.
45 (defvar async-byte-compile-log-file "~/.emacs.d/async-bytecomp.log")
47 (defun async-byte-recompile-directory (directory &optional arg force quiet)
48 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
51 when (file-exists-p f) do (delete-file f))
52 ;; Ensure async is reloaded when async.elc is deleted.
53 ;; This happen when recompiling its own directory.
56 `(lambda (&optional ignore)
57 (if (file-exists-p async-byte-compile-log-file)
58 (let ((buf (get-buffer-create byte-compile-log-buffer))
60 (with-current-buffer buf
61 (goto-char (point-max))
62 (let ((inhibit-read-only t))
63 (insert-file-contents async-byte-compile-log-file)
66 (delete-file async-byte-compile-log-file)
69 (goto-char (point-min))
70 (while (re-search-forward "^.*:Error:" nil t)
73 (message "Failed to compile %d files in directory `%s'" n ,directory)
74 (message "Directory `%s' compiled asynchronously with warnings" ,directory)))))
76 (message "Directory `%s' compiled asynchronously with success" ,directory))))))
80 ,(async-inject-variables "\\`load-path\\'")
81 (let ((default-directory (file-name-as-directory ,directory))
83 (add-to-list 'load-path default-directory)
84 (byte-recompile-directory ,directory ,arg ,force)
85 (when (get-buffer byte-compile-log-buffer)
86 (setq error-data (with-current-buffer byte-compile-log-buffer
87 (buffer-substring-no-properties (point-min) (point-max))))
88 (unless (string= error-data "")
89 (with-temp-file ,async-byte-compile-log-file
91 (insert error-data))))))
93 (message "Started compiling asynchronously directory %s..." directory)))
95 (defadvice package--compile (around byte-compile-async activate)
96 ;; FIXME this seems redundant and unneeded, the only thing it
97 ;; does is loading the autoload file to update load-path but
98 ;; async-byte-recompile-directory is already doing this.
99 ;; for the rest (i.e installing info) it is done anyway after
100 ;; compilation in package-activate (force arg).
101 (package-activate-1 pkg-desc)
102 (async-byte-recompile-directory (package-desc-dir pkg-desc) 0 t t))
104 (provide 'async-bytecomp)
106 ;;; async-bytecomp.el ends here