1 # Attempts to discover ffi library with a linkable ffi_call function.
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
12 # FFI_LIBRARY_DIR may be set to define search paths for the ffi library.
14 # If successful, the following variables will be defined:
20 # HAVE_FFI_H or HAVE_FFI_FFI_H is defined depending on the ffi.h include path.
22 # Additionally, the following import target will be defined:
25 find_path(FFI_INCLUDE_DIRS ffi.h PATHS ${FFI_INCLUDE_DIR})
26 if( EXISTS "${FFI_INCLUDE_DIRS}/ffi.h" )
27 set(FFI_HEADER ffi.h CACHE INTERNAL "")
28 set(HAVE_FFI_H 1 CACHE INTERNAL "")
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 "")
37 find_library(FFI_LIBRARIES ffi PATHS ${FFI_LIBRARY_DIR})
40 include(CMakePushCheckState)
41 cmake_push_check_state()
42 list(APPEND CMAKE_REQUIRED_LIBRARIES ${FFI_LIBRARIES})
43 set(HAVE_FFI_CALL_SRC [=[
48 typedef struct ffi_cif ffi_cif;
49 void ffi_call(ffi_cif *cif, void (*fn)(void), void *rvalue, void **avalue);
53 int main(void) { ffi_call(0, 0, 0, 0); }
55 if(DEFINED CMAKE_C_COMPILER)
56 include(CheckCSourceCompiles)
57 check_c_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
59 include(CheckCXXSourceCompiles)
60 check_cxx_source_compiles("${HAVE_FFI_CALL_SRC}" HAVE_FFI_CALL)
62 cmake_pop_check_state()
65 unset(required_includes)
66 if(FFI_REQUIRE_INCLUDE)
67 set(required_includes FFI_INCLUDE_DIRS)
70 include(FindPackageHandleStandardArgs)
71 find_package_handle_standard_args(FFI
78 mark_as_advanced(FFI_LIBRARIES
86 if(NOT TARGET FFI::ffi)
87 add_library(FFI::ffi UNKNOWN IMPORTED)
88 set_target_properties(FFI::ffi PROPERTIES IMPORTED_LOCATION "${FFI_LIBRARIES}")
90 set_target_properties(FFI::ffi PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}")