FindSWIG.cmake 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. If a
  8. ``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` argument is given to the
  9. :command:`find_package` command, it will also determine supported target
  10. languages.
  11. When a version is requested, it can be specified as a simple value or as a
  12. range. For a detailed description of version range usage and capabilities,
  13. refer to the :command:`find_package` command.
  14. The module defines the following variables:
  15. ``SWIG_FOUND``
  16. Whether SWIG and any required components were found on the system.
  17. ``SWIG_EXECUTABLE``
  18. Path to the SWIG executable.
  19. ``SWIG_DIR``
  20. Path to the installed SWIG ``Lib`` directory (result of ``swig -swiglib``).
  21. ``SWIG_VERSION``
  22. SWIG executable version (result of ``swig -version``).
  23. ``SWIG_<lang>_FOUND``
  24. If ``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` are requested, each available
  25. target language ``<lang>`` (lowercase) will be set to TRUE.
  26. Any ``COMPONENTS`` given to ``find_package`` should be the names of supported
  27. target languages as provided to the LANGUAGE argument of ``swig_add_library``,
  28. such as ``python`` or ``perl5``. Language names *must* be lowercase.
  29. All information is collected from the ``SWIG_EXECUTABLE``, so the version
  30. to be found can be changed from the command line by means of setting
  31. ``SWIG_EXECUTABLE``.
  32. Example usage requiring SWIG 4.0 or higher and Python language support, with
  33. optional Fortran support:
  34. .. code-block:: cmake
  35. find_package(SWIG 4.0 COMPONENTS python OPTIONAL_COMPONENTS fortran)
  36. if(SWIG_FOUND)
  37. message("SWIG found: ${SWIG_EXECUTABLE}")
  38. if(NOT SWIG_fortran_FOUND)
  39. message(WARNING "SWIG Fortran bindings cannot be generated")
  40. endif()
  41. endif()
  42. .. _`SWIG`: http://swig.org
  43. #]=======================================================================]
  44. # compute list of possible names
  45. unset (_SWIG_NAMES)
  46. if (SWIG_FIND_VERSION_RANGE)
  47. foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
  48. if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MIN_MAJOR
  49. AND ((SWIG_FIND_VERSION_RANGE_MAX STREQUAL "INCLUDE" AND _SWIG_MAJOR VERSION_LESS_EQUAL SWIG_FIND_VERSION_MAX)
  50. OR (SWIG_FIND_VERSION_RANGE_MAX STREQUAL "EXCLUDE" AND _SWIG_MAJOR VERSION_LESS SWIG_FIND_VERSION_MAX)))
  51. list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
  52. endif()
  53. endforeach()
  54. elseif(SWIG_FIND_VERSION)
  55. if (SWIG_FIND_VERSION_EXACT)
  56. set(_SWIG_NAMES swig${SWIG_FIND_VERSION_MAJOR}.0)
  57. else()
  58. foreach (_SWIG_MAJOR IN ITEMS 4 3 2)
  59. if (_SWIG_MAJOR VERSION_GREATER_EQUAL SWIG_FIND_VERSION_MAJOR)
  60. list (APPEND _SWIG_NAMES swig${_SWIG_MAJOR}.0)
  61. endif()
  62. endforeach()
  63. endif()
  64. else()
  65. set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
  66. endif()
  67. if (NOT _SWIG_NAMES)
  68. # try to find any version
  69. set (_SWIG_NAMES swig4.0 swig3.0 swig2.0)
  70. endif()
  71. find_program(SWIG_EXECUTABLE NAMES ${_SWIG_NAMES} swig)
  72. if(SWIG_EXECUTABLE)
  73. execute_process(COMMAND ${SWIG_EXECUTABLE} -swiglib
  74. OUTPUT_VARIABLE SWIG_swiglib_output
  75. ERROR_VARIABLE SWIG_swiglib_error
  76. RESULT_VARIABLE SWIG_swiglib_result)
  77. if(SWIG_swiglib_result)
  78. if(SWIG_FIND_REQUIRED)
  79. message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${SWIG_swiglib_error}")
  80. else()
  81. message(STATUS "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${SWIG_swiglib_error}")
  82. endif()
  83. else()
  84. string(REGEX REPLACE "[\n\r]+" ";" SWIG_swiglib_output ${SWIG_swiglib_output})
  85. find_path(SWIG_DIR swig.swg PATHS ${SWIG_swiglib_output} NO_CMAKE_FIND_ROOT_PATH)
  86. if(SWIG_DIR)
  87. set(SWIG_USE_FILE ${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake)
  88. execute_process(COMMAND ${SWIG_EXECUTABLE} -version
  89. OUTPUT_VARIABLE SWIG_version_output
  90. ERROR_VARIABLE SWIG_version_output
  91. RESULT_VARIABLE SWIG_version_result)
  92. if(SWIG_version_result)
  93. message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -version\" failed with output:\n${SWIG_version_output}")
  94. else()
  95. string(REGEX REPLACE ".*SWIG Version[^0-9.]*\([0-9.]+\).*" "\\1"
  96. SWIG_version_output "${SWIG_version_output}")
  97. set(SWIG_VERSION ${SWIG_version_output} CACHE STRING "Swig version" FORCE)
  98. endif()
  99. endif()
  100. endif()
  101. if(SWIG_FIND_COMPONENTS)
  102. execute_process(COMMAND ${SWIG_EXECUTABLE} -help
  103. OUTPUT_VARIABLE SWIG_swighelp_output
  104. ERROR_VARIABLE SWIG_swighelp_error
  105. RESULT_VARIABLE SWIG_swighelp_result)
  106. if(SWIG_swighelp_result)
  107. message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${SWIG_swiglib_error}")
  108. else()
  109. string(REPLACE "\n" ";" SWIG_swighelp_output "${SWIG_swighelp_output}")
  110. foreach(SWIG_line IN LISTS SWIG_swighelp_output)
  111. if(SWIG_line MATCHES "-([A-Za-z0-9_]+) +- *Generate.*wrappers")
  112. set(SWIG_${CMAKE_MATCH_1}_FOUND TRUE)
  113. endif()
  114. endforeach()
  115. endif()
  116. endif()
  117. endif()
  118. include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
  119. find_package_handle_standard_args(
  120. SWIG HANDLE_COMPONENTS
  121. REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
  122. VERSION_VAR SWIG_VERSION
  123. HANDLE_VERSION_RANGE)
  124. mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)