guile-config.in 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #!@-bindir-@/guile \
  2. -e main -s
  3. !#
  4. ;;;; guile-config --- utility for linking programs with Guile
  5. ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
  6. ;;;;
  7. ;;;; Copyright (C) 1998, 2001 Free Software Foundation, Inc.
  8. ;;;;
  9. ;;;; This program is free software; you can redistribute it and/or modify
  10. ;;;; it under the terms of the GNU General Public License as published by
  11. ;;;; the Free Software Foundation; either version 2, or (at your option)
  12. ;;;; any later version.
  13. ;;;;
  14. ;;;; This program is distributed in the hope that it will be useful,
  15. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;;;; GNU General Public License for more details.
  18. ;;;;
  19. ;;;; You should have received a copy of the GNU General Public License
  20. ;;;; along with this software; see the file COPYING. If not, write to
  21. ;;;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. ;;;; Boston, MA 02110-1301 USA
  23. ;;;;
  24. ;;;; As a special exception, the Free Software Foundation gives permission
  25. ;;;; for additional uses of the text contained in its release of GUILE.
  26. ;;;;
  27. ;;;; The exception is that, if you link the GUILE library with other files
  28. ;;;; to produce an executable, this does not by itself cause the
  29. ;;;; resulting executable to be covered by the GNU General Public License.
  30. ;;;; Your use of that executable is in no way restricted on account of
  31. ;;;; linking the GUILE library code into it.
  32. ;;;;
  33. ;;;; This exception does not however invalidate any other reasons why
  34. ;;;; the executable file might be covered by the GNU General Public License.
  35. ;;;;
  36. ;;;; This exception applies only to the code released by the
  37. ;;;; Free Software Foundation under the name GUILE. If you copy
  38. ;;;; code from other Free Software Foundation releases into a copy of
  39. ;;;; GUILE, as the General Public License permits, the exception does
  40. ;;;; not apply to the code that you add in this way. To avoid misleading
  41. ;;;; anyone as to the status of such modified files, you must delete
  42. ;;;; this exception notice from them.
  43. ;;;;
  44. ;;;; If you write modifications of your own for GUILE, it is your choice
  45. ;;;; whether to permit this exception to apply to your modifications.
  46. ;;;; If you do not wish that, delete this exception notice.
  47. ;;; TODO:
  48. ;;; * Add some plausible structure for returning the right exit status,
  49. ;;; just something that encourages people to do the correct thing.
  50. ;;; * Implement the static library support. This requires that
  51. ;;; some portion of the module system be done.
  52. (use-modules (ice-9 string-fun))
  53. ;;;; main function, command-line processing
  54. ;;; The script's entry point.
  55. (define (main args)
  56. (set-program-name! (car args))
  57. (let ((args (cdr args)))
  58. (cond
  59. ((null? args) (show-help '())
  60. (quit 1))
  61. ((assoc (car args) command-table)
  62. => (lambda (row)
  63. (set! subcommand-name (car args))
  64. ((cadr row) (cdr args))))
  65. (else (show-help '())
  66. (quit 1)))))
  67. (define program-name #f)
  68. (define subcommand-name #f)
  69. (define program-version "@-GUILE_VERSION-@")
  70. ;;; Given an executable path PATH, set program-name to something
  71. ;;; appropriate f or use in error messages (i.e., with leading
  72. ;;; directory names stripped).
  73. (define (set-program-name! path)
  74. (set! program-name (basename path)))
  75. (define (show-help args)
  76. (cond
  77. ((null? args) (show-help-overview))
  78. ((assoc (car args) command-table)
  79. => (lambda (row) ((caddr row))))
  80. (else
  81. (show-help-overview))))
  82. (define (show-help-overview)
  83. (display-line-error "Usage: ")
  84. (for-each (lambda (row) ((cadddr row)))
  85. command-table))
  86. (define (usage-help)
  87. (let ((dle display-line-error)
  88. (p program-name))
  89. (dle " " p " --help - show usage info (this message)")
  90. (dle " " p " --help SUBCOMMAND - show help for SUBCOMMAND")))
  91. (define (show-version args)
  92. (display-line-error program-name " - Guile version " program-version))
  93. (define (help-version)
  94. (let ((dle display-line-error))
  95. (dle "Usage: " program-name " --version")
  96. (dle "Show the version of this script. This is also the version of")
  97. (dle "Guile this script was installed with.")))
  98. (define (usage-version)
  99. (display-line-error
  100. " " program-name " --version - show installed script and Guile version"))
  101. ;;;; the "link" subcommand
  102. ;;; Write a set of linker flags to standard output to include the
  103. ;;; libraries that libguile needs to link against.
  104. ;;;
  105. ;;; In the long run, we want to derive these flags from Guile module
  106. ;;; declarations files that are installed along the load path. For
  107. ;;; now, we're just going to reach into Guile's configuration info and
  108. ;;; hack it out.
  109. (define (build-link args)
  110. ;; If PATH has the form FOO/libBAR.a, return the substring
  111. ;; BAR, otherwise return #f.
  112. (define (match-lib path)
  113. (let* ((base (basename path))
  114. (len (string-length base)))
  115. (if (and (> len 5)
  116. (string=? (substring base 0 3) "lib")
  117. (string=? (substring base (- len 2)) ".a"))
  118. (substring base 3 (- len 2))
  119. #f)))
  120. (if (> (length args) 0)
  121. (error
  122. (string-append program-name
  123. " link: arguments to subcommand not yet implemented")))
  124. (let ((libdir (get-build-info 'libdir))
  125. (other-flags
  126. (let loop ((libs
  127. ;; Get the string of linker flags we used to build
  128. ;; Guile, and break it up into a list.
  129. (separate-fields-discarding-char #\space
  130. (get-build-info 'LIBS)
  131. list)))
  132. (cond
  133. ((null? libs) '())
  134. ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
  135. ((match-lib (car libs))
  136. => (lambda (bar)
  137. (cons (string-append "-l" bar)
  138. (loop (cdr libs)))))
  139. ;; Remove any empty strings that may have seeped in there.
  140. ((string=? (car libs) "") (loop (cdr libs)))
  141. (else (cons (car libs) (loop (cdr libs))))))))
  142. ;; Include libguile itself in the list, along with the directory
  143. ;; it was installed in, but do *not* add /usr/lib since that may
  144. ;; prevent other programs from specifying non-/usr/lib versions
  145. ;; via their foo-config scripts. If *any* app puts -L/usr/lib in
  146. ;; the output of its foo-config script then it may prevent the use
  147. ;; a non-/usr/lib install of anything that also has a /usr/lib
  148. ;; install. For now we hard-code /usr/lib, but later maybe we can
  149. ;; do something more dynamic (i.e. what do we need.
  150. ;; Display the flags, separated by spaces.
  151. (if (or (string=? libdir "/usr/lib")
  152. (string=? libdir "/usr/lib/"))
  153. (display-separated (cons "-lguile -lguile-ltdl" other-flags))
  154. (display-separated (cons
  155. (string-append "-L" (get-build-info 'libdir))
  156. (cons "-lguile -lguile-ltdl" other-flags))))
  157. (newline)))
  158. (define (help-link)
  159. (let ((dle display-line-error))
  160. (dle "Usage: " program-name " link")
  161. (dle "Print linker flags for building the `guile' executable.")
  162. (dle "Print the linker command-line flags necessary to link against")
  163. (dle "the Guile library, and any other libraries it requires.")))
  164. (define (usage-link)
  165. (display-line-error
  166. " " program-name " link - print libraries to link with"))
  167. ;;;; The "compile" subcommand
  168. (define (build-compile args)
  169. (if (> (length args) 0)
  170. (error
  171. (string-append program-name
  172. " compile: no arguments expected")))
  173. ;; See gcc manual wrt fixincludes. Search for "Use of
  174. ;; `-I/usr/include' may cause trouble." For now we hard-code this.
  175. ;; Later maybe we can do something more dynamic.
  176. (if (not (string=? (get-build-info 'includedir) "/usr/include"))
  177. (display-line "-I" (get-build-info 'includedir))))
  178. (define (help-compile)
  179. (let ((dle display-line-error))
  180. (dle "Usage: " program-name " compile")
  181. (dle "Print C compiler flags for compiling code that uses Guile.")
  182. (dle "This includes any `-I' flags needed to find Guile's header files.")))
  183. (define (usage-compile)
  184. (display-line-error
  185. " " program-name " compile - print C compiler flags to compile with"))
  186. ;;;; The "info" subcommand
  187. (define (build-info args)
  188. (cond
  189. ((null? args) (show-all-vars))
  190. ((null? (cdr args)) (show-var (car args)))
  191. (else (display-line-error "Usage: " program-name " info [VAR]")
  192. (quit 2))))
  193. (define (show-all-vars)
  194. (for-each (lambda (binding)
  195. (display-line (car binding) " = " (cdr binding)))
  196. %guile-build-info))
  197. (define (show-var var)
  198. (display (get-build-info (string->symbol var)))
  199. (newline))
  200. (define (help-info)
  201. (let ((d display-line-error))
  202. (d "Usage: " program-name " info [VAR]")
  203. (d "Display the value of the Makefile variable VAR used when Guile")
  204. (d "was built. If VAR is omitted, display all Makefile variables.")
  205. (d "Use this command to find out where Guile was installed,")
  206. (d "where it will look for Scheme code at run-time, and so on.")))
  207. (define (usage-info)
  208. (display-line-error
  209. " " program-name " info [VAR] - print Guile build directories"))
  210. ;;;; trivial utilities
  211. (define (get-build-info name)
  212. (let ((val (assq name %guile-build-info)))
  213. (if (not (pair? val))
  214. (begin
  215. (display-line-error
  216. program-name " " subcommand-name ": no such build-info: " name)
  217. (quit 2)))
  218. (cdr val)))
  219. (define (display-line . args)
  220. (apply display-line-port (current-output-port) args))
  221. (define (display-line-error . args)
  222. (apply display-line-port (current-error-port) args))
  223. (define (display-line-port port . args)
  224. (for-each (lambda (arg) (display arg port))
  225. args)
  226. (newline port))
  227. (define (display-separated args)
  228. (if (not (null? args))
  229. (begin
  230. (display (car args))
  231. (for-each
  232. (lambda (arg) (display " ") (display arg))
  233. (cdr args)))))
  234. ;;;; the command table
  235. ;;; We define this down here, so Guile builds the list after all the
  236. ;;; functions have been defined.
  237. (define command-table
  238. (list
  239. (list "--version" show-version help-version usage-version)
  240. (list "--help" show-help show-help-overview usage-help)
  241. (list "link" build-link help-link usage-link)
  242. (list "compile" build-compile help-compile usage-compile)
  243. (list "info" build-info help-info usage-info)))
  244. ;;; Local Variables:
  245. ;;; mode: scheme
  246. ;;; End: