Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 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 <mail@cynapses.org> |
| 14 | # |
| 15 | # Redistribution and use in source and binary forms, with or without |
| 16 | # modification, are permitted provided that the following conditions |
| 17 | # are met: |
| 18 | # |
| 19 | # 1. Redistributions of source code must retain the copyright |
| 20 | # notice, this list of conditions and the following disclaimer. |
| 21 | # 2. Redistributions in binary form must reproduce the copyright |
| 22 | # notice, this list of conditions and the following disclaimer in the |
| 23 | # documentation and/or other materials provided with the distribution. |
| 24 | # 3. The name of the author may not be used to endorse or promote products |
| 25 | # derived from this software without specific prior written permission. |
| 26 | # |
| 27 | # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 28 | # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 29 | # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 30 | # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 31 | # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 32 | # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 33 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 34 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 35 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 36 | # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | # |
| 38 | |
| 39 | function(FIND_PACKAGE_VERSION_CHECK _NAME _FAIL_MSG) |
| 40 | string(TOUPPER ${_NAME} _NAME_UPPER) |
| 41 | set(_AGE "old") |
| 42 | |
| 43 | if(${_NAME}_FIND_VERSION_EXACT) |
| 44 | if (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION) |
| 45 | # exact version found |
| 46 | set(${_NAME_UPPER}_FOUND TRUE) |
| 47 | else (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION) |
| 48 | # exect version not found |
| 49 | set(${_NAME_UPPER}_FOUND FALSE) |
| 50 | # check if newer or older |
| 51 | if (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION) |
| 52 | set(_AGE "new") |
| 53 | else (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION) |
| 54 | set(_AGE "old") |
| 55 | endif (${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION) |
| 56 | endif (${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION) |
| 57 | else (${_NAME}_FIND_VERSION_EXACT) |
| 58 | if (${_NAME}_FIND_VERSION) |
| 59 | if (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION) |
| 60 | set(${_NAME_UPPER}_FOUND FALSE) |
| 61 | set(_AGE "old") |
| 62 | else (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION) |
| 63 | set(${_NAME_UPPER}_FOUND TRUE) |
| 64 | endif (${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION) |
| 65 | endif (${_NAME}_FIND_VERSION) |
| 66 | endif(${_NAME}_FIND_VERSION_EXACT) |
| 67 | |
| 68 | if ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") |
| 69 | if (${_NAME}_FIND_VERSION_EXACT) |
| 70 | set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, version ${${_NAME}_FIND_VERSION} is required.") |
| 71 | else (${_NAME}_FIND_VERSION_EXACT) |
| 72 | set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, at least version ${${_NAME}_FIND_VERSION} is required.") |
| 73 | endif (${_NAME}_FIND_VERSION_EXACT) |
| 74 | else ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") |
| 75 | set(_FAIL_MESSAGE "${_FAIL_MSG}") |
| 76 | endif ("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") |
| 77 | |
| 78 | if (NOT ${_NAME_UPPER}_FOUND) |
| 79 | if (${_NAME}_FIND_REQUIRED) |
| 80 | message(FATAL_ERROR "${_FAIL_MESSAGE}") |
| 81 | else (${_NAME}_FIND_REQUIRED) |
| 82 | if (NOT ${_NAME}_FIND_QUIETLY) |
| 83 | message(STATUS "${_FAIL_MESSAGE}") |
| 84 | endif (NOT ${_NAME}_FIND_QUIETLY) |
| 85 | endif (${_NAME}_FIND_REQUIRED) |
| 86 | endif (NOT ${_NAME_UPPER}_FOUND) |
| 87 | |
| 88 | set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE) |
| 89 | endfunction(FIND_PACKAGE_VERSION_CHECK) |