From fc7dd7dbb715a31b0f39e83462f1b4a6a088a1e5 Mon Sep 17 00:00:00 2001 From: Quantum Date: Sun, 28 Feb 2021 19:51:54 -0500 Subject: [PATCH] [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. --- client/CMakeLists.txt | 2 +- common/CMakeLists.txt | 2 ++ common/cmake/FindBFD.cmake | 40 ++++++++++++++++++++++++ common/src/platform/linux/CMakeLists.txt | 3 +- 4 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 common/cmake/FindBFD.cmake diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index e6dc9bef..bd76708a 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.0) 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(CheckCCompilerFlag) diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 420c2863..282b00de 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,6 +1,8 @@ cmake_minimum_required(VERSION 3.0) project(lg_common LANGUAGES C) +list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/") + include_directories( ${PROJECT_SOURCE_DIR}/include ) diff --git a/common/cmake/FindBFD.cmake b/common/cmake/FindBFD.cmake new file mode 100644 index 00000000..e16d1d5b --- /dev/null +++ b/common/cmake/FindBFD.cmake @@ -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) diff --git a/common/src/platform/linux/CMakeLists.txt b/common/src/platform/linux/CMakeLists.txt index 4dbc63e5..334af35b 100644 --- a/common/src/platform/linux/CMakeLists.txt +++ b/common/src/platform/linux/CMakeLists.txt @@ -16,7 +16,8 @@ add_library(lg_common_platform_code STATIC ) if(ENABLE_BACKTRACE) - target_link_libraries(lg_common_platform_code bfd) + find_package(BFD) + target_link_libraries(lg_common_platform_code ${BFD_LIBRARIES}) endif() target_link_libraries(lg_common_platform_code