Add arch tagline
[bpt/emacs.git] / lisp / cedet / ede / proj-elisp.el
1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Handle Emacs Lisp in an EDE Project file.
27
28 (require 'ede/proj)
29 (require 'ede/pmake)
30 (require 'ede/pconf)
31
32 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
33
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."
49 ))
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.")
52
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.")
59
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 $^"
73 )
74 :autoconf '("AM_PATH_LISPDIR")
75 :sourcetype '(ede-source-emacs)
76 ; :objectextention ".elc"
77 )
78 "Compile Emacs Lisp programs.")
79
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.")
85
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")
95 ))
96 ;; Is this in our list.
97 (member fname (oref this auxsource))
98 )
99 (call-next-method) ; The usual thing.
100 ))
101
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)
114 )
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))
122
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 )
134 (if (or (not (file-exists-p elc))
135 (file-newer-than-file-p fsrc elc))
136 (progn
137 (setq comp (1+ comp))
138 (byte-compile-file fsrc))
139 (setq utd (1+ utd)))))
140 (oref obj source))
141 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
142 (cons comp utd)
143 ))
144
145 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp) version)
146 "In a Lisp file, updated a version string for THIS to VERSION.
147 There are standards in Elisp files specifying how the version string
148 is found, such as a `-version' variable, or the standard header."
149 (if (and (slot-boundp this 'versionsource)
150 (oref this versionsource))
151 (let ((vs (oref this versionsource))
152 (match nil))
153 (while vs
154 (save-excursion
155 (set-buffer (find-file-noselect
156 (ede-expand-filename this (car vs))))
157 (goto-char (point-min))
158 (let ((case-fold-search t))
159 (if (re-search-forward "-version\\s-+\"\\([^\"]+\\)\"" nil t)
160 (progn
161 (setq match t)
162 (delete-region (match-beginning 1)
163 (match-end 1))
164 (goto-char (match-beginning 1))
165 (insert version)))))
166 (setq vs (cdr vs)))
167 (if (not match) (call-next-method)))))
168
169
170 ;;; Makefile generation functions
171 ;;
172 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
173 "Return the variable name for THIS's sources."
174 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
175 (t (concat (ede-pmake-varname this) "_LISP"))))
176
177 (defun ede-proj-makefile-insert-loadpath-items (items)
178 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
179 (when items
180 (ede-pmake-insert-variable-shared "LOADPATH"
181 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
182 (while items
183 (when (not (save-excursion
184 (re-search-backward
185 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
186 begin t)))
187 (insert " " (car items)))
188 (setq items (cdr items)))))
189 ))
190
191 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
192 "Insert variables needed by target THIS."
193 (let ((newitems (if (oref this aux-packages)
194 (ede-proj-elisp-packages-to-loadpath
195 (oref this aux-packages))))
196 )
197 (ede-proj-makefile-insert-loadpath-items newitems)))
198
199 (defun ede-proj-elisp-add-path (path)
200 "Add path PATH into the file if it isn't already there."
201 (goto-char (point-min))
202 (if (re-search-forward (concat "(cons \\\""
203 (regexp-quote path))
204 nil t)
205 nil;; We have it already
206 (if (re-search-forward "(cons nil" nil t)
207 (progn
208 ;; insert stuff here
209 (end-of-line)
210 (insert "\n"
211 " echo \"(setq load-path (cons \\\""
212 path
213 "\\\" load-path))\" >> script")
214 )
215 (error "Don't know how to update load path"))))
216
217 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
218 "Tweak the configure file (current buffer) to accomodate THIS."
219 (call-next-method)
220 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
221 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile)))
222 (if (or (not ec) (not (file-exists-p ec)))
223 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
224 (save-excursion
225 (if (file-symlink-p ec)
226 (progn
227 ;; Desymlinkafy
228 (rename-file ec (concat ec ".tmp"))
229 (copy-file (concat ec ".tmp") ec)
230 (delete-file (concat ec ".tmp"))))
231 (set-buffer (find-file-noselect ec t))
232 (ede-proj-elisp-add-path "..")
233 (let ((paths (ede-proj-elisp-packages-to-loadpath
234 (oref this aux-packages))))
235 ;; Add in the current list of paths
236 (while paths
237 (ede-proj-elisp-add-path (car paths))
238 (setq paths (cdr paths))))
239 (save-buffer)) )))
240
241 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
242 "Flush the configure file (current buffer) to accomodate THIS."
243 ;; Remove crufty old paths from elisp-compile
244 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
245 )
246 (if (and ec (file-exists-p ec))
247 (save-excursion
248 (set-buffer (find-file-noselect ec t))
249 (goto-char (point-min))
250 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
251 nil t)
252 (let ((path (match-string 1)))
253 (if (string= path "nil")
254 nil
255 (delete-region (save-excursion (beginning-of-line) (point))
256 (save-excursion (end-of-line)
257 (forward-char 1)
258 (point))))))))))
259
260 ;;;
261 ;; Autoload generators
262 ;;
263 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
264 ((availablecompilers :initform (ede-emacs-cedet-autogen-compiler))
265 (aux-packages :initform ("cedet-autogen"))
266 (phony :initform t)
267 (autoload-file :initarg :autoload-file
268 :initform "loaddefs.el"
269 :type string
270 :custom string
271 :documentation "The file that autoload definitions are placed in.
272 There should be one load defs file for a given package. The load defs are created
273 for all Emacs Lisp sources that exist in the directory of the created target.")
274 (autoload-dirs :initarg :autoload-dirs
275 :initform nil
276 :type list
277 :custom (repeat string)
278 :documentation "The directories to scan for autoload definitions.
279 If nil defaults to the current directory.")
280 )
281 "Target that builds an autoload file.
282 Files do not need to be added to this target.")
283
284
285 ;;; Claiming files
286 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
287 "Return t if object THIS lays claim to the file in BUFFER.
288 Lays claim to all .elc files that match .el files in this target."
289 (if (string-match
290 (concat (regexp-quote (oref this autoload-file)) "$")
291 (buffer-file-name buffer))
292 t
293 (call-next-method) ; The usual thing.
294 ))
295
296 ;; Compilers
297 (defvar ede-emacs-cedet-autogen-compiler
298 (ede-compiler
299 "ede-emacs-autogen-compiler"
300 :name "emacs"
301 :variables '(("EMACS" . "emacs"))
302 :commands
303 '("@echo \"(add-to-list 'load-path nil)\" > $@-compile-script"
304 "for loadpath in . ${LOADPATH}; do \\"
305 " echo \"(add-to-list 'load-path \\\"$$loadpath\\\")\" >> $@-compile-script; \\"
306 "done;"
307 "@echo \"(require 'cedet-autogen)\" >> $@-compile-script"
308 "\"$(EMACS)\" -batch --no-site-file -l $@-compile-script -f cedet-batch-update-autoloads $(LOADDEFS) $(LOADDIRS)"
309 )
310 :sourcetype '(ede-source-emacs)
311 )
312 "Build an autoloads file.")
313
314 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
315 "List of compilers being used by OBJ.
316 If the `compiler' slot is empty, get the car of the compilers list."
317 (let ((comp (oref obj compiler)))
318 (if comp
319 (if (listp comp)
320 (setq comp (mapcar 'symbol-value comp))
321 (setq comp (list (symbol-value comp))))
322 ;; Get the first element from our list of compilers.
323 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
324 (setq comp (list (car avail)))))
325 comp))
326
327 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
328 &optional
329 moresource)
330 "Insert the source variables needed by THIS.
331 Optional argument MORESOURCE is a list of additional sources to add to the
332 sources variable."
333 nil)
334
335 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
336 "Return the variable name for THIS's sources."
337 nil) ; "LOADDEFS")
338
339 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
340 "Return a string representing the dependencies for THIS.
341 Always return an empty string for an autoloads generator."
342 "")
343
344 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
345 "Insert variables needed by target THIS."
346 (ede-pmake-insert-variable-shared "LOADDEFS"
347 (insert (oref this autoload-file)))
348 (ede-pmake-insert-variable-shared "LOADDIRS"
349 (insert (mapconcat 'identity
350 (or (oref this autoload-dirs) '("."))
351 " ")))
352 )
353
354 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
355 "Create or update the autoload target."
356 (require 'cedet-autogen)
357 (let ((default-directory (ede-expand-filename obj ".")))
358 (apply 'cedet-update-autoloads
359 (oref obj autoload-file)
360 (oref obj autoload-dirs))
361 ))
362
363 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
364 "In a Lisp file, updated a version string for THIS to VERSION.
365 There are standards in Elisp files specifying how the version string
366 is found, such as a `-version' variable, or the standard header."
367 nil)
368
369 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
370 "Insert any symbols that the DIST rule should depend on.
371 Emacs Lisp autoload files ship the generated .el files.
372 Argument THIS is the target which needs to insert an info file."
373 ;; In some cases, this is ONLY the index file. That should generally
374 ;; be ok.
375 (insert " " (ede-proj-makefile-target-name this))
376 )
377
378 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
379 "Insert any symbols that the DIST rule should distribute.
380 Emacs Lisp autoload files ship the generated .el files.
381 Argument THIS is the target which needs to insert an info file."
382 (insert " " (oref this autoload-file))
383 )
384
385 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
386 "Tweak the configure file (current buffer) to accomodate THIS."
387 (error "Autoloads not supported in autoconf yet"))
388
389 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
390 "Flush the configure file (current buffer) to accomodate THIS."
391 nil)
392
393 (provide 'ede/proj-elisp)
394
395 ;; arch-tag: 3802c94b-d04d-4ecf-9bab-b29ed6e77588
396 ;;; ede/proj-elisp.el ends here