]> git.donarmstrong.com Git - lilypond.git/blob - guile18/guile-config/guile-config.in
Import guile-1.8 as multiple upstream tarball component
[lilypond.git] / guile18 / 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, 2008 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                (if (or (string=? libdir "/usr/lib")
155                        (string=? libdir "/usr/lib/"))
156                    ""
157                    (string-append "-L" (get-build-info 'libdir)))
158                "-lguile -lltdl"
159                (string-join other-flags)
160
161                )))
162     (newline)))
163
164
165 (define (help-link)
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.")))
171
172 (define (usage-link)
173   (display-line-error
174    "  " program-name " link        - print libraries to link with"))
175
176
177 \f
178 ;;;; The "compile" subcommand
179
180 (define (build-compile args)
181   (if (> (length args) 0)
182       (error
183        (string-append program-name
184                       " compile: no arguments expected")))
185
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.
189   (display
190    (string-append
191     (if (not (string=? (get-build-info 'includedir) "/usr/include"))
192          (string-append "-I" (get-build-info 'includedir) " ")
193          " ")
194     
195     (get-build-info 'CFLAGS)
196     "\n"
197     )))
198
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.")))
204
205 (define (usage-compile)
206   (display-line-error
207    "  " program-name " compile     - print C compiler flags to compile with"))
208
209 \f
210 ;;;; The "info" subcommand
211
212 (define (build-info args)
213   (cond
214    ((null? args) (show-all-vars))
215    ((null? (cdr args)) (show-var (car args)))
216    (else (display-line-error "Usage: " program-name " info [VAR]")
217          (quit 2))))
218
219 (define (show-all-vars)
220   (for-each (lambda (binding)
221               (display-line (car binding) " = " (cdr binding)))
222             %guile-build-info))
223
224 (define (show-var var)
225   (display (get-build-info (string->symbol var)))
226   (newline))
227
228 (define (help-info)
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.")))
235
236 (define (usage-info)
237   (display-line-error
238    "  " program-name " info [VAR]  - print Guile build directories"))
239
240 \f
241 ;;;; trivial utilities
242
243 (define (get-build-info name)
244   (let ((val (assq name %guile-build-info)))
245     (if (not (pair? val))
246         (begin
247           (display-line-error
248            program-name " " subcommand-name ": no such build-info: " name)
249           (quit 2)))
250     (cdr val)))
251
252 (define (display-line . args)
253   (apply display-line-port (current-output-port) args))
254
255 (define (display-line-error . args)
256   (apply display-line-port (current-error-port) args))
257
258 (define (display-line-port port . args)
259   (for-each (lambda (arg) (display arg port))
260             args)
261   (newline port))
262
263 \f
264 ;;;; the command table
265
266 ;;; We define this down here, so Guile builds the list after all the
267 ;;; functions have been defined.
268 (define command-table
269   (list
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)))
275
276 \f
277 ;;; Local Variables:
278 ;;; mode: scheme
279 ;;; End: