FindSWIG.cmake 6.7 KB

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