]> code.delx.au - gnu-emacs-elpa/blob - async-bytecomp.el
Use package--get-deps to get packages dependencies.
[gnu-emacs-elpa] / async-bytecomp.el
1 ;;; async-bytecomp.el --- Async functions to compile elisp files async
2
3 ;; Copyright (C) 2014 John Wiegley
4 ;; Copyright (C) 2014 Thierry Volpiatto
5
6 ;; Authors: John Wiegley <jwiegley@gmail.com>
7 ;; Thierry Volpiatto <thierry.volpiatto@gmail.com>
8
9 ;; Keywords: dired async byte-compile
10 ;; X-URL: https://github.com/jwiegley/dired-async
11
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.
16
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.
21
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.
26
27 ;;; Commentary:
28 ;;
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.
37 ;;
38 ;; NB: This package is advicing the function `package--compile'.
39
40 ;;; Code:
41
42 (require 'cl-lib)
43 (require 'async)
44
45 (defcustom async-bytecomp-allowed-packages '(async helm)
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 :group 'async
50 :type '(repeat (choice symbol)))
51
52 (defvar async-byte-compile-log-file "~/.emacs.d/async-bytecomp.log")
53
54 (defun async-byte-recompile-directory (directory &optional quiet)
55 "Compile all *.el files in DIRECTORY asynchronously.
56 All *.elc files are systematically deleted before proceeding."
57 (cl-loop with dir = (directory-files directory t "\\.elc\\'")
58 unless dir return nil
59 for f in dir
60 when (file-exists-p f) do (delete-file f))
61 ;; Ensure async is reloaded when async.elc is deleted.
62 ;; This happen when recompiling its own directory.
63 (load "async")
64 (let ((call-back
65 `(lambda (&optional ignore)
66 (if (file-exists-p async-byte-compile-log-file)
67 (let ((buf (get-buffer-create byte-compile-log-buffer))
68 (n 0))
69 (with-current-buffer buf
70 (goto-char (point-max))
71 (let ((inhibit-read-only t))
72 (insert-file-contents async-byte-compile-log-file)
73 (compilation-mode))
74 (display-buffer buf)
75 (delete-file async-byte-compile-log-file)
76 (unless ,quiet
77 (save-excursion
78 (goto-char (point-min))
79 (while (re-search-forward "^.*:Error:" nil t)
80 (cl-incf n)))
81 (if (> n 0)
82 (message "Failed to compile %d files in directory `%s'" n ,directory)
83 (message "Directory `%s' compiled asynchronously with warnings" ,directory)))))
84 (unless ,quiet
85 (message "Directory `%s' compiled asynchronously with success" ,directory))))))
86 (async-start
87 `(lambda ()
88 (require 'bytecomp)
89 ,(async-inject-variables "\\`\\(load-path\\)\\|byte\\'")
90 (let ((default-directory (file-name-as-directory ,directory))
91 error-data)
92 (add-to-list 'load-path default-directory)
93 (byte-recompile-directory ,directory 0 t)
94 (when (get-buffer byte-compile-log-buffer)
95 (setq error-data (with-current-buffer byte-compile-log-buffer
96 (buffer-substring-no-properties (point-min) (point-max))))
97 (unless (string= error-data "")
98 (with-temp-file ,async-byte-compile-log-file
99 (erase-buffer)
100 (insert error-data))))))
101 call-back)
102 (message "Started compiling asynchronously directory %s" directory)))
103
104 (defvar package-archive-contents)
105 (defvar package-alist)
106 (declare-function package-desc-reqs "package.el" (cl-x))
107 (declare-function package--get-deps "package.el" (pkg &optional only))
108
109 (unless (fboundp 'package--get-deps)
110 (defun package--get-deps (pkg &optional only)
111 (let* ((pkg-desc (cadr (assq pkg package-alist)))
112 (direct-deps (cl-loop for p in (package-desc-reqs pkg-desc)
113 for name = (car p)
114 when (assq name package-alist)
115 collect name))
116 (indirect-deps (unless (eq only 'direct)
117 (delete-dups
118 (cl-loop for p in direct-deps
119 append (package--get-deps p))))))
120 (cl-case only
121 (direct direct-deps)
122 (separate (list direct-deps indirect-deps))
123 (indirect indirect-deps)
124 (t (delete-dups (append direct-deps indirect-deps)))))))
125
126 (defun async-bytecomp-get-allowed-pkgs ()
127 (when async-bytecomp-allowed-packages
128 (cl-loop for p in async-bytecomp-allowed-packages
129 append (package--get-deps p) into reqs
130 finally return
131 (delete-dups
132 (append async-bytecomp-allowed-packages reqs)))))
133
134 (defadvice package--compile (around byte-compile-async activate)
135 (let ((cur-package (package-desc-name pkg-desc)))
136 (if (memq cur-package (async-bytecomp-get-allowed-pkgs))
137 (progn
138 (when (eq cur-package 'async)
139 (fmakunbound 'async-byte-recompile-directory))
140 (package-activate-1 pkg-desc)
141 (load "async-bytecomp") ; emacs-24.3 don't reload new files.
142 (async-byte-recompile-directory (package-desc-dir pkg-desc) t))
143 ad-do-it)))
144
145
146 (provide 'async-bytecomp)
147
148 ;;; async-bytecomp.el ends here