summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Help/release/dev/FindSWIG-version-range.rst4
-rw-r--r--Modules/FindSWIG.cmake41
-rw-r--r--Tests/RunCMake/FindSWIG/RunCMakeTest.cmake1
-rw-r--r--Tests/RunCMake/FindSWIG/version-range.cmake30
4 files changed, 70 insertions, 6 deletions
diff --git a/Help/release/dev/FindSWIG-version-range.rst b/Help/release/dev/FindSWIG-version-range.rst
new file mode 100644
index 0000000..37567d3
--- /dev/null
+++ b/Help/release/dev/FindSWIG-version-range.rst
@@ -0,0 +1,4 @@
+FindSWIG-version-range
+----------------------
+
+* :module:`FindSWIG` module gains the capability to manage a version range.
diff --git a/Modules/FindSWIG.cmake b/Modules/FindSWIG.cmake
index 2fded49..f732289 100644
--- a/Modules/FindSWIG.cmake
+++ b/Modules/FindSWIG.cmake
@@ -7,11 +7,16 @@ FindSWIG
Find the Simplified Wrapper and Interface Generator (SWIG_) executable.
-
This module finds an installed SWIG and determines its version. If a
-``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` argument is given to ``find_package``,
-it will also determine supported target languages. The module sents the
-following variables:
+``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` argument is given to the
+:command:`find_package` command, it will also determine supported target
+languages.
+
+When a version is requested, it can be specified as a simple value or as a
+range. For a detailed description of version range usage and capabilities,
+refer to the :command:`find_package` command.
+
+The module defines the following variables:
``SWIG_FOUND``
Whether SWIG and any required components were found on the system.
@@ -50,7 +55,30 @@ optional Fortran support:
#]=======================================================================]
-find_program(SWIG_EXECUTABLE NAMES swig4.0 swig3.0 swig2.0 swig)
+# compute list of possible names
+if (SWIG_FIND_VERSION_RANGE)
+ set (_SWIG_NAMES)
+ foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
+ if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MIN_MAJOR
+ AND ((SWIG_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _SWIG_MAJOR VERSION_LESS_EQUAL SWIG_FIND_VERSION_MAX)
+ OR (SWIG_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _SWIG_MAJOR VERSION_LESS SWIG_FIND_VERSION_MAX)))
+ list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
+ endif()
+ endforeach()
+elseif(SWIG_FIND_VERSION)
+ if (SWIG_FIND_VERSION_EXACT)
+ set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
+ else()
+ foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
+ if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MAJOR)
+ list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
+ endif()
+ endif()
+else()
+ set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
+endif()
+
+find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig)
if(SWIG_EXECUTABLE)
execute_process(COMMAND ${SWIG_EXECUTABLE} -swiglib
@@ -105,6 +133,7 @@ include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
find_package_handle_standard_args(
SWIG HANDLE_COMPONENTS
REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
- VERSION_VAR SWIG_VERSION)
+ VERSION_VAR SWIG_VERSION
+ HANDLE_VERSION_RANGE)
mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)
diff --git a/Tests/RunCMake/FindSWIG/RunCMakeTest.cmake b/Tests/RunCMake/FindSWIG/RunCMakeTest.cmake
index 5f5f7f5..6becc3c 100644
--- a/Tests/RunCMake/FindSWIG/RunCMakeTest.cmake
+++ b/Tests/RunCMake/FindSWIG/RunCMakeTest.cmake
@@ -2,3 +2,4 @@ include(RunCMake)
run_cmake(components)
run_cmake(missing-components)
+run_cmake(version-range)
diff --git a/Tests/RunCMake/FindSWIG/version-range.cmake b/Tests/RunCMake/FindSWIG/version-range.cmake
new file mode 100644
index 0000000..7ba1134
--- /dev/null
+++ b/Tests/RunCMake/FindSWIG/version-range.cmake
@@ -0,0 +1,30 @@
+cmake_minimum_required (VERSION 3.18...3.19)
+
+find_package (SWIG)
+if (NOT SWIG_FOUND)
+ message (FATAL_ERROR "Failed to find SWIG")
+endif()
+
+# clean-up SWIG variables
+unset (SWIG_EXECUTABLE CACHE)
+unset (SWIG_DIR CACHE)
+
+## Specify a range including current SWIG version
+string (REGEX MATCH "^([0-9]+)" upper_version "${SWIG_VERSION}")
+math (EXPR upper_version "${upper_version} + 1")
+
+find_package (SWIG 1.0...${upper_version}.0)
+if (NOT SWIG_FOUND)
+ message (FATAL_ERROR "Failed to find SWIG with version range 1.0...${upper_version}.0")
+endif()
+
+# clean-up SWIG variables
+unset (SWIG_EXECUTABLE CACHE)
+unset (SWIG_DIR CACHE)
+
+## Specify a range excluding current SWIG version
+set (range 1.0...<${SWIG_VERSION})
+find_package (SWIG ${range})
+if (SWIG_FOUND)
+ message (FATAL_ERROR "Unexpectedly find SWIG with version range ${range}")
+endif()