PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/cedet/ede/config.el

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