1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find provider for `backtrace(3) <https://man7.org/linux/man-pages/man3/backtrace.3.html>`__.
10 Checks if OS supports ``backtrace(3)`` via either ``libc`` or custom library.
11 This module defines the following variables:
14 The header file needed for ``backtrace(3)``. Cached.
15 Could be forcibly set by user.
16 ``Backtrace_INCLUDE_DIRS``
17 The include directories needed to use ``backtrace(3)`` header.
18 ``Backtrace_LIBRARIES``
19 The libraries (linker flags) needed to use ``backtrace(3)``, if any.
21 Is set if and only if ``backtrace(3)`` support detected.
23 The following cache variables are also available to set or use:
26 The external library providing backtrace, if any.
27 ``Backtrace_INCLUDE_DIR``
28 The directory holding the ``backtrace(3)`` header.
30 Typical usage is to generate of header file using :command:`configure_file`
31 with the contents like the following::
33 #cmakedefine01 Backtrace_FOUND
35 # include <${Backtrace_HEADER}>
38 And then reference that generated header file in actual source.
43 .. versionadded:: 3.30
45 This module defines the following :prop_tgt:`IMPORTED` targets:
47 ``Backtrace::Backtrace``
48 An interface library providing usage requirements for the found components.
50 #]=======================================================================]
52 include(CMakePushCheckState)
53 include(CheckSymbolExists)
54 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
56 # List of variables to be provided to find_package_handle_standard_args()
57 set(_Backtrace_STD_ARGS Backtrace_INCLUDE_DIR)
60 set(_Backtrace_HEADER_TRY "${Backtrace_HEADER}")
61 else(Backtrace_HEADER)
62 set(_Backtrace_HEADER_TRY "execinfo.h")
63 endif(Backtrace_HEADER)
65 find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
66 set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
68 if (NOT DEFINED Backtrace_LIBRARY)
69 # First, check if we already have backtrace(), e.g., in libc
70 cmake_push_check_state(RESET)
71 set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
72 set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
73 check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
74 cmake_pop_check_state()
77 if(_Backtrace_SYM_FOUND)
78 # Avoid repeating the message() call below each time CMake is run.
79 if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
80 message(STATUS "backtrace facility detected in default set of libraries")
82 set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
84 # Check for external library, for non-glibc systems
85 if(Backtrace_INCLUDE_DIR)
86 # OpenBSD has libbacktrace renamed to libexecinfo
87 find_library(Backtrace_LIBRARY "execinfo")
88 else() # respect user wishes
89 set(_Backtrace_HEADER_TRY "backtrace.h")
90 find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
91 find_library(Backtrace_LIBRARY "backtrace")
94 # Prepend list with library path as it's more common practice
95 set(_Backtrace_STD_ARGS Backtrace_LIBRARY ${_Backtrace_STD_ARGS})
98 set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
99 set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")
101 find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
102 mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)
104 if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
105 if(Backtrace_LIBRARY)
106 add_library(Backtrace::Backtrace UNKNOWN IMPORTED)
107 set_property(TARGET Backtrace::Backtrace PROPERTY IMPORTED_LOCATION "${Backtrace_LIBRARY}")
109 add_library(Backtrace::Backtrace INTERFACE IMPORTED)
110 target_link_libraries(Backtrace::Backtrace INTERFACE ${Backtrace_LIBRARIES})
112 target_include_directories(Backtrace::Backtrace INTERFACE ${Backtrace_INCLUDE_DIRS})