Fortran submodule file naming

In many applications, a Fortran submodule is defined in a different file than the Fortran module that uses it. Build generators such as CMake and Meson must consider that each Fortran compiler treats Fortran submodule interface file naming distinctively. Fortran module interface files are akin to C++20 modules. Fortran module files are automatically generated and generally not human-readable. Thanks to code we contributed Meson ≥ 0.50 supports Fortran submodule.

Examples

A brief taxonomy of file naming conventions for Fortran submodules follows. Assume a two-file program:

file basic.f90

module demo
real, parameter :: pi = 4.*atan(1.)
real :: tau

interface
  module subroutine hello(pi,tau)
    real, intent(in) :: pi
    real, intent(out) :: tau
  end subroutine hello
end interface
contains
end module demo

program sm
use demo
call hello(pi, tau)
print *,'pi=',pi, 'tau=', tau
end program

file basic_sub.f90

submodule (demo) hi
contains
module procedure hello
  tau = 2*pi
end procedure hello
end submodule hi

Note that the order of the commands for each compiler matters–the module must be built before the submodule, to generate the necessary module interface files BEFORE compiling the submodule. To simplify the output, we only include commands for Gfortran–substitute the desired compiler name. Of course, each compiler creates corresponding basic.o and basic_sub.o object files as usual, omitted here for clarity.

  1. gfortran -c basic.90 creates files: basic.o demo.mod demo.smod.
  2. gfortran -c basic_sub.f90 -o basic_sub.o creates files: basic_sub.o demo@hi.smod.
  3. gfortran basic.o basic_sub.o -o basic creates executable basic

Here is a table of the module interface files generated by each compiler. “module” are the files generated by step #1, building the file containing the Fortran module. “submodule” are the files generated by step #2, building the containing the Fortran submodule.

Compilermodule filessubmodule files
gfortrandemo.mod demo.smoddemo@hi.smod
flangdemo.moddemo-hi.mod
nvfortrandemo.moddemo-hi.mod
ifortdemo.moddemo@hi.smod
xlf2008demo.moddemo_hi.smod

Notes