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 # compute list of possible names
63 if (SWIG_FIND_VERSION_RANGE)
64 foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
65 if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MIN_MAJOR
66 AND ((SWIG_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _SWIG_MAJOR VERSION_LESS_EQUAL SWIG_FIND_VERSION_MAX)
67 OR (SWIG_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _SWIG_MAJOR VERSION_LESS SWIG_FIND_VERSION_MAX)))
68 list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
71 elseif(SWIG_FIND_VERSION)
72 if (SWIG_FIND_VERSION_EXACT)
73 set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
75 foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
76 if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MAJOR)
77 list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
82 set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
85 # try to find any version
86 set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
89 find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig)
92 if(SWIG_EXECUTABLE AND NOT SWIG_DIR)
93 # Find default value for SWIG library directory
94 execute_process(COMMAND "${SWIG_EXECUTABLE}" -swiglib
95 OUTPUT_VARIABLE _swig_output
96 ERROR_VARIABLE _swig_error
97 RESULT_VARIABLE _swig_result)
100 set(_msg "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${_swig_error}")
101 if(SWIG_FIND_REQUIRED)
102 message(SEND_ERROR "${_msg}")
104 message(STATUS "${_msg}")
108 string(REGEX REPLACE "[\n\r]+" ";" _SWIG_LIB ${_swig_output})
111 # Find SWIG library directory
112 find_path(SWIG_DIR swig.swg PATHS ${_SWIG_LIB} NO_CMAKE_FIND_ROOT_PATH)
116 if(SWIG_EXECUTABLE AND SWIG_DIR AND NOT SWIG_VERSION)
117 # Determine SWIG version
118 execute_process(COMMAND "${SWIG_EXECUTABLE}" -version
119 OUTPUT_VARIABLE _swig_output
120 ERROR_VARIABLE _swig_output
121 RESULT_VARIABLE _swig_result)
123 message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -version\" failed with output:\n${_swig_output}")
125 string(REGEX REPLACE ".*SWIG Version[^0-9.]*\([0-9.]+\).*" "\\1"
126 _swig_output "${_swig_output}")
127 set(SWIG_VERSION ${_swig_output} CACHE STRING "Swig version" FORCE)
131 if(SWIG_EXECUTABLE AND SWIG_FIND_COMPONENTS)
132 execute_process(COMMAND "${SWIG_EXECUTABLE}" -help
133 OUTPUT_VARIABLE _swig_output
134 ERROR_VARIABLE _swig_error
135 RESULT_VARIABLE _swig_result)
137 message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${_swig_error}")
139 string(REPLACE "\n" ";" _swig_output "${_swig_output}")
140 foreach(SWIG_line IN LISTS _swig_output)
141 if(SWIG_line MATCHES "-([A-Za-z0-9_]+) +- *Generate.*wrappers")
142 set(SWIG_${CMAKE_MATCH_1}_FOUND TRUE)
152 include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
153 find_package_handle_standard_args(
154 SWIG HANDLE_COMPONENTS
155 REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
156 VERSION_VAR SWIG_VERSION
157 HANDLE_VERSION_RANGE)
160 set(SWIG_USE_FILE "${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake")
163 mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)