CMakePushCheckState.cmake 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # This module defines two macros:
  2. # CMAKE_PUSH_CHECK_STATE()
  3. # and
  4. # CMAKE_POP_CHECK_STATE()
  5. # These two macros can be used to save and restore the state of the variables
  6. # CMAKE_REQUIRED_FLAGS, CMAKE_REQUIRED_DEFINITIONS, CMAKE_REQUIRED_LIBRARIES
  7. # and CMAKE_REQUIRED_INCLUDES used by the various Check-files coming with CMake,
  8. # like e.g. check_function_exists() etc.
  9. # The variable contents are pushed on a stack, pushing multiple times is supported.
  10. # This is useful e.g. when executing such tests in a Find-module, where they have to be set,
  11. # but after the Find-module has been executed they should have the same value
  12. # as they had before.
  13. #
  14. # Usage:
  15. # cmake_push_check_state()
  16. # set(CMAKE_REQUIRED_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} -DSOME_MORE_DEF)
  17. # check_function_exists(...)
  18. # cmake_pop_check_state()
  19. #=============================================================================
  20. # Copyright 2006-2011 Alexander Neundorf, <[email protected]>
  21. #
  22. # Redistribution and use in source and binary forms, with or without
  23. # modification, are permitted provided that the following conditions
  24. # are met:
  25. #
  26. # * Redistributions of source code must retain the above copyright
  27. # notice, this list of conditions and the following disclaimer.
  28. #
  29. # * Redistributions in binary form must reproduce the above copyright
  30. # notice, this list of conditions and the following disclaimer in the
  31. # documentation and/or other materials provided with the distribution.
  32. #
  33. # * Neither the names of Kitware, Inc., the Insight Software Consortium,
  34. # nor the names of their contributors may be used to endorse or promote
  35. # products derived from this software without specific prior written
  36. # permission.
  37. #
  38. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  39. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  40. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  41. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  42. # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  44. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  45. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  46. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  47. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. #
  50. # ------------------------------------------------------------------------------
  51. #
  52. # The above copyright and license notice applies to distributions of
  53. # CMake in source and binary form. Some source files contain additional
  54. # notices of original copyright by their contributors; see each source
  55. # for details. Third-party software packages supplied with CMake under
  56. # compatible licenses provide their own copyright notices documented in
  57. # corresponding subdirectories.
  58. #
  59. # ------------------------------------------------------------------------------
  60. #
  61. # CMake was initially developed by Kitware with the following sponsorship:
  62. #
  63. # * National Library of Medicine at the National Institutes of Health
  64. # as part of the Insight Segmentation and Registration Toolkit (ITK).
  65. #
  66. # * US National Labs (Los Alamos, Livermore, Sandia) ASC Parallel
  67. # Visualization Initiative.
  68. #
  69. # * National Alliance for Medical Image Computing (NAMIC) is funded by the
  70. # National Institutes of Health through the NIH Roadmap for Medical Research,
  71. # Grant U54 EB005149.
  72. #
  73. # * Kitware, Inc.
  74. macro(CMAKE_PUSH_CHECK_STATE)
  75. if(NOT DEFINED _CMAKE_PUSH_CHECK_STATE_COUNTER)
  76. set(_CMAKE_PUSH_CHECK_STATE_COUNTER 0)
  77. endif()
  78. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}+1")
  79. set(_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_INCLUDES})
  80. set(_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_DEFINITIONS})
  81. set(_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_LIBRARIES})
  82. set(_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER} ${CMAKE_REQUIRED_FLAGS})
  83. endmacro()
  84. macro(CMAKE_POP_CHECK_STATE)
  85. # don't pop more than we pushed
  86. if("${_CMAKE_PUSH_CHECK_STATE_COUNTER}" GREATER "0")
  87. set(CMAKE_REQUIRED_INCLUDES ${_CMAKE_REQUIRED_INCLUDES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  88. set(CMAKE_REQUIRED_DEFINITIONS ${_CMAKE_REQUIRED_DEFINITIONS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  89. set(CMAKE_REQUIRED_LIBRARIES ${_CMAKE_REQUIRED_LIBRARIES_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  90. set(CMAKE_REQUIRED_FLAGS ${_CMAKE_REQUIRED_FLAGS_SAVE_${_CMAKE_PUSH_CHECK_STATE_COUNTER}})
  91. math(EXPR _CMAKE_PUSH_CHECK_STATE_COUNTER "${_CMAKE_PUSH_CHECK_STATE_COUNTER}-1")
  92. endif()
  93. endmacro()