link_libffi.xcpjh


//////////////////// libffi //////////////////////
/*
libffi is a Portable Foreign Function Interface Library

The libffi library provides a portable, high level programming interface to various calling 
conventions. This allows a programmer to call any function specified by a call interface 
description at run-time.

FFI stands for Foreign Function Interface. A foreign function interface is the popular name for the 
interface that allows code written in one language to call code written in another language. The 
libffi library really only provides the lowest, machine dependent layer of a fully featured foreign 
function interface. A layer must exist above libffi that handles type conversions for values passed 
between the two languages.

Download from http://sourceware.org/libffi/

libffi-3.2.1 was released on November 12, 2014

Steps:

1)  Download from
    ftp://sourceware.org/pub/libffi/libffi-3.2.1.tar.gz

2)  Unzip to $(PATH_TO_ROOT)/ThirdParty.Ceda/libffi/libffi-3.2.1

3)  Run the msvcc.sh wrapper script during configuration like so:

        path/to/configure CC=path/to/msvcc.sh CXX=path/to/msvcc.sh LD=link CPP="cl -nologo -EP"

    For 64-bit Windows builds, use CC="path/to/msvcc.sh -m64" and
    CXX="path/to/msvcc.sh -m64".  You may also need to specify --build
    appropriately.

    See http://hostagebrain.blogspot.com.au/2015/06/building-libffi-on-windows-by-visual.html

MSYS2
-----

pacman -Ss libffi

    mingw32/mingw-w64-i686-libffi 3.2.1-4 [installed]
        A portable, high level programming interface to various calling conventions (mingw-w64)
    mingw64/mingw-w64-x86_64-libffi 3.2.1-4 [installed]
        A portable, high level programming interface to various calling conventions (mingw-w64)
    msys/libffi 3.2.1-1 (libraries) [installed]
        Portable, high level programming interface to various calling conventions
    msys/libffi-devel 3.2.1-1 (development)
        Libffi headers and libraries

It seems that libffi is already installed. Relevant files:

    C:\msys32\mingw32\lib
    C:\msys32\mingw64\lib
        libffi-6.lib

    C:\msys32\mingw32\lib\libffi-3.2.1\include
    C:\msys32\mingw64\lib\libffi-3.2.1\include
        ffi.h
        ffitarget.h

    C:\msys32\mingw32\bin
    C:\msys32\mingw64\bin
        libffi-6.dll
*/

@def bool LIBFFI_USE_MSYS2 = true

@if (IsMsvcCompiler)
{    
    $LIBFFI_VERSION = "3.2.1"

    @if (LIBFFI_USE_MSYS2)
    {
    }
    @else
    {
        $LIBFFI_ROOT = "$(PATH_TO_ROOT)/ThirdParty.Ceda/libffi/libffi-$(LIBFFI_VERSION)"

        // Can't reference UseStaticCRT here (it might not be defined yet)
    //    @if (UseStaticCRT)
    //    {
    //        $LIBFFI_WIN32 = "$(LIBFFI_ROOT)/build-x86-MT-debug"
    //        $LIBFFI_WIN64 = "$(LIBFFI_ROOT)/build-x64-MT-debug"
    //    }
    //    @else
    //    {
            $LIBFFI_WIN32 = "$(LIBFFI_ROOT)/build-x86-MD-debug"
            $LIBFFI_WIN64 = "$(LIBFFI_ROOT)/build-x64-MD-debug"
    //    }
    }

    @if (LIBFFI_USE_MSYS2)
    {
        +cpp("Win32")
        {
            // C:/msys32/mingw32/lib/libffi-3.2.1/include
            /I "$(MSYS2_WIN32_LIB)/libffi-$(LIBFFI_VERSION)/include"
        }

        +cpp("x64")
        {
            // C:/msys32/mingw64/lib/libffi-3.2.1/include
            /I "$(MSYS2_WIN64_LIB)/libffi-$(LIBFFI_VERSION)/include"
        }

        +link("Win32")
        {
            "$(MSYS2_WIN32_LIB)/libffi-6.lib"
        }

        +link("x64")
        {
            "$(MSYS2_WIN64_LIB)/libffi-6.lib"
        }
    }
    @else
    {
        +cpp
        {
            // libffi seems to be built as a static library on Windows (libffi_convenience.lib).
            // A  DLL is created (libffi-6.dll), but appears to be empty (either functions/data
            // are not exported, or there are no functions/data in the dll).
            // It is necessary to define FFI_BUILDING to force the linker to use the symbols
            // from the library rather than the dll (i.e. linker uses '__imp_' prefix when linking
            // to an extern symbol in a dll, but this is absent if linking to the libffi_convenience
            // library). It appears that the Windows build of libffi might be a little confused.
            /D "FFI_BUILDING"
        }

        +cpp("Win32")
        {
            /I "$(LIBFFI_WIN32)/include"
        }

        +cpp("x64")
        {
            /I "$(LIBFFI_WIN64)/include"
        }

        +link("Win32")
        {
            /LIBPATH:"$(LIBFFI_WIN32)/.libs"
        }

        +link("x64")
        {
            /LIBPATH:"$(LIBFFI_WIN64)/.libs"
        }

        +link
        {
            "libffi_convenience.lib"
        }
    }
}

@if (IsLinuxPlatform)
{
    // requires sudo apt-get install libffi-dev
    +cpp
    {
        // libffi seems to be built as a static library on Windows (libffi_convenience.lib).
        // A  DLL is created (libffi-6.dll), but appears to be empty (either functions/data
        // are not exported, or there are no functions/data in the dll).
        // It is necessary to define FFI_BUILDING to force the linker to use the symbols
        // from the library rather than the dll (i.e. linker uses '__imp_' prefix when linking
        // to an extern symbol in a dll, but this is absent if linking to the libffi_convenience
        // library). It appears that the Windows build of libffi might be a little confused.
        -D "FFI_BUILDING"
    }
    +link { -lffi }
}

@if (IsMacOSXPlatform)
{
    +cpp { -I /usr/include/ffi }
    +link { -L/usr/lib -lffi }
}