Refill some copyright headers.
[emacs.git] / lisp / cedet / ede / proj-elisp.el
blobf442b1a071f4ea10c7ed4e0dd639d30ace7c484a
1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007,
4 ;; 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Handle Emacs Lisp in an EDE Project file.
28 (require 'ede/proj)
29 (require 'ede/pmake)
30 (require 'ede/pconf)
32 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
34 ;;; Code:
35 (defclass ede-proj-target-elisp (ede-proj-target-makefile)
36 ((menu :initform nil)
37 (keybindings :initform nil)
38 (phony :initform t)
39 (sourcetype :initform '(ede-source-emacs))
40 (availablecompilers :initform '(ede-emacs-compiler ede-xemacs-compiler))
41 (aux-packages :initarg :aux-packages
42 :initform nil
43 :type list
44 :custom (repeat string)
45 :documentation "Additional packages needed.
46 There should only be one toplevel package per auxiliary tool needed.
47 These packages location is found, and added to the compile time
48 load path."
50 "This target consists of a group of lisp files.
51 A lisp target may be one general program with many separate lisp files in it.")
53 (defvar ede-source-emacs
54 (ede-sourcecode "ede-emacs-source"
55 :name "Emacs Lisp"
56 :sourcepattern "\\.el$"
57 :garbagepattern '("*.elc"))
58 "Emacs Lisp source code definition.")
60 (defvar ede-emacs-compiler
61 (ede-compiler
62 "ede-emacs-compiler"
63 :name "emacs"
64 :variables '(("EMACS" . "emacs")
65 ("EMACSFLAGS" . "-batch --no-site-file"))
66 :commands
67 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
68 "for loadpath in . ${LOADPATH}; do \\"
69 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
70 "done;"
71 "@echo \"(setq debug-on-error t)\" >> $@-compile-script"
72 "\"$(EMACS)\" $(EMACSFLAGS) -l $@-compile-script -f batch-byte-compile $^"
74 :autoconf '("AM_PATH_LISPDIR")
75 :sourcetype '(ede-source-emacs)
76 ; :objectextention ".elc"
78 "Compile Emacs Lisp programs.")
80 (defvar ede-xemacs-compiler
81 (clone ede-emacs-compiler "ede-xemacs-compiler"
82 :name "xemacs"
83 :variables '(("EMACS" . "xemacs")))
84 "Compile Emacs Lisp programs with XEmacs.")
86 ;;; Claiming files
87 (defmethod ede-buffer-mine ((this ede-proj-target-elisp) buffer)
88 "Return t if object THIS lays claim to the file in BUFFER.
89 Lays claim to all .elc files that match .el files in this target."
90 (if (string-match "\\.elc$" (buffer-file-name buffer))
91 (let ((fname
92 (concat
93 (file-name-sans-extension (buffer-file-name buffer))
94 ".el")
96 ;; Is this in our list.
97 (member fname (oref this auxsource))
99 (call-next-method) ; The usual thing.
102 ;;; Emacs Lisp Compiler
103 ;;; Emacs Lisp Target
104 (defun ede-proj-elisp-packages-to-loadpath (packages)
105 "Convert a list of PACKAGES, to a list of load path."
106 (let ((paths nil)
107 (ldir nil))
108 (while packages
109 (or (setq ldir (locate-library (car packages)))
110 (error "Cannot find package %s" (car packages)))
111 (let* ((fnd (file-name-directory ldir))
112 (rel (file-relative-name fnd))
113 (full nil)
115 ;; Make sure the relative name isn't to far off
116 (when (string-match "^\\.\\./\\.\\./\\.\\./\\.\\." rel)
117 (setq full fnd))
118 ;; Do the setup.
119 (setq paths (cons (or full rel) paths)
120 packages (cdr packages))))
121 paths))
123 (defmethod project-compile-target ((obj ede-proj-target-elisp))
124 "Compile all sources in a Lisp target OBJ.
125 Bonus: Return a cons cell: (COMPILED . UPTODATE)."
126 (let* ((proj (ede-target-parent obj))
127 (dir (oref proj directory))
128 (comp 0)
129 (utd 0))
130 (mapc (lambda (src)
131 (let* ((fsrc (expand-file-name src dir))
132 (elc (concat (file-name-sans-extension fsrc) ".elc")))
133 (if (eq (byte-recompile-file fsrc nil 0) t)
134 (setq comp (1+ comp))
135 (setq utd (1+ utd)))))
136 (oref obj source))
137 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
138 (cons comp utd)))
140 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp) version)
141 "In a Lisp file, updated a version string for THIS to VERSION.
142 There are standards in Elisp files specifying how the version string
143 is found, such as a `-version' variable, or the standard header."
144 (if (and (slot-boundp this 'versionsource)
145 (oref this versionsource))
146 (let ((vs (oref this versionsource))
147 (match nil))
148 (while vs
149 (with-current-buffer (find-file-noselect
150 (ede-expand-filename this (car vs)))
151 (goto-char (point-min))
152 (let ((case-fold-search t))
153 (if (re-search-forward "-version\\s-+\"\\([^\"]+\\)\"" nil t)
154 (progn
155 (setq match t)
156 (delete-region (match-beginning 1)
157 (match-end 1))
158 (goto-char (match-beginning 1))
159 (insert version)))))
160 (setq vs (cdr vs)))
161 (if (not match) (call-next-method)))))
164 ;;; Makefile generation functions
166 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
167 "Return the variable name for THIS's sources."
168 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
169 (t (concat (ede-pmake-varname this) "_LISP"))))
171 (defun ede-proj-makefile-insert-loadpath-items (items)
172 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
173 (when items
174 (ede-pmake-insert-variable-shared "LOADPATH"
175 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
176 (while items
177 (when (not (save-excursion
178 (re-search-backward
179 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
180 begin t)))
181 (insert " " (car items)))
182 (setq items (cdr items)))))
185 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
186 "Insert variables needed by target THIS."
187 (let ((newitems (if (oref this aux-packages)
188 (ede-proj-elisp-packages-to-loadpath
189 (oref this aux-packages))))
191 (ede-proj-makefile-insert-loadpath-items newitems)))
193 (defun ede-proj-elisp-add-path (path)
194 "Add path PATH into the file if it isn't already there."
195 (goto-char (point-min))
196 (if (re-search-forward (concat "(cons \\\""
197 (regexp-quote path))
198 nil t)
199 nil;; We have it already
200 (if (re-search-forward "(cons nil" nil t)
201 (progn
202 ;; insert stuff here
203 (end-of-line)
204 (insert "\n"
205 " echo \"(setq load-path (cons \\\""
206 path
207 "\\\" load-path))\" >> script")
209 (error "Don't know how to update load path"))))
211 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
212 "Tweak the configure file (current buffer) to accomodate THIS."
213 (call-next-method)
214 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
215 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile)))
216 (if (or (not ec) (not (file-exists-p ec)))
217 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
218 (save-excursion
219 (if (file-symlink-p ec)
220 (progn
221 ;; Desymlinkafy
222 (rename-file ec (concat ec ".tmp"))
223 (copy-file (concat ec ".tmp") ec)
224 (delete-file (concat ec ".tmp"))))
225 (set-buffer (find-file-noselect ec t))
226 (ede-proj-elisp-add-path "..")
227 (let ((paths (ede-proj-elisp-packages-to-loadpath
228 (oref this aux-packages))))
229 ;; Add in the current list of paths
230 (while paths
231 (ede-proj-elisp-add-path (car paths))
232 (setq paths (cdr paths))))
233 (save-buffer)) )))
235 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
236 "Flush the configure file (current buffer) to accomodate THIS."
237 ;; Remove crufty old paths from elisp-compile
238 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
240 (if (and ec (file-exists-p ec))
241 (with-current-buffer (find-file-noselect ec t)
242 (goto-char (point-min))
243 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
244 nil t)
245 (let ((path (match-string 1)))
246 (if (string= path "nil")
248 (delete-region (point-at-bol) (point-at-bol 2)))))))))
251 ;; Autoload generators
253 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
254 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
255 (aux-packages :initform ("cedet-autogen"))
256 (phony :initform t)
257 (autoload-file :initarg :autoload-file
258 :initform "loaddefs.el"
259 :type string
260 :custom string
261 :documentation "The file that autoload definitions are placed in.
262 There should be one load defs file for a given package. The load defs are created
263 for all Emacs Lisp sources that exist in the directory of the created target.")
264 (autoload-dirs :initarg :autoload-dirs
265 :initform nil
266 :type list
267 :custom (repeat string)
268 :documentation "The directories to scan for autoload definitions.
269 If nil defaults to the current directory.")
271 "Target that builds an autoload file.
272 Files do not need to be added to this target.")
275 ;;; Claiming files
276 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
277 "Return t if object THIS lays claim to the file in BUFFER.
278 Lays claim to all .elc files that match .el files in this target."
279 (if (string-match
280 (concat (regexp-quote (oref this autoload-file)) "$")
281 (buffer-file-name buffer))
283 (call-next-method) ; The usual thing.
286 ;; Compilers
287 (defvar ede-emacs-cedet-autogen-compiler
288 (ede-compiler
289 "ede-emacs-autogen-compiler"
290 :name "emacs"
291 :variables '(("EMACS" . "emacs"))
292 :commands
293 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
294 "for loadpath in . ${LOADPATH}; do \\"
295 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
296 "done;"
297 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
298 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
300 :sourcetype '(ede-source-emacs)
302 "Build an autoloads file.")
304 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
305 "List of compilers being used by OBJ.
306 If the `compiler' slot is empty, get the car of the compilers list."
307 (let ((comp (oref obj compiler)))
308 (if comp
309 (if (listp comp)
310 (setq comp (mapcar 'symbol-value comp))
311 (setq comp (list (symbol-value comp))))
312 ;; Get the first element from our list of compilers.
313 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
314 (setq comp (list (car avail)))))
315 comp))
317 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
318 &optional
319 moresource)
320 "Insert the source variables needed by THIS.
321 Optional argument MORESOURCE is a list of additional sources to add to the
322 sources variable."
323 nil)
325 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
326 "Return the variable name for THIS's sources."
327 nil) ; "LOADDEFS")
329 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
330 "Return a string representing the dependencies for THIS.
331 Always return an empty string for an autoloads generator."
334 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
335 "Insert variables needed by target THIS."
336 (ede-pmake-insert-variable-shared "LOADDEFS"
337 (insert (oref this autoload-file)))
338 (ede-pmake-insert-variable-shared "LOADDIRS"
339 (insert (mapconcat 'identity
340 (or (oref this autoload-dirs) '("."))
341 " ")))
344 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
345 "Create or update the autoload target."
346 (require 'cedet-autogen)
347 (let ((default-directory (ede-expand-filename obj ".")))
348 (apply 'cedet-update-autoloads
349 (oref obj autoload-file)
350 (oref obj autoload-dirs))
353 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
354 "In a Lisp file, updated a version string for THIS to VERSION.
355 There are standards in Elisp files specifying how the version string
356 is found, such as a `-version' variable, or the standard header."
357 nil)
359 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
360 "Insert any symbols that the DIST rule should depend on.
361 Emacs Lisp autoload files ship the generated .el files.
362 Argument THIS is the target which needs to insert an info file."
363 ;; In some cases, this is ONLY the index file. That should generally
364 ;; be ok.
365 (insert " " (ede-proj-makefile-target-name this))
368 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
369 "Insert any symbols that the DIST rule should distribute.
370 Emacs Lisp autoload files ship the generated .el files.
371 Argument THIS is the target which needs to insert an info file."
372 (insert " " (oref this autoload-file))
375 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
376 "Tweak the configure file (current buffer) to accomodate THIS."
377 (error "Autoloads not supported in autoconf yet"))
379 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
380 "Flush the configure file (current buffer) to accomodate THIS."
381 nil)
383 (provide 'ede/proj-elisp)
385 ;;; ede/proj-elisp.el ends here