FindBacktrace.cmake 5.4 KB

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