Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / cmake / modules / FindFFI.cmake
blobc9ba104601872eb8a5b0a2ceed09b1326a66919f
1 # Attempts to discover ffi library with a linkable ffi_call function.
3 # Example usage:
5 # find_package(FFI)
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.
12 # FFI_LIBRARY_DIR may be set to define search paths for the ffi library.
14 # If successful, the following variables will be defined:
15 # FFI_FOUND
16 # FFI_INCLUDE_DIRS
17 # FFI_LIBRARIES
18 # HAVE_FFI_CALL
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:
23 # FFI::ffi
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 "")
29 else()
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()
35 endif()
37 find_library(FFI_LIBRARIES ffi PATHS ${FFI_LIBRARY_DIR})
39 if(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()
63 endif()
65 unset(required_includes)
66 if(FFI_REQUIRE_INCLUDE)
67   set(required_includes FFI_INCLUDE_DIRS)
68 endif()
70 include(FindPackageHandleStandardArgs)
71 find_package_handle_standard_args(FFI
72                                   FOUND_VAR
73                                     FFI_FOUND
74                                   REQUIRED_VARS
75                                     FFI_LIBRARIES
76                                     ${required_includes}
77                                     HAVE_FFI_CALL)
78 mark_as_advanced(FFI_LIBRARIES
79                  FFI_INCLUDE_DIRS
80                  HAVE_FFI_CALL
81                  FFI_HEADER
82                  HAVE_FFI_H
83                  HAVE_FFI_FFI_H)
85 if(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()
93 endif()