# F2PY

The purpose of the F2PY –Fortran to Python interface generator– project is to provide a connection between Python and Fortran languages.

F2PY is a Python package (with a command line tool f2py and a module f2py2e) that facilitates creating/building Python C/API extension modules that make it possible to call Fortran 77/90/95 external subroutines and Fortran 90/95 module subroutines as well as C functions; to access Fortran 77 COMMON blocks and Fortran 90/95 module data, including allocatable arrays from Python.

For more information on the F2PY project, see http://www.f2py.com/.

The following variables are defined:

    F2PY_EXECUTABLE      - absolute path to the F2PY executable

    F2PY_VERSION_STRING  - the version of F2PY found
    F2PY_VERSION_MAJOR   - the F2PY major version
    F2PY_VERSION_MINOR   - the F2PY minor version
    F2PY_VERSION_PATCH   - the F2PY patch version

Note

By default, the module finds the F2PY program associated with the installed NumPy package.

## Example usage

Assuming that a package named `method` is declared in `setup.py` and that the corresponding directory containing `__init__.py` also exists, the following CMake code can be added to `method/CMakeLists.txt` to ensure the C sources associated with `cylinder_methods.f90` are generated and the corresponding module is compiled:

    find_package(F2PY REQUIRED)

    set(f2py_module_name "_cylinder_methods")
    set(fortran_src_file "${CMAKE_CURRENT_SOURCE_DIR}/cylinder_methods.f90")

    set(generated_module_file ${CMAKE_CURRENT_BINARY_DIR}/${f2py_module_name}${PYTHON_EXTENSION_MODULE_SUFFIX})

    add_custom_target(${f2py_module_name} ALL
      DEPENDS ${generated_module_file}
      )

    add_custom_command(
      OUTPUT ${generated_module_file}
      COMMAND ${F2PY_EXECUTABLE}
        -m ${f2py_module_name}
        -c
        ${fortran_src_file}
      WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
      )

    install(FILES ${generated_module_file} DESTINATION methods)

Warning

Using `f2py` with `-c` argument means that f2py is also responsible to build the module. In that case, CMake is not used to find the compiler and configure the associated build system.

The following functions are defined:

add_f2py_target

Create a custom rule to generate the source code for a Python extension module using f2py.

> add_f2py_target(<Name> [<F2PYInput>]

>   [OUTPUT_VAR <OutputVar>])

`<Name>` is the name of the new target, and `<F2PYInput>` is the path to a pyf source file. Note that, despite the name, no new targets are created by this function. Instead, see `OUTPUT_VAR` for retrieving the path to the generated source for subsequent targets.

If only `<Name>` is provided, and it ends in the “.pyf” extension, then it is assumed to be the `<F2PYInput>`. The name of the input without the extension is used as the target name. If only `<Name>` is provided, and it does not end in the “.pyf” extension, then the `<F2PYInput>` is assumed to be `<Name>.pyf`.

Options:

`OUTPUT_VAR <OutputVar>`

  Set the variable `<OutputVar>` in the parent scope to the path to the generated source file. By default, `<Name>` is used as the output variable name.

`DEPENDS [source [source2...]]`

  Sources that must be generated before the F2PY command is run.

Defined variables:

`<OutputVar>`

  The path of the generated source file.

## Example usage

    find_package(F2PY)

    # Note: In this case, either one of these arguments may be omitted; their
    # value would have been inferred from that of the other.
    add_f2py_target(f2py_code f2py_code.pyf)

    add_library(f2py_code MODULE ${f2py_code})
    target_link_libraries(f2py_code ...)