Skip to content

Commit

Permalink
#2320: add Findlibunwind.cmake file
Browse files Browse the repository at this point in the history
  • Loading branch information
cwschilly committed Jul 31, 2024
1 parent 5d4975d commit d463c84
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 6 deletions.
70 changes: 70 additions & 0 deletions cmake-modules/Findlibunwind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This file downloaded from https://raw.githubusercontent.com/m-a-d-n-e-s-s/madness/master/cmake/modules/FindLibunwind.cmake
#
# - Try to find Libunwind
# Input variables:
# LIBUNWIND_ROOT_DIR - The libunwind install directory
# LIBUNWIND_INCLUDE_DIR - The libunwind include directory
# LIBUNWIND_LIBRARY - The libunwind library directory
# Output variables:
# LIBUNWIND_FOUND - System has libunwind
# LIBUNWIND_INCLUDE_DIRS - The libunwind include directories
# LIBUNWIND_LIBRARIES - The libraries needed to use libunwind
# LIBUNWIND_VERSION - The version string for libunwind

include(FindPackageHandleStandardArgs)

if(NOT DEFINED LIBUNWIND_FOUND)

# Set default sarch paths for libunwind
if(LIBUNWIND_ROOT_DIR)
set(LIBUNWIND_INCLUDE_DIR ${LIBUNWIND_ROOT_DIR}/include CACHE PATH "The include directory for libunwind")
if(CMAKE_SIZEOF_VOID_P EQUAL 8 AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
set(LIBUNWIND_LIBRARY ${LIBUNWIND_ROOT_DIR}/lib64;${LIBUNWIND_ROOT_DIR}/lib CACHE PATH "The library directory for libunwind")
else()
set(LIBUNWIND_LIBRARY ${LIBUNWIND_ROOT_DIR}/lib CACHE PATH "The library directory for libunwind")
endif()
endif()

find_path(LIBUNWIND_INCLUDE_DIRS NAMES libunwind.h
HINTS ${LIBUNWIND_INCLUDE_DIR})

find_library(LIBUNWIND_LIBRARIES unwind
HINTS ${LIBUNWIND_LIBRARY})

# Get libunwind version
if(EXISTS "${LIBUNWIND_INCLUDE_DIRS}/libunwind-common.h")
file(READ "${LIBUNWIND_INCLUDE_DIRS}/libunwind-common.h" _libunwind_version_header)
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_MAJOR[ \t]+([0-9]+).*" "\\1"
LIBUNWIND_MAJOR_VERSION "${_libunwind_version_header}")
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_MINOR[ \t]+([0-9]+).*" "\\1"
LIBUNWIND_MINOR_VERSION "${_libunwind_version_header}")
string(REGEX REPLACE ".*define[ \t]+UNW_VERSION_EXTRA[ \t]+([0-9]*).*" "\\1"
LIBUNWIND_MICRO_VERSION "${_libunwind_version_header}")
if(LIBUNWIND_MICRO_VERSION)
set(LIBUNWIND_VERSION "${LIBUNWIND_MAJOR_VERSION}.${LIBUNWIND_MINOR_VERSION}.${LIBUNWIND_MICRO_VERSION}")
else()
set(LIBUNWIND_VERSION "${LIBUNWIND_MAJOR_VERSION}.${LIBUNWIND_MINOR_VERSION}")
endif()
unset(_libunwind_version_header)
endif()

# handle the QUIETLY and REQUIRED arguments and set LIBUNWIND_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(Libunwind
FOUND_VAR LIBUNWIND_FOUND
VERSION_VAR LIBUNWIND_VERSION
REQUIRED_VARS LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIRS)

mark_as_advanced(LIBUNWIND_INCLUDE_DIR LIBUNWIND_LIBRARY
LIBUNWIND_INCLUDE_DIRS LIBUNWIND_LIBRARIES)

endif()

if(LIBUNWIND_FOUND)
if(NOT TARGET LIBUNWIND::LIBUNWIND)
add_library(LIBUNWIND::LIBUNWIND UNKNOWN IMPORTED)
set_target_properties(LIBUNWIND::LIBUNWIND PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${LIBUNWIND_INCLUDE_DIRS}"
IMPORTED_LOCATION "${LIBUNWIND_LIBRARIES}" )
endif()
endif()
2 changes: 1 addition & 1 deletion cmake/check_system_functions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ check_include_files(mach/mach.h vt_has_mach_mach_h)
check_include_files(sys/resource.h vt_has_sys_resource_h)
check_include_files(unistd.h vt_has_unistd_h)
check_include_files(inttypes.h vt_has_inttypes_h)
check_include_files(libunwind.h vt_has_libunwind_h)
# check_include_files(libunwind.h vt_has_libunwind)
check_include_files(execinfo.h vt_has_execinfo_h)

check_function_exists(mstats vt_has_mstats)
Expand Down
2 changes: 1 addition & 1 deletion cmake/link_vt.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function(link_target_with_vt)
endif()

if (NOT DEFINED ARG_LINK_UNWIND AND ${ARG_DEFAULT_LINK_SET} OR ARG_LINK_UNWIND)
if (vt_has_libunwind_h)
if (vt_has_libunwind)
if (${ARG_DEBUG_LINK})
message(STATUS "link_target_with_vt: unwind=${ARG_LINK_UNWIND}")
endif()
Expand Down
7 changes: 7 additions & 0 deletions cmake/load_libunwind.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
set(vt_has_libunwind 0)

find_package(libunwind)

if(libunwind_FOUND)
set(vt_has_libunwind 1)
endif()
3 changes: 3 additions & 0 deletions cmake/load_packages.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ find_package(Perl)
# Doxygen package
include(cmake/load_doxygen.cmake)

# Optionally link with libunwind
include(cmake/load_libunwind.cmake)

# Optionally link with Zoltan
include(cmake/load_zoltan_package.cmake)

Expand Down
2 changes: 1 addition & 1 deletion cmake_config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
#cmakedefine vt_has_unistd_h
#cmakedefine vt_has_inttypes_h
#cmakedefine vt_has_sysconf
#cmakedefine vt_has_libunwind_h
#cmakedefine vt_has_libunwind
#cmakedefine vt_has_execinfo_h

#if vt_feature_cmake_external_fmt
Expand Down
4 changes: 2 additions & 2 deletions src/vt/configs/error/stack_out.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

#include <cxxabi.h>

#if defined(vt_has_libunwind_h)
#if defined(vt_has_libunwind)
# define UNW_LOCAL_ONLY
# include <libunwind.h>
#elif defined(vt_has_execinfo_h)
Expand All @@ -59,7 +59,7 @@ namespace vt { namespace debug { namespace stack {

DumpStackType dumpStack(int skip) {
DumpStackType stack;
#if defined(vt_has_libunwind_h)
#if defined(vt_has_libunwind)

unw_cursor_t cursor;
unw_context_t context;
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/configs/test_stack_dumping.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

namespace vt { namespace tests { namespace unit {

#if defined(vt_has_libunwind_h) || defined(vt_has_execinfo_h)
#if defined(vt_has_libunwind) || defined(vt_has_execinfo_h)

struct TestStackDumping : TestParallelHarness {};

Expand Down

0 comments on commit d463c84

Please sign in to comment.