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)
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.
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)
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
44 ;; 1) Insert distribution source variables for targets
45 ;; 2) Insert user requested rules
48 (require 'ede-proj-obj
)
49 (require 'ede-proj-comp
)
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."
56 (isdist (string= mfilename
(ede-proj-dist-makefile 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.
67 (if (and (obj-of-class-p obj
'ede-proj-target-makefile
)
68 (string= (oref obj makefile
) mfilename
))
69 (setq mt
(cons obj mt
))))
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*
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"))
82 ;; Insert a giant pile of stuff that is common between
83 ;; one of our Makefiles, and a Makefile.in
85 "# Automatically Generated " (file-name-nondirectory mfilename
)
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"))))
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"
98 ;; Just this project's variables
99 (ede-proj-makefile-insert-variables this
)
103 ((eq (oref this makefile-type
) 'Makefile
)
104 (let* ((targ (if isdist
(oref this targets
) mt
))
105 (sp (oref this subproj
))
108 (ede-proj-makefile-dependency-files tg
))
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
))
117 (insert "VERSION=" (oref top version
) "\n"
118 "DISTDIR=$(top)" (oref top name
) "-$(VERSION)")
119 (while (ede-parent-project tmp
)
123 (file-name-nondirectory
125 (file-name-directory (oref tmp file
))))
127 tmp
(ede-parent-project tmp
)))
128 (insert subdir
"\n"))
129 ;; Some built in variables for C code
132 (insert "top_builddir = ")
136 (if (/= tc
0) (insert "/")))
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
)
144 (mapconcat (lambda (f)
146 (file-name-nondirectory
147 (file-name-sans-extension
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
))))
160 (insert " " (ede-name c
))
165 ;; Add in the include files
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 "
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
190 (ede-proj-makefile-tags this mt
)
191 (ede-proj-makefile-insert-dist-rules this
)))
193 ((eq (oref this makefile-type
) 'Makefile.in
)
194 (error "Makefile.in is not supported"))
195 ((eq (oref this makefile-type
) 'Makefile.am
)
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")
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."
219 (while (= (preceding-char) ?
\\)
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
)
230 (ede-pmake-end-of-variable)
231 (if (< (current-column) 40)
232 (if (and (/= (preceding-char) ?
=)
233 (/= (preceding-char) ?
))
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
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
)))
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."
264 ;;; GENERIC VARIABLES
266 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-project
)
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
)))
277 ;; Insert all variables, and augment them with details from
278 ;; the current configuration.
281 (if (assoc (car c
) conf-table
)
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.
289 (if (member (car c
) conf-done
)
291 (insert (car c
) "=" (cdr c
) "\n")))
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
)
305 "Insert the source variables needed by THIS.
306 Optional argument MORESOURCE is a list of additional sources to add to the
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
))
312 (insert (mapconcat (lambda (a) a
) (oref this source
) " "))
314 (insert " \\\n " (mapconcat (lambda (a) a
) moresource
" ") "")))))
316 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-target
) &optional
318 "Insert variables needed by target THIS.
319 Optional argument MORESOURCE is a list of additional sources to add to the
321 (ede-proj-makefile-insert-source-variables this moresource
)
324 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-target-makefile
)
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
336 (let* ((proj (ede-target-parent this
))
337 (conf-table (ede-proj-makefile-configuration-variables
338 this
(oref proj configuration-default
)))
341 ;; Add in all variables from the configuration not allready covered.
343 (if (member (car c
) conf-done
)
345 (insert (car c
) "=" (cdr c
) "\n")))
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
)))
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
)))
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."
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."
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
))))
379 (setq mc
(sort (apply 'append mc
) 'string
<))
380 ;; Filter out duplicates from the targets.
382 (if (and (car uniq
) (string= (car uniq
) (car mc
)))
384 (setq uniq
(cons (car mc
) 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
))
395 (setq garb
(append (oref (car src
) garbagepattern
) garb
)
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
))
406 (insert (ede-name this
) ":")
409 (directory-file-name (ede-subproject-relative-path this
))
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."
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
))
441 ;; Build CLEAN, DIST, TAG, and other rules here.
445 (mapconcat (lambda (c) c
) junk
" ")
447 (insert ".PHONY: dist\n")
449 (ede-proj-makefile-insert-dist-dependencies this
)
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
))
459 (let ((sv (ede-proj-makefile-sourcevar (car tmp
))))
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
) ")")
469 ;; If its already in the dist target, then skip it.
472 (t (setq sv
(car 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.
482 (let ((rp (directory-file-name (ede-subproject-relative-path sproj
))))
484 "; $(MAKE) $(MFLAGS) DISTDIR=$(DISTDIR)/" rp
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.
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"
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."
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
)))
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
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
)))
552 (concat out
"$(" (ede-compiler-intermediate-object-variable
554 (ede-pmake-varname this
)) ")")
557 (let ((sv (ede-proj-makefile-sourcevar this
))
558 (aux (oref this auxsource
)))
560 (if (and (stringp sv
) (not (string= sv
"")))
564 (setq out
(concat out
" " (car aux
)))
565 (setq aux
(cdr aux
)))
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."
575 ;; Loop over all source variables and insert them
577 (insert "$(" (ede-proj-makefile-sourcevar (car tg
)) ") ")
581 (insert "\tetags $^\n"))
582 ;; Now recurse into all subprojects
583 (setq tg
(oref this subproj
))
585 (insert "\tcd " (ede-subproject-relative-path (car tg
)) "; make $(MFLAGS) $@\n")
592 ;;; ede-pmake.el ends here