[Top][All Lists]

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

Re: #define SOMETHING some_value

From: Matt Wette
Subject: Re: #define SOMETHING some_value
Date: Sun, 11 Jun 2017 13:07:20 -0700

> On Jun 11, 2017, at 12:42 PM, Mark H Weaver <address@hidden> wrote:
> Amirouche Boubekki <address@hidden> writes:
>> What I do is that I hardcode the define in scheme using simple define form
>> for instance, the following:
>> #define SOMETHING some_value
>> Becomes:
>> (define SOMETHING some_value)
> Right.  Unfortunately, preprocessor macros are replaced with their
> right-hand-sides in the first phase of C compilation (preprocessing),
> and then forgotten.  These macros are not stored in the shared objects,
> so it's simply not possible for us to retrieve them.
> For this reason, I'm sorry to say that the association between SOMETHING
> and some_value must be redundantly represented in your Guile bindings,
> as Amirouche describes above.
> It would be nice to have a tool to extract this information from .h
> files automatically, but since there's no guarantee that the .h files
> are present on the user's machine at run time, nor is there a robust way
> to find those .h files, this would be a tool for your convenience as a
> developer, and would still require you to redundantly store the output
> of this tool in your Scheme sources.
> I'm sorry that I don't have a better answer for you.
>      Mark

I am working on a tool “ffi helper”  to automatically generate the ffi code.  
The headers have to be present.  And ffi-helper will use pig-config to locate 
the header and libraries.  With respect to the #defines, I am working to 
autogenerate that, but I have to be able to filter out the wanted stuff and not 
the cruft, so it will be looking at simple defines (i.e., w/o args) and if 
those evaluate to a C constant (e.g., integer or string) then keep, otherwise 

Currently ffi-help will wrap enums.  From Cairo
;; typedef enum _cairo_status cairo_status_t;
(define wrap-cairo_status_t
  (let ((vnl '((0 . CAIRO_STATUS_SUCCESS)
               (1 . CAIRO_STATUS_NO_MEMORY)
               (39 . CAIRO_STATUS_LAST_STATUS))))
    (lambda (name) (assq-ref vnl name))))
(define unwrap-cairo_status_t
  (let ((nvl '((CAIRO_STATUS_SUCCESS . 0)
               (CAIRO_STATUS_NO_MEMORY . 1)
               (CAIRO_STATUS_LAST_STATUS . 39))))
    (lambda (name) (assq-ref nvl name))))
(export wrap-cairo_status_t unwrap-cairo_status_t)

It uses a helper file that looks like:

;; cairo.ffi                            -*- Scheme -*-
(use-modules (ffi-help))

(define-ffi-helper (cairo cairo)
  #:pkg-config "cairo"
  #:include "cairo-svg.h"
  #:library "libcairo"
  #:filter (lambda (path) (string=? "cairo" (substring path 0 5)))

reply via email to

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