Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / ede / proj-elisp.el
blobf27ae8adf74998699736593dd0eccbfe45031ee8
1 ;;; ede-proj-elisp.el --- EDE Generic Project Emacs Lisp support
3 ;; Copyright (C) 1998-2005, 2007-2014 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/>.
23 ;;; Commentary:
25 ;; Handle Emacs Lisp in an EDE Project file.
27 (require 'ede/proj)
28 (require 'ede/pmake)
29 (require 'ede/pconf)
31 (autoload 'semantic-ede-proj-target-grammar "semantic/ede-grammar")
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."
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.")
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.")
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"))
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)))
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.")
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"
103 "Compile Emacs Lisp programs.")
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.")
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")
121 ;; Is this in our list.
122 (member fname (oref this auxsource))
124 (call-next-method) ; The usual thing.
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)
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))
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)))
168 (if (eq (byte-recompile-file fsrc nil 0) t)
169 (setq comp (1+ comp))
170 (setq utd (1+ utd)))))))
172 (oref obj source))
173 (message "All Emacs Lisp sources are up to date in %s" (eieio-object-name obj))
174 (cons comp utd)))
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 ;; The next method will include comments such as "Version:"
198 (call-next-method))))
201 ;;; Makefile generation functions
203 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp))
204 "Return the variable name for THIS's sources."
205 (cond ((ede-proj-automake-p) '("lisp_LISP" . share))
206 (t (concat (ede-pmake-varname this) "_LISP"))))
208 (defun ede-proj-makefile-insert-loadpath-items (items)
209 "Insert a sequence of ITEMS into the Makefile LOADPATH variable."
210 (when items
211 (ede-pmake-insert-variable-shared "LOADPATH"
212 (let ((begin (save-excursion (re-search-backward "\\s-*="))))
213 (while items
214 (when (not (save-excursion
215 (re-search-backward
216 (concat "\\s-" (regexp-quote (car items)) "[ \n\t\\]")
217 begin t)))
218 (insert " " (car items)))
219 (setq items (cdr items)))))
222 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp))
223 "Insert variables needed by target THIS."
224 (let ((newitems (if (oref this aux-packages)
225 (ede-proj-elisp-packages-to-loadpath
226 (oref this aux-packages)))))
227 (ede-proj-makefile-insert-loadpath-items newitems)))
229 (defun ede-proj-elisp-add-path (path)
230 "Add path PATH into the file if it isn't already there."
231 (goto-char (point-min))
232 (if (re-search-forward (concat "(cons \\\""
233 (regexp-quote path))
234 nil t)
235 nil;; We have it already
236 (if (re-search-forward "(cons nil" nil t)
237 (progn
238 ;; insert stuff here
239 (end-of-line)
240 (insert "\n"
241 " echo \"(setq load-path (cons \\\""
242 path
243 "\\\" load-path))\" >> script")
245 (error "Don't know how to update load path"))))
247 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp))
248 "Tweak the configure file (current buffer) to accommodate THIS."
249 (call-next-method)
250 ;; Ok, now we have to tweak the autoconf provided `elisp-comp' program.
251 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
252 (enable-local-variables nil))
253 (if (or (not ec) (not (file-exists-p ec)))
254 (message "No elisp-comp file. There may be compile errors? Rerun a second time.")
255 (save-excursion
256 (if (file-symlink-p ec)
257 (progn
258 ;; Change symlinks to copies.
259 (rename-file ec (concat ec ".tmp"))
260 (copy-file (concat ec ".tmp") ec)
261 (delete-file (concat ec ".tmp"))))
262 (set-buffer (find-file-noselect ec t))
263 (ede-proj-elisp-add-path "..")
264 (let ((paths (ede-proj-elisp-packages-to-loadpath
265 (oref this aux-packages))))
266 ;; Add in the current list of paths
267 (while paths
268 (ede-proj-elisp-add-path (car paths))
269 (setq paths (cdr paths))))
270 (save-buffer)) )))
272 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp))
273 "Flush the configure file (current buffer) to accommodate THIS."
274 ;; Remove crufty old paths from elisp-compile
275 (let ((ec (ede-expand-filename this "elisp-comp" 'newfile))
276 (enable-local-variables nil))
277 (if (and ec (file-exists-p ec))
278 (with-current-buffer (find-file-noselect ec t)
279 (goto-char (point-min))
280 (while (re-search-forward "(cons \\([^ ]+\\) load-path)"
281 nil t)
282 (let ((path (match-string 1)))
283 (if (string= path "nil")
285 (delete-region (point-at-bol) (point-at-bol 2)))))))))
288 ;; Autoload generators
290 (defclass ede-proj-target-elisp-autoloads (ede-proj-target-elisp)
291 ((availablecompilers :initform '(ede-emacs-cedet-autogen-compiler))
292 (phony :initform t)
293 (rules :initform nil)
294 (autoload-file :initarg :autoload-file
295 :initform "loaddefs.el"
296 :type string
297 :custom string
298 :documentation "The file that autoload definitions are placed in.
299 There should be one load defs file for a given package. The load defs are created
300 for all Emacs Lisp sources that exist in the directory of the created target.")
301 (autoload-dirs :initarg :autoload-dirs
302 :initform nil
303 :type list
304 :custom (repeat string)
305 :documentation "The directories to scan for autoload definitions.
306 If nil defaults to the current directory.")
308 "Target that builds an autoload file.
309 Files do not need to be added to this target.")
312 ;;; Claiming files
313 (defmethod ede-buffer-mine ((this ede-proj-target-elisp-autoloads) buffer)
314 "Return t if object THIS lays claim to the file in BUFFER.
315 Lays claim to all .elc files that match .el files in this target."
316 (if (string-match
317 (concat (regexp-quote (oref this autoload-file)) "$")
318 (buffer-file-name buffer))
320 (call-next-method) ; The usual thing.
323 ;; Compilers
324 (defvar ede-emacs-cedet-autogen-compiler
325 (ede-compiler
326 "ede-emacs-autogen-compiler"
327 :name "emacs"
328 :variables '(("EMACS" . "emacs")
329 ("EMACSFLAGS" . "-batch --no-site-file --eval '(setq debug-on-error t)'")
330 ("require" . "$(foreach r,$(1),(require (quote $(r))))"))
331 :commands
332 '("$(EMACS) $(EMACSFLAGS) $(addprefix -L ,$(LOADPATH)) \
333 --eval '(setq generated-autoload-file \"$(abspath $(LOADDEFS))\")' \
334 -f batch-update-autoloads $(abspath $(LOADDIRS))")
335 :rules (list (ede-makefile-rule "clean-autoloads" :target "clean-autoloads" :phony t :rules '("rm -f $(LOADDEFS)")))
336 :sourcetype '(ede-source-emacs)
338 "Build an autoloads file.")
340 (defmethod ede-proj-compilers ((obj ede-proj-target-elisp-autoloads))
341 "List of compilers being used by OBJ.
342 If the `compiler' slot is empty, get the car of the compilers list."
343 (let ((comp (oref obj compiler)))
344 (if comp
345 (if (listp comp)
346 (setq comp (mapcar 'symbol-value comp))
347 (setq comp (list (symbol-value comp))))
348 ;; Get the first element from our list of compilers.
349 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers))))
350 (setq comp (list (car avail)))))
351 comp))
353 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target-elisp-autoloads)
354 &optional
355 moresource)
356 "Insert the source variables needed by THIS.
357 Optional argument MORESOURCE is a list of additional sources to add to the
358 sources variable."
359 nil)
361 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target-elisp-autoloads))
362 "Return the variable name for THIS's sources."
363 nil) ; "LOADDEFS")
365 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-elisp-autoloads))
366 "Return a string representing the dependencies for THIS.
367 Always return an empty string for an autoloads generator."
370 (defmethod ede-proj-makefile-insert-variables :AFTER ((this ede-proj-target-elisp-autoloads))
371 "Insert variables needed by target THIS."
372 (ede-pmake-insert-variable-shared "LOADDEFS"
373 (insert (oref this autoload-file)))
374 (ede-pmake-insert-variable-shared "LOADDIRS"
375 (insert (mapconcat 'identity
376 (or (oref this autoload-dirs) '("."))
377 " ")))
380 (defmethod project-compile-target ((obj ede-proj-target-elisp-autoloads))
381 "Create or update the autoload target."
382 (require 'cedet-autogen)
383 (let ((default-directory (ede-expand-filename obj ".")))
384 (apply 'cedet-update-autoloads
385 (oref obj autoload-file)
386 (oref obj autoload-dirs))
389 (defmethod ede-update-version-in-source ((this ede-proj-target-elisp-autoloads) version)
390 "In a Lisp file, updated a version string for THIS to VERSION.
391 There are standards in Elisp files specifying how the version string
392 is found, such as a `-version' variable, or the standard header."
393 nil)
395 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target-elisp-autoloads))
396 "Insert any symbols that the DIST rule should depend on.
397 Emacs Lisp autoload files ship the generated .el files.
398 Argument THIS is the target which needs to insert an info file."
399 ;; In some cases, this is ONLY the index file. That should generally
400 ;; be ok.
401 (insert " " (ede-proj-makefile-target-name this))
404 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target-elisp-autoloads))
405 "Insert any symbols that the DIST rule should distribute.
406 Emacs Lisp autoload files ship the generated .el files.
407 Argument THIS is the target which needs to insert an info file."
408 (insert " " (oref this autoload-file))
411 (defmethod ede-proj-tweak-autoconf ((this ede-proj-target-elisp-autoloads))
412 "Tweak the configure file (current buffer) to accommodate THIS."
413 (error "Autoloads not supported in autoconf yet"))
415 (defmethod ede-proj-flush-autoconf ((this ede-proj-target-elisp-autoloads))
416 "Flush the configure file (current buffer) to accommodate THIS."
417 nil)
419 (provide 'ede/proj-elisp)
421 ;;; ede/proj-elisp.el ends here