CheckVariableExists.cmake 2.6 KB

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