CheckVariableExists.cmake 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # - Check if the variable exists.
  2. # CHECK_VARIABLE_EXISTS(VAR VARIABLE)
  3. #
  4. # VAR - the name of the variable
  5. # VARIABLE - variable to store the result
  6. #
  7. # This macro is only for C variables.
  8. #
  9. # The following variables may be set before calling this macro to
  10. # modify the way the check is run:
  11. #
  12. # CMAKE_REQUIRED_FLAGS = string of compile command line flags
  13. # CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar)
  14. # CMAKE_REQUIRED_LIBRARIES = list of libraries to link
  15. MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  16. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  17. SET(MACRO_CHECK_VARIABLE_DEFINITIONS
  18. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  19. MESSAGE(STATUS "Looking for ${VAR}")
  20. IF(CMAKE_REQUIRED_LIBRARIES)
  21. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  22. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  23. ELSE(CMAKE_REQUIRED_LIBRARIES)
  24. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  25. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  26. TRY_COMPILE(${VARIABLE}
  27. ${CMAKE_BINARY_DIR}
  28. ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  29. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  30. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  31. "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
  32. OUTPUT_VARIABLE OUTPUT)
  33. IF(${VARIABLE})
  34. SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  35. MESSAGE(STATUS "Looking for ${VAR} - found")
  36. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  37. "Determining if the variable ${VAR} exists passed with the following output:\n"
  38. "${OUTPUT}\n\n")
  39. ELSE(${VARIABLE})
  40. SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  41. MESSAGE(STATUS "Looking for ${VAR} - not found")
  42. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  43. "Determining if the variable ${VAR} exists failed with the following output:\n"
  44. "${OUTPUT}\n\n")
  45. ENDIF(${VARIABLE})
  46. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  47. ENDMACRO(CHECK_VARIABLE_EXISTS)