Added EmacsConfigurationAndHelp directory
[temp.git] / site-lisp / cedet-1.0pre4 / ede / ede-pmake.el
blobd8b5f7cca72fc8c35f7fb6afb04d35e0cb407ba2
1 ;;; ede-pmake.el --- EDE Generic Project Makefile code generator.
3 ;;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7 ;; RCS: $Id: ede-pmake.el,v 1.47 2007/02/19 13:46:24 zappo Exp $
9 ;; This software 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 2, or (at your option)
12 ;; any later version.
14 ;; This software 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.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
24 ;;; Commentary:
25 ;;
26 ;; Code generator for Makefiles.
28 ;; Here is how it should work:
29 ;; 1) Collect information about the project and targets
30 ;; 2) Insert header into the Makefile
31 ;; 3) Insert basic variables (target/source)
32 ;; 4) Conditional
33 ;; a) Makefile
34 ;; 1) Insert support variables (compiler variables, etc)
35 ;; 2) Insert VERSION and DISTDIR
36 ;; 3) Specify top build dir if necessary
37 ;; 4) Specify compile/link commands (c, etc)
38 ;; 5) Specify dependency files
39 ;; 6) Specify all: target
40 ;; 7) Include dependency files
41 ;; 8) Insert commonized target specify rules
42 ;; 9) Insert clean: and dist: rules
43 ;; b) Automake file
44 ;; 1) Insert distribution source variables for targets
45 ;; 2) Insert user requested rules
47 (require 'ede-proj)
48 (require 'ede-proj-obj)
49 (require 'ede-proj-comp)
51 ;;; Code:
52 (defmethod ede-proj-makefile-create ((this ede-proj-project) mfilename)
53 "Create a Makefile for all Makefile targets in THIS.
54 MFILENAME is the makefile to generate."
55 (let ((mt nil) tmp
56 (isdist (string= mfilename (ede-proj-dist-makefile this)))
57 (depth 0)
58 (tmp this)
60 ;; Find out how deep this project is.
61 (while (ede-parent-project tmp)
62 (setq depth (1+ depth)
63 tmp (ede-parent-project tmp)))
64 ;; Collect the targets that belong in a makefile.
65 (mapcar
66 (lambda (obj)
67 (if (and (obj-of-class-p obj 'ede-proj-target-makefile)
68 (string= (oref obj makefile) mfilename))
69 (setq mt (cons obj mt))))
70 (oref this targets))
71 ;; Fix the order so things compile in the right direction.
72 (setq mt (nreverse mt))
73 ;; Add in the header part of the Makefile*
74 (save-excursion
75 (set-buffer (find-file-noselect mfilename))
76 (goto-char (point-min))
77 (if (not (looking-at "# Automatically Generated \\w+ by EDE."))
78 (if (not (y-or-n-p (format "Really replace %s?" mfilename)))
79 (error "Not replacing Makefile."))
80 (message "Replace EDE Makefile"))
81 (erase-buffer)
82 ;; Insert a giant pile of stuff that is common between
83 ;; one of our Makefiles, and a Makefile.in
84 (insert
85 "# Automatically Generated " (file-name-nondirectory mfilename)
86 " by EDE.\n"
87 "# For use with: "
88 (with-slots (makefile-type) this
89 (cond ((eq makefile-type 'Makefile) "make")
90 ((eq makefile-type 'Makefile.in) "autoconf")
91 ((eq makefile-type 'Makefile.am) "automake")
92 (t (error ":makefile-type in project invalid"))))
93 "\n#\n"
94 "# DO NOT MODIFY THIS FILE OR YOUR CHANGES MAY BE LOST.\n"
95 "# EDE is the Emacs Development Environment.\n"
96 "# http://cedet.sourceforge.net/ede.shtml\n"
97 "# \n")
98 ;; Just this project's variables
99 (ede-proj-makefile-insert-variables this)
100 ;; Space
101 (insert "\n")
102 (cond
103 ((eq (oref this makefile-type) 'Makefile)
104 (let* ((targ (if isdist (oref this targets) mt))
105 (sp (oref this subproj))
106 (df (apply 'append
107 (mapcar (lambda (tg)
108 (ede-proj-makefile-dependency-files tg))
109 targ))))
110 ;; Distribution variables
111 (ede-compiler-begin-unique
112 (mapcar 'ede-proj-makefile-insert-variables targ))
113 ;; Only add the distribution stuff in when depth != 0
114 (let ((top (ede-toplevel this))
115 (tmp this)
116 (subdir ""))
117 (insert "VERSION=" (oref top version) "\n"
118 "DISTDIR=$(top)" (oref top name) "-$(VERSION)")
119 (while (ede-parent-project tmp)
120 (setq subdir
121 (concat
123 (file-name-nondirectory
124 (directory-file-name
125 (file-name-directory (oref tmp file))))
126 subdir)
127 tmp (ede-parent-project tmp)))
128 (insert subdir "\n"))
129 ;; Some built in variables for C code
130 (if df
131 (let ((tc depth))
132 (insert "top_builddir = ")
133 (while (/= 0 tc)
134 (setq tc (1- tc))
135 (insert "..")
136 (if (/= tc 0) (insert "/")))
137 (insert "\n")))
138 (insert "\n")
139 ;; Create a variable with all the dependency files to include
140 ;; These methods borrowed from automake.
141 (if (and (oref this automatic-dependencies) df)
142 (progn
143 (insert "DEP_FILES="
144 (mapconcat (lambda (f)
145 (concat ".deps/"
146 (file-name-nondirectory
147 (file-name-sans-extension
148 f)) ".P"))
149 df " "))))
151 ;; Insert ALL Rule
153 (insert "\n\nall:")
154 (mapc (lambda (c)
155 (if (and (slot-exists-p c 'partofall) (oref c partofall))
156 ;; Only insert this rule if it is a part of ALL.
157 (insert " " (ede-proj-makefile-target-name c))))
158 targ)
159 (mapc (lambda (c)
160 (insert " " (ede-name c))
163 (insert "\n\n")
165 ;; Add in the include files
167 (mapc (lambda (c)
168 (insert "include " c "\n\n"))
169 (oref this include-file))
170 ;; Some C inference rules
171 ;; Dependency rules borrowed from automake.
172 (if (and (oref this automatic-dependencies) df)
173 (insert "DEPS_MAGIC := $(shell mkdir .deps > /dev/null "
174 "2>&1 || :)\n"
175 "-include $(DEP_FILES)\n\n"))
177 ;; General makefile rules stored in the individual targets
179 (ede-compiler-begin-unique
180 (ede-proj-makefile-insert-rules this)
181 (mapc 'ede-proj-makefile-insert-rules targ))
183 ;; phony targets for sub projects
185 (mapc 'ede-proj-makefile-insert-subproj-rules sp)
187 ;; Distribution rules such as CLEAN and DIST
189 (when isdist
190 (ede-proj-makefile-tags this mt)
191 (ede-proj-makefile-insert-dist-rules this)))
192 (save-buffer))
193 ((eq (oref this makefile-type) 'Makefile.in)
194 (error "Makefile.in is not supported"))
195 ((eq (oref this makefile-type) 'Makefile.am)
196 (require 'ede-pconf)
197 ;; Distribution variables
198 (let ((targ (if isdist (oref this targets) mt)))
199 (ede-compiler-begin-unique
200 (mapc 'ede-proj-makefile-insert-automake-pre-variables targ))
201 (ede-compiler-begin-unique
202 (mapc 'ede-proj-makefile-insert-source-variables targ))
203 (ede-compiler-begin-unique
204 (mapc 'ede-proj-makefile-insert-automake-post-variables targ))
205 (ede-compiler-begin-unique
206 (ede-proj-makefile-insert-user-rules this))
207 (insert "\n# End of Makefile.am\n")
208 (save-buffer))
210 (t (error "Unknown makefile type when generating Makefile")))
211 ;; Put the cursor in a nice place
212 (goto-char (point-min)))))
214 ;;; VARIABLE insertion
216 (defun ede-pmake-end-of-variable ()
217 "Move to the end of the variable declaration under point."
218 (end-of-line)
219 (while (= (preceding-char) ?\\)
220 (forward-char 1)
221 (end-of-line))
224 (defmacro ede-pmake-insert-variable-shared (varname &rest body)
225 "Add VARNAME into the current Makefile.
226 Execute BODY in a location where a value can be placed."
227 `(let ((addcr t) (v ,varname))
228 (if (re-search-backward (concat "^" v "\\s-*=") nil t)
229 (progn
230 (ede-pmake-end-of-variable)
231 (if (< (current-column) 40)
232 (if (and (/= (preceding-char) ?=)
233 (/= (preceding-char) ? ))
234 (insert " "))
235 (insert "\\\n "))
236 (setq addcr nil))
237 (insert v "="))
238 ,@body
239 (if addcr (insert "\n"))
240 (goto-char (point-max))))
241 (put 'ede-pmake-insert-variable-shared 'lisp-indent-function 1)
243 ;;; SOURCE VARIABLE NAME CONSTRUCTION
245 ;;;###autoload
246 (defun ede-pmake-varname (obj)
247 "Convert OBJ into a variable name name, which converts . to _."
248 (let ((name (oref obj name)))
249 (while (string-match "\\." name)
250 (setq name (replace-match "_" nil t name)))
251 name))
253 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target))
254 "Return the variable name for THIS's sources."
255 (concat (ede-pmake-varname this) "_YOU_FOUND_A_BUG"))
257 ;;; DEPENDENCY FILE GENERATOR LISTS
259 (defmethod ede-proj-makefile-dependency-files ((this ede-proj-target))
260 "Return a list of source files to convert to dependencies.
261 Argument THIS is the target to get sources from."
262 nil)
264 ;;; GENERIC VARIABLES
266 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-project)
267 configuration)
268 "Return a list of configuration variables from THIS.
269 Use CONFIGURATION as the current configuration to query."
270 (cdr (assoc configuration (oref this configuration-variables))))
272 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-project))
273 "Insert variables needed by target THIS."
274 (let ((conf-table (ede-proj-makefile-configuration-variables
275 this (oref this configuration-default)))
276 (conf-done nil))
277 ;; Insert all variables, and augment them with details from
278 ;; the current configuration.
279 (mapc (lambda (c)
280 (insert (car c) "=")
281 (if (assoc (car c) conf-table)
282 (progn
283 (insert (cdr (assoc (car c) conf-table)) " ")
284 (setq conf-done (cons (car c) conf-done))))
285 (insert (cdr c) "\n"))
286 (oref this variables))
287 ;; Add in all variables from the configuration not allready covered.
288 (mapc (lambda (c)
289 (if (member (car c) conf-done)
291 (insert (car c) "=" (cdr c) "\n")))
292 conf-table))
293 (let* ((top "")
294 (tmp this))
295 (while (ede-parent-project tmp)
296 (setq tmp (ede-parent-project tmp)
297 top (concat "../" top)))
298 (insert "\ntop=" top))
299 (insert "\nede_FILES=" (file-name-nondirectory (oref this file)) " "
300 (file-name-nondirectory (ede-proj-dist-makefile this)) "\n"))
302 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target)
303 &optional
304 moresource)
305 "Insert the source variables needed by THIS.
306 Optional argument MORESOURCE is a list of additional sources to add to the
307 sources variable."
308 (let ((sv (ede-proj-makefile-sourcevar this)))
309 ;; This variable may be shared between targets
310 (ede-pmake-insert-variable-shared (cond ((listp sv) (car sv))
311 (t sv))
312 (insert (mapconcat (lambda (a) a) (oref this source) " "))
313 (if moresource
314 (insert " \\\n " (mapconcat (lambda (a) a) moresource " ") "")))))
316 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-target) &optional
317 moresource)
318 "Insert variables needed by target THIS.
319 Optional argument MORESOURCE is a list of additional sources to add to the
320 sources variable."
321 (ede-proj-makefile-insert-source-variables this moresource)
324 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-target-makefile)
325 configuration)
326 "Return a list of configuration variables from THIS.
327 Use CONFIGURATION as the current configuration to query."
328 (cdr (assoc configuration (oref this configuration-variables))))
330 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-target-makefile)
331 &optional moresource)
332 "Insert variables needed by target THIS.
333 Optional argument MORESOURCE is a list of additional sources to add to the
334 sources variable."
335 (call-next-method)
336 (let* ((proj (ede-target-parent this))
337 (conf-table (ede-proj-makefile-configuration-variables
338 this (oref proj configuration-default)))
339 (conf-done nil)
341 ;; Add in all variables from the configuration not allready covered.
342 (mapc (lambda (c)
343 (if (member (car c) conf-done)
345 (insert (car c) "=" (cdr c) "\n")))
346 conf-table))
347 (let ((comp (ede-proj-compilers this))
348 (link (ede-proj-linkers this))
349 (name (ede-proj-makefile-target-name this))
350 (src (oref this source)))
351 (while comp
352 (ede-compiler-only-once (car comp)
353 (ede-proj-makefile-insert-object-variables (car comp) name src)
354 (ede-proj-makefile-insert-variables (car comp)))
355 (setq comp (cdr comp)))
356 (while link
357 (ede-linker-only-once (car link)
358 (ede-proj-makefile-insert-variables (car link)))
359 (setq link (cdr link)))))
361 (defmethod ede-proj-makefile-insert-automake-pre-variables
362 ((this ede-proj-target))
363 "Insert variables needed by target THIS in Makefile.am before SOURCES."
364 nil)
366 (defmethod ede-proj-makefile-insert-automake-post-variables
367 ((this ede-proj-target))
368 "Insert variables needed by target THIS in Makefile.am after SOURCES."
369 nil)
371 ;;; GARBAGE PATTERNS
373 (defmethod ede-proj-makefile-garbage-patterns ((this ede-proj-project))
374 "Return a list of patterns that are considred garbage to THIS.
375 These are removed with make clean."
376 (let ((mc (ede-map-targets
377 this (lambda (c) (ede-proj-makefile-garbage-patterns c))))
378 (uniq nil))
379 (setq mc (sort (apply 'append mc) 'string<))
380 ;; Filter out duplicates from the targets.
381 (while mc
382 (if (and (car uniq) (string= (car uniq) (car mc)))
384 (setq uniq (cons (car mc) uniq)))
385 (setq mc (cdr mc)))
386 (nreverse uniq)))
388 (defmethod ede-proj-makefile-garbage-patterns ((this ede-proj-target))
389 "Return a list of patterns that are considred garbage to THIS.
390 These are removed with make clean."
391 ;; Get the the source object from THIS, and use the specified garbage.
392 (let ((src (ede-target-sourcecode this))
393 (garb nil))
394 (while src
395 (setq garb (append (oref (car src) garbagepattern) garb)
396 src (cdr src)))
397 garb))
400 ;;; RULES
402 (defmethod ede-proj-makefile-insert-subproj-rules ((this ede-proj-project))
403 "Insert a rule for the project THIS which should be a subproject."
404 (insert ".PHONY:" (ede-name this))
405 (newline)
406 (insert (ede-name this) ":")
407 (newline)
408 (insert "\tcd "
409 (directory-file-name (ede-subproject-relative-path this))
410 "; $(MAKE)")
411 (newline)
412 (newline)
415 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-project))
416 "Insert rules needed by THIS target."
417 (mapc 'ede-proj-makefile-insert-rules (oref this inference-rules))
420 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-project))
421 "Insert any symbols that the DIST rule should depend on.
422 Argument THIS is the project that should insert stuff."
423 (mapc 'ede-proj-makefile-insert-dist-dependencies (oref this targets))
426 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target))
427 "Insert any symbols that the DIST rule should depend on.
428 Argument THIS is the target that should insert stuff."
429 nil)
431 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target))
432 "Insert any symbols that the DIST rule should depend on.
433 Argument THIS is the target that should insert stuff."
434 (ede-proj-makefile-insert-dist-dependencies this)
437 (defmethod ede-proj-makefile-insert-dist-rules ((this ede-proj-project))
438 "Insert distribution rules for THIS in a Makefile, such as CLEAN and DIST."
439 (let ((junk (ede-proj-makefile-garbage-patterns this))
440 tmp)
441 ;; Build CLEAN, DIST, TAG, and other rules here.
442 (if junk
443 (insert "\nclean:\n"
444 "\trm -f "
445 (mapconcat (lambda (c) c) junk " ")
446 "\n\n"))
447 (insert ".PHONY: dist\n")
448 (insert "\ndist:")
449 (ede-proj-makefile-insert-dist-dependencies this)
450 (insert "\n")
451 (unless (or (ede-subproject-p this)
452 (oref this metasubproject))
453 ;; Only delete if we are the toplevel project.
454 (insert "\trm -rf $(DISTDIR)\n"))
455 (insert "\tmkdir $(DISTDIR)\n") ;We may need a -p, but I think not.
456 (setq tmp (oref this targets))
457 (insert "\tcp")
458 (while tmp
459 (let ((sv (ede-proj-makefile-sourcevar (car tmp))))
460 (if (listp sv)
461 ;; Handle special case variables.
462 (cond ((eq (cdr sv) 'share)
463 ;; This variable may be shared between multiple targets.
464 (if (re-search-backward (concat "\\$(" (car sv) ")")
465 (save-excursion
466 (beginning-of-line)
467 (point))
469 ;; If its already in the dist target, then skip it.
471 (setq sv (car sv))))
472 (t (setq sv (car sv)))))
473 (if (stringp sv)
474 (insert " $(" sv ")"))
475 (ede-proj-makefile-insert-dist-filepatterns (car tmp))
476 (setq tmp (cdr tmp))))
477 (insert " $(ede_FILES) $(DISTDIR)\n")
479 ;; Call our sub projects.
480 (ede-map-subprojects
481 this (lambda (sproj)
482 (let ((rp (directory-file-name (ede-subproject-relative-path sproj))))
483 (insert "\tcd " rp
484 "; $(MAKE) $(MFLAGS) DISTDIR=$(DISTDIR)/" rp
485 " dist"
486 "\n"))))
488 ;; Tar up the stuff.
489 (unless (or (ede-subproject-p this)
490 (oref this metasubproject))
491 (insert "\ttar -cvzf $(DISTDIR).tar.gz $(DISTDIR)\n"
492 "\trm -rf $(DISTDIR)\n"))
494 ;; Make sure the Makefile is ok.
495 (insert "\n"
496 (file-name-nondirectory (buffer-file-name)) ": "
497 (file-name-nondirectory (oref this file)) "\n"
498 ;; "$(EMACS) -batch Project.ede -l ede -f ede-proj-regenerate"
499 "\t@echo Makefile is out of date! "
500 "It needs to be regenerated by EDE.\n"
501 "\t@echo If you have not modified Project.ede, you can"
502 " use 'touch' to update the Makefile time stamp.\n"
503 "\t@false\n\n"
504 "\n\n# End of Makefile\n")))
506 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-target))
507 "Insert rules needed by THIS target."
508 nil)
510 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-target-makefile))
511 "Insert rules needed by THIS target."
512 (mapc 'ede-proj-makefile-insert-rules (oref this rules))
513 (let ((c (ede-proj-compilers this)))
514 (when c
515 (mapc 'ede-proj-makefile-insert-rules c)
516 (if (oref this phony)
517 (insert ".PHONY: " (ede-proj-makefile-target-name this) "\n"))
518 (insert (ede-proj-makefile-target-name this) ": "
519 (ede-proj-makefile-dependencies this) "\n")
520 (ede-proj-makefile-insert-commands this)
523 (defmethod ede-proj-makefile-insert-commands ((this ede-proj-target-makefile))
524 "Insert the commands needed by target THIS.
525 For targets, insert the commands needed by the chosen compiler."
526 (mapc 'ede-proj-makefile-insert-commands (ede-proj-compilers this))
527 (mapc 'ede-proj-makefile-insert-commands (ede-proj-linkers this)))
529 (defmethod ede-proj-makefile-insert-user-rules ((this ede-proj-project))
530 "Insert user specified rules needed by THIS target.
531 This is different from `ede-proj-makefile-insert-rules' in that this
532 function won't create the building rules which are auto created with
533 automake."
534 (mapc 'ede-proj-makefile-insert-user-rules (oref this inference-rules)))
536 (defmethod ede-proj-makefile-insert-user-rules ((this ede-proj-target))
537 "Insert user specified rules needed by THIS target."
538 (mapc 'ede-proj-makefile-insert-rules (oref this rules)))
540 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-makefile))
541 "Return a string representing the dependencies for THIS.
542 Some compilers only use the first element in the dependencies, others
543 have a list of intermediates (object files), and others don't care.
544 This allows customization of how these elements appear."
545 (let* ((c (ede-proj-compilers this))
546 (io (ede-or (mapcar 'ede-compiler-intermediate-objects-p c)))
547 (out nil))
548 (if io
549 (progn
550 (while c
551 (setq out
552 (concat out "$(" (ede-compiler-intermediate-object-variable
553 (car c)
554 (ede-pmake-varname this)) ")")
555 c (cdr c)))
556 out)
557 (let ((sv (ede-proj-makefile-sourcevar this))
558 (aux (oref this auxsource)))
559 (setq out
560 (if (and (stringp sv) (not (string= sv "")))
561 (concat "$(" sv ")")
562 ""))
563 (while aux
564 (setq out (concat out " " (car aux)))
565 (setq aux (cdr aux)))
566 out))))
568 ;; Tags
569 (defmethod ede-proj-makefile-tags ((this ede-proj-project) targets)
570 "Insert into the current location rules to make recursive TAGS files.
571 Argument THIS is the project to create tags for.
572 Argument TARGETS are the targets we should depend on for TAGS."
573 (insert "tags: ")
574 (let ((tg targets))
575 ;; Loop over all source variables and insert them
576 (while tg
577 (insert "$(" (ede-proj-makefile-sourcevar (car tg)) ") ")
578 (setq tg (cdr tg)))
579 (insert "\n")
580 (if targets
581 (insert "\tetags $^\n"))
582 ;; Now recurse into all subprojects
583 (setq tg (oref this subproj))
584 (while tg
585 (insert "\tcd " (ede-subproject-relative-path (car tg)) "; make $(MFLAGS) $@\n")
586 (setq tg (cdr tg)))
587 (insert "\n")))
590 (provide 'ede-pmake)
592 ;;; ede-pmake.el ends here