proj-elisp.el 13 KB

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