]> code.delx.au - gnu-emacs/blob - lisp/cedet/ede/proj-elisp.el
66c7106336330dc53685eaaf3c53b716f2075a1d
[gnu-emacs] / lisp / cedet / ede / proj-elisp.el
1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
2
3 ;; Copyright (C) 1998-2005, 2007-2011 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24 ;;
25 ;; Handle Emacs Lisp in an EDE Project file.
26
27 (require 'ede/proj)
28 (require 'ede/pmake)
29 (require 'ede/pconf)
30
31 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
32
33 ;;; Code:
34 (defclass ede-proj-target-elisp (ede-proj-target-makefile)
35 ((menu :initform nil)
36 (keybindings :initform nil)
37 (phony :initform t)
38 (sourcetype :initform '(ede-source-emacs))
39 (availablecompilers :initform '(ede-emacs-compiler ede-xemacs-compiler))
40 (aux-packages :initarg :aux-packages
41 :initform nil
42 :type list
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
47 load path."
48 ))
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.")
51
52 (defvar ede-source-emacs
53 (ede-sourcecode "ede-emacs-source"
54 :name "Emacs Lisp"
55 :sourcepattern "\\.el$"
56 :garbagepattern '("*.elc"))
57 "Emacs Lisp source code definition.")
58
59 (defvar ede-emacs-compiler
60 (ede-compiler
61 "ede-emacs-compiler"
62 :name "emacs"
63 :variables '(("EMACS" . "emacs")
64 ("EMACSFLAGS" . "-batch --no-site-file"))
65 :commands
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; \\"
69 "done;"
70 "@echo \"(setq debug-on-error t)\" >> $@-compile-script"
71 "\"$(EMACS)\" $(EMACSFLAGS) -l $@-compile-script -f batch-byte-compile $^"
72 )
73 :autoconf '("AM_PATH_LISPDIR")
74 :sourcetype '(ede-source-emacs)
75 ; :objectextention ".elc"
76 )
77 "Compile Emacs Lisp programs.")
78
79 (defvar ede-xemacs-compiler
80 (clone ede-emacs-compiler "ede-xemacs-compiler"
81 :name "xemacs"
82 :variables '(("EMACS" . "xemacs")))
83 "Compile Emacs Lisp programs with XEmacs.")
84
85 ;;; Claiming files
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))
90 (let ((fname
91 (concat
92 (file-name-sans-extension (buffer-file-name buffer))
93 ".el")
94 ))
95 ;; Is this in our list.
96 (member fname (oref this auxsource))
97 )
98 (call-next-method) ; The usual thing.
99 ))
100
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."
105 (let ((paths nil)
106 (ldir nil))
107 (while packages
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))
112 (full nil)
113 )
114 ;; Make sure the relative name isn't to far off
115 (when (string-match "^\\.\\./\\.\\./\\.\\./\\.\\." rel)
116 (setq full fnd))
117 ;; Do the setup.
118 (setq paths (cons (or full rel) paths)
119 packages (cdr packages))))
120 paths))
121
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))
127 (comp 0)
128 (utd 0))
129 (mapc (lambda (src)
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)))))
135 (oref obj source))
136 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
137 (cons comp utd)))
138
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))
146 (match nil))
147 (while vs
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)
153 (progn
154 (setq match t)
155 (delete-region (match-beginning 1)
156 (match-end 1))
157 (goto-char (match-beginning 1))
158 (insert version)))))
159 (setq vs (cdr vs)))
160 (if (not match) (call-next-method)))))
161
162
163 ;;; Makefile generation functions
164 ;;
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"))))
169
170 (defun ede-proj-makefile-insert-loadpath-items (items)
171 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
172 (when items
173 (ede-pmake-insert-variable-shared "LOADPATH"
174 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
175 (while items
176 (when (not (save-excursion
177 (re-search-backward
178 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
179 begin t)))
180 (insert " " (car items)))
181 (setq items (cdr items)))))
182 ))
183
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))))
189 )
190 (ede-proj-makefile-insert-loadpath-items newitems)))
191
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 \\\""
196 (regexp-quote path))
197 nil t)
198 nil;; We have it already
199 (if (re-search-forward "(cons nil" nil t)
200 (progn
201 ;; insert stuff here
202 (end-of-line)
203 (insert "\n"
204 " echo \"(setq load-path (cons \\\""
205 path
206 "\\\" load-path))\" >> script")
207 )
208 (error "Don't know how to update load path"))))
209
210 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
211 "Tweak the configure file (current buffer) to accommodate THIS."
212 (call-next-method)
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.")
217 (save-excursion
218 (if (file-symlink-p ec)
219 (progn
220 ;; Desymlinkify
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
229 (while paths
230 (ede-proj-elisp-add-path (car paths))
231 (setq paths (cdr paths))))
232 (save-buffer)) )))
233
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))
238 )
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)"
243 nil t)
244 (let ((path (match-string 1)))
245 (if (string= path "nil")
246 nil
247 (delete-region (point-at-bol) (point-at-bol 2)))))))))
248
249 ;;;
250 ;; Autoload generators
251 ;;
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"))
255 (phony :initform t)
256 (autoload-file :initarg :autoload-file
257 :initform "loaddefs.el"
258 :type string
259 :custom string
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
264 :initform nil
265 :type list
266 :custom (repeat string)
267 :documentation "The directories to scan for autoload definitions.
268 If nil defaults to the current directory.")
269 )
270 "Target that builds an autoload file.
271 Files do not need to be added to this target.")
272
273
274 ;;; Claiming files
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."
278 (if (string-match
279 (concat (regexp-quote (oref this autoload-file)) "$")
280 (buffer-file-name buffer))
281 t
282 (call-next-method) ; The usual thing.
283 ))
284
285 ;; Compilers
286 (defvar ede-emacs-cedet-autogen-compiler
287 (ede-compiler
288 "ede-emacs-autogen-compiler"
289 :name "emacs"
290 :variables '(("EMACS" . "emacs"))
291 :commands
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; \\"
295 "done;"
296 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
297 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
298 )
299 :sourcetype '(ede-source-emacs)
300 )
301 "Build an autoloads file.")
302
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)))
307 (if comp
308 (if (listp comp)
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)))))
314 comp))
315
316 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
317 &optional
318 moresource)
319 "Insert the source variables needed by THIS.
320 Optional argument MORESOURCE is a list of additional sources to add to the
321 sources variable."
322 nil)
323
324 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
325 "Return the variable name for THIS's sources."
326 nil) ; "LOADDEFS")
327
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."
331 "")
332
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) '("."))
340 " ")))
341 )
342
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))
350 ))
351
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."
356 nil)
357
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
363 ;; be ok.
364 (insert " " (ede-proj-makefile-target-name this))
365 )
366
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))
372 )
373
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"))
377
378 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
379 "Flush the configure file (current buffer) to accommodate THIS."
380 nil)
381
382 (provide 'ede/proj-elisp)
383
384 ;;; ede/proj-elisp.el ends here