File: FindBacktrace.cmake

package info (click to toggle)
cmake 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 147,284 kB
  • sloc: ansic: 403,915; cpp: 290,772; sh: 4,102; python: 3,357; yacc: 3,106; lex: 1,189; f90: 532; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 230; perl: 217; objc: 215; xml: 198; makefile: 97; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (168 lines) | stat: -rw-r--r-- 5,504 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file LICENSE.rst or https://cmake.org/licensing for details.

#[=======================================================================[.rst:
FindBacktrace
-------------

Finds `backtrace(3) <https://man7.org/linux/man-pages/man3/backtrace.3.html>`_,
a library that provides functions for application self-debugging.

This module checks whether ``backtrace(3)`` is supported, either through the
standard C library (``libc``), or a separate library.

Imported Targets
^^^^^^^^^^^^^^^^

.. versionadded:: 3.30

This module provides the following :ref:`Imported Targets`:

``Backtrace::Backtrace``
  An interface library encapsulating the usage requirements of Backtrace.  This
  target is available only when Backtrace is found.

Result Variables
^^^^^^^^^^^^^^^^

This module defines the following variables:

``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``
  Boolean indicating whether the ``backtrace(3)`` support is available.

Cache Variables
^^^^^^^^^^^^^^^

The following cache variables are also available to set or use:

``Backtrace_HEADER``
  The header file needed for ``backtrace(3)``.  This variable allows dynamic
  usage of the header in the project code.  It can also be overridden by the
  user.
``Backtrace_LIBRARY``
  The external library providing backtrace, if any.
``Backtrace_INCLUDE_DIR``
  The directory holding the ``backtrace(3)`` header.

Examples
^^^^^^^^

Finding Backtrace and linking it to a project target as of CMake 3.30:

.. code-block:: cmake
  :caption: CMakeLists.txt

  find_package(Backtrace)
  target_link_libraries(app PRIVATE Backtrace::Backtrace)

The ``Backtrace_HEADER`` variable can be used, for example, in a configuration
header file created by :command:`configure_file`:

.. code-block:: cmake
  :caption: CMakeLists.txt

  add_library(app app.c)

  find_package(Backtrace)
  target_link_libraries(app PRIVATE Backtrace::Backtrace)

  configure_file(config.h.in config.h)

.. code-block:: c
  :caption: config.h.in

  #cmakedefine01 Backtrace_FOUND
  #if Backtrace_FOUND
  #  include <@Backtrace_HEADER@>
  #endif

.. code-block:: c
  :caption: app.c

  #include "config.h"

If the project needs to support CMake 3.29 or earlier, the imported target can
be defined manually:

.. code-block:: cmake
  :caption: CMakeLists.txt

  find_package(Backtrace)
  if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
    add_library(Backtrace::Backtrace INTERFACE IMPORTED)
    set_target_properties(
      Backtrace::Backtrace
      PROPERTIES
        INTERFACE_LINK_LIBRARIES "${Backtrace_LIBRARIES}"
        INTERFACE_INCLUDE_DIRECTORIES "${Backtrace_INCLUDE_DIRS}"
    )
  endif()
  target_link_libraries(app PRIVATE Backtrace::Backtrace)
#]=======================================================================]

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()
  set(_Backtrace_HEADER_TRY "execinfo.h")
endif()

find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})

if (NOT DEFINED Backtrace_LIBRARY)
  # First, check if we already have backtrace(), e.g., in libc
  cmake_push_check_state(RESET)
  set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
  set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
  check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
  cmake_pop_check_state()
endif()

if(_Backtrace_SYM_FOUND)
  # Avoid repeating the message() call below each time CMake is run.
  if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
    message(STATUS "backtrace facility detected in default set of libraries")
  endif()
  set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
else()
  # Check for external library, for non-glibc systems
  if(Backtrace_INCLUDE_DIR)
    # OpenBSD has libbacktrace renamed to libexecinfo
    find_library(Backtrace_LIBRARY "execinfo")
  else()     # respect user wishes
    set(_Backtrace_HEADER_TRY "backtrace.h")
    find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
    find_library(Backtrace_LIBRARY "backtrace")
  endif()

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

set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")

find_package_handle_standard_args(Backtrace REQUIRED_VARS ${_Backtrace_STD_ARGS})
mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)

if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
  if(Backtrace_LIBRARY)
    add_library(Backtrace::Backtrace UNKNOWN IMPORTED)
    set_property(TARGET Backtrace::Backtrace PROPERTY IMPORTED_LOCATION "${Backtrace_LIBRARY}")
  else()
    add_library(Backtrace::Backtrace INTERFACE IMPORTED)
    target_link_libraries(Backtrace::Backtrace INTERFACE ${Backtrace_LIBRARIES})
  endif()
  target_include_directories(Backtrace::Backtrace INTERFACE ${Backtrace_INCLUDE_DIRS})
endif()