Add 2010 to copyright years.
[bpt/emacs.git] / lisp / cedet / ede / project-am.el
CommitLineData
acc33231
CY
1;;; project-am.el --- A project management scheme based on automake files.
2
114f9c96 3;; Copyright (C) 1998, 1999, 2000, 2003, 2005, 2007, 2008, 2009, 2010
a785b776 4;; Free Software Foundation, Inc.
acc33231
CY
5
6;; Author: Eric M. Ludlam <zappo@gnu.org>
7;; Version: 0.0.3
8;; Keywords: project, make
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26;;
27;; The GNU Automake tool is the first step towards having a really
28;; good project management system. It provides a simple and concise
29;; look at what is actually in a project, and records it in a simple
30;; fashion.
31;;
32;; project-am uses the structure defined in all good GNU projects with
33;; the Automake file as it's base template, and then maintains that
34;; information during edits, automatically updating the automake file
35;; where appropriate.
36
37
38;; (eval-and-compile
39;; ;; Compatibility for makefile mode.
40;; (condition-case nil
41;; (require 'makefile "make-mode")
42;; (error (require 'make-mode "make-mode")))
43
44;; ;; Requiring the .el files prevents incomplete builds.
45;; (require 'eieio "eieio.el")
46;; (require 'ede "ede.el"))
47
48(require 'make-mode)
49(require 'ede)
50(require 'ede/make)
51(require 'ede/makefile-edit)
52
53(declare-function autoconf-parameters-for-macro "ede/autoconf-edit")
54(eval-when-compile (require 'compile))
55
56;;; Code:
57(defgroup project-am nil
58 "File and tag browser frame."
59 :group 'tools
60 :group 'ede
61 )
62
63(defcustom project-am-compile-project-command nil
64 "*Default command used to compile a project."
65 :group 'project-am
66 :type 'string)
67
68(defcustom project-am-compile-target-command (concat ede-make-command " -k %s")
69 "*Default command used to compile a project."
70 :group 'project-am
71 :type 'string)
72
73(defcustom project-am-debug-target-function 'gdb
74 "*Default Emacs command used to debug a target."
75 :group 'project-am
76 :type 'sexp) ; make this be a list some day
77
78(defconst project-am-type-alist
79 '(("bin" project-am-program "bin_PROGRAMS" t)
80 ("sbin" project-am-program "sbin_PROGRAMS" t)
81 ("noinstbin" project-am-program "noinst_PROGRAMS" t)
82 ("checkbin" project-am-program "check_PROGRAMS" t)
83 ("lib" project-am-lib "lib_LIBS" t)
84 ("libraries" project-am-lib "lib_LIBRARIES" t)
85 ("librariesnoinst" project-am-lib "noinst_LIBRARIES" t)
86 ("pkglibraries" project-am-lib "pkglib_LIBRARIES" t)
87 ("checklibs" project-am-lib "check_LIBRARIES" t)
88 ("ltlibraries" project-am-lib "lib_LTLIBRARIES" t)
89 ("ltlibrariesnoinst" project-am-lib "noinst_LTLIBRARIES" t)
90 ("pkgltlibraries" project-am-lib "pkglib_LTLIBRARIES" t)
91 ("checkltlibs" project-am-lib "check_LTLIBRARIES" t)
92 ("headernoinst" project-am-header-noinst "noinst_HEADERS")
93 ("headerinst" project-am-header-inst "include_HEADERS")
94 ("headerpkg" project-am-header-pkg "pkginclude_HEADERS")
95 ("headerpkg" project-am-header-chk "check_HEADERS")
96 ("texinfo" project-am-texinfo "info_TEXINFOS" t)
97 ("man" project-am-man "man_MANS")
98 ("lisp" project-am-lisp "lisp_LISP")
99 ;; for other global files track EXTRA_
100 ("extrabin" project-am-program "EXTRA_PROGRAMS" t)
101 ("builtsrcs" project-am-built-src "BUILT_SOURCES")
102 ("extradist" project-am-extra-dist "EXTRA_DIST")
103 ;; Custom libraries targets?
104 ;; ("ltlibcustom" project-am-lib ".*?_LTLIBRARIES" t)
105 )
106 "Alist of type names and the type of object to create for them.
107Each entry is of th form:
a785b776 108 (EMACSNAME CLASS AUTOMAKEVAR INDIRECT)
acc33231
CY
109where EMACSNAME is a name for Emacs to use.
110CLASS is the EDE target class to represent the target.
111AUTOMAKEVAR is the Automake variable to identify. This cannot be a
112 regular expression.
113INDIRECT is optional. If it is non-nil, then the variable in
114question lists other variables that need to be looked up.")
115
116(defclass project-am-target (ede-target)
117 nil
118 "Base target class for everything in project-am.")
119
120(defclass project-am-objectcode (project-am-target)
121 ((source :initarg :source :documentation "List of source files."))
122 "A target which creates object code, like a C program or library.")
123
124(defclass project-am-program (project-am-objectcode)
125 ((ldadd :initarg :ldadd :documentation "Additional LD args."
126 :initform nil))
127 "A top level program to build")
128
129(defclass project-am-header (project-am-target)
130 ()
131 "A group of misc source files, such as headers.")
132
133(defclass project-am-header-noinst (project-am-header)
134 ()
135 "A group of header files that are not installed.")
136
137(defclass project-am-header-inst (project-am-header)
138 ()
139 "A group of header files that are not installed.")
140
141(defclass project-am-header-pkg (project-am-header)
142 ()
143 "A group of header files that are not installed.")
144
145(defclass project-am-header-chk (project-am-header)
146 ()
147 "A group of header files that are not installed.")
148
149(defclass project-am-lib (project-am-objectcode)
150 nil
151 "A top level library to build")
152
153(defclass project-am-lisp (project-am-target)
154 ()
155 "A group of Emacs Lisp programs to byte compile.")
156
157(defclass project-am-texinfo (project-am-target)
158 ((include :initarg :include
159 :initform nil
160 :documentation "Additional texinfo included in this one."))
161 "A top level texinfo file to build.")
162
163(defclass project-am-man (project-am-target)
164 nil
165 "A top level man file to build.")
166
167;; For generic files tracker like EXTRA_DIST
168(defclass project-am-built-src (project-am-target)
169 ()
170 "A group of Emacs Lisp programs to byte compile.")
171
172(defclass project-am-extra-dist (project-am-target)
173 ()
174 "A group of Emacs Lisp programs to byte compile.")
175
176(defclass project-am-makefile (ede-project)
177 ((targets :initarg :targets
178 :initform nil
179 :documentation "Top level targets in this makefile.")
180 (configureoutputfiles
181 :initform nil
182 :documentation
183 "List of files output from configure system.")
184 )
185 "Encode one makefile.")
186
187;;; Code:
188(defmethod project-add-file ((ot project-am-target))
189 "Add the current buffer into a project.
190OT is the object target. DIR is the directory to start in."
a785b776 191 (let* ((target (if ede-object (error "Already associated w/ a target")
acc33231
CY
192 (let ((amf (project-am-load default-directory)))
193 (if (not amf) (error "No project file"))
194 (completing-read "Target: "
195 (object-assoc-list 'name
196 (oref amf targets))
197 nil t))))
198 ;; The input target might be new. See if we can find it.
199 (amf (ede-load-project-file (oref ot path)))
200 (ot (object-assoc target 'name (oref amf targets)))
201 (ofn (file-name-nondirectory (buffer-file-name))))
202 (if (not ot)
203 (setq ot
204 (project-new-target
205 target (project-am-preferred-target-type (buffer-file-name)))))
206 (ede-with-projectfile ot
207 (makefile-move-to-macro (project-am-macro ot))
208 (ede-maybe-checkout)
209 (makefile-end-of-command)
210 (insert " " ofn)
211 (makefile-fill-paragraph nil)
212 (project-rescan ot)
213 (save-buffer))
214 (setq ede-object ot)))
215
216(defmethod project-remove-file ((ot project-am-target) fnnd)
217 "Remove the current buffer from any project targets."
218 (ede-with-projectfile ot
219 (makefile-move-to-macro (project-am-macro ot))
220 (if (and buffer-read-only vc-mode
221 (y-or-n-p "Checkout Makefile.am from VC? "))
222 (vc-toggle-read-only t))
223 (ede-maybe-checkout)
224 (makefile-navigate-macro (concat " *" (regexp-quote (ede-name fnnd))))
225 (replace-match "" t t nil 0)
226 (makefile-fill-paragraph nil)
227 (project-rescan ot)
228 (save-buffer))
229 (setq ede-object nil))
230
231(defmethod project-edit-file-target ((obj project-am-target))
232 "Edit the target associated w/ this file."
233 (find-file (concat (oref obj path) "Makefile.am"))
234 (goto-char (point-min))
235 (makefile-move-to-macro (project-am-macro obj))
236 (if (= (point-min) (point))
237 (re-search-forward (ede-target-name obj))))
238
239(defmethod project-new-target ((proj project-am-makefile)
240 &optional name type)
241 "Create a new target named NAME.
242Argument TYPE is the type of target to insert. This is a string
243matching something in `project-am-type-alist' or type class symbol.
244Despite the fact that this is a method, it depends on the current
245buffer being in order to provide a smart default target type."
246 (let* ((name (or name (read-string "Name: " "")))
247 (type (or type
248 (completing-read "Type: "
249 project-am-type-alist
250 nil t
251 (cond ((eq major-mode 'texinfo-mode)
252 "texinfo")
253 ((eq major-mode 'nroff-mode)
254 "man")
255 ((eq major-mode 'emacs-lisp-mode)
256 "lisp")
257 (t "bin")))))
258 (ntype (assoc type project-am-type-alist))
259 (ot nil))
260 (setq ot (apply (car (cdr ntype)) name :name name
261 :path (expand-file-name default-directory) nil))
262 (if (not ot) (error "Error creating target object %S" ntype))
263 (ede-with-projectfile ot
264 (goto-char (point-min))
265 (ede-maybe-checkout)
266 (makefile-next-dependency)
267 (if (= (point) (point-min))
268 (goto-char (point-max))
269 (beginning-of-line)
270 (insert "\n")
271 (forward-char -1))
272 ;; Add the new target sources macro (if needed)
273 (if (project-am-macro ot)
274 (makefile-insert-macro (project-am-macro ot)))
275 ;; Add to the list of objects.
276 (goto-char (point-min))
277 (makefile-move-to-macro (car (cdr (cdr ntype))))
278 (if (= (point) (point-min))
279 (progn
280 (if (re-search-forward makefile-macroassign-regex nil t)
281 (progn (forward-line -1)
282 (end-of-line)
283 (insert "\n"))
284 ;; If the above search fails, thats ok. We'd just want to be at
285 ;; point-min anyway.
286 )
287 (makefile-insert-macro (car (cdr (cdr ntype))))))
288 (makefile-end-of-command)
289 (insert " " (ede-target-name ot))
290 (save-buffer)
291 ;; Rescan the object in this makefile.
292 (project-rescan ede-object))))
293
294;(defun project-am-rescan-toplevel ()
295; "Rescan all projects in which the current buffer resides."
296; (interactive)
297; (let* ((tlof (project-am-find-topmost-level default-directory))
298; (tlo (project-am-load tlof))
299; (ede-deep-rescan t)) ; scan deep in this case.
300; ;; tlo is the top level object for whatever file we are in
301; ;; or nil. If we have an object, call the rescan method.
302; (if tlo (project-am-rescan tlo))))
303
304;;
305;; NOTE TO SELF
306;;
307;; This should be handled at the EDE level, calling a method of the
308;; top most project.
309;;
310(defmethod project-compile-project ((obj project-am-target) &optional command)
311 "Compile the entire current project.
312Argument COMMAND is the command to use when compiling."
313 (require 'compile)
314 (if (not command)
315 (setq
316 command
317 ;; This interactive statement was taken from compile, and I'll
318 ;; use the same command history too.
319 (progn
320 (if (not project-am-compile-project-command)
321 (setq project-am-compile-project-command compile-command))
322 (if (or compilation-read-command current-prefix-arg)
323 (read-from-minibuffer "Project compile command: "
324 ;; hardcode make -k
325 ;; This is compile project after all.
326 project-am-compile-project-command
327 nil nil '(compile-history . 1))
328 project-am-compile-project-command))))
329 ;; When compile a project, we might be in a subdirectory,
330 ;; so we have to make sure we move all the way to the top.
331 (let* ((default-directory (project-am-find-topmost-level default-directory)))
332 (compile command)))
333
334(defmethod project-compile-project ((obj project-am-makefile)
335 &optional command)
336 "Compile the entire current project.
337Argument COMMAND is the command to use when compiling."
338 (require 'compile)
339 (if (not command)
340 (setq
341 command
342 ;; This interactive statement was taken from compile, and I'll
343 ;; use the same command history too.
344 (progn
345 (if (not project-am-compile-project-command)
346 (setq project-am-compile-project-command compile-command))
347 (if (or compilation-read-command current-prefix-arg)
348 (read-from-minibuffer "Project compile command: "
349 ;; hardcode make -k
350 ;; This is compile project after all.
351 project-am-compile-project-command
352 nil nil '(compile-history . 1))
353 project-am-compile-project-command))))
354 ;; When compile a project, we might be in a subdirectory,
355 ;; so we have to make sure we move all the way to the top.
356 (let* ((default-directory (project-am-find-topmost-level default-directory)))
357 (compile command)))
358
359(defmethod project-compile-target ((obj project-am-target) &optional command)
360 "Compile the current target.
361Argument COMMAND is the command to use for compiling the target."
362 (require 'compile)
363 (if (not project-am-compile-project-command)
364 (setq project-am-compile-project-command compile-command))
365 (if (not command)
366 (setq
367 command
368 (if compilation-read-command
369 (read-from-minibuffer "Project compile command: "
370 ;; hardcode make -k
371 ;; This is compile project after all.
372 (if ede-object
373 (format
374 project-am-compile-target-command
375 (project-compile-target-command
376 ede-object))
377 project-am-compile-target-command)
378 nil nil
379 '(compile-history . 1))
380 (if ede-object
381 project-am-compile-project-command
382 (format
383 project-am-compile-target-command
384 (project-compile-target-command ede-object))))))
385 ;; We better be in the right place when compiling a specific target.
386 (compile command))
387
388(defmethod project-debug-target ((obj project-am-objectcode))
389 "Run the current project target in a debugger."
390 (let ((tb (get-buffer-create " *padt*"))
391 (dd (oref obj path))
392 (cmd nil))
393 (unwind-protect
394 (progn
67d3ffe4 395 (require 'ede/shell)
acc33231
CY
396 (set-buffer tb)
397 (setq default-directory dd)
398 (setq cmd (read-from-minibuffer
399 "Run (like this): "
400 (concat (symbol-name project-am-debug-target-function)
401 " " (ede-target-name obj))))
402 (funcall project-am-debug-target-function cmd))
403 (kill-buffer tb))))
404
67d3ffe4
CY
405(defmethod project-run-target ((obj project-am-objectcode))
406 "Run the current project target in comint buffer."
407 (let ((tb (get-buffer-create " *padt*"))
408 (dd (oref obj path))
409 (cmd nil))
410 (unwind-protect
411 (progn
412 (set-buffer tb)
413 (setq default-directory dd)
414 (setq cmd (read-from-minibuffer
415 "Run (like this): "
416 (concat (ede-target-name obj))))
417 (ede-shell-run-something obj cmd))
418 (kill-buffer tb))))
419
acc33231
CY
420(defmethod project-make-dist ((this project-am-target))
421 "Run the current project in the debugger."
422 (require 'compile)
423 (if (not project-am-compile-project-command)
424 (setq project-am-compile-project-command compile-command))
425 (project-compile-project this (concat project-am-compile-project-command
426 " dist")))
427
428;;; Project loading and saving
429;;
430(defun project-am-load (project &optional rootproj)
431 "Read an automakefile PROJECT into our data structure.
432Make sure that the tree down to our makefile is complete so that there
433is cohesion in the project. Return the project file (or sub-project).
434If a given set of projects has already been loaded, then do nothing
435but return the project for the directory given.
436Optional ROOTPROJ is the root EDE project."
437 ;; @TODO - rationalize this to the newer EDE way of doing things.
438 (setq project (expand-file-name project))
439 (let* ((ede-constructing t)
440 (fn (project-am-find-topmost-level (file-name-as-directory project)))
441 (amo nil)
442 (trimmed (if (string-match (regexp-quote fn)
443 project)
444 (replace-match "" t t project)
445 ""))
446 (subdir nil))
447 (setq amo (object-assoc (expand-file-name "Makefile.am" fn)
448 'file ede-projects))
449 (if amo
450 (error "Synchronous error in ede/project-am objects")
451 (let ((project-am-constructing t))
452 (setq amo (project-am-load-makefile fn))))
453 (if (not amo)
454 nil
455 ;; Now scan down from amo, and find the current directory
456 ;; from the PROJECT file.
457 (while (< 0 (length trimmed))
458 (if (string-match "\\([a-zA-Z0-9.-]+\\)/" trimmed)
459 (setq subdir (match-string 0 trimmed)
460 trimmed (replace-match "" t t trimmed))
461 (error "Error scanning down path for project"))
462 (setq amo (project-am-subtree
463 amo
464 (expand-file-name "Makefile.am"
465 (expand-file-name subdir fn)))
466 fn (expand-file-name subdir fn)))
467 amo)
468 ))
469
470(defun project-am-find-topmost-level (dir)
471 "Find the topmost automakefile starting with DIR."
472 (let ((newdir dir))
473 (while (or (file-exists-p (concat newdir "Makefile.am"))
474 (file-exists-p (concat newdir "configure.ac"))
475 (file-exists-p (concat newdir "configure.in"))
476 )
477 (setq dir newdir newdir
478 (file-name-directory (directory-file-name newdir))))
479 (expand-file-name dir)))
480
481(defmacro project-am-with-makefile-current (dir &rest forms)
482 "Set the Makefile.am in DIR to be the current buffer.
483Run FORMS while the makefile is current.
484Kill the makefile if it was not loaded before the load."
485 `(let* ((fn (expand-file-name "Makefile.am" ,dir))
486 (fb nil)
487 (kb (get-file-buffer fn)))
488 (if (not (file-exists-p fn))
489 nil
490 (save-excursion
491 (if kb (setq fb kb)
492 ;; We need to find-file this thing, but don't use
493 ;; any semantic features.
29e1a603 494 (let ((semantic-init-hook nil))
acc33231
CY
495 (setq fb (find-file-noselect fn)))
496 )
497 (set-buffer fb)
498 (prog1 ,@forms
499 (if (not kb) (kill-buffer (current-buffer))))))))
500(put 'project-am-with-makefile-current 'lisp-indent-function 1)
501
502(add-hook 'edebug-setup-hook
503 (lambda ()
504 (def-edebug-spec project-am-with-makefile-current
505 (form def-body))))
506
507
508(defun project-am-load-makefile (path)
509 "Convert PATH into a project Makefile, and return its project object.
510It does not check for existing project objects. Use `project-am-load'."
511 (project-am-with-makefile-current path
512 (if (and ede-object (project-am-makefile-p ede-object))
513 ede-object
514 (let* ((pi (project-am-package-info path))
515 (pn (or (nth 0 pi) (project-am-last-dir fn)))
516 (ver (or (nth 1 pi) "0.0"))
517 (bug (nth 2 pi))
518 (cof (nth 3 pi))
519 (ampf (project-am-makefile
520 pn :name pn
521 :version ver
522 :mailinglist (or bug "")
523 :file fn)))
524 (oset ampf :directory (file-name-directory fn))
525 (oset ampf configureoutputfiles cof)
526 (make-local-variable 'ede-object)
527 (setq ede-object ampf)
528 ;; Move the rescan after we set ede-object to prevent recursion
529 (project-rescan ampf)
530 ampf))))
531
532;;; Methods:
533(defmethod ede-find-target ((amf project-am-makefile) buffer)
534 "Fetch the target belonging to BUFFER."
535 (or (call-next-method)
536 (let ((targ (oref amf targets))
537 (sobj (oref amf subproj))
538 (obj nil))
539 (while (and targ (not obj))
540 (if (ede-buffer-mine (car targ) buffer)
541 (setq obj (car targ)))
542 (setq targ (cdr targ)))
543 (while (and sobj (not obj))
544 (setq obj (project-am-buffer-object (car sobj) buffer)
545 sobj (cdr sobj)))
546 obj)))
547
548(defmethod project-targets-for-file ((proj project-am-makefile))
549 "Return a list of targets the project PROJ."
550 (oref proj targets))
551
552(defun project-am-scan-for-targets (currproj dir)
553 "Scan the current Makefile.am for targets.
554CURRPROJ is the current project being scanned.
555DIR is the directory to apply to new targets."
556 (let* ((otargets (oref currproj targets))
557 (ntargets nil)
558 (tmp nil)
559 )
560 (mapc
561 ;; Map all the different types
562 (lambda (typecar)
563 (let ((macro (nth 2 typecar))
564 (class (nth 1 typecar))
565 (indirect (nth 3 typecar))
566 ;(name (car typecar))
567 )
568 (if indirect
569 ;; Map all the found objects
570 (mapc (lambda (lstcar)
571 (setq tmp (object-assoc lstcar 'name otargets))
572 (when (not tmp)
573 (setq tmp (apply class lstcar :name lstcar
574 :path dir nil)))
575 (project-rescan tmp)
576 (setq ntargets (cons tmp ntargets)))
577 (makefile-macro-file-list macro))
578 ;; Non-indirect will have a target whos sources
579 ;; are actual files, not names of other targets.
580 (let ((files (makefile-macro-file-list macro)))
581 (when files
582 (setq tmp (object-assoc macro 'name otargets))
583 (when (not tmp)
584 (setq tmp (apply class macro :name macro
585 :path dir nil)))
586 (project-rescan tmp)
587 (setq ntargets (cons tmp ntargets))
588 ))
589 )
590 ))
591 project-am-type-alist)
592 ntargets))
593
594(defmethod project-rescan ((this project-am-makefile))
595 "Rescan the makefile for all targets and sub targets."
596 (project-am-with-makefile-current (file-name-directory (oref this file))
597 ;;(message "Scanning %s..." (oref this file))
598 (let* ((pi (project-am-package-info (oref this directory)))
599 (pn (nth 0 pi))
600 (pv (nth 1 pi))
601 (bug (nth 2 pi))
602 (cof (nth 3 pi))
603 (osubproj (oref this subproj))
604 (csubproj (or
605 ;; If DIST_SUBDIRS doesn't exist, then go for the
606 ;; static list of SUBDIRS. The DIST version should
607 ;; contain SUBDIRS plus extra stuff.
608 (makefile-macro-file-list "DIST_SUBDIRS")
609 (makefile-macro-file-list "SUBDIRS")))
610 (csubprojexpanded nil)
611 (nsubproj nil)
612 ;; Targets are excluded here because they require
613 ;; special attention.
614 (dir (expand-file-name default-directory))
615 (tmp nil)
616 (ntargets (project-am-scan-for-targets this dir))
617 )
618
619 (and pn (string= (directory-file-name
620 (oref this directory))
621 (directory-file-name
622 (project-am-find-topmost-level
623 (oref this directory))))
624 (oset this name pn)
625 (and pv (oset this version pv))
626 (and bug (oset this mailinglist bug))
627 (oset this configureoutputfiles cof))
628
629; ;; LISP is different. Here there is only one kind of lisp (that I know of
630; ;; anyway) so it doesn't get mapped when it is found.
631; (if (makefile-move-to-macro "lisp_LISP")
632; (let ((tmp (project-am-lisp "lisp"
633; :name "lisp"
634; :path dir)))
635; (project-rescan tmp)
636; (setq ntargets (cons tmp ntargets))))
637;
638 ;; Now that we have this new list, chuck the old targets
639 ;; and replace it with the new list of targets I just created.
640 (oset this targets (nreverse ntargets))
641 ;; We still have a list of targets. For all buffers, make sure
642 ;; their object still exists!
643
644 ;; FIGURE THIS OUT
645
646 (mapc (lambda (sp)
647 (let ((var (makefile-extract-varname-from-text sp))
648 )
649 (if (not var)
650 (setq csubprojexpanded (cons sp csubprojexpanded))
651 ;; If it is a variable, expand that variable, and keep going.
652 (let ((varexp (makefile-macro-file-list var)))
653 (dolist (V varexp)
654 (setq csubprojexpanded (cons V csubprojexpanded)))))
655 ))
656 csubproj)
657
658 ;; Ok, now lets look at all our sub-projects.
659 (mapc (lambda (sp)
660 (let* ((subdir (file-name-as-directory
661 (expand-file-name
662 sp (file-name-directory (oref this :file)))))
663 (submake (expand-file-name
664 "Makefile.am"
665 subdir)))
666 (if (string= submake (oref this :file))
667 nil ;; don't recurse.. please!
668
669 ;; For each project id found, see if we need to recycle,
670 ;; and if we do not, then make a new one. Check the deep
671 ;; rescan value for behavior patterns.
672 (setq tmp (object-assoc
673 submake
674 'file osubproj))
675 (if (not tmp)
676 (setq tmp
677 (condition-case nil
678 ;; In case of problem, ignore it.
679 (project-am-load-makefile subdir)
680 (error nil)))
681 ;; If we have tmp, then rescan it only if deep mode.
682 (if ede-deep-rescan
683 (project-rescan tmp)))
684 ;; Tac tmp onto our list of things to keep, but only
685 ;; if tmp was found.
686 (when tmp
687 ;;(message "Adding %S" (object-print tmp))
688 (setq nsubproj (cons tmp nsubproj)))))
689 )
690 (nreverse csubprojexpanded))
691 (oset this subproj nsubproj)
692 ;; All elements should be updated now.
693 )))
694
695
696(defmethod project-rescan ((this project-am-program))
697 "Rescan object THIS."
698 (oset this :source (makefile-macro-file-list (project-am-macro this)))
699 (oset this :ldadd (makefile-macro-file-list
700 (concat (oref this :name) "_LDADD"))))
701
702(defmethod project-rescan ((this project-am-lib))
703 "Rescan object THIS."
704 (oset this :source (makefile-macro-file-list (project-am-macro this))))
705
706(defmethod project-rescan ((this project-am-texinfo))
707 "Rescan object THIS."
708 (oset this :include (makefile-macro-file-list (project-am-macro this))))
709
710(defmethod project-rescan ((this project-am-man))
711 "Rescan object THIS."
712 (oset this :source (makefile-macro-file-list (project-am-macro this))))
713
714(defmethod project-rescan ((this project-am-lisp))
715 "Rescan the lisp sources."
716 (oset this :source (makefile-macro-file-list (project-am-macro this))))
717
718(defmethod project-rescan ((this project-am-header))
719 "Rescan the Header sources for object THIS."
720 (oset this :source (makefile-macro-file-list (project-am-macro this))))
721
722(defmethod project-rescan ((this project-am-built-src))
723 "Rescan built sources for object THIS."
724 (oset this :source (makefile-macro-file-list "BUILT_SOURCES")))
725
726(defmethod project-rescan ((this project-am-extra-dist))
727 "Rescan object THIS."
728 (oset this :source (makefile-macro-file-list "EXTRA_DIST")))
729 ;; NOTE: The below calls 'file' then checks that it is some sort of
730 ;; text file. The file command may not be available on all platforms
731 ;; and some files may not exist yet. (ie - auto-generated)
732
733 ;;(mapc
734 ;; (lambda (f)
735 ;; ;; prevent garbage to be parsed, could we use :aux ?
736 ;; (if (and (not (member f (oref this :source)))
737 ;; (string-match-p "ASCII\\|text"
738 ;; (shell-command-to-string
739 ;; (concat "file " f))))
740 ;; (oset this :source (cons f (oref this :source)))))
741 ;; (makefile-macro-file-list "EXTRA_DIST")))
742
743(defmethod project-am-macro ((this project-am-objectcode))
744 "Return the default macro to 'edit' for this object type."
745 (concat (subst-char-in-string ?- ?_ (oref this :name)) "_SOURCES"))
746
747(defmethod project-am-macro ((this project-am-header-noinst))
748 "Return the default macro to 'edit' for this object."
749 "noinst_HEADERS")
750
751(defmethod project-am-macro ((this project-am-header-inst))
752 "Return the default macro to 'edit' for this object."
753 "include_HEADERS")
754
755(defmethod project-am-macro ((this project-am-header-pkg))
756 "Return the default macro to 'edit' for this object."
757 "pkginclude_HEADERS")
758
759(defmethod project-am-macro ((this project-am-header-chk))
760 "Return the default macro to 'edit' for this object."
761 "check_HEADERS")
762
763(defmethod project-am-macro ((this project-am-texinfo))
764 "Return the default macro to 'edit' for this object type."
765 (concat (file-name-sans-extension (oref this :name)) "_TEXINFOS"))
766
767(defmethod project-am-macro ((this project-am-man))
768 "Return the default macro to 'edit' for this object type."
769 (oref this :name))
770
771(defmethod project-am-macro ((this project-am-lisp))
772 "Return the default macro to 'edit' for this object."
773 "lisp_LISP")
774
775(defun project-am-buffer-object (amf buffer)
776 "Return an object starting with AMF associated with BUFFER.
777nil means that this buffer belongs to no-one."
778 (if (not amf)
779 nil
780 (if (ede-buffer-mine amf buffer)
781 amf
782 (let ((targ (oref amf targets))
783 (sobj (oref amf subproj))
784 (obj nil))
785 (while (and targ (not obj))
786 (if (ede-buffer-mine (car targ) buffer)
787 (setq obj (car targ)))
788 (setq targ (cdr targ)))
789 (while (and sobj (not obj))
790 (setq obj (project-am-buffer-object (car sobj) buffer)
791 sobj (cdr sobj)))
792 obj))))
793
794(defmethod ede-buffer-mine ((this project-am-makefile) buffer)
795 "Return t if object THIS lays claim to the file in BUFFER."
796 (let ((efn (expand-file-name (buffer-file-name buffer))))
797 (or (string= (oref this :file) efn)
798 (string-match "/configure\\.ac$" efn)
799 (string-match "/configure\\.in$" efn)
800 (string-match "/configure$" efn)
801 ;; Search output files.
802 (let ((ans nil))
803 (dolist (f (oref this configureoutputfiles))
804 (when (string-match (concat (regexp-quote f) "$") efn)
805 (setq ans t)))
806 ans)
807 )))
808
809(defmethod ede-buffer-mine ((this project-am-objectcode) buffer)
810 "Return t if object THIS lays claim to the file in BUFFER."
811 (member (file-name-nondirectory (buffer-file-name buffer))
812 (oref this :source)))
813
814(defmethod ede-buffer-mine ((this project-am-texinfo) buffer)
815 "Return t if object THIS lays claim to the file in BUFFER."
816 (let ((bfn (buffer-file-name buffer)))
817 (or (string= (oref this :name) (file-name-nondirectory bfn))
818 (member (file-name-nondirectory bfn) (oref this :include)))))
819
820(defmethod ede-buffer-mine ((this project-am-man) buffer)
821 "Return t if object THIS lays claim to the file in BUFFER."
822 (string= (oref this :name) (buffer-file-name buffer)))
823
824(defmethod ede-buffer-mine ((this project-am-lisp) buffer)
825 "Return t if object THIS lays claim to the file in BUFFER."
826 (member (file-name-nondirectory (buffer-file-name buffer))
827 (oref this :source)))
828
829(defmethod project-am-subtree ((ampf project-am-makefile) subdir)
830 "Return the sub project in AMPF specified by SUBDIR."
831 (object-assoc (expand-file-name subdir) 'file (oref ampf subproj)))
832
833(defmethod project-compile-target-command ((this project-am-target))
834 "Default target to use when compiling a given target."
835 ;; This is a pretty good default for most.
836 "")
837
838(defmethod project-compile-target-command ((this project-am-objectcode))
839 "Default target to use when compiling an object code target."
840 (oref this :name))
841
842(defmethod project-compile-target-command ((this project-am-texinfo))
843 "Default target t- use when compling a texinfo file."
844 (let ((n (oref this :name)))
845 (if (string-match "\\.texi?\\(nfo\\)?" n)
846 (setq n (replace-match ".info" t t n)))
847 n))
848
849\f
850;;; Generic useful functions
851
852(defun project-am-last-dir (file)
853 "Return the last part of a directory name.
854Argument FILE is the file to extract the end directory name from."
855 (let* ((s (file-name-directory file))
856 (d (directory-file-name s))
857 )
858 (file-name-nondirectory d))
859 )
860
861(defun project-am-preferred-target-type (file)
862 "For FILE, return the preferred type for that file."
863 (cond ((string-match "\\.texi?\\(nfo\\)$" file)
864 project-am-texinfo)
865 ((string-match "\\.[0-9]$" file)
866 project-am-man)
867 ((string-match "\\.el$" file)
868 project-am-lisp)
869 (t
870 project-am-program)))
871
872(defmethod ede-buffer-header-file((this project-am-objectcode) buffer)
873 "There are no default header files."
874 (or (call-next-method)
875 (let ((s (oref this source))
876 (found nil))
877 (while (and s (not found))
878 ;; Add more logic here if applicable.
879 (if (string-match "\\.\\(h\\|H\\|hh\\|hpp\\)" (car s))
880 (setq found (car s)))
881 (setq s (cdr s)))
882 found)))
883
884(defmethod ede-documentation ((this project-am-texinfo))
885 "Return a list of files that provides documentation.
886Documentation is not for object THIS, but is provided by THIS for other
887files in the project."
888 (let* ((src (append (oref this source)
889 (oref this include)))
890 (proj (ede-target-parent this))
891 (dir (oref proj directory))
892 (out nil))
893 ;; Loop over all entries and expand
894 (while src
895 (setq out (cons
896 (expand-file-name (car src) dir)
897 out))
898 (setq src (cdr src)))
899 ;; return it
900 out))
901
902
903;;; Configure.in queries.
904;;
905(defvar project-am-autoconf-file-options
906 '("configure.in" "configure.ac")
907 "List of possible configure files to look in for project info.")
908
909(defun project-am-autoconf-file (dir)
910 "Return the name of the autoconf file to use in DIR."
911 (let ((ans nil))
912 (dolist (L project-am-autoconf-file-options)
913 (when (file-exists-p (expand-file-name L dir))
914 (setq ans (expand-file-name L dir))))
915 ans))
916
917(defmacro project-am-with-config-current (file &rest forms)
918 "Set the Configure FILE in the top most directory above DIR as current.
919Run FORMS in the configure file.
920Kill the Configure buffer if it was not already in a buffer."
921 `(save-excursion
922 (let ((fb (generate-new-buffer ,file)))
923 (set-buffer fb)
924 (erase-buffer)
925 (insert-file-contents ,file)
926 (prog1 ,@forms
927 (kill-buffer fb)))))
928
929(put 'project-am-with-config-current 'lisp-indent-function 1)
930
931(add-hook 'edebug-setup-hook
932 (lambda ()
933 (def-edebug-spec project-am-with-config-current
934 (form def-body))))
935
936(defmacro project-am-extract-shell-variable (var)
937 "Extract the value of the shell variable VAR from a shell script."
938 (save-excursion
939 (goto-char (point-min))
940 (when (re-search-forward (concat "^" (regexp-quote var) "\\s-*=\\s-*")
941 nil t)
942 (buffer-substring-no-properties (point) (point-at-eol)))))
943
944(defun project-am-extract-package-info (dir)
945 "Extract the package information for directory DIR."
946 (let ((conf-in (project-am-autoconf-file dir))
947 (conf-sh (expand-file-name "configure" dir))
948 (name (file-name-nondirectory
949 (directory-file-name dir)))
950 (ver "1.0")
951 (bugrep nil)
952 (configfiles nil)
953 )
954 (cond
955 ;; Try configure.in or configure.ac
956 (conf-in
957 (require 'ede/autoconf-edit)
958 (project-am-with-config-current conf-in
959 (let ((aci (autoconf-parameters-for-macro "AC_INIT"))
960 (aia (autoconf-parameters-for-macro "AM_INIT_AUTOMAKE"))
961 (acf (autoconf-parameters-for-macro "AC_CONFIG_FILES"))
962 (aco (autoconf-parameters-for-macro "AC_OUTPUT"))
963 )
964 (cond
965 ;; AC init has more than 1 parameter
966 ((> (length aci) 1)
967 (setq name (nth 0 aci)
968 ver (nth 1 aci)
969 bugrep (nth 2 aci)))
970 ;; The init automake has more than 1 parameter
971 ((> (length aia) 1)
972 (setq name (nth 0 aia)
973 ver (nth 1 aia)
974 bugrep (nth 2 aia)))
975 )
976 ;; AC_CONFIG_FILES, or AC_OUTPUT lists everything that
977 ;; should be detected as part of this PROJECT, but not in a
978 ;; particular TARGET.
979 (let ((outfiles (cond (aco (list (car aco)))
980 (t acf))))
981 (if (> (length outfiles) 1)
982 (setq configfiles outfiles)
983 (setq configfiles (split-string (car outfiles) " " t)))
984 )
985 ))
986 )
987 ;; Else, try the script
988 ((file-exists-p conf-sh)
989 (project-am-with-config-current conf-sh
990 (setq name (project-am-extract-shell-variable "PACKAGE_NAME")
991 ver (project-am-extract-shell-variable "PACKAGE_VERSION")
992 )
993 ))
994 ;; Don't know what else....
995 (t
996 nil))
997 ;; Return stuff
998 (list name ver bugrep configfiles)
999 ))
1000
1001(defun project-am-package-info (dir)
1002 "Get the package information for directory topmost project dir over DIR.
a785b776 1003Calculates the info with `project-am-extract-package-info'."
acc33231
CY
1004 (let ((top (ede-toplevel)))
1005 (when top (setq dir (oref top :directory)))
1006 (project-am-extract-package-info dir)))
1007
1008(provide 'ede/project-am)
1009
3999968a 1010;; arch-tag: 528db935-f186-4240-b647-e305c5b784a2
acc33231 1011;;; ede/project-am.el ends here