proj-comp.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. ;;; ede/proj-comp.el --- EDE Generic Project compiler/rule driver
  2. ;; Copyright (C) 1999-2001, 2004-2005, 2007, 2009-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  5. ;; Keywords: project, make
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; This software handles the maintenance of compiler and rule definitions
  20. ;; for different object types.
  21. ;;
  22. ;; The `ede-compiler' class lets different types of project objects create
  23. ;; definitions of compilers that can be swapped in and out for compiling
  24. ;; source code. Users can also define new compiler types whenever they
  25. ;; some customized behavior.
  26. ;;
  27. ;; The `ede-makefile-rule' class lets users add customized rules into their
  28. ;; objects, and also lets different compilers add chaining rules to their
  29. ;; behaviors.
  30. ;;
  31. ;; It is important that all new compiler types be registered once. That
  32. ;; way the chaining rules and variables are inserted into any given Makefile
  33. ;; only once.
  34. ;;
  35. ;; To insert many compiler elements, wrap them in `ede-compiler-begin-unique'
  36. ;; before calling their insert methods.
  37. ;; To write a method that inserts a variable or rule for a compiler
  38. ;; based object, wrap the body of your call in `ede-compiler-only-once'
  39. (eval-when-compile (require 'cl))
  40. (require 'ede) ;source object
  41. (require 'ede/autoconf-edit)
  42. ;;; Types:
  43. (defclass ede-compilation-program (eieio-instance-inheritor)
  44. ((name :initarg :name
  45. :type string
  46. :custom string
  47. :documentation "Name of this type of compiler.")
  48. (variables :initarg :variables
  49. :type list
  50. :custom (repeat (cons (string :tag "Variable")
  51. (string :tag "Value")))
  52. :documentation
  53. "Variables needed in the Makefile for this compiler.
  54. An assoc list where each element is (VARNAME . VALUE) where VARNAME
  55. is a string, and VALUE is either a string, or a list of strings.
  56. For example, GCC would define CC=gcc, and emacs would define EMACS=emacs.")
  57. (sourcetype :initarg :sourcetype
  58. :type list ;; of symbols
  59. :documentation
  60. "A list of `ede-sourcecode' objects this class will handle.
  61. This is used to match target objects with the compilers and linkers
  62. they can use, and which files this object is interested in."
  63. :accessor ede-object-sourcecode)
  64. (rules :initarg :rules
  65. :initform nil
  66. :type list
  67. :custom (repeat (object :objecttype ede-makefile-rule))
  68. :documentation
  69. "Auxiliary rules needed for this compiler to run.
  70. For example, yacc/lex files need additional chain rules, or inferences.")
  71. (commands :initarg :commands
  72. :type list
  73. :custom (repeat string)
  74. :documentation
  75. "The commands used to execute this compiler.
  76. The object which uses this compiler will place these commands after
  77. its rule definition.")
  78. (autoconf :initarg :autoconf
  79. :initform nil
  80. :type list
  81. :custom (repeat string)
  82. :documentation
  83. "Autoconf function to call if this type of compiler is used.
  84. When a project is in Automake mode, this defines the autoconf function to
  85. call to initialize automake to use this compiler.
  86. For example, there may be multiple C compilers, but they all probably
  87. use the same autoconf form.")
  88. (objectextention :initarg :objectextention
  89. :type string
  90. :documentation
  91. "A string which is the extension used for object files.
  92. For example, C code uses .o on Unix, and Emacs Lisp uses .elc.")
  93. )
  94. "A program used to compile or link a program via a Makefile.
  95. Contains everything needed to output code into a Makefile, or autoconf
  96. file.")
  97. (defclass ede-compiler (ede-compilation-program)
  98. ((makedepends :initarg :makedepends
  99. :initform nil
  100. :type boolean
  101. :documentation
  102. "Non-nil if this compiler can make dependencies.")
  103. (uselinker :initarg :uselinker
  104. :initform nil
  105. :type boolean
  106. :documentation
  107. "Non-nil if this compiler creates code that can be linked.
  108. This requires that the containing target also define a list of available
  109. linkers that can be used.")
  110. )
  111. "Definition for a compiler.
  112. Different types of objects will provide different compilers for
  113. different situations.")
  114. (defclass ede-linker (ede-compilation-program)
  115. ()
  116. "Contains information needed to link many generated object files together.")
  117. (defclass ede-makefile-rule ()
  118. ((target :initarg :target
  119. :initform ""
  120. :type string
  121. :custom string
  122. :documentation "The target pattern.
  123. A pattern of \"%.o\" is used for inference rules, and would match object files.
  124. A target of \"foo.o\" explicitly matches the file foo.o.")
  125. (dependencies :initarg :dependencies
  126. :initform ""
  127. :type string
  128. :custom string
  129. :documentation "Dependencies on this target.
  130. A pattern of \"%.o\" would match a file of the same prefix as the target
  131. if that target is also an inference rule pattern.
  132. A dependency of \"foo.c\" explicitly lists foo.c as a dependency.
  133. A variable such as $(name_SOURCES) will list all the source files
  134. belonging to the target name.")
  135. (rules :initarg :rules
  136. :initform nil
  137. :type list
  138. :custom (repeat string)
  139. :documentation "Scripts to execute.
  140. These scripts will be executed in sh (Unless the SHELL variable is overridden).
  141. Do not prefix with TAB.
  142. Each individual element of this list can be either a string, or
  143. a lambda function. (The custom element does not yet express that.")
  144. (phony :initarg :phony
  145. :initform nil
  146. :type boolean
  147. :custom boolean
  148. :documentation "Is this a phony rule?
  149. Adds this rule to a .PHONY list."))
  150. "A single rule for building some target.")
  151. ;;; Code:
  152. (defvar ede-compiler-list nil
  153. "The master list of all EDE compilers.")
  154. (defvar ede-linker-list nil
  155. "The master list of all EDE compilers.")
  156. (defvar ede-current-build-list nil
  157. "List of EDE compilers that have already inserted parts of themselves.
  158. This is used when creating a Makefile to prevent duplicate variables and
  159. rules from being created.")
  160. (defmethod initialize-instance :AFTER ((this ede-compiler) &rest fields)
  161. "Make sure that all ede compiler objects are cached in
  162. `ede-compiler-list'."
  163. (add-to-list 'ede-compiler-list this))
  164. (defmethod initialize-instance :AFTER ((this ede-linker) &rest fields)
  165. "Make sure that all ede compiler objects are cached in
  166. `ede-linker-list'."
  167. (add-to-list 'ede-linker-list this))
  168. (defmacro ede-compiler-begin-unique (&rest body)
  169. "Execute BODY, making sure that `ede-current-build-list' is maintained.
  170. This will prevent rules from creating duplicate variables or rules."
  171. `(let ((ede-current-build-list nil))
  172. ,@body))
  173. (defmacro ede-compiler-only-once (object &rest body)
  174. "Using OBJECT, execute BODY only once per Makefile generation."
  175. `(if (not (member ,object ede-current-build-list))
  176. (progn
  177. (add-to-list 'ede-current-build-list ,object)
  178. ,@body)))
  179. (defmacro ede-linker-begin-unique (&rest body)
  180. "Execute BODY, making sure that `ede-current-build-list' is maintained.
  181. This will prevent rules from creating duplicate variables or rules."
  182. `(let ((ede-current-build-list nil))
  183. ,@body))
  184. (defmacro ede-linker-only-once (object &rest body)
  185. "Using OBJECT, execute BODY only once per Makefile generation."
  186. `(if (not (member ,object ede-current-build-list))
  187. (progn
  188. (add-to-list 'ede-current-build-list ,object)
  189. ,@body)))
  190. (add-hook 'edebug-setup-hook
  191. (lambda ()
  192. (def-edebug-spec ede-compiler-begin-unique def-body)
  193. (def-edebug-spec ede-compiler-only-once (form def-body))
  194. (def-edebug-spec ede-linker-begin-unique def-body)
  195. (def-edebug-spec ede-linker-only-once (form def-body))
  196. (def-edebug-spec ede-pmake-insert-variable-shared (form def-body))
  197. ))
  198. ;;; Querys
  199. (defun ede-proj-find-compiler (compilers sourcetype)
  200. "Return a compiler from the list COMPILERS that will compile SOURCETYPE."
  201. (while (and compilers
  202. (not (member sourcetype (oref (car compilers) sourcetype))))
  203. (setq compilers (cdr compilers)))
  204. (car-safe compilers))
  205. (defun ede-proj-find-linker (linkers sourcetype)
  206. "Return a compiler from the list LINKERS to be used with SOURCETYPE."
  207. (while (and linkers
  208. (slot-boundp (car linkers) 'sourcetype)
  209. (not (member sourcetype (oref (car linkers) sourcetype))))
  210. (setq linkers (cdr linkers)))
  211. (car-safe linkers))
  212. ;;; Methods:
  213. (defmethod ede-proj-tweak-autoconf ((this ede-compilation-program))
  214. "Tweak the configure file (current buffer) to accommodate THIS."
  215. (mapcar
  216. (lambda (obj)
  217. (cond ((stringp obj)
  218. (autoconf-insert-new-macro obj))
  219. ((consp obj)
  220. (autoconf-insert-new-macro (car obj) (cdr obj)))
  221. (t (error "Autoconf directives must be a string, or cons cell")))
  222. )
  223. (oref this autoconf)))
  224. (defmethod ede-proj-flush-autoconf ((this ede-compilation-program))
  225. "Flush the configure file (current buffer) to accommodate THIS."
  226. nil)
  227. (defmacro proj-comp-insert-variable-once (varname &rest body)
  228. "Add VARNAME into the current Makefile if it doesn't exist.
  229. Execute BODY in a location where a value can be placed."
  230. `(let ((addcr t) (v ,varname))
  231. (unless (re-search-backward (concat "^" v "\\s-*=") nil t)
  232. (insert v "=")
  233. ,@body
  234. (if addcr (insert "\n"))
  235. (goto-char (point-max)))
  236. ))
  237. (put 'proj-comp-insert-variable-once 'lisp-indent-function 1)
  238. (defmethod ede-proj-makefile-insert-variables ((this ede-compilation-program))
  239. "Insert variables needed by the compiler THIS."
  240. (if (eieio-instance-inheritor-slot-boundp this 'variables)
  241. (with-slots (variables) this
  242. (mapcar
  243. (lambda (var)
  244. (proj-comp-insert-variable-once (car var)
  245. (let ((cd (cdr var)))
  246. (if (listp cd)
  247. (mapc (lambda (c) (insert " " c)) cd)
  248. (insert cd)))))
  249. variables))))
  250. (defmethod ede-compiler-intermediate-objects-p ((this ede-compiler))
  251. "Return non-nil if THIS has intermediate object files.
  252. If this compiler creates code that can be linked together,
  253. then the object files created by the compiler are considered intermediate."
  254. (oref this uselinker))
  255. (defmethod ede-compiler-intermediate-object-variable ((this ede-compiler)
  256. targetname)
  257. "Return a string based on THIS representing a make object variable.
  258. TARGETNAME is the name of the target that these objects belong to."
  259. (concat targetname "_OBJ"))
  260. (defmethod ede-proj-makefile-insert-object-variables ((this ede-compiler)
  261. targetname sourcefiles)
  262. "Insert an OBJ variable to specify object code to be generated for THIS.
  263. The name of the target is TARGETNAME as a string. SOURCEFILES is the list of
  264. files to be objectified.
  265. Not all compilers do this."
  266. (if (ede-compiler-intermediate-objects-p this)
  267. (progn
  268. (insert (ede-compiler-intermediate-object-variable this targetname)
  269. "=")
  270. (let ((src (oref this sourcetype)))
  271. (mapc (lambda (s)
  272. (let ((ts src))
  273. (while (and ts (not (ede-want-file-source-p
  274. (symbol-value (car ts)) s)))
  275. (setq ts (cdr ts)))
  276. ;; Only insert the object if the given file is a major
  277. ;; source-code type.
  278. (if ts;; a match as a source file.
  279. (insert " " (file-name-sans-extension s)
  280. (oref this objectextention)))))
  281. sourcefiles)
  282. (insert "\n")))))
  283. (defmethod ede-proj-makefile-insert-rules ((this ede-compilation-program))
  284. "Insert rules needed for THIS compiler object."
  285. (ede-compiler-only-once this
  286. (mapc 'ede-proj-makefile-insert-rules (oref this rules))))
  287. (defmethod ede-proj-makefile-insert-rules ((this ede-makefile-rule))
  288. "Insert rules needed for THIS rule object."
  289. (if (oref this phony) (insert ".PHONY: (oref this target)\n"))
  290. (insert (oref this target) ": " (oref this dependencies) "\n\t"
  291. (mapconcat (lambda (c) c) (oref this rules) "\n\t")
  292. "\n\n"))
  293. (defmethod ede-proj-makefile-insert-commands ((this ede-compilation-program))
  294. "Insert the commands needed to use compiler THIS.
  295. The object creating makefile rules must call this method for the
  296. compiler it decides to use after inserting in the rule."
  297. (when (slot-boundp this 'commands)
  298. (with-slots (commands) this
  299. (mapc
  300. (lambda (obj) (insert "\t"
  301. (cond ((stringp obj)
  302. obj)
  303. ((and (listp obj)
  304. (eq (car obj) 'lambda))
  305. (funcall obj))
  306. (t
  307. (format "%S" obj)))
  308. "\n"))
  309. commands))
  310. (insert "\n")))
  311. ;;; Some details about our new macro
  312. ;;
  313. (add-hook 'edebug-setup-hook
  314. (lambda ()
  315. (def-edebug-spec ede-compiler-begin-unique def-body)))
  316. (put 'ede-compiler-begin-unique 'lisp-indent-function 0)
  317. (put 'ede-compiler-only-once 'lisp-indent-function 1)
  318. (put 'ede-linker-begin-unique 'lisp-indent-function 0)
  319. (put 'ede-linker-only-once 'lisp-indent-function 1)
  320. (provide 'ede/proj-comp)
  321. ;;; ede/proj-comp.el ends here