4 ;;;; guile-config --- utility for linking programs with Guile
5 ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
7 ;;;; Copyright (C) 1998, 2001 Free Software Foundation, Inc.
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.
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.
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., 59 Temple Place, Suite 330,
22 ;;;; Boston, MA 02111-1307 USA
24 ;;;; As a special exception, the Free Software Foundation gives permission
25 ;;;; for additional uses of the text contained in its release of GUILE.
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.
33 ;;;; This exception does not however invalidate any other reasons why
34 ;;;; the executable file might be covered by the GNU General Public License.
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.
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.
49 ;;; * Add some plausible structure for returning the right exit status,
50 ;;; just something that encourages people to do the correct thing.
51 ;;; * Implement the static library support. This requires that
52 ;;; some portion of the module system be done.
54 (use-modules (ice-9 string-fun))
57 ;;;; main function, command-line processing
59 ;;; The script's entry point.
61 (set-program-name! (car args))
62 (let ((args (cdr args)))
64 ((null? args) (show-help '())
66 ((assoc (car args) command-table)
68 (set! subcommand-name (car args))
69 ((cadr row) (cdr args))))
73 (define program-name #f)
74 (define subcommand-name #f)
75 (define program-version "1.6.0")
77 ;;; Given an executable path PATH, set program-name to something
78 ;;; appropriate f or use in error messages (i.e., with leading
79 ;;; directory names stripped).
80 (define (set-program-name! path)
81 (set! program-name (basename path)))
83 (define (show-help args)
85 ((null? args) (show-help-overview))
86 ((assoc (car args) command-table)
87 => (lambda (row) ((caddr row))))
89 (show-help-overview))))
91 (define (show-help-overview)
92 (display-line-error "Usage: ")
93 (for-each (lambda (row) ((cadddr row)))
97 (let ((dle display-line-error)
99 (dle " " p " --help - show usage info (this message)")
100 (dle " " p " --help SUBCOMMAND - show help for SUBCOMMAND")))
102 (define (show-version args)
103 (display-line-error program-name " - Guile version " program-version))
105 (define (help-version)
106 (let ((dle display-line-error))
107 (dle "Usage: " program-name " --version")
108 (dle "Show the version of this script. This is also the version of")
109 (dle "Guile this script was installed with.")))
111 (define (usage-version)
113 " " program-name " --version - show installed script and Guile version"))
116 ;;;; the "link" subcommand
118 ;;; Write a set of linker flags to standard output to include the
119 ;;; libraries that libguile needs to link against.
121 ;;; In the long run, we want to derive these flags from Guile module
122 ;;; declarations files that are installed along the load path. For
123 ;;; now, we're just going to reach into Guile's configuration info and
125 (define (build-link args)
127 ;; If PATH has the form FOO/libBAR.a, return the substring
128 ;; BAR, otherwise return #f.
129 (define (match-lib path)
130 (let* ((base (basename path))
131 (len (string-length base)))
133 (string=? (substring base 0 3) "lib")
134 (string=? (substring base (- len 2)) ".a"))
135 (substring base 3 (- len 2))
138 (if (> (length args) 0)
140 (string-append program-name
141 " link: arguments to subcommand not yet implemented")))
143 (let ((libdir (get-build-info 'libdir))
146 ;; Get the string of linker flags we used to build
147 ;; Guile, and break it up into a list.
148 (separate-fields-discarding-char #\space
149 (get-build-info 'LIBS)
155 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
156 ((match-lib (car libs))
158 (cons (string-append "-l" bar)
161 ;; Remove any empty strings that may have seeped in there.
162 ((string=? (car libs) "") (loop (cdr libs)))
164 (else (cons (car libs) (loop (cdr libs))))))))
166 ;; Include libguile itself in the list, along with the directory
167 ;; it was installed in, but do *not* add /usr/lib since that may
168 ;; prevent other programs from specifying non-/usr/lib versions
169 ;; via their foo-config scripts. If *any* app puts -L/usr/lib in
170 ;; the output of its foo-config script then it may prevent the use
171 ;; a non-/usr/lib install of anything that also has a /usr/lib
172 ;; install. For now we hard-code /usr/lib, but later maybe we can
173 ;; do something more dynamic (i.e. what do we need.
175 ;; Display the flags, separated by spaces.
176 (if (or (string=? libdir "/usr/lib")
177 (string=? libdir "/usr/lib/"))
178 (display-separated (cons "-lguile" other-flags))
179 (display-separated (cons
180 (string-append "-L" (get-build-info 'libdir))
181 (cons "-lguile" other-flags))))
185 (let ((dle display-line-error))
186 (dle "Usage: " program-name " link")
187 (dle "Print linker flags for building the `guile' executable.")
188 (dle "Print the linker command-line flags necessary to link against")
189 (dle "the Guile library, and any other libraries it requires.")))
193 " " program-name " link - print libraries to link with"))
197 ;;;; The "compile" subcommand
199 (define (build-compile args)
200 (if (> (length args) 0)
202 (string-append program-name
203 " compile: no arguments expected")))
205 ;; See gcc manual wrt fixincludes. Search for "Use of
206 ;; `-I/usr/include' may cause trouble." For now we hard-code this.
207 ;; Later maybe we can do something more dynamic.
208 (if (not (string=? (get-build-info 'includedir) "/usr/include"))
209 (display-line "-I" (get-build-info 'includedir))))
211 (define (help-compile)
212 (let ((dle display-line-error))
213 (dle "Usage: " program-name " compile")
214 (dle "Print C compiler flags for compiling code that uses Guile.")
215 (dle "This includes any `-I' flags needed to find Guile's header files.")))
217 (define (usage-compile)
219 " " program-name " compile - print C compiler flags to compile with"))
222 ;;;; The "info" subcommand
224 (define (build-info args)
226 ((null? args) (show-all-vars))
227 ((null? (cdr args)) (show-var (car args)))
228 (else (display-line-error "Usage: " program-name " info [VAR]")
231 (define (show-all-vars)
232 (for-each (lambda (binding)
233 (display-line (car binding) " = " (cdr binding)))
236 (define (show-var var)
237 (display (get-build-info (string->symbol var)))
241 (let ((d display-line-error))
242 (d "Usage: " program-name " info [VAR]")
243 (d "Display the value of the Makefile variable VAR used when Guile")
244 (d "was built. If VAR is omitted, display all Makefile variables.")
245 (d "Use this command to find out where Guile was installed,")
246 (d "where it will look for Scheme code at run-time, and so on.")))
250 " " program-name " info [VAR] - print Guile build directories"))
253 ;;;; trivial utilities
255 (define (get-build-info name)
256 (let ((val (assq name %guile-build-info)))
257 (if (not (pair? val))
260 program-name " " subcommand-name ": no such build-info: " name)
264 (define (display-line . args)
265 (apply display-line-port (current-output-port) args))
267 (define (display-line-error . args)
268 (apply display-line-port (current-error-port) args))
270 (define (display-line-port port . args)
271 (for-each (lambda (arg) (display arg port))
275 (define (display-separated args)
276 (if (not (null? args))
280 (lambda (arg) (display " ") (display arg))
284 ;;;; the command table
286 ;;; We define this down here, so Guile builds the list after all the
287 ;;; functions have been defined.
288 (define command-table
290 (list "--version" show-version help-version usage-version)
291 (list "--help" show-help show-help-overview usage-help)
292 (list "link" build-link help-link usage-link)
293 (list "compile" build-compile help-compile usage-compile)
294 (list "info" build-info help-info usage-info)))