Merge topic 'cuda_add_12.8_new_sm_support'
[kiteware-cmake.git] / Modules / FindSWIG.cmake
blob94c75cc6bc5d66ae6addc68eeb08ebb428a3f253
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:
5 FindSWIG
6 --------
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
15   languages.
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:
24 ``SWIG_FOUND``
25   Whether SWIG and any required components were found on the system.
26 ``SWIG_EXECUTABLE``
27   Path to the SWIG executable.
28 ``SWIG_DIR``
29   Path to the installed SWIG ``Lib`` directory (result of ``swig -swiglib``).
30 ``SWIG_VERSION``
31   SWIG executable version (result of ``swig -version``).
32 ``SWIG_<lang>_FOUND``
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
42 ``SWIG_EXECUTABLE``.
44 Example usage requiring SWIG 4.0 or higher and Python language support, with
45 optional Fortran support:
47 .. code-block:: cmake
49    find_package(SWIG 4.0 COMPONENTS python OPTIONAL_COMPONENTS fortran)
50    if(SWIG_FOUND)
51      message("SWIG found: ${SWIG_EXECUTABLE}")
52      if(NOT SWIG_fortran_FOUND)
53        message(WARNING "SWIG Fortran bindings cannot be generated")
54      endif()
55    endif()
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)
70   if(_swig_result)
71     set_property (CACHE _SWIG_REASON_FAILURE PROPERTY VALUE "Cannot use the executable \"${_swig_executable}\"")
72     if (_swig_output)
73       set_property (CACHE _SWIG_REASON_FAILURE APPEND_STRING PROPERTY VALUE ": ${_swig_output}")
74     endif()
75   else()
76     string(REGEX REPLACE ".*SWIG Version[^0-9.]*\([0-9.]+\).*" "\\1"
77                          _swig_output "${_swig_output}")
78     set(${_swig_version} ${_swig_output} PARENT_SCOPE)
79   endif()
80 endfunction()
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)
87     return()
88   endif()
89   if(NOT SWIG_FIND_VERSION)
90     return()
91   endif()
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)
96   else()
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}\"")
99   endif()
100 endfunction()
102 unset (_SWIG_REASON_FAILURE)
103 set (_SWIG_REASON_FAILURE CACHE INTERNAL "SWIG reason failure")
105 # compute list of possible names
106 unset (_SWIG_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)
113     endif()
114   endforeach()
115 elseif(SWIG_FIND_VERSION)
116   if (SWIG_FIND_VERSION_EXACT)
117     set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
118   else()
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)
122       endif()
123     endforeach()
124   endif()
125 else()
126   set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
127 endif()
128 if (NOT _SWIG_NAMES)
129   # try to find any version
130   set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
131 endif()
133 find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig NAMES_PER_DIR
134                              VALIDATOR _swig_validate_find_executable)
135 unset(_SWIG_NAMES)
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)
144   if(_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}")
148     else()
149       message(STATUS "${_msg}")
150     endif()
151     unset(_msg)
152   else()
153     string(REGEX REPLACE "[\n\r]+" ";" _SWIG_LIB ${_swig_output})
154   endif()
156   # Find SWIG library directory
157   find_path(SWIG_DIR swig.swg PATHS ${_SWIG_LIB} NO_CMAKE_FIND_ROOT_PATH)
158   unset(_SWIG_LIB)
159 endif()
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)
165 endif()
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)
172   if(_swig_result)
173     message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${_swig_error}")
174   else()
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)
179       endif()
180     endforeach()
181   endif()
182 endif()
184 find_package_handle_standard_args(
185   SWIG HANDLE_COMPONENTS
186   REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
187   VERSION_VAR SWIG_VERSION
188   HANDLE_VERSION_RANGE
189   FAIL_MESSAGE "${_SWIG_REASON_FAILURE}")
191 unset(_swig_output)
192 unset(_swig_error)
193 unset(_swig_result)
195 unset(_SWIG_REASON_FAILURE CACHE)
197 if(SWIG_FOUND)
198   set(SWIG_USE_FILE "${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake")
199 endif()
201 mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)