Skip to content

Commit

Permalink
Find dependencies for statically-linked libdw, try to use pkg-config
Browse files Browse the repository at this point in the history
BackwardConfig.cmake was not explicitly pulling in the
dependencies (libelf, libz, etc.) of libdw, which made it fail to link
when libdw was built as a static library (and therefore didn't
explicitly encode those dependencies).

This changes it to first try using pkg-config to find libdw and its
dependencies, and then fall back to looking up the individual
libraries it might depend on.
  • Loading branch information
csw committed Dec 17, 2021
1 parent 4924389 commit d04d150
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions BackwardConfig.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ endif()
# CONFIGS
###############################################################################
include(FindPackageHandleStandardArgs)
if(NOT PKG_CONFIG_FOUND)
include(CMakeFindDependencyMacro)
find_dependency(PkgConfig)
endif()

if (STACK_WALKING_LIBUNWIND)
# libunwind works on the macOS without having to add special include
Expand Down Expand Up @@ -87,12 +91,30 @@ if (${STACK_DETAILS_AUTO_DETECT})
set(_name_mismatched_arg NAME_MISMATCHED)
endif()
# find libdw
find_path(LIBDW_INCLUDE_DIR NAMES "elfutils/libdw.h" "elfutils/libdwfl.h")
find_library(LIBDW_LIBRARY dw)
set(LIBDW_INCLUDE_DIRS ${LIBDW_INCLUDE_DIR} )
set(LIBDW_LIBRARIES ${LIBDW_LIBRARY} )
find_package_handle_standard_args(libdw ${_name_mismatched_arg}
REQUIRED_VARS LIBDW_LIBRARY LIBDW_INCLUDE_DIR)
# start by trying pkg-config
pkg_search_module(LIBDW libdw)
if(NOT LIBDW_FOUND)
find_path(LIBDW_INCLUDE_DIR NAMES "elfutils/libdw.h" "elfutils/libdwfl.h")
find_library(LIBDW_LIBRARY dw)
# if it's statically linked, look for all the possible dependencies
find_library(LIBELF_LIBRARY elf)
find_library(LIBZ_LIBRARY z)
find_library(LIBBZ2_LIBRARY bz2)
find_library(LIBLZMA_LIBRARY lzma)
find_library(LIBZSTD_LIBRARY zstd)
set(LIBDW_INCLUDE_DIRS ${LIBDW_INCLUDE_DIR} )
set(LIBDW_LIBRARIES ${LIBDW_LIBRARY}
$<$<BOOL:${LIBELF_LIBRARY}>:${LIBELF_LIBRARY}>
$<$<BOOL:${LIBZ_LIBRARY}>:${LIBZ_LIBRARY}>
$<$<BOOL:${LIBBZ2_LIBRARY}>:${LIBBZ2_LIBRARY}>
$<$<BOOL:${LIBLZMA_LIBRARY}>:${LIBLZMA_LIBRARY}>
$<$<BOOL:${LIBZSTD_LIBRARY}>:${LIBZSTD_LIBRARY}>)
find_package_handle_standard_args(libdw ${_name_mismatched_arg}
REQUIRED_VARS LIBDW_LIBRARY LIBDW_INCLUDE_DIR)
endif()
# libdw doesn't declare its pthread dependency
find_library(LIBPTHREAD_LIBRARY pthread)
set(LIBDW_LIBRARIES ${LIBDW_LIBRARIES} ${LIBPTHREAD_LIBRARY})
mark_as_advanced(LIBDW_INCLUDE_DIR LIBDW_LIBRARY)

# find libbfd
Expand Down

0 comments on commit d04d150

Please sign in to comment.