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