Package: jin2for
jin2for - Generate FORTRAN source files from jinja2 templatesWhyThe FORTRAN programing language has not a built-in templating engine like C++ does.
In concequence, it is not unusual to end-up needing to write code like this onefunctionsum_i2(a,b)result(c)implicit noneinteger(2),intent(in)::ainteger(2),intent(in)::binteger(2)::cc=a+bend functionfunctionsum_i4(a,b)result(c)implicit noneinteger(4),intent(in)::ainteger(4),intent(in)::binteger(4)::cc=a+bend functionfunctionsum_i8(a,b)result(c)implicit noneinteger(8),intent(in)::ainteger(8),intent(in)::binteger(8)::cc=a+bend functionThe standard approach to circumvent code repetition is to use pre-processor directives.
However, the functionality of the preprocessor is quite limited and this approach often
leads to criptic code that is difficult to read.The goal ofjin2foris to facilitate the usage ofjinja2as a more flexible preprocessor in FORTRAN projects.jinja2is a mature, powerful, and flexible templeting engine that allows to write templates like this one:{%foripin[2,4,8]%}functionsum_i{{ip}}(a,b)result(c)implicit noneinteger({{ip}}),intent(in)::ainteger({{ip}}),intent(in)::binteger({{ip}})::cc=a+bend function{%endfor%}If you store this templated code in a file, sayfoo.t90, and compile it withjin2for$jin2forfoo.t90you will obtain a filefoo.f90that contains piece of FORTRAN code shown at the beginning of this README file.
Internally,jin2foruses thejinja2engine to render the templates. So, any validjinja2template can be processed in this way.jin2for is environment awarejin2forpredefines a number of useful python objects that can be used as template parameters.For instance, if we want to generate the previoussumfunction for all the integer
types available in the installedgfortrancompiler, we can use the predefinedinteger_typesobject.
Create a filesum_ints.t90containing this template:{%fortininteger_types%}functionsum_{{t.alias}}(a,b)result(c)implicit none{{t.decl}},intent(in)::a{{t.decl}},intent(in)::b{{t.decl}}::cc=a+bend function{%endfor%}and compile it with$jin2for--generate-forgfortransum_ints.t90jin2forwill find out for you which are the supported integer kinds of the installedgfortrancompiler
and render the template only for those kinds.Installationjin2foris a command line application written in Python 3 and distributed viapypi.org.It can be installed using pip:$pipinstalljin2forIf you obtain any installation error, make sure that you are using Python 3, e.g.,$python3-mpipinstalljin2forThe latest development version can be installed manually from soruce:$gitclonehttps://gitlab.com/fverdugo/jin2for
$cdjin2for
$python3setup.pytest# optional$python3setup.pyinstallUsageBasic usageThe basic command line interface (CLI) ressembles to the one of the GNU and Intel compilers. For instance,$jin2forfile1.t90file2.t90will "compile" (i.e., render) thejinja2template filesfile1.t90andfile2.t90into the FORTRAN source filesfile1.f90andfile2.f90.
By default,jin2forwill replace the extension of the input files to.f90to generate the output file names.The filesfile1.t90andfile2.t90are allowed to include or import some otherjinja2templates containing, e.g., macros or definitions.
If these included template files are located in a folder,
namelyfolder/for/included/templates, different from the current working directory,jin2forhas to be informed with the-Iflag:$jin2for-Ifolder/for/included/templatesfile1.t90file2.t90More advanced usageSee documentationDocumentationFor the template syntax, see the officialjinja2 documentation.For the full CLI reference:$jin2for-h