summaryrefslogtreecommitdiff
path: root/lisp/cedet/ede/project-am.el
blob: 715f3cf46d514c5ccebfc0a7e27e89f40e90aa3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
;;; project-am.el --- A project management scheme based on automake files.

;; Copyright (C) 1998-2000, 2003, 2005, 2007-2016 Free Software
;; Foundation, Inc.

;; Author: Eric M. Ludlam <zappo@gnu.org>
;; Old-Version: 0.0.3
;; Keywords: project, make

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; The GNU Automake tool is the first step towards having a really
;; good project management system.  It provides a simple and concise
;; look at what is actually in a project, and records it in a simple
;; fashion.
;;
;; project-am uses the structure defined in all good GNU projects with
;; the Automake file as its base template, and then maintains that
;; information during edits, automatically updating the automake file
;; where appropriate.

(require 'make-mode)
(require 'ede)
(require 'ede/make)
(require 'ede/makefile-edit)
(require 'semantic/find) ;; for semantic-find-tags-by-...
(require 'ede/autoconf-edit)

(declare-function autoconf-parameters-for-macro "ede/autoconf-edit")
(declare-function ede-shell-run-something "ede/shell")
(eval-when-compile (require 'compile))

;;; Code:
(defgroup project-am nil
  "File and tag browser frame."
  :group 'tools
  :group 'ede
  )

(defcustom project-am-compile-project-command nil
  "*Default command used to compile a project."
  :group 'project-am
  :type '(choice (const nil) string))

(defcustom project-am-compile-target-command (concat ede-make-command " -k %s")
  "*Default command used to compile a project."
  :group 'project-am
  :type 'string)

(defcustom project-am-debug-target-function 'gdb
  "*Default Emacs command used to debug a target."
  :group 'project-am
  :type 'function) ; make this be a list some day

(defconst project-am-type-alist
  '(("bin" project-am-program "bin_PROGRAMS" t)
    ("sbin" project-am-program "sbin_PROGRAMS" t)
    ("noinstbin" project-am-program "noinst_PROGRAMS" t)
    ("checkbin" project-am-program "check_PROGRAMS" t)
    ("lib" project-am-lib "lib_LIBS" t)
    ("libraries" project-am-lib "lib_LIBRARIES" t)
    ("librariesnoinst" project-am-lib "noinst_LIBRARIES" t)
    ("pkglibraries" project-am-lib "pkglib_LIBRARIES" t)
    ("checklibs" project-am-lib "check_LIBRARIES" t)
    ("ltlibraries" project-am-lib "lib_LTLIBRARIES" t)
    ("ltlibrariesnoinst" project-am-lib "noinst_LTLIBRARIES" t)
    ("pkgltlibraries" project-am-lib "pkglib_LTLIBRARIES" t)
    ("checkltlibs" project-am-lib "check_LTLIBRARIES" t)
    ("headernoinst" project-am-header-noinst "noinst_HEADERS")
    ("headerinst" project-am-header-inst "include_HEADERS")
    ("headerpkg" project-am-header-pkg "pkginclude_HEADERS")
    ("headerpkg" project-am-header-chk "check_HEADERS")
    ("texinfo" project-am-texinfo "info_TEXINFOS" t)
    ("man" project-am-man "man_MANS")
    ("lisp" project-am-lisp "lisp_LISP")
    ;; for other global files track EXTRA_
    ("extrabin" project-am-program "EXTRA_PROGRAMS" t)
    ("builtsrcs" project-am-built-src "BUILT_SOURCES")
    ("extradist" project-am-extra-dist "EXTRA_DIST")
    ;; Custom libraries targets?
    ;; ("ltlibcustom" project-am-lib ".*?_LTLIBRARIES" t)
    )
  "Alist of type names and the type of object to create for them.
Each entry is of the form:
  (EMACSNAME CLASS AUTOMAKEVAR INDIRECT)
where EMACSNAME is a name for Emacs to use.
CLASS is the EDE target class to represent the target.
AUTOMAKEVAR is the Automake variable to identify.  This cannot be a
   regular expression.
INDIRECT is optional.  If it is non-nil, then the variable in
question lists other variables that need to be looked up.")


(defconst project-am-meta-type-alist
  '((project-am-program "_PROGRAMS$" t)
    (project-am-lib "_\\(LIBS\\|LIBRARIES\\|LTLIBRARIES\\)$" t)

    ;; direct primary target use a dummy object (man target)
    ;; update to: * 3.3 Uniform  in automake-1.11 info node.
    (project-am-man "_\\(DATA\\|HEADERS\\|PYTHON\\|JAVA\\|SCRIPTS\\|MANS\\|TEXINFOS\\)$" nil)
    )
  "Alist of meta-target type, each entry has form:
     (CLASS REGEXPVAR INDIRECT)
where CLASS is the EDE target class for target.
REGEXPVAR is the regexp used in `semantic-find-tags-by-name-regexp'.
INDIRECT is optional. If it is non-nil, then the variable in it have
other meta-variable based on this name.")


(defclass project-am-target (ede-target)
  nil
  "Base target class for everything in project-am.")

(defclass project-am-objectcode (project-am-target)
  ((source :initarg :source :documentation "List of source files."))
  "A target which creates object code, like a C program or library.")

(defclass project-am-program (project-am-objectcode)
  ((ldadd :initarg :ldadd :documentation "Additional LD args."
	  :initform nil))
  "A top level program to build")

(defclass project-am-header (project-am-target)
  ()
  "A group of misc source files, such as headers.")

(defclass project-am-header-noinst (project-am-header)
  ()
  "A group of header files that are not installed.")

(defclass project-am-header-inst (project-am-header)
  ()
  "A group of header files that are not installed.")

(defclass project-am-header-pkg (project-am-header)
  ()
  "A group of header files that are not installed.")

(defclass project-am-header-chk (project-am-header)
  ()
  "A group of header files that are not installed.")

(defclass project-am-lib (project-am-objectcode)
  nil
  "A top level library to build")

(defclass project-am-lisp (project-am-target)
  ()
  "A group of Emacs Lisp programs to byte compile.")

(defclass project-am-texinfo (project-am-target)
  ((include :initarg :include
	    :initform nil
	    :documentation "Additional texinfo included in this one."))
  "A top level texinfo file to build.")

(defclass project-am-man (project-am-target)
  nil
  "A top level man file to build.")

;; For generic files tracker like EXTRA_DIST
(defclass project-am-built-src (project-am-target)
  ()
  "A group of Emacs Lisp programs to byte compile.")

(defclass project-am-extra-dist (project-am-target)
  ()
  "A group of Emacs Lisp programs to byte compile.")

(defclass project-am-makefile (ede-project)
  ((targets :initarg :targets
	    :initform nil
	    :documentation "Top level targets in this makefile.")
   (configureoutputfiles
    :initform nil
    :documentation
    "List of files output from configure system.")
   )
  "Encode one makefile.")

;;; Code:
(cl-defmethod project-add-file ((ot project-am-target))
  "Add the current buffer into a project.
OT is the object target.  DIR is the directory to start in."
  (let* ((target (if ede-object (error "Already associated w/ a target")
		   (let ((amf (project-am-load default-directory)))
		     (if (not amf) (error "No project file"))
		     (completing-read "Target: "
				      (object-assoc-list 'name
							 (oref amf targets))
				      nil t))))
	 ;; The input target might be new.  See if we can find it.
	 (amf (ede-target-parent ot))
	 (ot (object-assoc target 'name (oref amf targets)))
	 (ofn (file-name-nondirectory (buffer-file-name))))
    (if (not ot)
	(setq ot
	      (project-new-target
	       target (project-am-preferred-target-type (buffer-file-name)))))
    (ede-with-projectfile ot
      (makefile-move-to-macro (project-am-macro ot))
      (makefile-end-of-command)
      (insert " " ofn)
      (makefile-fill-paragraph nil)
      (project-rescan ot)
      (save-buffer))
    (setq ede-object ot)))

(cl-defmethod project-remove-file ((ot project-am-target) fnnd)
  "Remove the current buffer from any project targets."
  (ede-with-projectfile ot
    (makefile-move-to-macro (project-am-macro ot))
    (makefile-navigate-macro (concat " *" (regexp-quote (ede-name fnnd))))
    (replace-match "" t t nil 0)
    (makefile-fill-paragraph nil)
    (project-rescan ot)
    (save-buffer))
  (setq ede-object nil))

(cl-defmethod project-edit-file-target ((obj project-am-target))
  "Edit the target associated w/ this file."
  (find-file (concat (oref obj path) "Makefile.am"))
  (goto-char (point-min))
  (makefile-move-to-macro (project-am-macro obj))
  (if (= (point-min) (point))
      (re-search-forward (ede-target-name obj))))

(cl-defmethod project-new-target ((proj project-am-makefile)
			       &optional name type)
  "Create a new target named NAME.
Argument TYPE is the type of target to insert.  This is a string
matching something in `project-am-type-alist' or type class symbol.
Despite the fact that this is a method, it depends on the current
buffer being in order to provide a smart default target type."
  (let* ((name (or name (read-string "Name: " "")))
	 (type (or type
		   (completing-read "Type: "
				    project-am-type-alist
				    nil t
				    (cond ((eq major-mode 'texinfo-mode)
					   "texinfo")
					  ((eq major-mode 'nroff-mode)
					   "man")
					  ((eq major-mode 'emacs-lisp-mode)
					   "lisp")
					  (t "bin")))))
	 (ntype (assoc type project-am-type-alist))
	 (ot nil))
    (setq ot (apply (car (cdr ntype)) name :name name
		    :path (expand-file-name default-directory) nil))
    (if (not ot) (error "Error creating target object %S" ntype))
    (ede-with-projectfile ot
      (goto-char (point-min))
      (makefile-next-dependency)
      (if (= (point) (point-min))
	  (goto-char (point-max))
	(beginning-of-line)
	(insert "\n")
	(forward-char -1))
      ;; Add the new target sources macro (if needed)
      (if (project-am-macro ot)
	  (makefile-insert-macro (project-am-macro ot)))
      ;; Add to the list of objects.
      (goto-char (point-min))
      (makefile-move-to-macro (car (cdr (cdr ntype))))
      (if (= (point) (point-min))
	  (progn
	    (if (re-search-forward makefile-macroassign-regex nil t)
		(progn (forward-line -1)
		       (end-of-line)
		       (insert "\n"))
	      ;; If the above search fails, that's ok.  We'd just want to be at
	      ;; point-min anyway.
	      )
	    (makefile-insert-macro (car (cdr (cdr ntype))))))
      (makefile-end-of-command)
      (insert " " (ede-target-name ot))
      (save-buffer)
      ;; Rescan the object in this makefile.
      (project-rescan ede-object))))

;;
;; NOTE TO SELF
;;
;;  This should be handled at the EDE level, calling a method of the
;; top most project.
;;
(cl-defmethod project-compile-project ((obj project-am-target) &optional command)
  "Compile the entire current project.
Argument COMMAND is the command to use when compiling."
  (require 'compile)
  (if (not command)
      (setq
       command
       ;; This interactive statement was taken from compile, and I'll
       ;; use the same command history too.
       (progn
	 (if (not project-am-compile-project-command)
	     (setq project-am-compile-project-command compile-command))
	 (if (or compilation-read-command current-prefix-arg)
	     (read-from-minibuffer "Project compile command: "
				   ;; hardcode make -k
				   ;; This is compile project after all.
				   project-am-compile-project-command
				   nil nil '(compile-history . 1))
	   project-am-compile-project-command))))
  ;; When compile a project, we might be in a subdirectory,
  ;; so we have to make sure we move all the way to the top.
  (let* ((default-directory (project-am-find-topmost-level default-directory)))
    (compile command)))

(cl-defmethod project-compile-project ((obj project-am-makefile)
				    &optional command)
  "Compile the entire current project.
Argument COMMAND is the command to use when compiling."
  (require 'compile)
  (if (not command)
      (setq
       command
       ;; This interactive statement was taken from compile, and I'll
       ;; use the same command history too.
       (progn
	 (if (not project-am-compile-project-command)
	     (setq project-am-compile-project-command compile-command))
	 (if (or compilation-read-command current-prefix-arg)
	     (read-from-minibuffer "Project compile command: "
				   ;; hardcode make -k
				   ;; This is compile project after all.
				   project-am-compile-project-command
				   nil nil '(compile-history . 1))
	   project-am-compile-project-command))))
  ;; When compile a project, we might be in a subdirectory,
  ;; so we have to make sure we move all the way to the top.
  (let* ((default-directory (project-am-find-topmost-level default-directory)))
    (compile command)))

(cl-defmethod project-compile-target ((obj project-am-target) &optional command)
  "Compile the current target.
Argument COMMAND is the command to use for compiling the target."
  (require 'compile)
  (if (not project-am-compile-project-command)
      (setq project-am-compile-project-command compile-command))
  (if (not command)
      (setq
       command
       (if compilation-read-command
	   (read-from-minibuffer "Project compile command: "
				 ;; hardcode make -k
				 ;; This is compile project after all.
				 (if ede-object
				     (format
				      project-am-compile-target-command
				      (project-compile-target-command
				       ede-object))
				   project-am-compile-target-command)
				 nil nil
				 '(compile-history . 1))
	 (if ede-object
	     project-am-compile-project-command
	   (format
	    project-am-compile-target-command
	    (project-compile-target-command ede-object))))))
  ;; We better be in the right place when compiling a specific target.
  (compile command))

(cl-defmethod project-debug-target ((obj project-am-objectcode))
  "Run the current project target in a debugger."
  (let ((tb (get-buffer-create " *padt*"))
	(dd (oref obj path))
	(cmd nil))
    (unwind-protect
	(progn
	  (require 'ede/shell)
	  (set-buffer tb)
	  (setq default-directory dd)
	  (setq cmd (read-from-minibuffer
		     "Run (like this): "
		     (concat (symbol-name project-am-debug-target-function)
			     " " (ede-target-name obj))))
	  (funcall project-am-debug-target-function cmd))
      (kill-buffer tb))))

(declare-function ede-shell-run-something "ede/shell")

(cl-defmethod project-run-target ((obj project-am-objectcode))
  "Run the current project target in comint buffer."
  (require 'ede/shell)
  (let ((tb (get-buffer-create " *padt*"))
	(dd (oref obj path))
	(cmd nil))
    (unwind-protect
	(progn
	  (set-buffer tb)
	  (setq default-directory dd)
	  (setq cmd (read-from-minibuffer
		     "Run (like this): "
		     (concat "./" (ede-target-name obj))))
	  (ede-shell-run-something obj cmd))
      (kill-buffer tb))))

(cl-defmethod project-make-dist ((this project-am-target))
  "Run the current project in the debugger."
  (require 'compile)
  (if (not project-am-compile-project-command)
      (setq project-am-compile-project-command compile-command))
  (project-compile-project this (concat project-am-compile-project-command
					" dist")))

;;; Project loading and saving
;;
(defun project-am-load (directory &optional rootproj)
  "Read an automakefile DIRECTORY into our data structure.
If a given set of projects has already been loaded, then do nothing
but return the project for the directory given.
Optional ROOTPROJ is the root EDE project."
  ;; Just jump into creating the project from the Makefiles.
  (project-am-load-makefile directory))

(defun project-am-find-topmost-level (dir)
  "Find the topmost automakefile starting with DIR."
  (let ((newdir dir))
    (while (or (file-exists-p (concat newdir "Makefile.am"))
	       (file-exists-p (concat newdir "configure.ac"))
	       (file-exists-p (concat newdir "configure.in"))
	       )
      (setq dir newdir newdir
	    (file-name-directory (directory-file-name newdir))))
    (expand-file-name dir)))

(defmacro project-am-with-makefile-current (dir &rest forms)
  "Set the Makefile.am in DIR to be the current buffer.
Run FORMS while the makefile is current.
Kill the makefile if it was not loaded before the load."
  `(let* ((fn (expand-file-name "Makefile.am" ,dir))
	  (fb nil)
	  (kb (get-file-buffer fn)))
     (if (not (file-exists-p fn))
	 nil
       (save-excursion
	 (if kb (setq fb kb)
	   ;; We need to find-file this thing, but don't use
	   ;; any semantic features.
	   (let ((semantic-init-hook nil)
		 (recentf-exclude '( (lambda (f) t) ))
		 )
	     (setq fb (find-file-noselect fn)))
	   )
	 (set-buffer fb)
	 (prog1 ,@forms
	   (if (not kb) (kill-buffer (current-buffer))))))))
(put 'project-am-with-makefile-current 'lisp-indent-function 1)

(add-hook 'edebug-setup-hook
	  (lambda ()
	    (def-edebug-spec project-am-with-makefile-current
	      (form def-body))))


(defun project-am-load-makefile (path &optional suggestedname)
  "Convert PATH into a project Makefile, and return its project object.
It does not check for existing project objects.  Use `project-am-load'.
Optional argument SUGGESTEDNAME will be the project name.
This is used when subprojects are made in named subdirectories."
  (project-am-with-makefile-current path
    (if (and ede-object (project-am-makefile-p ede-object))
	ede-object
      (let* ((pi (project-am-package-info path))
	     (sfn (when suggestedname
		    (project-am-last-dir suggestedname)))
	     (pn (or sfn (nth 0 pi) (project-am-last-dir fn)))
	     (ver (or (nth 1 pi) "0.0"))
	     (bug (nth 2 pi))
	     (cof (nth 3 pi))
	     (ampf (project-am-makefile
		    pn :name pn
		    :version ver
		    :mailinglist (or bug "")
		    :file fn)))
	(oset ampf :directory (file-name-directory fn))
	(oset ampf configureoutputfiles cof)
	(make-local-variable 'ede-object)
	(setq ede-object ampf)
	;; Move the rescan after we set ede-object to prevent recursion
	(project-rescan ampf)
	ampf))))

;;; Methods:
(cl-defmethod project-targets-for-file ((proj project-am-makefile))
  "Return a list of targets the project PROJ."
  (oref proj targets))

(defun project-am-scan-for-targets (currproj dir)
  "Scan the current Makefile.am for targets.
CURRPROJ is the current project being scanned.
DIR is the directory to apply to new targets."
  (let* ((otargets (oref currproj targets))
	 ;; `ntargets' results in complete targets list
	 ;; not only the new targets by diffing.
	 (ntargets nil)
	 (tmp nil)
	 )

    (mapc
     ;; Map all the different types
     (lambda (typecar)
       (let ((macro (nth 2 typecar))
	     (class (nth 1 typecar))
	     (indirect (nth 3 typecar))
	     )
	 (if indirect
	     ;; Map all the found objects
	     (mapc (lambda (lstcar)
		     (setq tmp (object-assoc lstcar 'name otargets))
		     (when (not tmp)
		       (setq tmp (apply class lstcar :name lstcar
					:path dir nil)))
		     (project-rescan tmp)
		     (setq ntargets (cons tmp ntargets)))
		   (makefile-macro-file-list macro))
	   ;; Non-indirect will have a target whos sources
	   ;; are actual files, not names of other targets.
	   (let ((files (makefile-macro-file-list macro)))
	     (when files
	       (setq tmp (object-assoc macro 'name otargets))
	       (when (not tmp)
		 (setq tmp (apply class macro :name macro
				  :path dir nil)))
	       (project-rescan tmp)
	       (setq ntargets (cons tmp ntargets))
	       ))
	   )
	 ))
     project-am-type-alist)

    ;; At now check variables for meta-target regexp
    ;; We have to check ntargets to avoid useless rescan.
    ;; Also we have check otargets to prevent duplication.
    (mapc
     (lambda (typecar)
       (let ((class (nth 0 typecar))
	     (metaregex (nth 1 typecar))
	     (indirect (nth 2 typecar)))
	 (if indirect
	     ;; Map all the found objects
	     (mapc
	      (lambda (lstcar)
		(unless (object-assoc lstcar 'name ntargets)
		  (or
		   (setq tmp (object-assoc lstcar 'name otargets))
		   (setq tmp (apply class lstcar :name lstcar
				    :path dir nil)))
		  (project-rescan tmp)
		  (setq ntargets (cons tmp ntargets))))
	      ;; build a target list to map over
	      (let (atargets)
		(dolist (TAG
			 (semantic-find-tags-by-name-regexp
			  metaregex (semantic-find-tags-by-class
				     'variable (semantic-fetch-tags))))
		  ;; default-value have to be a list
		  (when (cadr (assoc ':default-value TAG))
		    (setq atargets
			  (append
			   (nreverse (cadr (assoc ':default-value TAG)))
			   atargets))))
		(nreverse atargets)))

	   ;; else not indirect, TODO: FIX various direct meta type in a sane way.
	   (dolist (T (semantic-find-tags-by-name-regexp
		       metaregex (semantic-find-tags-by-class
				  'variable (semantic-fetch-tags))))
	     (unless (setq tmp (object-assoc (car T) 'name ntargets))
	       (or (setq tmp (object-assoc (car T) 'name otargets))
		   ;; we are really new
		   (setq tmp (apply class (car T) :name (car T)
				    :path dir nil)))
	       (project-rescan tmp)
	       (setq ntargets (cons tmp ntargets))))
	   )))
     project-am-meta-type-alist)
    ntargets))

(defun project-am-expand-subdirlist (place subdirs)
  "Store in PLACE the SUBDIRS expanded from variables.
Strip out duplicates, and recurse on variables."
  (mapc (lambda (sp)
	  (let ((var (makefile-extract-varname-from-text sp)))
	    (if var
		;; If it is a variable, expand that variable, and keep going.
		(project-am-expand-subdirlist
		 place (makefile-macro-file-list var))
	      ;; Else, add SP in if it isn't a dup.
	      (if (member sp (symbol-value place))
		  nil ; don't do it twice.
		(set place (cons sp (symbol-value place))) ;; add
		))))
	subdirs)
  )

(cl-defmethod project-rescan ((this project-am-makefile) &optional suggestedname)
  "Rescan the makefile for all targets and sub targets."
  (project-am-with-makefile-current (file-name-directory (oref this file))
    ;;(message "Scanning %s..." (oref this file))
    (let* ((pi (project-am-package-info (oref this directory)))
	   (pn (nth 0 pi))
	   (pv (nth 1 pi))
	   (bug (nth 2 pi))
	   (cof (nth 3 pi))
	   (osubproj (oref this subproj))
	   ;; 1/30/10 - We need to append these two lists together,
	   ;; then strip out duplicates.  Expanding this list (via
	   ;; references to other variables should also strip out dups
	   (csubproj (append
		      (makefile-macro-file-list "DIST_SUBDIRS")
		      (makefile-macro-file-list "SUBDIRS")))
	   (csubprojexpanded nil)
	   (nsubproj nil)
	   ;; Targets are excluded here because they require
	   ;; special attention.
	   (dir (expand-file-name default-directory))
	   (tmp nil)
	   (ntargets (project-am-scan-for-targets this dir))
	   )
      (if suggestedname
	  (oset this name (project-am-last-dir suggestedname))
	;; Else, setup toplevel project info.
	(and pn (string= (directory-file-name
			  (oref this directory))
			 (directory-file-name
			  (project-am-find-topmost-level
			   (oref this directory))))
	     (oset this name pn)
	     (and pv (oset this version pv))
	     (and bug (oset this mailinglist bug))
	     (oset this configureoutputfiles cof)))
      ;; Now that we have this new list, chuck the old targets
      ;; and replace it with the new list of targets I just created.
      (oset this targets (nreverse ntargets))
      ;; We still have a list of targets.  For all buffers, make sure
      ;; their object still exists!
      ;; FIGURE THIS OUT
      (project-am-expand-subdirlist 'csubprojexpanded csubproj)
      ;; Ok, now let's look at all our sub-projects.
      (mapc (lambda (sp)
	      (let* ((subdir (file-name-as-directory
			      (expand-file-name
			       sp (file-name-directory (oref this :file)))))
		     (submake (expand-file-name
			       "Makefile.am"
			       subdir)))
		(if (string= submake (oref this :file))
		    nil	;; don't recurse.. please!
		  ;; For each project id found, see if we need to recycle,
		  ;; and if we do not, then make a new one.  Check the deep
		  ;; rescan value for behavior patterns.
		  (setq tmp (object-assoc
			     submake
			     'file osubproj))
		  (if (not tmp)
		      (setq tmp
			    (condition-case nil
				;; In case of problem, ignore it.
				(project-am-load-makefile subdir subdir)
			      (error nil)))
		    ;; If we have tmp, then rescan it only if deep mode.
		    (if ede-deep-rescan
			(project-rescan tmp subdir)))
		  ;; Tac tmp onto our list of things to keep, but only
		  ;; if tmp was found.
		  (when tmp
		    ;;(message "Adding %S" (object-print tmp))
		    (setq nsubproj (cons tmp nsubproj)))))
	      )
	    (nreverse csubprojexpanded))
      (oset this subproj nsubproj)
      ;; All elements should be updated now.
      )))


(cl-defmethod project-rescan ((this project-am-program))
  "Rescan object THIS."
  (oset this :source (makefile-macro-file-list (project-am-macro this)))
  (unless (oref this :source)
    (oset this :source (list (concat (oref this :name) ".c"))))
  (oset this :ldadd (makefile-macro-file-list
		     (concat (oref this :name) "_LDADD"))))

(cl-defmethod project-rescan ((this project-am-lib))
  "Rescan object THIS."
  (oset this :source (makefile-macro-file-list (project-am-macro this)))
  (unless (oref this :source)
    (oset this :source (list (concat (file-name-sans-extension (oref this :name)) ".c")))))

(cl-defmethod project-rescan ((this project-am-texinfo))
  "Rescan object THIS."
  (oset this :include (makefile-macro-file-list (project-am-macro this))))

(cl-defmethod project-rescan ((this project-am-man))
  "Rescan object THIS."
  (oset this :source (makefile-macro-file-list (project-am-macro this))))

(cl-defmethod project-rescan ((this project-am-lisp))
  "Rescan the lisp sources."
  (oset this :source (makefile-macro-file-list (project-am-macro this))))

(cl-defmethod project-rescan ((this project-am-header))
  "Rescan the Header sources for object THIS."
  (oset this :source (makefile-macro-file-list (project-am-macro this))))

(cl-defmethod project-rescan ((this project-am-built-src))
  "Rescan built sources for object THIS."
  (oset this :source (makefile-macro-file-list "BUILT_SOURCES")))

(cl-defmethod project-rescan ((this project-am-extra-dist))
  "Rescan object THIS."
  (oset this :source (makefile-macro-file-list "EXTRA_DIST")))

(cl-defmethod project-am-macro ((this project-am-objectcode))
  "Return the default macro to `edit' for this object type."
  (concat (subst-char-in-string ?- ?_ (oref this :name)) "_SOURCES"))

(cl-defmethod project-am-macro ((this project-am-header-noinst))
  "Return the default macro to `edit' for this object."
  "noinst_HEADERS")

(cl-defmethod project-am-macro ((this project-am-header-inst))
  "Return the default macro to `edit' for this object."
  "include_HEADERS")

(cl-defmethod project-am-macro ((this project-am-header-pkg))
  "Return the default macro to `edit' for this object."
  "pkginclude_HEADERS")

(cl-defmethod project-am-macro ((this project-am-header-chk))
  "Return the default macro to `edit' for this object."
  "check_HEADERS")

(cl-defmethod project-am-macro ((this project-am-texinfo))
  "Return the default macro to `edit' for this object type."
  (concat (file-name-sans-extension (oref this :name)) "_TEXINFOS"))

(cl-defmethod project-am-macro ((this project-am-man))
  "Return the default macro to `edit' for this object type."
  (oref this :name))

(cl-defmethod project-am-macro ((this project-am-lisp))
  "Return the default macro to `edit' for this object."
  "lisp_LISP")

(defun project-am-buffer-object (amf buffer)
  "Return an object starting with AMF associated with BUFFER.
nil means that this buffer belongs to no-one."
  (if (not amf)
      nil
    (if (ede-buffer-mine amf buffer)
	amf
      (let ((targ (oref amf targets))
	    (sobj (oref amf subproj))
	    (obj nil))
	(while (and targ (not obj))
	  (if (ede-buffer-mine (car targ) buffer)
	      (setq obj (car targ)))
	  (setq targ (cdr targ)))
	(while (and sobj (not obj))
	  (setq obj (project-am-buffer-object (car sobj) buffer)
		sobj (cdr sobj)))
	obj))))

(cl-defmethod ede-buffer-mine ((this project-am-makefile) buffer)
  "Return t if object THIS lays claim to the file in BUFFER."
  (let ((efn  (expand-file-name (buffer-file-name buffer))))
    (or (string= (oref this :file) efn)
	(string-match "/configure\\.ac$" efn)
	(string-match "/configure\\.in$" efn)
	(string-match "/configure$" efn)
	;; Search output files.
	(let ((ans nil))
	  (dolist (f (oref this configureoutputfiles))
	    (when (string-match (concat (regexp-quote f) "$") efn)
	      (setq ans t)))
	  ans)
	)))

(cl-defmethod ede-buffer-mine ((this project-am-objectcode) buffer)
  "Return t if object THIS lays claim to the file in BUFFER."
  (member (file-relative-name (buffer-file-name buffer) (oref this :path))
	  (oref this :source)))

(cl-defmethod ede-buffer-mine ((this project-am-texinfo) buffer)
  "Return t if object THIS lays claim to the file in BUFFER."
  (let ((bfn (file-relative-name (buffer-file-name buffer)
				 (oref this :path))))
    (or (string= (oref this :name)  bfn)
	(member bfn (oref this :include)))))

(cl-defmethod ede-buffer-mine ((this project-am-man) buffer)
  "Return t if object THIS lays claim to the file in BUFFER."
  (string= (oref this :name)
	   (file-relative-name (buffer-file-name buffer) (oref this :path))))

(cl-defmethod ede-buffer-mine ((this project-am-lisp) buffer)
  "Return t if object THIS lays claim to the file in BUFFER."
  (member (file-relative-name (buffer-file-name buffer) (oref this :path))
	  (oref this :source)))

(cl-defmethod project-am-subtree ((ampf project-am-makefile) subdir)
  "Return the sub project in AMPF specified by SUBDIR."
  (object-assoc (expand-file-name subdir) 'file (oref ampf subproj)))

(cl-defmethod project-compile-target-command ((this project-am-target))
  "Default target to use when compiling a given target."
  ;; This is a pretty good default for most.
  "")

(cl-defmethod project-compile-target-command ((this project-am-objectcode))
  "Default target to use when compiling an object code target."
  (oref this :name))

(cl-defmethod project-compile-target-command ((this project-am-texinfo))
  "Default target t- use when compiling a texinfo file."
  (let ((n (oref this :name)))
    (if (string-match "\\.texi?\\(nfo\\)?" n)
	(setq n (replace-match ".info" t t n)))
    n))


;;; Generic useful functions

(defun project-am-last-dir (file)
  "Return the last part of a directory name.
Argument FILE is the file to extract the end directory name from."
  (let* ((s (file-name-directory file))
	 (d (directory-file-name s))
	 )
    (file-name-nondirectory d))
  )

(defun project-am-preferred-target-type (file)
  "For FILE, return the preferred type for that file."
  (cond ((string-match "\\.texi?\\(nfo\\)$" file)
	 'project-am-texinfo)
	((string-match "\\.[0-9]$" file)
	 'project-am-man)
	((string-match "\\.el$" file)
	 'project-am-lisp)
	(t
	 'project-am-program)))

(cl-defmethod ede-buffer-header-file((this project-am-objectcode) buffer)
  "There are no default header files."
  (or (cl-call-next-method)
      (let ((s (oref this source))
	    (found nil))
	(while (and s (not found))
	  ;; Add more logic here if applicable.
	  (if (string-match "\\.\\(h\\|H\\|hh\\|hpp\\)" (car s))
	      (setq found (car s)))
	  (setq s (cdr s)))
	found)))

(cl-defmethod ede-documentation ((this project-am-texinfo))
  "Return a list of files that provides documentation.
Documentation is not for object THIS, but is provided by THIS for other
files in the project."
  (let* ((src (append (oref this source)
		      (oref this include)))
	 (proj (ede-target-parent this))
	 (dir (oref proj directory))
	 (out nil))
    ;; Loop over all entries and expand
    (while src
      (setq out (cons
		 (expand-file-name (car src) dir)
		 out))
      (setq src (cdr src)))
    ;; return it
    out))


;;; Configure.ac queries.
;;
(defvar project-am-autoconf-file-options
  '("configure.ac" "configure.in")
  "List of possible configure files to look in for project info.")

(defun project-am-autoconf-file (dir)
  "Return the name of the autoconf file to use in DIR."
  (let ((ans nil))
    (dolist (L project-am-autoconf-file-options)
      (when (file-exists-p (expand-file-name L dir))
	(setq ans (expand-file-name L dir))))
    ans))

(defmacro project-am-with-config-current (file &rest forms)
  "Set the Configure FILE in the top most directory above DIR as current.
Run FORMS in the configure file.
Kill the Configure buffer if it was not already in a buffer."
  `(save-excursion
     (let ((fb (generate-new-buffer ,file)))
       (set-buffer fb)
       (erase-buffer)
       (insert-file-contents ,file)
       (prog1 ,@forms
	 (kill-buffer fb)))))

(put 'project-am-with-config-current 'lisp-indent-function 1)

(add-hook 'edebug-setup-hook
	  (lambda ()
	    (def-edebug-spec project-am-with-config-current
	      (form def-body))))

(defmacro project-am-extract-shell-variable (var)
  "Extract the value of the shell variable VAR from a shell script."
  (save-excursion
    (goto-char (point-min))
    (when (re-search-forward (concat "^" (regexp-quote var) "\\s-*=\\s-*")
			     nil t)
      (buffer-substring-no-properties (point) (point-at-eol)))))

(defun project-am-extract-package-info (dir)
  "Extract the package information for directory DIR."
  (let ((conf-in (project-am-autoconf-file dir))
	(conf-sh (expand-file-name "configure" dir))
	(name (file-name-nondirectory
	       (directory-file-name dir)))
	(ver "1.0")
	(bugrep nil)
	(configfiles nil)
	)
    (cond
     ;; Try configure.ac or configure.in
     (conf-in
      (project-am-with-config-current conf-in
	(let ((aci (autoconf-parameters-for-macro "AC_INIT"))
	      (aia (autoconf-parameters-for-macro "AM_INIT_AUTOMAKE"))
	      (acf (autoconf-parameters-for-macro "AC_CONFIG_FILES"))
	      (aco (autoconf-parameters-for-macro "AC_OUTPUT"))
	      )
	  (cond
	   ;; AC init has more than 1 parameter
	   ((> (length aci) 1)
	    (setq name (nth 0 aci)
		  ver (nth 1 aci)
		  bugrep (nth 2 aci)))
	   ;; The init automake has more than 1 parameter
	   ((> (length aia) 1)
	    (setq name (nth 0 aia)
		  ver (nth 1 aia)
		  bugrep (nth 2 aia)))
	   )
	  ;; AC_CONFIG_FILES, or AC_OUTPUT lists everything that
	  ;; should be detected as part of this PROJECT, but not in a
	  ;; particular TARGET.
	  (let ((outfiles (cond (aco (list (car aco)))
				(t acf))))
	    (if (> (length outfiles) 1)
		(setq configfiles outfiles)
	      (setq configfiles (split-string (car outfiles) "\\s-" t)))
	    )
	  ))
      )
     ;; Else, try the script
     ((file-exists-p conf-sh)
      (project-am-with-config-current conf-sh
	(setq name (project-am-extract-shell-variable "PACKAGE_NAME")
	      ver (project-am-extract-shell-variable "PACKAGE_VERSION")
	      )
	))
     ;; Don't know what else....
     (t
      nil))
    ;; Return stuff
    (list name ver bugrep configfiles)
    ))

(defun project-am-package-info (dir)
  "Get the package information for directory topmost project dir over DIR.
Calculates the info with `project-am-extract-package-info'."
  (let ((top (ede-toplevel)))
    (when top (setq dir (oref top :directory)))
    (project-am-extract-package-info dir)))

;; for simple per project include path extension
(cl-defmethod ede-system-include-path ((this project-am-makefile))
  "Return `project-am-localvars-include-path', usually local variable
per file or in .dir-locals.el or similar."
  (bound-and-true-p project-am-localvars-include-path))

(cl-defmethod ede-system-include-path ((this project-am-target))
  "Return `project-am-localvars-include-path', usually local variable
per file or in .dir-locals.el or similar."
  (bound-and-true-p project-am-localvars-include-path))


(provide 'ede/project-am)

;; Local variables:
;; generated-autoload-load-name: "ede/project-am"
;; End:

;;; ede/project-am.el ends here