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