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. |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 24 | # 3. The name of the author may not be used to endorse or promote products |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 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) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 44 | if(${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 45 | # exact version found |
| 46 | set(${_NAME_UPPER}_FOUND TRUE) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 47 | else() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 48 | # exect version not found |
| 49 | set(${_NAME_UPPER}_FOUND FALSE) |
| 50 | # check if newer or older |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 51 | if(${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 52 | set(_AGE "new") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 53 | else() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 54 | set(_AGE "old") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 55 | endif() |
| 56 | endif() |
| 57 | else() |
| 58 | if(${_NAME}_FIND_VERSION) |
| 59 | if(${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 60 | set(${_NAME_UPPER}_FOUND FALSE) |
| 61 | set(_AGE "old") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 62 | else() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 63 | set(${_NAME_UPPER}_FOUND TRUE) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 64 | endif() |
| 65 | endif() |
| 66 | endif() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 67 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 68 | if("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG") |
| 69 | if(${_NAME}_FIND_VERSION_EXACT) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 70 | set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, version ${${_NAME}_FIND_VERSION} is required.") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 71 | else() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 72 | set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, at least version ${${_NAME}_FIND_VERSION} is required.") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 73 | endif() |
| 74 | else() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 75 | set(_FAIL_MESSAGE "${_FAIL_MSG}") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 76 | endif() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 77 | |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 78 | if(NOT ${_NAME_UPPER}_FOUND) |
| 79 | if(${_NAME}_FIND_REQUIRED) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 80 | message(FATAL_ERROR "${_FAIL_MESSAGE}") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 81 | else() |
| 82 | if(NOT ${_NAME}_FIND_QUIETLY) |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 83 | message(STATUS "${_FAIL_MESSAGE}") |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 84 | endif() |
| 85 | endif() |
| 86 | endif() |
Radek Krejci | 4f2d40d | 2015-10-08 12:55:01 +0200 | [diff] [blame] | 87 | |
| 88 | set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE) |
Claus Klein | 2209191 | 2020-01-20 13:45:47 +0100 | [diff] [blame] | 89 | endfunction() |