CheckVariableExists.cmake 2.6 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. CheckVariableExists
  5. -------------------
  6. Check if the variable exists.
  7. .. command:: CHECK_VARIABLE_EXISTS
  8. .. code-block:: cmake
  9. CHECK_VARIABLE_EXISTS(VAR VARIABLE)
  10. ::
  11. VAR - the name of the variable
  12. VARIABLE - variable to store the result
  13. Will be created as an internal cache variable.
  14. This macro is only for ``C`` variables.
  15. The following variables may be set before calling this macro to modify
  16. the way the check is run:
  17. .. include:: /module/CMAKE_REQUIRED_FLAGS.txt
  18. .. include:: /module/CMAKE_REQUIRED_DEFINITIONS.txt
  19. .. include:: /module/CMAKE_REQUIRED_LINK_OPTIONS.txt
  20. .. include:: /module/CMAKE_REQUIRED_LIBRARIES.txt
  21. .. include:: /module/CMAKE_REQUIRED_QUIET.txt
  22. #]=======================================================================]
  23. include_guard(GLOBAL)
  24. macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  25. if(NOT DEFINED "${VARIABLE}")
  26. set(MACRO_CHECK_VARIABLE_DEFINITIONS
  27. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  28. if(NOT CMAKE_REQUIRED_QUIET)
  29. message(CHECK_START "Looking for ${VAR}")
  30. endif()
  31. if(CMAKE_REQUIRED_LINK_OPTIONS)
  32. set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS
  33. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  34. else()
  35. set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS)
  36. endif()
  37. if(CMAKE_REQUIRED_LIBRARIES)
  38. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  39. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  40. else()
  41. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  42. endif()
  43. if(CMAKE_REQUIRED_LINK_DIRECTORIES)
  44. set(_CVE_LINK_DIRECTORIES
  45. "-DLINK_DIRECTORIES:STRING=${CMAKE_REQUIRED_LINK_DIRECTORIES}")
  46. else()
  47. set(_CVE_LINK_DIRECTORIES)
  48. endif()
  49. try_compile(${VARIABLE}
  50. SOURCES ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  51. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  52. ${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
  53. ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
  54. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  55. "${_CVE_LINK_DIRECTORIES}"
  56. )
  57. unset(_CVE_LINK_DIRECTORIES)
  58. if(${VARIABLE})
  59. set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  60. if(NOT CMAKE_REQUIRED_QUIET)
  61. message(CHECK_PASS "found")
  62. endif()
  63. else()
  64. set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  65. if(NOT CMAKE_REQUIRED_QUIET)
  66. message(CHECK_FAIL "not found")
  67. endif()
  68. endif()
  69. endif()
  70. endmacro()