1 ;;; scan-api --- Scan and group interpreter and libguile interface elements
3 ;; Copyright (C) 2002, 2006, 2011 Free Software Foundation, Inc.
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public License
7 ;; as published by the Free Software Foundation; either version 3, or
8 ;; (at your option) any later version.
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this software; see the file COPYING.LESSER. If
17 ;; not, write to the Free Software Foundation, Inc., 51 Franklin
18 ;; Street, Fifth Floor, Boston, MA 02110-1301 USA
20 ;;; Author: Thien-Thi Nguyen <ttn@gnu.org>
24 ;; Usage: scan-api GUILE SOFILE [GROUPINGS ...]
26 ;; Invoke GUILE, an executable guile interpreter, and use nm(1) on SOFILE, a
27 ;; shared-object library, to determine available interface elements, and
28 ;; display them to stdout as an alist:
30 ;; ((meta ...) (interface ...))
32 ;; The meta fields are `GUILE_LOAD_PATH', `LTDL_LIBRARY_PATH', `guile'
33 ;; `libguileinterface', `sofile' and `groups'. The interface elements are in
34 ;; turn sub-alists w/ keys `groups' and `scan-data'. Interface elements
35 ;; initially belong in one of two groups `Scheme' or `C' (but not both --
36 ;; signal error if that happens).
38 ;; Optional GROUPINGS ... are files each containing a single "grouping
39 ;; definition" alist with each entry of the form:
41 ;; (NAME (description "DESCRIPTION") (members SYM...))
43 ;; All of the SYM... should be proper subsets of the interface. In addition
44 ;; to `description' and `members' forms, the entry may optionally include:
46 ;; (grok USE-MODULES (lambda (x) CODE))
48 ;; where CODE implements a group-membership predicate to be applied to `x', a
49 ;; symbol. [When evaluated, CODE can assume (use-modules MODULE) has been
50 ;; executed where MODULE is an element of USE-MODULES, a list. [NOT YET
53 ;; Currently, there are two convenience predicates that operate on `x':
54 ;; (in-group? x GROUP)
55 ;; (name-prefix? x PREFIX)
57 ;; TODO: Allow for concurrent Scheme/C membership.
58 ;; Completely separate reporting.
62 (define-module (scripts scan-api)
63 :use-module (ice-9 popen)
64 :use-module (ice-9 rdelim)
65 :use-module (ice-9 regex)
68 (define %include-in-guild-list #f)
69 (define %summary "Generate an API description for a Guile extension.")
71 (define put set-object-property!)
72 (define get object-property)
74 (define (add-props object . args)
75 (let loop ((args args))
78 (let ((key (car args))
80 (put object key value)
81 (loop (cddr args))))))
83 (define (scan re command match)
84 (let ((rx (make-regexp re))
85 (port (open-pipe command OPEN_READ)))
86 (let loop ((line (read-line port)))
87 (or (eof-object? line)
89 (cond ((regexp-exec rx line) => match))
90 (loop (read-line port)))))))
92 (define (scan-Scheme! ht guile)
93 (scan "^.guile.+: ([^ \t]+)([ \t]+(.+))*$"
94 (format #f "~A -c '~S ~S'"
96 '(use-modules (ice-9 session))
99 (let ((x (string->symbol (match:substring m 1))))
100 (put x 'Scheme (or (match:substring m 3)
102 (hashq-set! ht x #t)))))
104 (define (scan-C! ht sofile)
105 (scan "^[0-9a-fA-F]+ ([B-TV-Z]) (.+)$"
106 (format #f "nm ~A" sofile)
108 (let ((x (string->symbol (match:substring m 2))))
109 (put x 'C (string->symbol (match:substring m 1)))
110 (and (hashq-get-handle ht x)
111 (error "both Scheme and C:" x))
112 (hashq-set! ht x #t)))))
114 (define THIS-MODULE (current-module))
116 (define (in-group? x group)
117 (memq group (get x 'groups)))
119 (define (name-prefix? x prefix)
120 (string-match (string-append "^" prefix) (symbol->string x)))
122 (define (add-group-name! x name)
123 (put x 'groups (cons name (get x 'groups))))
125 (define (make-grok-proc name form)
126 (let* ((predicate? (eval form THIS-MODULE))
129 (add-group-name! x name)))))
133 (define (make-members-proc name members)
135 (and (memq x members)
136 (add-group-name! x name)))))
140 (define (make-grouper files) ; \/^^^o/ . o
141 (let ((hook (make-hook 1))) ; /\____\
146 (let ((name (car gdef))
147 (members (assq-ref gdef 'members))
148 (grok (assq-ref gdef 'grok)))
150 (error "bad grouping, must have `members' or `grok'"))
153 (add-props (make-grok-proc name (cadr grok))
155 (assq-ref gdef 'description))
156 (make-members-proc name members))
158 (read (open-file file OPEN_READ))))
162 (define (scan-api . args)
163 (let ((guile (list-ref args 0))
164 (sofile (list-ref args 1))
165 (grouper (false-if-exception (make-grouper (cddr args))))
166 (ht (make-hash-table 3331)))
167 (scan-Scheme! ht guile)
169 (let ((all (sort (hash-fold (lambda (key value prior-result)
172 'string (symbol->string key)
173 'scan-data (or (get key 'Scheme)
175 'groups (if (get key 'Scheme)
178 (and grouper (run-hook grouper key))
179 (cons key prior-result))
183 (string<? (get a 'string)
185 (format #t ";;; generated by scan-api -- do not edit!\n\n")
187 (format #t "(meta\n")
188 (format #t " (GUILE_LOAD_PATH . ~S)\n"
189 (or (getenv "GUILE_LOAD_PATH") ""))
190 (format #t " (LTDL_LIBRARY_PATH . ~S)\n"
191 (or (getenv "LTDL_LIBRARY_PATH") ""))
192 (format #t " (guile . ~S)\n" guile)
193 (format #t " (libguileinterface . ~S)\n"
196 (format #f "~A -c '(display ~A)'"
198 '(assq-ref %guile-build-info
200 (lambda (m) (set! i (match:substring m 1))))
202 (format #t " (sofile . ~S)\n" sofile)
204 (cons 'groups (append (if grouper
205 (map (lambda (p) (get p 'name))
206 (hook->list grouper))
209 (format #t ") ;; end of meta\n")
210 (format #t "(interface\n")
211 (for-each (lambda (x)
212 (format #t "(~A ~A (scan-data ~S))\n"
214 (cons 'groups (get x 'groups))
217 (format #t ") ;; end of interface\n")
218 (format #t ") ;; eof\n")))
221 (define main scan-api)
223 ;;; scan-api ends here