]> code.delx.au - gnu-emacs/blob - lisp/cedet/ede/project-am.el
c5042ca3a9a038b384ae1fd84e3297da88d31e91
[gnu-emacs] / lisp / cedet / ede / project-am.el
1 ;;; project-am.el --- A project management scheme based on automake files.
2
3 ;; Copyright (C) 1998, 1999, 2000, 2003, 2005, 2007, 2008, 2009, 2010, 2011
4 ;; Free Software Foundation, Inc.
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 its base template, and then maintains that
34 ;; information during edits, automatically updating the automake file
35 ;; where appropriate.
36
37 (require 'make-mode)
38 (require 'ede)
39 (require 'ede/make)
40 (require 'ede/makefile-edit)
41 (require 'semantic/find) ;; for semantic-find-tags-by-...
42 (require 'ede/autoconf-edit)
43
44 (declare-function autoconf-parameters-for-macro "ede/autoconf-edit")
45 (declare-function ede-shell-run-something "ede/shell")
46 (eval-when-compile (require 'compile))
47
48 ;;; Code:
49 (defgroup project-am nil
50 "File and tag browser frame."
51 :group 'tools
52 :group 'ede
53 )
54
55 (defcustom project-am-compile-project-command nil
56 "*Default command used to compile a project."
57 :group 'project-am
58 :type 'string)
59
60 (defcustom project-am-compile-target-command (concat ede-make-command " -k %s")
61 "*Default command used to compile a project."
62 :group 'project-am
63 :type 'string)
64
65 (defcustom project-am-debug-target-function 'gdb
66 "*Default Emacs command used to debug a target."
67 :group 'project-am
68 :type 'sexp) ; make this be a list some day
69
70 (defconst project-am-type-alist
71 '(("bin" project-am-program "bin_PROGRAMS" t)
72 ("sbin" project-am-program "sbin_PROGRAMS" t)
73 ("noinstbin" project-am-program "noinst_PROGRAMS" t)
74 ("checkbin" project-am-program "check_PROGRAMS" t)
75 ("lib" project-am-lib "lib_LIBS" t)
76 ("libraries" project-am-lib "lib_LIBRARIES" t)
77 ("librariesnoinst" project-am-lib "noinst_LIBRARIES" t)
78 ("pkglibraries" project-am-lib "pkglib_LIBRARIES" t)
79 ("checklibs" project-am-lib "check_LIBRARIES" t)
80 ("ltlibraries" project-am-lib "lib_LTLIBRARIES" t)
81 ("ltlibrariesnoinst" project-am-lib "noinst_LTLIBRARIES" t)
82 ("pkgltlibraries" project-am-lib "pkglib_LTLIBRARIES" t)
83 ("checkltlibs" project-am-lib "check_LTLIBRARIES" t)
84 ("headernoinst" project-am-header-noinst "noinst_HEADERS")
85 ("headerinst" project-am-header-inst "include_HEADERS")
86 ("headerpkg" project-am-header-pkg "pkginclude_HEADERS")
87 ("headerpkg" project-am-header-chk "check_HEADERS")
88 ("texinfo" project-am-texinfo "info_TEXINFOS" t)
89 ("man" project-am-man "man_MANS")
90 ("lisp" project-am-lisp "lisp_LISP")
91 ;; for other global files track EXTRA_
92 ("extrabin" project-am-program "EXTRA_PROGRAMS" t)
93 ("builtsrcs" project-am-built-src "BUILT_SOURCES")
94 ("extradist" project-am-extra-dist "EXTRA_DIST")
95 ;; Custom libraries targets?
96 ;; ("ltlibcustom" project-am-lib ".*?_LTLIBRARIES" t)
97 )
98 "Alist of type names and the type of object to create for them.
99 Each entry is of the form:
100 (EMACSNAME CLASS AUTOMAKEVAR INDIRECT)
101 where EMACSNAME is a name for Emacs to use.
102 CLASS is the EDE target class to represent the target.
103 AUTOMAKEVAR is the Automake variable to identify. This cannot be a
104 regular expression.
105 INDIRECT is optional. If it is non-nil, then the variable in
106 question lists other variables that need to be looked up.")
107
108
109 (defconst project-am-meta-type-alist
110 '((project-am-program "_PROGRAMS$" t)
111 (project-am-lib "_\\(LIBS\\|LIBRARIES\\|LTLIBRARIES\\)$" t)
112
113 ;; direct primary target use a dummy object (man target)
114 ;; update to: * 3.3 Uniform in automake-1.11 info node.
115 (project-am-man "_\\(DATA\\|HEADERS\\|PYTHON\\|JAVA\\|SCRIPTS\\|MANS\\|TEXINFOS\\)$" nil)
116 )
117 "Alist of meta-target type, each entry has form:
118 (CLASS REGEXPVAR INDIRECT)
119 where CLASS is the EDE target class for target.
120 REGEXPVAR is the regexp used in `semantic-find-tags-by-name-regexp'.
121 INDIRECT is optional. If it is non-nil, then the variable in it have
122 other meta-variable based on this name.")
123
124
125 (defclass project-am-target (ede-target)
126 nil
127 "Base target class for everything in project-am.")
128
129 (defclass project-am-objectcode (project-am-target)
130 ((source :initarg :source :documentation "List of source files."))
131 "A target which creates object code, like a C program or library.")
132
133 (defclass project-am-program (project-am-objectcode)
134 ((ldadd :initarg :ldadd :documentation "Additional LD args."
135 :initform nil))
136 "A top level program to build")
137
138 (defclass project-am-header (project-am-target)
139 ()
140 "A group of misc source files, such as headers.")
141
142 (defclass project-am-header-noinst (project-am-header)
143 ()
144 "A group of header files that are not installed.")
145
146 (defclass project-am-header-inst (project-am-header)
147 ()
148 "A group of header files that are not installed.")
149
150 (defclass project-am-header-pkg (project-am-header)
151 ()
152 "A group of header files that are not installed.")
153
154 (defclass project-am-header-chk (project-am-header)
155 ()
156 "A group of header files that are not installed.")
157
158 (defclass project-am-lib (project-am-objectcode)
159 nil
160 "A top level library to build")
161
162 (defclass project-am-lisp (project-am-target)
163 ()
164 "A group of Emacs Lisp programs to byte compile.")
165
166 (defclass project-am-texinfo (project-am-target)
167 ((include :initarg :include
168 :initform nil
169 :documentation "Additional texinfo included in this one."))
170 "A top level texinfo file to build.")
171
172 (defclass project-am-man (project-am-target)
173 nil
174 "A top level man file to build.")
175
176 ;; For generic files tracker like EXTRA_DIST
177 (defclass project-am-built-src (project-am-target)
178 ()
179 "A group of Emacs Lisp programs to byte compile.")
180
181 (defclass project-am-extra-dist (project-am-target)
182 ()
183 "A group of Emacs Lisp programs to byte compile.")
184
185 (defclass project-am-makefile (ede-project)
186 ((targets :initarg :targets
187 :initform nil
188 :documentation "Top level targets in this makefile.")
189 (configureoutputfiles
190 :initform nil
191 :documentation
192 "List of files output from configure system.")
193 )
194 "Encode one makefile.")
195
196 ;;; Code:
197 (defmethod project-add-file ((ot project-am-target))
198 "Add the current buffer into a project.
199 OT is the object target. DIR is the directory to start in."
200 (let* ((target (if ede-object (error "Already associated w/ a target")
201 (let ((amf (project-am-load default-directory)))
202 (if (not amf) (error "No project file"))
203 (completing-read "Target: "
204 (object-assoc-list 'name
205 (oref amf targets))
206 nil t))))
207 ;; The input target might be new. See if we can find it.
208 (amf (ede-load-project-file (oref ot path)))
209 (ot (object-assoc target 'name (oref amf targets)))
210 (ofn (file-name-nondirectory (buffer-file-name))))
211 (if (not ot)
212 (setq ot
213 (project-new-target
214 target (project-am-preferred-target-type (buffer-file-name)))))
215 (ede-with-projectfile ot
216 (makefile-move-to-macro (project-am-macro ot))
217 (ede-maybe-checkout)
218 (makefile-end-of-command)
219 (insert " " ofn)
220 (makefile-fill-paragraph nil)
221 (project-rescan ot)
222 (save-buffer))
223 (setq ede-object ot)))
224
225 (defmethod project-remove-file ((ot project-am-target) fnnd)
226 "Remove the current buffer from any project targets."
227 (ede-with-projectfile ot
228 (makefile-move-to-macro (project-am-macro ot))
229 (if (and buffer-read-only vc-mode
230 (y-or-n-p "Checkout Makefile.am from VC? "))
231 (vc-toggle-read-only t))
232 (ede-maybe-checkout)
233 (makefile-navigate-macro (concat " *" (regexp-quote (ede-name fnnd))))
234 (replace-match "" t t nil 0)
235 (makefile-fill-paragraph nil)
236 (project-rescan ot)
237 (save-buffer))
238 (setq ede-object nil))
239
240 (defmethod project-edit-file-target ((obj project-am-target))
241 "Edit the target associated w/ this file."
242 (find-file (concat (oref obj path) "Makefile.am"))
243 (goto-char (point-min))
244 (makefile-move-to-macro (project-am-macro obj))
245 (if (= (point-min) (point))
246 (re-search-forward (ede-target-name obj))))
247
248 (defmethod project-new-target ((proj project-am-makefile)
249 &optional name type)
250 "Create a new target named NAME.
251 Argument TYPE is the type of target to insert. This is a string
252 matching something in `project-am-type-alist' or type class symbol.
253 Despite the fact that this is a method, it depends on the current
254 buffer being in order to provide a smart default target type."
255 (let* ((name (or name (read-string "Name: " "")))
256 (type (or type
257 (completing-read "Type: "
258 project-am-type-alist
259 nil t
260 (cond ((eq major-mode 'texinfo-mode)
261 "texinfo")
262 ((eq major-mode 'nroff-mode)
263 "man")
264 ((eq major-mode 'emacs-lisp-mode)
265 "lisp")
266 (t "bin")))))
267 (ntype (assoc type project-am-type-alist))
268 (ot nil))
269 (setq ot (apply (car (cdr ntype)) name :name name
270 :path (expand-file-name default-directory) nil))
271 (if (not ot) (error "Error creating target object %S" ntype))
272 (ede-with-projectfile ot
273 (goto-char (point-min))
274 (ede-maybe-checkout)
275 (makefile-next-dependency)
276 (if (= (point) (point-min))
277 (goto-char (point-max))
278 (beginning-of-line)
279 (insert "\n")
280 (forward-char -1))
281 ;; Add the new target sources macro (if needed)
282 (if (project-am-macro ot)
283 (makefile-insert-macro (project-am-macro ot)))
284 ;; Add to the list of objects.
285 (goto-char (point-min))
286 (makefile-move-to-macro (car (cdr (cdr ntype))))
287 (if (= (point) (point-min))
288 (progn
289 (if (re-search-forward makefile-macroassign-regex nil t)
290 (progn (forward-line -1)
291 (end-of-line)
292 (insert "\n"))
293 ;; If the above search fails, thats ok. We'd just want to be at
294 ;; point-min anyway.
295 )
296 (makefile-insert-macro (car (cdr (cdr ntype))))))
297 (makefile-end-of-command)
298 (insert " " (ede-target-name ot))
299 (save-buffer)
300 ;; Rescan the object in this makefile.
301 (project-rescan ede-object))))
302
303 ;;
304 ;; NOTE TO SELF
305 ;;
306 ;; This should be handled at the EDE level, calling a method of the
307 ;; top most project.
308 ;;
309 (defmethod project-compile-project ((obj project-am-target) &optional command)
310 "Compile the entire current project.
311 Argument COMMAND is the command to use when compiling."
312 (require 'compile)
313 (if (not command)
314 (setq
315 command
316 ;; This interactive statement was taken from compile, and I'll
317 ;; use the same command history too.
318 (progn
319 (if (not project-am-compile-project-command)
320 (setq project-am-compile-project-command compile-command))
321 (if (or compilation-read-command current-prefix-arg)
322 (read-from-minibuffer "Project compile command: "
323 ;; hardcode make -k
324 ;; This is compile project after all.
325 project-am-compile-project-command
326 nil nil '(compile-history . 1))
327 project-am-compile-project-command))))
328 ;; When compile a project, we might be in a subdirectory,
329 ;; so we have to make sure we move all the way to the top.
330 (let* ((default-directory (project-am-find-topmost-level default-directory)))
331 (compile command)))
332
333 (defmethod project-compile-project ((obj project-am-makefile)
334 &optional command)
335 "Compile the entire current project.
336 Argument COMMAND is the command to use when compiling."
337 (require 'compile)
338 (if (not command)
339 (setq
340 command
341 ;; This interactive statement was taken from compile, and I'll
342 ;; use the same command history too.
343 (progn
344 (if (not project-am-compile-project-command)
345 (setq project-am-compile-project-command compile-command))
346 (if (or compilation-read-command current-prefix-arg)
347 (read-from-minibuffer "Project compile command: "
348 ;; hardcode make -k
349 ;; This is compile project after all.
350 project-am-compile-project-command
351 nil nil '(compile-history . 1))
352 project-am-compile-project-command))))
353 ;; When compile a project, we might be in a subdirectory,
354 ;; so we have to make sure we move all the way to the top.
355 (let* ((default-directory (project-am-find-topmost-level default-directory)))
356 (compile command)))
357
358 (defmethod project-compile-target ((obj project-am-target) &optional command)
359 "Compile the current target.
360 Argument COMMAND is the command to use for compiling the target."
361 (require 'compile)
362 (if (not project-am-compile-project-command)
363 (setq project-am-compile-project-command compile-command))
364 (if (not command)
365 (setq
366 command
367 (if compilation-read-command
368 (read-from-minibuffer "Project compile command: "
369 ;; hardcode make -k
370 ;; This is compile project after all.
371 (if ede-object
372 (format
373 project-am-compile-target-command
374 (project-compile-target-command
375 ede-object))
376 project-am-compile-target-command)
377 nil nil
378 '(compile-history . 1))
379 (if ede-object
380 project-am-compile-project-command
381 (format
382 project-am-compile-target-command
383 (project-compile-target-command ede-object))))))
384 ;; We better be in the right place when compiling a specific target.
385 (compile command))
386
387 (defmethod project-debug-target ((obj project-am-objectcode))
388 "Run the current project target in a debugger."
389 (let ((tb (get-buffer-create " *padt*"))
390 (dd (oref obj path))
391 (cmd nil))
392 (unwind-protect
393 (progn
394 (require 'ede/shell)
395 (set-buffer tb)
396 (setq default-directory dd)
397 (setq cmd (read-from-minibuffer
398 "Run (like this): "
399 (concat (symbol-name project-am-debug-target-function)
400 " " (ede-target-name obj))))
401 (funcall project-am-debug-target-function cmd))
402 (kill-buffer tb))))
403
404 (defmethod project-run-target ((obj project-am-objectcode))
405 "Run the current project target in comint buffer."
406 (require 'ede/shell)
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
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 (directory &optional rootproj)
431 "Read an automakefile DIRECTORY into our data structure.
432 If a given set of projects has already been loaded, then do nothing
433 but return the project for the directory given.
434 Optional ROOTPROJ is the root EDE project."
435 (let* ((ede-constructiong t)
436 (amo (object-assoc (expand-file-name "Makefile.am" directory)
437 'file ede-projects)))
438 (when (not amo)
439 (setq amo (project-am-load-makefile directory)))
440 amo))
441
442 (defun project-am-find-topmost-level (dir)
443 "Find the topmost automakefile starting with DIR."
444 (let ((newdir dir))
445 (while (or (file-exists-p (concat newdir "Makefile.am"))
446 (file-exists-p (concat newdir "configure.ac"))
447 (file-exists-p (concat newdir "configure.in"))
448 )
449 (setq dir newdir newdir
450 (file-name-directory (directory-file-name newdir))))
451 (expand-file-name dir)))
452
453 (defmacro project-am-with-makefile-current (dir &rest forms)
454 "Set the Makefile.am in DIR to be the current buffer.
455 Run FORMS while the makefile is current.
456 Kill the makefile if it was not loaded before the load."
457 `(let* ((fn (expand-file-name "Makefile.am" ,dir))
458 (fb nil)
459 (kb (get-file-buffer fn)))
460 (if (not (file-exists-p fn))
461 nil
462 (save-excursion
463 (if kb (setq fb kb)
464 ;; We need to find-file this thing, but don't use
465 ;; any semantic features.
466 (let ((semantic-init-hook nil)
467 (recentf-exclude '( (lambda (f) t) ))
468 )
469 (setq fb (find-file-noselect fn)))
470 )
471 (set-buffer fb)
472 (prog1 ,@forms
473 (if (not kb) (kill-buffer (current-buffer))))))))
474 (put 'project-am-with-makefile-current 'lisp-indent-function 1)
475
476 (add-hook 'edebug-setup-hook
477 (lambda ()
478 (def-edebug-spec project-am-with-makefile-current
479 (form def-body))))
480
481
482 (defun project-am-load-makefile (path &optional suggestedname)
483 "Convert PATH into a project Makefile, and return its project object.
484 It does not check for existing project objects. Use `project-am-load'.
485 Optional argument SUGGESTEDNAME will be the project name.
486 This is used when subprojects are made in named subdirectories."
487 (project-am-with-makefile-current path
488 (if (and ede-object (project-am-makefile-p ede-object))
489 ede-object
490 (let* ((pi (project-am-package-info path))
491 (sfn (when suggestedname
492 (project-am-last-dir suggestedname)))
493 (pn (or sfn (nth 0 pi) (project-am-last-dir fn)))
494 (ver (or (nth 1 pi) "0.0"))
495 (bug (nth 2 pi))
496 (cof (nth 3 pi))
497 (ampf (project-am-makefile
498 pn :name pn
499 :version ver
500 :mailinglist (or bug "")
501 :file fn)))
502 (oset ampf :directory (file-name-directory fn))
503 (oset ampf configureoutputfiles cof)
504 (make-local-variable 'ede-object)
505 (setq ede-object ampf)
506 ;; Move the rescan after we set ede-object to prevent recursion
507 (project-rescan ampf)
508 ampf))))
509
510 ;;; Methods:
511 (defmethod project-targets-for-file ((proj project-am-makefile))
512 "Return a list of targets the project PROJ."
513 (oref proj targets))
514
515 (defun project-am-scan-for-targets (currproj dir)
516 "Scan the current Makefile.am for targets.
517 CURRPROJ is the current project being scanned.
518 DIR is the directory to apply to new targets."
519 (let* ((otargets (oref currproj targets))
520 ;; `ntargets' results in complete targets list
521 ;; not only the new targets by diffing.
522 (ntargets nil)
523 (tmp nil)
524 )
525
526 (mapc
527 ;; Map all the different types
528 (lambda (typecar)
529 (let ((macro (nth 2 typecar))
530 (class (nth 1 typecar))
531 (indirect (nth 3 typecar))
532 )
533 (if indirect
534 ;; Map all the found objects
535 (mapc (lambda (lstcar)
536 (setq tmp (object-assoc lstcar 'name otargets))
537 (when (not tmp)
538 (setq tmp (apply class lstcar :name lstcar
539 :path dir nil)))
540 (project-rescan tmp)
541 (setq ntargets (cons tmp ntargets)))
542 (makefile-macro-file-list macro))
543 ;; Non-indirect will have a target whos sources
544 ;; are actual files, not names of other targets.
545 (let ((files (makefile-macro-file-list macro)))
546 (when files
547 (setq tmp (object-assoc macro 'name otargets))
548 (when (not tmp)
549 (setq tmp (apply class macro :name macro
550 :path dir nil)))
551 (project-rescan tmp)
552 (setq ntargets (cons tmp ntargets))
553 ))
554 )
555 ))
556 project-am-type-alist)
557
558 ;; At now check variables for meta-target regexp
559 ;; We have to check ntargets to avoid useless rescan.
560 ;; Also we have check otargets to prevent duplication.
561 (mapc
562 (lambda (typecar)
563 (let ((class (nth 0 typecar))
564 (metaregex (nth 1 typecar))
565 (indirect (nth 2 typecar)))
566 (if indirect
567 ;; Map all the found objects
568 (mapc
569 (lambda (lstcar)
570 (unless (object-assoc lstcar 'name ntargets)
571 (or
572 (setq tmp (object-assoc lstcar 'name otargets))
573 (setq tmp (apply class lstcar :name lstcar
574 :path dir nil)))
575 (project-rescan tmp)
576 (setq ntargets (cons tmp ntargets))))
577 ;; build a target list to map over
578 (let (atargets)
579 (dolist (TAG
580 (semantic-find-tags-by-name-regexp
581 metaregex (semantic-find-tags-by-class
582 'variable (semantic-fetch-tags))))
583 ;; default-value have to be a list
584 (when (cadr (assoc ':default-value TAG))
585 (setq atargets
586 (append
587 (nreverse (cadr (assoc ':default-value TAG)))
588 atargets))))
589 (nreverse atargets)))
590
591 ;; else not indirect, TODO: FIX various direct meta type in a sane way.
592 (dolist (T (semantic-find-tags-by-name-regexp
593 metaregex (semantic-find-tags-by-class
594 'variable (semantic-fetch-tags))))
595 (unless (setq tmp (object-assoc (car T) 'name ntargets))
596 (or (setq tmp (object-assoc (car T) 'name otargets))
597 ;; we are really new
598 (setq tmp (apply class (car T) :name (car T)
599 :path dir nil)))
600 (project-rescan tmp)
601 (setq ntargets (cons tmp ntargets))))
602 )))
603 project-am-meta-type-alist)
604 ntargets))
605
606 (defun project-am-expand-subdirlist (place subdirs)
607 "Store in PLACE the SUBDIRS expanded from variables.
608 Strip out duplicates, and recurse on variables."
609 (mapc (lambda (sp)
610 (let ((var (makefile-extract-varname-from-text sp)))
611 (if var
612 ;; If it is a variable, expand that variable, and keep going.
613 (project-am-expand-subdirlist
614 place (makefile-macro-file-list var))
615 ;; Else, add SP in if it isn't a dup.
616 (if (member sp (symbol-value place))
617 nil ; don't do it twice.
618 (set place (cons sp (symbol-value place))) ;; add
619 ))))
620 subdirs)
621 )
622
623 (defmethod project-rescan ((this project-am-makefile) &optional suggestedname)
624 "Rescan the makefile for all targets and sub targets."
625 (project-am-with-makefile-current (file-name-directory (oref this file))
626 ;;(message "Scanning %s..." (oref this file))
627 (let* ((pi (project-am-package-info (oref this directory)))
628 (pn (nth 0 pi))
629 (pv (nth 1 pi))
630 (bug (nth 2 pi))
631 (cof (nth 3 pi))
632 (osubproj (oref this subproj))
633 ;; 1/30/10 - We need to append these two lists together,
634 ;; then strip out duplicates. Expanding this list (via
635 ;; references to other variables should also strip out dups
636 (csubproj (append
637 (makefile-macro-file-list "DIST_SUBDIRS")
638 (makefile-macro-file-list "SUBDIRS")))
639 (csubprojexpanded nil)
640 (nsubproj nil)
641 ;; Targets are excluded here because they require
642 ;; special attention.
643 (dir (expand-file-name default-directory))
644 (tmp nil)
645 (ntargets (project-am-scan-for-targets this dir))
646 )
647 (if suggestedname
648 (oset this name (project-am-last-dir suggestedname))
649 ;; Else, setup toplevel project info.
650 (and pn (string= (directory-file-name
651 (oref this directory))
652 (directory-file-name
653 (project-am-find-topmost-level
654 (oref this directory))))
655 (oset this name pn)
656 (and pv (oset this version pv))
657 (and bug (oset this mailinglist bug))
658 (oset this configureoutputfiles cof)))
659 ;; Now that we have this new list, chuck the old targets
660 ;; and replace it with the new list of targets I just created.
661 (oset this targets (nreverse ntargets))
662 ;; We still have a list of targets. For all buffers, make sure
663 ;; their object still exists!
664 ;; FIGURE THIS OUT
665 (project-am-expand-subdirlist 'csubprojexpanded csubproj)
666 ;; Ok, now lets look at all our sub-projects.
667 (mapc (lambda (sp)
668 (let* ((subdir (file-name-as-directory
669 (expand-file-name
670 sp (file-name-directory (oref this :file)))))
671 (submake (expand-file-name
672 "Makefile.am"
673 subdir)))
674 (if (string= submake (oref this :file))
675 nil ;; don't recurse.. please!
676 ;; For each project id found, see if we need to recycle,
677 ;; and if we do not, then make a new one. Check the deep
678 ;; rescan value for behavior patterns.
679 (setq tmp (object-assoc
680 submake
681 'file osubproj))
682 (if (not tmp)
683 (setq tmp
684 (condition-case nil
685 ;; In case of problem, ignore it.
686 (project-am-load-makefile subdir subdir)
687 (error nil)))
688 ;; If we have tmp, then rescan it only if deep mode.
689 (if ede-deep-rescan
690 (project-rescan tmp subdir)))
691 ;; Tac tmp onto our list of things to keep, but only
692 ;; if tmp was found.
693 (when tmp
694 ;;(message "Adding %S" (object-print tmp))
695 (setq nsubproj (cons tmp nsubproj)))))
696 )
697 (nreverse csubprojexpanded))
698 (oset this subproj nsubproj)
699 ;; All elements should be updated now.
700 )))
701
702
703 (defmethod project-rescan ((this project-am-program))
704 "Rescan object THIS."
705 (oset this :source (makefile-macro-file-list (project-am-macro this)))
706 (unless (oref this :source)
707 (oset this :source (list (concat (oref this :name) ".c"))))
708 (oset this :ldadd (makefile-macro-file-list
709 (concat (oref this :name) "_LDADD"))))
710
711 (defmethod project-rescan ((this project-am-lib))
712 "Rescan object THIS."
713 (oset this :source (makefile-macro-file-list (project-am-macro this)))
714 (unless (oref this :source)
715 (oset this :source (list (concat (file-name-sans-extension (oref this :name)) ".c")))))
716
717 (defmethod project-rescan ((this project-am-texinfo))
718 "Rescan object THIS."
719 (oset this :include (makefile-macro-file-list (project-am-macro this))))
720
721 (defmethod project-rescan ((this project-am-man))
722 "Rescan object THIS."
723 (oset this :source (makefile-macro-file-list (project-am-macro this))))
724
725 (defmethod project-rescan ((this project-am-lisp))
726 "Rescan the lisp sources."
727 (oset this :source (makefile-macro-file-list (project-am-macro this))))
728
729 (defmethod project-rescan ((this project-am-header))
730 "Rescan the Header sources for object THIS."
731 (oset this :source (makefile-macro-file-list (project-am-macro this))))
732
733 (defmethod project-rescan ((this project-am-built-src))
734 "Rescan built sources for object THIS."
735 (oset this :source (makefile-macro-file-list "BUILT_SOURCES")))
736
737 (defmethod project-rescan ((this project-am-extra-dist))
738 "Rescan object THIS."
739 (oset this :source (makefile-macro-file-list "EXTRA_DIST")))
740
741 (defmethod project-am-macro ((this project-am-objectcode))
742 "Return the default macro to 'edit' for this object type."
743 (concat (subst-char-in-string ?- ?_ (oref this :name)) "_SOURCES"))
744
745 (defmethod project-am-macro ((this project-am-header-noinst))
746 "Return the default macro to 'edit' for this object."
747 "noinst_HEADERS")
748
749 (defmethod project-am-macro ((this project-am-header-inst))
750 "Return the default macro to 'edit' for this object."
751 "include_HEADERS")
752
753 (defmethod project-am-macro ((this project-am-header-pkg))
754 "Return the default macro to 'edit' for this object."
755 "pkginclude_HEADERS")
756
757 (defmethod project-am-macro ((this project-am-header-chk))
758 "Return the default macro to 'edit' for this object."
759 "check_HEADERS")
760
761 (defmethod project-am-macro ((this project-am-texinfo))
762 "Return the default macro to 'edit' for this object type."
763 (concat (file-name-sans-extension (oref this :name)) "_TEXINFOS"))
764
765 (defmethod project-am-macro ((this project-am-man))
766 "Return the default macro to 'edit' for this object type."
767 (oref this :name))
768
769 (defmethod project-am-macro ((this project-am-lisp))
770 "Return the default macro to 'edit' for this object."
771 "lisp_LISP")
772
773 (defun project-am-buffer-object (amf buffer)
774 "Return an object starting with AMF associated with BUFFER.
775 nil means that this buffer belongs to no-one."
776 (if (not amf)
777 nil
778 (if (ede-buffer-mine amf buffer)
779 amf
780 (let ((targ (oref amf targets))
781 (sobj (oref amf subproj))
782 (obj nil))
783 (while (and targ (not obj))
784 (if (ede-buffer-mine (car targ) buffer)
785 (setq obj (car targ)))
786 (setq targ (cdr targ)))
787 (while (and sobj (not obj))
788 (setq obj (project-am-buffer-object (car sobj) buffer)
789 sobj (cdr sobj)))
790 obj))))
791
792 (defmethod ede-buffer-mine ((this project-am-makefile) buffer)
793 "Return t if object THIS lays claim to the file in BUFFER."
794 (let ((efn (expand-file-name (buffer-file-name buffer))))
795 (or (string= (oref this :file) efn)
796 (string-match "/configure\\.ac$" efn)
797 (string-match "/configure\\.in$" efn)
798 (string-match "/configure$" efn)
799 ;; Search output files.
800 (let ((ans nil))
801 (dolist (f (oref this configureoutputfiles))
802 (when (string-match (concat (regexp-quote f) "$") efn)
803 (setq ans t)))
804 ans)
805 )))
806
807 (defmethod ede-buffer-mine ((this project-am-objectcode) buffer)
808 "Return t if object THIS lays claim to the file in BUFFER."
809 (member (file-relative-name (buffer-file-name buffer) (oref this :path))
810 (oref this :source)))
811
812 (defmethod ede-buffer-mine ((this project-am-texinfo) buffer)
813 "Return t if object THIS lays claim to the file in BUFFER."
814 (let ((bfn (file-relative-name (buffer-file-name buffer)
815 (oref this :path))))
816 (or (string= (oref this :name) bfn)
817 (member bfn (oref this :include)))))
818
819 (defmethod ede-buffer-mine ((this project-am-man) buffer)
820 "Return t if object THIS lays claim to the file in BUFFER."
821 (string= (oref this :name)
822 (file-relative-name (buffer-file-name buffer) (oref this :path))))
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-relative-name (buffer-file-name buffer) (oref this :path))
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.
854 Argument 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.
886 Documentation is not for object THIS, but is provided by THIS for other
887 files 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.
919 Run FORMS in the configure file.
920 Kill 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 (project-am-with-config-current conf-in
958 (let ((aci (autoconf-parameters-for-macro "AC_INIT"))
959 (aia (autoconf-parameters-for-macro "AM_INIT_AUTOMAKE"))
960 (acf (autoconf-parameters-for-macro "AC_CONFIG_FILES"))
961 (aco (autoconf-parameters-for-macro "AC_OUTPUT"))
962 )
963 (cond
964 ;; AC init has more than 1 parameter
965 ((> (length aci) 1)
966 (setq name (nth 0 aci)
967 ver (nth 1 aci)
968 bugrep (nth 2 aci)))
969 ;; The init automake has more than 1 parameter
970 ((> (length aia) 1)
971 (setq name (nth 0 aia)
972 ver (nth 1 aia)
973 bugrep (nth 2 aia)))
974 )
975 ;; AC_CONFIG_FILES, or AC_OUTPUT lists everything that
976 ;; should be detected as part of this PROJECT, but not in a
977 ;; particular TARGET.
978 (let ((outfiles (cond (aco (list (car aco)))
979 (t acf))))
980 (if (> (length outfiles) 1)
981 (setq configfiles outfiles)
982 (setq configfiles (split-string (car outfiles) "\\s-" t)))
983 )
984 ))
985 )
986 ;; Else, try the script
987 ((file-exists-p conf-sh)
988 (project-am-with-config-current conf-sh
989 (setq name (project-am-extract-shell-variable "PACKAGE_NAME")
990 ver (project-am-extract-shell-variable "PACKAGE_VERSION")
991 )
992 ))
993 ;; Don't know what else....
994 (t
995 nil))
996 ;; Return stuff
997 (list name ver bugrep configfiles)
998 ))
999
1000 (defun project-am-package-info (dir)
1001 "Get the package information for directory topmost project dir over DIR.
1002 Calculates the info with `project-am-extract-package-info'."
1003 (let ((top (ede-toplevel)))
1004 (when top (setq dir (oref top :directory)))
1005 (project-am-extract-package-info dir)))
1006
1007 ;; for simple per project include path extension
1008 (defmethod ede-system-include-path ((this project-am-makefile))
1009 "Return `project-am-localvars-include-path', usually local variable
1010 per file or in .dir-locals.el or similar."
1011 (bound-and-true-p project-am-localvars-include-path))
1012
1013 (defmethod ede-system-include-path ((this project-am-target))
1014 "Return `project-am-localvars-include-path', usually local variable
1015 per file or in .dir-locals.el or similar."
1016 (bound-and-true-p project-am-localvars-include-path))
1017
1018
1019 (provide 'ede/project-am)
1020
1021 ;; arch-tag: 528db935-f186-4240-b647-e305c5b784a2
1022 ;;; ede/project-am.el ends here