| 1 |
********************************************* |
| 2 |
Fypp — Python powered Fortran metaprogramming |
| 3 |
********************************************* |
| 4 |
|
| 5 |
.. image:: https://travis-ci.org/aradi/fypp.svg?branch=develop |
| 6 |
:target: https://travis-ci.org/aradi/fypp |
| 7 |
|
| 8 |
Fypp is a Python powered preprocessor. It can be used for any programming |
| 9 |
languages but its primary aim is to offer a Fortran preprocessor, which helps to |
| 10 |
extend Fortran with condititional compiling and template metaprogramming |
| 11 |
capabilities. Instead of introducing its own expression syntax, it uses Python |
| 12 |
expressions in its preprocessor directives, offering the consistency and |
| 13 |
versatility of Python when formulating metaprogramming tasks. It puts strong |
| 14 |
emphasis on robustness and on neat integration into developing toolchains. |
| 15 |
|
| 16 |
The project is `hosted on github <https://github.com/aradi/fypp>`_. |
| 17 |
|
| 18 |
`Detailed DOCUMENTATION <http://fypp.readthedocs.org>`_ is available on |
| 19 |
`readthedocs.org <http://fypp.readthedocs.org>`_. |
| 20 |
|
| 21 |
Fypp is released under the *BSD 2-clause license*. |
| 22 |
|
| 23 |
|
| 24 |
Main features |
| 25 |
============= |
| 26 |
|
| 27 |
* Definition, evaluation and removal of variables:: |
| 28 |
|
| 29 |
#:if DEBUG > 0 |
| 30 |
print *, "Some debug information" |
| 31 |
#:endif |
| 32 |
|
| 33 |
#:set LOGLEVEL = 2 |
| 34 |
print *, "LOGLEVEL: ${LOGLEVEL}$" |
| 35 |
|
| 36 |
#:del LOGLEVEL |
| 37 |
|
| 38 |
* Macro definitions and macro calls:: |
| 39 |
|
| 40 |
#:def ASSERT(cond) |
| 41 |
#:if DEBUG > 0 |
| 42 |
if (.not. ${cond}$) then |
| 43 |
print *, "Assert failed in file ${_FILE_}$, line ${_LINE_}$" |
| 44 |
error stop |
| 45 |
end if |
| 46 |
#:endif |
| 47 |
#:enddef ASSERT |
| 48 |
|
| 49 |
! Invoked via direct call (argument needs no quotation) |
| 50 |
@:ASSERT(size(myArray) > 0) |
| 51 |
|
| 52 |
! Invoked as Python expression (argument needs quotation) |
| 53 |
$:ASSERT('size(myArray) > 0') |
| 54 |
|
| 55 |
* Conditional output:: |
| 56 |
|
| 57 |
program test |
| 58 |
#:if defined('WITH_MPI') |
| 59 |
use mpi |
| 60 |
#:elif defined('WITH_OPENMP') |
| 61 |
use openmp |
| 62 |
#:else |
| 63 |
use serial |
| 64 |
#:endif |
| 65 |
|
| 66 |
* Iterated output (e.g. for generating Fortran templates):: |
| 67 |
|
| 68 |
interface myfunc |
| 69 |
#:for dtype in ['real', 'dreal', 'complex', 'dcomplex'] |
| 70 |
module procedure myfunc_${dtype}$ |
| 71 |
#:endfor |
| 72 |
end interface myfunc |
| 73 |
|
| 74 |
* Inline directives:: |
| 75 |
|
| 76 |
logical, parameter :: hasMpi = #{if defined('MPI')}# .true. #{else}# .false. #{endif}# |
| 77 |
|
| 78 |
* Insertion of arbitrary Python expressions:: |
| 79 |
|
| 80 |
character(*), parameter :: comp_date = "${time.strftime('%Y-%m-%d')}$" |
| 81 |
|
| 82 |
* Inclusion of files during preprocessing:: |
| 83 |
|
| 84 |
#:include "macrodefs.fypp" |
| 85 |
|
| 86 |
* Using Fortran-style continutation lines in preprocessor directives:: |
| 87 |
|
| 88 |
#:if var1 > var2 & |
| 89 |
& or var2 > var4 |
| 90 |
print *, "Doing something here" |
| 91 |
#:endif |
| 92 |
|
| 93 |
* Passing (unquoted) multiline string arguments to callables:: |
| 94 |
|
| 95 |
#! Callable needs only string argument |
| 96 |
#:def DEBUG_CODE(code) |
| 97 |
#:if DEBUG > 0 |
| 98 |
$:code |
| 99 |
#:endif |
| 100 |
#:enddef DEBUG_CODE |
| 101 |
|
| 102 |
#! Pass code block as first positional argument |
| 103 |
#:block DEBUG_CODE |
| 104 |
if (size(array) > 100) then |
| 105 |
print *, "DEBUG: spuriously large array" |
| 106 |
end if |
| 107 |
#:endblock DEBUG_CODE |
| 108 |
|
| 109 |
#! Callable needs also non-string argument types |
| 110 |
#:def REPEAT_CODE(code, repeat) |
| 111 |
#:for ind in range(repeat) |
| 112 |
$:code |
| 113 |
#:endfor |
| 114 |
#:enddef REPEAT_CODE |
| 115 |
|
| 116 |
#! Pass code block as positional argument and 3 as keyword argument "repeat" |
| 117 |
#:block REPEAT_CODE(repeat=3) |
| 118 |
this will be repeated 3 times |
| 119 |
#:endblock REPEAT_CODE |
| 120 |
|
| 121 |
* Preprocessor comments:: |
| 122 |
|
| 123 |
#! This will not show up in the output |
| 124 |
#! Also the newline characters at the end of the lines will be suppressed |
| 125 |
|
| 126 |
* Suppressing the preprocessor output in selected regions:: |
| 127 |
|
| 128 |
#! Definitions are read, but no output (e.g. newlines) will be produced |
| 129 |
#:mute |
| 130 |
#:include "macrodefs.fypp" |
| 131 |
#:endmute |
| 132 |
|
| 133 |
* Explicit request for stopping the preprocessor:: |
| 134 |
|
| 135 |
#:if DEBUGLEVEL < 0 |
| 136 |
#:stop 'Negative debug level not allowed!' |
| 137 |
#:endif |
| 138 |
|
| 139 |
* Easy check for macro parameter sanity:: |
| 140 |
|
| 141 |
#:def mymacro(RANK) |
| 142 |
#! Macro only works for RANK 1 and above |
| 143 |
#:assert RANK > 0 |
| 144 |
: |
| 145 |
#:enddef mymacro |
| 146 |
|
| 147 |
* Line numbering directives in output:: |
| 148 |
|
| 149 |
program test |
| 150 |
#:if defined('MPI') |
| 151 |
use mpi |
| 152 |
#:endif |
| 153 |
: |
| 154 |
|
| 155 |
transformed to :: |
| 156 |
|
| 157 |
# 1 "test.fypp" 1 |
| 158 |
program test |
| 159 |
# 3 "test.fypp" |
| 160 |
use mpi |
| 161 |
# 5 "test.fypp" |
| 162 |
: |
| 163 |
|
| 164 |
when variable ``MPI`` is defined and Fypp was instructed to generate line |
| 165 |
markers. |
| 166 |
|
| 167 |
* Automatic folding of generated lines exceeding line length limit |
| 168 |
|
| 169 |
|
| 170 |
Installing |
| 171 |
========== |
| 172 |
|
| 173 |
Fypp needs a working Python 3 interpreter (Python 3.5 or above). |
| 174 |
|
| 175 |
When you install Fypp, you obtain the command line tool ``fypp`` and the Python |
| 176 |
module ``fypp.py``. Latter you can import if you want to access the |
| 177 |
functionality of Fypp directly from within your Python scripts. |
| 178 |
|
| 179 |
|
| 180 |
Installing via conda |
| 181 |
-------------------- |
| 182 |
|
| 183 |
The last stable release of Fypp can be easily installed as conda package by |
| 184 |
issuing :: |
| 185 |
|
| 186 |
conda install -c conda-forge fypp |
| 187 |
|
| 188 |
|
| 189 |
Installing via pip |
| 190 |
------------------ |
| 191 |
|
| 192 |
You can also use Pythons command line installer ``pip`` in order to download the |
| 193 |
stable release from the `Fypp page on PyPI <http://pypi.python.org/pypi/fypp>`_ |
| 194 |
and install it on your system. |
| 195 |
|
| 196 |
If you want to install Fypp into the module system of the active Python 3 |
| 197 |
interpreter (typically the case when you are using a Python virtual |
| 198 |
environment), issue :: |
| 199 |
|
| 200 |
pip3 install fypp |
| 201 |
|
| 202 |
Alternatively, you can install Fypp into the user space (under `~/.local`) with |
| 203 |
:: |
| 204 |
|
| 205 |
pip3 install --user fypp |
| 206 |
|
| 207 |
|
| 208 |
Installing via MSYS2 pacman |
| 209 |
--------------------------- |
| 210 |
|
| 211 |
On Windows you can use the `MSYS2 toolchain <https://www.msys2.org/>`_ to install |
| 212 |
Fypp in a MinGW terminal. To install Fypp use:: |
| 213 |
|
| 214 |
pacman -S mingw-w64-x86_64-python-fypp |
| 215 |
|
| 216 |
Make sure the selected architecture is matching your current MinGW terminal. |
| 217 |
For all supporting MinGW architectures visit check the package index |
| 218 |
`here <https://packages.msys2.org/base/mingw-w64-python-fypp>`_. |
| 219 |
|
| 220 |
|
| 221 |
Manual install |
| 222 |
-------------- |
| 223 |
|
| 224 |
For a manual install, you can download the source code of the **stable** |
| 225 |
releases from the `Fypp project website |
| 226 |
<https://github.com/aradi/fypp/releases>`_. |
| 227 |
|
| 228 |
If you wish to obtain the latest **development** version, clone the projects |
| 229 |
repository:: |
| 230 |
|
| 231 |
git clone https://github.com/aradi/fypp.git |
| 232 |
|
| 233 |
and check out the `master` branch. |
| 234 |
|
| 235 |
The command line tool is a single stand-alone script. You can run it directly |
| 236 |
from the source folder :: |
| 237 |
|
| 238 |
FYPP_SOURCE_FOLDER/bin/fypp |
| 239 |
|
| 240 |
or after copying it from the `bin` folder to any location listed in your `PATH` |
| 241 |
environment variable, by just issuing :: |
| 242 |
|
| 243 |
fypp |
| 244 |
|
| 245 |
The python module ``fypp.py`` can be found in ``FYP_SOURCE_FOLDER/src``. |
| 246 |
|
| 247 |
|
| 248 |
Running |
| 249 |
======= |
| 250 |
|
| 251 |
The Fypp command line tool reads a file, preprocesses it and writes it to |
| 252 |
another file, so you would typically invoke it like:: |
| 253 |
|
| 254 |
fypp source.fpp source.f90 |
| 255 |
|
| 256 |
which would process `source.fpp` and write the result to `source.f90`. If |
| 257 |
input and output files are not specified, information is read from stdin and |
| 258 |
written to stdout. |
| 259 |
|
| 260 |
The behavior of Fypp can be influenced with various command line options. A |
| 261 |
summary of all command line options can be obtained by:: |
| 262 |
|
| 263 |
fypp -h |