autofrisk.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ;;; autofrisk --- Generate module checks for use with auto* tools
  2. ;; Copyright (C) 2002, 2006, 2009, 2011 Free Software Foundation, Inc.
  3. ;;
  4. ;; This program is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public License
  6. ;; as published by the Free Software Foundation; either version 3, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this software; see the file COPYING.LESSER. If
  16. ;; not, write to the Free Software Foundation, Inc., 51 Franklin
  17. ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
  19. ;;; Commentary:
  20. ;; Usage: autofrisk [file]
  21. ;;
  22. ;; This program looks for the file modules.af in the current directory
  23. ;; and writes out modules.af.m4 containing autoconf definitions.
  24. ;; If given, look for FILE instead of modules.af and output to FILE.m4.
  25. ;;
  26. ;; After running autofrisk, you should add to configure.ac the lines:
  27. ;; AUTOFRISK_CHECKS
  28. ;; AUTOFRISK_SUMMARY
  29. ;; Then run "aclocal -I ." to update aclocal.m4, and finally autoconf.
  30. ;;
  31. ;; The modules.af file consists of a series of configuration forms (Scheme
  32. ;; lists), which have one of the following formats:
  33. ;; (files-glob PATTERN ...)
  34. ;; (non-critical-external MODULE ...)
  35. ;; (non-critical-internal MODULE ...)
  36. ;; (programs (MODULE PROG ...) ...)
  37. ;; (pww-varname VARNAME)
  38. ;; PATTERN is a string that may contain "*" and "?" characters to be
  39. ;; expanded into filenames. MODULE is a list of symbols naming a
  40. ;; module, such as `(srfi srfi-1)'. VARNAME is a shell-safe name to use
  41. ;; instead of "probably_wont_work", the default. This var is passed to
  42. ;; `AC_SUBST'. PROG is a string.
  43. ;;
  44. ;; Only the `files-glob' form is required.
  45. ;;
  46. ;; TODO: Write better commentary.
  47. ;; Make "please see README" configurable.
  48. ;;; Code:
  49. (define-module (scripts autofrisk)
  50. :autoload (ice-9 popen) (open-input-pipe)
  51. :use-module (srfi srfi-1)
  52. :use-module (srfi srfi-8)
  53. :use-module (srfi srfi-13)
  54. :use-module (srfi srfi-14)
  55. :use-module (scripts read-scheme-source)
  56. :use-module (scripts frisk)
  57. :export (autofrisk))
  58. (define %include-in-guild-list #f)
  59. (define %summary "Generate snippets for use in configure.ac files.")
  60. (define *recognized-keys* '(files-glob
  61. non-critical-external
  62. non-critical-internal
  63. programs
  64. pww-varname))
  65. (define (canonical-configuration forms)
  66. (let ((chk (lambda (condition . x)
  67. (or condition (apply error "syntax error:" x)))))
  68. (chk (list? forms) "input not a list")
  69. (chk (every list? forms) "non-list element")
  70. (chk (every (lambda (form) (< 1 (length form))) forms) "list too short")
  71. (let ((un #f))
  72. (chk (every (lambda (form)
  73. (let ((key (car form)))
  74. (and (symbol? key)
  75. (or (eq? 'quote key)
  76. (memq key *recognized-keys*)
  77. (begin
  78. (set! un key)
  79. #f)))))
  80. forms)
  81. "unrecognized key:" un))
  82. (let ((bunched (map (lambda (key)
  83. (fold (lambda (form so-far)
  84. (or (and (eq? (car form) key)
  85. (cdr form)
  86. (append so-far (cdr form)))
  87. so-far))
  88. (list key)
  89. forms))
  90. *recognized-keys*)))
  91. (lambda (key)
  92. (assq-ref bunched key)))))
  93. (define (>>strong modules)
  94. (for-each (lambda (module)
  95. (format #t "GUILE_MODULE_REQUIRED~A\n" module))
  96. modules))
  97. (define (safe-name module)
  98. (let ((var (object->string module)))
  99. (string-map! (lambda (c)
  100. (if (char-set-contains? char-set:letter+digit c)
  101. c
  102. #\_))
  103. var)
  104. var))
  105. (define *pww* "probably_wont_work")
  106. (define (>>weak weak-edges)
  107. (for-each (lambda (edge)
  108. (let* ((up (edge-up edge))
  109. (down (edge-down edge))
  110. (var (format #f "have_guile_module~A" (safe-name up))))
  111. (format #t "GUILE_MODULE_AVAILABLE(~A, ~A)\n" var up)
  112. (format #t "test \"$~A\" = no &&\n ~A=\"~A $~A\"~A"
  113. var *pww* down *pww* "\n\n")))
  114. weak-edges))
  115. (define (>>program module progs)
  116. (let ((vars (map (lambda (prog)
  117. (format #f "guile_module~Asupport_~A"
  118. (safe-name module)
  119. prog))
  120. progs)))
  121. (for-each (lambda (var prog)
  122. (format #t "AC_PATH_PROG(~A, ~A)\n" var prog))
  123. vars progs)
  124. (format #t "test \\\n")
  125. (for-each (lambda (var)
  126. (format #t " \"$~A\" = \"\" -o \\\n" var))
  127. vars)
  128. (format #t "~A &&\n~A=\"~A $~A\"\n\n"
  129. (list-ref (list "war = peace"
  130. "freedom = slavery"
  131. "ignorance = strength")
  132. (random 3))
  133. *pww* module *pww*)))
  134. (define (>>programs programs)
  135. (for-each (lambda (form)
  136. (>>program (car form) (cdr form)))
  137. programs))
  138. (define (unglob pattern)
  139. (let ((p (open-input-pipe (format #f "echo '(' ~A ')'" pattern))))
  140. (map symbol->string (read p))))
  141. (define (>>checks forms)
  142. (let* ((cfg (canonical-configuration forms))
  143. (files (apply append (map unglob (cfg 'files-glob))))
  144. (ncx (cfg 'non-critical-external))
  145. (nci (cfg 'non-critical-internal))
  146. (report ((make-frisker) files))
  147. (external (report 'external)))
  148. (let ((pww-varname (cfg 'pww-varname)))
  149. (or (null? pww-varname) (set! *pww* (car pww-varname))))
  150. (receive (weak strong)
  151. (partition (lambda (module)
  152. (or (member module ncx)
  153. (every (lambda (i)
  154. (member i nci))
  155. (map edge-down (mod-down-ls module)))))
  156. external)
  157. (format #t "AC_DEFUN([AUTOFRISK_CHECKS],[\n\n")
  158. (>>strong strong)
  159. (format #t "\n~A=~S\n\n" *pww* "")
  160. (>>weak (fold (lambda (module so-far)
  161. (append so-far (mod-down-ls module)))
  162. (list)
  163. weak))
  164. (>>programs (cfg 'programs))
  165. (format #t "AC_SUBST(~A)\n])\n\n" *pww*))))
  166. (define (>>summary)
  167. (format #t
  168. (symbol->string
  169. '#{
  170. AC_DEFUN([AUTOFRISK_SUMMARY],[
  171. if test ! "$~A" = "" ; then
  172. p=" ***"
  173. echo "$p"
  174. echo "$p NOTE:"
  175. echo "$p The following modules probably won't work:"
  176. echo "$p $~A"
  177. echo "$p They can be installed anyway, and will work if their"
  178. echo "$p dependencies are installed later. Please see README."
  179. echo "$p"
  180. fi
  181. ])
  182. }#)
  183. *pww* *pww*))
  184. (define (autofrisk . args)
  185. (let ((file (if (null? args) "modules.af" (car args))))
  186. (or (file-exists? file)
  187. (error "could not find input file:" file))
  188. (with-output-to-file (format #f "~A.m4" file)
  189. (lambda ()
  190. (>>checks (read-scheme-source-silently file))
  191. (>>summary)))))
  192. (define main autofrisk)
  193. ;; Local variables:
  194. ;; eval: (put 'receive 'scheme-indent-function 2)
  195. ;; End:
  196. ;;; autofrisk ends here