* guile-config.in (build-link): Include a -L option in the output
[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 ;;; TODO:
8 ;;; * Add some plausible structure for returning the right exit status,
9 ;;; just something that encourages people to do the correct thing.
10 ;;; * Implement the static library support. This requires that
11 ;;; some portion of the module system be done.
12
13 (use-modules (ice-9 string-fun))
14
15 \f
16 ;;;; main function, command-line processing
17
18 ;;; The script's entry point.
19 (define (main args)
20 (set-program-name! (car args))
21 (let ((args (cdr args)))
22 (cond
23 ((null? args) (show-help '())
24 (quit 1))
25 ((assoc (car args) command-table)
26 => (lambda (row)
27 (set! subcommand-name (car args))
28 ((cadr row) (cdr args))))
29 (else (show-help '())
30 (quit 1)))))
31
32 (define program-name #f)
33 (define subcommand-name #f)
34 (define program-version "@-GUILE_VERSION-@")
35
36 ;;; Given an executable path PATH, set program-name to something
37 ;;; appropriate f or use in error messages (i.e., with leading
38 ;;; directory names stripped).
39 (define (set-program-name! path)
40 (set! program-name (basename path)))
41
42 (define (show-help args)
43 (cond
44 ((null? args) (show-help-overview))
45 ((assoc (car args) command-table)
46 => (lambda (row) ((caddr row))))
47 (else
48 (show-help-overview))))
49
50 (define (show-help-overview)
51 (let ((dl display-line-error))
52 (dl "Usage: ")
53 (dl " " program-name " link - print libraries to link with")
54 ;; Not yet implemented.
55 ;; (dl " " program-name " main - generate initialization code")
56 (dl " " program-name " info [VAR] - print Guile build directories")
57 (dl " " program-name " --help - show usage info (this message)")
58 (dl " " program-name " --help SUBCOMMAND - show help for SUBCOMMAND")
59 (dl " " program-name " --version - show running version")))
60
61 (define (show-version args)
62 (display-line program-name " - Guile version " program-version))
63
64 \f
65 ;;;; the "link" subcommand
66
67 ;;; Write a set of linker flags to standard output to include the
68 ;;; libraries that libguile needs to link against.
69 ;;;
70 ;;; In the long run, we want to derive these flags from Guile module
71 ;;; declarations files that are installed along the load path. For
72 ;;; now, we're just going to reach into Guile's configuration info and
73 ;;; hack it out.
74 (define (build-link args)
75 (if (> (length args) 0)
76 (error
77 (string-append program-name
78 " link: arguments to subcommand not yet implemented")))
79
80 ;; If PATH has the form FOO/libBAR.a, return the substring
81 ;; BAR, otherwise return #f.
82 (define (match-lib path)
83 (let* ((base (basename path))
84 (len (string-length base)))
85 (if (and (> len 5)
86 (string=? (make-shared-substring base 0 3) "lib")
87 (string=? (make-shared-substring base (- len 2)) ".a"))
88 (make-shared-substring base 3 (- len 2))
89 #f)))
90
91 (let* ((flags
92 (let loop ((libs
93 ;; Get the string of linker flags we used to build
94 ;; Guile, and break it up into a list.
95 (separate-fields-discarding-char #\space
96 (get-build-info 'LIBS)
97 list)))
98
99 (cond
100 ((null? libs) '())
101
102 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
103 ((match-lib (car libs))
104 => (lambda (bar)
105 (cons (string-append "-l" bar)
106 (loop (cdr libs)))))
107
108 ;; Remove any empty strings that may have seeped in there.
109 ((string=? (car libs) "") (loop (cdr libs)))
110
111 (else (cons (car libs) (loop (cdr libs)))))))
112
113 ;; Include libguile itself in the list, along with the
114 ;; directory it was installed in.
115 (flags (cons (string-append "-L" (get-build-info 'libdir))
116 (cons "-lguile" flags))))
117
118 ;; Display the flags, separated by spaces.
119 (display-separated flags)
120 (newline)))
121
122 (define (help-link)
123 (let ((dle display-line-error))
124 (dle "Usage: " program-name " link")
125 (dle "Print linker flags for building the `guile' executable.")
126 (dle "Print the linker command-line flags necessary to link against the")
127 (dle "Guile library, and any other libraries it requires.")))
128
129
130 \f
131 ;;;; The "main" subcommand
132
133 ;;; We haven't implemented this yet, because we don't have the
134 ;;; mechanisms in place to discover the installed static link
135 ;;; libraries. When we do implement this, remember to fix the message
136 ;;; in show-help-overview.
137 (define (build-main args)
138 (display-line-error program-name ": `main' subcommand not yet implemented")
139 (quit 2))
140
141 (define (help-main)
142 (let ((dle display-line-error))
143 (dle "Usage: " program-name " main")
144 (dle "This subcommand is not yet implemented.")))
145
146 \f
147 ;;;; The "info" subcommand
148
149 (define (build-info args)
150 (cond
151 ((null? args) (show-all-vars))
152 ((null? (cdr args)) (show-var (car args)))
153 (else (display-line-error "Usage: " program-name " info [VAR]")
154 (quit 2))))
155
156 (define (show-all-vars)
157 (for-each (lambda (binding)
158 (display-line (car binding) " = " (cdr binding)))
159 %guile-build-info))
160
161 (define (show-var var)
162 (display (get-build-info (string->symbol var)))
163 (newline))
164
165 (define (help-info)
166 (let ((dle display-line-error))
167 (dle "Usage: " program-name " info [VAR]")
168 (dle "Display the value of the Makefile variable VAR used when Guile")
169 (dle "was built. If VAR is omitted, display all Makefile variables.")
170 (dle "Use this command to find out where Guile was installed,")
171 (dle "where it will look for Scheme code at run-time, and so on.")))
172
173
174 \f
175 ;;;; trivial utilities
176
177 (define (get-build-info name)
178 (let ((val (assq name %guile-build-info)))
179 (if (not (pair? val))
180 (begin
181 (display-line-error
182 program-name " " subcommand-name ": no such build-info: " name)
183 (quit 2)))
184 (cdr val)))
185
186 (define (display-line . args)
187 (apply display-line-port (current-output-port) args))
188
189 (define (display-line-error . args)
190 (apply display-line-port (current-error-port) args))
191
192 (define (display-line-port port . args)
193 (for-each (lambda (arg) (display arg port))
194 args)
195 (newline))
196
197 (define (display-separated args)
198 (let loop ((args args))
199 (cond ((null? args))
200 ((null? (cdr args)) (display (car args)))
201 (else (display (car args))
202 (display " ")
203 (loop (cdr args))))))
204
205 \f
206 ;;;; the command table
207
208 ;;; We define this down here, so Guile builds the list after all the
209 ;;; functions have been defined.
210 (define command-table
211 (list
212 (list "--version" show-version show-help-overview)
213 (list "--help" show-help show-help-overview)
214 (list "link" build-link help-link)
215 (list "main" build-main help-main)
216 (list "info" build-info help-info)))
217
218
219
220 \f
221 ;;; Local Variables:
222 ;;; mode: scheme
223 ;;; End: