1
0

CheckVariableExists.cmake 1.8 KB

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