linux.el 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ;;; ede/linux.el --- Special project for Linux
  2. ;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Provide a special project type just for Linux, cause Linux is special.
  18. ;;
  19. ;; Identifies a Linux project automatically.
  20. ;; Speedy ede-expand-filename based on extension.
  21. ;; Pre-populates the preprocessor map from lisp.h
  22. ;;
  23. ;; ToDo :
  24. ;; * Add "build" options.
  25. ;; * Add texinfo lookup options.
  26. ;; * Add website
  27. (require 'ede)
  28. (declare-function semanticdb-file-table-object "semantic/db")
  29. (declare-function semanticdb-needs-refresh-p "semantic/db")
  30. (declare-function semanticdb-refresh-table "semantic/db")
  31. ;;; Code:
  32. (defvar ede-linux-project-list nil
  33. "List of projects created by option `ede-linux-project'.")
  34. (defun ede-linux-file-existing (dir)
  35. "Find a Linux project in the list of Linux projects.
  36. DIR is the directory to search from."
  37. (let ((projs ede-linux-project-list)
  38. (ans nil))
  39. (while (and projs (not ans))
  40. (let ((root (ede-project-root-directory (car projs))))
  41. (when (string-match (concat "^" (regexp-quote root)) dir)
  42. (setq ans (car projs))))
  43. (setq projs (cdr projs)))
  44. ans))
  45. ;;;###autoload
  46. (defun ede-linux-project-root (&optional dir)
  47. "Get the root directory for DIR."
  48. (when (not dir) (setq dir default-directory))
  49. (let ((case-fold-search t)
  50. (proj (ede-linux-file-existing dir)))
  51. (if proj
  52. (ede-up-directory (file-name-directory
  53. (oref proj :file)))
  54. ;; No pre-existing project. Let's take a wild-guess if we have
  55. ;; an Linux project here.
  56. (when (string-match "linux[^/]*" dir)
  57. (let ((base (substring dir 0 (match-end 0))))
  58. (when (file-exists-p (expand-file-name "scripts/ver_linux" base))
  59. base))))))
  60. (defun ede-linux-version (dir)
  61. "Find the Linux version for the Linux src in DIR."
  62. (let ((buff (get-buffer-create " *linux-query*")))
  63. (with-current-buffer buff
  64. (erase-buffer)
  65. (setq default-directory (file-name-as-directory dir))
  66. (insert-file-contents "Makefile" nil 0 512)
  67. (goto-char (point-min))
  68. (let (major minor sub)
  69. (re-search-forward "^VERSION *= *\\([0-9.]+\\)")
  70. (setq major (match-string 1))
  71. (re-search-forward "^PATCHLEVEL *= *\\([0-9.]+\\)")
  72. (setq minor (match-string 1))
  73. (re-search-forward "^SUBLEVEL *= *\\([0-9.]+\\)")
  74. (setq sub (match-string 1))
  75. (prog1
  76. (concat major "." minor "." sub)
  77. (kill-buffer buff)
  78. )))))
  79. (defclass ede-linux-project (ede-project eieio-instance-tracker)
  80. ((tracking-symbol :initform 'ede-linux-project-list)
  81. )
  82. "Project Type for the Linux source code."
  83. :method-invocation-order :depth-first)
  84. (defun ede-linux-load (dir &optional rootproj)
  85. "Return an Linux Project object if there is a match.
  86. Return nil if there isn't one.
  87. Argument DIR is the directory it is created for.
  88. ROOTPROJ is nil, since there is only one project."
  89. (or (ede-linux-file-existing dir)
  90. ;; Doesn't already exist, so let's make one.
  91. (ede-linux-project "Linux"
  92. :name "Linux"
  93. :version (ede-linux-version dir)
  94. :directory (file-name-as-directory dir)
  95. :file (expand-file-name "scripts/ver_linux"
  96. dir))
  97. (ede-add-project-to-global-list this)
  98. )
  99. )
  100. ;;;###autoload
  101. (add-to-list 'ede-project-class-files
  102. (ede-project-autoload "linux"
  103. :name "LINUX ROOT"
  104. :file 'ede/linux
  105. :proj-file "scripts/ver_linux"
  106. :proj-root 'ede-linux-project-root
  107. :load-type 'ede-linux-load
  108. :class-sym 'ede-linux-project
  109. :new-p nil)
  110. t)
  111. (defclass ede-linux-target-c (ede-target)
  112. ()
  113. "EDE Linux Project target for C code.
  114. All directories need at least one target.")
  115. (defclass ede-linux-target-misc (ede-target)
  116. ()
  117. "EDE Linux Project target for Misc files.
  118. All directories need at least one target.")
  119. (defmethod initialize-instance ((this ede-linux-project)
  120. &rest fields)
  121. "Make sure the targets slot is bound."
  122. (call-next-method)
  123. (unless (slot-boundp this 'targets)
  124. (oset this :targets nil)))
  125. ;;; File Stuff
  126. ;;
  127. (defmethod ede-project-root-directory ((this ede-linux-project)
  128. &optional file)
  129. "Return the root for THIS Linux project with file."
  130. (ede-up-directory (file-name-directory (oref this file))))
  131. (defmethod ede-project-root ((this ede-linux-project))
  132. "Return my root."
  133. this)
  134. (defmethod ede-find-subproject-for-directory ((proj ede-linux-project)
  135. dir)
  136. "Return PROJ, for handling all subdirs below DIR."
  137. proj)
  138. ;;; TARGET MANAGEMENT
  139. ;;
  140. (defun ede-linux-find-matching-target (class dir targets)
  141. "Find a target that is a CLASS and is in DIR in the list of TARGETS."
  142. (let ((match nil))
  143. (dolist (T targets)
  144. (when (and (object-of-class-p T class)
  145. (string= (oref T :path) dir))
  146. (setq match T)
  147. ))
  148. match))
  149. (defmethod ede-find-target ((proj ede-linux-project) buffer)
  150. "Find an EDE target in PROJ for BUFFER.
  151. If one doesn't exist, create a new one for this directory."
  152. (let* ((ext (file-name-extension (buffer-file-name buffer)))
  153. (cls (cond ((not ext)
  154. 'ede-linux-target-misc)
  155. ((string-match "c\\|h" ext)
  156. 'ede-linux-target-c)
  157. (t 'ede-linux-target-misc)))
  158. (targets (oref proj targets))
  159. (dir default-directory)
  160. (ans (ede-linux-find-matching-target cls dir targets))
  161. )
  162. (when (not ans)
  163. (setq ans (make-instance
  164. cls
  165. :name (file-name-nondirectory
  166. (directory-file-name dir))
  167. :path dir
  168. :source nil))
  169. (object-add-to-list proj :targets ans)
  170. )
  171. ans))
  172. ;;; UTILITIES SUPPORT.
  173. ;;
  174. (defmethod ede-preprocessor-map ((this ede-linux-target-c))
  175. "Get the pre-processor map for Linux C code.
  176. All files need the macros from lisp.h!"
  177. (require 'semantic/db)
  178. (let* ((proj (ede-target-parent this))
  179. (root (ede-project-root proj))
  180. (versionfile (ede-expand-filename root "include/linux/version.h"))
  181. (table (when (and versionfile (file-exists-p versionfile))
  182. (semanticdb-file-table-object versionfile)))
  183. (filemap '( ("__KERNEL__" . "")
  184. ))
  185. )
  186. (when table
  187. (when (semanticdb-needs-refresh-p table)
  188. (semanticdb-refresh-table table))
  189. (setq filemap (append filemap (oref table lexical-table)))
  190. )
  191. filemap
  192. ))
  193. (defun ede-linux-file-exists-name (name root subdir)
  194. "Return a file name if NAME exists under ROOT with SUBDIR in between."
  195. (let ((F (expand-file-name name (expand-file-name subdir root))))
  196. (when (file-exists-p F) F)))
  197. (defmethod ede-expand-filename-impl ((proj ede-linux-project) name)
  198. "Within this project PROJ, find the file NAME.
  199. Knows about how the Linux source tree is organized."
  200. (let* ((ext (file-name-extension name))
  201. (root (ede-project-root proj))
  202. (dir (ede-project-root-directory root))
  203. (F (cond
  204. ((not ext) nil)
  205. ((string-match "h" ext)
  206. (or (ede-linux-file-exists-name name dir "")
  207. (ede-linux-file-exists-name name dir "include"))
  208. )
  209. ((string-match "txt" ext)
  210. (ede-linux-file-exists-name name dir "Documentation"))
  211. (t nil)))
  212. )
  213. (or F (call-next-method))))
  214. (provide 'ede/linux)
  215. ;; Local variables:
  216. ;; generated-autoload-file: "loaddefs.el"
  217. ;; generated-autoload-load-name: "ede/linux"
  218. ;; End:
  219. ;;; ede/linux.el ends here