1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
3 ;; Copyright (C) 1998-2005, 2007-2012 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Handle Emacs Lisp in an EDE Project file.
31 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
34 (defclass ede-proj-target-elisp (ede-proj-target-makefile)
36 (keybindings :initform nil)
38 (sourcetype :initform '(ede-source-emacs))
39 (availablecompilers :initform '(ede-emacs-compiler ede-xemacs-compiler))
40 (aux-packages :initarg :aux-packages
43 :custom (repeat string)
44 :documentation "Additional packages needed.
45 There should only be one toplevel package per auxiliary tool needed.
46 These packages location is found, and added to the compile time
49 "This target consists of a group of lisp files.
50 A lisp target may be one general program with many separate lisp files in it.")
52 (defvar ede-source-emacs
53 (ede-sourcecode "ede-emacs-source"
55 :sourcepattern "\\.el$"
56 :garbagepattern '("*.elc"))
57 "Emacs Lisp source code definition.")
59 (defvar ede-emacs-compiler
63 :variables '(("EMACS" . "emacs")
64 ("EMACSFLAGS" . "-batch --no-site-file"))
66 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
67 "for loadpath in . ${LOADPATH}; do \\"
68 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
70 "@echo \"(setq debug-on-error t)\" >> $@-compile-script"
71 "\"$(EMACS)\" $(EMACSFLAGS) -l $@-compile-script -f batch-byte-compile $^"
73 :autoconf '("AM_PATH_LISPDIR")
74 :sourcetype '(ede-source-emacs)
75 ; :objectextention ".elc"
77 "Compile Emacs Lisp programs.")
79 (defvar ede-xemacs-compiler
80 (clone ede-emacs-compiler "ede-xemacs-compiler"
82 :variables '(("EMACS" . "xemacs")))
83 "Compile Emacs Lisp programs with XEmacs.")
86 (defmethod ede-buffer-mine ((this ede-proj-target-elisp) buffer)
87 "Return t if object THIS lays claim to the file in BUFFER.
88 Lays claim to all .elc files that match .el files in this target."
89 (if (string-match "\\.elc$" (buffer-file-name buffer))
92 (file-name-sans-extension (buffer-file-name buffer))
95 ;; Is this in our list.
96 (member fname (oref this auxsource))
98 (call-next-method) ; The usual thing.
101 ;;; Emacs Lisp Compiler
102 ;;; Emacs Lisp Target
103 (defun ede-proj-elisp-packages-to-loadpath (packages)
104 "Convert a list of PACKAGES, to a list of load path."
108 (or (setq ldir (locate-library (car packages)))
109 (error "Cannot find package %s" (car packages)))
110 (let* ((fnd (file-name-directory ldir))
111 (rel (file-relative-name fnd))
114 ;; Make sure the relative name isn't to far off
115 (when (string-match "^\\.\\./\\.\\./\\.\\./\\.\\." rel)
118 (setq paths (cons (or full rel) paths)
119 packages (cdr packages))))
122 (defmethod project-compile-target ((obj ede-proj-target-elisp))
123 "Compile all sources in a Lisp target OBJ.
124 Bonus: Return a cons cell: (COMPILED . UPTODATE)."
125 (let* ((proj (ede-target-parent obj))
126 (dir (oref proj directory))
130 (let* ((fsrc (expand-file-name src dir))
131 (elc (concat (file-name-sans-extension fsrc) ".elc")))
132 (if (eq (byte-recompile-file fsrc nil 0) t)
133 (setq comp (1+ comp))
134 (setq utd (1+ utd)))))
136 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
139 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp) version)
140 "In a Lisp file, updated a version string for THIS to VERSION.
141 There are standards in Elisp files specifying how the version string
142 is found, such as a `-version' variable, or the standard header."
143 (if (and (slot-boundp this 'versionsource)
144 (oref this versionsource))
145 (let ((vs (oref this versionsource))
148 (with-current-buffer (find-file-noselect
149 (ede-expand-filename this (car vs)))
150 (goto-char (point-min))
151 (let ((case-fold-search t))
152 (if (re-search-forward "-version\\s-+\"\\([^\"]+\\)\"" nil t)
155 (delete-region (match-beginning 1)
157 (goto-char (match-beginning 1))
160 (if (not match) (call-next-method)))))
163 ;;; Makefile generation functions
165 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
166 "Return the variable name for THIS's sources."
167 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
168 (t (concat (ede-pmake-varname this) "_LISP"))))
170 (defun ede-proj-makefile-insert-loadpath-items (items)
171 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
173 (ede-pmake-insert-variable-shared "LOADPATH"
174 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
176 (when (not (save-excursion
178 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
180 (insert " " (car items)))
181 (setq items (cdr items)))))
184 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
185 "Insert variables needed by target THIS."
186 (let ((newitems (if (oref this aux-packages)
187 (ede-proj-elisp-packages-to-loadpath
188 (oref this aux-packages))))
190 (ede-proj-makefile-insert-loadpath-items newitems)))
192 (defun ede-proj-elisp-add-path (path)
193 "Add path PATH into the file if it isn't already there."
194 (goto-char (point-min))
195 (if (re-search-forward (concat "(cons \\\""
198 nil;; We have it already
199 (if (re-search-forward "(cons nil" nil t)
204 " echo \"(setq load-path (cons \\\""
206 "\\\" load-path))\" >> script")
208 (error "Don't know how to update load path"))))
210 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
211 "Tweak the configure file (current buffer) to accommodate THIS."
213 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
214 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile)))
215 (if (or (not ec) (not (file-exists-p ec)))
216 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
218 (if (file-symlink-p ec)
221 (rename-file ec (concat ec ".tmp"))
222 (copy-file (concat ec ".tmp") ec)
223 (delete-file (concat ec ".tmp"))))
224 (set-buffer (find-file-noselect ec t))
225 (ede-proj-elisp-add-path "..")
226 (let ((paths (ede-proj-elisp-packages-to-loadpath
227 (oref this aux-packages))))
228 ;; Add in the current list of paths
230 (ede-proj-elisp-add-path (car paths))
231 (setq paths (cdr paths))))
234 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
235 "Flush the configure file (current buffer) to accommodate THIS."
236 ;; Remove crufty old paths from elisp-compile
237 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
239 (if (and ec (file-exists-p ec))
240 (with-current-buffer (find-file-noselect ec t)
241 (goto-char (point-min))
242 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
244 (let ((path (match-string 1)))
245 (if (string= path "nil")
247 (delete-region (point-at-bol) (point-at-bol 2)))))))))
250 ;; Autoload generators
252 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
253 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
254 (aux-packages :initform ("cedet-autogen"))
256 (autoload-file :initarg :autoload-file
257 :initform "loaddefs.el"
260 :documentation "The file that autoload definitions are placed in.
261 There should be one load defs file for a given package. The load defs are created
262 for all Emacs Lisp sources that exist in the directory of the created target.")
263 (autoload-dirs :initarg :autoload-dirs
266 :custom (repeat string)
267 :documentation "The directories to scan for autoload definitions.
268 If nil defaults to the current directory.")
270 "Target that builds an autoload file.
271 Files do not need to be added to this target.")
275 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
276 "Return t if object THIS lays claim to the file in BUFFER.
277 Lays claim to all .elc files that match .el files in this target."
279 (concat (regexp-quote (oref this autoload-file)) "$")
280 (buffer-file-name buffer))
282 (call-next-method) ; The usual thing.
286 (defvar ede-emacs-cedet-autogen-compiler
288 "ede-emacs-autogen-compiler"
290 :variables '(("EMACS" . "emacs"))
292 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
293 "for loadpath in . ${LOADPATH}; do \\"
294 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
296 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
297 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
299 :sourcetype '(ede-source-emacs)
301 "Build an autoloads file.")
303 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
304 "List of compilers being used by OBJ.
305 If the `compiler' slot is empty, get the car of the compilers list."
306 (let ((comp (oref obj compiler)))
309 (setq comp (mapcar 'symbol-value comp))
310 (setq comp (list (symbol-value comp))))
311 ;; Get the first element from our list of compilers.
312 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
313 (setq comp (list (car avail)))))
316 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
319 "Insert the source variables needed by THIS.
320 Optional argument MORESOURCE is a list of additional sources to add to the
324 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
325 "Return the variable name for THIS's sources."
328 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
329 "Return a string representing the dependencies for THIS.
330 Always return an empty string for an autoloads generator."
333 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
334 "Insert variables needed by target THIS."
335 (ede-pmake-insert-variable-shared "LOADDEFS"
336 (insert (oref this autoload-file)))
337 (ede-pmake-insert-variable-shared "LOADDIRS"
338 (insert (mapconcat 'identity
339 (or (oref this autoload-dirs) '("."))
343 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
344 "Create or update the autoload target."
345 (require 'cedet-autogen)
346 (let ((default-directory (ede-expand-filename obj ".")))
347 (apply 'cedet-update-autoloads
348 (oref obj autoload-file)
349 (oref obj autoload-dirs))
352 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
353 "In a Lisp file, updated a version string for THIS to VERSION.
354 There are standards in Elisp files specifying how the version string
355 is found, such as a `-version' variable, or the standard header."
358 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
359 "Insert any symbols that the DIST rule should depend on.
360 Emacs Lisp autoload files ship the generated .el files.
361 Argument THIS is the target which needs to insert an info file."
362 ;; In some cases, this is ONLY the index file. That should generally
364 (insert " " (ede-proj-makefile-target-name this))
367 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
368 "Insert any symbols that the DIST rule should distribute.
369 Emacs Lisp autoload files ship the generated .el files.
370 Argument THIS is the target which needs to insert an info file."
371 (insert " " (oref this autoload-file))
374 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
375 "Tweak the configure file (current buffer) to accommodate THIS."
376 (error "Autoloads not supported in autoconf yet"))
378 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
379 "Flush the configure file (current buffer) to accommodate THIS."
382 (provide 'ede/proj-elisp)
384 ;;; ede/proj-elisp.el ends here