cpp-root.el 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. ;;; ede/cpp-root.el --- A simple way to wrap a C++ project with a single root
  2. ;; Copyright (C) 2007-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. ;; NOTE: ede/cpp-root.el has been commented so as to also make it
  18. ;; useful for learning how to make similar project types.
  19. ;;
  20. ;; Not everyone can use automake, or an EDE project type. For
  21. ;; pre-existing code, it is often helpful jut to be able to wrap the
  22. ;; whole thing up in as simple a way as possible.
  23. ;;
  24. ;; The cpp-root project type will allow you to create a single object
  25. ;; with no save-file in your .emacs file that will be recognized, and
  26. ;; provide a way to easily allow EDE to provide Semantic with the
  27. ;; ability to find header files, and other various source files
  28. ;; quickly.
  29. ;;
  30. ;; The cpp-root class knows a few things about C++ projects, such as
  31. ;; the prevalence of "include" directories, and typical file-layout
  32. ;; stuff. If this isn't sufficient, you can subclass
  33. ;; `ede-cpp-root-project' and add your own tweaks in just a few lines.
  34. ;; See the end of this file for an example.
  35. ;;
  36. ;;; EXAMPLE
  37. ;;
  38. ;; Add this to your .emacs file, modifying appropriate bits as needed.
  39. ;;
  40. ;; (ede-cpp-root-project "SOMENAME" :file "/dir/to/some/file")
  41. ;;
  42. ;; Replace SOMENAME with whatever name you want, and the filename to
  43. ;; an actual file at the root of your project. It might be a
  44. ;; Makefile, a README file. Whatever. It doesn't matter. It's just
  45. ;; a key to hang the rest of EDE off of.
  46. ;;
  47. ;; The most likely reason to create this project, is to help make
  48. ;; finding files within the project faster. In conjunction with
  49. ;; Semantic completion, having a short include path is key. You can
  50. ;; override the include path like this:
  51. ;;
  52. ;; (ede-cpp-root-project "NAME" :file "FILENAME"
  53. ;; :include-path '( "/include" "../include" "/c/include" )
  54. ;; :system-include-path '( "/usr/include/c++/3.2.2/" )
  55. ;; :spp-table '( ("MOOSE" . "")
  56. ;; ("CONST" . "const") )
  57. ;; :spp-files '( "include/config.h" )
  58. ;; )
  59. ;;
  60. ;; In this case each item in the include path list is searched. If
  61. ;; the directory starts with "/", then that expands to the project
  62. ;; root directory. If a directory does not start with "/", then it
  63. ;; is relative to the default-directory of the current buffer when
  64. ;; the file name is expanded.
  65. ;;
  66. ;; The include path only affects C/C++ header files. Use the slot
  67. ;; :header-match-regexp to change it.
  68. ;;
  69. ;; The :system-include-path allows you to specify full directory
  70. ;; names to include directories where system header files can be
  71. ;; found. These will be applied to files in this project only.
  72. ;;
  73. ;; The :spp-table provides a list of project specific #define style
  74. ;; macros that are unique to this project, passed in to the compiler
  75. ;; on the command line, or are in special headers.
  76. ;;
  77. ;; The :spp-files option is like :spp-table, except you can provide a
  78. ;; file name for a header in your project where most of your CPP
  79. ;; macros reside. Doing this can be easier than listing everything in
  80. ;; the :spp-table option. The files listed in :spp-files should not
  81. ;; start with a /, and are relative to something in :include-path.;;
  82. ;;
  83. ;; If you want to override the file-finding tool with your own
  84. ;; function you can do this:
  85. ;;
  86. ;; (ede-cpp-root-project "NAME" :file "FILENAME" :locate-fcn 'MYFCN)
  87. ;;
  88. ;; Where FILENAME is a file in the root directory of the project.
  89. ;; Where MYFCN is a symbol for a function. See:
  90. ;;
  91. ;; M-x describe-class RET ede-cpp-root-project RET
  92. ;;
  93. ;; for documentation about the locate-fcn extension.
  94. ;;
  95. ;;; ADVANCED EXAMPLE
  96. ;;
  97. ;; If the cpp-root project style is right for you, but you want a
  98. ;; dynamic loader, instead of hard-coding values in your .emacs, you
  99. ;; can do that too, but you will need to write some lisp code.
  100. ;;
  101. ;; To do that, you need to add an entry to the
  102. ;; `ede-project-class-files' list, and also provide two functions to
  103. ;; teach EDE how to load your project pattern
  104. ;;
  105. ;; It would look like this:
  106. ;;
  107. ;; (defun MY-FILE-FOR-DIR (&optional dir)
  108. ;; "Return a full file name to the project file stored in DIR."
  109. ;; <write your code here, or return nil>
  110. ;; )
  111. ;;
  112. ;; (defun MY-ROOT-FCN ()
  113. ;; "Return the root directory for `default-directory'"
  114. ;; ;; You might be able to use `ede-cpp-root-project-root'.
  115. ;; )
  116. ;;
  117. ;; (defun MY-LOAD (dir)
  118. ;; "Load a project of type `cpp-root' for the directory DIR.
  119. ;; Return nil if there isn't one."
  120. ;; (ede-cpp-root-project "NAME" :file (expand-file-name "FILE" dir)
  121. ;; :locate-fcn 'MYFCN)
  122. ;; )
  123. ;;
  124. ;; (add-to-list 'ede-project-class-files
  125. ;; (ede-project-autoload "cpp-root"
  126. ;; :name "CPP ROOT"
  127. ;; :file 'ede/cpp-root
  128. ;; :proj-file 'MY-FILE-FOR-DIR
  129. ;; :proj-root 'MY-ROOT-FCN
  130. ;; :load-type 'MY-LOAD
  131. ;; :class-sym 'ede-cpp-root)
  132. ;; t)
  133. ;;
  134. ;;; TODO
  135. ;;
  136. ;; Need a way to reconfigure a project, and have it affect all open buffers.
  137. ;; From Tobias Gerdin:
  138. ;;
  139. ;; >>3) Is there any way to refresh a ede-cpp-root-project dynamically? I have
  140. ;; >>some file open part of the project, fiddle with the include paths and would
  141. ;; >>like the open buffer to notice this when I re-evaluate the
  142. ;; >>ede-cpp-root-project constructor.
  143. ;; >
  144. ;; > Another good idea. The easy way is to "revert-buffer" as needed. The
  145. ;; > ede "project local variables" does this already, so it should be easy
  146. ;; > to adapt something.
  147. ;;
  148. ;; I actually tried reverting the buffer but Semantic did not seem to pick
  149. ;; up the differences (the "include summary" reported the same include paths).
  150. (require 'ede)
  151. (defvar semantic-lex-spp-project-macro-symbol-obarray)
  152. (declare-function semantic-lex-make-spp-table "semantic/lex-spp")
  153. (declare-function semanticdb-file-table-object "semantic/db")
  154. (declare-function semanticdb-needs-refresh-p "semantic/db")
  155. (declare-function semanticdb-refresh-table "semantic/db")
  156. ;;; Code:
  157. ;;; PROJECT CACHE:
  158. ;;
  159. ;; cpp-root projects are created in a .emacs or other config file, but
  160. ;; there still needs to be a way for a particular file to be
  161. ;; identified against it. The cache is where we look to map a file
  162. ;; against a project.
  163. ;;
  164. ;; Setting up a simple in-memory cache of active projects allows the
  165. ;; user to re-load their configuration file several times without
  166. ;; messing up the active project set.
  167. ;;
  168. (defvar ede-cpp-root-project-list nil
  169. "List of projects created by option `ede-cpp-root-project'.")
  170. (defun ede-cpp-root-file-existing (dir)
  171. "Find a cpp-root project in the list of cpp-root projects.
  172. DIR is the directory to search from."
  173. (let ((projs ede-cpp-root-project-list)
  174. (ans nil))
  175. (while (and projs (not ans))
  176. (let ((root (ede-project-root-directory (car projs))))
  177. (when (string-match (concat "^" (regexp-quote root)) dir)
  178. (setq ans (car projs))))
  179. (setq projs (cdr projs)))
  180. ans))
  181. ;;; PROJECT AUTOLOAD CONFIG
  182. ;;
  183. ;; Each project type registers itself into the project-class list.
  184. ;; This way, each time a file is loaded, EDE can map that file to a
  185. ;; project. This project type checks files against the internal cache
  186. ;; of projects created by the user.
  187. ;;
  188. ;; EDE asks two kinds of questions. One is, does this DIR belong to a
  189. ;; project. If it does, it then asks, what is the ROOT directory to
  190. ;; the project in DIR. This is easy for cpp-root projects, but more
  191. ;; complex for multiply nested projects.
  192. ;;
  193. ;; If EDE finds out that a project exists for DIR, it then loads that
  194. ;; project. The LOAD routine can either create a new project object
  195. ;; (if it needs to load it off disk) or more likely can return an
  196. ;; existing object for the discovered directory. cpp-root always uses
  197. ;; the second case.
  198. (defun ede-cpp-root-project-file-for-dir (&optional dir)
  199. "Return a full file name to the project file stored in DIR."
  200. (let ((proj (ede-cpp-root-file-existing dir)))
  201. (when proj (oref proj :file))))
  202. (defvar ede-cpp-root-count 0
  203. "Count number of hits to the cpp root thing.
  204. This is a debugging variable to test various optimizations in file
  205. lookup in the main EDE logic.")
  206. ;;;###autoload
  207. (defun ede-cpp-root-project-root (&optional dir)
  208. "Get the root directory for DIR."
  209. (let ((projfile (ede-cpp-root-project-file-for-dir
  210. (or dir default-directory))))
  211. (setq ede-cpp-root-count (1+ ede-cpp-root-count))
  212. ;(debug)
  213. (when projfile
  214. (file-name-directory projfile))))
  215. (defun ede-cpp-root-load (dir &optional rootproj)
  216. "Return a CPP root object if you created one.
  217. Return nil if there isn't one.
  218. Argument DIR is the directory it is created for.
  219. ROOTPROJ is nil, since there is only one project."
  220. ;; Snoop through our master list.
  221. (ede-cpp-root-file-existing dir))
  222. ;;;###autoload
  223. (add-to-list 'ede-project-class-files
  224. (ede-project-autoload "cpp-root"
  225. :name "CPP ROOT"
  226. :file 'ede/cpp-root
  227. :proj-file 'ede-cpp-root-project-file-for-dir
  228. :proj-root 'ede-cpp-root-project-root
  229. :load-type 'ede-cpp-root-load
  230. :class-sym 'ede-cpp-root
  231. :new-p nil)
  232. t)
  233. ;;; CLASSES
  234. ;;
  235. ;; EDE sets up projects with two kinds of objects.
  236. ;;
  237. ;; The PROJECT is a class that represents everything under a directory
  238. ;; hierarchy. A TARGET represents a subset of files within a project.
  239. ;; A project can have multiple targets, and multiple sub-projects.
  240. ;; Sub projects should map to sub-directories.
  241. ;;
  242. ;; The CPP-ROOT project maps any file in C or C++ mode to a target for
  243. ;; C files.
  244. ;;
  245. ;; When creating a custom project the project developer an opportunity
  246. ;; to run code to setup various tools whenever an associated buffer is
  247. ;; loaded. The CPP-ROOT project spends most of its time setting up C
  248. ;; level include paths, and PreProcessor macro tables.
  249. (defclass ede-cpp-root-target (ede-target)
  250. ()
  251. "EDE cpp-root project target.
  252. All directories need at least one target.")
  253. (defclass ede-cpp-root-project (ede-project eieio-instance-tracker)
  254. ((tracking-symbol :initform 'ede-cpp-root-project-list)
  255. (include-path :initarg :include-path
  256. :initform '( "/include" "../include/" )
  257. :type list
  258. :documentation
  259. "The default locate function expands filenames within a project.
  260. If a header file (.h, .hh, etc) name is expanded, and
  261. the :locate-fcn slot is nil, then the include path is checked
  262. first, and other directories are ignored. For very large
  263. projects, this optimization can save a lot of time.
  264. Directory names in the path can be relative to the current
  265. buffer's `default-directory' (not starting with a /). Directories
  266. that are relative to the project's root should start with a /, such
  267. as \"/include\", meaning the directory `include' off the project root
  268. directory.")
  269. (system-include-path :initarg :system-include-path
  270. :initform nil
  271. :type list
  272. :documentation
  273. "The system include path for files in this project.
  274. C files initialized in an ede-cpp-root-project have their semantic
  275. system include path set to this value. If this is nil, then the
  276. semantic path is not modified.")
  277. (spp-table :initarg :spp-table
  278. :initform nil
  279. :type list
  280. :documentation
  281. "C Preprocessor macros for your files.
  282. Preprocessor symbols will be used while parsing your files.
  283. These macros might be passed in through the command line compiler, or
  284. are critical symbols derived from header files. Providing header files
  285. macro values through this slot improves accuracy and performance.
  286. Use `:spp-files' to use these files directly.")
  287. (spp-files :initarg :spp-files
  288. :initform nil
  289. :type list
  290. :documentation
  291. "C header file with Preprocessor macros for your files.
  292. The PreProcessor symbols appearing in these files will be used while
  293. parsing files in this project.
  294. See `semantic-lex-c-preprocessor-symbol-map' for more on how this works.")
  295. (header-match-regexp :initarg :header-match-regexp
  296. :initform
  297. "\\.\\(h\\(h\\|xx\\|pp\\|\\+\\+\\)?\\|H\\)$\\|\\<\\w+$"
  298. :type string
  299. :documentation
  300. "Regexp used to identify C/C++ header files.")
  301. (locate-fcn :initarg :locate-fcn
  302. :initform nil
  303. :type (or null function)
  304. :documentation
  305. "The locate function can be used in place of
  306. `ede-expand-filename' so you can quickly customize your custom target
  307. to use specialized local routines instead of the EDE routines.
  308. The function symbol must take two arguments:
  309. NAME - The name of the file to find.
  310. DIR - The directory root for this cpp-root project.
  311. It should return the fully qualified file name passed in from NAME. If that file does not
  312. exist, it should return nil."
  313. )
  314. )
  315. "EDE cpp-root project class.
  316. Each directory needs a project file to control it.")
  317. ;;; INIT
  318. ;;
  319. ;; Most projects use `initialize-instance' to do special setup
  320. ;; on the object when it is created. In this case, EDE-CPP-ROOT can
  321. ;; find previous copies of this project, and make sure that one of the
  322. ;; objects is deleted.
  323. (defmethod initialize-instance ((this ede-cpp-root-project)
  324. &rest fields)
  325. "Make sure the :file is fully expanded."
  326. ;; Add ourselves to the master list
  327. (call-next-method)
  328. (let ((f (expand-file-name (oref this :file))))
  329. ;; Remove any previous entries from the main list.
  330. (let ((old (eieio-instance-tracker-find (file-name-directory f)
  331. :directory 'ede-cpp-root-project-list)))
  332. ;; This is safe, because :directory isn't filled in till later.
  333. (when (and old (not (eq old this)))
  334. (delete-instance old)))
  335. ;; Basic initialization.
  336. (when (or (not (file-exists-p f))
  337. (file-directory-p f))
  338. (delete-instance this)
  339. (error ":file for ede-cpp-root must be a file"))
  340. (oset this :file f)
  341. (oset this :directory (file-name-directory f))
  342. (ede-project-directory-remove-hash (file-name-directory f))
  343. (ede-add-project-to-global-list this)
  344. (unless (slot-boundp this 'targets)
  345. (oset this :targets nil))
  346. ;; We need to add ourselves to the master list.
  347. ;;(setq ede-projects (cons this ede-projects))
  348. ))
  349. ;;; SUBPROJ Management.
  350. ;;
  351. ;; This is a way to allow a subdirectory to point back to the root
  352. ;; project, simplifying authoring new single-point projects.
  353. (defmethod ede-find-subproject-for-directory ((proj ede-cpp-root-project)
  354. dir)
  355. "Return PROJ, for handling all subdirs below DIR."
  356. proj)
  357. ;;; TARGET MANAGEMENT
  358. ;;
  359. ;; Creating new targets on a per directory basis is a good way to keep
  360. ;; files organized. See ede-emacs for an example with multiple file
  361. ;; types.
  362. (defmethod ede-find-target ((proj ede-cpp-root-project) buffer)
  363. "Find an EDE target in PROJ for BUFFER.
  364. If one doesn't exist, create a new one for this directory."
  365. (let* ((targets (oref proj targets))
  366. (dir default-directory)
  367. (ans (object-assoc dir :path targets))
  368. )
  369. (when (not ans)
  370. (setq ans (ede-cpp-root-target dir
  371. :name (file-name-nondirectory
  372. (directory-file-name dir))
  373. :path dir
  374. :source nil))
  375. (object-add-to-list proj :targets ans)
  376. )
  377. ans))
  378. ;;; FILE NAMES
  379. ;;
  380. ;; One of the more important jobs of EDE is to find files in a
  381. ;; directory structure. cpp-root has tricks it knows about how most C
  382. ;; projects are set up with include paths.
  383. ;;
  384. ;; This tools also uses the ede-locate setup for augmented file name
  385. ;; lookup using external tools.
  386. (defmethod ede-expand-filename-impl ((proj ede-cpp-root-project) name)
  387. "Within this project PROJ, find the file NAME.
  388. This knows details about or source tree."
  389. ;; The slow part of the original is looping over subprojects.
  390. ;; This version has no subprojects, so this will handle some
  391. ;; basic cases.
  392. (let ((ans (call-next-method)))
  393. (unless ans
  394. (let* ((lf (oref proj locate-fcn))
  395. (dir (file-name-directory (oref proj file))))
  396. (if lf
  397. (setq ans (funcall lf name dir))
  398. (if (ede-cpp-root-header-file-p proj name)
  399. ;; Else, use our little hack.
  400. (let ((ip (oref proj include-path))
  401. (tmp nil))
  402. (while ip
  403. ;; Translate
  404. (setq tmp (ede-cpp-root-translate-file proj (car ip)))
  405. ;; Test this name.
  406. (setq tmp (expand-file-name name tmp))
  407. (if (file-exists-p tmp)
  408. (setq ans tmp))
  409. (setq ip (cdr ip)) ))
  410. ;; Else, do the usual.
  411. (setq ans (call-next-method)))
  412. )))
  413. (or ans (call-next-method))))
  414. (defmethod ede-project-root ((this ede-cpp-root-project))
  415. "Return my root."
  416. this)
  417. (defmethod ede-project-root-directory ((this ede-cpp-root-project))
  418. "Return my root."
  419. (file-name-directory (oref this file)))
  420. ;;; C/CPP SPECIFIC CODE
  421. ;;
  422. ;; The following code is specific to setting up header files,
  423. ;; include lists, and Preprocessor symbol tables.
  424. (defmethod ede-cpp-root-header-file-p ((proj ede-cpp-root-project) name)
  425. "Non nil if in PROJ the filename NAME is a header."
  426. (save-match-data
  427. (string-match (oref proj header-match-regexp) name)))
  428. (defmethod ede-cpp-root-translate-file ((proj ede-cpp-root-project) filename)
  429. "For PROJ, translate a user specified FILENAME.
  430. This is for project include paths and spp source files."
  431. ;; Step one: Root of this project.
  432. (let ((dir (file-name-directory (oref proj file))))
  433. ;; Step two: Analyze first char, and rehost
  434. (if (and (not (string= filename "")) (= (aref filename 0) ?/))
  435. ;; Check relative to root of project
  436. (setq filename (expand-file-name (substring filename 1)
  437. dir))
  438. ;; Relative to current directory.
  439. (setq filename (expand-file-name filename)))
  440. filename))
  441. (defmethod ede-set-project-variables ((project ede-cpp-root-project) &optional buffer)
  442. "Set variables local to PROJECT in BUFFER.
  443. Also set up the lexical preprocessor map."
  444. (call-next-method)
  445. (when (and (featurep 'semantic/bovine/c) (featurep 'semantic/lex-spp))
  446. (setq semantic-lex-spp-project-macro-symbol-obarray
  447. (semantic-lex-make-spp-table (oref project spp-table)))
  448. ))
  449. (defmethod ede-system-include-path ((this ede-cpp-root-project))
  450. "Get the system include path used by project THIS."
  451. (oref this system-include-path))
  452. (defmethod ede-preprocessor-map ((this ede-cpp-root-project))
  453. "Get the pre-processor map for project THIS."
  454. (require 'semantic/db)
  455. (let ((spp (oref this spp-table))
  456. (root (ede-project-root this))
  457. )
  458. (mapc
  459. (lambda (F)
  460. (let* ((expfile (ede-expand-filename root F))
  461. (table (when expfile
  462. (semanticdb-file-table-object expfile)))
  463. )
  464. (when (not table)
  465. (message "Cannot find file %s in project." F))
  466. (when (and table (semanticdb-needs-refresh-p table))
  467. (semanticdb-refresh-table table)
  468. (setq spp (append spp (oref table lexical-table))))))
  469. (oref this spp-files))
  470. spp))
  471. (defmethod ede-system-include-path ((this ede-cpp-root-target))
  472. "Get the system include path used by project THIS."
  473. (ede-system-include-path (ede-target-parent this)))
  474. (defmethod ede-preprocessor-map ((this ede-cpp-root-target))
  475. "Get the pre-processor map for project THIS."
  476. (ede-preprocessor-map (ede-target-parent this)))
  477. ;;; Quick Hack
  478. (defun ede-create-lots-of-projects-under-dir (dir projfile &rest attributes)
  479. "Create a bunch of projects under directory DIR.
  480. PROJFILE is a file name sans directory that indicates a subdirectory
  481. is a project directory.
  482. Generic ATTRIBUTES, such as :include-path can be added.
  483. Note: This needs some work."
  484. (let ((files (directory-files dir t)))
  485. (dolist (F files)
  486. (if (file-exists-p (expand-file-name projfile F))
  487. `(ede-cpp-root-project (file-name-nondirectory F)
  488. :name (file-name-nondirectory F)
  489. :file (expand-file-name projfile F)
  490. attributes)))))
  491. (provide 'ede/cpp-root)
  492. ;; Local variables:
  493. ;; generated-autoload-file: "loaddefs.el"
  494. ;; generated-autoload-load-name: "ede/cpp-root"
  495. ;; End:
  496. ;;; ede/cpp-root.el ends here