Skip to content

Commit

Permalink
update cmake helper for static stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
adriweb committed Jun 22, 2024
1 parent 5e01d5e commit e94c316
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 59 deletions.
103 changes: 50 additions & 53 deletions gui/qt/.cmake/try_static_libs.cmake
Original file line number Diff line number Diff line change
@@ -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})
Expand All @@ -32,67 +27,69 @@ 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")
set(LIB_NAME_FULL "bzip2")
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()
15 changes: 9 additions & 6 deletions gui/qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand All @@ -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)
Expand All @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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})
Expand Down

0 comments on commit e94c316

Please sign in to comment.