Imported Upstream version 3.25.0
[platform/upstream/cmake.git] / Modules / FindSWIG.cmake
1 # Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2 # file Copyright.txt or https://cmake.org/licensing for details.
3
4 #[=======================================================================[.rst:
5 FindSWIG
6 --------
7
8 Find the Simplified Wrapper and Interface Generator (SWIG_) executable.
9
10 This module finds an installed SWIG and determines its version.
11
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.
16
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.
21
22 The module defines the following variables:
23
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.
35
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.
39
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``.
43
44 Example usage requiring SWIG 4.0 or higher and Python language support, with
45 optional Fortran support:
46
47 .. code-block:: cmake
48
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()
56
57 .. _SWIG: https://swig.org
58
59 #]=======================================================================]
60
61 # compute list of possible names
62 unset (_SWIG_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)
69     endif()
70   endforeach()
71 elseif(SWIG_FIND_VERSION)
72   if (SWIG_FIND_VERSION_EXACT)
73     set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
74   else()
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)
78       endif()
79     endforeach()
80   endif()
81 else()
82   set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
83 endif()
84 if (NOT _SWIG_NAMES)
85   # try to find any version
86   set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
87 endif()
88
89 find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig)
90 unset(_SWIG_NAMES)
91
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)
98
99   if(_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}")
103     else()
104       message(STATUS "${_msg}")
105     endif()
106     unset(_msg)
107   else()
108     string(REGEX REPLACE "[\n\r]+" ";" _SWIG_LIB ${_swig_output})
109   endif()
110
111   # Find SWIG library directory
112   find_path(SWIG_DIR swig.swg PATHS ${_SWIG_LIB} NO_CMAKE_FIND_ROOT_PATH)
113   unset(_SWIG_LIB)
114 endif()
115
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)
122   if(_swig_result)
123     message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -version\" failed with output:\n${_swig_output}")
124   else()
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)
128   endif()
129 endif()
130
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)
136   if(_swig_result)
137     message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${_swig_error}")
138   else()
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)
143       endif()
144     endforeach()
145   endif()
146 endif()
147
148 unset(_swig_output)
149 unset(_swig_error)
150 unset(_swig_result)
151
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)
158
159 if(SWIG_FOUND)
160   set(SWIG_USE_FILE "${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake")
161 endif()
162
163 mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)