CheckVariableExists.cmake 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #=============================================================================
  16. # Copyright 2002-2009 Kitware, Inc.
  17. #
  18. # Distributed under the OSI-approved BSD License (the "License");
  19. # see accompanying file Copyright.txt for details.
  20. #
  21. # This software is distributed WITHOUT ANY WARRANTY; without even the
  22. # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  23. # See the License for more information.
  24. #=============================================================================
  25. # (To distributed this file outside of CMake, substitute the full
  26. # License text for the above reference.)
  27. MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  28. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  29. SET(MACRO_CHECK_VARIABLE_DEFINITIONS
  30. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  31. MESSAGE(STATUS "Looking for ${VAR}")
  32. IF(CMAKE_REQUIRED_LIBRARIES)
  33. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  34. "-DLINK_LIBRARIES:STRING=${CMAKE_REQUIRED_LIBRARIES}")
  35. ELSE(CMAKE_REQUIRED_LIBRARIES)
  36. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  37. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  38. TRY_COMPILE(${VARIABLE}
  39. ${CMAKE_BINARY_DIR}
  40. ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  41. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  42. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  43. "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
  44. OUTPUT_VARIABLE OUTPUT)
  45. IF(${VARIABLE})
  46. SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  47. MESSAGE(STATUS "Looking for ${VAR} - found")
  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(${VARIABLE})
  52. SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  53. MESSAGE(STATUS "Looking for ${VAR} - not found")
  54. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  55. "Determining if the variable ${VAR} exists failed with the following output:\n"
  56. "${OUTPUT}\n\n")
  57. ENDIF(${VARIABLE})
  58. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  59. ENDMACRO(CHECK_VARIABLE_EXISTS)