Update copyright year to 2015
[emacs.git] / lisp / cedet / ede / config.el
blobe479af0559535f6f0d60c54b1dca85e136773b60
1 ;;; ede/config.el --- Configuration Handler baseclass
3 ;; Copyright (C) 2014-2015 Free Software Foundation, Inc.
5 ;; Author: Eric 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 ;; Some auto-detecting projects (such as the 'generic' project type)
25 ;; can be enhanced by also saving a configuration file that is EDE
26 ;; specific. EDE will be able to load that configuration from the save
27 ;; file as a way of augmenting what is normally already detected.
29 ;; How To Use:
31 ;; Subclass `ede-extra-config', and add the features you want to use.
32 ;; Several mixins are available for adding in C++ or Java support. Bring
33 ;; in the pieces you need.
35 ;; Your project and targets should all have a common baseclass from
36 ;; `ede-project-with-config' or `ede-target-with-config'. When
37 ;; subclassing the project, be sure to override the class allocated
38 ;; slots for the `config-class'. This will tie your new project to
39 ;; the new configuration type.
41 ;; You can also override the file name used to save the configuration
42 ;; object in.
44 ;; If you need to take special action in `project-rescan' be sure to also
45 ;; call `call-next-method' to also get the configuration rescanned.
47 ;; Note on config file safety:
49 ;; Normally an EDE project that loads a save file should have it's
50 ;; autoload slot :safe-p set to nil. Projects who save data via
51 ;; config.el can mark their project as :safe-p t. The config system will
52 ;; do the queries needed to protect the user. This allows a generic
53 ;; project to become active in cases where no save file exists, nor is
54 ;; needed.
56 ;;; Code:
57 (require 'ede)
59 ;;; CONFIG
61 ;; This is the base of a configuration class supported by the
62 ;; `ede-project-with-config' baseclass.
64 (defclass ede-extra-config (eieio-persistent)
65 ((extension :initform ".ede")
66 (file-header-line :initform ";; EDE Project Configuration")
67 (project :type ede-project-with-config-child
68 :documentation
69 "The project this config is bound to.")
70 (ignored-file :initform nil
71 :type (or null symbol)
72 :documentation
73 "Set to non-nil if this was created and an on-disk file
74 was ignored. Use this to warn the user that they might want to load in
75 an on-disk version.")
77 "Baseclass for auxiliary configuration files for EDE.
78 This should be subclassed by projects that auto detect a project
79 and also want to save some extra level of configuration.")
81 ;;; PROJECT BASECLASS
83 ;; Subclass this baseclass if you want your EDE project to also
84 ;; support saving an extra configuration file of unique data
85 ;; needed for this project.
87 (defclass ede-project-with-config (ede-project)
88 ((menu :initform nil)
89 (config-file-basename
90 :initform "Config.ede"
91 :allocation :class
92 :type string
93 :documentation
94 "The filename to use for saving the configuration.
95 This filename excludes the directory name and is used to
96 initialize the :file slot of the persistent baseclass.")
97 (config-class
98 :initform ede-extra-config
99 :allocation :class
100 :type class
101 :documentation
102 "The class of the configuration used by this project.")
103 (config :initform nil
104 :type (or null ede-extra-config-child)
105 :documentation
106 "The configuration object for this project.")
108 "Baseclass for projects that save a configuration.")
110 (defclass ede-target-with-config (ede-target)
112 "Baseclass for targets of classes that use a config object.")
114 ;;; Rescanning
116 (defmethod project-rescan ((this ede-project-with-config))
117 "Rescan this generic project from the sources."
118 ;; Force the config to be rescanned.
119 (oset this config nil)
120 ;; Ask if it is safe to load the config from disk.
121 (ede-config-get-configuration this t)
124 ;;; Project Methods for configuration
126 (defmethod ede-config-get-configuration ((proj ede-project-with-config) &optional loadask)
127 "Return the configuration for the project PROJ.
128 If optional LOADASK is non-nil, then if a project file exists, and if
129 the directory isn't on the `safe' list, ask to add it to the safe list."
130 (let ((config (oref proj config)))
132 ;; If the request is coming at a time when we want to ask the user,
133 ;; and there already is a configuration, AND the last time we ignored
134 ;; the on-file version we did so automatically (without asking) then
135 ;; in theory there are NO mods to this config, and we should re-ask,
136 ;; and possibly re-load.
137 (when (and loadask config (eq (oref config ignored-file) 'auto))
138 (setq config nil))
140 (when (not config)
141 (let* ((top (oref proj :directory))
142 (fname (expand-file-name (oref proj config-file-basename) top))
143 (class (oref proj config-class))
144 (ignore-type nil))
145 (if (and (file-exists-p fname)
146 (or (ede-directory-safe-p top)
147 ;; Only force the load if someone asked.
148 (and loadask (ede-check-project-directory top))))
149 ;; Load in the configuration
150 (setq config (eieio-persistent-read fname class))
151 ;; If someone said not to load stuff from here then
152 ;; pop up a warning.
153 (when (file-exists-p fname)
154 (message "Ignoring EDE config file for now and creating a new one. Use C-c . g to load it.")
155 ;; Set how it was ignored.
156 (if loadask
157 (setq ignore-type 'manual)
158 (setq ignore-type 'auto))
160 ;; Create a new one.
161 (setq config (make-instance class
162 "Configuration"
163 :file fname))
164 (oset config ignored-file ignore-type)
166 ;; Set initial values based on project.
167 (ede-config-setup-configuration proj config))
168 ;; Link things together.
169 (oset proj config config)
170 (oset config project proj)))
171 config))
173 (defmethod ede-config-setup-configuration ((proj ede-project-with-config) config)
174 "Default configuration setup method."
175 nil)
177 (defmethod ede-commit-project ((proj ede-project-with-config))
178 "Commit any change to PROJ to its file."
179 (let ((config (ede-config-get-configuration proj)))
180 (ede-commit config)))
182 ;;; Customization
184 (defmethod ede-customize ((proj ede-project-with-config))
185 "Customize the EDE project PROJ by actually configuring the config object."
186 (let ((config (ede-config-get-configuration proj t)))
187 (eieio-customize-object config)))
189 (defmethod ede-customize ((target ede-target-with-config))
190 "Customize the EDE TARGET by actually configuring the config object."
191 ;; Nothing unique for the targets, use the project.
192 (ede-customize-project))
194 (defmethod eieio-done-customizing ((config ede-extra-config))
195 "Called when EIEIO is done customizing the configuration object.
196 We need to go back through the old buffers, and update them with
197 the new configuration."
198 (ede-commit config)
199 ;; Loop over all the open buffers, and re-apply.
200 (ede-map-targets
201 (oref config project)
202 (lambda (target)
203 (ede-map-target-buffers
204 target
205 (lambda (b)
206 (with-current-buffer b
207 (ede-apply-target-options)))))))
209 (defmethod ede-commit ((config ede-extra-config))
210 "Commit all changes to the configuration to disk."
211 ;; So long as the user is trying to safe this config, make sure they can
212 ;; get at it again later.
213 (let ((dir (file-name-directory (oref config file))))
214 (ede-check-project-directory dir))
216 (eieio-persistent-save config))
218 ;;; PROJECT MIXINS
220 ;; These are project part mixins. Use multiple inheritance for each
221 ;; piece of these configuration options you would like to have as part
222 ;; of your project.
224 ;;; PROGRAM
225 ;; If there is a program that can be run or debugged that is unknown
226 ;; and needs to be configured.
227 (defclass ede-extra-config-program ()
228 ((debug-command :initarg :debug-command
229 :initform "gdb "
230 :type string
231 :group commands
232 :custom string
233 :group (default build)
234 :documentation
235 "Command used for debugging this project.")
236 (run-command :initarg :run-command
237 :initform ""
238 :type string
239 :group commands
240 :custom string
241 :group (default build)
242 :documentation
243 "Command used to run something related to this project."))
244 "Class to mix into a configuration for debug/run of programs.")
246 (defclass ede-project-with-config-program ()
248 "Class to mix into a project with configuration for programs.")
250 (defclass ede-target-with-config-program ()
252 "Class to mix into a project with configuration for programs.
253 This class brings in method overloads for running and debugging
254 programs from a project.")
256 (defmethod project-debug-target ((target ede-target-with-config-program))
257 "Run the current project derived from TARGET in a debugger."
258 (let* ((proj (ede-target-parent target))
259 (config (ede-config-get-configuration proj t))
260 (debug (oref config :debug-command))
261 (cmd (read-from-minibuffer
262 "Debug Command: "
263 debug))
264 (cmdsplit (split-string cmd " " t))
265 ;; @TODO - this depends on the user always typing in something good
266 ;; like "gdb" or "dbx" which also exists as a useful Emacs command.
267 ;; Is there a better way?
268 (cmdsym (intern-soft (car cmdsplit))))
269 (call-interactively cmdsym t)))
271 (defmethod project-run-target ((target ede-target-with-config-program))
272 "Run the current project derived from TARGET."
273 (let* ((proj (ede-target-parent target))
274 (config (ede-config-get-configuration proj t))
275 (run (concat "./" (oref config :run-command)))
276 (cmd (read-from-minibuffer "Run (like this): " run)))
277 (ede-shell-run-something target cmd)))
279 ;;; BUILD
280 ;; If the build style is unknown and needs to be configured.
281 (defclass ede-extra-config-build ()
282 ((build-command :initarg :build-command
283 :initform "make -k"
284 :type string
285 :group commands
286 :custom string
287 :group (default build)
288 :documentation
289 "Command used for building this project."))
290 "Class to mix into a configuration for compilation.")
292 (defclass ede-project-with-config-build ()
294 "Class to mix into a project with configuration for builds.
295 This class brings in method overloads for building.")
297 (defclass ede-target-with-config-build ()
299 "Class to mix into a project with configuration for builds.
300 This class brings in method overloads for for building.")
302 (defmethod project-compile-project ((proj ede-project-with-config-build) &optional command)
303 "Compile the entire current project PROJ.
304 Argument COMMAND is the command to use when compiling."
305 (let* ((config (ede-config-get-configuration proj t))
306 (comp (oref config :build-command)))
307 (compile comp)))
309 (defmethod project-compile-target ((obj ede-target-with-config-build) &optional command)
310 "Compile the current target OBJ.
311 Argument COMMAND is the command to use for compiling the target."
312 (project-compile-project (ede-current-project) command))
314 ;;; C / C++
315 ;; Configure includes and preprocessor symbols for C/C++ needed by
316 ;; Semantic.
317 (defclass ede-extra-config-c ()
318 ((c-include-path :initarg :c-include-path
319 :initform nil
320 :type list
321 :custom (repeat (string :tag "Path"))
322 :group c
323 :documentation
324 "The include path used by C/C++ projects.
325 The include path is used when searching for symbols.")
326 (c-preprocessor-table :initarg :c-preprocessor-table
327 :initform nil
328 :type list
329 :custom (repeat (cons (string :tag "Macro")
330 (string :tag "Value")))
331 :group c
332 :documentation
333 "Preprocessor Symbols for this project.
334 When files within this project are parsed by CEDET, these symbols will be
335 used to resolve macro occurrences in source files.
336 If you modify this slot, you will need to force your source files to be
337 parsed again.")
338 (c-preprocessor-files :initarg :c-preprocessor-files
339 :initform nil
340 :type list
341 :group c
342 :custom (repeat (string :tag "Include File"))
343 :documentation
344 "Files parsed and used to populate preprocessor tables.
345 When files within this project are parsed by CEDET, these symbols will be used to
346 resolve macro occurrences in source files.
347 If you modify this slot, you will need to force your source files to be
348 parsed again."))
349 "Class to mix into a configuration for compilation.")
351 (defclass ede-project-with-config-c ()
353 "Class to mix into a project for C/C++ support.")
355 (defclass ede-target-with-config-c ()
357 "Class to mix into a project for C/C++ support.
358 This target brings in methods used by Semantic to query
359 the preprocessor map, and include paths.")
361 (defmethod ede-preprocessor-map ((this ede-target-with-config-c))
362 "Get the pre-processor map for some generic C code."
363 (let* ((proj (ede-target-parent this))
364 (root (ede-project-root proj))
365 (config (ede-config-get-configuration proj))
366 filemap
368 ;; Preprocessor files
369 (dolist (G (oref config :c-preprocessor-files))
370 (let ((table (semanticdb-file-table-object
371 (ede-expand-filename root G))))
372 (when table
373 (when (semanticdb-needs-refresh-p table)
374 (semanticdb-refresh-table table))
375 (setq filemap (append filemap (oref table lexical-table)))
377 ;; The core table
378 (setq filemap (append filemap (oref config :c-preprocessor-table)))
380 filemap
383 (defmethod ede-system-include-path ((this ede-target-with-config-c))
384 "Get the system include path used by project THIS."
385 (let* ((proj (ede-target-parent this))
386 (config (ede-config-get-configuration proj)))
387 (oref config c-include-path)))
389 ;;; Java
390 ;; Configuration needed for programming with Java.
391 (defclass ede-extra-config-java ()
393 "Class to mix into a configuration for compilation.")
395 (defclass ede-project-with-config-java ()
397 "Class to mix into a project to support java.
398 This brings in methods to support Semantic querying the
399 java class path.")
401 (defclass ede-target-with-config-java ()
403 "Class to mix into a project to support java.")
405 (defmethod ede-java-classpath ((proj ede-project-with-config-java))
406 "Return the classpath for this project."
407 (oref (ede-config-get-configuration proj) :classpath))
409 ;; Local variables:
410 ;; generated-autoload-file: "loaddefs.el"
411 ;; generated-autoload-load-name: "ede/config"
412 ;; End:
414 (provide 'ede/config)
416 ;;; ede/config.el ends here