FindBacktrace.cmake 3.7 KB

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