CheckVariableExists.cmake 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. ::
  8. CHECK_VARIABLE_EXISTS(VAR VARIABLE)
  9. ::
  10. VAR - the name of the variable
  11. VARIABLE - variable to store the result
  12. Will be created as an internal cache variable.
  13. This macro is only for C variables.
  14. The following variables may be set before calling this macro to modify
  15. the way the check is run:
  16. ::
  17. CMAKE_REQUIRED_FLAGS = string of compile command line flags
  18. CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  19. CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  20. CMAKE_REQUIRED_QUIET = execute quietly without messages
  21. #]=======================================================================]
  22. include_guard(GLOBAL)
  23. macro(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  24. if(NOT DEFINED "${VARIABLE}")
  25. set(MACRO_CHECK_VARIABLE_DEFINITIONS
  26. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  27. if(NOT CMAKE_REQUIRED_QUIET)
  28. message(STATUS "Looking for ${VAR}")
  29. endif()
  30. if(CMAKE_REQUIRED_LIBRARIES)
  31. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  32. LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  33. else()
  34. set(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  35. endif()
  36. try_compile(${VARIABLE}
  37. ${CMAKE_BINARY_DIR}
  38. ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  39. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  40. ${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}
  41. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  42. OUTPUT_VARIABLE OUTPUT)
  43. if(${VARIABLE})
  44. set(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  45. if(NOT CMAKE_REQUIRED_QUIET)
  46. message(STATUS "Looking for ${VAR} - found")
  47. endif()
  48. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  49. "Determining if the variable ${VAR} exists passed with the following output:\n"
  50. "${OUTPUT}\n\n")
  51. else()
  52. set(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  53. if(NOT CMAKE_REQUIRED_QUIET)
  54. message(STATUS "Looking for ${VAR} - not found")
  55. endif()
  56. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  57. "Determining if the variable ${VAR} exists failed with the following output:\n"
  58. "${OUTPUT}\n\n")
  59. endif()
  60. endif()
  61. endmacro()