1 # Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
4 #[=======================================================================[.rst:
8 Find the Simplified Wrapper and Interface Generator (SWIG_) executable.
10 This module finds an installed SWIG and determines its version.
12 .. versionadded:: 3.18
13 If a ``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` argument is given to the
14 :command:`find_package` command, it will also determine supported target
17 .. versionadded:: 3.19
18 When a version is requested, it can be specified as a simple value or as a
19 range. For a detailed description of version range usage and capabilities,
20 refer to the :command:`find_package` command.
22 The module defines the following variables:
25 Whether SWIG and any required components were found on the system.
27 Path to the SWIG executable.
29 Path to the installed SWIG ``Lib`` directory (result of ``swig -swiglib``).
31 SWIG executable version (result of ``swig -version``).
33 If ``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` are requested, each available
34 target language ``<lang>`` (lowercase) will be set to TRUE.
36 Any ``COMPONENTS`` given to ``find_package`` should be the names of supported
37 target languages as provided to the LANGUAGE argument of ``swig_add_library``,
38 such as ``python`` or ``perl5``. Language names *must* be lowercase.
40 All information is collected from the ``SWIG_EXECUTABLE``, so the version
41 to be found can be changed from the command line by means of setting
44 Example usage requiring SWIG 4.0 or higher and Python language support, with
45 optional Fortran support:
49 find_package(SWIG 4.0 COMPONENTS python OPTIONAL_COMPONENTS fortran)
51 message("SWIG found: ${SWIG_EXECUTABLE}")
52 if(NOT SWIG_fortran_FOUND)
53 message(WARNING "SWIG Fortran bindings cannot be generated")
57 .. _SWIG: https://swig.org
59 #]=======================================================================]
61 include(FindPackageHandleStandardArgs)
63 function(_swig_get_version _swig_executable _swig_version)
64 unset(${_swig_version} PARENT_SCOPE)
65 # Determine SWIG version
66 execute_process(COMMAND "${_swig_executable}" -version
67 OUTPUT_VARIABLE _swig_output
68 ERROR_VARIABLE _swig_output
69 RESULT_VARIABLE _swig_result)
71 set_property (CACHE _SWIG_REASON_FAILURE PROPERTY VALUE "Cannot use the executable \"${_swig_executable}\"")
73 set_property (CACHE _SWIG_REASON_FAILURE APPEND_STRING PROPERTY VALUE ": ${_swig_output}")
76 string(REGEX REPLACE ".*SWIG Version[^0-9.]*\([0-9.]+\).*" "\\1"
77 _swig_output "${_swig_output}")
78 set(${_swig_version} ${_swig_output} PARENT_SCOPE)
82 function(_swig_validate_find_executable status executable)
83 _swig_get_version("${executable}" _swig_find_version)
84 if(NOT _swig_find_version)
85 # executable is unusable
86 set (${status} FALSE PARENT_SCOPE)
89 if(NOT SWIG_FIND_VERSION)
93 find_package_check_version(${_swig_find_version} _swig_version_is_valid HANDLE_VERSION_RANGE)
94 if(_swig_version_is_valid)
95 unset(_SWIG_REASON_FAILURE CACHE)
97 set (${status} FALSE PARENT_SCOPE)
98 set_property (CACHE _SWIG_REASON_FAILURE PROPERTY VALUE "Could NOT find SWIG: Found unsuitable version \"${_swig_find_version}\" for the executable \"${executable}\"")
102 unset (_SWIG_REASON_FAILURE)
103 set (_SWIG_REASON_FAILURE CACHE INTERNAL "SWIG reason failure")
105 # compute list of possible names
107 if (SWIG_FIND_VERSION_RANGE)
108 foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
109 if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MIN_MAJOR
110 AND ((SWIG_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _SWIG_MAJOR VERSION_LESS_EQUAL SWIG_FIND_VERSION_MAX)
111 OR (SWIG_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _SWIG_MAJOR VERSION_LESS SWIG_FIND_VERSION_MAX)))
112 list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
115 elseif(SWIG_FIND_VERSION)
116 if (SWIG_FIND_VERSION_EXACT)
117 set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
119 foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
120 if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MAJOR)
121 list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
126 set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
129 # try to find any version
130 set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
133 find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig NAMES_PER_DIR
134 VALIDATOR _swig_validate_find_executable)
137 if(SWIG_EXECUTABLE AND NOT SWIG_DIR)
138 # Find default value for SWIG library directory
139 execute_process(COMMAND "${SWIG_EXECUTABLE}" -swiglib
140 OUTPUT_VARIABLE _swig_output
141 ERROR_VARIABLE _swig_error
142 RESULT_VARIABLE _swig_result)
145 set(_msg "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${_swig_error}")
146 if(SWIG_FIND_REQUIRED)
147 message(SEND_ERROR "${_msg}")
149 message(STATUS "${_msg}")
153 string(REGEX REPLACE "[\n\r]+" ";" _SWIG_LIB ${_swig_output})
156 # Find SWIG library directory
157 find_path(SWIG_DIR swig.swg PATHS ${_SWIG_LIB} NO_CMAKE_FIND_ROOT_PATH)
161 if(SWIG_EXECUTABLE AND SWIG_DIR AND NOT SWIG_VERSION)
162 # Determine SWIG version
163 _swig_get_version("${SWIG_EXECUTABLE}" _swig_output)
164 set(SWIG_VERSION ${_swig_output} CACHE STRING "Swig version" FORCE)
167 if(SWIG_EXECUTABLE AND SWIG_FIND_COMPONENTS)
168 execute_process(COMMAND "${SWIG_EXECUTABLE}" -help
169 OUTPUT_VARIABLE _swig_output
170 ERROR_VARIABLE _swig_error
171 RESULT_VARIABLE _swig_result)
173 message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${_swig_error}")
175 string(REPLACE "\n" ";" _swig_output "${_swig_output}")
176 foreach(SWIG_line IN LISTS _swig_output)
177 if(SWIG_line MATCHES "-([A-Za-z0-9_]+) +- *Generate.*wrappers")
178 set(SWIG_${CMAKE_MATCH_1}_FOUND TRUE)
184 find_package_handle_standard_args(
185 SWIG HANDLE_COMPONENTS
186 REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
187 VERSION_VAR SWIG_VERSION
189 FAIL_MESSAGE "${_SWIG_REASON_FAILURE}")
195 unset(_SWIG_REASON_FAILURE CACHE)
198 set(SWIG_USE_FILE "${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake")
201 mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)