# - Find provider for backtrace(3)
# Checks if OS supports backtrace(3) via either libc or custom library.
# This module defines the following variables:
#  BACKTRACE_HEADER       - The header file needed for backtrace(3). Cached.
#                           Could be forcibly set by user.
#  BACKTRACE_INCLUDE_DIRS - The include directories needed to use backtrace(3) header.
#  BACKTRACE_LIBRARIES    - The libraries (linker flags) needed to use backtrace(3), if any.
#  BACKTRACE_FOUND        - Is set if and only if backtrace(3) support detected.
#
# The following cache variables are also available to set or use:
#   BACKTRACE_LIBRARY     - The external library providing backtrace, if any.
#   BACKTRACE_INCLUDE_DIR - The directory holding the backtrace(3) header.
#
# Typical usage is to generate of header file using configure_file() with the
# contents like the following:
#  #cmakedefine01 BACKTRACE_FOUND
#  #if BACKTRACE_FOUND
#  # include <${BACKTRACE_HEADER}>
#  #endif
# And then reference that generated header file in actual source.

#=============================================================================
# Copyright 2013 Vadim Zhukov
#
# Distributed under the OSI-approved BSD License (the "License");
# see accompanying file Copyright.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even the
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the License for more information.
#=============================================================================
# (To distribute this file outside of CMake, substitute the full
#  License text for the above reference.)


include(CMakePushCheckState)
include(CheckSymbolExists)
include(FindPackageHandleStandardArgs)

# List of variables to be provided to find_package_handle_standard_args()
set(_BACKTRACE_STD_ARGS BACKTRACE_INCLUDE_DIR)

if(BACKTRACE_HEADER)
	set(_BACKTRACE_HEADER_TRY "${BACKTRACE_HEADER}")
else(BACKTRACE_HEADER)
	set(_BACKTRACE_HEADER_TRY "execinfo.h")
endif(BACKTRACE_HEADER)

find_path(BACKTRACE_INCLUDE_DIR "${_BACKTRACE_HEADER_TRY}")
set(BACKTRACE_INCLUDE_DIRS ${BACKTRACE_INCLUDE_DIR})
 
# First, check if we already have backtrace(), e.g., in libc
cmake_push_check_state()
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_REQUIRED_INCLUDES} ${BACKTRACE_INCLUDE_DIRS})
check_symbol_exists("backtrace" "${_BACKTRACE_HEADER_TRY}" _BACKTRACE_SYM_FOUND)
cmake_pop_check_state()

if(_BACKTRACE_SYM_FOUND)
	set(BACKTRACE_LIBRARY)
	if(NOT BACKTRACE_FIND_QUIETLY)
		message("backtrace detected in default set of libraries: "
		        ${CMAKE_REQUIRED_LIBRARIES})
	endif(NOT BACKTRACE_FIND_QUIETLY)
else(_BACKTRACE_SYM_FOUND)
	# Check for external library, for non-glibc systems
	if(BACKTRACE_INCLUDE_DIR)
		# OpenBSD has libbacktrace renamed to libexecinfo
		find_library(BACKTRACE_LIBRARY "execinfo")
	elseif(NOT BACKTRACE_HEADER)     # respect user wishes
		set(_BACKTRACE_HEADER_TRY "backtrace.h")
		find_path(BACKTRACE_INCLUDE_DIR ${_BACKTRACE_HEADER_TRY})
		find_library(BACKTRACE_LIBRARY "backtrace")
	endif(BACKTRACE_INCLUDE_DIR)

	# Prepend list with library path as it's more common practice
	set(_BACKTRACE_STD_ARGS BACKTRACE_LIBRARIES ${_BACKTRACE_STD_ARGS})
endif(_BACKTRACE_SYM_FOUND)

set(BACKTRACE_LIBRARIES ${BACKTRACE_LIBRARY})
set(BACKTRACE_HEADER "${_BACKTRACE_HEADER_TRY}" CACHE INTERNAL "Header providing backtrace(3) facility")

find_package_handle_standard_args(backtrace DEFAULT_MSG ${_BACKTRACE_STD_ARGS})
mark_as_advanced(BACKTRACE_HEADER BACKTRACE_INCLUDE_DIR BACKTRACE_LIBRARY)
