From e94c316491206646e6fa52c5bf20648a4e6297c5 Mon Sep 17 00:00:00 2001 From: Adrien Bertrand Date: Fri, 21 Jun 2024 21:19:23 -0400 Subject: [PATCH] update cmake helper for static stuff. --- gui/qt/.cmake/try_static_libs.cmake | 103 ++++++++++++++-------------- gui/qt/CMakeLists.txt | 15 ++-- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/gui/qt/.cmake/try_static_libs.cmake b/gui/qt/.cmake/try_static_libs.cmake index 95f63ae39..6c73636a1 100644 --- a/gui/qt/.cmake/try_static_libs.cmake +++ b/gui/qt/.cmake/try_static_libs.cmake @@ -1,26 +1,21 @@ macro(try_static_libs_if_needed) if (TRY_STATIC_LIBS) - if(NOT CMAKE_FIND_LIBRARY_PREFIXES) - set(STATIC_LIB_PREFIX "") - else() - list(GET CMAKE_FIND_LIBRARY_PREFIXES 0 STATIC_LIB_PREFIX) - endif() + list(APPEND DEPS_LIBRARY_DIRS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") list(APPEND DEPS_STATIC_LIBRARY_DIRS "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") if(APPLE) - set(DYNAMIC_LIB_EXTS ".dylib" ".tbd") - set(STATIC_LIB_EXT ".a") - list(APPEND DEPS_LIBRARY_DIRS "/usr/lib" "${CMAKE_OSX_SYSROOT}/usr/lib" "/opt/homebrew/lib" "/usr/local/opt" "/usr/local/lib" "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}") - list(APPEND DEPS_STATIC_LIBRARY_DIRS "/opt/homebrew/lib" "/usr/local/opt" "/usr/local/lib" "${CMAKE_OSX_SYSROOT}/usr/lib" ) - elseif(UNIX) - set(DYNAMIC_LIB_EXTS ".so") - set(STATIC_LIB_EXT ".a") - elseif(WIN32 OR MINGW) - set(DYNAMIC_LIB_EXTS ".dll") - set(STATIC_LIB_EXT ".lib") + list(APPEND DEPS_LIBRARY_DIRS "/usr/lib" "${CMAKE_OSX_SYSROOT}/usr/lib" "/opt/homebrew/opt" "/opt/homebrew/lib" "/usr/local/opt" "/usr/local/lib") + list(APPEND DEPS_STATIC_LIBRARY_DIRS "/opt/homebrew/lib" "/opt/homebrew/opt" "/usr/local/opt" "/usr/local/lib" "${CMAKE_OSX_SYSROOT}/usr/lib" ) endif() + + list(REMOVE_DUPLICATES DEPS_STATIC_LDFLAGS_OTHER) list(REMOVE_DUPLICATES DEPS_STATIC_LIBRARIES) list(REMOVE_DUPLICATES DEPS_STATIC_LIBRARY_DIRS) - set(TRY_STATIC_DEPS_LDFLAGS "") + list(REMOVE_DUPLICATES DEPS_LIBRARY_DIRS) + + set(TRY_STATIC_DEPS_LIBS "") + set(TRY_STATIC_DEPS_LIBSDIRS "") + set(TRY_STATIC_DEPS_LDFLAGS_OTHER "") + foreach(LIB_NAME ${DEPS_STATIC_LIBRARIES}) set(LIB_NAME_FULL ${LIB_NAME}) foreach(LIB_DIR ${DEPS_STATIC_LIBRARY_DIRS}) @@ -32,14 +27,27 @@ macro(try_static_libs_if_needed) # /lib/x86_64-linux-gnu/libz.so.1: error adding symbols: DSO missing from command line if(LIB_NAME STREQUAL "archive" AND CMAKE_SYSTEM_NAME STREQUAL "Linux" AND CMAKE_CXX_COMPILER_ID MATCHES "GNU") message(WARNING "[GCC Linux] Skipping libarchive static lib search due to a linking issue, will fallback on the shared lib") + list(APPEND TRY_STATIC_DEPS_LIBS "${LIB_NAME}") + set(${LIB_NAME}_FOUND YES) break() endif() - set(LIB_FULLPATH "${LIB_DIR}/${STATIC_LIB_PREFIX}${LIB_NAME}${STATIC_LIB_EXT}") + # there are relocation issues when linking some libs on linux: + # /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libbz2.a(bzlib.o): relocation R_X86_64_PC32 against symbol `BZ2_crc32Table' + # /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libz.a(deflate.o): relocation R_X86_64_PC32 against symbol `z_errmsg' + # /usr/bin/ld: /usr/lib/x86_64-linux-gnu/libusb-1.0.a(libusb_1_0_la-core.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' + # can not be used when making a shared object; recompile with -fPIC + if(LIB_NAME MATCHES "^(bz2|z|m|cairo|cairo-gobject|usb-1.0|png16|fontconfig|freetype|harfbuzz|harfbuzz-gobject|icu.+)$" AND CMAKE_SYSTEM_NAME STREQUAL "Linux") + message(WARNING "[Linux] Skipping '${LIB_NAME}' static lib search due to a linking issue, will fallback on the shared lib") + list(APPEND TRY_STATIC_DEPS_LIBS "${LIB_NAME}") + set(${LIB_NAME}_FOUND YES) + break() + endif() + set(LIB_FULLPATH "${LIB_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}") if(NOT EXISTS ${LIB_FULLPATH}) - set(LIB_FULLPATH "${LIB_DIR}/${LIB_NAME_FULL}/lib/${STATIC_LIB_PREFIX}${LIB_NAME}-static${STATIC_LIB_EXT}") + set(LIB_FULLPATH "${LIB_DIR}/${LIB_NAME_FULL}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${LIB_NAME}-static${CMAKE_STATIC_LIBRARY_SUFFIX}") endif() if(NOT EXISTS ${LIB_FULLPATH}) - set(LIB_FULLPATH "${LIB_DIR}/lib${LIB_NAME_FULL}/lib/${STATIC_LIB_PREFIX}${LIB_NAME}${STATIC_LIB_EXT}") + set(LIB_FULLPATH "${LIB_DIR}/lib${LIB_NAME_FULL}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}") endif() if(NOT EXISTS ${LIB_FULLPATH}) if(LIB_NAME_FULL STREQUAL "bz2") @@ -47,52 +55,41 @@ macro(try_static_libs_if_needed) elseif(LIB_NAME_FULL STREQUAL "z") set(LIB_NAME_FULL "zlib") endif() - set(LIB_FULLPATH "${LIB_DIR}/${LIB_NAME_FULL}/lib/${STATIC_LIB_PREFIX}${LIB_NAME}${STATIC_LIB_EXT}") + set(LIB_FULLPATH "${LIB_DIR}/${LIB_NAME_FULL}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}${LIB_NAME}${CMAKE_STATIC_LIBRARY_SUFFIX}") endif() if(EXISTS ${LIB_FULLPATH}) message("[Info] Found static lib '${LIB_NAME}' for '${PROJECT_NAME}': ${LIB_FULLPATH}") - list(APPEND TRY_STATIC_DEPS_LDFLAGS ${LIB_FULLPATH}) + list(APPEND TRY_STATIC_DEPS_LIBS ${LIB_FULLPATH}) set(${LIB_NAME}_FOUND YES) break() endif() endforeach() if(NOT ${LIB_NAME}_FOUND) - foreach(LIB_DIR ${DEPS_LIBRARY_DIRS}) - if(NOT EXISTS ${LIB_DIR}) - continue() - endif() - foreach(DYNAMIC_LIB_EXT ${DYNAMIC_LIB_EXTS}) - set(LIB_FULLPATH "${LIB_DIR}/${STATIC_LIB_PREFIX}${LIB_NAME}${DYNAMIC_LIB_EXT}") - if(NOT EXISTS ${LIB_FULLPATH}) - set(LIB_FULLPATH "${LIB_DIR}/${LIB_NAME_FULL}/lib/${STATIC_LIB_PREFIX}${LIB_NAME}${DYNAMIC_LIB_EXT}") - endif() - if(NOT EXISTS ${LIB_FULLPATH}) - set(LIB_FULLPATH "${LIB_DIR}/lib${LIB_NAME_FULL}/lib/${STATIC_LIB_PREFIX}${LIB_NAME}${DYNAMIC_LIB_EXT}") - endif() - if(EXISTS ${LIB_FULLPATH}) - list(PREPEND TRY_STATIC_DEPS_LDFLAGS "-l${LIB_NAME}") - set(${LIB_NAME}_FOUND YES) - message("[Warning] Could not find static lib '${LIB_NAME}' for '${PROJECT_NAME}', using found shared lib instead: ${LIB_FULLPATH}") - break() - endif() - endforeach() - if(${LIB_NAME}_FOUND) - break() - endif() - endforeach() - if(NOT ${LIB_NAME}_FOUND) - message("[Warning] Could not find static nor shared lib '${LIB_NAME}' for '${PROJECT_NAME}'. Adding '-l${LIB_NAME}' directly anyway!") - list(APPEND TRY_STATIC_DEPS_LDFLAGS "-l${LIB_NAME}") - endif() + message("[Warning] Could not find static nor shared lib '${LIB_NAME}' for '${PROJECT_NAME}'. Adding the lib directly anyway!") + list(APPEND TRY_STATIC_DEPS_LIBS "${LIB_NAME}") + set(${LIB_NAME}_FOUND YES) endif() endforeach() + list(REMOVE_DUPLICATES TRY_STATIC_DEPS_LIBS) + + # Lib dirs foreach(LIB_DIR ${DEPS_LIBRARY_DIRS}) if(EXISTS ${LIB_DIR}) - list(PREPEND TRY_STATIC_DEPS_LDFLAGS "-L${LIB_DIR}") + list(APPEND TRY_STATIC_DEPS_LIBSDIRS "${LIB_DIR}") endif() endforeach() - string(REPLACE ";" " " DEPS_STATIC_LDFLAGS_OTHER "${DEPS_STATIC_LDFLAGS_OTHER}") - list(APPEND TRY_STATIC_DEPS_LDFLAGS "${DEPS_STATIC_LDFLAGS_OTHER}") - list(REMOVE_DUPLICATES TRY_STATIC_DEPS_LDFLAGS) + list(REMOVE_DUPLICATES TRY_STATIC_DEPS_LIBSDIRS) + + # Raw flags + if(APPLE) + list(APPEND TRY_STATIC_DEPS_LDFLAGS_OTHER "-pthread") + foreach(framework CoreFoundation Foundation Carbon Cocoa ApplicationServices AppKit IOKit Security) + if (DEPS_STATIC_LDFLAGS_OTHER MATCHES "${framework}") + list(APPEND TRY_STATIC_DEPS_LDFLAGS_OTHER "-framework ${framework}") + endif() + endforeach() + else() + list(APPEND TRY_STATIC_DEPS_LDFLAGS_OTHER "${DEPS_STATIC_LDFLAGS_OTHER}") + endif() endif() -endmacro() +endmacro() \ No newline at end of file diff --git a/gui/qt/CMakeLists.txt b/gui/qt/CMakeLists.txt index 16337114a..12ba24075 100644 --- a/gui/qt/CMakeLists.txt +++ b/gui/qt/CMakeLists.txt @@ -230,7 +230,7 @@ find_package(PkgConfig QUIET) # Set manually when installed with Homebrew, see https://github.com/Homebrew/legacy-homebrew/issues/45891 # Note: we can't do the same for libiconv because it exports libiconv_* symbols and not iconv_*, so we fallback on the non-static system lib... if(APPLE) - set(BREW_LIB_PATHS "/opt/homebrew/lib" "/usr/local/opt" "/usr/local/lib") + set(BREW_LIB_PATHS "/opt/homebrew/lib" "/opt/homebrew/opt" "/usr/local/opt" "/usr/local/lib") foreach(LIB_DIR ${BREW_LIB_PATHS}) if(EXISTS "${LIB_DIR}/libarchive/lib/pkgconfig") set(ENV{PKG_CONFIG_PATH} "${LIB_DIR}/libarchive/lib/pkgconfig:$ENV{PKG_CONFIG_PATH}") @@ -243,7 +243,7 @@ if(PkgConfig_FOUND AND TRY_STATIC_LIBS) pkg_check_modules(DEPS libarchive) if(DEPS_FOUND) try_static_libs_if_needed() - if(DEPS_STATIC_LIBRARY_DIRS AND TRY_STATIC_DEPS_LDFLAGS) + if(TRY_STATIC_DEPS_LIBS) find_package(Iconv) if(Iconv_FOUND) if(NOT Iconv_IS_BUILT_IN) @@ -252,8 +252,8 @@ if(PkgConfig_FOUND AND TRY_STATIC_LIBS) endif() set(LibArchive_FOUND ON) set(LibArchive_INCLUDE_DIRS ${DEPS_STATIC_INCLUDE_DIRS}) - set(LibArchive_LIBRARY_DIRS ${DEPS_STATIC_LIBRARY_DIRS}) - set(LibArchive_LIBRARIES ${TRY_STATIC_DEPS_LDFLAGS}) + set(LibArchive_LIBRARY_DIRS ${TRY_STATIC_DEPS_LIBSDIRS}) + set(LibArchive_LIBRARIES ${TRY_STATIC_DEPS_LDFLAGS_OTHER} ${TRY_STATIC_DEPS_LIBS}) endif() endif() endif() @@ -286,9 +286,11 @@ if(NOT WIN32 AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/capture/libpng-apng/.libs/l pkg_check_modules(DEPS zlib) if(DEPS_FOUND) try_static_libs_if_needed() - if(TRY_STATIC_DEPS_LDFLAGS) + if(DEPS_STATIC_LIBRARY_DIRS AND TRY_STATIC_DEPS_LIBS) set(ZLIB_FOUND ON) - set(ZLIB_LIBRARIES ${TRY_STATIC_DEPS_LDFLAGS}) + set(ZLIB_INCLUDE_DIRS ${DEPS_STATIC_INCLUDE_DIRS}) + set(ZLIB_LIBRARY_DIRS ${TRY_STATIC_DEPS_LIBSDIRS}) + set(ZLIB_LIBRARIES ${TRY_STATIC_DEPS_LDFLAGS_OTHER} ${TRY_STATIC_DEPS_LIBS}) endif() endif() if(NOT ZLIB_FOUND) @@ -301,6 +303,7 @@ if(NOT WIN32 AND EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/capture/libpng-apng/.libs/l endif() if(ZLIB_FOUND) target_compile_definitions(CEmu PRIVATE "PNG_SUPPORT") + target_include_directories(CEmu PRIVATE ${ZLIB_INCLUDE_DIRS}) target_include_directories(CEmu PRIVATE capture/libpng-apng) target_link_directories(CEmu PRIVATE ${ZLIB_LIBRARY_DIRS}) target_link_libraries(CEmu PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/capture/libpng-apng/.libs/libpng16.a ${ZLIB_LIBRARIES})