[common] cmake: correctly link static libbfd.a

When linking against libbfd.so, just passing libbfd.so to the compiler is
sufficient. When linking against the static version libbfd.a, however,
we must additionally link against libiberty.a and libz.a.

This commit adds a CMake helper to find the correct libraries that need
to be passed to link against libbfd correctly.
This commit is contained in:
Quantum 2021-02-28 19:51:54 -05:00 committed by Geoffrey McRae
parent b00c25a822
commit fc7dd7dbb7
4 changed files with 45 additions and 2 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(looking-glass-client C) project(looking-glass-client C)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/") list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
include(GNUInstallDirs) include(GNUInstallDirs)
include(CheckCCompilerFlag) include(CheckCCompilerFlag)

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.0) cmake_minimum_required(VERSION 3.0)
project(lg_common LANGUAGES C) project(lg_common LANGUAGES C)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")
include_directories( include_directories(
${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/include
) )

View File

@ -0,0 +1,40 @@
# Try to find the BFD librairies
# BFD_FOUND - system has BFD lib
# BFD_INCLUDE_DIR - the BFD include directory
# BFD_LIBRARIES - Libraries needed to use BFD
if (BFD_INCLUDE_DIR AND BFD_LIBRARIES)
# Already in cache, be silent
set(BFD_FIND_QUIETLY TRUE)
endif (BFD_INCLUDE_DIR AND BFD_LIBRARIES)
find_path(BFD_INCLUDE_DIR NAMES bfd.h)
find_library(BFD_LIBRARIES NAMES bfd)
include(FindPackageHandleStandardArgs)
if (";${BFD_LIBRARIES};" MATCHES "bfd.a;")
MESSAGE(STATUS "Linking against static bfd")
find_library(BFD_LIBIBERTY_LIBRARIES NAMES libiberty.a)
find_package_handle_standard_args(BFD_LIBIBERTY DEFAULT_MSG BFD_LIBIBERTY_LIBRARIES)
find_library(BFD_LIBZ_LIBRARIES NAMES libz.a)
find_package_handle_standard_args(BFD_LIBZ DEFAULT_MSG BFD_LIBZ_LIBRARIES)
if (NOT ${BFD_LIBIBERTY_FOUND})
message(FATAL_ERROR "Using static libbfd.a, but libiberty.a not available")
elseif (NOT ${BFD_LIBZ_FOUND})
message(FATAL_ERROR "Using static libbfd.a, but libz.a not available")
else()
list(APPEND BFD_LIBRARIES ${BFD_LIBIBERTY_LIBRARIES} ${BFD_LIBZ_LIBRARIES})
endif()
endif()
MESSAGE(STATUS "BFD libs: " "${BFD_LIBRARIES}")
find_package_handle_standard_args(BFD DEFAULT_MSG BFD_LIBRARIES BFD_INCLUDE_DIR)
MESSAGE(STATUS "BFD libs: " "${BFD_LIBRARIES}")
mark_as_advanced(BFD_INCLUDE_DIR BFD_LIBRARIES)

View File

@ -16,7 +16,8 @@ add_library(lg_common_platform_code STATIC
) )
if(ENABLE_BACKTRACE) if(ENABLE_BACKTRACE)
target_link_libraries(lg_common_platform_code bfd) find_package(BFD)
target_link_libraries(lg_common_platform_code ${BFD_LIBRARIES})
endif() endif()
target_link_libraries(lg_common_platform_code target_link_libraries(lg_common_platform_code