4 ;;;; guile-config --- utility for linking programs with Guile
5 ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
7 ;;;; Copyright (C) 1998, 2001, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
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.
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.
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
24 ;;; * Add some plausible structure for returning the right exit status,
25 ;;; just something that encourages people to do the correct thing.
26 ;;; * Implement the static library support. This requires that
27 ;;; some portion of the module system be done.
29 (use-modules (ice-9 string-fun))
32 ;;;; main function, command-line processing
34 ;;; The script's entry point.
36 (set-program-name! (car args))
37 (let ((args (cdr args)))
39 ((null? args) (show-help '())
41 ((assoc (car args) command-table)
43 (set! subcommand-name (car args))
44 ((cadr row) (cdr args))))
48 (define program-name #f)
49 (define subcommand-name #f)
50 (define program-version "@-GUILE_VERSION-@")
52 ;;; Given an executable path PATH, set program-name to something
53 ;;; appropriate f or use in error messages (i.e., with leading
54 ;;; directory names stripped).
55 (define (set-program-name! path)
56 (set! program-name (basename path)))
58 (define (show-help args)
60 ((null? args) (show-help-overview))
61 ((assoc (car args) command-table)
62 => (lambda (row) ((caddr row))))
64 (show-help-overview))))
66 (define (show-help-overview)
67 (display-line-error "Usage: ")
68 (for-each (lambda (row) ((cadddr row)))
72 (let ((dle display-line-error)
74 (dle " " p " --help - show usage info (this message)")
75 (dle " " p " --help SUBCOMMAND - show help for SUBCOMMAND")))
77 (define (show-version args)
78 (display-line-error program-name " - Guile version " program-version))
80 (define (help-version)
81 (let ((dle display-line-error))
82 (dle "Usage: " program-name " --version")
83 (dle "Show the version of this script. This is also the version of")
84 (dle "Guile this script was installed with.")))
86 (define (usage-version)
88 " " program-name " --version - show installed script and Guile version"))
91 ;;;; the "link" subcommand
93 ;;; Write a set of linker flags to standard output to include the
94 ;;; libraries that libguile needs to link against.
96 ;;; In the long run, we want to derive these flags from Guile module
97 ;;; declarations files that are installed along the load path. For
98 ;;; now, we're just going to reach into Guile's configuration info and
100 (define (build-link args)
102 ;; If PATH has the form FOO/libBAR.a, return the substring
103 ;; BAR, otherwise return #f.
104 (define (match-lib path)
105 (let* ((base (basename path))
106 (len (string-length base)))
108 (string=? (substring base 0 3) "lib")
109 (string=? (substring base (- len 2)) ".a"))
110 (substring base 3 (- len 2))
113 (if (> (length args) 0)
115 (string-append program-name
116 " link: arguments to subcommand not yet implemented")))
118 (let ((libdir (get-build-info 'libdir))
121 ;; Get the string of linker flags we used to build
122 ;; Guile, and break it up into a list.
123 (separate-fields-discarding-char #\space
124 (get-build-info 'LIBS)
130 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
131 ((match-lib (car libs))
133 (cons (string-append "-l" bar)
136 ;; Remove any empty strings that may have seeped in there.
137 ((string=? (car libs) "") (loop (cdr libs)))
139 (else (cons (car libs) (loop (cdr libs))))))))
141 ;; Include libguile itself in the list, along with the directory
142 ;; it was installed in, but do *not* add /usr/lib since that may
143 ;; prevent other programs from specifying non-/usr/lib versions
144 ;; via their foo-config scripts. If *any* app puts -L/usr/lib in
145 ;; the output of its foo-config script then it may prevent the use
146 ;; a non-/usr/lib install of anything that also has a /usr/lib
147 ;; install. For now we hard-code /usr/lib, but later maybe we can
148 ;; do something more dynamic (i.e. what do we need.
150 ;; Display the flags, separated by spaces.
151 (display (string-join
153 (get-build-info 'CFLAGS)
154 (if (or (string=? libdir "/usr/lib")
155 (string=? libdir "/usr/lib/"))
157 (string-append "-L" (get-build-info 'libdir)))
159 (string-join other-flags)
166 (let ((dle display-line-error))
167 (dle "Usage: " program-name " link")
168 (dle "Print linker flags for building the `guile' executable.")
169 (dle "Print the linker command-line flags necessary to link against")
170 (dle "the Guile library, and any other libraries it requires.")))
174 " " program-name " link - print libraries to link with"))
178 ;;;; The "compile" subcommand
180 (define (build-compile args)
181 (if (> (length args) 0)
183 (string-append program-name
184 " compile: no arguments expected")))
186 ;; See gcc manual wrt fixincludes. Search for "Use of
187 ;; `-I/usr/include' may cause trouble." For now we hard-code this.
188 ;; Later maybe we can do something more dynamic.
191 (if (not (string=? (get-build-info 'includedir) "/usr/include"))
192 (string-append "-I" (get-build-info 'includedir) " ")
195 (get-build-info 'CFLAGS)
199 (define (help-compile)
200 (let ((dle display-line-error))
201 (dle "Usage: " program-name " compile")
202 (dle "Print C compiler flags for compiling code that uses Guile.")
203 (dle "This includes any `-I' flags needed to find Guile's header files.")))
205 (define (usage-compile)
207 " " program-name " compile - print C compiler flags to compile with"))
210 ;;;; The "info" subcommand
212 (define (build-info args)
214 ((null? args) (show-all-vars))
215 ((null? (cdr args)) (show-var (car args)))
216 (else (display-line-error "Usage: " program-name " info [VAR]")
219 (define (show-all-vars)
220 (for-each (lambda (binding)
221 (display-line (car binding) " = " (cdr binding)))
224 (define (show-var var)
225 (display (get-build-info (string->symbol var)))
229 (let ((d display-line-error))
230 (d "Usage: " program-name " info [VAR]")
231 (d "Display the value of the Makefile variable VAR used when Guile")
232 (d "was built. If VAR is omitted, display all Makefile variables.")
233 (d "Use this command to find out where Guile was installed,")
234 (d "where it will look for Scheme code at run-time, and so on.")))
238 " " program-name " info [VAR] - print Guile build directories"))
241 ;;;; trivial utilities
243 (define (get-build-info name)
244 (let ((val (assq name %guile-build-info)))
245 (if (not (pair? val))
248 program-name " " subcommand-name ": no such build-info: " name)
252 (define (display-line . args)
253 (apply display-line-port (current-output-port) args))
255 (define (display-line-error . args)
256 (apply display-line-port (current-error-port) args))
258 (define (display-line-port port . args)
259 (for-each (lambda (arg) (display arg port))
264 ;;;; the command table
266 ;;; We define this down here, so Guile builds the list after all the
267 ;;; functions have been defined.
268 (define command-table
270 (list "--version" show-version help-version usage-version)
271 (list "--help" show-help show-help-overview usage-help)
272 (list "link" build-link help-link usage-link)
273 (list "compile" build-compile help-compile usage-compile)
274 (list "info" build-info help-info usage-info)))