1 ;;; ede/linux.el --- Special project for Linux
3 ;; Copyright (C) 2008, 2009 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs 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 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs 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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; Provide a special project type just for Linux, cause Linux is special.
26 ;; Identifies a Linux project automatically.
27 ;; Speedy ede-expand-filename based on extension.
28 ;; Pre-populates the preprocessor map from lisp.h
31 ;; * Add "build" options.
32 ;; * Add texinfo lookup options.
36 (declare-function semanticdb-file-table-object
"semantic/db")
37 (declare-function semanticdb-needs-refresh-p
"semantic/db")
38 (declare-function semanticdb-refresh-table
"semantic/db")
41 (defvar ede-linux-project-list nil
42 "List of projects created by option `ede-linux-project'.")
44 (defun ede-linux-file-existing (dir)
45 "Find a Linux project in the list of Linux projects.
46 DIR is the directory to search from."
47 (let ((projs ede-linux-project-list
)
49 (while (and projs
(not ans
))
50 (let ((root (ede-project-root-directory (car projs
))))
51 (when (string-match (concat "^" (regexp-quote root
)) dir
)
52 (setq ans
(car projs
))))
53 (setq projs
(cdr projs
)))
57 (defun ede-linux-project-root (&optional dir
)
58 "Get the root directory for DIR."
59 (when (not dir
) (setq dir default-directory
))
60 (let ((case-fold-search t
)
61 (proj (ede-linux-file-existing dir
)))
63 (ede-up-directory (file-name-directory
65 ;; No pre-existing project. Lets take a wild-guess if we have
66 ;; an Linux project here.
67 (when (string-match "linux[^/]*" dir
)
68 (let ((base (substring dir
0 (match-end 0))))
69 (when (file-exists-p (expand-file-name "scripts/ver_linux" base
))
72 (defun ede-linux-version (dir)
73 "Find the Linux version for the Linux src in DIR."
74 (let ((buff (get-buffer-create " *linux-query*")))
78 (setq default-directory
(file-name-as-directory dir
))
79 (insert-file-contents "Makefile" nil
0 512)
80 (goto-char (point-min))
81 (let (major minor sub
)
82 (re-search-forward "^VERSION *= *\\([0-9.]+\\)")
83 (setq major
(match-string 1))
84 (re-search-forward "^PATCHLEVEL *= *\\([0-9.]+\\)")
85 (setq minor
(match-string 1))
86 (re-search-forward "^SUBLEVEL *= *\\([0-9.]+\\)")
87 (setq sub
(match-string 1))
89 (concat major
"." minor
"." sub
)
93 (defclass ede-linux-project
(ede-project eieio-instance-tracker
)
94 ((tracking-symbol :initform
'ede-linux-project-list
)
96 "Project Type for the Linux source code."
97 :method-invocation-order
:depth-first
)
99 (defun ede-linux-load (dir &optional rootproj
)
100 "Return an Linux Project object if there is a match.
101 Return nil if there isn't one.
102 Argument DIR is the directory it is created for.
103 ROOTPROJ is nil, since there is only one project."
104 (or (ede-linux-file-existing dir
)
105 ;; Doesn't already exist, so lets make one.
106 (ede-linux-project "Linux"
108 :version
(ede-linux-version dir
)
109 :directory
(file-name-as-directory dir
)
110 :file
(expand-file-name "scripts/ver_linux"
112 (ede-add-project-to-global-list this
)
116 (defclass ede-linux-target-c
(ede-target)
118 "EDE Linux Project target for C code.
119 All directories need at least one target.")
121 (defclass ede-linux-target-misc
(ede-target)
123 "EDE Linux Project target for Misc files.
124 All directories need at least one target.")
126 (defmethod initialize-instance ((this ede-linux-project
)
128 "Make sure the :file is fully expanded."
130 (unless (slot-boundp this
'targets
)
131 (oset this
:targets nil
)))
135 (defmethod ede-project-root-directory ((this ede-linux-project
)
137 "Return the root for THIS Linux project with file."
138 (ede-up-directory (file-name-directory (oref this file
))))
140 (defmethod ede-project-root ((this ede-linux-project
))
144 (defmethod ede-find-subproject-for-directory ((proj ede-linux-project
)
146 "Return PROJ, for handling all subdirs below DIR."
149 ;;; TARGET MANAGEMENT
151 (defun ede-linux-find-matching-target (class dir targets
)
152 "Find a target that is a CLASS and is in DIR in the list of TARGETS."
155 (when (and (object-of-class-p T class
)
156 (string= (oref T
:path
) dir
))
161 (defmethod ede-find-target ((proj ede-linux-project
) buffer
)
162 "Find an EDE target in PROJ for BUFFER.
163 If one doesn't exist, create a new one for this directory."
164 (let* ((ext (file-name-extension (buffer-file-name buffer
)))
165 (cls (cond ((not ext
)
166 'ede-linux-target-misc
)
167 ((string-match "c\\|h" ext
)
169 (t 'ede-linux-target-misc
)))
170 (targets (oref proj targets
))
171 (dir default-directory
)
172 (ans (ede-linux-find-matching-target cls dir targets
))
175 (setq ans
(make-instance
177 :name
(file-name-nondirectory
178 (directory-file-name dir
))
181 (object-add-to-list proj
:targets ans
)
185 ;;; UTILITIES SUPPORT.
187 (defmethod ede-preprocessor-map ((this ede-linux-target-c
))
188 "Get the pre-processor map for Linux C code.
189 All files need the macros from lisp.h!"
190 (require 'semantic
/db
)
191 (let* ((proj (ede-target-parent this
))
192 (root (ede-project-root proj
))
193 (versionfile (ede-expand-filename root
"include/linux/version.h"))
194 (table (when (and versionfile
(file-exists-p versionfile
))
195 (semanticdb-file-table-object versionfile
)))
196 (filemap '( ("__KERNEL__" .
"")
200 (when (semanticdb-needs-refresh-p table
)
201 (semanticdb-refresh-table table
))
202 (setq filemap
(append filemap
(oref table lexical-table
)))
207 (defun ede-linux-file-exists-name (name root subdir
)
208 "Return a file name if NAME exists under ROOT with SUBDIR in between."
209 (let ((F (expand-file-name name
(expand-file-name subdir root
))))
210 (when (file-exists-p F
) F
)))
212 (defmethod ede-expand-filename-impl ((proj ede-linux-project
) name
)
213 "Within this project PROJ, find the file NAME.
214 Knows about how the Linux source tree is organized."
215 (let* ((ext (file-name-extension name
))
216 (root (ede-project-root proj
))
217 (dir (ede-project-root-directory root
))
220 ((string-match "h" ext
)
221 (or (ede-linux-file-exists-name name dir
"")
222 (ede-linux-file-exists-name name dir
"include"))
224 ((string-match "txt" ext
)
225 (ede-linux-file-exists-name name dir
"Documentation"))
228 (or F
(call-next-method))))
233 ;; generated-autoload-file: "loaddefs.el"
234 ;; generated-autoload-feature: ede/loaddefs
235 ;; generated-autoload-load-name: "ede/linux"
238 ;; arch-tag: 41f310c8-b169-4259-8a2d-0ff4bd0a736d
239 ;;; ede/linux.el ends here