1 ;;; ede/proj-comp.el --- EDE Generic Project compiler/rule driver
3 ;; Copyright (C) 1999, 2000, 2001, 2004, 2005, 2007, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: project, make
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; This software handles the maintenance of compiler and rule definitions
27 ;; for different object types.
29 ;; The `ede-compiler' class lets different types of project objects create
30 ;; definitions of compilers that can be swapped in and out for compiling
31 ;; source code. Users can also define new compiler types whenever they
32 ;; some customized behavior.
34 ;; The `ede-makefile-rule' class lets users add customized rules into their
35 ;; objects, and also lets different compilers add chaining rules to their
38 ;; It is important that all new compiler types be registered once. That
39 ;; way the chaining rules and variables are inserted into any given Makefile
42 ;; To insert many compiler elements, wrap them in `ede-compiler-begin-unique'
43 ;; before calling their insert methods.
44 ;; To write a method that inserts a variable or rule for a compiler
45 ;; based object, wrap the body of your call in `ede-compiler-only-once'
47 (require 'ede
) ;source object
48 (require 'ede
/autoconf-edit
)
49 (eval-when-compile (require 'ede
/pmake
))
52 (defclass ede-compilation-program
(eieio-instance-inheritor)
56 :documentation
"Name of this type of compiler.")
57 (variables :initarg
:variables
59 :custom
(repeat (cons (string :tag
"Variable")
60 (string :tag
"Value")))
62 "Variables needed in the Makefile for this compiler.
63 An assoc list where each element is (VARNAME . VALUE) where VARNAME
64 is a string, and VALUE is either a string, or a list of strings.
65 For example, GCC would define CC=gcc, and emacs would define EMACS=emacs.")
66 (sourcetype :initarg
:sourcetype
67 :type list
;; of symbols
69 "A list of `ede-sourcecode' objects this class will handle.
70 This is used to match target objects with the compilers and linkers
71 they can use, and which files this object is interested in."
72 :accessor ede-object-sourcecode
)
73 (rules :initarg
:rules
76 :custom
(repeat (object :objecttype ede-makefile-rule
))
78 "Auxiliary rules needed for this compiler to run.
79 For example, yacc/lex files need additional chain rules, or inferences.")
80 (commands :initarg
:commands
82 :custom
(repeat string
)
84 "The commands used to execute this compiler.
85 The object which uses this compiler will place these commands after
86 it's rule definition.")
87 (autoconf :initarg
:autoconf
90 :custom
(repeat string
)
92 "Autoconf function to call if this type of compiler is used.
93 When a project is in Automake mode, this defines the autoconf function to
94 call to initialize automake to use this compiler.
95 For example, there may be multiple C compilers, but they all probably
96 use the same autoconf form.")
97 (objectextention :initarg
:objectextention
100 "A string which is the extention used for object files.
101 For example, C code uses .o on unix, and Emacs Lisp uses .elc.")
103 "A program used to compile or link a program via a Makefile.
104 Contains everything needed to output code into a Makefile, or autoconf
107 (defclass ede-compiler
(ede-compilation-program)
108 ((makedepends :initarg
:makedepends
112 "Non-nil if this compiler can make dependencies.")
113 (uselinker :initarg
:uselinker
117 "Non-nil if this compiler creates code that can be linked.
118 This requires that the containing target also define a list of available
119 linkers that can be used.")
121 "Definition for a compiler.
122 Different types of objects will provide different compilers for
123 different situations.")
125 (defclass ede-linker
(ede-compilation-program)
127 "Contains information needed to link many generated object files together.")
129 (defclass ede-makefile-rule
()
130 ((target :initarg
:target
134 :documentation
"The target pattern.
135 A pattern of \"%.o\" is used for inference rules, and would match object files.
136 A target of \"foo.o\" explicitly matches the file foo.o.")
137 (dependencies :initarg
:dependencies
141 :documentation
"Dependencies on this target.
142 A pattern of \"%.o\" would match a file of the same prefix as the target
143 if that target is also an inference rule pattern.
144 A dependency of \"foo.c\" explicitly lists foo.c as a dependency.
145 A variable such as $(name_SOURCES) will list all the source files
146 belonging to the target name.")
147 (rules :initarg
:rules
150 :custom
(repeat string
)
151 :documentation
"Scripts to execute.
152 These scripst will be executed in sh (Unless the SHELL variable is overriden).
153 Do not prefix with TAB.
154 Each individual element of this list can be either a string, or
155 a lambda function. (The custom element does not yet express that.")
156 (phony :initarg
:phony
160 :documentation
"Is this a phony rule?
161 Adds this rule to a .PHONY list."))
162 "A single rule for building some target.")
165 (defvar ede-compiler-list nil
166 "The master list of all EDE compilers.")
168 (defvar ede-linker-list nil
169 "The master list of all EDE compilers.")
171 (defvar ede-current-build-list nil
172 "List of EDE compilers that have already inserted parts of themselves.
173 This is used when creating a Makefile to prevent duplicate variables and
174 rules from being created.")
176 (defmethod initialize-instance :AFTER
((this ede-compiler
) &rest fields
)
177 "Make sure that all ede compiler objects are cached in
178 `ede-compiler-list'."
179 (add-to-list 'ede-compiler-list this
))
181 (defmethod initialize-instance :AFTER
((this ede-linker
) &rest fields
)
182 "Make sure that all ede compiler objects are cached in
184 (add-to-list 'ede-linker-list this
))
186 (defmacro ede-compiler-begin-unique
(&rest body
)
187 "Execute BODY, making sure that `ede-current-build-list' is maintained.
188 This will prevent rules from creating duplicate variables or rules."
189 `(let ((ede-current-build-list nil
))
192 (defmacro ede-compiler-only-once
(object &rest body
)
193 "Using OBJECT, execute BODY only once per Makefile generation."
194 `(if (not (member ,object ede-current-build-list
))
196 (add-to-list 'ede-current-build-list
,object
)
199 (defmacro ede-linker-begin-unique
(&rest body
)
200 "Execute BODY, making sure that `ede-current-build-list' is maintained.
201 This will prevent rules from creating duplicate variables or rules."
202 `(let ((ede-current-build-list nil
))
205 (defmacro ede-linker-only-once
(object &rest body
)
206 "Using OBJECT, execute BODY only once per Makefile generation."
207 `(if (not (member ,object ede-current-build-list
))
209 (add-to-list 'ede-current-build-list
,object
)
212 (add-hook 'edebug-setup-hook
214 (def-edebug-spec ede-compiler-begin-unique def-body
)
215 (def-edebug-spec ede-compiler-only-once
(form def-body
))
216 (def-edebug-spec ede-linker-begin-unique def-body
)
217 (def-edebug-spec ede-linker-only-once
(form def-body
))
218 (def-edebug-spec ede-pmake-insert-variable-shared
(form def-body
))
222 (defun ede-proj-find-compiler (compilers sourcetype
)
223 "Return a compiler from the list COMPILERS that will compile SOURCETYPE."
224 (while (and compilers
225 (not (member sourcetype
(oref (car compilers
) sourcetype
))))
226 (setq compilers
(cdr compilers
)))
227 (car-safe compilers
))
229 (defun ede-proj-find-linker (linkers sourcetype
)
230 "Return a compiler from the list LINKERS to be used with SOURCETYPE."
232 (slot-boundp (car linkers
) 'sourcetype
)
233 (not (member sourcetype
(oref (car linkers
) sourcetype
))))
234 (setq linkers
(cdr linkers
)))
238 (defmethod ede-proj-tweak-autoconf ((this ede-compilation-program
))
239 "Tweak the configure file (current buffer) to accomodate THIS."
243 (autoconf-insert-new-macro obj
))
245 (autoconf-insert-new-macro (car obj
) (cdr obj
)))
246 (t (error "Autoconf directives must be a string, or cons cell")))
248 (oref this autoconf
)))
250 (defmethod ede-proj-flush-autoconf ((this ede-compilation-program
))
251 "Flush the configure file (current buffer) to accomodate THIS."
254 (defmethod ede-proj-makefile-insert-variables ((this ede-compilation-program
))
255 "Insert variables needed by the compiler THIS."
256 (if (eieio-instance-inheritor-slot-boundp this
'variables
)
257 (with-slots (variables) this
260 (ede-pmake-insert-variable-once (car var
)
261 (let ((cd (cdr var
)))
263 (mapc (lambda (c) (insert " " c
)) cd
)
267 (defmethod ede-compiler-intermediate-objects-p ((this ede-compiler
))
268 "Return non-nil if THIS has intermediate object files.
269 If this compiler creates code that can be linked together,
270 then the object files created by the compiler are considered intermediate."
271 (oref this uselinker
))
273 (defmethod ede-compiler-intermediate-object-variable ((this ede-compiler
)
275 "Return a string based on THIS representing a make object variable.
276 TARGETNAME is the name of the target that these objects belong to."
277 (concat targetname
"_OBJ"))
279 (defmethod ede-proj-makefile-insert-object-variables ((this ede-compiler
)
280 targetname sourcefiles
)
281 "Insert an OBJ variable to specify object code to be generated for THIS.
282 The name of the target is TARGETNAME as a string. SOURCEFILES is the list of
283 files to be objectified.
284 Not all compilers do this."
285 (if (ede-compiler-intermediate-objects-p this
)
287 (insert (ede-compiler-intermediate-object-variable this targetname
)
289 (let ((src (oref this sourcetype
)))
292 (while (and ts
(not (ede-want-file-source-p
293 (symbol-value (car ts
)) s
)))
295 ;; Only insert the object if the given file is a major
297 (if ts
;; a match as a source file.
298 (insert " " (file-name-sans-extension s
)
299 (oref this objectextention
)))))
303 (defmethod ede-proj-makefile-insert-rules ((this ede-compilation-program
))
304 "Insert rules needed for THIS compiler object."
305 (ede-compiler-only-once this
306 (mapc 'ede-proj-makefile-insert-rules
(oref this rules
))))
308 (defmethod ede-proj-makefile-insert-rules ((this ede-makefile-rule
))
309 "Insert rules needed for THIS rule object."
310 (if (oref this phony
) (insert ".PHONY: (oref this target)\n"))
311 (insert (oref this target
) ": " (oref this dependencies
) "\n\t"
312 (mapconcat (lambda (c) c
) (oref this rules
) "\n\t")
315 (defmethod ede-proj-makefile-insert-commands ((this ede-compilation-program
))
316 "Insert the commands needed to use compiler THIS.
317 The object creating makefile rules must call this method for the
318 compiler it decides to use after inserting in the rule."
319 (when (slot-boundp this
'commands
)
320 (with-slots (commands) this
322 (lambda (obj) (insert "\t"
326 (eq (car obj
) 'lambda
))
334 ;;; Some details about our new macro
336 (add-hook 'edebug-setup-hook
338 (def-edebug-spec ede-compiler-begin-unique def-body
)))
339 (put 'ede-compiler-begin-unique
'lisp-indent-function
0)
340 (put 'ede-compiler-only-once
'lisp-indent-function
1)
341 (put 'ede-linker-begin-unique
'lisp-indent-function
0)
342 (put 'ede-linker-only-once
'lisp-indent-function
1)
344 (provide 'ede
/proj-comp
)
346 ;; arch-tag: ade67766-1a5d-467a-826a-93e95594d717
347 ;;; ede/proj-comp.el ends here