From a3d2c17713aee26586feb4cc578bd2bbb9391dd9 Mon Sep 17 00:00:00 2001 From: Peter Kokot Date: Thu, 17 Jul 2025 16:01:11 +0200 Subject: [PATCH] CMake: Fix dlopen check Systems can have dlopen symbol in various libraries. For example, on Haiku, dlopen is in 'root' library, which is linked by default. The CMAKE_DL_LIBS variable is automatically set by CMake and contains the name of the library or libraries that contain dynamic loading functionality. CMakePushCheckState checks for the symbol in isolation, so the required libraries variable can be modified. --- CMakeLists.txt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab3d0d245..553f67a31 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ include(CheckStructHasMember) include(CheckSymbolExists) include(CMakeDependentOption) include(CMakePackageConfigHelpers) +include(CMakePushCheckState) include(FindPkgConfig) include(GNUInstallDirs) @@ -323,10 +324,13 @@ target_include_directories( ) if(LIBXML2_WITH_MODULES) - check_library_exists(dl dlopen "" HAVE_DLOPEN) + cmake_push_check_state(RESET) + set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_DL_LIBS}) + check_symbol_exists(dlopen "dlfcn.h" HAVE_DLOPEN) + cmake_pop_check_state() if(HAVE_DLOPEN) - target_link_libraries(LibXml2 PRIVATE dl) - set(MODULE_LIBS "-ldl") + target_link_libraries(LibXml2 PRIVATE ${CMAKE_DL_LIBS}) + list(TRANSFORM CMAKE_DL_LIBS PREPEND "-l" OUTPUT_VARIABLE MODULE_LIBS) else() check_library_exists(dld shl_load "" HAVE_SHLLOAD) if(HAVE_SHLLOAD)