FindSWIG.cmake 2.58 KB
Newer Older
1 2 3
# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.

4 5 6 7
#[=======================================================================[.rst:
FindSWIG
--------

8
Find Simplified Wrapper and Interface Generator (SWIG)
9 10 11 12 13

This module finds an installed SWIG.  It sets the following variables:

::

14
  SWIG_FOUND - set to "True" if SWIG is found
15 16 17 18 19 20 21
  SWIG_DIR - the directory where swig is installed
  SWIG_EXECUTABLE - the path to the swig executable
  SWIG_VERSION   - the version number of the swig executable



The minimum required version of SWIG can be specified using the
22
standard syntax, e.g.   :command:`find_package(SWIG 1.1)`
23

24
All information is collected from the ``SWIG_EXECUTABLE``, so the version
25
to be found can be changed from the command line by means of setting
26
``SWIG_EXECUTABLE``
27
#]=======================================================================]
28

29
find_program(SWIG_EXECUTABLE NAMES swig4.0 swig3.0 swig2.0 swig)
30

31 32
if(SWIG_EXECUTABLE)
  execute_process(COMMAND ${SWIG_EXECUTABLE} -swiglib
33 34 35 36
    OUTPUT_VARIABLE SWIG_swiglib_output
    ERROR_VARIABLE SWIG_swiglib_error
    RESULT_VARIABLE SWIG_swiglib_result)

37 38 39
  if(SWIG_swiglib_result)
    if(SWIG_FIND_REQUIRED)
      message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${SWIG_swiglib_error}")
40
    else()
41
      message(STATUS "Command \"${SWIG_EXECUTABLE} -swiglib\" failed with output:\n${SWIG_swiglib_error}")
42 43
    endif()
  else()
44
    string(REGEX REPLACE "[\n\r]+" ";" SWIG_swiglib_output ${SWIG_swiglib_output})
45
    find_path(SWIG_DIR swig.swg PATHS ${SWIG_swiglib_output} NO_CMAKE_FIND_ROOT_PATH)
46
    if(SWIG_DIR)
47
      set(SWIG_USE_FILE ${CMAKE_CURRENT_LIST_DIR}/UseSWIG.cmake)
48
      execute_process(COMMAND ${SWIG_EXECUTABLE} -version
49 50 51
        OUTPUT_VARIABLE SWIG_version_output
        ERROR_VARIABLE SWIG_version_output
        RESULT_VARIABLE SWIG_version_result)
52 53
      if(SWIG_version_result)
        message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -version\" failed with output:\n${SWIG_version_output}")
54
      else()
55
        string(REGEX REPLACE ".*SWIG Version[^0-9.]*\([0-9.]+\).*" "\\1"
56
          SWIG_version_output "${SWIG_version_output}")
57
        set(SWIG_VERSION ${SWIG_version_output} CACHE STRING "Swig version" FORCE)
58 59 60 61
      endif()
    endif()
  endif()
endif()
62

63
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
64 65
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SWIG  REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
                                        VERSION_VAR SWIG_VERSION )
66

67
mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)