[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

ffi-helper: looking for C libraries to try

From: Matt Wette
Subject: ffi-helper: looking for C libraries to try
Date: Fri, 14 Jul 2017 16:17:20 -0700

Hi All,

I am working on a ffi-helper: a program that will read in a C dot-h file and 
generate a Guile dot-scm file which
defines a module to provide hooks into the associated C library.  My goal is to 
have something to release ~Oct 2017.

I am looking for suggestions for libraries (w/ includes of course) to use as 
test cases to flush-out and debug my code.  I have worked on cairo and now 
working on gdbm.  I tried git2 but those headers are too broken. 

If you have suggestions for test cases, please post.  They should be commonly 
(I use MacPorts and would want them to be installable in that context.)


I have now been able to compile-ffi the following on my Mac. I’m sure bugs 

(define-ffi-module (cairo cairo)
 #:pkg-config "cairo"
 #:include '(“cairo.h" "cairo-pdf.h" "cairo-svg.h")

 ;; the following are bent pipe to scm-module
 #:export (make-cairo-unit-matrix)

(define (make-cairo-unit-matrix)
 (make-cairo_matrix_t #(1.0 0.0 0.0 1.0 0.0 0.0)))

I convert the above “cairo.ffi” file to “cairo.scm” using the following command

        mwette$ guild compile-ffi cairo/cairo.ffi

The above generates 397 FFI declarations in cairo.scm, a file which is about 
6000 lines long, compared to the original “cairo.ffi” which is less than 10 
lines long.

Some generated code:

;; typedef struct _cairo_device cairo_device_t;
(define-fh-pointer-type cairo_device_t*)

;; union _cairo_path_data_t {
;;   struct {
;;     cairo_path_data_type_t type;
;;     int length;
;;   } header;
;;   struct {
;;     double x, y;
;;   } point;
;; };
(define cairo_path_data_t-desc
   (list `(header
               (list `(type ,cairo_path_data_type_t-desc)
                     `(length ,int))))
         `(point ,(bs:struct (list `(y ,double) `(x ,double)))))))
(export cairo_path_data_t-desc)
(define-fh-bytestructure-type/p cairo_path_data_t cairo_path_data_t-desc)
(define union-_cairo_path_data_t cairo_path_data_t)

;; typedef enum _cairo_path_data_type {
;; } cairo_path_data_type_t;
(define-fh-enum-type cairo_path_data_type_t

;; typedef void (*cairo_destroy_func_t)(void *data);
(define (wrap-cairo_destroy_func_t proc) ;; => pointer
(ffi:procedure->pointer ffi:void proc (list '*))
(export wrap-cairo_destroy_func_t)

;; cairo_status_t cairo_device_set_user_data(cairo_device_t *device, const 
;;     cairo_user_data_key_t *key, void *user_data, cairo_destroy_func_t 
;;     destroy);
(define cairo_device_set_user_data
 (let ((~f (ffi:pointer->procedure
             (lib-func "cairo_device_set_user_data")
             (list '* '* '* '*))))
   (lambda (device key user_data destroy)
     (let ((~device (unwrap-cairo_device_t* device))
           (~key (unwrap-cairo_user_data_key_t* key))
           (~user_data (unwrap~pointer user_data))
           (~destroy (unwrap-cairo_destroy_func_t destroy)))
         (~f ~device ~key ~user_data ~destroy))))))
(export cairo_device_set_user_data)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]