Skip to content

How gtk fortran is generated

vmagnin edited this page May 26, 2023 · 6 revisions

How gtk-fortran is generated

For each release of gtk-fortran with new GTK/GLib versions, we use the src/cfwrapper/cfwrapper.py script under Linux to parse all the GTK .h header files in /usr/include/ and automatically generate the src/*-auto.f90 files containing interfaces to the C functions. It also writes the list of all those functions with information on the required files and the C prototypes in the src/gtk-fortran-index.csv file, a cfwrapper/cfwrapper-errors.csv file listing the encountered problems, and the list of GTK and funptr types in src/gtk-fortran_types.csv and src/gtk-fortran_funptr.csv.

The most important option of the Python script is -g to choose the GTK major version to use:

$ ./cfwrapper.py --help
usage: cfwrapper.py [-h] [-g 2|3|4 | -l LIBRARY [LIBRARY ...]] [-m MODULE [MODULE ...]] [-v VERSION] [-b] [-d] [-s SUFFIX]

Generate gtk-fortran files (can also be tried on other libraries)

options:
  -h, --help            show this help message and exit
  -g 2|3|4, --gtk 2|3|4
                        GTK major version
  -l LIBRARY [LIBRARY ...], --library LIBRARY [LIBRARY ...]
                        Directory containing the header files
  -m MODULE [MODULE ...], --module MODULE [MODULE ...]
                        Name of the corresponding Fortran module (must be used with -l)
  -v VERSION, --version VERSION
                        gtk-fortran semantic versioning
  -b, --build           Build gtk-fortran libraries and examples
  -d, --deprecated      Remove deprecated functions
  -s SUFFIX, --suffix SUFFIX
                        Add a suffix to the functions names

Examples:
$ ./cfwrapper.py -g 4 -v 4.4.0 -b
For libraries other than GTK:
$ ./cfwrapper.py -l /usr/include/foo1 /usr/include/foo2 -m my_foo1 my_foo2

GPLv3 license with RLE exception 3.1, https://github.com/vmagnin/gtk-fortran

Let's for example generate a new gtk-fortran 4.4.0 version with GTK 4:

$ ./cfwrapper.py -g 4 -v 4.4.0
 Pass 1: looking for enumerators, funptr and derived types...
 Pass 2: looking for C functions... 
/usr/include/cairo               =>  cairo-auto.f90          136322 bytes
/usr/include/gdk-pixbuf-2.0      =>  gdk-pixbuf-auto.f90      62797 bytes
/usr/include/glib-2.0            =>  glib-auto.f90          1427105 bytes
/usr/include/gtk-4.0/gdk         =>  gdk-auto.f90            195412 bytes
/usr/include/gtk-4.0/gsk         =>  gsk-auto.f90             72999 bytes
/usr/include/gtk-4.0/gtk         =>  gtk-auto.in            1147472 bytes
/usr/include/gtk-4.0/unix-print  =>  unix-print-auto.f90      26101 bytes
/usr/include/graphene-1.0        =>  graphene-auto.f90       125930 bytes
/usr/include/pango-1.0           =>  pango-auto.f90          199918 bytes

Extracting GDK events for gtk-4-fortran

=== Statistics (ready to paste in the Status wiki page) ===

## gtk-fortran 4.4.0, GTK 4.10.1, GLib 2.76.1, Ubuntu 23.04 x86_64, Python 3.11.2
vmagnin, Fri, 26 May 2023 14:43:02 +0000
* nb_files scanned =           739
* nb_generated_interfaces =  10312
* nb_deprecated_functions =   1119
* nb_type_errors =              87
* nb_errors (others) =         335
* nb_lines treated =         33234
* nb_variadic functions =      141
* nb_enumerators =             372
* nb_win32_utf8 =                0
* Number of types =             81
* Number of funptr types =     210
* Computing time: 5.97 s
* SHA1:  8ff34837481c8ecd55a516fe90f9d7aa00341a03
 >>>>>> SHA 1 HAS BEEN MODIFIED ! It was  14d240a345bea0ad4ef0d4747cdd1acc6f6e8eb2  <<<<<< 


 Used types: ['c_ptr', 'c_char', 'c_int', 'c_funptr', 'c_double', 'c_long', 'c_int32_t', 'c_int8_t', 'c_float', 'c_size_t', 'c_int64_t', 'c_int16_t', '?', 'c_long_double']

The statistics are archived on the Status page. Note that the process is fast: only a few seconds.

Now, when building the project, two things can happen:

  • The build process runs flawlessly :-) The new gtk-fortran release can be pushed!
  • The build fails because some *-auto.f90 files contain weird things. It typically occurs when some new coding conventions are introduced in some .h file of a GTK library, or when some new C preprocessor constants appear in some files (for example https://github.com/vmagnin/gtk-fortran/issues/234). In that case, one or more regex must be updated or added in the wrapper scripts.

It can happen that the function you need is not in gtk-fortran: you can post an issue and I will try to tune the cfwrapper.py to detect it, but in the meanwhile you can of course write your own Fortran / C interface for that function in your program.

See Known issues and limitations to know more about our wrapper limitations.

Clone this wiki locally