CMakePushCheckState.cmake 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #.rst:
  2. # CMakePushCheckState
  3. # -------------------
  4. #
  5. #
  6. #
  7. # This module defines three macros: CMAKE_PUSH_CHECK_STATE()
  8. # CMAKE_POP_CHECK_STATE() and CMAKE_RESET_CHECK_STATE() These macros can
  9. # be used to save, restore and reset (i.e., clear contents) the state of
  10. # the variables CMAKE_REQUIRED_FLAGS, CMAKE_REQUIRED_DEFINITIONS,
  11. # CMAKE_REQUIRED_LIBRARIES, CMAKE_REQUIRED_INCLUDES and CMAKE_EXTRA_INCLUDE_FILES
  12. # used by the various Check-files coming with CMake, like e.g.
  13. # check_function_exists() etc. The variable contents are pushed on a
  14. # stack, pushing multiple times is supported. This is useful e.g. when
  15. # executing such tests in a Find-module, where they have to be set, but
  16. # after the Find-module has been executed they should have the same
  17. # value as they had before.
  18. #
  19. # CMAKE_PUSH_CHECK_STATE() macro receives optional argument RESET.
  20. # Whether it's specified, CMAKE_PUSH_CHECK_STATE() will set all
  21. # CMAKE_REQUIRED_* variables to empty values, same as
  22. # CMAKE_RESET_CHECK_STATE() call will do.
  23. #
  24. # Usage:
  25. #
  26. # ::
  27. #
  28. # cmake_push_check_state(RESET)
  29. # set(CMAKE_REQUIRED_DEFINITIONS -DSOME_MORE_DEF)
  30. # check_function_exists(...)
  31. # cmake_reset_check_state()
  32. # set(CMAKE_REQUIRED_DEFINITIONS -DANOTHER_DEF)
  33. # check_function_exists(...)
  34. # cmake_pop_check_state()
  35. #=============================================================================
  36. # Copyright 2006-2011 Alexander Neundorf, <neundorf@kde.org>
  37. #
  38. # Distributed under the OSI-approved BSD License (the "License");
  39. # see accompanying file Copyright.txt for details.
  40. #
  41. # This software is distributed WITHOUT ANY WARRANTY; without even the
  42. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  43. # See the License for more information.
  44. #=============================================================================
  45. # (To distribute this file outside of CMake, substitute the full
  46. # License text for the above reference.)
  47. macro(CMAKE_RESET_CHECK_STATE)
  48. set(CMAKE_EXTRA_INCLUDE_FILES)
  49. set(CMAKE_REQUIRED_INCLUDES)
  50. set(CMAKE_REQUIRED_DEFINITIONS)
  51. set(CMAKE_REQUIRED_LIBRARIES)
  52. set(CMAKE_REQUIRED_FLAGS)
  53. set(CMAKE_REQUIRED_QUIET)
  54. endmacro()
  55. macro(CMAKE_PUSH_CHECK_STATE)
  56. if(NOT DEFINED _CMAKE_PUSH_CHECK_STATE_COUNTER)
  57. set(_CMAKE_PUSH_CHECK_STATE_COUNTER 0)
  58. endif()
  59. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}+1")
  60. set(_CMAKE_EXTRA_INCLUDE_FILES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_EXTRA_INCLUDE_FILES})
  61. set(_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_INCLUDES})
  62. set(_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_DEFINITIONS})
  63. set(_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_LIBRARIES})
  64. set(_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_FLAGS})
  65. set(_CMAKE_REQUIRED_QUIET_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_QUIET})
  66. if (ARGC GREATER 0 AND ARGV0 STREQUAL "RESET")
  67. cmake_reset_check_state()
  68. endif()
  69. endmacro()
  70. macro(CMAKE_POP_CHECK_STATE)
  71. # don't pop more than we pushed
  72. if("${_CMAKE_PUSH_CHECK_STATE_COUNTER}" GREATER "0")
  73. set(CMAKE_EXTRA_INCLUDE_FILES ${_CMAKE_EXTRA_INCLUDE_FILES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  74. set(CMAKE_REQUIRED_INCLUDES ${_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  75. set(CMAKE_REQUIRED_DEFINITIONS ${_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  76. set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  77. set(CMAKE_REQUIRED_FLAGS ${_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  78. set(CMAKE_REQUIRED_QUIET ${_CMAKE_REQUIRED_QUIET_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  79. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}-1")
  80. endif()
  81. endmacro()