blob: 48663cdb49af9b34fc3584e48e0e2d5bf0baa23a [file] [log] [blame]
Radek Krejci4f2d40d2015-10-08 12:55:01 +02001# 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 Klein22091912020-01-20 13:45:47 +010024# 3. The name of the author may not be used to endorse or promote products
Radek Krejci4f2d40d2015-10-08 12:55:01 +020025# 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
39function(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 Klein22091912020-01-20 13:45:47 +010044 if(${_NAME}_FIND_VERSION VERSION_EQUAL ${_NAME}_VERSION)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020045 # exact version found
46 set(${_NAME_UPPER}_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010047 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020048 # exect version not found
49 set(${_NAME_UPPER}_FOUND FALSE)
50 # check if newer or older
Claus Klein22091912020-01-20 13:45:47 +010051 if(${_NAME}_FIND_VERSION VERSION_LESS ${_NAME}_VERSION)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020052 set(_AGE "new")
Claus Klein22091912020-01-20 13:45:47 +010053 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020054 set(_AGE "old")
Claus Klein22091912020-01-20 13:45:47 +010055 endif()
56 endif()
57 else()
58 if(${_NAME}_FIND_VERSION)
59 if(${_NAME}_VERSION VERSION_LESS ${_NAME}_FIND_VERSION)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020060 set(${_NAME_UPPER}_FOUND FALSE)
61 set(_AGE "old")
Claus Klein22091912020-01-20 13:45:47 +010062 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020063 set(${_NAME_UPPER}_FOUND TRUE)
Claus Klein22091912020-01-20 13:45:47 +010064 endif()
65 endif()
66 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020067
Claus Klein22091912020-01-20 13:45:47 +010068 if("${_FAIL_MSG}" STREQUAL "DEFAULT_MSG")
69 if(${_NAME}_FIND_VERSION_EXACT)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020070 set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, version ${${_NAME}_FIND_VERSION} is required.")
Claus Klein22091912020-01-20 13:45:47 +010071 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020072 set(_FAIL_MESSAGE "The installed ${_NAME} version ${${_NAME}_VERSION} is too ${_AGE}, at least version ${${_NAME}_FIND_VERSION} is required.")
Claus Klein22091912020-01-20 13:45:47 +010073 endif()
74 else()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020075 set(_FAIL_MESSAGE "${_FAIL_MSG}")
Claus Klein22091912020-01-20 13:45:47 +010076 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020077
Claus Klein22091912020-01-20 13:45:47 +010078 if(NOT ${_NAME_UPPER}_FOUND)
79 if(${_NAME}_FIND_REQUIRED)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020080 message(FATAL_ERROR "${_FAIL_MESSAGE}")
Claus Klein22091912020-01-20 13:45:47 +010081 else()
82 if(NOT ${_NAME}_FIND_QUIETLY)
Radek Krejci4f2d40d2015-10-08 12:55:01 +020083 message(STATUS "${_FAIL_MESSAGE}")
Claus Klein22091912020-01-20 13:45:47 +010084 endif()
85 endif()
86 endif()
Radek Krejci4f2d40d2015-10-08 12:55:01 +020087
88 set(${_NAME_UPPER}_FOUND ${${_NAME_UPPER}_FOUND} PARENT_SCOPE)
Claus Klein22091912020-01-20 13:45:47 +010089endfunction()