autofrisk.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;;; autofrisk --- Generate module checks for use with auto* tools
  2. ;; Copyright (C) 2002, 2006, 2009 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 *recognized-keys* '(files-glob
  59. non-critical-external
  60. non-critical-internal
  61. programs
  62. pww-varname))
  63. (define (canonical-configuration forms)
  64. (let ((chk (lambda (condition . x)
  65. (or condition (apply error "syntax error:" x)))))
  66. (chk (list? forms) "input not a list")
  67. (chk (every list? forms) "non-list element")
  68. (chk (every (lambda (form) (< 1 (length form))) forms) "list too short")
  69. (let ((un #f))
  70. (chk (every (lambda (form)
  71. (let ((key (car form)))
  72. (and (symbol? key)
  73. (or (eq? 'quote key)
  74. (memq key *recognized-keys*)
  75. (begin
  76. (set! un key)
  77. #f)))))
  78. forms)
  79. "unrecognized key:" un))
  80. (let ((bunched (map (lambda (key)
  81. (fold (lambda (form so-far)
  82. (or (and (eq? (car form) key)
  83. (cdr form)
  84. (append so-far (cdr form)))
  85. so-far))
  86. (list key)
  87. forms))
  88. *recognized-keys*)))
  89. (lambda (key)
  90. (assq-ref bunched key)))))
  91. (define (>>strong modules)
  92. (for-each (lambda (module)
  93. (format #t "GUILE_MODULE_REQUIRED~A\n" module))
  94. modules))
  95. (define (safe-name module)
  96. (let ((var (object->string module)))
  97. (string-map! (lambda (c)
  98. (if (char-set-contains? char-set:letter+digit c)
  99. c
  100. #\_))
  101. var)
  102. var))
  103. (define *pww* "probably_wont_work")
  104. (define (>>weak weak-edges)
  105. (for-each (lambda (edge)
  106. (let* ((up (edge-up edge))
  107. (down (edge-down edge))
  108. (var (format #f "have_guile_module~A" (safe-name up))))
  109. (format #t "GUILE_MODULE_AVAILABLE(~A, ~A)\n" var up)
  110. (format #t "test \"$~A\" = no &&\n ~A=\"~A $~A\"~A"
  111. var *pww* down *pww* "\n\n")))
  112. weak-edges))
  113. (define (>>program module progs)
  114. (let ((vars (map (lambda (prog)
  115. (format #f "guile_module~Asupport_~A"
  116. (safe-name module)
  117. prog))
  118. progs)))
  119. (for-each (lambda (var prog)
  120. (format #t "AC_PATH_PROG(~A, ~A)\n" var prog))
  121. vars progs)
  122. (format #t "test \\\n")
  123. (for-each (lambda (var)
  124. (format #t " \"$~A\" = \"\" -o \\\n" var))
  125. vars)
  126. (format #t "~A &&\n~A=\"~A $~A\"\n\n"
  127. (list-ref (list "war = peace"
  128. "freedom = slavery"
  129. "ignorance = strength")
  130. (random 3))
  131. *pww* module *pww*)))
  132. (define (>>programs programs)
  133. (for-each (lambda (form)
  134. (>>program (car form) (cdr form)))
  135. programs))
  136. (define (unglob pattern)
  137. (let ((p (open-input-pipe (format #f "echo '(' ~A ')'" pattern))))
  138. (map symbol->string (read p))))
  139. (define (>>checks forms)
  140. (let* ((cfg (canonical-configuration forms))
  141. (files (apply append (map unglob (cfg 'files-glob))))
  142. (ncx (cfg 'non-critical-external))
  143. (nci (cfg 'non-critical-internal))
  144. (report ((make-frisker) files))
  145. (external (report 'external)))
  146. (let ((pww-varname (cfg 'pww-varname)))
  147. (or (null? pww-varname) (set! *pww* (car pww-varname))))
  148. (receive (weak strong)
  149. (partition (lambda (module)
  150. (or (member module ncx)
  151. (every (lambda (i)
  152. (member i nci))
  153. (map edge-down (mod-down-ls module)))))
  154. external)
  155. (format #t "AC_DEFUN([AUTOFRISK_CHECKS],[\n\n")
  156. (>>strong strong)
  157. (format #t "\n~A=~S\n\n" *pww* "")
  158. (>>weak (fold (lambda (module so-far)
  159. (append so-far (mod-down-ls module)))
  160. (list)
  161. weak))
  162. (>>programs (cfg 'programs))
  163. (format #t "AC_SUBST(~A)\n])\n\n" *pww*))))
  164. (define (>>summary)
  165. (format #t
  166. (symbol->string
  167. '#{
  168. AC_DEFUN([AUTOFRISK_SUMMARY],[
  169. if test ! "$~A" = "" ; then
  170. p=" ***"
  171. echo "$p"
  172. echo "$p NOTE:"
  173. echo "$p The following modules probably won't work:"
  174. echo "$p $~A"
  175. echo "$p They can be installed anyway, and will work if their"
  176. echo "$p dependencies are installed later. Please see README."
  177. echo "$p"
  178. fi
  179. ])
  180. }#)
  181. *pww* *pww*))
  182. (define (autofrisk . args)
  183. (let ((file (if (null? args) "modules.af" (car args))))
  184. (or (file-exists? file)
  185. (error "could not find input file:" file))
  186. (with-output-to-file (format #f "~A.m4" file)
  187. (lambda ()
  188. (>>checks (read-scheme-source-silently file))
  189. (>>summary)))))
  190. (define main autofrisk)
  191. ;; Local variables:
  192. ;; eval: (put 'receive 'scheme-indent-function 2)
  193. ;; End:
  194. ;;; autofrisk ends here