Skip to content

Commit b689b4f

Browse files
authored
[LLVM][CMake] Add ffi_static target for the FFI static library (#78779)
Summary: This patch is an attempt to make the `find_package(FFI)` support in LLVM prefer to provide the static library version if present. This is currently an optional library for building `libffi`, and its presence implies that it should likely be used. This patch is an attempt to fix some problems observed with testing programs linked against `libffi` on many different systems that could have conflicting paths. Linking it statically prevents this. This patch adds the `ffi_static` target for this library.
1 parent ab1b499 commit b689b4f

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

llvm/cmake/modules/FindFFI.cmake

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# FFI_FOUND
1616
# FFI_INCLUDE_DIRS
1717
# FFI_LIBRARIES
18+
# FFI_STATIC_LIBRARIES
1819
# HAVE_FFI_CALL
1920
#
2021
# HAVE_FFI_H or HAVE_FFI_FFI_H is defined depending on the ffi.h include path.
@@ -34,7 +35,8 @@ else()
3435
endif()
3536
endif()
3637

37-
find_library(FFI_LIBRARIES ffi PATHS ${FFI_LIBRARY_DIR})
38+
find_library(FFI_LIBRARIES NAMES ffi PATHS ${FFI_LIBRARY_DIR})
39+
find_library(FFI_STATIC_LIBRARIES NAMES libffi.a PATHS ${FFI_LIBRARY_DIR})
3840

3941
if(FFI_LIBRARIES)
4042
include(CMakePushCheckState)
@@ -76,18 +78,26 @@ find_package_handle_standard_args(FFI
7678
${required_includes}
7779
HAVE_FFI_CALL)
7880
mark_as_advanced(FFI_LIBRARIES
81+
FFI_STATIC_LIBRARIES
7982
FFI_INCLUDE_DIRS
8083
HAVE_FFI_CALL
8184
FFI_HEADER
8285
HAVE_FFI_H
8386
HAVE_FFI_FFI_H)
8487

8588
if(FFI_FOUND)
86-
if(NOT TARGET FFI::ffi)
89+
if(NOT TARGET FFI::ffi AND FFI_LIBRARIES)
8790
add_library(FFI::ffi UNKNOWN IMPORTED)
8891
set_target_properties(FFI::ffi PROPERTIES IMPORTED_LOCATION "${FFI_LIBRARIES}")
8992
if(FFI_INCLUDE_DIRS)
9093
set_target_properties(FFI::ffi PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}")
9194
endif()
9295
endif()
96+
if(NOT TARGET FFI::ffi_static AND FFI_STATIC_LIBRARIES)
97+
add_library(FFI::ffi_static UNKNOWN IMPORTED)
98+
set_target_properties(FFI::ffi_static PROPERTIES IMPORTED_LOCATION "${FFI_STATIC_LIBRARIES}")
99+
if(FFI_INCLUDE_DIRS)
100+
set_target_properties(FFI::ffi_static PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${FFI_INCLUDE_DIRS}")
101+
endif()
102+
endif()
93103
endif()

openmp/libomptarget/plugins-nextgen/CMakeLists.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "${tmachine}$")
4949
)
5050

5151
if(LIBOMPTARGET_DEP_LIBFFI_FOUND)
52-
libomptarget_say("Building ${tmachine_libname} plugin linked with libffi")
53-
target_link_libraries("omptarget.rtl.${tmachine_libname}" PRIVATE
54-
${FFI_LIBRARIES})
55-
target_include_directories("omptarget.rtl.${tmachine_libname}" PRIVATE
56-
${FFI_INCLUDE_DIRS})
52+
libomptarget_say("Building ${tmachine_libname} plugin linked with libffi")
53+
if(FFI_STATIC_LIBRARIES)
54+
target_link_libraries(
55+
"omptarget.rtl.${tmachine_libname}" PRIVATE FFI::ffi_static)
56+
else()
57+
target_link_libraries(
58+
"omptarget.rtl.${tmachine_libname}" PRIVATE FFI::ffi)
59+
endif()
5760
else()
5861
libomptarget_say("Building ${tmachine_libname} plugie for dlopened libffi")
5962
target_sources("omptarget.rtl.${tmachine_libname}" PRIVATE

0 commit comments

Comments
 (0)