4 ;;;; build-guile --- utility for linking programs with Guile
5 ;;;; Jim Blandy <jim@red-bean.com> --- September 1997
7 (use-modules (ice-9 regex)
11 ;;;; main function, command-line processing
13 ;;; The script's entry point.
15 (set-program-name! (car args))
16 (let ((args (cdr args)))
18 ((null? args) (show-help '()))
19 ((assoc (car args) command-table)
21 ((cadr row) (cdr args))))
25 (define program-name #f)
26 (define program-version "@-GUILE_VERSION-@")
28 ;;; Given an executable path PATH, set program-name to something
29 ;;; appropriate f or use in error messages (i.e., with leading
30 ;;; directory names stripped).
31 (define (set-program-name! path)
34 ((string-match "/([^/]+)$" path)
35 => (lambda (match) (match:substring match 1)))
38 (define (show-help args)
40 ((null? args) (show-help-overview))
41 ((assoc (car args) command-table)
42 => (lambda (row) ((caddr row))))
44 (show-help-overview))))
46 (define (show-help-overview)
47 (let ((dl display-line-error))
49 (dl " " program-name " link - print libraries to link with")
50 ;; Not yet implemented.
51 ;; (dl " " program-name " main - generate initialization code")
52 (dl " " program-name " info [VAR] - print Guile build directories")
53 (dl " " program-name " --help - show usage info (this message)")
54 (dl " " program-name " --help SUBCOMMAND - show help for SUBCOMMAND")
55 (dl " " program-name " --version - show running version")))
57 (define (show-version args)
58 (display-line program-name " - Guile version " program-version))
61 ;;;; the "link" subcommand
63 ;;; Write a set of linker flags to standard output to include the
64 ;;; libraries that libguile needs to link against.
66 ;;; In the long run, we want to derive these flags from Guile module
67 ;;; declarations files that are installed along the load path. For
68 ;;; now, we're just going to reach into Guile's configuration info and
70 (define (build-link args)
71 (if (> (length args) 0)
73 (string-append program-name
74 " link: arguments to subcommand not yet implemented")))
78 ;; Get the string of linker flags we used to build
79 ;; Guile, and break it up into a list.
80 (separate-fields-discarding-char #\space
81 (get-build-info 'LIBS)
86 ;; Turn any "FOO/libBAR.a" elements into "-lBAR".
87 ((string-match "^.*/lib([^./]+).a$" (car libs))
89 (cons (string-append "-l" (match:substring match 1))
92 ;; Remove any empty strings that may have seeped in there.
93 ((string=? (car libs) "") (loop (cdr libs)))
95 (else (cons (car libs) (loop (cdr libs)))))))
97 ;; Don't omit -lguile itself from the list of flags.
98 (flags (cons "-lguile" flags)))
100 ;; Display the flags, separated by spaces.
101 (display-separated flags)
105 (let ((dle display-line-error))
106 (dle "Usage: " program-name " link")
107 (dle "Print linker flags for building the `guile' executable.")
108 (dle "Print the linker command-line flags necessary to link against the")
109 (dle "Guile library, and any other libraries it requires.")))
113 (define (get-build-info name)
114 (let ((val (assq name %guile-build-info)))
115 (or val (error "get-build-info: no such build info: " name))
119 ;;;; The "main" subcommand
121 ;;; We haven't implemented this yet, because we don't have the
122 ;;; mechanisms in place to discover the installed static link
123 ;;; libraries. When we do implement this, remember to fix the message
124 ;;; in show-help-overview.
125 (define (build-main args)
126 (display-line-error program-name ": `main' subcommand not yet implemented"))
129 (let ((dle display-line-error))
130 (dle "Usage: " program-name " main")
131 (dle "This subcommand is not yet implemented.")))
134 ;;;; The "info" subcommand
136 (define (build-info args)
138 ((null? args) (show-all-vars))
139 ((null? (cdr args)) (show-var (car args)))
141 (display-line-error "Usage: " program-name " info [VAR]"))))
143 (define (show-all-vars)
144 (for-each (lambda (binding)
145 (display-line (car binding) " = " (cdr binding)))
148 (define (show-var var)
149 (display (get-build-info (string->symbol var)))
153 (let ((dle display-line-error))
154 (dle "Usage: " program-name " info [VAR]")
155 (dle "Display the value of the Makefile variable VAR used when Guile")
156 (dle "was built. If VAR is omitted, display all Makefile variables.")
157 (dle "Use this command to find out where Guile was installed,")
158 (dle "where it will look for Scheme code at run-time, and so on.")))
162 ;;;; trivial utilities
164 (define (display-line . args)
165 (apply display-line-port (current-output-port) args))
167 (define (display-line-error . args)
168 (apply display-line-port (current-error-port) args))
170 (define (display-line-port port . args)
171 (for-each (lambda (arg) (display arg port))
175 (define (display-separated args)
176 (let loop ((args args))
178 ((null? (cdr args)) (display (car args)))
179 (else (display (car args))
181 (loop (cdr args))))))
184 ;;;; the command table
186 ;;; We define this down here, so Guile builds the list after all the
187 ;;; functions have been defined.
188 (define command-table
190 (list "--version" show-version show-help-overview)
191 (list "--help" show-help show-help-overview)
192 (list "link" build-link help-link)
193 (list "main" build-main help-main)
194 (list "info" build-info help-info)))