Update copyright notices for 2013.
[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-2005, 2007-2013 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 (pre-load-packages :initarg :pre-load-packages
50 :initform nil
51 :type list
52 :custom (repeat string)
53 :documentation "Additional packages to pre-load.
54 Each package name will be loaded with `require'.
55 Each package's directory should also appear in :aux-packages via a package name.")
56 )
57 "This target consists of a group of lisp files.
58 A lisp target may be one general program with many separate lisp files in it.")
59
60 (defmethod ede-proj-makefile-insert-rules :after ((this ede-proj-target-elisp))
61 "Insert rules needed by THIS target.
62 This inserts the PRELOADS target-local variable."
63 (let ((preloads (oref this pre-load-packages)))
64 (when preloads
65 (insert (format "%s: PRELOADS=%s\n"
66 (oref this name)
67 (mapconcat 'identity preloads " ")))))
68 (insert "\n"))
69
70 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp))
71 "Return a string representing the dependencies for THIS.
72 Some compilers only use the first element in the dependencies, others
73 have a list of intermediates (object files), and others don't care.
74 This allows customization of how these elements appear.
75 For Emacs Lisp, return addsuffix command on source files."
76 (format "$(addsuffix c, $(%s))"
77 (ede-proj-makefile-sourcevar this)))
78
79 (defvar ede-source-emacs
80 (ede-sourcecode "ede-emacs-source"
81 :name "Emacs Lisp"
82 :sourcepattern "\\.el$"
83 :garbagepattern '("*.elc"))
84 "Emacs Lisp source code definition.")
85
86 (defvar ede-emacs-compiler
87 (ede-compiler
88 "ede-emacs-compiler"
89 :name "emacs"
90 :variables '(("EMACS" . "emacs")
91 ("EMACSFLAGS" . "-batch --no-site-file --eval '(setq debug-on-error t)'")
92 ("require" . "$(foreach r,$(1),(require (quote $(r))))"))
93 :rules (list (ede-makefile-rule
94 "elisp-inference-rule"
95 :target "%.elc"
96 :dependencies "%.el"
97 :rules '("$(EMACS) $(EMACSFLAGS) $(addprefix -L ,$(LOADPATH)) \
98 --eval '(progn $(call require, $(PRELOADS)))' -f batch-byte-compile $^")))
99 :autoconf '("AM_PATH_LISPDIR")
100 :sourcetype '(ede-source-emacs)
101 :objectextention ".elc"
102 )
103 "Compile Emacs Lisp programs.")
104
105 (defvar ede-xemacs-compiler
106 (clone ede-emacs-compiler "ede-xemacs-compiler"
107 :name "xemacs"
108 :variables '(("EMACS" . "xemacs")))
109 "Compile Emacs Lisp programs with XEmacs.")
110
111 ;;; Claiming files
112 (defmethod ede-buffer-mine ((this ede-proj-target-elisp) buffer)
113 "Return t if object THIS lays claim to the file in BUFFER.
114 Lays claim to all .elc files that match .el files in this target."
115 (if (string-match "\\.elc$" (buffer-file-name buffer))
116 (let ((fname
117 (concat
118 (file-name-sans-extension (buffer-file-name buffer))
119 ".el")
120 ))
121 ;; Is this in our list.
122 (member fname (oref this auxsource))
123 )
124 (call-next-method) ; The usual thing.
125 ))
126
127 ;;; Emacs Lisp Compiler
128 ;;; Emacs Lisp Target
129 (defun ede-proj-elisp-packages-to-loadpath (packages)
130 "Convert a list of PACKAGES, to a list of load path."
131 (let ((paths nil)
132 (ldir nil))
133 (while packages
134 (or (setq ldir (locate-library (car packages)))
135 (error "Cannot find package %s" (car packages)))
136 (let* ((fnd (file-name-directory ldir))
137 (rel (file-relative-name fnd))
138 (full nil)
139 )
140 ;; Make sure the relative name isn't to far off
141 (when (string-match "^\\.\\./\\.\\./\\.\\./\\.\\./\\.\\." rel)
142 (setq full fnd))
143 ;; Do the setup.
144 (setq paths (cons (or full rel) paths)
145 packages (cdr packages))))
146 paths))
147
148 (defmethod project-compile-target ((obj ede-proj-target-elisp))
149 "Compile all sources in a Lisp target OBJ.
150 Bonus: Return a cons cell: (COMPILED . UPTODATE)."
151 (let* ((proj (ede-target-parent obj))
152 (dir (oref proj directory))
153 (comp 0)
154 (utd 0))
155 (mapc (lambda (src)
156 (let* ((fsrc (expand-file-name src dir))
157 (elc (concat (file-name-sans-extension fsrc) ".elc")))
158 (with-no-warnings
159 (if (< emacs-major-version 24)
160 ;; Does not have `byte-recompile-file'
161 (if (or (not (file-exists-p elc))
162 (file-newer-than-file-p fsrc elc))
163 (progn
164 (setq comp (1+ comp))
165 (byte-compile-file fsrc))
166 (setq utd (1+ utd)))
167
168 (if (eq (byte-recompile-file fsrc nil 0) t)
169 (setq comp (1+ comp))
170 (setq utd (1+ utd)))))))
171
172 (oref obj source))
173 (message "All Emacs Lisp sources are up to date in %s" (object-name obj))
174 (cons comp utd)))
175
176 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp) version)
177 "In a Lisp file, updated a version string for THIS to VERSION.
178 There are standards in Elisp files specifying how the version string
179 is found, such as a `-version' variable, or the standard header."
180 (if (and (slot-boundp this 'versionsource)
181 (oref this versionsource))
182 (let ((vs (oref this versionsource))
183 (match nil))
184 (while vs
185 (with-current-buffer (find-file-noselect
186 (ede-expand-filename this (car vs)))
187 (goto-char (point-min))
188 (let ((case-fold-search t))
189 (if (re-search-forward "-version\\s-+\"\\([^\"]+\\)\"" nil t)
190 (progn
191 (setq match t)
192 (delete-region (match-beginning 1)
193 (match-end 1))
194 (goto-char (match-beginning 1))
195 (insert version)))))
196 (setq vs (cdr vs)))
197 (if (not match) (call-next-method)))))
198
199
200 ;;; Makefile generation functions
201 ;;
202 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
203 "Return the variable name for THIS's sources."
204 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
205 (t (concat (ede-pmake-varname this) "_LISP"))))
206
207 (defun ede-proj-makefile-insert-loadpath-items (items)
208 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
209 (when items
210 (ede-pmake-insert-variable-shared "LOADPATH"
211 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
212 (while items
213 (when (not (save-excursion
214 (re-search-backward
215 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
216 begin t)))
217 (insert " " (car items)))
218 (setq items (cdr items)))))
219 ))
220
221 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
222 "Insert variables needed by target THIS."
223 (let ((newitems (if (oref this aux-packages)
224 (ede-proj-elisp-packages-to-loadpath
225 (oref this aux-packages)))))
226 (ede-proj-makefile-insert-loadpath-items newitems)))
227
228 (defun ede-proj-elisp-add-path (path)
229 "Add path PATH into the file if it isn't already there."
230 (goto-char (point-min))
231 (if (re-search-forward (concat "(cons \\\""
232 (regexp-quote path))
233 nil t)
234 nil;; We have it already
235 (if (re-search-forward "(cons nil" nil t)
236 (progn
237 ;; insert stuff here
238 (end-of-line)
239 (insert "\n"
240 " echo \"(setq load-path (cons \\\""
241 path
242 "\\\" load-path))\" >> script")
243 )
244 (error "Don't know how to update load path"))))
245
246 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
247 "Tweak the configure file (current buffer) to accommodate THIS."
248 (call-next-method)
249 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
250 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
251 (enable-local-variables nil))
252 (if (or (not ec) (not (file-exists-p ec)))
253 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
254 (save-excursion
255 (if (file-symlink-p ec)
256 (progn
257 ;; Desymlinkify
258 (rename-file ec (concat ec ".tmp"))
259 (copy-file (concat ec ".tmp") ec)
260 (delete-file (concat ec ".tmp"))))
261 (set-buffer (find-file-noselect ec t))
262 (ede-proj-elisp-add-path "..")
263 (let ((paths (ede-proj-elisp-packages-to-loadpath
264 (oref this aux-packages))))
265 ;; Add in the current list of paths
266 (while paths
267 (ede-proj-elisp-add-path (car paths))
268 (setq paths (cdr paths))))
269 (save-buffer)) )))
270
271 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
272 "Flush the configure file (current buffer) to accommodate THIS."
273 ;; Remove crufty old paths from elisp-compile
274 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
275 (enable-local-variables nil))
276 (if (and ec (file-exists-p ec))
277 (with-current-buffer (find-file-noselect ec t)
278 (goto-char (point-min))
279 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
280 nil t)
281 (let ((path (match-string 1)))
282 (if (string= path "nil")
283 nil
284 (delete-region (point-at-bol) (point-at-bol 2)))))))))
285
286 ;;;
287 ;; Autoload generators
288 ;;
289 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
290 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
291 (phony :initform t)
292 (rules :initform nil)
293 (autoload-file :initarg :autoload-file
294 :initform "loaddefs.el"
295 :type string
296 :custom string
297 :documentation "The file that autoload definitions are placed in.
298 There should be one load defs file for a given package. The load defs are created
299 for all Emacs Lisp sources that exist in the directory of the created target.")
300 (autoload-dirs :initarg :autoload-dirs
301 :initform nil
302 :type list
303 :custom (repeat string)
304 :documentation "The directories to scan for autoload definitions.
305 If nil defaults to the current directory.")
306 )
307 "Target that builds an autoload file.
308 Files do not need to be added to this target.")
309
310
311 ;;; Claiming files
312 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
313 "Return t if object THIS lays claim to the file in BUFFER.
314 Lays claim to all .elc files that match .el files in this target."
315 (if (string-match
316 (concat (regexp-quote (oref this autoload-file)) "$")
317 (buffer-file-name buffer))
318 t
319 (call-next-method) ; The usual thing.
320 ))
321
322 ;; Compilers
323 (defvar ede-emacs-cedet-autogen-compiler
324 (ede-compiler
325 "ede-emacs-autogen-compiler"
326 :name "emacs"
327 :variables '(("EMACS" . "emacs")
328 ("EMACSFLAGS" . "-batch --no-site-file --eval '(setq debug-on-error t)'")
329 ("require" . "$(foreach r,$(1),(require (quote $(r))))"))
330 :commands
331 '("$(EMACS) $(EMACSFLAGS) $(addprefix -L ,$(LOADPATH)) \
332 --eval '(setq generated-autoload-file \"$(abspath $(LOADDEFS))\")' \
333 -f batch-update-autoloads $(abspath $(LOADDIRS))")
334 :rules (list (ede-makefile-rule "clean-autoloads" :target "clean-autoloads" :phony t :rules '("rm -f $(LOADDEFS)")))
335 :sourcetype '(ede-source-emacs)
336 )
337 "Build an autoloads file.")
338
339 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
340 "List of compilers being used by OBJ.
341 If the `compiler' slot is empty, get the car of the compilers list."
342 (let ((comp (oref obj compiler)))
343 (if comp
344 (if (listp comp)
345 (setq comp (mapcar 'symbol-value comp))
346 (setq comp (list (symbol-value comp))))
347 ;; Get the first element from our list of compilers.
348 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
349 (setq comp (list (car avail)))))
350 comp))
351
352 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
353 &optional
354 moresource)
355 "Insert the source variables needed by THIS.
356 Optional argument MORESOURCE is a list of additional sources to add to the
357 sources variable."
358 nil)
359
360 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
361 "Return the variable name for THIS's sources."
362 nil) ; "LOADDEFS")
363
364 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
365 "Return a string representing the dependencies for THIS.
366 Always return an empty string for an autoloads generator."
367 "")
368
369 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
370 "Insert variables needed by target THIS."
371 (ede-pmake-insert-variable-shared "LOADDEFS"
372 (insert (oref this autoload-file)))
373 (ede-pmake-insert-variable-shared "LOADDIRS"
374 (insert (mapconcat 'identity
375 (or (oref this autoload-dirs) '("."))
376 " ")))
377 )
378
379 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
380 "Create or update the autoload target."
381 (require 'cedet-autogen)
382 (let ((default-directory (ede-expand-filename obj ".")))
383 (apply 'cedet-update-autoloads
384 (oref obj autoload-file)
385 (oref obj autoload-dirs))
386 ))
387
388 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
389 "In a Lisp file, updated a version string for THIS to VERSION.
390 There are standards in Elisp files specifying how the version string
391 is found, such as a `-version' variable, or the standard header."
392 nil)
393
394 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
395 "Insert any symbols that the DIST rule should depend on.
396 Emacs Lisp autoload files ship the generated .el files.
397 Argument THIS is the target which needs to insert an info file."
398 ;; In some cases, this is ONLY the index file. That should generally
399 ;; be ok.
400 (insert " " (ede-proj-makefile-target-name this))
401 )
402
403 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
404 "Insert any symbols that the DIST rule should distribute.
405 Emacs Lisp autoload files ship the generated .el files.
406 Argument THIS is the target which needs to insert an info file."
407 (insert " " (oref this autoload-file))
408 )
409
410 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
411 "Tweak the configure file (current buffer) to accommodate THIS."
412 (error "Autoloads not supported in autoconf yet"))
413
414 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
415 "Flush the configure file (current buffer) to accommodate THIS."
416 nil)
417
418 (provide 'ede/proj-elisp)
419
420 ;;; ede/proj-elisp.el ends here