project-am.el 35 KB

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