summaryrefslogtreecommitdiff
path: root/lisp/cedet/ede/proj-comp.el
blob: abe5866de0a3d8b28634344ccd699caac910c009 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
;;; ede/proj-comp.el --- EDE Generic Project compiler/rule driver

;; Copyright (C) 1999-2001, 2004-2005, 2007, 2009-2015 Free Software
;; Foundation, Inc.

;; Author: Eric M. Ludlam <zappo@gnu.org>
;; Keywords: project, make

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; This software handles the maintenance of compiler and rule definitions
;; for different object types.
;;
;; The `ede-compiler' class lets different types of project objects create
;; definitions of compilers that can be swapped in and out for compiling
;; source code.  Users can also define new compiler types whenever they
;; some customized behavior.
;;
;; The `ede-makefile-rule' class lets users add customized rules into their
;; objects, and also lets different compilers add chaining rules to their
;; behaviors.
;;
;; It is important that all new compiler types be registered once.  That
;; way the chaining rules and variables are inserted into any given Makefile
;; only once.
;;
;; To insert many compiler elements, wrap them in `ede-compiler-begin-unique'
;; before calling their insert methods.
;; To write a method that inserts a variable or rule for a compiler
;; based object, wrap the body of your call in `ede-compiler-only-once'

(eval-when-compile (require 'cl))
(require 'ede)				;source object
(require 'ede/autoconf-edit)

;;; Types:
(defclass ede-compilation-program (eieio-instance-inheritor)
  ((name :initarg :name
	 :type string
	 :custom string
	 :documentation "Name of this type of compiler.")
   (variables :initarg :variables
	      :type list
	      :custom (repeat (cons (string :tag "Variable")
				    (string :tag "Value")))
	      :documentation
	      "Variables needed in the Makefile for this compiler.
An assoc list where each element is (VARNAME . VALUE) where VARNAME
is a string, and VALUE is either a string, or a list of strings.
For example, GCC would define CC=gcc, and emacs would define EMACS=emacs.")
   (sourcetype :initarg :sourcetype
	       :type list ;; of symbols
	       :documentation
	       "A list of `ede-sourcecode' objects this class will handle.
This is used to match target objects with the compilers and linkers
they can use, and which files this object is interested in."
	       :accessor ede-object-sourcecode)
   (rules :initarg :rules
	  :initform nil
	  :type list
	  :custom (repeat (object :objecttype ede-makefile-rule))
	  :documentation
	  "Auxiliary rules needed for this compiler to run.
For example, yacc/lex files need additional chain rules, or inferences.")
   (commands :initarg :commands
	    :type list
	    :custom (repeat string)
	    :documentation
	    "The commands used to execute this compiler.
The object which uses this compiler will place these commands after
its rule definition.")
   (autoconf :initarg :autoconf
	     :initform nil
	     :type list
	     :custom (repeat string)
	     :documentation
	     "Autoconf function to call if this type of compiler is used.
When a project is in Automake mode, this defines the autoconf function to
call to initialize automake to use this compiler.
For example, there may be multiple C compilers, but they all probably
use the same autoconf form.")
   (objectextention :initarg :objectextention
		    :type string
		    :documentation
		    "A string which is the extension used for object files.
For example, C code uses .o on Unix, and Emacs Lisp uses .elc.")
   )
  "A program used to compile or link a program via a Makefile.
Contains everything needed to output code into a Makefile, or autoconf
file.")

(defclass ede-compiler (ede-compilation-program)
  ((makedepends :initarg :makedepends
		:initform nil
		:type boolean
		:documentation
		"Non-nil if this compiler can make dependencies.")
   (uselinker :initarg :uselinker
	      :initform nil
	      :type boolean
	      :documentation
	      "Non-nil if this compiler creates code that can be linked.
This requires that the containing target also define a list of available
linkers that can be used.")
   )
  "Definition for a compiler.
Different types of objects will provide different compilers for
different situations.")

(defclass ede-linker (ede-compilation-program)
  ()
  "Contains information needed to link many generated object files together.")

(defclass ede-makefile-rule ()
  ((target :initarg :target
	   :initform ""
	   :type string
	   :custom string
	   :documentation "The target pattern.
A pattern of \"%.o\" is used for inference rules, and would match object files.
A target of \"foo.o\" explicitly matches the file foo.o.")
   (dependencies :initarg :dependencies
		 :initform ""
		 :type string
		 :custom string
		 :documentation "Dependencies on this target.
A pattern of \"%.o\" would match a file of the same prefix as the target
if that target is also an inference rule pattern.
A dependency of \"foo.c\" explicitly lists foo.c as a dependency.
A variable such as $(name_SOURCES) will list all the source files
belonging to the target name.")
   (rules :initarg :rules
	  :initform nil
	  :type list
	  :custom (repeat string)
	  :documentation "Scripts to execute.
These scripts will be executed in sh (Unless the SHELL variable is overridden).
Do not prefix with TAB.
Each individual element of this list can be either a string, or
a lambda function.  (The custom element does not yet express that.")
   (phony :initarg :phony
	  :initform nil
	  :type boolean
	  :custom boolean
	  :documentation "Is this a phony rule?
Adds this rule to a .PHONY list."))
  "A single rule for building some target.")

;;; Code:
(defvar ede-compiler-list nil
  "The master list of all EDE compilers.")

(defvar ede-linker-list nil
  "The master list of all EDE compilers.")

(defvar ede-current-build-list nil
  "List of EDE compilers that have already inserted parts of themselves.
This is used when creating a Makefile to prevent duplicate variables and
rules from being created.")

(defmethod initialize-instance :AFTER ((this ede-compiler) &rest fields)
  "Make sure that all ede compiler objects are cached in
`ede-compiler-list'."
  (add-to-list 'ede-compiler-list this))

(defmethod initialize-instance :AFTER ((this ede-linker) &rest fields)
  "Make sure that all ede compiler objects are cached in
`ede-linker-list'."
  (add-to-list 'ede-linker-list this))

(defmacro ede-compiler-begin-unique (&rest body)
  "Execute BODY, making sure that `ede-current-build-list' is maintained.
This will prevent rules from creating duplicate variables or rules."
  `(let ((ede-current-build-list nil))
    ,@body))

(defmacro ede-compiler-only-once (object &rest body)
  "Using OBJECT, execute BODY only once per Makefile generation."
  `(if (not (member ,object ede-current-build-list))
       (progn
	 (add-to-list 'ede-current-build-list ,object)
	 ,@body)))

(defmacro ede-linker-begin-unique (&rest body)
  "Execute BODY, making sure that `ede-current-build-list' is maintained.
This will prevent rules from creating duplicate variables or rules."
  `(let ((ede-current-build-list nil))
    ,@body))

(defmacro ede-linker-only-once (object &rest body)
  "Using OBJECT, execute BODY only once per Makefile generation."
  `(if (not (member ,object ede-current-build-list))
       (progn
	 (add-to-list 'ede-current-build-list ,object)
	 ,@body)))

(add-hook 'edebug-setup-hook
	  (lambda ()
	    (def-edebug-spec ede-compiler-begin-unique def-body)
	    (def-edebug-spec ede-compiler-only-once (form def-body))
	    (def-edebug-spec ede-linker-begin-unique def-body)
	    (def-edebug-spec ede-linker-only-once (form def-body))
	    (def-edebug-spec ede-pmake-insert-variable-shared (form def-body))
	    ))

;;; Queries
(defun ede-proj-find-compiler (compilers sourcetype)
  "Return a compiler from the list COMPILERS that will compile SOURCETYPE."
  (while (and compilers
	      (not (member sourcetype (oref (car compilers) sourcetype))))
    (setq compilers (cdr compilers)))
  (car-safe compilers))

(defun ede-proj-find-linker (linkers sourcetype)
  "Return a compiler from the list LINKERS to be used with SOURCETYPE."
  (while (and linkers
	      (slot-boundp (car linkers) 'sourcetype)
	      (not (member sourcetype (oref (car linkers) sourcetype))))
    (setq linkers (cdr linkers)))
  (car-safe linkers))

;;; Methods:
(defmethod ede-proj-tweak-autoconf ((this ede-compilation-program))
  "Tweak the configure file (current buffer) to accommodate THIS."
  (mapcar
   (lambda (obj)
     (cond ((stringp obj)
	      (autoconf-insert-new-macro obj))
	     ((consp obj)
	      (autoconf-insert-new-macro (car obj) (cdr obj)))
	     (t (error "Autoconf directives must be a string, or cons cell")))
     )
   (oref this autoconf)))

(defmethod ede-proj-flush-autoconf ((this ede-compilation-program))
  "Flush the configure file (current buffer) to accommodate THIS."
  nil)

(defmacro proj-comp-insert-variable-once (varname &rest body)
  "Add VARNAME into the current Makefile if it doesn't exist.
Execute BODY in a location where a value can be placed."
  `(let ((addcr t) (v ,varname))
     (unless (re-search-backward (concat "^" v "\\s-*=") nil t)
       (insert v "=")
       ,@body
       (if addcr (insert "\n"))
       (goto-char (point-max)))
     ))
(put 'proj-comp-insert-variable-once 'lisp-indent-function 1)

(defmethod ede-proj-makefile-insert-variables ((this ede-compilation-program))
  "Insert variables needed by the compiler THIS."
  (if (eieio-instance-inheritor-slot-boundp this 'variables)
      (with-slots (variables) this
	(mapcar
	 (lambda (var)
	   (proj-comp-insert-variable-once (car var)
	     (let ((cd (cdr var)))
	       (if (listp cd)
		   (mapc (lambda (c) (insert " " c)) cd)
		 (insert cd)))))
	 variables))))

(defmethod ede-compiler-intermediate-objects-p ((this ede-compiler))
  "Return non-nil if THIS has intermediate object files.
If this compiler creates code that can be linked together,
then the object files created by the compiler are considered intermediate."
  (oref this uselinker))

(defmethod ede-compiler-intermediate-object-variable ((this ede-compiler)
						      targetname)
  "Return a string based on THIS representing a make object variable.
TARGETNAME is the name of the target that these objects belong to."
  (concat targetname "_OBJ"))

(defmethod ede-proj-makefile-insert-object-variables ((this ede-compiler)
						      targetname sourcefiles)
  "Insert an OBJ variable to specify object code to be generated for THIS.
The name of the target is TARGETNAME as a string.  SOURCEFILES is the list of
files to be objectified.
Not all compilers do this."
  (if (ede-compiler-intermediate-objects-p this)
      (progn
	(insert (ede-compiler-intermediate-object-variable this targetname)
		"=")
	(let ((src (oref this sourcetype)))
	  (mapc (lambda (s)
		  (let ((ts src))
		    (while (and ts (not (ede-want-file-source-p
					 (symbol-value (car ts)) s)))
		      (setq ts (cdr ts)))
		    ;; Only insert the object if the given file is a major
		    ;; source-code type.
		    (if ts;; a match as a source file.
			(insert " " (file-name-sans-extension s)
				(oref this objectextention)))))
		sourcefiles)
	  (insert "\n")))))

(defmethod ede-proj-makefile-insert-rules ((this ede-compilation-program))
  "Insert rules needed for THIS compiler object."
  (ede-compiler-only-once this
    (mapc 'ede-proj-makefile-insert-rules (oref this rules))))

(defmethod ede-proj-makefile-insert-rules ((this ede-makefile-rule))
  "Insert rules needed for THIS rule object."
  (if (oref this phony) (insert ".PHONY: " (oref this target) "\n"))
  (insert (oref this target) ": " (oref this dependencies) "\n\t"
	  (mapconcat (lambda (c) c) (oref this rules) "\n\t")
	  "\n\n"))

(defmethod ede-proj-makefile-insert-commands ((this ede-compilation-program))
  "Insert the commands needed to use compiler THIS.
The object creating makefile rules must call this method for the
compiler it decides to use after inserting in the rule."
  (when (slot-boundp this 'commands)
    (with-slots (commands) this
      (mapc
       (lambda (obj) (insert
		      (if (bolp) "\t" " ")
		      (cond ((stringp obj)
			     obj)
			    ((and (listp obj)
				  (eq (car obj) 'lambda))
			     (funcall obj))
			    (t
			     (format "%S" obj)))
		      "\n"))
       commands))
    (insert "\n")))

;;; Some details about our new macro
;;
(add-hook 'edebug-setup-hook
	  (lambda ()
	    (def-edebug-spec ede-compiler-begin-unique def-body)))
(put 'ede-compiler-begin-unique 'lisp-indent-function 0)
(put 'ede-compiler-only-once 'lisp-indent-function 1)
(put 'ede-linker-begin-unique 'lisp-indent-function 0)
(put 'ede-linker-only-once 'lisp-indent-function 1)

(provide 'ede/proj-comp)

;;; ede/proj-comp.el ends here