# LIBUNWIND_FOUND       - True if libunwind was found
# LIBUNWIND_LIBRARIES   - The libraries needed to use libunwind
# LIBUNWIND_INCLUDE_DIR - Location of unwind.h and libunwind.h

SET(LIBUNWIND_SEARCH_DIRS ${LIBUNWIND_ROOT})
IF(LIBUNWIND_NO_SYSTEM_PATHS)
    LIST(APPEND LIBUNWIND_SEARCH_DIRS NO_CMAKE_SYSTEM_PATH)
ENDIF()

IF(LIBUNWIND_USE_STATIC_LIBS)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES .a)
ENDIF()

FIND_PATH(LIBUNWIND_INCLUDE_DIR
    libunwind.h
    PATH_SUFFIXES "include"
    HINTS ${LIBUNWIND_SEARCH_DIRS})
IF(NOT LIBUNWIND_INCLUDE_DIR)
    MESSAGE(FATAL_ERROR "failed to find libunwind.h")
ELSEIF(NOT EXISTS "${LIBUNWIND_INCLUDE_DIR}/unwind.h")
    MESSAGE(FATAL_ERROR "libunwind.h was found, but unwind.h was not found in that directory")
    SET(LIBUNWIND_INCLUDE_DIR "")
ENDIF()

FIND_LIBRARY(LIBUNWIND_GENERIC_LIBRARY
    "unwind"
    PATH_SUFFIXES "lib" "src/.libs"
    HINTS ${LIBUNWIND_SEARCH_DIRS})
IF(NOT LIBUNWIND_GENERIC_LIBRARY)
    MESSAGE(FATAL_ERROR "failed to find unwind generic library")
ENDIF()
SET(LIBUNWIND_LIBRARIES ${LIBUNWIND_GENERIC_LIBRARY})

IF(CMAKE_SYSTEM_PROCESSOR MATCHES "^arm")
    SET(LIBUNWIND_ARCH "arm")
ELSEIF(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64")
    SET(LIBUNWIND_ARCH "x86_64")
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES "^i.86$")
    SET(LIBUNWIND_ARCH "x86")
ENDIF()

IF(LIBUNWIND_ARCH)
    FIND_LIBRARY(LIBUNWIND_ARCH_LIBRARY
        "unwind-${LIBUNWIND_ARCH}"
        PATH_SUFFIXES "lib" "src/.libs"
        HINTS ${LIBUNWIND_SEARCH_DIRS})
    IF(NOT LIBUNWIND_ARCH_LIBRARY)
        MESSAGE(FATAL_ERROR "failed to find unwind-${LIBUNWIND_ARCH}")
    ENDIF()
    LIST(APPEND LIBUNWIND_LIBRARIES ${LIBUNWIND_ARCH_LIBRARY})
ENDIF()

MARK_AS_ADVANCED(LIBUNWIND_LIBRARIES LIBUNWIND_INCLUDE_DIR)
