Changed license terms to the plain LGPL thru-out.
[bpt/guile.git] / guile-config / guile-config.in
CommitLineData
9a56cb24
JB
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
093d0179 6;;;;
12ce651a 7;;;; Copyright (C) 1998, 2001 Free Software Foundation, Inc.
093d0179 8;;;;
73be1d9e
MV
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.
093d0179 13;;;;
73be1d9e 14;;;; This library is distributed in the hope that it will be useful,
093d0179 15;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
73be1d9e
MV
16;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17;;;; Lesser General Public License for more details.
093d0179 18;;;;
73be1d9e
MV
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
9a56cb24
JB
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
da509974 29(use-modules (ice-9 string-fun))
9a56cb24
JB
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)
da509974 56 (set! program-name (basename path)))
9a56cb24
JB
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)
204c26b9
JB
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")))
9a56cb24
JB
76
77(define (show-version args)
204c26b9
JB
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"))
9a56cb24
JB
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)
9a56cb24 101
da509974
JB
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)
9fb77163
DH
108 (string=? (substring base 0 3) "lib")
109 (string=? (substring base (- len 2)) ".a"))
110 (substring base 3 (- len 2))
da509974
JB
111 #f)))
112
6073aaf2
MD
113 (if (> (length args) 0)
114 (error
115 (string-append program-name
116 " link: arguments to subcommand not yet implemented")))
117
efb378b0
RB
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
e96452c4 150 ;; Display the flags, separated by spaces.
efb378b0
RB
151 (if (or (string=? libdir "/usr/lib")
152 (string=? libdir "/usr/lib/"))
844c219a 153 (display-separated (cons "-lguile -lguile-ltdl" other-flags))
efb378b0
RB
154 (display-separated (cons
155 (string-append "-L" (get-build-info 'libdir))
844c219a 156 (cons "-lguile -lguile-ltdl" other-flags))))
e96452c4 157 (newline)))
9a56cb24
JB
158
159(define (help-link)
160 (let ((dle display-line-error))
161 (dle "Usage: " program-name " link")
162 (dle "Print linker flags for building the `guile' executable.")
204c26b9
JB
163 (dle "Print the linker command-line flags necessary to link against")
164 (dle "the Guile library, and any other libraries it requires.")))
9a56cb24 165
204c26b9
JB
166(define (usage-link)
167 (display-line-error
168 " " program-name " link - print libraries to link with"))
9a56cb24 169
9a56cb24 170
204c26b9
JB
171\f
172;;;; The "compile" subcommand
9a56cb24 173
817e0769
JB
174(define (build-compile args)
175 (if (> (length args) 0)
176 (error
177 (string-append program-name
178 " compile: no arguments expected")))
efb378b0
RB
179
180 ;; See gcc manual wrt fixincludes. Search for "Use of
181 ;; `-I/usr/include' may cause trouble." For now we hard-code this.
182 ;; Later maybe we can do something more dynamic.
183 (if (not (string=? (get-build-info 'includedir) "/usr/include"))
184 (display-line "-I" (get-build-info 'includedir))))
817e0769
JB
185
186(define (help-compile)
187 (let ((dle display-line-error))
188 (dle "Usage: " program-name " compile")
189 (dle "Print C compiler flags for compiling code that uses Guile.")
190 (dle "This includes any `-I' flags needed to find Guile's header files.")))
191
192(define (usage-compile)
193 (display-line-error
194 " " program-name " compile - print C compiler flags to compile with"))
9a56cb24
JB
195
196\f
197;;;; The "info" subcommand
198
199(define (build-info args)
200 (cond
201 ((null? args) (show-all-vars))
202 ((null? (cdr args)) (show-var (car args)))
203 (else (display-line-error "Usage: " program-name " info [VAR]")
204 (quit 2))))
205
206(define (show-all-vars)
207 (for-each (lambda (binding)
208 (display-line (car binding) " = " (cdr binding)))
209 %guile-build-info))
210
211(define (show-var var)
212 (display (get-build-info (string->symbol var)))
213 (newline))
214
215(define (help-info)
204c26b9
JB
216 (let ((d display-line-error))
217 (d "Usage: " program-name " info [VAR]")
218 (d "Display the value of the Makefile variable VAR used when Guile")
219 (d "was built. If VAR is omitted, display all Makefile variables.")
220 (d "Use this command to find out where Guile was installed,")
221 (d "where it will look for Scheme code at run-time, and so on.")))
9a56cb24 222
204c26b9
JB
223(define (usage-info)
224 (display-line-error
225 " " program-name " info [VAR] - print Guile build directories"))
9a56cb24
JB
226
227\f
228;;;; trivial utilities
229
230(define (get-build-info name)
231 (let ((val (assq name %guile-build-info)))
232 (if (not (pair? val))
233 (begin
234 (display-line-error
235 program-name " " subcommand-name ": no such build-info: " name)
236 (quit 2)))
237 (cdr val)))
238
239(define (display-line . args)
240 (apply display-line-port (current-output-port) args))
241
242(define (display-line-error . args)
243 (apply display-line-port (current-error-port) args))
244
245(define (display-line-port port . args)
246 (for-each (lambda (arg) (display arg port))
247 args)
709a308d 248 (newline port))
9a56cb24
JB
249
250(define (display-separated args)
efb378b0
RB
251 (if (not (null? args))
252 (begin
253 (display (car args))
254 (for-each
255 (lambda (arg) (display " ") (display arg))
256 (cdr args)))))
9a56cb24
JB
257
258\f
259;;;; the command table
260
261;;; We define this down here, so Guile builds the list after all the
262;;; functions have been defined.
263(define command-table
264 (list
204c26b9
JB
265 (list "--version" show-version help-version usage-version)
266 (list "--help" show-help show-help-overview usage-help)
267 (list "link" build-link help-link usage-link)
268 (list "compile" build-compile help-compile usage-compile)
269 (list "info" build-info help-info usage-info)))
9a56cb24
JB
270
271\f
272;;; Local Variables:
273;;; mode: scheme
274;;; End: