1 ;;; ede-pmake.el --- EDE Generic Project Makefile code generator.
3 ;;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 ;;; 2007, 2008, 2009 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 ;; 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
)
51 (declare-function ede-srecode-setup
"ede/srecode")
52 (declare-function ede-srecode-insert
"ede/srecode")
55 (defmethod ede-proj-makefile-create ((this ede-proj-project
) mfilename
)
56 "Create a Makefile for all Makefile targets in THIS.
57 MFILENAME is the makefile to generate."
58 (require 'ede
/srecode
)
60 (isdist (string= mfilename
(ede-proj-dist-makefile this
)))
65 ;; Find out how deep this project is.
67 (while (setq tmp
(ede-parent-project tmp
))
68 (setq depth
(1+ depth
))))
69 ;; Collect the targets that belong in a makefile.
72 (if (and (obj-of-class-p obj
'ede-proj-target-makefile
)
73 (string= (oref obj makefile
) mfilename
))
74 (setq mt
(cons obj mt
))))
76 ;; Fix the order so things compile in the right direction.
77 (setq mt
(nreverse mt
))
78 ;; Add in the header part of the Makefile*
80 (setq orig-buffer
(get-file-buffer mfilename
))
81 (set-buffer (setq buff-to-kill
(find-file-noselect mfilename
)))
82 (goto-char (point-min))
85 (not (looking-at "# Automatically Generated \\w+ by EDE.")))
86 (if (not (y-or-n-p (format "Really replace %s? " mfilename
)))
87 (error "Not replacing Makefile"))
88 (message "Replace EDE Makefile"))
91 ;; Insert a giant pile of stuff that is common between
92 ;; one of our Makefiles, and a Makefile.in
96 (with-slots (makefile-type) this
97 (cond ((eq makefile-type
'Makefile
) "make")
98 ((eq makefile-type
'Makefile.in
) "autoconf")
99 ((eq makefile-type
'Makefile.am
) "automake")
100 (t (error ":makefile-type in project invalid")))))
102 ;; Just this project's variables
103 (ede-proj-makefile-insert-variables this
)
109 ((eq (oref this makefile-type
) 'Makefile
)
110 ;; Make sure the user has the right kind of make
111 (ede-make-check-version)
113 (let* ((targ (if isdist
(oref this targets
) mt
))
114 (sp (oref this subproj
))
117 (ede-proj-makefile-dependency-files tg
))
119 ;; Distribution variables
120 (ede-compiler-begin-unique
121 (mapc 'ede-proj-makefile-insert-variables targ
))
122 ;; Only add the distribution stuff in when depth != 0
123 (let ((top (ede-toplevel this
))
126 (insert "VERSION=" (oref top version
) "\n"
127 "DISTDIR=$(top)" (oref top name
) "-$(VERSION)")
128 (while (ede-parent-project tmp
)
132 (file-name-nondirectory
134 (file-name-directory (oref tmp file
))))
136 tmp
(ede-parent-project tmp
)))
137 (insert subdir
"\n"))
138 ;; Some built in variables for C code
141 (insert "top_builddir = ")
145 (if (/= tc
0) (insert "/")))
148 ;; Create a variable with all the dependency files to include
149 ;; These methods borrowed from automake.
150 (if (and (oref this automatic-dependencies
) df
)
153 (mapconcat (lambda (f)
155 (file-name-nondirectory
156 (file-name-sans-extension
164 (if (and (slot-exists-p c
'partofall
) (oref c partofall
))
165 ;; Only insert this rule if it is a part of ALL.
166 (insert " " (ede-proj-makefile-target-name c
))))
169 (insert " " (ede-name c
))
174 ;; Add in the include files
177 (insert "include " c
"\n\n"))
178 (oref this include-file
))
179 ;; Some C inference rules
180 ;; Dependency rules borrowed from automake.
182 ;; NOTE: This is GNU Make specific.
183 (if (and (oref this automatic-dependencies
) df
)
184 (insert "DEPS_MAGIC := $(shell mkdir .deps > /dev/null "
186 "-include $(DEP_FILES)\n\n"))
188 ;; General makefile rules stored in the individual targets
190 (ede-compiler-begin-unique
191 (ede-proj-makefile-insert-rules this
)
192 (mapc 'ede-proj-makefile-insert-rules targ
))
194 ;; phony targets for sub projects
196 (mapc 'ede-proj-makefile-insert-subproj-rules sp
)
198 ;; Distribution rules such as CLEAN and DIST
201 (ede-proj-makefile-tags this mt
)
202 (ede-proj-makefile-insert-dist-rules this
)))
204 ((eq (oref this makefile-type
) 'Makefile.in
)
205 (error "Makefile.in is not supported"))
206 ((eq (oref this makefile-type
) 'Makefile.am
)
208 ;; Distribution variables
209 (let ((targ (if isdist
(oref this targets
) mt
)))
210 (ede-compiler-begin-unique
211 (mapc 'ede-proj-makefile-insert-automake-pre-variables targ
))
212 (ede-compiler-begin-unique
213 (mapc 'ede-proj-makefile-insert-source-variables targ
))
214 (ede-compiler-begin-unique
215 (mapc 'ede-proj-makefile-insert-automake-post-variables targ
))
216 (ede-compiler-begin-unique
217 (ede-proj-makefile-insert-user-rules this
))
218 (insert "\n# End of Makefile.am\n")
221 (t (error "Unknown makefile type when generating Makefile")))
222 ;; Put the cursor in a nice place
223 (goto-char (point-min)))
224 ;; If we have an original buffer, then don't kill it.
225 (when (not orig-buffer
)
226 (kill-buffer buff-to-kill
))
229 ;;; VARIABLE insertion
231 (defun ede-pmake-end-of-variable ()
232 "Move to the end of the variable declaration under point."
234 (while (= (preceding-char) ?
\\)
239 (defmacro ede-pmake-insert-variable-shared
(varname &rest body
)
240 "Add VARNAME into the current Makefile.
241 Execute BODY in a location where a value can be placed."
242 `(let ((addcr t
) (v ,varname
))
243 (if (re-search-backward (concat "^" v
"\\s-*=") nil t
)
245 (ede-pmake-end-of-variable)
246 (if (< (current-column) 40)
247 (if (and (/= (preceding-char) ?
=)
248 (/= (preceding-char) ?
))
254 (if addcr
(insert "\n"))
255 (goto-char (point-max))))
256 (put 'ede-pmake-insert-variable-shared
'lisp-indent-function
1)
258 ;;; SOURCE VARIABLE NAME CONSTRUCTION
260 (defsubst ede-pmake-varname
(obj)
261 "Convert OBJ into a variable name name.
262 Change . to _ in the variable name."
263 (let ((name (oref obj name
)))
264 (while (string-match "\\." name
)
265 (setq name
(replace-match "_" nil t name
)))
268 (defmethod ede-proj-makefile-sourcevar ((this ede-proj-target
))
269 "Return the variable name for THIS's sources."
270 (concat (ede-pmake-varname this
) "_YOU_FOUND_A_BUG"))
272 ;;; DEPENDENCY FILE GENERATOR LISTS
274 (defmethod ede-proj-makefile-dependency-files ((this ede-proj-target
))
275 "Return a list of source files to convert to dependencies.
276 Argument THIS is the target to get sources from."
279 ;;; GENERIC VARIABLES
281 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-project
)
283 "Return a list of configuration variables from THIS.
284 Use CONFIGURATION as the current configuration to query."
285 (cdr (assoc configuration
(oref this configuration-variables
))))
287 (defmethod ede-proj-makefile-insert-variables-new ((this ede-proj-project
))
288 "Insert variables needed by target THIS.
290 NOTE: Not yet in use! This is part of an SRecode conversion of
291 EDE that is in progress."
292 ; (let ((conf-table (ede-proj-makefile-configuration-variables
293 ; this (oref this configuration-default)))
296 ; (ede-srecode-insert-with-dictionary
297 ; "declaration:ede-vars"
299 ; ;; Insert all variables, and augment them with details from
300 ; ;; the current configuration.
303 ; (let ((ldict (srecode-dictionary-add-section-dictionary
306 ; (srecode-dictionary-set-value ldict "NAME" (car c))
307 ; (if (assoc (car c) conf-table)
308 ; (let ((vdict (srecode-dictionary-add-section-dictionary
310 ; (srecode-dictionary-set-value
311 ; vdict "VAL" (cdr (assoc (car c) conf-table)))
312 ; (setq conf-done (cons (car c) conf-done))))
313 ; (let ((vdict (srecode-dictionary-add-section-dictionary
315 ; (srecode-dictionary-set-value vdict "VAL" (cdr c))))
318 ; (oref this variables))
320 ; ;; Add in all variables from the configuration not allready covered.
323 ; (if (member (car c) conf-done)
325 ; (let* ((ldict (srecode-dictionary-add-section-dictionary
327 ; (vdict (srecode-dictionary-add-section-dictionary
330 ; (srecode-dictionary-set-value ldict "NAME" (car c))
331 ; (srecode-dictionary-set-value vdict "VAL" (cdr c))))
337 ;; @TODO - finish off this function, and replace the below fcn
342 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-project
))
343 "Insert variables needed by target THIS."
344 (let ((conf-table (ede-proj-makefile-configuration-variables
345 this
(oref this configuration-default
)))
347 ;; Insert all variables, and augment them with details from
348 ;; the current configuration.
351 (if (assoc (car c
) conf-table
)
353 (insert (cdr (assoc (car c
) conf-table
)) " ")
354 (setq conf-done
(cons (car c
) conf-done
))))
355 (insert (cdr c
) "\n"))
356 (oref this variables
))
357 ;; Add in all variables from the configuration not allready covered.
359 (if (member (car c
) conf-done
)
361 (insert (car c
) "=" (cdr c
) "\n")))
365 (while (ede-parent-project tmp
)
366 (setq tmp
(ede-parent-project tmp
)
367 top
(concat "../" top
)))
368 (insert "\ntop=" top
))
369 (insert "\nede_FILES=" (file-name-nondirectory (oref this file
)) " "
370 (file-name-nondirectory (ede-proj-dist-makefile this
)) "\n"))
372 (defmethod ede-proj-makefile-insert-source-variables ((this ede-proj-target
)
375 "Insert the source variables needed by THIS.
376 Optional argument MORESOURCE is a list of additional sources to add to the
378 (let ((sv (ede-proj-makefile-sourcevar this
)))
379 ;; This variable may be shared between targets
380 (ede-pmake-insert-variable-shared (cond ((listp sv
) (car sv
))
382 (insert (mapconcat (lambda (a) a
) (oref this source
) " "))
384 (insert " \\\n " (mapconcat (lambda (a) a
) moresource
" ") "")))))
386 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-target
) &optional
388 "Insert variables needed by target THIS.
389 Optional argument MORESOURCE is a list of additional sources to add to the
391 (ede-proj-makefile-insert-source-variables this moresource
)
394 (defmethod ede-proj-makefile-configuration-variables ((this ede-proj-target-makefile
)
396 "Return a list of configuration variables from THIS.
397 Use CONFIGURATION as the current configuration to query."
398 (cdr (assoc configuration
(oref this configuration-variables
))))
400 (defmethod ede-proj-makefile-insert-variables ((this ede-proj-target-makefile
)
401 &optional moresource
)
402 "Insert variables needed by target THIS.
403 Optional argument MORESOURCE is a list of additional sources to add to the
406 (let* ((proj (ede-target-parent this
))
407 (conf-table (ede-proj-makefile-configuration-variables
408 this
(oref proj configuration-default
)))
411 ;; Add in all variables from the configuration not allready covered.
413 (if (member (car c
) conf-done
)
415 (insert (car c
) "=" (cdr c
) "\n")))
417 (let ((comp (ede-proj-compilers this
))
418 (link (ede-proj-linkers this
))
419 (name (ede-proj-makefile-target-name this
))
420 (src (oref this source
)))
422 (ede-compiler-only-once (car comp
)
423 (ede-proj-makefile-insert-object-variables (car comp
) name src
)
424 (ede-proj-makefile-insert-variables (car comp
)))
425 (setq comp
(cdr comp
)))
427 (ede-linker-only-once (car link
)
428 (ede-proj-makefile-insert-variables (car link
)))
429 (setq link
(cdr link
)))))
431 (defmethod ede-proj-makefile-insert-automake-pre-variables
432 ((this ede-proj-target
))
433 "Insert variables needed by target THIS in Makefile.am before SOURCES."
436 (defmethod ede-proj-makefile-insert-automake-post-variables
437 ((this ede-proj-target
))
438 "Insert variables needed by target THIS in Makefile.am after SOURCES."
443 (defmethod ede-proj-makefile-garbage-patterns ((this ede-proj-project
))
444 "Return a list of patterns that are considered garbage to THIS.
445 These are removed with make clean."
446 (let ((mc (ede-map-targets
447 this
(lambda (c) (ede-proj-makefile-garbage-patterns c
))))
449 (setq mc
(sort (apply 'append mc
) 'string
<))
450 ;; Filter out duplicates from the targets.
452 (if (and (car uniq
) (string= (car uniq
) (car mc
)))
454 (setq uniq
(cons (car mc
) uniq
)))
458 (defmethod ede-proj-makefile-garbage-patterns ((this ede-proj-target
))
459 "Return a list of patterns that are considered garbage to THIS.
460 These are removed with make clean."
461 ;; Get the the source object from THIS, and use the specified garbage.
462 (let ((src (ede-target-sourcecode this
))
465 (setq garb
(append (oref (car src
) garbagepattern
) garb
)
472 (defmethod ede-proj-makefile-insert-subproj-rules ((this ede-proj-project
))
473 "Insert a rule for the project THIS which should be a subproject."
474 (insert ".PHONY:" (ede-name this
))
476 (insert (ede-name this
) ":")
478 (insert "\t$(MAKE) -C " (directory-file-name (ede-subproject-relative-path this
)))
483 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-project
))
484 "Insert rules needed by THIS target."
485 (mapc 'ede-proj-makefile-insert-rules
(oref this inference-rules
))
488 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-project
))
489 "Insert any symbols that the DIST rule should depend on.
490 Argument THIS is the project that should insert stuff."
491 (mapc 'ede-proj-makefile-insert-dist-dependencies
(oref this targets
))
494 (defmethod ede-proj-makefile-insert-dist-dependencies ((this ede-proj-target
))
495 "Insert any symbols that the DIST rule should depend on.
496 Argument THIS is the target that should insert stuff."
499 (defmethod ede-proj-makefile-insert-dist-filepatterns ((this ede-proj-target
))
500 "Insert any symbols that the DIST rule should depend on.
501 Argument THIS is the target that should insert stuff."
502 (ede-proj-makefile-insert-dist-dependencies this
)
505 (defmethod ede-proj-makefile-insert-dist-rules ((this ede-proj-project
))
506 "Insert distribution rules for THIS in a Makefile, such as CLEAN and DIST."
507 (let ((junk (ede-proj-makefile-garbage-patterns this
))
509 ;; Build CLEAN, DIST, TAG, and other rules here.
513 (mapconcat (lambda (c) c
) junk
" ")
515 ;; @TODO: ^^^ Clean should also recurse. ^^^
517 (insert ".PHONY: dist\n")
519 (ede-proj-makefile-insert-dist-dependencies this
)
521 (unless (or (ede-subproject-p this
)
522 (oref this metasubproject
))
523 ;; Only delete if we are the toplevel project.
524 (insert "\trm -rf $(DISTDIR)\n"))
525 (insert "\tmkdir $(DISTDIR)\n") ;We may need a -p, but I think not.
526 (setq tmp
(oref this targets
))
529 (let ((sv (ede-proj-makefile-sourcevar (car tmp
))))
531 ;; Handle special case variables.
532 (cond ((eq (cdr sv
) 'share
)
533 ;; This variable may be shared between multiple targets.
534 (if (re-search-backward (concat "\\$(" (car sv
) ")")
539 ;; If its already in the dist target, then skip it.
542 (t (setq sv
(car sv
)))))
544 (insert " $(" sv
")"))
545 (ede-proj-makefile-insert-dist-filepatterns (car tmp
))
546 (setq tmp
(cdr tmp
))))
547 (insert " $(ede_FILES) $(DISTDIR)\n")
549 ;; Call our sub projects.
552 (let ((rp (directory-file-name (ede-subproject-relative-path sproj
))))
553 (insert "\t$(MAKE) -C " rp
" $(MFLAGS) DISTDIR=$(DISTDIR)/" rp
558 (unless (or (ede-subproject-p this
)
559 (oref this metasubproject
))
560 (insert "\ttar -cvzf $(DISTDIR).tar.gz $(DISTDIR)\n"
561 "\trm -rf $(DISTDIR)\n"))
563 ;; Make sure the Makefile is ok.
565 (file-name-nondirectory (buffer-file-name)) ": "
566 (file-name-nondirectory (oref this file
)) "\n"
567 ;; "$(EMACS) -batch Project.ede -l ede -f ede-proj-regenerate"
568 "\t@echo Makefile is out of date! "
569 "It needs to be regenerated by EDE.\n"
570 "\t@echo If you have not modified Project.ede, you can"
571 " use 'touch' to update the Makefile time stamp.\n"
573 "\n\n# End of Makefile\n")))
575 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-target
))
576 "Insert rules needed by THIS target."
579 (defmethod ede-proj-makefile-insert-rules ((this ede-proj-target-makefile
))
580 "Insert rules needed by THIS target."
581 (mapc 'ede-proj-makefile-insert-rules
(oref this rules
))
582 (let ((c (ede-proj-compilers this
)))
584 (mapc 'ede-proj-makefile-insert-rules c
)
585 (if (oref this phony
)
586 (insert ".PHONY: " (ede-proj-makefile-target-name this
) "\n"))
587 (insert (ede-proj-makefile-target-name this
) ": "
588 (ede-proj-makefile-dependencies this
) "\n")
589 (ede-proj-makefile-insert-commands this
)
592 (defmethod ede-proj-makefile-insert-commands ((this ede-proj-target-makefile
))
593 "Insert the commands needed by target THIS.
594 For targets, insert the commands needed by the chosen compiler."
595 (mapc 'ede-proj-makefile-insert-commands
(ede-proj-compilers this
))
596 (when (object-assoc t
:uselinker
(ede-proj-compilers this
))
597 (mapc 'ede-proj-makefile-insert-commands
(ede-proj-linkers this
))))
600 (defmethod ede-proj-makefile-insert-user-rules ((this ede-proj-project
))
601 "Insert user specified rules needed by THIS target.
602 This is different from `ede-proj-makefile-insert-rules' in that this
603 function won't create the building rules which are auto created with
605 (mapc 'ede-proj-makefile-insert-user-rules
(oref this inference-rules
)))
607 (defmethod ede-proj-makefile-insert-user-rules ((this ede-proj-target
))
608 "Insert user specified rules needed by THIS target."
609 (mapc 'ede-proj-makefile-insert-rules
(oref this rules
)))
611 (defmethod ede-proj-makefile-dependencies ((this ede-proj-target-makefile
))
612 "Return a string representing the dependencies for THIS.
613 Some compilers only use the first element in the dependencies, others
614 have a list of intermediates (object files), and others don't care.
615 This allows customization of how these elements appear."
616 (let* ((c (ede-proj-compilers this
))
617 (io (ede-or (mapcar 'ede-compiler-intermediate-objects-p c
)))
623 (concat out
"$(" (ede-compiler-intermediate-object-variable
625 (ede-proj-makefile-target-name this
)) ")")
628 (let ((sv (ede-proj-makefile-sourcevar this
))
629 (aux (oref this auxsource
)))
631 (if (and (stringp sv
) (not (string= sv
"")))
635 (setq out
(concat out
" " (car aux
)))
636 (setq aux
(cdr aux
)))
640 (defmethod ede-proj-makefile-tags ((this ede-proj-project
) targets
)
641 "Insert into the current location rules to make recursive TAGS files.
642 Argument THIS is the project to create tags for.
643 Argument TARGETS are the targets we should depend on for TAGS."
646 ;; Loop over all source variables and insert them
648 (insert "$(" (ede-proj-makefile-sourcevar (car tg
)) ") ")
652 (insert "\tetags $^\n"))
653 ;; Now recurse into all subprojects
654 (setq tg
(oref this subproj
))
656 (insert "\t$(MAKE) -C " (ede-subproject-relative-path (car tg
)) " $(MFLAGS) $@\n")
663 ;;; ede/pmake.el ends here