CheckVariableExists.cmake 2.9 KB

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