# - 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       - header file needed for backtrace(3)
#  BACKTRACE_INCLUDE_DIR  - location of backtrace(3) header
#  BACKTRACE_INCLUDE_DIRS - include directories needed to use backtrace(3) header
#  BACKTRACE_LIBRARY      - external library containing backtrace(3), if any
#  BACKTRACE_LIBRARIES    - libraries (linker flags) needed to use backtrace(3), if any
#  BACKTRACE_FOUND        - is set if and only if backtrace(3) support detected
#
# 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)

set(BACKTRACE_HEADER "execinfo.h")
find_path(BACKTRACE_INCLUDE_DIR ${BACKTRACE_HEADER})
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_DIR})
check_symbol_exists("backtrace" ${BACKTRACE_HEADER} _BACKTRACE_SYM_FOUND)
cmake_pop_check_state()

if(_BACKTRACE_SYM_FOUND)
	# Warning: CMAKE_REQUIRED_LIBRARIES could contain non-paths, so
	# don't add BACKTRACE_LIBRARIES to standard args list in this case.
	set(BACKTRACE_LIBRARY)
	set(BACKTRACE_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
	if(NOT BACKTRACE_FIND_QUIETLY)
		message("backtrace detected in default set of libraries: "
		        ${BACKTRACE_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")
	else(BACKTRACE_INCLUDE_DIR)
		set(BACKTRACE_HEADER "backtrace.h")
		find_path(BACKTRACE_INCLUDE_DIR ${BACKTRACE_HEADER})
		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})
find_package_handle_standard_args(backtrace DEFAULT_MSG ${_BACKTRACE_STD_ARGS})
mark_as_advanced(BACKTRACE_HEADER BACKTRACE_INCLUDE_DIR BACKTRACE_INCLUDE_DIRS BACKTRACE_LIBRARIES)
