1
0
mirror of https://github.com/Kitware/CMake.git synced 2025-10-14 02:08:27 +08:00

FindSWIG: Add COMPONENTS support for SWIG target languages

Newer versions of SWIG drop support for some target languages, and some
forks of SWIG (such as for Fortran and MATLAB) aren't supported by the
mainline version of SWIG.

Swig versions as old as 1.3.6 (circa 2001) and possibly older use the
same format for listing available wrappers "%-15s - Generate %s
wrappers", so component detection should be quite reliable.
This commit is contained in:
Seth R Johnson
2020-02-20 22:05:55 -05:00
parent 25ca8e5ce5
commit e131d9f974
10 changed files with 91 additions and 15 deletions

View File

@@ -0,0 +1,5 @@
findswig-components
-------------------
* The :module:`FindSWIG` module now accepts target languages as ``COMPONENTS``
and ``OPTIONAL_COMPONENTS`` arguments to ``find_package``.

View File

@@ -5,25 +5,49 @@
FindSWIG
--------
Find Simplified Wrapper and Interface Generator (SWIG)
This module finds an installed SWIG. It sets the following variables:
::
SWIG_FOUND - set to "True" if SWIG is found
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
Find the Simplified Wrapper and Interface Generator (SWIG_) executable.
This module finds an installed SWIG and determines its version. If a
``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` argument is given to ``find_package``,
it will also determine supported target languages. The module sents the
following variables:
The minimum required version of SWIG can be specified using the
standard syntax, e.g. :command:`find_package(SWIG 1.1)`
``SWIG_FOUND``
Whether SWIG and any required components were found on the system.
``SWIG_EXECUTABLE``
Path to the SWIG executable.
``SWIG_DIR``
Path to the installed SWIG ``Lib`` directory (result of ``swig -swiglib``).
``SWIG_VERSION``
SWIG executable version (result of ``swig -version``).
``SWIG_<lang>_FOUND``
If ``COMPONENTS`` or ``OPTIONAL_COMPONENTS`` are requested, each available
target language ``<lang>`` (lowercase) will be set to TRUE.
Any ``COMPONENTS`` given to ``find_package`` should be the names of supported
target languages as provided to the LANGUAGE argument of ``swig_add_library``,
such as ``python`` or ``perl5``. Language names *must* be lowercase.
All information is collected from the ``SWIG_EXECUTABLE``, so the version
to be found can be changed from the command line by means of setting
``SWIG_EXECUTABLE``
``SWIG_EXECUTABLE``.
Example usage requiring SWIG 4.0 or higher and Python language support, with
optional Fortran support:
.. code-block:: cmake
find_package(SWIG 4.0 COMPONENTS python OPTIONAL_COMPONENTS fortran)
if(SWIG_FOUND)
message("SWIG found: ${SWIG_EXECUTABLE}")
if(NOT SWIG_fortran_FOUND)
message(WARNING "SWIG Fortran bindings cannot be generated")
endif()
endif()
.. _`SWIG`: http://swig.org
#]=======================================================================]
find_program(SWIG_EXECUTABLE NAMES swig4.0 swig3.0 swig2.0 swig)
@@ -58,10 +82,29 @@ if(SWIG_EXECUTABLE)
endif()
endif()
endif()
if(SWIG_FIND_COMPONENTS)
execute_process(COMMAND ${SWIG_EXECUTABLE} -help
OUTPUT_VARIABLE SWIG_swighelp_output
ERROR_VARIABLE SWIG_swighelp_error
RESULT_VARIABLE SWIG_swighelp_result)
if(SWIG_swighelp_result)
message(SEND_ERROR "Command \"${SWIG_EXECUTABLE} -help\" failed with output:\n${SWIG_swiglib_error}")
else()
string(REPLACE "\n" ";" SWIG_swighelp_output "${SWIG_swighelp_output}")
foreach(SWIG_line IN LISTS SWIG_swighelp_output)
if(SWIG_line MATCHES "-([A-Za-z0-9_]+) +- *Generate.*wrappers")
set(SWIG_${CMAKE_MATCH_1}_FOUND TRUE)
endif()
endforeach()
endif()
endif()
endif()
include(${CMAKE_CURRENT_LIST_DIR}/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SWIG REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
VERSION_VAR SWIG_VERSION )
find_package_handle_standard_args(
SWIG HANDLE_COMPONENTS
REQUIRED_VARS SWIG_EXECUTABLE SWIG_DIR
VERSION_VAR SWIG_VERSION)
mark_as_advanced(SWIG_DIR SWIG_VERSION SWIG_EXECUTABLE)

View File

@@ -188,6 +188,7 @@ add_RunCMake_test(FindBoost)
add_RunCMake_test(FindLua)
add_RunCMake_test(FindOpenGL)
if(CMake_TEST_UseSWIG)
add_RunCMake_test(FindSWIG)
add_RunCMake_test(UseSWIG -DCMake_TEST_FindPython=${CMake_TEST_FindPython})
endif()
if(NOT CMAKE_C_COMPILER_ID MATCHES "Watcom")

View File

@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.10)
project(${RunCMake_TEST} C)
include(${RunCMake_TEST}.cmake)

View File

@@ -0,0 +1,4 @@
include(RunCMake)
run_cmake(components)
run_cmake(missing-components)

View File

@@ -0,0 +1,6 @@
-- Found SWIG: [^ ]+ \(found version "[0-9.]+"\) found components: java python missing components: PERL
-- SWIG_VERSION='[0-9.]+'
-- SWIG_java_FOUND=TRUE
-- SWIG_python_FOUND=TRUE
-- SWIG_PERL_FOUND=
-- Configuring done

View File

@@ -0,0 +1,9 @@
# Note that 'perl' will not be found because it is not lowercase.
find_package(SWIG REQUIRED
COMPONENTS java
OPTIONAL_COMPONENTS python PERL)
message(STATUS "SWIG_VERSION='${SWIG_VERSION}'")
foreach(_lang java python PERL)
message(STATUS "SWIG_${_lang}_FOUND=${SWIG_${_lang}_FOUND}")
endforeach()

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1 @@
Could NOT find SWIG \(missing: invalid\)

View File

@@ -0,0 +1,3 @@
find_package(SWIG REQUIRED
COMPONENTS invalid
OPTIONAL_COMPONENTS alsoinvalid)