1
0

CheckVariableExists.cmake 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. ``CMAKE_REQUIRED_FLAGS``
  18. string of compile command line flags.
  19. ``CMAKE_REQUIRED_DEFINITIONS``
  20. list of macros to define (-DFOO=bar).
  21. ``CMAKE_REQUIRED_LINK_OPTIONS``
  22. .. versionadded:: 3.14
  23. list of options to pass to link command.
  24. ``CMAKE_REQUIRED_LIBRARIES``
  25. list of libraries to link.
  26. ``CMAKE_REQUIRED_QUIET``
  27. .. versionadded:: 3.1
  28. execute quietly without messages.
  29. #]=======================================================================]
  30. include_guard(GLOBAL)
  31. macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  32. if(NOT DEFINED "${VARIABLE}")
  33. set(MACRO_CHECK_VARIABLE_DEFINITIONS
  34. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  35. if(NOT CMAKE_REQUIRED_QUIET)
  36. message(CHECK_START "Looking for ${VAR}")
  37. endif()
  38. if(CMAKE_REQUIRED_LINK_OPTIONS)
  39. set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS
  40. LINK_OPTIONS ${CMAKE_REQUIRED_LINK_OPTIONS})
  41. else()
  42. set(CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS)
  43. endif()
  44. if(CMAKE_REQUIRED_LIBRARIES)
  45. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  46. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  47. else()
  48. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  49. endif()
  50. try_compile(${VARIABLE}
  51. SOURCES ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  52. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  53. ${CHECK_VARIABLE_EXISTS_ADD_LINK_OPTIONS}
  54. ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
  55. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  56. OUTPUT_VARIABLE OUTPUT)
  57. if(${VARIABLE})
  58. set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  59. if(NOT CMAKE_REQUIRED_QUIET)
  60. message(CHECK_PASS "found")
  61. endif()
  62. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  63. "Determining if the variable ${VAR} exists passed with the following output:\n"
  64. "${OUTPUT}\n\n")
  65. else()
  66. set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  67. if(NOT CMAKE_REQUIRED_QUIET)
  68. message(CHECK_FAIL "not found")
  69. endif()
  70. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  71. "Determining if the variable ${VAR} exists failed with the following output:\n"
  72. "${OUTPUT}\n\n")
  73. endif()
  74. endif()
  75. endmacro()