1# Attempts to discover ffi library with a linkable ffi_call function.
2#
3# Example usage:
4#
5# find_package(FFI)
6#
7# FFI_REQUIRE_INCLUDE may be set to consider ffi found if the includes
8# are present in addition to the library. This is useful to keep off
9# for the imported package on platforms that install the library but
10# not the headers.
11#
12# FFI_LIBRARY_DIR may be set to define search paths for the ffi library.
13#
14# If successful, the following variables will be defined:
15# FFI_FOUND
16# FFI_INCLUDE_DIRS
17# FFI_LIBRARIES
18# HAVE_FFI_CALL
19#
20# HAVE_FFI_H or HAVE_FFI_FFI_H is defined depending on the ffi.h include path.
21#
22# Additionally, the following import target will be defined:
23# FFI::ffi
24
25find_path(FFI_INCLUDE_DIRS ffi.h PATHS ${FFI_INCLUDE_DIR})
26if( EXISTS "${FFI_INCLUDE_DIRS}/ffi.h" )
27  set(FFI_HEADER ffi.h CACHE INTERNAL "")
28  set(HAVE_FFI_H 1 CACHE INTERNAL "")
29else()
30  find_path(FFI_INCLUDE_DIRS ffi/ffi.h PATHS ${FFI_INCLUDE_DIR})
31  if( EXISTS "${FFI_INCLUDE_DIRS}/ffi/ffi.h" )
32    set(FFI_HEADER ffi/ffi.h CACHE INTERNAL "")
33    set(HAVE_FFI_FFI_H 1 CACHE INTERNAL "")
34  endif()
35endif()
36
37find_library(FFI_LIBRARIES ffi PATHS ${FFI_LIBRARY_DIR})
38
39if(FFI_LIBRARIES)
40  include(CMakePushCheckState)
41  cmake_push_check_state()
42  list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARIES})
43  set(HAVE_FFI_CALL_SRC [=[
44    #ifdef __cplusplus
45    extern "C" {
46    #endif
47    struct ffi_cif;
48    typedef struct ffi_cif ffi_cif;
49    void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
50    #ifdef __cplusplus
51    }
52    #endif
53    int main(void) { ffi_call(0, 0, 0, 0); }
54    ]=])
55  if(DEFINED CMAKE_C_COMPILER)
56    include(CheckCSourceCompiles)
57    check_c_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
58  else()
59    include(CheckCXXSourceCompiles)
60    check_cxx_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
61  endif()
62  cmake_pop_check_state()
63endif()
64
65unset(required_includes)
66if(FFI_REQUIRE_INCLUDE)
67  set(required_includes FFI_INCLUDE_DIRS)
68endif()
69
70include(FindPackageHandleStandardArgs)
71find_package_handle_standard_args(FFI
72                                  FOUND_VAR
73                                    FFI_FOUND
74                                  REQUIRED_VARS
75                                    FFI_LIBRARIES
76                                    ${required_includes}
77                                    HAVE_FFI_CALL)
78mark_as_advanced(FFI_LIBRARIES
79                 FFI_INCLUDE_DIRS
80                 HAVE_FFI_CALL
81                 FFI_HEADER
82                 HAVE_FFI_H
83                 HAVE_FFI_FFI_H)
84
85if(FFI_FOUND)
86  if(NOT TARGET FFI::ffi)
87    add_library(FFI::ffi UNKNOWN IMPORTED)
88    set_target_properties(FFI::ffi PROPERTIES IMPORTED_LOCATION "${FFI_LIBRARIES}")
89    if(FFI_INCLUDE_DIRS)
90      set_target_properties(FFI::ffi PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}")
91    endif()
92  endif()
93endif()
94