SWIG-Fortran for creating Fortran wrappers to C++ libraries

The SWIG-Fortran is a tool to auto create native fortran interfaces, so that one can access C/C++ based libraries in Fortran codes. Relevant links:

We list it at Programming utilities - Fortran Programming Language.

I noticed we list the license as GPL, but it seems to be MIT, we should fix that.

One thing that I was wondering is whether one can leverage Clang to simply create the Fortran wrappers (perhaps via SWIG-Fortran) automatically. There are tools like that for Python, such as:

2 Likes

I always wondered why SWIG-Fortran is maintained as fork of SWIG rather than contributed to upstream. SWIG itself is a standard tool installable with most package managers, building the patched SWIG-Fortran version from source to make use of the Fortran export seems inconvenient, because it requires to manage multiple SWIG installations on the same system.

Since SWIG-Fortran is based on SWIG, which is GPL, I don’t see how the patched version can be MIT. The required patches can be available under MIT, but this doesn’t change the version of the project.

SWIG builds on interface files, which in the simplest case are just the C or C++ header files, but also allow for some additional customization of the interface. It is quite a powerful tool, once you figured out how to set it up properly, you can basically export to any supported language in SWIG, which makes it much wider in scope than cppyy, cffi or pybind11 which specialize on one particular language binding.

3 Likes

An interesting perspective on SWIG can be found here, the live experience section is very insightful.

I was in a webinar where the author of the SWIG-Fortran project mentioned that he had been trying to get them to accept PRs for several years without any luck…

1 Like

I found the PR against SWIG here:

A 16k line patch is quite a thing. Getting big patches merged is always a lot of work for everybody involved, both contributors and maintainers. This one has unfortunately gone stale.

3 Likes

The author of swig-fortran tried hard to make this PR happen, but it has not passed so far. What a pity. The demand for the c-fortran interface is very large. It is time-consuming and laborious to do it (c to fortran interface) all manually, and this kind of work is very boring.
I hope there is something like swig-fortran that can generate c-fortran code interfaces automatically. Maybe fortran-lang organization can contribute to such a package in the future, which is significant!

4 Likes

Any idea why the initial author stopped working on this project? I looked through those comments and am not sure why the problems could not be worked out.

The author has a related project that exports C++11 standard library features to fortran using a fork of Swig.

Docs are at: https://flibcpp.readthedocs.io/en/latest/ .

Indeed. The issues section shows repeated attempts by the author of swig-fortran trying to reach out to the reviewer(s) over 3 years but seems to receive limited guidance and then the response seems to go silent. That is unfortunate.

Keep in mind that we are in no position to judge any of the involved parties here. Yes, it is sad that this project is stuck, but sometimes things just don’t work out. It can be from time to time difficult for first-time contributors to communicate with an established community and similarly it can be a huge demand on project maintainers to help with the on-boarding of new developers.

The more important question here is whether there is a way for us to help move this project forward again.

1 Like

This was an interesting post from someone who had been the author of earlier versions of SWIG from the Python developer lists.

Parsing C++ was the largest technical hurdle. Another option would be to use the Clang C++ front end, and write new code that generates the back end.

But given that SWIG exists, I still think it might be less work overall to hack on it and get the Fortran extension to work.

1 Like

Yes, I think from the user perspective the Clang path would be the most friendly.