merge from 1.8 branch
[bpt/guile.git] / guile-config / guile-config.in
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 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
23 ;;; TODO:
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.
28
29 (use-modules (ice-9 string-fun))
30
31 \f
32 ;;;; main function, command-line processing
33
34 ;;; The script's entry point.
35 (define (main args)
36 (set-program-name! (car args))
37 (let ((args (cdr args)))
38 (cond
39 ((null? args) (show-help '())
40 (quit 1))
41 ((assoc (car args) command-table)
42 => (lambda (row)
43 (set! subcommand-name (car args))
44 ((cadr row) (cdr args))))
45 (else (show-help '())
46 (quit 1)))))
47
48 (define program-name #f)
49 (define subcommand-name #f)
50 (define program-version "@-GUILE_VERSION-@")
51
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)))
57
58 (define (show-help args)
59 (cond
60 ((null? args) (show-help-overview))
61 ((assoc (car args) command-table)
62 => (lambda (row) ((caddr row))))
63 (else
64 (show-help-overview))))
65
66 (define (show-help-overview)
67 (display-line-error "Usage: ")
68 (for-each (lambda (row) ((cadddr row)))
69 command-table))
70
71 (define (usage-help)
72 (let ((dle display-line-error)
73 (p program-name))
74 (dle " " p " --help - show usage info (this message)")
75 (dle " " p " --help SUBCOMMAND - show help for SUBCOMMAND")))
76
77 (define (show-version args)
78 (display-line-error program-name " - Guile version " program-version))
79
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.")))
85
86 (define (usage-version)
87 (display-line-error
88 " " program-name " --version - show installed script and Guile version"))
89
90 \f
91 ;;;; the "link" subcommand
92
93 ;;; Write a set of linker flags to standard output to include the
94 ;;; libraries that libguile needs to link against.
95 ;;;
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
99 ;;; hack it out.
100 (define (build-link args)
101
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)))
107 (if (and (> len 5)
108 (string=? (substring base 0 3) "lib")
109 (string=? (substring base (- len 2)) ".a"))
110 (substring base 3 (- len 2))
111 #f)))
112
113 (if (> (length args) 0)
114 (error
115 (string-append program-name
116 " link: arguments to subcommand not yet implemented")))
117
118 (let ((libdir (get-build-info 'libdir))
119 (other-flags
120 (let loop ((libs
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)
125 list)))
126
127 (cond
128 ((null? libs) '())
129
130 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
131 ((match-lib (car libs))
132 => (lambda (bar)
133 (cons (string-append "-l" bar)
134 (loop (cdr libs)))))
135
136 ;; Remove any empty strings that may have seeped in there.
137 ((string=? (car libs) "") (loop (cdr libs)))
138
139 (else (cons (car libs) (loop (cdr libs))))))))
140
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.
149
150 ;; Display the flags, separated by spaces.
151 (display (string-join
152 (list
153 (get-build-info 'CFLAGS)
154 "-lguile -lltdl"
155 (if (string=? libdir "/usr/lib/")
156 ""
157 (string-append "-L" (get-build-info 'libdir)))
158 (string-join other-flags)
159
160 )))
161 (newline)))
162
163
164 (define (help-link)
165 (let ((dle display-line-error))
166 (dle "Usage: " program-name " link")
167 (dle "Print linker flags for building the `guile' executable.")
168 (dle "Print the linker command-line flags necessary to link against")
169 (dle "the Guile library, and any other libraries it requires.")))
170
171 (define (usage-link)
172 (display-line-error
173 " " program-name " link - print libraries to link with"))
174
175
176 \f
177 ;;;; The "compile" subcommand
178
179 (define (build-compile args)
180 (if (> (length args) 0)
181 (error
182 (string-append program-name
183 " compile: no arguments expected")))
184
185 ;; See gcc manual wrt fixincludes. Search for "Use of
186 ;; `-I/usr/include' may cause trouble." For now we hard-code this.
187 ;; Later maybe we can do something more dynamic.
188 (display
189 (string-append
190 (if (not (string=? (get-build-info 'includedir) "/usr/include"))
191 (string-append "-I" (get-build-info 'includedir) " ")
192 " ")
193
194 (get-build-info 'CFLAGS)
195 "\n"
196 )))
197
198 (define (help-compile)
199 (let ((dle display-line-error))
200 (dle "Usage: " program-name " compile")
201 (dle "Print C compiler flags for compiling code that uses Guile.")
202 (dle "This includes any `-I' flags needed to find Guile's header files.")))
203
204 (define (usage-compile)
205 (display-line-error
206 " " program-name " compile - print C compiler flags to compile with"))
207
208 \f
209 ;;;; The "info" subcommand
210
211 (define (build-info args)
212 (cond
213 ((null? args) (show-all-vars))
214 ((null? (cdr args)) (show-var (car args)))
215 (else (display-line-error "Usage: " program-name " info [VAR]")
216 (quit 2))))
217
218 (define (show-all-vars)
219 (for-each (lambda (binding)
220 (display-line (car binding) " = " (cdr binding)))
221 %guile-build-info))
222
223 (define (show-var var)
224 (display (get-build-info (string->symbol var)))
225 (newline))
226
227 (define (help-info)
228 (let ((d display-line-error))
229 (d "Usage: " program-name " info [VAR]")
230 (d "Display the value of the Makefile variable VAR used when Guile")
231 (d "was built. If VAR is omitted, display all Makefile variables.")
232 (d "Use this command to find out where Guile was installed,")
233 (d "where it will look for Scheme code at run-time, and so on.")))
234
235 (define (usage-info)
236 (display-line-error
237 " " program-name " info [VAR] - print Guile build directories"))
238
239 \f
240 ;;;; trivial utilities
241
242 (define (get-build-info name)
243 (let ((val (assq name %guile-build-info)))
244 (if (not (pair? val))
245 (begin
246 (display-line-error
247 program-name " " subcommand-name ": no such build-info: " name)
248 (quit 2)))
249 (cdr val)))
250
251 (define (display-line . args)
252 (apply display-line-port (current-output-port) args))
253
254 (define (display-line-error . args)
255 (apply display-line-port (current-error-port) args))
256
257 (define (display-line-port port . args)
258 (for-each (lambda (arg) (display arg port))
259 args)
260 (newline port))
261
262 \f
263 ;;;; the command table
264
265 ;;; We define this down here, so Guile builds the list after all the
266 ;;; functions have been defined.
267 (define command-table
268 (list
269 (list "--version" show-version help-version usage-version)
270 (list "--help" show-help show-help-overview usage-help)
271 (list "link" build-link help-link usage-link)
272 (list "compile" build-compile help-compile usage-compile)
273 (list "info" build-info help-info usage-info)))
274
275 \f
276 ;;; Local Variables:
277 ;;; mode: scheme
278 ;;; End: