Search code examples
dllrebolrebol3

How do you build Rebol's "Ren-C" branch with LibFFI support?


I'd like to access a dynamic library using FFI features in the Ren-C Rebol branch. I understand this is possible by building with LibFFI support enabled. What steps do I need to take to enable this?

I mainly use OS X for development, though would also like to be able to build it for use with Linux.


Solution

  • (Note: This is probably the kind of information that should be added to the Wiki, as it is not so much a language question but the kind of thing that is subject to change over time. But, answerable, so...)

    If you're using the GNU make method to build (where make -f makefile.boot generates a makefile for you) then you should find some lines in there like:

    TO_OS_BASE?= TO_OSX
    TO_OS_NAME?= TO_OSX_X64
    OS_ID?= 0.2.40
    BIN_SUFFIX=
    RAPI_FLAGS= -D__LP64__ -DENDIAN_LITTLE -DHAS_LL_CONSTS -O1 ...
    HOST_FLAGS= -DREB_EXE  -D__LP64__ -DENDIAN_LITTLE ...
    

    Modify the RAPI_FLAGS and HOST_FLAGS lines at the beginning to add -DHAVE_LIBFFI_AVAILABLE. That (-D)efines a preprocessor directive to tell the code it's okay to generate calls to FFI, because you have it available for linking later.

    Now to tell it where to find include files. There's a line for includes that should look like:

    INCL ?= .
    I= -I$(INCL) -I$S/include/ -I$S/codecs/ ...
    

    To the tail of that you need to add something that will look like -I/usr/local/opt/libffi/lib/libffi-3.0.13/include, or similar. The actual directory will depend on where you have libffi on your system. On the OSX system I'm looking at, that has two files in it, ffi.h and ffitarget.h.

    (Note: I'm afraid I don't know how these files got on this computer. They didn't ship with the OS, so they came from...somewhere. I don't generally develop on OSX--nor for that matter do I use this FFI. You'll have to consult your local FFI-on-OSX website, or perhaps for support contact Atronix Engineering) who added the FFI features to Rebol.)

    Then it's necessary to tell it where you have libffi on your system. You'll find a CLIB line that is likely just CLIB= -lm. You'd change this for example to:

    CLIB= -L/usr/local/opt/libffi/lib -lm -lffi
    

    -lffi Tells it to look for the ffi (-l)ibrary, and -lxxx means it assumes the name of the library will be libxxx[something]. -L/usr/local/opt/libffi/lib tells it where to look for it. You'll have to figure out where (if anywhere) you have libffi, and if not get it. If you had it, the directory would have contents something like:

    libffi-3.0.13
    libffi.6.dylib
    libffi.a
    libffi.dylib
    pkgconfig
    

    I mainly use OS X for development, though would also like to be able to build it for use with Linux.

    On Linux it's similar but generally much easier to get the library, as easy as sudo apt-get install libffi-dev. Same step for the RFLAGS and CFLAGS, and it should take care of the location automatically... so you can add just -lffi to CLIB.