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>
10 ;; Keywords: dired async byte-compile
11 ;; X-URL: https://github.com/jwiegley/dired-async
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License as
15 ;; published by the Free Software Foundation; either version 2, or (at
16 ;; your option) any later version.
18 ;; This program is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 ;; General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; This package provide the `async-byte-recompile-directory' function
31 ;; which allows, as the name says to recompile a directory outside of
32 ;; your running emacs.
33 ;; The benefit is your files will be compiled in a clean environment without
34 ;; the old *.el files loaded.
35 ;; Among other things, this fix a bug in package.el which recompile
36 ;; the new files in the current environment with the old files loaded, creating
37 ;; errors in most packages after upgrades.
39 ;; NB: This package is advicing the function `package--compile'.
45 (defvar async-byte-compile-log-file "/tmp/async-bytecomp.log")
47 (defun async-byte-recompile-directory (directory &optional arg force)
48 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
51 when (file-exists-p f) do (delete-file f))
53 `(lambda (&optional ignore)
54 (if (file-exists-p async-byte-compile-log-file)
56 (pop-to-buffer (generate-new-buffer-name
57 byte-compile-log-buffer))
59 (insert-file-contents async-byte-compile-log-file)
61 (delete-file async-byte-compile-log-file)
64 (goto-char (point-min))
65 (while (re-search-forward "^.*:Error:" nil t)
68 (message "Failed to compile %d files in directory `%s'" n ,directory))))
69 (message "Directory `%s' compiled asynchronously with success" ,directory)))))
73 ,(async-inject-variables "\\`load-path\\'")
74 (let ((default-directory (file-name-as-directory ,directory))
76 (add-to-list 'load-path default-directory)
77 (byte-recompile-directory ,directory ,arg ,force)
78 (when (get-buffer byte-compile-log-buffer)
79 (setq error-data (with-current-buffer byte-compile-log-buffer
80 (buffer-substring-no-properties (point-min) (point-max))))
81 (unless (string= error-data "")
82 (with-temp-file ,async-byte-compile-log-file
84 (insert error-data))))))
86 (message "Started compiling asynchronously directory %s..." directory)))
88 (defadvice package--compile (around byte-compile-async activate)
89 (package-activate-1 pkg-desc)
90 (async-byte-recompile-directory (package-desc-dir pkg-desc) 0 t))
92 (provide 'async-bytecomp)
94 ;;; async-bytecomp.el ends here