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