1 ;;; ede-proj.el --- EDE Generic Project file driver
3 ;;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2007 Eric M. Ludlam
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
6 ;; Keywords: project, make
7 ;; RCS: $Id: ede-proj.el,v 1.1 2007-11-26 15:22:10 michaels 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 ;; EDE defines a method for managing a project. EDE-PROJ aims to be a
27 ;; generic project file format based on the EIEIO object stream
28 ;; methods. Changes in the project structure will require Makefile
29 ;; rebuild. The targets provided in ede-proj can be augmented with
30 ;; additional target types inherited directly from `ede-proj-target'.
32 (eval-and-compile '(require 'ede))
33 (require 'ede-proj-comp)
35 ;;; Class Definitions:
36 (defclass ede-proj-target (ede-target)
37 ((auxsource :initarg :auxsource
40 :custom (repeat (string :tag "File"))
41 :label "Auxiliary Source Files"
42 :group (default source)
43 :documentation "Auxilliary source files included in this target.
44 Each of these is considered equivalent to a source file, but it is not
45 distributed, and each should have a corresponding rule to build it.")
48 :documentation "Non-nil when generated files needs updating.")
49 (compiler :initarg :compiler
51 :type (or null symbol)
52 :custom (choice (const :tag "None" nil)
53 :slotofchoices availablecompilers)
54 :label "Compiler for building sources"
57 "The compiler to be used to compile this object.
58 This should be a symbol, which contains the object defining the compiler.
59 This enables save/restore to do so by name, permitting the sharing
60 of these compiler resources, and global customization thereof.")
61 (linker :initarg :linker
63 :type (or null symbol)
64 :custom (choice (const :tag "None" nil)
65 :slotofchoices availablelinkers)
66 :label "Linker for combining intermediate object files."
69 "The linker to be used to link compled sources for this object.
70 This should be a symbol, which contains the object defining the linker.
71 This enables save/restore to do so by name, permitting the sharing
72 of these linker resources, and global customization thereof.")
73 ;; Class allocated slots
74 (phony :allocation :class
78 "A phony target is one where the build target does not relate to a file.
79 Such targets are always built, but make knows how to deal with them..")
80 (availablecompilers :allocation :class
84 "A list of `ede-compiler' objects.
85 These are the compilers the user can choose from when setting the
87 (availablelinkers :allocation :class
91 "A list of `ede-linker' objects.
92 These are the linkers the user can choose from when setting the
95 "Abstract class for ede-proj targets.")
97 (defclass ede-proj-target-makefile (ede-proj-target)
98 ((makefile :initarg :makefile
102 :label "Parent Makefile"
104 :documentation "File name of generated Makefile.")
105 (partofall :initarg :partofall
109 :label "Part of `all:' target"
112 "Non nil means the rule created is part of the all target.
113 Setting this to nil creates the rule to build this item, but does not
114 include it in the ALL`all:' rule.")
115 (configuration-variables
116 :initarg :configuration-variables
119 :custom (repeat (cons (string :tag "Configuration")
121 (cons (string :tag "Name")
122 (string :tag "Value")))))
123 :label "Environment Variables for configurations"
125 :documentation "Makefile variables appended to use in different configurations.
126 These variables are used in the makefile when a configuration becomes active.
127 Target variables are always renamed such as foo_CFLAGS, then included into
128 commands where the variable would usually appear.")
129 (rules :initarg :rules
132 :custom (repeat (object :objecttype ede-makefile-rule))
133 :label "Additional Rules"
136 "Arbitrary rules and dependencies needed to make this target.
137 It is safe to leave this blank.")
139 "Abstract class for Makefile based targets.")
141 (autoload 'ede-proj-target-aux "ede-proj-aux"
142 "Target class for a group of lisp files." nil nil)
143 (autoload 'ede-proj-target-elisp "ede-proj-elisp"
144 "Target class for a group of lisp files." nil nil)
145 (autoload 'ede-proj-target-elisp-autoloads "ede-proj-elisp"
146 "Target class for generating autoload files." nil nil)
147 (autoload 'ede-proj-target-scheme "ede-proj-scheme"
148 "Target class for a group of lisp files." nil nil)
149 (autoload 'ede-proj-target-makefile-miscelaneous "ede-proj-misc"
150 "Target class for a group of miscelaneous w/ a special makefile." nil nil)
151 (autoload 'ede-proj-target-makefile-program "ede-proj-prog"
152 "Target class for building a program." nil nil)
153 (autoload 'ede-proj-target-makefile-archive "ede-proj-archive"
154 "Target class for building an archive of object code." nil nil)
155 (autoload 'ede-proj-target-makefile-shared-object "ede-proj-shared"
156 "Target class for building a shared object." nil nil)
157 (autoload 'ede-proj-target-makefile-info "ede-proj-info"
158 "Target class for info files." nil nil)
160 (defvar ede-proj-target-alist
161 '(("program" . ede-proj-target-makefile-program)
162 ("archive" . ede-proj-target-makefile-archive)
163 ("sharedobject" . ede-proj-target-makefile-shared-object)
164 ("emacs lisp" . ede-proj-target-elisp)
165 ("emacs lisp autoloads" . ede-proj-target-elisp-autoloads)
166 ("info" . ede-proj-target-makefile-info)
167 ("auxiliary" . ede-proj-target-aux)
168 ("scheme" . ede-proj-target-scheme)
169 ("miscelaneous" . ede-proj-target-makefile-miscelaneous)
171 "Alist of names to class types for available project target classes.")
173 (defun ede-proj-register-target (name class)
174 "Register a new target class with NAME and class symbol CLASS.
175 This enables the creation of your target type."
176 (let ((a (assoc name ede-proj-target-alist)))
179 (setq ede-proj-target-alist
180 (cons (cons name class) ede-proj-target-alist)))))
182 (defclass ede-proj-project (ede-project)
183 ((makefile-type :initarg :makefile-type
186 :custom (choice (const Makefile)
191 :documentation "The type of Makefile to generate.
192 Can be one of 'Makefile, 'Makefile.in, or 'Makefile.am.
193 If this value is NOT 'Makefile, then that overrides the :makefile slot
195 (variables :initarg :variables
198 :custom (repeat (cons (string :tag "Name")
199 (string :tag "Value")))
200 :documentation "Variables to set in this Makefile.")
201 (configuration-variables
202 :initarg :configuration-variables
203 :initform ("debug" (("DEBUG" . "1")))
205 :custom (repeat (cons (string :tag "Configuration")
207 (cons (string :tag "Name")
208 (string :tag "Value")))))
209 :documentation "Makefile variables to use in different configurations.
210 These variables are used in the makefile when a configuration becomes active.")
211 (inference-rules :initarg :inference-rules
214 (object :objecttype ede-makefile-rule))
215 :documentation "Inference rules to add to the makefile.")
216 (include-file :initarg :include-file
219 (string :tag "Include File"))
220 :documentation "Additional files to include.
221 These files can contain additional rules, variables, and customizations.")
222 (automatic-dependencies
223 :initarg :automatic-dependencies
227 :group (default settings)
229 "Non-nil to do implement automatic dependencies in the Makefile.")
232 [ "Regenerate Makefiles" ede-proj-regenerate t ]
233 [ "Upload Distribution" ede-upload-distribution t ]
237 :initarg :metasubproject
241 :group (default settings)
243 "Non-nil if this is a metasubproject.
244 Usually, a subproject is determined by a parent project. If multiple top level
245 projects are grouped into a large project not maintained by EDE, then you need
246 to set this to non-nil. The only effect is that the `dist' rule will then avoid
249 "The EDE-PROJ project definition class.")
252 (defun ede-proj-load (project)
253 "Load a project file PROJECT."
256 (subdirs (directory-files project nil "[^.].*" nil)))
257 (set-buffer (get-buffer-create " *tmp proj read*"))
260 (insert-file-contents (concat project "Project.ede")
262 (goto-char (point-min))
263 (setq ret (read (current-buffer)))
264 (if (not (eq (car ret) 'ede-proj-project))
265 (error "Corrupt project file"))
266 (setq ret (eval ret))
267 (oset ret file (concat project "Project.ede")))
268 (kill-buffer " *tmp proj read*"))
270 (let ((sd (concat project (car subdirs))))
271 (if (and (file-directory-p sd)
272 (ede-directory-project-p (concat sd "/")))
273 (oset ret subproj (cons (ede-proj-load (concat sd "/"))
274 (oref ret subproj))))
275 (setq subdirs (cdr subdirs))))
278 (defun ede-proj-save (&optional project)
279 "Write out object PROJECT into its file."
281 (if (not project) (setq project (ede-current-project)))
282 (let ((b (set-buffer (get-buffer-create " *tmp proj write*")))
283 (cfn (oref project file)))
287 (let ((standard-output (current-buffer)))
288 (oset project file (file-name-nondirectory cfn))
289 (object-write project ";; EDE project file."))
292 ;; Restore the :file on exit.
293 (oset project file cfn)
296 (defmethod ede-commit-local-variables ((proj ede-proj-project))
297 "Commit change to local variables in PROJ."
298 (ede-proj-save proj))
300 (defmethod eieio-done-customizing ((proj ede-proj-project))
301 "Call this when a user finishes customizing this object.
302 Argument PROJ is the project to save."
304 (ede-proj-save proj))
306 (defmethod eieio-done-customizing ((target ede-proj-target))
307 "Call this when a user finishes customizing this object.
308 Argument TARGET is the project we are completing customization on."
310 (ede-proj-save (ede-current-project)))
312 (defmethod ede-commit-project ((proj ede-proj-project))
313 "Commit any change to PROJ to its file."
314 (ede-proj-save proj))
316 (defmethod ede-buffer-mine ((this ede-proj-project) buffer)
317 "Return t if object THIS lays claim to the file in BUFFER."
318 (let ((f (ede-convert-path this (buffer-file-name buffer))))
319 (or (string= (file-name-nondirectory (oref this file)) f)
320 (string= (ede-proj-dist-makefile this) f)
321 (string-match "Makefile\\(\\.\\(in\\|am\\)\\)?" f)
322 (string-match "config\\(ure\\.in\\|\\.stutus\\)?" f)
325 (defmethod ede-buffer-mine ((this ede-proj-target) buffer)
326 "Return t if object THIS lays claim to the file in BUFFER."
327 (or (call-next-method)
328 (ede-target-buffer-in-sourcelist this buffer (oref this auxsource))))
331 ;;; EDE command functions
333 (defvar ede-proj-target-history nil
334 "History when querying for a target type.")
336 (defmethod project-new-target ((this ede-proj-project))
337 "Create a new target in THIS based on the current buffer."
338 (let* ((name (read-string "Name: " ""))
339 (type (completing-read "Type: " ede-proj-target-alist
340 nil t nil '(ede-proj-target-history . 1)))
342 (src (if (and (buffer-file-name)
343 (y-or-n-p (format "Add %s to %s? " (buffer-name) name)))
344 (buffer-file-name))))
345 (setq ot (funcall (cdr (assoc type ede-proj-target-alist)) name :name name
346 :path (ede-convert-path this default-directory)
348 (list (file-name-nondirectory src))
350 ;; If we added it, set the local buffer's object.
353 (ede-apply-object-keymap)))
354 ;; Add it to the project object
355 (oset this targets (cons ot (oref this targets)))
357 (ede-proj-save this)))
359 (defmethod project-new-target-custom ((this ede-proj-project))
360 "Create a new target in THIS for custom."
361 (let* ((name (read-string "Name: " ""))
362 (type (completing-read "Type: " ede-proj-target-alist
363 nil t nil '(ede-proj-target-history . 1))))
364 (funcall (cdr (assoc type ede-proj-target-alist)) name :name name
365 :path (ede-convert-path this default-directory)
368 (defmethod project-delete-target ((this ede-proj-target))
369 "Delete the current target THIS from it's parent project."
370 (let ((p (ede-current-project))
371 (ts (oref this source)))
372 ;; Loop across all sources. If it exists in a buffer,
373 ;; clear it's object.
375 (let* ((default-directory (oref this path))
376 (b (get-file-buffer (car ts))))
380 (if (eq ede-object this)
382 (setq ede-object nil)
383 (ede-apply-object-keymap))))))
385 ;; Remove THIS from it's parent.
386 ;; The two vectors should be pointer equivalent.
387 (oset p targets (delq this (oref p targets)))
388 (ede-proj-save (ede-current-project))))
390 (defmethod project-add-file ((this ede-proj-target) file)
391 "Add to target THIS the current buffer represented as FILE."
392 (let ((file (ede-convert-path this file))
393 (src (ede-target-sourcecode this))
395 (while (and src (not (ede-want-file-p (car src) file)))
396 (setq src (cdr src)))
399 (cond ((ede-want-file-source-p this file)
400 (object-add-to-list this 'source file t))
401 ((ede-want-file-auxiliary-p this file)
402 (object-add-to-list this 'auxsource file t))
403 (t (error "`project-add-file(ede-target)' source mismatch error")))
406 (defmethod project-remove-file ((target ede-proj-target) file)
407 "For TARGET, remove FILE.
408 FILE must be massaged by `ede-convert-path'."
409 ;; Speedy delete should be safe.
410 (object-remove-from-list target 'source (ede-convert-path target file))
411 (object-remove-from-list target 'auxsource (ede-convert-path target file))
414 (defmethod project-update-version ((this ede-proj-project))
415 "The :version of project THIS has changed."
418 (defmethod project-make-dist ((this ede-proj-project))
419 "Build a distribution for the project based on THIS target."
420 ;; I'm a lazy bum, so I'll make a makefile for doing this sort
421 ;; of thing, and rely only on that small section of code.
422 (let ((pm (ede-proj-dist-makefile this))
423 (df (project-dist-files this)))
424 (if (and (file-exists-p (car df))
425 (not (y-or-n-p "Dist file already exists. Rebuild? ")))
426 (error "Try `ede-update-version' before making a distribution"))
427 (ede-proj-setup-buildenvironment this)
428 (if (string= pm "Makefile.am") (setq pm "Makefile"))
429 (compile (concat "make -f " pm " dist"))
432 (defmethod project-dist-files ((this ede-proj-project))
433 "Return a list of files that constitutes a distribution of THIS project."
435 ;; Note to self, keep this first for the above fn to check against.
436 (concat (oref this name) "-" (oref this version) ".tar.gz")
439 (defmethod project-compile-project ((proj ede-proj-project) &optional command)
440 "Compile the entire current project PROJ.
441 Argument COMMAND is the command to use when compiling."
442 (let ((pm (ede-proj-dist-makefile proj))
443 (default-directory (file-name-directory (oref proj file))))
444 (ede-proj-setup-buildenvironment proj)
445 (if (string= pm "Makefile.am") (setq pm "Makefile"))
446 (compile (concat "make -f " pm " all"))))
448 ;;; Target type specific compilations/debug
450 (defmethod project-compile-target ((obj ede-proj-target) &optional command)
451 "Compile the current target OBJ.
452 Argument COMMAND is the command to use for compiling the target."
453 (project-compile-project (ede-current-project) command))
455 (defmethod project-compile-target ((obj ede-proj-target-makefile)
457 "Compile the current target program OBJ.
458 Optional argument COMMAND is the s the alternate command to use."
459 (ede-proj-setup-buildenvironment (ede-current-project))
460 (compile (concat "make -f " (oref obj makefile) " "
461 (ede-proj-makefile-target-name obj))))
463 (defmethod project-debug-target ((obj ede-proj-target))
464 "Run the current project target OBJ in a debugger."
465 (error "Debug-target not supported by %s" (object-name obj)))
467 (defmethod ede-proj-makefile-target-name ((this ede-proj-target))
468 "Return the name of the main target for THIS target."
471 ;;; Compiler and source code generators
473 (defmethod ede-want-file-auxiliary-p ((this ede-target) file)
474 "Return non-nil if THIS target wants FILE."
475 ;; By default, all targets reference the source object, and let it decide.
476 (let ((src (ede-target-sourcecode this)))
477 (while (and src (not (ede-want-file-auxiliary-p (car src) file)))
478 (setq src (cdr src)))
481 (defmethod ede-proj-compilers ((obj ede-proj-target))
482 "List of compilers being used by OBJ.
483 If the `compiler' slot is empty, concoct one on a first match found
484 basis for any given type from the `availablecompilers' slot.
485 Otherwise, return the `compiler' slot.
486 Converts all symbols into the objects to be used."
487 (when (slot-exists-p obj 'compiler)
488 (let ((comp (oref obj compiler)))
490 ;; Now that we have a pre-set compilers to use, convert tye symbols
491 ;; into objects for ease of use
493 (setq comp (mapcar 'symbol-value comp))
494 (setq comp (list (symbol-value comp))))
495 (let ((avail (mapcar 'symbol-value (oref obj availablecompilers)))
496 (st (oref obj sourcetype))
497 (sources (oref obj source)))
498 ;; COMP is not specified, so generate a list from the available
501 (if (ede-want-any-source-files-p (symbol-value (car st)) sources)
502 (let ((c (ede-proj-find-compiler avail (car st))))
503 (if c (setq comp (cons c comp)))))
504 (setq st (cdr st)))))
505 ;; Return the disovered compilers
508 (defmethod ede-proj-linkers ((obj ede-proj-target))
509 "List of linkers being used by OBJ.
510 If the `linker' slot is empty, concoct one on a first match found
511 basis for any given type from the `availablelinkers' slot.
512 Otherwise, return the `linker' slot.
513 Converts all symbols into the objects to be used."
514 (when (slot-exists-p obj 'linker)
515 (let ((link (oref obj linker)))
517 ;; Now that we have a pre-set linkers to use, convert type symbols
518 ;; into objects for ease of use
519 (setq link (mapcar 'symbol-value link))
520 (let ((avail (mapcar 'symbol-value (oref obj availablelinkers)))
521 (st (oref obj sourcetype))
522 (sources (oref obj source)))
523 ;; LINKER is not specified, so generate a list from the available
526 (if (ede-want-any-source-files-p (symbol-value (car st)) sources)
527 (let ((c (ede-proj-find-linker avail (car st))))
528 (if c (setq link (cons c link)))))
531 ;; No linker stands out! Loop over our linkers and pull out
532 ;; the first that has no source type requirement.
533 (while (and avail (not (slot-boundp (car avail) 'sourcetype)))
534 (setq avail (cdr avail)))
535 (setq link (cdr avail)))))
536 ;; Return the disovered linkers
540 ;;; Target type specific autogenerating gobbldegook.
543 ;; This provides prevents recursive loading during a compile
545 (require 'ede-pmake "ede-pmake.el")
546 (require 'ede-pconf "ede-pconf.el"))
548 (defun ede-proj-makefile-type (&optional proj)
549 "Makefile type of the current project PROJ."
550 (oref (or proj (ede-current-project)) makefile-type))
552 (defun ede-proj-automake-p (&optional proj)
553 "Return non-nil if the current project PROJ is automake mode."
554 (eq (ede-proj-makefile-type proj) 'Makefile.am))
556 (defun ede-proj-autoconf-p (&optional proj)
557 "Return non-nil if the current project PROJ is automake mode."
558 (eq (ede-proj-makefile-type proj) 'Makefile.in))
560 (defun ede-proj-make-p (&optional proj)
561 "Return non-nil if the current project PROJ is automake mode."
562 (eq (ede-proj-makefile-type proj) 'Makefile))
564 (defmethod ede-proj-dist-makefile ((this ede-proj-project))
565 "Return the name of the Makefile with the DIST target in it for THIS."
566 (cond ((eq (oref this makefile-type) 'Makefile.am)
567 (concat (file-name-directory (oref this file))
569 ((eq (oref this makefile-type) 'Makefile.in)
570 (concat (file-name-directory (oref this file))
572 ((object-assoc "Makefile" 'makefile (oref this targets))
573 (concat (file-name-directory (oref this file))
576 (let ((targets (oref this targets)))
580 'ede-proj-target-makefile)))
581 (setq targets (cdr targets)))
582 (if targets (oref (car targets) makefile)
583 (concat (file-name-directory (oref this file))
586 (defun ede-proj-regenerate ()
587 "Regenerate Makefiles for and edeproject project."
589 (ede-proj-setup-buildenvironment (ede-current-project) t))
591 (defmethod ede-proj-makefile-create-maybe ((this ede-proj-project) mfilename)
592 "Create a Makefile for all Makefile targets in THIS if needed.
593 MFILENAME is the makefile to generate."
594 ;; For now, pass through until dirty is implemented.
596 (if (or (not (file-exists-p mfilename))
597 (file-newer-than-file-p (oref this file) mfilename))
598 (ede-proj-makefile-create this mfilename)))
600 (defmethod ede-proj-setup-buildenvironment ((this ede-proj-project)
602 "Setup the build environment for project THIS.
603 Handles the Makefile, or a Makefile.am configure.in combination.
604 Optional argument FORCE will force items to be regenerated."
606 (ede-proj-makefile-create-maybe this (ede-proj-dist-makefile this))
608 (ede-proj-makefile-create this (ede-proj-dist-makefile this)))
609 (if (ede-proj-automake-p this)
612 ;; If the user wants to force this, do it some other way?
613 (ede-proj-configure-synchronize this)
614 ;; Now run automake to fill in the blanks, autoconf, and other
615 ;; auto thingies so that we can just say "make" when done.
618 ;; Rebuild all subprojects
620 this (lambda (sproj) (ede-proj-setup-buildenvironment sproj force)))
624 ;;; Lower level overloads
626 (defmethod project-rescan ((this ede-proj-project))
627 "Rescan the EDE proj project THIS."
628 (ede-with-projectfile this
629 (goto-char (point-min))
630 (let ((l (read (current-buffer)))
631 (fields (object-slots this))
632 (targets (oref this targets)))
633 (setq l (cdr (cdr l))) ;; objtype and name skip
634 (while fields ; reset to defaults those that dont appear.
635 (if (and (not (assoc (car fields) l))
636 (not (eq (car fields) 'file)))
637 (let ((eieio-skip-typecheck t))
638 ;; This is a hazardous thing, for some elements
639 ;; might not be bound. Skip typechecking and duplicate
640 ;; unbound slots along the way.
641 (eieio-oset this (car fields)
642 (eieio-oref-default this (car fields)))))
643 (setq fields (cdr fields)))
645 (let ((field (car l)) (val (car (cdr l))))
646 (cond ((eq field targets)
647 (let ((targets (oref this targets))
649 (setq val (cdr val)) ;; skip the `list'
651 (let ((o (object-assoc (car (cdr (car val))) ; name
654 (project-rescan o (car val))
655 (setq o (eval (car val))))
656 (setq newtarg (cons o newtarg)))
657 (setq val (cdr val)))
658 (oset this targets newtarg)))
660 (eieio-oset this field val))))
661 (setq l (cdr (cdr l))))))) ;; field/value
663 (defmethod project-rescan ((this ede-proj-target) readstream)
664 "Rescan target THIS from the read list READSTREAM."
665 (setq readstream (cdr (cdr readstream))) ;; constructor/name
667 (let ((tag (car readstream))
668 (val (car (cdr readstream))))
669 (eieio-oset this tag val))
670 (setq readstream (cdr (cdr readstream)))))
673 (add-to-list 'auto-mode-alist '("Project\\.ede" . emacs-lisp-mode))
677 ;;; ede-proj.el ends here