FindSWIG — CMake 4.3.0 Documentation

# FindSWIG

Finds the installed Simplified Wrapper and Interface Generator (SWIG) executable and determines its version:

    find_package(SWIG [<version>] [COMPONENTS <langs>...] [...])

Added in version 3.19: Support for specifying version range when calling the `find_package()` command. When a version is requested, it can be specified as a single value as before, and now also a version range can be used. For a detailed description of version range usage and capabilities, refer to the `find_package()` command.

## Components

Added in version 3.18.

This module supports optional components to specify target languages.

If a `COMPONENTS` or `OPTIONAL_COMPONENTS` argument is given to the `find_package()` command, it will also determine supported target languages.

    find_package(SWIG [COMPONENTS <langs>...] [OPTIONAL_COMPONENTS <langs>...])

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.

## Result Variables

This module defines the following variables:

`SWIG_FOUND`

  Boolean indicating whether (the requested version of) SWIG and any required components were found on the system.

`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.

`SWIG_DIR`

  Path to the installed SWIG `Lib` directory (result of `swig -swiglib`).

## Cache Variables

The following cache variables may also be set:

`SWIG_EXECUTABLE`

  The path to the SWIG executable.

  This executable is used to retrieve all information for this module. It can be also manually set to change the version to be found from the command line.

## Examples

Example usage requiring SWIG 4.0 or higher and Python language support, with optional Fortran support:

    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()

This module is commonly used in conjunction with the `UseSWIG` module:

    find_package(SWIG COMPONENTS python)
    if(SWIG_FOUND)
      include(UseSWIG)

      swig_add_library(mymod LANGUAGE python SOURCES mymod.i)
    endif()

## See Also

- The `UseSWIG` module to use SWIG in CMake.