Commit | Line | Data |
---|---|---|
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 | ;;;; |
89acd190 | 7 | ;;;; Copyright (C) 1998, 2001, 2004 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. |
89acd190 | 151 | (display-separated (list (get-build-info 'CFLAGS) "")) |
efb378b0 RB |
152 | (if (or (string=? libdir "/usr/lib") |
153 | (string=? libdir "/usr/lib/")) | |
844c219a | 154 | (display-separated (cons "-lguile -lguile-ltdl" other-flags)) |
efb378b0 RB |
155 | (display-separated (cons |
156 | (string-append "-L" (get-build-info 'libdir)) | |
844c219a | 157 | (cons "-lguile -lguile-ltdl" other-flags)))) |
e96452c4 | 158 | (newline))) |
9a56cb24 JB |
159 | |
160 | (define (help-link) | |
161 | (let ((dle display-line-error)) | |
162 | (dle "Usage: " program-name " link") | |
163 | (dle "Print linker flags for building the `guile' executable.") | |
204c26b9 JB |
164 | (dle "Print the linker command-line flags necessary to link against") |
165 | (dle "the Guile library, and any other libraries it requires."))) | |
9a56cb24 | 166 | |
204c26b9 JB |
167 | (define (usage-link) |
168 | (display-line-error | |
169 | " " program-name " link - print libraries to link with")) | |
9a56cb24 | 170 | |
9a56cb24 | 171 | |
204c26b9 JB |
172 | \f |
173 | ;;;; The "compile" subcommand | |
9a56cb24 | 174 | |
817e0769 JB |
175 | (define (build-compile args) |
176 | (if (> (length args) 0) | |
177 | (error | |
178 | (string-append program-name | |
179 | " compile: no arguments expected"))) | |
efb378b0 RB |
180 | |
181 | ;; See gcc manual wrt fixincludes. Search for "Use of | |
182 | ;; `-I/usr/include' may cause trouble." For now we hard-code this. | |
183 | ;; Later maybe we can do something more dynamic. | |
184 | (if (not (string=? (get-build-info 'includedir) "/usr/include")) | |
89acd190 MV |
185 | (display-separated (list "-I" (get-build-info 'includedir) ""))) |
186 | (display-line (get-build-info 'CFLAGS))) | |
817e0769 JB |
187 | |
188 | (define (help-compile) | |
189 | (let ((dle display-line-error)) | |
190 | (dle "Usage: " program-name " compile") | |
191 | (dle "Print C compiler flags for compiling code that uses Guile.") | |
192 | (dle "This includes any `-I' flags needed to find Guile's header files."))) | |
193 | ||
194 | (define (usage-compile) | |
195 | (display-line-error | |
196 | " " program-name " compile - print C compiler flags to compile with")) | |
9a56cb24 JB |
197 | |
198 | \f | |
199 | ;;;; The "info" subcommand | |
200 | ||
201 | (define (build-info args) | |
202 | (cond | |
203 | ((null? args) (show-all-vars)) | |
204 | ((null? (cdr args)) (show-var (car args))) | |
205 | (else (display-line-error "Usage: " program-name " info [VAR]") | |
206 | (quit 2)))) | |
207 | ||
208 | (define (show-all-vars) | |
209 | (for-each (lambda (binding) | |
210 | (display-line (car binding) " = " (cdr binding))) | |
211 | %guile-build-info)) | |
212 | ||
213 | (define (show-var var) | |
214 | (display (get-build-info (string->symbol var))) | |
215 | (newline)) | |
216 | ||
217 | (define (help-info) | |
204c26b9 JB |
218 | (let ((d display-line-error)) |
219 | (d "Usage: " program-name " info [VAR]") | |
220 | (d "Display the value of the Makefile variable VAR used when Guile") | |
221 | (d "was built. If VAR is omitted, display all Makefile variables.") | |
222 | (d "Use this command to find out where Guile was installed,") | |
223 | (d "where it will look for Scheme code at run-time, and so on."))) | |
9a56cb24 | 224 | |
204c26b9 JB |
225 | (define (usage-info) |
226 | (display-line-error | |
227 | " " program-name " info [VAR] - print Guile build directories")) | |
9a56cb24 JB |
228 | |
229 | \f | |
230 | ;;;; trivial utilities | |
231 | ||
232 | (define (get-build-info name) | |
233 | (let ((val (assq name %guile-build-info))) | |
234 | (if (not (pair? val)) | |
235 | (begin | |
236 | (display-line-error | |
237 | program-name " " subcommand-name ": no such build-info: " name) | |
238 | (quit 2))) | |
239 | (cdr val))) | |
240 | ||
241 | (define (display-line . args) | |
242 | (apply display-line-port (current-output-port) args)) | |
243 | ||
244 | (define (display-line-error . args) | |
245 | (apply display-line-port (current-error-port) args)) | |
246 | ||
247 | (define (display-line-port port . args) | |
248 | (for-each (lambda (arg) (display arg port)) | |
249 | args) | |
709a308d | 250 | (newline port)) |
9a56cb24 JB |
251 | |
252 | (define (display-separated args) | |
efb378b0 RB |
253 | (if (not (null? args)) |
254 | (begin | |
255 | (display (car args)) | |
256 | (for-each | |
257 | (lambda (arg) (display " ") (display arg)) | |
258 | (cdr args))))) | |
9a56cb24 JB |
259 | |
260 | \f | |
261 | ;;;; the command table | |
262 | ||
263 | ;;; We define this down here, so Guile builds the list after all the | |
264 | ;;; functions have been defined. | |
265 | (define command-table | |
266 | (list | |
204c26b9 JB |
267 | (list "--version" show-version help-version usage-version) |
268 | (list "--help" show-help show-help-overview usage-help) | |
269 | (list "link" build-link help-link usage-link) | |
270 | (list "compile" build-compile help-compile usage-compile) | |
271 | (list "info" build-info help-info usage-info))) | |
9a56cb24 JB |
272 | |
273 | \f | |
274 | ;;; Local Variables: | |
275 | ;;; mode: scheme | |
276 | ;;; End: |