4 ;;;; guile-config --- utility for linking programs with Guile
5 ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
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.
13 (use-modules (ice-9 string-fun))
16 ;;;; main function, command-line processing
18 ;;; The script's entry point.
20 (set-program-name! (car args))
21 (let ((args (cdr args)))
23 ((null? args) (show-help '())
25 ((assoc (car args) command-table)
27 (set! subcommand-name (car args))
28 ((cadr row) (cdr args))))
32 (define program-name #f)
33 (define subcommand-name #f)
34 (define program-version "@-GUILE_VERSION-@")
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)))
42 (define (show-help args)
44 ((null? args) (show-help-overview))
45 ((assoc (car args) command-table)
46 => (lambda (row) ((caddr row))))
48 (show-help-overview))))
50 (define (show-help-overview)
51 (let ((dl display-line-error))
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")))
61 (define (show-version args)
62 (display-line program-name " - Guile version " program-version))
65 ;;;; the "link" subcommand
67 ;;; Write a set of linker flags to standard output to include the
68 ;;; libraries that libguile needs to link against.
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
74 (define (build-link args)
75 (if (> (length args) 0)
77 (string-append program-name
78 " link: arguments to subcommand not yet implemented")))
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)))
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))
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)
102 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
103 ((match-lib (car libs))
105 (cons (string-append "-l" bar)
108 ;; Remove any empty strings that may have seeped in there.
109 ((string=? (car libs) "") (loop (cdr libs)))
111 (else (cons (car libs) (loop (cdr libs)))))))
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))))
118 ;; Display the flags, separated by spaces.
119 (display-separated flags)
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.")))
131 ;;;; The "main" subcommand
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")
142 (let ((dle display-line-error))
143 (dle "Usage: " program-name " main")
144 (dle "This subcommand is not yet implemented.")))
147 ;;;; The "info" subcommand
149 (define (build-info args)
151 ((null? args) (show-all-vars))
152 ((null? (cdr args)) (show-var (car args)))
153 (else (display-line-error "Usage: " program-name " info [VAR]")
156 (define (show-all-vars)
157 (for-each (lambda (binding)
158 (display-line (car binding) " = " (cdr binding)))
161 (define (show-var var)
162 (display (get-build-info (string->symbol var)))
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.")))
175 ;;;; trivial utilities
177 (define (get-build-info name)
178 (let ((val (assq name %guile-build-info)))
179 (if (not (pair? val))
182 program-name " " subcommand-name ": no such build-info: " name)
186 (define (display-line . args)
187 (apply display-line-port (current-output-port) args))
189 (define (display-line-error . args)
190 (apply display-line-port (current-error-port) args))
192 (define (display-line-port port . args)
193 (for-each (lambda (arg) (display arg port))
197 (define (display-separated args)
198 (let loop ((args args))
200 ((null? (cdr args)) (display (car args)))
201 (else (display (car args))
203 (loop (cdr args))))))
206 ;;;; the command table
208 ;;; We define this down here, so Guile builds the list after all the
209 ;;; functions have been defined.
210 (define command-table
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)))