CheckVariableExists.cmake 2.4 KB

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