CheckVariableExists.cmake 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 distribute this file outside of CMake, substitute the full
  26. # License text for the above reference.)
  27. INCLUDE("${CMAKE_CURRENT_LIST_DIR}/CMakeExpandImportedTargets.cmake")
  28. MACRO(CHECK_VARIABLE_EXISTS VAR VARIABLE)
  29. IF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  30. SET(MACRO_CHECK_VARIABLE_DEFINITIONS
  31. "-DCHECK_VARIABLE_EXISTS=${VAR} ${CMAKE_REQUIRED_FLAGS}")
  32. MESSAGE(STATUS "Looking for ${VAR}")
  33. IF(CMAKE_REQUIRED_LIBRARIES)
  34. # this one translates potentially used imported library targets to their files on disk
  35. CMAKE_EXPAND_IMPORTED_TARGETS(_ADJUSTED_CMAKE_REQUIRED_LIBRARIES LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} CONFIGURATION "${CMAKE_TRY_COMPILE_CONFIGURATION}")
  36. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES
  37. "-DLINK_LIBRARIES:STRING=${_ADJUSTED_CMAKE_REQUIRED_LIBRARIES}")
  38. ELSE(CMAKE_REQUIRED_LIBRARIES)
  39. SET(CHECK_VARIABLE_EXISTS_ADD_LIBRARIES)
  40. ENDIF(CMAKE_REQUIRED_LIBRARIES)
  41. TRY_COMPILE(${VARIABLE}
  42. ${CMAKE_BINARY_DIR}
  43. ${CMAKE_ROOT}/Modules/CheckVariableExists.c
  44. COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS}
  45. CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_VARIABLE_DEFINITIONS}
  46. "${CHECK_VARIABLE_EXISTS_ADD_LIBRARIES}"
  47. OUTPUT_VARIABLE OUTPUT)
  48. IF(${VARIABLE})
  49. SET(${VARIABLE} 1 CACHE INTERNAL "Have variable ${VAR}")
  50. MESSAGE(STATUS "Looking for ${VAR} - found")
  51. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log
  52. "Determining if the variable ${VAR} exists passed with the following output:\n"
  53. "${OUTPUT}\n\n")
  54. ELSE(${VARIABLE})
  55. SET(${VARIABLE} "" CACHE INTERNAL "Have variable ${VAR}")
  56. MESSAGE(STATUS "Looking for ${VAR} - not found")
  57. FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  58. "Determining if the variable ${VAR} exists failed with the following output:\n"
  59. "${OUTPUT}\n\n")
  60. ENDIF(${VARIABLE})
  61. ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$")
  62. ENDMACRO(CHECK_VARIABLE_EXISTS)