FindBacktrace.cmake 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. # file LICENSE.rst or https://cmake.org/licensing for details.
  3. #[=======================================================================[.rst:
  4. FindBacktrace
  5. -------------
  6. Finds `backtrace(3) <https://man7.org/linux/man-pages/man3/backtrace.3.html>`_,
  7. a library that provides functions for application self-debugging:
  8. .. code-block:: cmake
  9. find_package(Backtrace [...])
  10. This module checks whether ``backtrace(3)`` is supported, either through the
  11. standard C library (``libc``), or a separate library.
  12. Imported Targets
  13. ^^^^^^^^^^^^^^^^
  14. This module provides the following :ref:`Imported Targets`:
  15. ``Backtrace::Backtrace``
  16. .. versionadded:: 3.30
  17. An interface library encapsulating the usage requirements of Backtrace. This
  18. target is available only when Backtrace is found.
  19. Result Variables
  20. ^^^^^^^^^^^^^^^^
  21. This module defines the following variables:
  22. ``Backtrace_FOUND``
  23. Boolean indicating whether the ``backtrace(3)`` support is available.
  24. ``Backtrace_INCLUDE_DIRS``
  25. The include directories needed to use ``backtrace(3)`` header.
  26. ``Backtrace_LIBRARIES``
  27. The libraries (linker flags) needed to use ``backtrace(3)``, if any.
  28. Cache Variables
  29. ^^^^^^^^^^^^^^^
  30. The following cache variables are also available to set or use:
  31. ``Backtrace_HEADER``
  32. The header file needed for ``backtrace(3)``. This variable allows dynamic
  33. usage of the header in the project code. It can also be overridden by the
  34. user.
  35. ``Backtrace_INCLUDE_DIR``
  36. The directory holding the ``backtrace(3)`` header.
  37. ``Backtrace_LIBRARY``
  38. The external library providing backtrace, if any.
  39. Examples
  40. ^^^^^^^^
  41. Finding Backtrace and linking it to a project target as of CMake 3.30:
  42. .. code-block:: cmake
  43. :caption: CMakeLists.txt
  44. find_package(Backtrace)
  45. target_link_libraries(app PRIVATE Backtrace::Backtrace)
  46. The ``Backtrace_HEADER`` variable can be used, for example, in a configuration
  47. header file created by :command:`configure_file`:
  48. .. code-block:: cmake
  49. :caption: CMakeLists.txt
  50. add_library(app app.c)
  51. find_package(Backtrace)
  52. target_link_libraries(app PRIVATE Backtrace::Backtrace)
  53. configure_file(config.h.in config.h)
  54. .. code-block:: c
  55. :caption: config.h.in
  56. #cmakedefine01 Backtrace_FOUND
  57. #if Backtrace_FOUND
  58. # include <@Backtrace_HEADER@>
  59. #endif
  60. .. code-block:: c
  61. :caption: app.c
  62. #include "config.h"
  63. If the project needs to support CMake 3.29 or earlier, the imported target can
  64. be defined manually:
  65. .. code-block:: cmake
  66. :caption: CMakeLists.txt
  67. find_package(Backtrace)
  68. if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
  69. add_library(Backtrace::Backtrace INTERFACE IMPORTED)
  70. set_target_properties(
  71. Backtrace::Backtrace
  72. PROPERTIES
  73. INTERFACE_LINK_LIBRARIES "${Backtrace_LIBRARIES}"
  74. INTERFACE_INCLUDE_DIRECTORIES "${Backtrace_INCLUDE_DIRS}"
  75. )
  76. endif()
  77. target_link_libraries(app PRIVATE Backtrace::Backtrace)
  78. #]=======================================================================]
  79. include(CMakePushCheckState)
  80. include(CheckSymbolExists)
  81. include(FindPackageHandleStandardArgs)
  82. # List of variables to be provided to find_package_handle_standard_args()
  83. set(_Backtrace_STD_ARGS Backtrace_INCLUDE_DIR)
  84. if(Backtrace_HEADER)
  85. set(_Backtrace_HEADER_TRY "${Backtrace_HEADER}")
  86. else()
  87. set(_Backtrace_HEADER_TRY "execinfo.h")
  88. endif()
  89. find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
  90. set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
  91. if (NOT DEFINED Backtrace_LIBRARY)
  92. # First, check if we already have backtrace(), e.g., in libc
  93. cmake_push_check_state(RESET)
  94. set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
  95. set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
  96. check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
  97. cmake_pop_check_state()
  98. endif()
  99. if(_Backtrace_SYM_FOUND)
  100. # Avoid repeating the message() call below each time CMake is run.
  101. if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
  102. message(STATUS "backtrace facility detected in default set of libraries")
  103. endif()
  104. set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
  105. else()
  106. # Check for external library, for non-glibc systems
  107. if(Backtrace_INCLUDE_DIR)
  108. # OpenBSD has libbacktrace renamed to libexecinfo
  109. find_library(Backtrace_LIBRARY "execinfo")
  110. else() # respect user wishes
  111. set(_Backtrace_HEADER_TRY "backtrace.h")
  112. find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
  113. find_library(Backtrace_LIBRARY "backtrace")
  114. endif()
  115. # Prepend list with library path as it's more common practice
  116. set(_Backtrace_STD_ARGS Backtrace_LIBRARY ${_Backtrace_STD_ARGS})
  117. endif()
  118. set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
  119. set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")
  120. find_package_handle_standard_args(Backtrace REQUIRED_VARS ${_Backtrace_STD_ARGS})
  121. mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)
  122. if(Backtrace_FOUND AND NOT TARGET Backtrace::Backtrace)
  123. if(Backtrace_LIBRARY)
  124. add_library(Backtrace::Backtrace UNKNOWN IMPORTED)
  125. set_property(TARGET Backtrace::Backtrace PROPERTY IMPORTED_LOCATION "${Backtrace_LIBRARY}")
  126. else()
  127. add_library(Backtrace::Backtrace INTERFACE IMPORTED)
  128. target_link_libraries(Backtrace::Backtrace INTERFACE ${Backtrace_LIBRARIES})
  129. endif()
  130. target_include_directories(Backtrace::Backtrace INTERFACE ${Backtrace_INCLUDE_DIRS})
  131. endif()