CMakePushCheckState.cmake 3.8 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. CMakePushCheckState
  5. -------------------
  6. This module defines three macros: ``CMAKE_PUSH_CHECK_STATE()``
  7. ``CMAKE_POP_CHECK_STATE()`` and ``CMAKE_RESET_CHECK_STATE()`` These macros can
  8. be used to save, restore and reset (i.e., clear contents) the state of
  9. the variables ``CMAKE_REQUIRED_FLAGS``, ``CMAKE_REQUIRED_DEFINITIONS``,
  10. ``CMAKE_REQUIRED_LINK_OPTIONS``, ``CMAKE_REQUIRED_LIBRARIES``,
  11. ``CMAKE_REQUIRED_INCLUDES`` and ``CMAKE_EXTRA_INCLUDE_FILES`` used by the
  12. various Check-files coming with CMake, like e.g. ``check_function_exists()``
  13. etc.
  14. The variable contents are pushed on a stack, pushing multiple times is
  15. supported. This is useful e.g. when executing such tests in a Find-module,
  16. where they have to be set, but after the Find-module has been executed they
  17. should have the same value as they had before.
  18. ``CMAKE_PUSH_CHECK_STATE()`` macro receives optional argument ``RESET``.
  19. Whether it's specified, ``CMAKE_PUSH_CHECK_STATE()`` will set all
  20. ``CMAKE_REQUIRED_*`` variables to empty values, same as
  21. ``CMAKE_RESET_CHECK_STATE()`` call will do.
  22. Usage:
  23. .. code-block:: cmake
  24. cmake_push_check_state(RESET)
  25. set(CMAKE_REQUIRED_DEFINITIONS -DSOME_MORE_DEF)
  26. check_function_exists(...)
  27. cmake_reset_check_state()
  28. set(CMAKE_REQUIRED_DEFINITIONS -DANOTHER_DEF)
  29. check_function_exists(...)
  30. cmake_pop_check_state()
  31. #]=======================================================================]
  32. macro(CMAKE_RESET_CHECK_STATE)
  33. set(CMAKE_EXTRA_INCLUDE_FILES)
  34. set(CMAKE_REQUIRED_INCLUDES)
  35. set(CMAKE_REQUIRED_DEFINITIONS)
  36. set(CMAKE_REQUIRED_LINK_OPTIONS)
  37. set(CMAKE_REQUIRED_LIBRARIES)
  38. set(CMAKE_REQUIRED_FLAGS)
  39. set(CMAKE_REQUIRED_QUIET)
  40. endmacro()
  41. macro(CMAKE_PUSH_CHECK_STATE)
  42. if(NOT DEFINED _CMAKE_PUSH_CHECK_STATE_COUNTER)
  43. set(_CMAKE_PUSH_CHECK_STATE_COUNTER 0)
  44. endif()
  45. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}+1")
  46. set(_CMAKE_EXTRA_INCLUDE_FILES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_EXTRA_INCLUDE_FILES})
  47. set(_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_INCLUDES})
  48. set(_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_DEFINITIONS})
  49. set(_CMAKE_REQUIRED_LINK_OPTIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_LINK_OPTIONS})
  50. set(_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_LIBRARIES})
  51. set(_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_FLAGS})
  52. set(_CMAKE_REQUIRED_QUIET_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_QUIET})
  53. if (${ARGC} GREATER 0 AND "${ARGV0}" STREQUAL "RESET")
  54. cmake_reset_check_state()
  55. endif()
  56. endmacro()
  57. macro(CMAKE_POP_CHECK_STATE)
  58. # don't pop more than we pushed
  59. if("${_CMAKE_PUSH_CHECK_STATE_COUNTER}" GREATER "0")
  60. set(CMAKE_EXTRA_INCLUDE_FILES ${_CMAKE_EXTRA_INCLUDE_FILES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  61. set(CMAKE_REQUIRED_INCLUDES ${_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  62. set(CMAKE_REQUIRED_DEFINITIONS ${_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  63. set(CMAKE_REQUIRED_LINK_OPTIONS ${_CMAKE_REQUIRED_LINK_OPTIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  64. set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  65. set(CMAKE_REQUIRED_FLAGS ${_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  66. set(CMAKE_REQUIRED_QUIET ${_CMAKE_REQUIRED_QUIET_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  67. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}-1")
  68. endif()
  69. endmacro()