guile-config.in 8.9 KB

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