FindPackageVersionCheck.cmake 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # FIND_PACKAGE_VERSION_CHECK(NAME (DEFAULT_MSG|"Custom failure message"))
  2. # This function is intended to be used in FindXXX.cmake modules files.
  3. # It handles NAME_FIND_VERSION and NAME_VERSION variables in a Module.
  4. #
  5. # Example:
  6. # find_package(LibSSH 0.3.2)
  7. #
  8. # # check for the version and set it
  9. # set(LibSSH_VERSION 0.3.0)
  10. # find_package_version_check(LibSSH DEFAULT_MSG)
  11. #
  12. #
  13. # Copyright (c) 2009 Andreas Schneider <asn@cryptomilk.org>
  14. #
  15. # Redistribution and use is allowed according to the terms of the New
  16. # BSD license.
  17. function(FIND_PACKAGE_VERSION_CHECK _NAME _FAIL_MSG)
  18. string(TOUPPER ${_NAME} _NAME_UPPER)
  19. set(_AGE "old")
  20. if(${_NAME}_FIND_VERSION_EXACT)
  21. if (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
  22. # exact version found
  23. set(${_NAME_UPPER}_FOUND TRUE)
  24. else (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
  25. # exect version not found
  26. set(${_NAME_UPPER}_FOUND FALSE)
  27. # check if newer or older
  28. if (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
  29. set(_AGE "new")
  30. else (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
  31. set(_AGE "old")
  32. endif (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
  33. endif (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
  34. else (${_NAME}_FIND_VERSION_EXACT)
  35. if (${_NAME}_FIND_VERSION)
  36. if (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
  37. set(${_NAME_UPPER}_FOUND FALSE)
  38. set(_AGE "old")
  39. else (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
  40. set(${_NAME_UPPER}_FOUND TRUE)
  41. endif (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
  42. endif (${_NAME}_FIND_VERSION)
  43. endif(${_NAME}_FIND_VERSION_EXACT)
  44. if ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
  45. if (${_NAME}_FIND_VERSION_EXACT)
  46. set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, version ${${_NAME}_FIND_VERSION} is required.")
  47. else (${_NAME}_FIND_VERSION_EXACT)
  48. set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, at least version ${${_NAME}_FIND_VERSION} is required.")
  49. endif (${_NAME}_FIND_VERSION_EXACT)
  50. else ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
  51. set(_FAIL_MESSAGE "${_FAIL_MSG}")
  52. endif ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
  53. if (NOT ${_NAME_UPPER}_FOUND)
  54. if (${_NAME}_FIND_REQUIRED)
  55. message(FATAL_ERROR "${_FAIL_MESSAGE}")
  56. else (${_NAME}_FIND_REQUIRED)
  57. if (NOT ${_NAME}_FIND_QUIETLY)
  58. message(STATUS "${_FAIL_MESSAGE}")
  59. endif (NOT ${_NAME}_FIND_QUIETLY)
  60. endif (${_NAME}_FIND_REQUIRED)
  61. endif (NOT ${_NAME_UPPER}_FOUND)
  62. set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
  63. endfunction(FIND_PACKAGE_VERSION_CHECK)