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