commit | author | age
|
5cb5f7
|
1 |
;;; async-bytecomp.el --- Compile elisp files asynchronously -*- lexical-binding: t -*- |
C |
2 |
|
|
3 |
;; Copyright (C) 2014-2016 Free Software Foundation, Inc. |
|
4 |
|
|
5 |
;; Authors: John Wiegley <jwiegley@gmail.com> |
|
6 |
;; Thierry Volpiatto <thierry.volpiatto@gmail.com> |
|
7 |
|
|
8 |
;; Keywords: dired async byte-compile |
|
9 |
;; X-URL: https://github.com/jwiegley/dired-async |
|
10 |
|
|
11 |
;; This program is free software; you can redistribute it and/or |
|
12 |
;; modify it under the terms of the GNU General Public License as |
|
13 |
;; published by the Free Software Foundation; either version 2, or (at |
|
14 |
;; your option) any later version. |
|
15 |
|
|
16 |
;; This program is distributed in the hope that it will be useful, but |
|
17 |
;; WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
;; General Public License for more details. |
|
20 |
|
|
21 |
;; You should have received a copy of the GNU General Public License |
|
22 |
;; along with GNU Emacs; see the file COPYING. If not, write to the |
|
23 |
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
|
24 |
;; Boston, MA 02111-1307, USA. |
|
25 |
|
|
26 |
;;; Commentary: |
|
27 |
;; |
|
28 |
;; This package provide the `async-byte-recompile-directory' function |
|
29 |
;; which allows, as the name says to recompile a directory outside of |
|
30 |
;; your running emacs. |
|
31 |
;; The benefit is your files will be compiled in a clean environment without |
|
32 |
;; the old *.el files loaded. |
|
33 |
;; Among other things, this fix a bug in package.el which recompile |
|
34 |
;; the new files in the current environment with the old files loaded, creating |
|
35 |
;; errors in most packages after upgrades. |
|
36 |
;; |
|
37 |
;; NB: This package is advicing the function `package--compile'. |
|
38 |
|
|
39 |
;;; Code: |
|
40 |
|
|
41 |
(require 'cl-lib) |
|
42 |
(require 'async) |
|
43 |
|
|
44 |
(defcustom async-bytecomp-allowed-packages |
|
45 |
'(async helm helm-core helm-ls-git helm-ls-hg magit) |
|
46 |
"Packages in this list will be compiled asynchronously by `package--compile'. |
|
47 |
All the dependencies of these packages will be compiled async too, |
|
48 |
so no need to add dependencies to this list. |
|
49 |
The value of this variable can also be a list with a single element, |
|
50 |
the symbol `all', in this case packages are always compiled asynchronously." |
|
51 |
:group 'async |
|
52 |
:type '(repeat (choice symbol))) |
|
53 |
|
|
54 |
(defvar async-byte-compile-log-file |
|
55 |
(concat user-emacs-directory "async-bytecomp.log")) |
|
56 |
|
|
57 |
;;;###autoload |
|
58 |
(defun async-byte-recompile-directory (directory &optional quiet) |
|
59 |
"Compile all *.el files in DIRECTORY asynchronously. |
|
60 |
All *.elc files are systematically deleted before proceeding." |
|
61 |
(cl-loop with dir = (directory-files directory t "\\.elc\\'") |
|
62 |
unless dir return nil |
|
63 |
for f in dir |
|
64 |
when (file-exists-p f) do (delete-file f)) |
|
65 |
;; Ensure async is reloaded when async.elc is deleted. |
|
66 |
;; This happen when recompiling its own directory. |
|
67 |
(load "async") |
|
68 |
(let ((call-back |
|
69 |
(lambda (&optional _ignore) |
|
70 |
(if (file-exists-p async-byte-compile-log-file) |
|
71 |
(let ((buf (get-buffer-create byte-compile-log-buffer)) |
|
72 |
(n 0)) |
|
73 |
(with-current-buffer buf |
|
74 |
(goto-char (point-max)) |
|
75 |
(let ((inhibit-read-only t)) |
|
76 |
(insert-file-contents async-byte-compile-log-file) |
|
77 |
(compilation-mode)) |
|
78 |
(display-buffer buf) |
|
79 |
(delete-file async-byte-compile-log-file) |
|
80 |
(unless quiet |
|
81 |
(save-excursion |
|
82 |
(goto-char (point-min)) |
|
83 |
(while (re-search-forward "^.*:Error:" nil t) |
|
84 |
(cl-incf n))) |
|
85 |
(if (> n 0) |
|
86 |
(message "Failed to compile %d files in directory `%s'" n directory) |
|
87 |
(message "Directory `%s' compiled asynchronously with warnings" directory))))) |
|
88 |
(unless quiet |
|
89 |
(message "Directory `%s' compiled asynchronously with success" directory)))))) |
|
90 |
(async-start |
|
91 |
`(lambda () |
|
92 |
(require 'bytecomp) |
|
93 |
,(async-inject-variables "\\`\\(load-path\\)\\|byte\\'") |
|
94 |
(let ((default-directory (file-name-as-directory ,directory)) |
|
95 |
error-data) |
|
96 |
(add-to-list 'load-path default-directory) |
|
97 |
(byte-recompile-directory ,directory 0 t) |
|
98 |
(when (get-buffer byte-compile-log-buffer) |
|
99 |
(setq error-data (with-current-buffer byte-compile-log-buffer |
|
100 |
(buffer-substring-no-properties (point-min) (point-max)))) |
|
101 |
(unless (string= error-data "") |
|
102 |
(with-temp-file ,async-byte-compile-log-file |
|
103 |
(erase-buffer) |
|
104 |
(insert error-data)))))) |
|
105 |
call-back) |
|
106 |
(unless quiet (message "Started compiling asynchronously directory %s" directory)))) |
|
107 |
|
|
108 |
(defvar package-archive-contents) |
|
109 |
(defvar package-alist) |
|
110 |
(declare-function package-desc-reqs "package.el" (cl-x)) |
|
111 |
|
|
112 |
(defun async-bytecomp--get-package-deps (pkg &optional only) |
|
113 |
;; Same as `package--get-deps' but parse instead `package-archive-contents' |
|
114 |
;; because PKG is not already installed and not present in `package-alist'. |
|
115 |
;; However fallback to `package-alist' in case PKG no more present |
|
116 |
;; in `package-archive-contents' due to modification to `package-archives'. |
|
117 |
;; See issue #58. |
|
118 |
(let* ((pkg-desc (cadr (or (assq pkg package-archive-contents) |
|
119 |
(assq pkg package-alist)))) |
|
120 |
(direct-deps (cl-loop for p in (package-desc-reqs pkg-desc) |
|
121 |
for name = (car p) |
|
122 |
when (or (assq name package-archive-contents) |
|
123 |
(assq name package-alist)) |
|
124 |
collect name)) |
|
125 |
(indirect-deps (unless (eq only 'direct) |
|
126 |
(delete-dups |
|
127 |
(cl-loop for p in direct-deps append |
|
128 |
(async-bytecomp--get-package-deps p)))))) |
|
129 |
(cl-case only |
|
130 |
(direct direct-deps) |
|
131 |
(separate (list direct-deps indirect-deps)) |
|
132 |
(indirect indirect-deps) |
|
133 |
(t (delete-dups (append direct-deps indirect-deps)))))) |
|
134 |
|
|
135 |
(defun async-bytecomp-get-allowed-pkgs () |
|
136 |
(when (and async-bytecomp-allowed-packages |
|
137 |
(listp async-bytecomp-allowed-packages)) |
|
138 |
(if package-archive-contents |
|
139 |
(cl-loop for p in async-bytecomp-allowed-packages |
|
140 |
when (assq p package-archive-contents) |
|
141 |
append (async-bytecomp--get-package-deps p) into reqs |
|
142 |
finally return |
|
143 |
(delete-dups |
|
144 |
(append async-bytecomp-allowed-packages reqs))) |
|
145 |
async-bytecomp-allowed-packages))) |
|
146 |
|
|
147 |
(defadvice package--compile (around byte-compile-async) |
|
148 |
(let ((cur-package (package-desc-name pkg-desc)) |
|
149 |
(pkg-dir (package-desc-dir pkg-desc))) |
|
150 |
(if (or (equal async-bytecomp-allowed-packages '(all)) |
|
151 |
(memq cur-package (async-bytecomp-get-allowed-pkgs))) |
|
152 |
(progn |
|
153 |
(when (eq cur-package 'async) |
|
154 |
(fmakunbound 'async-byte-recompile-directory)) |
|
155 |
;; Add to `load-path' the latest version of async and |
|
156 |
;; reload it when reinstalling async. |
|
157 |
(when (string= cur-package "async") |
|
158 |
(cl-pushnew pkg-dir load-path) |
|
159 |
(load "async-bytecomp")) |
|
160 |
;; `async-byte-recompile-directory' will add directory |
|
161 |
;; as needed to `load-path'. |
|
162 |
(async-byte-recompile-directory (package-desc-dir pkg-desc) t)) |
|
163 |
ad-do-it))) |
|
164 |
|
|
165 |
;;;###autoload |
|
166 |
(define-minor-mode async-bytecomp-package-mode |
|
167 |
"Byte compile asynchronously packages installed with package.el. |
|
168 |
Async compilation of packages can be controlled by |
|
169 |
`async-bytecomp-allowed-packages'." |
|
170 |
:group 'async |
|
171 |
:global t |
|
172 |
(if async-bytecomp-package-mode |
|
173 |
(ad-activate 'package--compile) |
|
174 |
(ad-deactivate 'package--compile))) |
|
175 |
|
|
176 |
;;;###autoload |
|
177 |
(defun async-byte-compile-file (file) |
|
178 |
"Byte compile Lisp code FILE asynchronously. |
|
179 |
|
|
180 |
Same as `byte-compile-file' but asynchronous." |
|
181 |
(interactive "fFile: ") |
|
182 |
(let ((call-back |
|
183 |
(lambda (&optional _ignore) |
|
184 |
(let ((bn (file-name-nondirectory file))) |
|
185 |
(if (file-exists-p async-byte-compile-log-file) |
|
186 |
(let ((buf (get-buffer-create byte-compile-log-buffer)) |
|
187 |
start) |
|
188 |
(with-current-buffer buf |
|
189 |
(goto-char (setq start (point-max))) |
|
190 |
(let ((inhibit-read-only t)) |
|
191 |
(insert-file-contents async-byte-compile-log-file) |
|
192 |
(compilation-mode)) |
|
193 |
(display-buffer buf) |
|
194 |
(delete-file async-byte-compile-log-file) |
|
195 |
(save-excursion |
|
196 |
(goto-char start) |
|
197 |
(if (re-search-forward "^.*:Error:" nil t) |
|
198 |
(message "Failed to compile `%s'" bn) |
|
199 |
(message "`%s' compiled asynchronously with warnings" bn))))) |
|
200 |
(message "`%s' compiled asynchronously with success" bn)))))) |
|
201 |
(async-start |
|
202 |
`(lambda () |
|
203 |
(require 'bytecomp) |
|
204 |
,(async-inject-variables "\\`load-path\\'") |
|
205 |
(let ((default-directory ,(file-name-directory file))) |
|
206 |
(add-to-list 'load-path default-directory) |
|
207 |
(byte-compile-file ,file) |
|
208 |
(when (get-buffer byte-compile-log-buffer) |
|
209 |
(setq error-data (with-current-buffer byte-compile-log-buffer |
|
210 |
(buffer-substring-no-properties (point-min) (point-max)))) |
|
211 |
(unless (string= error-data "") |
|
212 |
(with-temp-file ,async-byte-compile-log-file |
|
213 |
(erase-buffer) |
|
214 |
(insert error-data)))))) |
|
215 |
call-back))) |
|
216 |
|
|
217 |
(provide 'async-bytecomp) |
|
218 |
|
|
219 |
;;; async-bytecomp.el ends here |