FindBacktrace.cmake 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # Copyright 2000-2016 Kitware, Inc.
  39. # Copyright 2000-2011 Insight Software Consortium
  40. # All rights reserved.
  41. #
  42. # Redistribution and use in source and binary forms, with or without
  43. # modification, are permitted provided that the following conditions
  44. # are met:
  45. #
  46. # * Redistributions of source code must retain the above copyright
  47. # notice, this list of conditions and the following disclaimer.
  48. #
  49. # * Redistributions in binary form must reproduce the above copyright
  50. # notice, this list of conditions and the following disclaimer in the
  51. # documentation and/or other materials provided with the distribution.
  52. #
  53. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  54. # nor the names of their contributors may be used to endorse or promote
  55. # products derived from this software without specific prior written
  56. # permission.
  57. #
  58. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  59. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  60. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  61. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  62. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  63. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  64. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  65. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  66. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  67. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  68. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  69. #=============================================================================
  70. include(CMakePushCheckState)
  71. include(CheckSymbolExists)
  72. include(FindPackageHandleStandardArgs)
  73. # List of variables to be provided to find_package_handle_standard_args()
  74. set(_Backtrace_STD_ARGS Backtrace_INCLUDE_DIR)
  75. if(Backtrace_HEADER)
  76. set(_Backtrace_HEADER_TRY "${Backtrace_HEADER}")
  77. else(Backtrace_HEADER)
  78. set(_Backtrace_HEADER_TRY "execinfo.h")
  79. endif(Backtrace_HEADER)
  80. find_path(Backtrace_INCLUDE_DIR "${_Backtrace_HEADER_TRY}")
  81. set(Backtrace_INCLUDE_DIRS ${Backtrace_INCLUDE_DIR})
  82. if (NOT DEFINED Backtrace_LIBRARY)
  83. # First, check if we already have backtrace(), e.g., in libc
  84. cmake_push_check_state(RESET)
  85. set(CMAKE_REQUIRED_INCLUDES ${Backtrace_INCLUDE_DIRS})
  86. set(CMAKE_REQUIRED_QUIET ${Backtrace_FIND_QUIETLY})
  87. check_symbol_exists("backtrace" "${_Backtrace_HEADER_TRY}" _Backtrace_SYM_FOUND)
  88. cmake_pop_check_state()
  89. endif()
  90. if(_Backtrace_SYM_FOUND)
  91. # Avoid repeating the message() call below each time CMake is run.
  92. if(NOT Backtrace_FIND_QUIETLY AND NOT DEFINED Backtrace_LIBRARY)
  93. message(STATUS "backtrace facility detected in default set of libraries")
  94. endif()
  95. set(Backtrace_LIBRARY "" CACHE FILEPATH "Library providing backtrace(3), empty for default set of libraries")
  96. else()
  97. # Check for external library, for non-glibc systems
  98. if(Backtrace_INCLUDE_DIR)
  99. # OpenBSD has libbacktrace renamed to libexecinfo
  100. find_library(Backtrace_LIBRARY "execinfo")
  101. elseif() # respect user wishes
  102. set(_Backtrace_HEADER_TRY "backtrace.h")
  103. find_path(Backtrace_INCLUDE_DIR ${_Backtrace_HEADER_TRY})
  104. find_library(Backtrace_LIBRARY "backtrace")
  105. endif()
  106. # Prepend list with library path as it's more common practice
  107. set(_Backtrace_STD_ARGS Backtrace_LIBRARY ${_Backtrace_STD_ARGS})
  108. endif()
  109. set(Backtrace_LIBRARIES ${Backtrace_LIBRARY})
  110. set(Backtrace_HEADER "${_Backtrace_HEADER_TRY}" CACHE STRING "Header providing backtrace(3) facility")
  111. find_package_handle_standard_args(Backtrace FOUND_VAR Backtrace_FOUND REQUIRED_VARS ${_Backtrace_STD_ARGS})
  112. mark_as_advanced(Backtrace_HEADER Backtrace_INCLUDE_DIR Backtrace_LIBRARY)