1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; grovel.lisp --- The CFFI Groveller.
5 ;;; Copyright (C) 2005-2006, Dan Knap <dankna@accela.net>
6 ;;; Copyright (C) 2005-2006, Emily Backes <lucca@accela.net>
7 ;;; Copyright (C) 2007, Stelian Ionescu <sionescu@cddr.org>
8 ;;; Copyright (C) 2007, Luis Oliveira <loliveira@common-lisp.net>
10 ;;; Permission is hereby granted, free of charge, to any person
11 ;;; obtaining a copy of this software and associated documentation
12 ;;; files (the "Software"), to deal in the Software without
13 ;;; restriction, including without limitation the rights to use, copy,
14 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
15 ;;; of the Software, and to permit persons to whom the Software is
16 ;;; furnished to do so, subject to the following conditions:
18 ;;; The above copyright notice and this permission notice shall be
19 ;;; included in all copies or substantial portions of the Software.
21 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
25 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
26 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 ;;; DEALINGS IN THE SOFTWARE.
31 (in-package #:cffi-grovel
)
35 (define-condition grovel-error
(simple-error) ())
37 (defun grovel-error (format-control &rest format-arguments
)
39 :format-control format-control
40 :format-arguments format-arguments
))
42 ;;; This warning is signalled when cffi-grovel can't find some macro.
43 ;;; Signalled by CONSTANT or CONSTANTENUM.
44 (define-condition missing-definition
(warning)
45 ((%name
:initarg
:name
:reader name-of
))
46 (:report
(lambda (condition stream
)
47 (format stream
"No definition for ~A"
48 (name-of condition
)))))
52 ;;; The header of the intermediate C file.
53 (defparameter *header
*
55 * This file has been automatically generated by cffi-grovel.
56 * Do not edit it by hand.
61 ;;; C code generated by cffi-grovel is inserted between the contents
62 ;;; of *PROLOGUE* and *POSTSCRIPT*, inside the main function's body.
64 (defparameter *prologue
*
66 #include <grovel/common.h>
68 int main(int argc, char**argv) {
70 FILE *output = argc > 1 ? fopen(argv[1], \"w\") : stdout;
71 fprintf(output, \";;;; This file has been automatically generated by \"
72 \"cffi-grovel.\\n;;;; Do not edit it by hand.\\n\\n\");
75 (defparameter *postscript
*
83 (defun unescape-for-c (text)
84 (with-output-to-string (result)
85 (loop for i below
(length text
)
86 for char
= (char text i
) do
87 (cond ((eql char
#\") (princ "\\\"" result
))
88 ((eql char
#\newline
) (princ "\\n" result
))
89 (t (princ char result
))))))
91 (defun c-format (out fmt
&rest args
)
92 (let ((text (unescape-for-c (format nil
"~?" fmt args
))))
93 (format out
"~& fputs(\"~A\", output);~%" text
)))
95 (defun c-printf (out fmt
&rest args
)
97 (format out
"~A" (unescape-for-c (format nil item
)))))
98 (format out
"~& fprintf(output, \"")
101 (loop for arg in args do
104 (format out
");~%")))
106 (defun c-print-integer-constant (out arg
&optional foreign-type
)
107 (let ((foreign-type (or foreign-type
:int
)))
108 (c-format out
"#.(cffi-grovel::convert-intmax-constant ")
109 (format out
"~& fprintf(output, \"%\"PRIiMAX, (intmax_t)~A);~%"
112 (c-write out
`(quote ,foreign-type
))
115 ;;; TODO: handle packages in a better way. One way is to process each
116 ;;; grovel form as it is read (like we already do for wrapper
117 ;;; forms). This way in can expect *PACKAGE* to have sane values.
118 ;;; This would require that "header forms" come before any other
120 (defun c-print-symbol (out symbol
&optional no-package
)
122 (let ((package (symbol-package symbol
)))
124 ((eq (find-package '#:keyword
) package
) ":~(~A~)")
125 (no-package "~(~A~)")
126 ((eq (find-package '#:cl
) package
) "cl:~(~A~)")
130 (defun c-write (out form
&optional no-package
)
133 (eq 'quote
(car form
)))
135 (c-write out
(cadr form
) no-package
))
138 (loop for subform in form
139 for first-p
= t then nil
140 unless first-p do
(c-format out
" ")
141 do
(c-write out subform no-package
))
144 (c-print-symbol out form no-package
))))
146 ;;; Always NIL for now, add {ENABLE,DISABLE}-AUTO-EXPORT grovel forms
147 ;;; later, if necessary.
148 (defvar *auto-export
* nil
)
150 (defun c-export (out symbol
)
151 (when (and *auto-export
* (not (keywordp symbol
)))
152 (c-format out
"(cl:export '")
153 (c-print-symbol out symbol t
)
154 (c-format out
")~%")))
156 (defun c-section-header (out section-type section-symbol
)
157 (format out
"~% /* ~A section for ~S */~%"
161 (defun remove-suffix (string suffix
)
162 (let ((suffix-start (- (length string
) (length suffix
))))
163 (if (and (> suffix-start
0)
164 (string= string suffix
:start1 suffix-start
))
165 (subseq string
0 suffix-start
)
168 (defgeneric %process-grovel-form
(name out arguments
)
169 (:method
(name out arguments
)
170 (declare (ignore out arguments
))
171 (grovel-error "Unknown Grovel syntax: ~S" name
)))
173 (defun process-grovel-form (out form
)
174 (%process-grovel-form
(form-kind form
) out
(cdr form
)))
176 (defun form-kind (form)
177 ;; Using INTERN here instead of FIND-SYMBOL will result in less
178 ;; cryptic error messages when an undefined grovel/wrapper form is
180 (intern (symbol-name (car form
)) '#:cffi-grovel
))
182 (defvar *header-forms
* '(c include define flag typedef
))
184 (defun header-form-p (form)
185 (member (form-kind form
) *header-forms
*))
187 (defun generate-c-file (input-file output-defaults
)
189 (with-standard-io-syntax)
190 (let ((c-file (make-c-file-name output-defaults
"__grovel"))
191 (*print-readably
* nil
)
193 (with-open-file (out c-file
:direction
:output
:if-exists
:supersede
))
194 (with-open-file (in input-file
:direction
:input
))
195 (flet ((read-forms (s)
197 (form (read s nil nil
) (read s nil nil
)))
198 ((null form
) (nreverse forms
))
202 (flag (warn "Groveler clause FLAG is deprecated, use CC-FLAGS instead.")))
205 (setf *package
* (find-package (second f
)))
208 ;; flatten progn forms
209 (mapc #'process-form
(rest f
)))
210 (t (push f forms
)))))
211 (process-form form
))))))
212 (let* ((forms (read-forms in
))
213 (header-forms (remove-if-not #'header-form-p forms
))
214 (body-forms (remove-if #'header-form-p forms
)))
215 (write-string *header
* out
)
216 (dolist (form header-forms
)
217 (process-grovel-form out form
))
218 (write-string *prologue
* out
)
219 (dolist (form body-forms
)
220 (process-grovel-form out form
))
221 (write-string *postscript
* out
)
224 (defun tmp-lisp-file-name (defaults)
225 (make-pathname :name
(strcat (pathname-name defaults
) ".grovel-tmp")
226 :type
"lisp" :defaults defaults
))
230 ;;; *PACKAGE* is rebound so that the IN-PACKAGE form can set it during
231 ;;; *the extent of a given grovel file.
232 (defun process-grovel-file (input-file &optional
(output-defaults input-file
))
233 (with-standard-io-syntax
234 (let* ((c-file (generate-c-file input-file output-defaults
))
235 (o-file (make-o-file-name c-file
))
236 (exe-file (make-exe-file-name c-file
))
237 (lisp-file (tmp-lisp-file-name c-file
))
238 (inputs (list (cc-include-grovel-argument) c-file
)))
241 ;; at least MKCL wants to separate compile and link
242 (cc-compile o-file inputs
)
243 (link-executable exe-file
(list o-file
)))
245 (grovel-error "~a" e
)))
246 (invoke exe-file lisp-file
)
249 ;;; OUT is lexically bound to the output stream within BODY.
250 (defmacro define-grovel-syntax
(name lambda-list
&body body
)
251 (with-unique-names (name-var args
)
252 `(defmethod %process-grovel-form
((,name-var
(eql ',name
)) out
,args
)
253 (declare (ignorable out
))
254 (destructuring-bind ,lambda-list
,args
257 (define-grovel-syntax c
(body)
258 (format out
"~%~A~%" body
))
260 (define-grovel-syntax include
(&rest includes
)
261 (format out
"~{#include <~A>~%~}" includes
))
263 (define-grovel-syntax define
(name &optional value
)
264 (format out
"#define ~A~@[ ~A~]~%" name value
))
266 (define-grovel-syntax typedef
(base-type new-type
)
267 (format out
"typedef ~A ~A;~%" base-type new-type
))
269 ;;; Is this really needed?
270 (define-grovel-syntax ffi-typedef
(new-type base-type
)
271 (c-format out
"(cffi:defctype ~S ~S)~%" new-type base-type
))
273 (define-grovel-syntax flag
(&rest flags
)
274 (appendf *cc-flags
* (parse-command-flags-list flags
)))
276 (define-grovel-syntax cc-flags
(&rest flags
)
277 (appendf *cc-flags
* (parse-command-flags-list flags
)))
279 (define-grovel-syntax pkg-config-cflags
(pkg &key optional
)
280 (let ((output-stream (make-string-output-stream))
281 (program+args
(list "pkg-config" pkg
"--cflags")))
282 (format *debug-io
* "~&;~{ ~a~}~%" program
+args
)
285 (run-program program
+args
286 :output
(make-broadcast-stream output-stream
*debug-io
*)
287 :error-output output-stream
)
289 (parse-command-flags (get-output-stream-string output-stream
))))
291 (let ((message (format nil
"~a~&~%~a~&"
292 e
(get-output-stream-string output-stream
))))
294 (format *debug-io
* "~&; ERROR: ~a" message
)
295 (format *debug-io
* "~&~%; Attempting to continue anyway.~%"))
297 (grovel-error "~a" message
))))))))
299 ;;; This form also has some "read time" effects. See GENERATE-C-FILE.
300 (define-grovel-syntax in-package
(name)
301 (c-format out
"(cl:in-package #:~A)~%~%" name
))
303 (define-grovel-syntax ctype
(lisp-name size-designator
)
304 (c-section-header out
"ctype" lisp-name
)
305 (c-export out lisp-name
)
306 (c-format out
"(cffi:defctype ")
307 (c-print-symbol out lisp-name t
)
309 (format out
"~& type_name(output, TYPE_SIGNED_P(~A), ~:[sizeof(~A)~;~D~]);~%"
311 (etypecase size-designator
316 (unless (keywordp lisp-name
)
317 (c-export out lisp-name
))
318 (let ((size-of-constant-name (symbolicate '#:size-of- lisp-name
)))
319 (c-export out size-of-constant-name
)
320 (c-format out
"(cl:defconstant "
321 size-of-constant-name lisp-name
)
322 (c-print-symbol out size-of-constant-name
)
323 (c-format out
" (cffi:foreign-type-size '")
324 (c-print-symbol out lisp-name
)
325 (c-format out
"))~%")))
327 ;;; Syntax differs from anything else in CFFI. Fix?
328 (define-grovel-syntax constant
((lisp-name &rest c-names
)
329 &key
(type 'integer
) documentation optional
)
330 (when (keywordp lisp-name
)
331 (setf lisp-name
(format-symbol "~A" lisp-name
)))
332 (c-section-header out
"constant" lisp-name
)
333 (dolist (c-name c-names
)
334 (format out
"~&#ifdef ~A~%" c-name
)
335 (c-export out lisp-name
)
336 (c-format out
"(cl:defconstant ")
337 (c-print-symbol out lisp-name t
)
341 (format out
"~& if(_64_BIT_VALUE_FITS_SIGNED_P(~A))~%" c-name
)
342 (format out
" fprintf(output, \"%lli\", (long long signed) ~A);" c-name
)
343 (format out
"~& else~%")
344 (format out
" fprintf(output, \"%llu\", (long long unsigned) ~A);" c-name
))
346 (format out
"~& fprintf(output, \"%s\", print_double_for_lisp((double)~A));~%" c-name
)))
348 (c-format out
" ~S" documentation
))
350 (format out
"~&#else~%"))
352 (c-format out
"(cl:warn 'cffi-grovel:missing-definition :name '~A)~%"
354 (dotimes (i (length c-names
))
355 (format out
"~&#endif~%")))
357 (define-grovel-syntax cunion
(union-lisp-name union-c-name
&rest slots
)
358 (let ((documentation (when (stringp (car slots
)) (pop slots
))))
359 (c-section-header out
"cunion" union-lisp-name
)
360 (c-export out union-lisp-name
)
362 (let ((slot-lisp-name (car slot
)))
363 (c-export out slot-lisp-name
)))
364 (c-format out
"(cffi:defcunion (")
365 (c-print-symbol out union-lisp-name t
)
366 (c-printf out
" :size %llu)" (format nil
"(long long unsigned) sizeof(~A)" union-c-name
))
368 (c-format out
"~% ~S" documentation
))
370 (destructuring-bind (slot-lisp-name slot-c-name
&key type count
)
372 (declare (ignore slot-c-name
))
373 (c-format out
"~% (")
374 (c-print-symbol out slot-lisp-name t
)
379 (c-format out
" :count ~D" count
))
381 ;; nb, works like :count :auto does in cstruct below
382 (c-printf out
" :count %llu"
383 (format nil
"(long long unsigned) sizeof(~A)" union-c-name
)))
386 (c-format out
")~%")))
388 (defun make-from-pointer-function-name (type-name)
389 (symbolicate '#:make- type-name
'#:-from-pointer
))
391 ;;; DEFINE-C-STRUCT-WRAPPER (in ../src/types.lisp) seems like a much
392 ;;; cleaner way to do this. Unless I can find any advantage in doing
393 ;;; it this way I'll delete this soon. --luis
394 (define-grovel-syntax cstruct-and-class-item
(&rest arguments
)
395 (process-grovel-form out
(cons 'cstruct arguments
))
396 (destructuring-bind (struct-lisp-name struct-c-name
&rest slots
)
398 (declare (ignore struct-c-name
))
399 (let* ((slot-names (mapcar #'car slots
))
400 (reader-names (mapcar
403 (strcat (symbol-name struct-lisp-name
) "-"
404 (symbol-name slot-name
))))
406 (initarg-names (mapcar
408 (intern (symbol-name slot-name
) "KEYWORD"))
410 (slot-decoders (mapcar (lambda (slot)
416 (declare (ignore lisp-name c-name
))
417 (cond ((and (eq type
:char
) count
)
418 'cffi
:foreign-string-to-lisp
)
422 `(defclass ,struct-lisp-name
()
423 ,(mapcar (lambda (slot-name initarg-name reader-name
)
424 `(,slot-name
:initarg
,initarg-name
425 :reader
,reader-name
))
430 (make-from-pointer-function-name struct-lisp-name
))
432 ;; this function is then used as a constructor for this class.
433 `(defun ,make-function-name
(pointer)
434 (cffi:with-foreign-slots
435 (,slot-names pointer
,struct-lisp-name
)
436 (make-instance ',struct-lisp-name
437 ,@(loop for slot-name in slot-names
438 for initarg-name in initarg-names
439 for slot-decoder in slot-decoders
442 collect
`(,slot-decoder
,slot-name
)
443 else collect slot-name
))))))
444 (c-export out make-function-name
)
445 (dolist (reader-name reader-names
)
446 (c-export out reader-name
))
447 (c-write out defclass-form
)
448 (c-write out make-defun-form
))))
450 (define-grovel-syntax cstruct
(struct-lisp-name struct-c-name
&rest slots
)
451 (let ((documentation (when (stringp (car slots
)) (pop slots
))))
452 (c-section-header out
"cstruct" struct-lisp-name
)
453 (c-export out struct-lisp-name
)
455 (let ((slot-lisp-name (car slot
)))
456 (c-export out slot-lisp-name
)))
457 (c-format out
"(cffi:defcstruct (")
458 (c-print-symbol out struct-lisp-name t
)
459 (c-printf out
" :size %llu)"
460 (format nil
"(long long unsigned) sizeof(~A)" struct-c-name
))
462 (c-format out
"~% ~S" documentation
))
464 (destructuring-bind (slot-lisp-name slot-c-name
&key type count
)
466 (c-format out
"~% (")
467 (c-print-symbol out slot-lisp-name t
)
471 (format out
"~& SLOT_SIGNED_P(autotype_tmp, ~A, ~A~@[[0]~]);~@*~%~
472 ~& type_name(output, autotype_tmp, sizeofslot(~A, ~A~@[[0]~]));~%"
479 (c-format out
"~A" type
)))
483 (c-format out
" :count ~D" count
))
485 (c-printf out
" :count %llu"
486 (format nil
"(long long unsigned) countofslot(~A, ~A)"
490 (format out
"~&#ifdef ~A~%" count
)
491 (c-printf out
" :count %llu"
492 (format nil
"(long long unsigned) (~A)" count
))
493 (format out
"~&#endif~%")))
494 (c-printf out
" :offset %lli)"
495 (format nil
"(long long signed) offsetof(~A, ~A)"
499 (let ((size-of-constant-name
500 (symbolicate '#:size-of- struct-lisp-name
)))
501 (c-export out size-of-constant-name
)
502 (c-format out
"(cl:defconstant "
503 size-of-constant-name struct-lisp-name
)
504 (c-print-symbol out size-of-constant-name
)
505 (c-format out
" (cffi:foreign-type-size '(:struct ")
506 (c-print-symbol out struct-lisp-name
)
507 (c-format out
")))~%"))))
509 (defmacro define-pseudo-cvar
(str name type
&key read-only
)
510 (let ((c-parse (let ((*read-eval
* nil
)
511 (*readtable
* (copy-readtable nil
)))
512 (setf (readtable-case *readtable
*) :preserve
)
513 (read-from-string str
))))
515 (symbol `(cffi:defcvar
(,(symbol-name c-parse
) ,name
516 :read-only
,read-only
)
518 (list (unless (and (= (length c-parse
) 2)
519 (null (second c-parse
))
520 (symbolp (first c-parse
))
521 (eql #\
* (char (symbol-name (first c-parse
)) 0)))
522 (grovel-error "Unable to parse c-string ~s." str
))
523 (let ((func-name (symbolicate "%" name
'#:-accessor
)))
525 (declaim (inline ,func-name
))
526 (cffi:defcfun
(,(string-trim "*" (symbol-name (first c-parse
)))
527 ,func-name
) :pointer
)
528 (define-symbol-macro ,name
529 (cffi:mem-ref
(,func-name
) ',type
)))))
530 (t (grovel-error "Unable to parse c-string ~s." str
)))))
532 (defun foreign-name-to-symbol (s)
533 (intern (substitute #\-
#\_
(string-upcase s
))))
535 (defun choose-lisp-and-foreign-names (string-or-list)
536 (etypecase string-or-list
537 (string (values string-or-list
(foreign-name-to-symbol string-or-list
)))
538 (list (destructuring-bind (fname lname
&rest args
) string-or-list
539 (declare (ignore args
))
540 (assert (and (stringp fname
) (symbolp lname
)))
541 (values fname lname
)))))
543 (define-grovel-syntax cvar
(name type
&key read-only
)
544 (multiple-value-bind (c-name lisp-name
)
545 (choose-lisp-and-foreign-names name
)
546 (c-section-header out
"cvar" lisp-name
)
547 (c-export out lisp-name
)
548 (c-printf out
"(cffi-grovel::define-pseudo-cvar \"%s\" "
549 (format nil
"indirect_stringify(~A)" c-name
))
550 (c-print-symbol out lisp-name t
)
554 (c-format out
" :read-only t"))
555 (c-format out
")~%")))
557 ;;; FIXME: where would docs on enum elements go?
558 (define-grovel-syntax cenum
(name &rest enum-list
)
559 (destructuring-bind (name &key base-type define-constants
)
561 (c-section-header out
"cenum" name
)
563 (c-format out
"(cffi:defcenum (")
564 (c-print-symbol out name t
)
567 (c-print-symbol out base-type t
))
569 (dolist (enum enum-list
)
570 (destructuring-bind ((lisp-name &rest c-names
) &key documentation
)
572 (declare (ignore documentation
))
573 (check-type lisp-name keyword
)
574 (loop for c-name in c-names do
575 (check-type c-name string
)
577 (c-print-symbol out lisp-name
)
579 (c-print-integer-constant out c-name base-type
)
580 (c-format out
")~%"))))
582 (when define-constants
583 (define-constants-from-enum out enum-list
))))
585 (define-grovel-syntax constantenum
(name &rest enum-list
)
586 (destructuring-bind (name &key base-type define-constants
)
588 (c-section-header out
"constantenum" name
)
590 (c-format out
"(cffi:defcenum (")
591 (c-print-symbol out name t
)
594 (c-print-symbol out base-type t
))
596 (dolist (enum enum-list
)
597 (destructuring-bind ((lisp-name &rest c-names
)
598 &key optional documentation
) enum
599 (declare (ignore documentation
))
600 (check-type lisp-name keyword
)
601 (c-format out
"~% (")
602 (c-print-symbol out lisp-name
)
603 (loop for c-name in c-names do
604 (check-type c-name string
)
605 (format out
"~&#ifdef ~A~%" c-name
)
607 (c-print-integer-constant out c-name base-type
)
608 (format out
"~&#else~%"))
612 (cl:warn 'cffi-grovel:missing-definition :name '~A) ~
615 (dotimes (i (length c-names
))
616 (format out
"~&#endif~%"))
619 (when define-constants
620 (define-constants-from-enum out enum-list
))))
622 (defun define-constants-from-enum (out enum-list
)
623 (dolist (enum enum-list
)
624 (destructuring-bind ((lisp-name &rest c-names
) &rest options
)
626 (%process-grovel-form
628 `((,(intern (string lisp-name
)) ,(car c-names
))
631 (defun convert-intmax-constant (constant base-type
)
632 "Convert the C CONSTANT to an integer of BASE-TYPE. The constant is
633 assumed to be an integer printed using the PRIiMAX printf(3) format
635 ;; | C Constant | Type | Return Value | Notes |
636 ;; |------------+---------+--------------+---------------------------------------|
637 ;; | -1 | :int32 | -1 | |
638 ;; | 0xffffffff | :int32 | -1 | CONSTANT may be a positive integer if |
639 ;; | | | | sizeof(intmax_t) > sizeof(int32_t) |
640 ;; | 0xffffffff | :uint32 | 4294967295 | |
641 ;; | -1 | :uint32 | 4294967295 | |
642 ;; |------------+---------+--------------+---------------------------------------|
643 (let* ((canonical-type (cffi::canonicalize-foreign-type base-type
))
644 (type-bits (* 8 (cffi:foreign-type-size canonical-type
)))
645 (2^n
(ash 1 type-bits
)))
646 (ecase canonical-type
647 ((:unsigned-char
:unsigned-short
:unsigned-int
648 :unsigned-long
:unsigned-long-long
)
650 ((:char
:short
:int
:long
:long-long
)
651 (let ((v (mod constant
2^n
)))
652 (if (logbitp (1- type-bits
) v
)
653 (- (mask-field (byte (1- type-bits
) 0) v
)
654 (ash 1 (1- type-bits
)))
657 (defun foreign-type-to-printf-specification (type)
658 "Return the printf specification associated with the foreign type TYPE."
659 (ecase (cffi::canonicalize-foreign-type type
)
661 (:unsigned-char
"\"%hhu\"")
663 (:unsigned-short
"\"%hu\"")
665 (:unsigned-int
"\"%u\"")
667 (:unsigned-long
"\"%lu\"")
668 (:long-long
"\"%lld\"")
669 (:unsigned-long-long
"\"%llu\"")))
671 ;; Defines a bitfield, with elements specified as ((LISP-NAME C-NAME)
672 ;; &key DOCUMENTATION). NAME-AND-OPTS can be either a symbol as name,
673 ;; or a list (NAME &key BASE-TYPE).
674 (define-grovel-syntax bitfield
(name-and-opts &rest masks
)
675 (destructuring-bind (name &key base-type
)
676 (ensure-list name-and-opts
)
677 (c-section-header out
"bitfield" name
)
679 (c-format out
"(cffi:defbitfield (")
680 (c-print-symbol out name t
)
683 (c-print-symbol out base-type t
))
686 (destructuring-bind ((lisp-name &rest c-names
)
687 &key optional documentation
) mask
688 (declare (ignore documentation
))
689 (check-type lisp-name symbol
)
690 (c-format out
"~% (")
691 (c-print-symbol out lisp-name
)
693 (dolist (c-name c-names
)
694 (check-type c-name string
)
695 (format out
"~&#ifdef ~A~%" c-name
)
696 (format out
"~& fprintf(output, ~A, ~A);~%"
697 (foreign-type-to-printf-specification (or base-type
:int
))
699 (format out
"~&#else~%"))
703 (cl:warn 'cffi-grovel:missing-definition :name '~A) ~
706 (dotimes (i (length c-names
))
707 (format out
"~&#endif~%"))
709 (c-format out
")~%")))
712 ;;;# Wrapper Generation
714 ;;; Here we generate a C file from a s-exp specification but instead
715 ;;; of compiling and running it, we compile it as a shared library
716 ;;; that can be subsequently loaded with LOAD-FOREIGN-LIBRARY.
718 ;;; Useful to get at macro functionality, errno, system calls,
719 ;;; functions that handle structures by value, etc...
721 ;;; Matching CFFI bindings are generated along with said C file.
723 (defun process-wrapper-form (out form
)
724 (%process-wrapper-form
(form-kind form
) out
(cdr form
)))
726 ;;; The various operators push Lisp forms onto this list which will be
727 ;;; written out by PROCESS-WRAPPER-FILE once everything is processed.
728 (defvar *lisp-forms
*)
730 (defun generate-c-lib-file (input-file output-defaults
)
731 (let ((*lisp-forms
* nil
)
732 (c-file (make-c-file-name output-defaults
"__wrapper")))
733 (with-open-file (out c-file
:direction
:output
:if-exists
:supersede
)
734 (with-open-file (in input-file
:direction
:input
)
735 (write-string *header
* out
)
736 (loop for form
= (read in nil nil
) while form
737 do
(process-wrapper-form out form
))))
738 (values c-file
(nreverse *lisp-forms
*))))
740 (defun make-soname (lib-soname output-defaults
)
741 (make-pathname :name lib-soname
742 :defaults output-defaults
))
744 (defun generate-bindings-file (lib-file lib-soname lisp-forms output-defaults
)
745 (with-standard-io-syntax
746 (let ((lisp-file (tmp-lisp-file-name output-defaults
))
747 (*print-readably
* nil
)
749 (with-open-file (out lisp-file
:direction
:output
:if-exists
:supersede
)
750 (format out
";;;; This file was automatically generated by cffi-grovel.~%~
751 ;;;; Do not edit by hand.~%")
752 (let ((*package
* (find-package '#:cl
))
754 (let ((*package
* (find-package :keyword
))
756 (read-from-string lib-soname
))))
758 (cffi:define-foreign-library
760 :type
:grovel-wrapper
761 :search-path
,(directory-namestring lib-file
))
762 (t ,(namestring (make-so-file-name lib-soname
))))
763 (cffi:use-foreign-library
,named-library-name
))
766 (dolist (form lisp-forms
)
771 (defun cc-include-grovel-argument ()
772 (format nil
"-I~A" (truename (system-source-directory :cffi-grovel
))))
774 ;;; *PACKAGE* is rebound so that the IN-PACKAGE form can set it during
775 ;;; *the extent of a given wrapper file.
776 (defun process-wrapper-file (input-file
778 (output-defaults (make-pathname :defaults input-file
:type
"processed"))
780 (with-standard-io-syntax
781 (multiple-value-bind (c-file lisp-forms
)
782 (generate-c-lib-file input-file output-defaults
)
783 (let ((lib-file (make-so-file-name (make-soname lib-soname output-defaults
)))
784 (o-file (make-o-file-name output-defaults
"__wrapper")))
785 (cc-compile o-file
(list (cc-include-grovel-argument) c-file
))
786 (link-shared-library lib-file
(list o-file
))
787 ;; FIXME: hardcoded library path.
788 (values (generate-bindings-file lib-file lib-soname lisp-forms output-defaults
)
791 (defgeneric %process-wrapper-form
(name out arguments
)
792 (:method
(name out arguments
)
793 (declare (ignore out arguments
))
794 (grovel-error "Unknown Grovel syntax: ~S" name
)))
796 ;;; OUT is lexically bound to the output stream within BODY.
797 (defmacro define-wrapper-syntax
(name lambda-list
&body body
)
798 (with-unique-names (name-var args
)
799 `(defmethod %process-wrapper-form
((,name-var
(eql ',name
)) out
,args
)
800 (declare (ignorable out
))
801 (destructuring-bind ,lambda-list
,args
804 (define-wrapper-syntax progn
(&rest forms
)
806 (process-wrapper-form out form
)))
808 (define-wrapper-syntax in-package
(name)
809 (assert (find-package name
) (name)
810 "Wrapper file specified (in-package ~s)~%~
811 however that does not name a known package."
813 (setq *package
* (find-package name
))
814 (push `(in-package ,name
) *lisp-forms
*))
816 (define-wrapper-syntax c
(&rest strings
)
817 (dolist (string strings
)
818 (write-line string out
)))
820 (define-wrapper-syntax flag
(&rest flags
)
821 (appendf *cc-flags
* (parse-command-flags-list flags
)))
823 (define-wrapper-syntax proclaim
(&rest proclamations
)
824 (push `(proclaim ,@proclamations
) *lisp-forms
*))
826 (define-wrapper-syntax declaim
(&rest declamations
)
827 (push `(declaim ,@declamations
) *lisp-forms
*))
829 (define-wrapper-syntax define
(name &optional value
)
830 (format out
"#define ~A~@[ ~A~]~%" name value
))
832 (define-wrapper-syntax include
(&rest includes
)
833 (format out
"~{#include <~A>~%~}" includes
))
835 ;;; FIXME: this function is not complete. Should probably follow
836 ;;; typedefs? Should definitely understand pointer types.
837 (defun c-type-name (typespec)
838 (let ((spec (ensure-list typespec
)))
839 (if (stringp (car spec
))
842 ((:uchar
:unsigned-char
) "unsigned char")
843 ((:unsigned-short
:ushort
) "unsigned short")
844 ((:unsigned-int
:uint
) "unsigned int")
845 ((:unsigned-long
:ulong
) "unsigned long")
846 ((:long-long
:llong
) "long long")
847 ((:unsigned-long-long
:ullong
) "unsigned long long")
850 (t (cffi::foreign-name
(car spec
) nil
))))))
852 (defun cffi-type (typespec)
853 (if (and (listp typespec
) (stringp (car typespec
)))
858 (check-type s
(and symbol
(not null
)))
861 (define-wrapper-syntax defwrapper
(name-and-options rettype
&rest args
)
862 (multiple-value-bind (lisp-name foreign-name options
)
863 (cffi::parse-name-and-options name-and-options
)
864 (let* ((foreign-name-wrap (strcat foreign-name
"_cffi_wrap"))
865 (fargs (mapcar (lambda (arg)
866 (list (c-type-name (second arg
))
867 (cffi::foreign-name
(first arg
) nil
)))
869 (fargnames (mapcar #'second fargs
)))
871 (format out
"~A ~A" (c-type-name rettype
) foreign-name-wrap
)
872 (format out
"(~{~{~A ~A~}~^, ~})~%" fargs
)
873 (format out
"{~% return ~A(~{~A~^, ~});~%}~%~%" foreign-name fargnames
)
875 (push `(cffi:defcfun
(,foreign-name-wrap
,lisp-name
,@options
)
877 ,@(mapcar (lambda (arg)
878 (list (symbol* (first arg
))
879 (cffi-type (second arg
))))
883 (define-wrapper-syntax defwrapper
* (name-and-options rettype args
&rest c-lines
)
885 (multiple-value-bind (lisp-name foreign-name options
)
886 (cffi::parse-name-and-options name-and-options
)
887 (let ((foreign-name-wrap (strcat foreign-name
"_cffi_wrap"))
888 (fargs (mapcar (lambda (arg)
889 (list (c-type-name (second arg
))
890 (cffi::foreign-name
(first arg
) nil
)))
892 (format out
"~A ~A" (c-type-name rettype
)
894 (format out
"(~{~{~A ~A~}~^, ~})~%" fargs
)
895 (format out
"{~%~{ ~A~%~}}~%~%" c-lines
)
897 (push `(cffi:defcfun
(,foreign-name-wrap
,lisp-name
,@options
)
899 ,@(mapcar (lambda (arg)
900 (list (symbol* (first arg
))
901 (cffi-type (second arg
))))