Debian Codegraph @

cmake/FindBacktrace.cmake

raw
1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #.rst:
5 # FindBacktrace
6 # -------------
7 #
8 # Find provider for backtrace(3).
9 #
10 # Checks if OS supports backtrace(3) via either libc or custom library.
11 # This module defines the following variables:
12 #
13 # ``Backtrace_HEADER``
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.
20 # ``Backtrace_FOUND``
21 # Is set if and only if backtrace(3) support detected.
22 #
23 # The following cache variables are also available to set or use:
24 #
25 # ``Backtrace_LIBRARY``
26 # The external library providing backtrace, if any.
27 # ``Backtrace_INCLUDE_DIR``
28 # The directory holding the backtrace(3) header.
29 #
30 # Typical usage is to generate of header file using configure_file() with the
31 # contents like the following::
32 #
33 # #cmakedefine01 Backtrace_FOUND
34 # #if Backtrace_FOUND
35 # # include <${Backtrace_HEADER}>
36 # #endif
37 #
38 # And then reference that generated header file in actual source.
39
40 include(CMakePushCheckState)
41 include(CheckSymbolExists)
42 include(FindPackageHandleStandardArgs)
43
44 # List of variables to be provided to find_package_handle_standard_args()
45 set(_Backtrace_STD_ARGS Backtrace_INCLUDE_DIR)
46
47 if(Backtrace_HEADER)
48 set(_Backtrace_HEADER_TRY "${Backtrace_HEADER}")
49 else(Backtrace_HEADER)
50 set(_Backtrace_HEADER_TRY "execinfo.h")
51 endif(Backtrace_HEADER)
52
53 find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
54 set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
55
56 if (NOT DEFINED Backtrace_LIBRARY)
57 # First, check if we already have backtrace(), e.g., in libc
58 cmake_push_check_state(RESET)
59 set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
60 set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
61 check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
62 cmake_pop_check_state()
63 endif()
64
65 if(_Backtrace_SYM_FOUND)
66 # Avoid repeating the message() call below each time CMake is run.
67 if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
68 message(STATUS "backtrace facility detected in default set of libraries")
69 endif()
70 set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
71 else()
72 # Check for external library, for non-glibc systems
73 if(Backtrace_INCLUDE_DIR)
74 # OpenBSD has libbacktrace renamed to libexecinfo
75 find_library(Backtrace_LIBRARY "execinfo")
76 elseif() # respect user wishes
77 set(_Backtrace_HEADER_TRY "backtrace.h")
78 find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
79 find_library(Backtrace_LIBRARY "backtrace")
80 endif()
81
82 # Prepend list with library path as it's more common practice
83 set(_Backtrace_STD_ARGS Backtrace_LIBRARY ${_Backtrace_STD_ARGS})
84 endif()
85
86 message(STATUS "Backtrace_LIBRARY: ${Backtrace_LIBRARY}")
87 if(Backtrace_LIBRARY STREQUAL "NOTFOUND")
88 set(Backtrace_LIBRARY "")
89 endif()
90 if(Backtrace_LIBRARY STREQUAL "Backtrace_LIBRARY-NOTFOUND")
91 set(Backtrace_LIBRARY "")
92 endif()
93
94 set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
95 set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")
96
97 find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
98 mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)