]> git.notmuchmail.org Git - notmuch/blob - emacs/make-deps.el
dcac319ce282c35bce12ce0451104bdc32bb0312
[notmuch] / emacs / make-deps.el
1 ;; make-deps.el --- compute make dependencies for Elisp sources
2 ;;
3 ;; Copyright © Austin Clements
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: Austin Clements <aclements@csail.mit.edu>
21
22 ;;; Code:
23
24 (defun batch-make-deps ()
25   "Invoke `make-deps' for each file on the command line."
26   (setq debug-on-error t)
27   (dolist (file command-line-args-left)
28     (let ((default-directory command-line-default-directory))
29       (find-file-literally file))
30     (make-deps command-line-default-directory))
31   (kill-emacs))
32
33 (defun make-deps (&optional dir)
34   "Print make dependencies for the current buffer.
35
36 This prints make dependencies to `standard-output' based on the
37 top-level `require' expressions in the current buffer.  Paths in
38 rules will be given relative to DIR, or `default-directory'."
39   (setq dir (or dir default-directory))
40   (save-excursion
41     (goto-char (point-min))
42     (condition-case nil
43         (while t
44           (let ((form (read (current-buffer))))
45             ;; Is it a (require 'x) form?
46             (when (and (listp form) (= (length form) 2)
47                        (eq (car form) 'require)
48                        (listp (cadr form)) (= (length (cadr form)) 2)
49                        (eq (car (cadr form)) 'quote)
50                        (symbolp (cadr (cadr form))))
51               ;; Find the required library
52               (let* ((name (cadr (cadr form)))
53                      (fname (locate-library (symbol-name name))))
54                 ;; Is this file and the library in the same directory?
55                 ;; If not, assume it's a system library and don't
56                 ;; bother depending on it.
57                 (when (and fname
58                            (string= (file-name-directory (buffer-file-name))
59                                     (file-name-directory fname)))
60                   ;; Print the dependency
61                   (princ (format "%s.elc: %s.elc\n"
62                                  (file-name-sans-extension
63                                   (file-relative-name (buffer-file-name) dir))
64                                  (file-name-sans-extension
65                                   (file-relative-name fname dir)))))))))
66       (end-of-file nil))))
67
68 ;;; make-deps.el ends here