]> code.delx.au - gnu-emacs/blob - lisp/cedet/ede/config.el
Update copyright year to 2016
[gnu-emacs] / lisp / cedet / ede / config.el
1 ;;; ede/config.el --- Configuration Handler baseclass
2
3 ;; Copyright (C) 2014-2016 Free Software Foundation, Inc.
4
5 ;; Author: Eric Ludlam <eric@siege-engine.com>
6
7 ;; This file is part of GNU Emacs.
8
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.
13
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.
18
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/>.
21
22 ;;; Commentary:
23 ;;
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.
28 ;;
29 ;; How To Use:
30 ;;
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.
34 ;;
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.
40 ;;
41 ;; You can also override the file name used to save the configuration
42 ;; object in.
43 ;;
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.
46 ;;
47 ;; Note on config file safety:
48 ;;
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.
55
56 ;;; Code:
57 (require 'ede)
58
59 ;;; CONFIG
60 ;;
61 ;; This is the base of a configuration class supported by the
62 ;; `ede-project-with-config' baseclass.
63 ;;
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.")
76 )
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.")
80
81 ;;; PROJECT BASECLASS
82 ;;
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.
86 ;;
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.")
107 )
108 "Baseclass for projects that save a configuration.")
109
110 (defclass ede-target-with-config (ede-target)
111 ()
112 "Baseclass for targets of classes that use a config object.")
113
114 ;;; Rescanning
115
116 (cl-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)
122 )
123
124 ;;; Project Methods for configuration
125
126 (cl-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)))
131
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))
139
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))
159 )
160 ;; Create a new one.
161 (setq config (make-instance class
162 "Configuration"
163 :file fname))
164 (oset config ignored-file ignore-type)
165
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))
172
173 (cl-defmethod ede-config-setup-configuration ((proj ede-project-with-config) config)
174 "Default configuration setup method."
175 nil)
176
177 (cl-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)))
181
182 ;;; Customization
183 ;;
184 (cl-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)))
188
189 (cl-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))
193
194 (cl-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)))))))
208
209 (cl-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))
215
216 (eieio-persistent-save config))
217
218 ;;; PROJECT MIXINS
219 ;;
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.
223
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.")
245
246 (defclass ede-project-with-config-program ()
247 ()
248 "Class to mix into a project with configuration for programs.")
249
250 (defclass ede-target-with-config-program ()
251 ()
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.")
255
256 (cl-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)))
270
271 (declare-function ede-shell-run-something "ede/shell")
272
273 (cl-defmethod project-run-target ((target ede-target-with-config-program))
274 "Run the current project derived from TARGET."
275 (let* ((proj (ede-target-parent target))
276 (config (ede-config-get-configuration proj t))
277 (run (concat "./" (oref config :run-command)))
278 (cmd (read-from-minibuffer "Run (like this): " run)))
279 (ede-shell-run-something target cmd)))
280
281 ;;; BUILD
282 ;; If the build style is unknown and needs to be configured.
283 (defclass ede-extra-config-build ()
284 ((build-command :initarg :build-command
285 :initform "make -k"
286 :type string
287 :group commands
288 :custom string
289 :group (default build)
290 :documentation
291 "Command used for building this project."))
292 "Class to mix into a configuration for compilation.")
293
294 (defclass ede-project-with-config-build ()
295 ()
296 "Class to mix into a project with configuration for builds.
297 This class brings in method overloads for building.")
298
299 (defclass ede-target-with-config-build ()
300 ()
301 "Class to mix into a project with configuration for builds.
302 This class brings in method overloads for for building.")
303
304 (cl-defmethod project-compile-project ((proj ede-project-with-config-build) &optional command)
305 "Compile the entire current project PROJ.
306 Argument COMMAND is the command to use when compiling."
307 (let* ((config (ede-config-get-configuration proj t))
308 (comp (oref config :build-command)))
309 (compile comp)))
310
311 (cl-defmethod project-compile-target ((obj ede-target-with-config-build) &optional command)
312 "Compile the current target OBJ.
313 Argument COMMAND is the command to use for compiling the target."
314 (project-compile-project (ede-current-project) command))
315
316 ;;; C / C++
317 ;; Configure includes and preprocessor symbols for C/C++ needed by
318 ;; Semantic.
319 (defclass ede-extra-config-c ()
320 ((c-include-path :initarg :c-include-path
321 :initform nil
322 :type list
323 :custom (repeat (string :tag "Path"))
324 :group c
325 :documentation
326 "The include path used by C/C++ projects.
327 The include path is used when searching for symbols.")
328 (c-preprocessor-table :initarg :c-preprocessor-table
329 :initform nil
330 :type list
331 :custom (repeat (cons (string :tag "Macro")
332 (string :tag "Value")))
333 :group c
334 :documentation
335 "Preprocessor Symbols for this project.
336 When files within this project are parsed by CEDET, these symbols will be
337 used to resolve macro occurrences in source files.
338 If you modify this slot, you will need to force your source files to be
339 parsed again.")
340 (c-preprocessor-files :initarg :c-preprocessor-files
341 :initform nil
342 :type list
343 :group c
344 :custom (repeat (string :tag "Include File"))
345 :documentation
346 "Files parsed and used to populate preprocessor tables.
347 When files within this project are parsed by CEDET, these symbols will be used to
348 resolve macro occurrences in source files.
349 If you modify this slot, you will need to force your source files to be
350 parsed again."))
351 "Class to mix into a configuration for compilation.")
352
353 (defclass ede-project-with-config-c ()
354 ()
355 "Class to mix into a project for C/C++ support.")
356
357 (defclass ede-target-with-config-c ()
358 ()
359 "Class to mix into a project for C/C++ support.
360 This target brings in methods used by Semantic to query
361 the preprocessor map, and include paths.")
362
363 (declare-function semanticdb-file-table-object "semantic/db"
364 (file &optional dontload))
365 (declare-function semanticdb-needs-refresh-p "semantic/db" (arg &rest args))
366 (declare-function semanticdb-refresh-table "semantic/db" (arg &rest args))
367
368 (cl-defmethod ede-preprocessor-map ((this ede-target-with-config-c))
369 "Get the pre-processor map for some generic C code."
370 (require 'semantic/sb)
371 (let* ((proj (ede-target-parent this))
372 (root (ede-project-root proj))
373 (config (ede-config-get-configuration proj))
374 filemap
375 )
376 ;; Preprocessor files
377 (dolist (G (oref config :c-preprocessor-files))
378 (let ((table (semanticdb-file-table-object
379 (ede-expand-filename root G))))
380 (when table
381 (when (semanticdb-needs-refresh-p table)
382 (semanticdb-refresh-table table))
383 (setq filemap (append filemap (oref table lexical-table)))
384 )))
385 ;; The core table
386 (setq filemap (append filemap (oref config :c-preprocessor-table)))
387
388 filemap
389 ))
390
391 (cl-defmethod ede-system-include-path ((this ede-target-with-config-c))
392 "Get the system include path used by project THIS."
393 (let* ((proj (ede-target-parent this))
394 (config (ede-config-get-configuration proj)))
395 (oref config c-include-path)))
396
397 ;;; Java
398 ;; Configuration needed for programming with Java.
399 (defclass ede-extra-config-java ()
400 ()
401 "Class to mix into a configuration for compilation.")
402
403 (defclass ede-project-with-config-java ()
404 ()
405 "Class to mix into a project to support java.
406 This brings in methods to support Semantic querying the
407 java class path.")
408
409 (defclass ede-target-with-config-java ()
410 ()
411 "Class to mix into a project to support java.")
412
413 (cl-defmethod ede-java-classpath ((proj ede-project-with-config-java))
414 "Return the classpath for this project."
415 (oref (ede-config-get-configuration proj) :classpath))
416
417 ;; Local variables:
418 ;; generated-autoload-file: "loaddefs.el"
419 ;; generated-autoload-load-name: "ede/config"
420 ;; End:
421
422 (provide 'ede/config)
423
424 ;;; ede/config.el ends here