cpp-root.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. ;;; ede/cpp-root.el --- A simple way to wrap a C++ project with a single root
  2. ;; Copyright (C) 2007-2015 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-function 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-LOAD (dir)
  113. ;; "Load a project of type `cpp-root' for the directory DIR.
  114. ;; Return nil if there isn't one."
  115. ;; (ede-cpp-root-project "NAME" :file (expand-file-name "FILE" dir)
  116. ;; :locate-fcn 'MYFCN)
  117. ;; )
  118. ;;
  119. ;; (ede-add-project-autoload
  120. ;; (ede-project-autoload "cpp-root"
  121. ;; :name "CPP ROOT"
  122. ;; :file 'ede/cpp-root
  123. ;; :proj-file 'MY-FILE-FOR-DIR
  124. ;; :load-type 'MY-LOAD
  125. ;; :class-sym 'ede-cpp-root-project
  126. ;; :safe-p t))
  127. ;;
  128. ;;; TODO
  129. ;;
  130. ;; Need a way to reconfigure a project, and have it affect all open buffers.
  131. ;; From Tobias Gerdin:
  132. ;;
  133. ;; >>3) Is there any way to refresh a ede-cpp-root-project dynamically? I have
  134. ;; >>some file open part of the project, fiddle with the include paths and would
  135. ;; >>like the open buffer to notice this when I re-evaluate the
  136. ;; >>ede-cpp-root-project constructor.
  137. ;; >
  138. ;; > Another good idea. The easy way is to "revert-buffer" as needed. The
  139. ;; > ede "project local variables" does this already, so it should be easy
  140. ;; > to adapt something.
  141. ;;
  142. ;; I actually tried reverting the buffer but Semantic did not seem to pick
  143. ;; up the differences (the "include summary" reported the same include paths).
  144. (require 'ede)
  145. (defvar semantic-lex-spp-project-macro-symbol-obarray)
  146. (declare-function semantic-lex-make-spp-table "semantic/lex-spp")
  147. (declare-function semanticdb-file-table-object "semantic/db")
  148. (declare-function semanticdb-needs-refresh-p "semantic/db")
  149. (declare-function semanticdb-refresh-table "semantic/db")
  150. ;;; Code:
  151. ;;; PROJECT CACHE:
  152. ;;
  153. ;; cpp-root projects are created in a .emacs or other config file. We
  154. ;; need to cache them so if the user re-loads a lisp file with the
  155. ;; config in it, we can flush out the old one and replace it.
  156. ;;
  157. (defvar ede-cpp-root-project-list nil
  158. "List of projects created by option `ede-cpp-root-project'.")
  159. ;;; CLASSES
  160. ;;
  161. ;; EDE sets up projects with two kinds of objects.
  162. ;;
  163. ;; The PROJECT is a class that represents everything under a directory
  164. ;; hierarchy. A TARGET represents a subset of files within a project.
  165. ;; A project can have multiple targets, and multiple sub-projects.
  166. ;; Sub projects should map to sub-directories.
  167. ;;
  168. ;; The CPP-ROOT project maps any file in C or C++ mode to a target for
  169. ;; C files.
  170. ;;
  171. ;; When creating a custom project the project developer an opportunity
  172. ;; to run code to setup various tools whenever an associated buffer is
  173. ;; loaded. The CPP-ROOT project spends most of its time setting up C
  174. ;; level include paths, and PreProcessor macro tables.
  175. (defclass ede-cpp-root-target (ede-target)
  176. ((project :initform nil
  177. :initarg :project))
  178. "EDE cpp-root project target.
  179. All directories need at least one target.")
  180. ;;;###autoload
  181. (defclass ede-cpp-root-project (ede-project eieio-instance-tracker)
  182. ((tracking-symbol :initform 'ede-cpp-root-project-list)
  183. (include-path :initarg :include-path
  184. :initform '( "/include" "../include/" )
  185. :type list
  186. :documentation
  187. "The default locate function expands filenames within a project.
  188. If a header file (.h, .hh, etc) name is expanded, and
  189. the :locate-fcn slot is nil, then the include path is checked
  190. first, and other directories are ignored. For very large
  191. projects, this optimization can save a lot of time.
  192. Directory names in the path can be relative to the current
  193. buffer's `default-directory' (not starting with a /). Directories
  194. that are relative to the project's root should start with a /, such
  195. as \"/include\", meaning the directory `include' off the project root
  196. directory.")
  197. (system-include-path :initarg :system-include-path
  198. :initform nil
  199. :type list
  200. :documentation
  201. "The system include path for files in this project.
  202. C files initialized in an ede-cpp-root-project have their semantic
  203. system include path set to this value. If this is nil, then the
  204. semantic path is not modified.")
  205. (spp-table :initarg :spp-table
  206. :initform nil
  207. :type list
  208. :documentation
  209. "C Preprocessor macros for your files.
  210. Preprocessor symbols will be used while parsing your files.
  211. These macros might be passed in through the command line compiler, or
  212. are critical symbols derived from header files. Providing header files
  213. macro values through this slot improves accuracy and performance.
  214. Use `:spp-files' to use these files directly.")
  215. (spp-files :initarg :spp-files
  216. :initform nil
  217. :type list
  218. :documentation
  219. "C header file with Preprocessor macros for your files.
  220. The PreProcessor symbols appearing in these files will be used while
  221. parsing files in this project.
  222. See `semantic-lex-c-preprocessor-symbol-map' for more on how this works.")
  223. (header-match-regexp :initarg :header-match-regexp
  224. :initform
  225. "\\.\\(h\\(h\\|xx\\|pp\\|\\+\\+\\)?\\|H\\)$\\|\\<\\w+$"
  226. :type string
  227. :documentation
  228. "Regexp used to identify C/C++ header files.")
  229. (locate-fcn :initarg :locate-fcn
  230. :initform nil
  231. :type (or null function)
  232. :documentation
  233. "The locate function can be used in place of
  234. `ede-expand-filename' so you can quickly customize your custom target
  235. to use specialized local routines instead of the EDE routines.
  236. The function symbol must take two arguments:
  237. NAME - The name of the file to find.
  238. DIR - The directory root for this cpp-root project.
  239. It should return the fully qualified file name passed in from NAME. If that file does not
  240. exist, it should return nil."
  241. )
  242. (compile-command :initarg :compile-command
  243. :initform nil
  244. :type (or null string function)
  245. :documentation
  246. "Compilation command that will be used for this project.
  247. It could be string or function that will accept proj argument and should return string.
  248. The string will be passed to `compile' function that will be issued in root
  249. directory of project."
  250. )
  251. )
  252. "EDE cpp-root project class.
  253. Each directory needs a project file to control it.")
  254. ;;; INIT
  255. ;;
  256. ;; Most projects use `initialize-instance' to do special setup
  257. ;; on the object when it is created. In this case, EDE-CPP-ROOT can
  258. ;; find previous copies of this project, and make sure that one of the
  259. ;; objects is deleted.
  260. (cl-defmethod initialize-instance ((this ede-cpp-root-project)
  261. &rest fields)
  262. "Make sure the :file is fully expanded."
  263. ;; Add ourselves to the master list
  264. (cl-call-next-method)
  265. (let ((f (expand-file-name (oref this :file))))
  266. ;; Remove any previous entries from the main list.
  267. (let ((old (eieio-instance-tracker-find (file-name-directory f)
  268. :directory 'ede-cpp-root-project-list)))
  269. ;; This is safe, because :directory isn't filled in till later.
  270. (when (and old (not (eq old this)))
  271. (ede-delete-project-from-global-list old)
  272. (delete-instance old)))
  273. ;; Basic initialization.
  274. (when (or (not (file-exists-p f))
  275. (file-directory-p f))
  276. (delete-instance this)
  277. (error ":file for ede-cpp-root-project must be a file"))
  278. (oset this :file f)
  279. (oset this :directory (file-name-directory f))
  280. (ede-project-directory-remove-hash (file-name-directory f))
  281. ;; NOTE: We must add to global list here because these classes are not
  282. ;; created via the typical loader, but instead via calls from a .emacs
  283. ;; file.
  284. (ede-add-project-to-global-list this)
  285. (unless (slot-boundp this 'targets)
  286. (oset this :targets nil))
  287. ))
  288. ;;; SUBPROJ Management.
  289. ;;
  290. ;; This is a way to allow a subdirectory to point back to the root
  291. ;; project, simplifying authoring new single-point projects.
  292. (cl-defmethod ede-find-subproject-for-directory ((proj ede-cpp-root-project)
  293. dir)
  294. "Return PROJ, for handling all subdirs below DIR."
  295. proj)
  296. ;;; TARGET MANAGEMENT
  297. ;;
  298. ;; Creating new targets on a per directory basis is a good way to keep
  299. ;; files organized. See ede-emacs for an example with multiple file
  300. ;; types.
  301. (cl-defmethod ede-find-target ((proj ede-cpp-root-project) buffer)
  302. "Find an EDE target in PROJ for BUFFER.
  303. If one doesn't exist, create a new one for this directory."
  304. (let* ((targets (oref proj targets))
  305. (dir default-directory)
  306. (ans (object-assoc dir :path targets))
  307. )
  308. (when (not ans)
  309. (setq ans (ede-cpp-root-target dir
  310. :name (file-name-nondirectory
  311. (directory-file-name dir))
  312. :path dir
  313. :source nil
  314. :project proj))
  315. (object-add-to-list proj :targets ans)
  316. )
  317. ans))
  318. ;;; FILE NAMES
  319. ;;
  320. ;; One of the more important jobs of EDE is to find files in a
  321. ;; directory structure. cpp-root has tricks it knows about how most C
  322. ;; projects are set up with include paths.
  323. ;;
  324. ;; This tools also uses the ede-locate setup for augmented file name
  325. ;; lookup using external tools.
  326. (cl-defmethod ede-expand-filename-impl ((proj ede-cpp-root-project) name)
  327. "Within this project PROJ, find the file NAME.
  328. This knows details about or source tree."
  329. ;; The slow part of the original is looping over subprojects.
  330. ;; This version has no subprojects, so this will handle some
  331. ;; basic cases.
  332. (let ((ans (cl-call-next-method)))
  333. (unless ans
  334. (let* ((lf (oref proj locate-fcn))
  335. (dir (file-name-directory (oref proj file))))
  336. (if lf
  337. (setq ans (funcall lf name dir))
  338. (if (ede-cpp-root-header-file-p proj name)
  339. ;; Else, use our little hack.
  340. (let ((ip (oref proj include-path))
  341. (tmp nil))
  342. (while ip
  343. ;; Translate
  344. (setq tmp (ede-cpp-root-translate-file proj (car ip)))
  345. ;; Test this name.
  346. (setq tmp (expand-file-name name tmp))
  347. (if (file-exists-p tmp)
  348. (setq ans tmp))
  349. (setq ip (cdr ip)) ))
  350. ;; Else, do the usual.
  351. (setq ans (cl-call-next-method)))
  352. )))
  353. ;; TODO - does this call-next-method happen twice. Is that bad?? Why is it here?
  354. (or ans (cl-call-next-method))))
  355. (cl-defmethod ede-project-root ((this ede-cpp-root-project))
  356. "Return my root."
  357. this)
  358. (cl-defmethod ede-project-root-directory ((this ede-cpp-root-project))
  359. "Return my root."
  360. (oref this directory))
  361. ;;; C/CPP SPECIFIC CODE
  362. ;;
  363. ;; The following code is specific to setting up header files,
  364. ;; include lists, and Preprocessor symbol tables.
  365. (cl-defmethod ede-cpp-root-header-file-p ((proj ede-cpp-root-project) name)
  366. "Non nil if in PROJ the filename NAME is a header."
  367. (save-match-data
  368. (string-match (oref proj header-match-regexp) name)))
  369. (cl-defmethod ede-cpp-root-translate-file ((proj ede-cpp-root-project) filename)
  370. "For PROJ, translate a user specified FILENAME.
  371. This is for project include paths and spp source files."
  372. ;; Step one: Root of this project.
  373. (let ((dir (file-name-directory (oref proj file))))
  374. ;; Step two: Analyze first char, and rehost
  375. (if (and (not (string= filename "")) (= (aref filename 0) ?/))
  376. ;; Check relative to root of project
  377. (setq filename (expand-file-name (substring filename 1)
  378. dir))
  379. ;; Relative to current directory.
  380. (setq filename (expand-file-name filename)))
  381. filename))
  382. (cl-defmethod ede-system-include-path ((this ede-cpp-root-project))
  383. "Get the system include path used by project THIS."
  384. (oref this system-include-path))
  385. (cl-defmethod ede-preprocessor-map ((this ede-cpp-root-project))
  386. "Get the pre-processor map for project THIS."
  387. (require 'semantic/db)
  388. (let ((spp (oref this spp-table))
  389. (root (ede-project-root this))
  390. )
  391. (mapc
  392. (lambda (F)
  393. (let* ((expfile (ede-expand-filename root F))
  394. (table (when expfile
  395. ;; Disable EDE init on preprocessor file load
  396. ;; otherwise we recurse, cause errs, etc.
  397. (let ((ede-constructing t))
  398. (semanticdb-file-table-object expfile))))
  399. )
  400. (cond
  401. ((not (file-exists-p expfile))
  402. (message "Cannot find file %s in project." F))
  403. ((string= expfile (buffer-file-name))
  404. ;; Don't include this file in it's own spp table.
  405. )
  406. ((not table)
  407. (message "No db table available for %s." expfile))
  408. (t
  409. (when (semanticdb-needs-refresh-p table)
  410. (semanticdb-refresh-table table))
  411. (setq spp (append spp (oref table lexical-table)))))))
  412. (oref this spp-files))
  413. spp))
  414. (cl-defmethod ede-system-include-path ((this ede-cpp-root-target))
  415. "Get the system include path used by target THIS."
  416. (ede-system-include-path (ede-target-parent this)))
  417. (cl-defmethod ede-preprocessor-map ((this ede-cpp-root-target))
  418. "Get the pre-processor map for project THIS."
  419. (ede-preprocessor-map (ede-target-parent this)))
  420. (cl-defmethod project-compile-project ((proj ede-cpp-root-project) &optional command)
  421. "Compile the entire current project PROJ.
  422. Argument COMMAND is the command to use when compiling."
  423. ;; we need to be in the proj root dir for this to work
  424. (let* ((cmd (oref proj :compile-command))
  425. (ov (oref proj :local-variables))
  426. (lcmd (when ov (cdr (assoc 'compile-command ov))))
  427. (cmd-str (cond
  428. ((stringp cmd) cmd)
  429. ((functionp cmd) (funcall cmd proj))
  430. ((stringp lcmd) lcmd)
  431. ((functionp lcmd) (funcall lcmd proj)))))
  432. (when cmd-str
  433. (let ((default-directory (ede-project-root-directory proj)))
  434. (compile cmd-str)))))
  435. (cl-defmethod project-compile-target ((obj ede-cpp-root-target) &optional command)
  436. "Compile the current target OBJ.
  437. Argument COMMAND is the command to use for compiling the target."
  438. (when (oref obj :project)
  439. (project-compile-project (oref obj :project) command)))
  440. (cl-defmethod project-rescan ((this ede-cpp-root-project))
  441. "Don't rescan this project from the sources."
  442. (message "cpp-root has nothing to rescan."))
  443. ;;; Quick Hack
  444. (defun ede-create-lots-of-projects-under-dir (dir projfile &rest attributes)
  445. "Create a bunch of projects under directory DIR.
  446. PROJFILE is a file name sans directory that indicates a subdirectory
  447. is a project directory.
  448. Generic ATTRIBUTES, such as :include-path can be added.
  449. Note: This needs some work."
  450. (let ((files (directory-files dir t)))
  451. (dolist (F files)
  452. (if (file-exists-p (expand-file-name projfile F))
  453. `(ede-cpp-root-project (file-name-nondirectory F)
  454. :name (file-name-nondirectory F)
  455. :file (expand-file-name projfile F)
  456. attributes)))))
  457. (provide 'ede/cpp-root)
  458. ;; Local variables:
  459. ;; generated-autoload-file: "loaddefs.el"
  460. ;; generated-autoload-load-name: "ede/cpp-root"
  461. ;; End:
  462. ;;; ede/cpp-root.el ends here