CheckVariableExists.cmake 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #
  2. # Check if the variable exists.
  3. # # CHECK_VARIABLE_EXISTS - macro which checks if the variable exists
  4. # VAR - the name of the variable
  5. # VARIABLE - variable to store the result
  6. #
  7. # If CMAKE_REQUIRED_FLAGS is set then those flags will be passed into the
  8. # compile of the program likewise if CMAKE_REQUIRED_LIBRARIES is set then
  9. # those libraries will be linked against the test program
  10. #
  11. # only for C variables
  12. #
  13. MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  14. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  15. SET(MACRO_CHECK_VARIABLE_DEFINITIONS
  16. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  17. MESSAGE(STATUS "Looking for ${VAR}")
  18. IF(CMAKE_REQUIRED_LIBRARIES)
  19. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  20. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  21. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  22. TRY_COMPILE(${VARIABLE}
  23. ${CMAKE_BINARY_DIR}
  24. ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  25. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  26. "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
  27. OUTPUT_VARIABLE OUTPUT)
  28. IF(${VARIABLE})
  29. SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  30. MESSAGE(STATUS "Looking for ${VAR} - found")
  31. FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeOutput.log
  32. "Determining if the variable ${VAR} exists passed with the following output:\n"
  33. "${OUTPUT}\n\n")
  34. ELSE(${VARIABLE})
  35. SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  36. MESSAGE(STATUS "Looking for ${VAR} - not found")
  37. FILE(APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log
  38. "Determining if the variable ${VAR} exists failed with the following output:\n"
  39. "${OUTPUT}\n\n")
  40. ENDIF(${VARIABLE})
  41. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  42. ENDMACRO(CHECK_VARIABLE_EXISTS)