Fypp — Python powered Fortran metaprogramming
.. image:: https://travis-ci.org/aradi/fypp.svg?branch=develop :target: https://travis-ci.org/aradi/fypp
Fypp is a Python powered preprocessor. It can be used for any programming languages but its primary aim is to offer a Fortran preprocessor, which helps to extend Fortran with condititional compiling and template metaprogramming capabilities. Instead of introducing its own expression syntax, it uses Python expressions in its preprocessor directives, offering the consistency and versatility of Python when formulating metaprogramming tasks. It puts strong emphasis on robustness and on neat integration into developing toolchains.
The project is hosted on github <https://github.com/aradi/fypp>
_.
Detailed DOCUMENTATION <http://fypp.readthedocs.org>
is available on
readthedocs.org <http://fypp.readthedocs.org>
.
Fypp is released under the BSD 2-clause license.
Definition, evaluation and removal of variables::
print *, "Some debug information"
print *, "LOGLEVEL: ${LOGLEVEL}$"
Macro definitions and macro calls::
if (.not. ${cond}$) then
print *, "Assert failed in file ${_FILE_}$, line ${_LINE_}$"
error stop
end if
! Invoked via direct call (argument needs no quotation) @:ASSERT(size(myArray) > 0)
! Invoked as Python expression (argument needs quotation) $:ASSERT('size(myArray) > 0')
Conditional output::
program test
use mpi
use openmp
use serial
Iterated output (e.g. for generating Fortran templates)::
interface myfunc
module procedure myfunc_${dtype}$
end interface myfunc
Inline directives::
logical, parameter :: hasMpi = #{if defined('MPI')}# .true. #{else}# .false. #{endif}#
Insertion of arbitrary Python expressions::
character(*), parameter :: comp_date = "${time.strftime('%Y-%m-%d')}$"
Inclusion of files during preprocessing::
Using Fortran-style continutation lines in preprocessor directives::
& or var2 > var4
print *, "Doing something here"
Passing (unquoted) multiline string arguments to callables::
$:code
if (size(array) > 100) then print *, "DEBUG: spuriously large array" end if
$:code
this will be repeated 3 times
Preprocessor comments::
Suppressing the preprocessor output in selected regions::
Explicit request for stopping the preprocessor::
Easy check for macro parameter sanity::
:
Line numbering directives in output::
program test
use mpi
:
transformed to ::
program test
use mpi
:
when variable MPI
is defined and Fypp was instructed to generate line
markers.
Automatic folding of generated lines exceeding line length limit
Fypp needs a working Python 3 interpreter (Python 3.5 or above).
When you install Fypp, you obtain the command line tool fypp
and the Python
module fypp.py
. Latter you can import if you want to access the
functionality of Fypp directly from within your Python scripts.
The last stable release of Fypp can be easily installed as conda package by issuing ::
conda install -c conda-forge fypp
You can also use Pythons command line installer pip
in order to download the
stable release from the Fypp page on PyPI <http://pypi.python.org/pypi/fypp>
_
and install it on your system.
If you want to install Fypp into the module system of the active Python 3 interpreter (typically the case when you are using a Python virtual environment), issue ::
pip3 install fypp
Alternatively, you can install Fypp into the user space (under ~/.local
) with
::
pip3 install --user fypp
On Windows you can use the MSYS2 toolchain <https://www.msys2.org/>
_ to install
Fypp in a MinGW terminal. To install Fypp use::
pacman -S mingw-w64-x86_64-python-fypp
Make sure the selected architecture is matching your current MinGW terminal.
For all supporting MinGW architectures visit check the package index
here <https://packages.msys2.org/base/mingw-w64-python-fypp>
_.
For a manual install, you can download the source code of the stable
releases from the Fypp project website <https://github.com/aradi/fypp/releases>
_.
If you wish to obtain the latest development version, clone the projects repository::
git clone https://github.com/aradi/fypp.git
and check out the master
branch.
The command line tool is a single stand-alone script. You can run it directly from the source folder ::
FYPP_SOURCE_FOLDER/bin/fypp
or after copying it from the bin
folder to any location listed in your PATH
environment variable, by just issuing ::
fypp
The python module fypp.py
can be found in FYP_SOURCE_FOLDER/src
.
The Fypp command line tool reads a file, preprocesses it and writes it to another file, so you would typically invoke it like::
fypp source.fpp source.f90
which would process source.fpp
and write the result to source.f90
. If
input and output files are not specified, information is read from stdin and
written to stdout.
The behavior of Fypp can be influenced with various command line options. A summary of all command line options can be obtained by::
fypp -h