5 # Find provider for backtrace(3).
7 # Checks if OS supports backtrace(3) via either libc or custom library.
8 # This module defines the following variables:
10 # ``Backtrace_HEADER``
11 # The header file needed for backtrace(3). Cached.
12 # Could be forcibly set by user.
13 # ``Backtrace_INCLUDE_DIRS``
14 # The include directories needed to use backtrace(3) header.
15 # ``Backtrace_LIBRARIES``
16 # The libraries (linker flags) needed to use backtrace(3), if any.
18 # Is set if and only if backtrace(3) support detected.
20 # The following cache variables are also available to set or use:
22 # ``Backtrace_LIBRARY``
23 # The external library providing backtrace, if any.
24 # ``Backtrace_INCLUDE_DIR``
25 # The directory holding the backtrace(3) header.
27 # Typical usage is to generate of header file using configure_file() with the
28 # contents like the following::
30 # #cmakedefine01 Backtrace_FOUND
32 # # include <${Backtrace_HEADER}>
35 # And then reference that generated header file in actual source.
37 #=============================================================================
38 # Copyright 2013 Vadim Zhukov
40 # Distributed under the OSI-approved BSD License (the "License");
41 # see accompanying file Copyright.txt for details.
43 # This software is distributed WITHOUT ANY WARRANTY; without even the
44 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
45 # See the License for more information.
46 #=============================================================================
47 # (To distribute this file outside of CMake, substitute the full
48 # License text for the above reference.)
51 include(CMakePushCheckState)
52 include(CheckSymbolExists)
53 include(FindPackageHandleStandardArgs)
55 # List of variables to be provided to find_package_handle_standard_args()
56 set(_Backtrace_STD_ARGS Backtrace_INCLUDE_DIR)
59 set(_Backtrace_HEADER_TRY "${Backtrace_HEADER}")
60 else(Backtrace_HEADER)
61 set(_Backtrace_HEADER_TRY "execinfo.h")
62 endif(Backtrace_HEADER)
64 find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
65 set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
67 if (NOT DEFINED Backtrace_LIBRARY)
68 # First, check if we already have backtrace(), e.g., in libc
69 cmake_push_check_state(RESET)
70 set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
71 set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
72 check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
73 cmake_pop_check_state()
76 if(_Backtrace_SYM_FOUND)
77 # Avoid repeating the message() call below each time CMake is run.
78 if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
79 message(STATUS "backtrace facility detected in default set of libraries")
81 set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
83 # Check for external library, for non-glibc systems
84 if(Backtrace_INCLUDE_DIR)
85 # OpenBSD has libbacktrace renamed to libexecinfo
86 find_library(Backtrace_LIBRARY "execinfo")
87 elseif() # respect user wishes
88 set(_Backtrace_HEADER_TRY "backtrace.h")
89 find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
90 find_library(Backtrace_LIBRARY "backtrace")
93 # Prepend list with library path as it's more common practice
94 set(_Backtrace_STD_ARGS Backtrace_LIBRARY ${_Backtrace_STD_ARGS})
97 set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
98 set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")
100 find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
101 mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)