FindBacktrace.cmake 3.4 KB

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