Fortran submodule file naming

In many applications, a Fortran submodule is defined in a different file than the Fortran module that uses it. A slight problem arises for build generators since each Fortran compiler treats Fortran submodule interface file naming distinctively. Fortran module interface files are somewhat of a dual to the header files of C / C++, except that module files are automatically generated and generally not human-readable. This non-standard Fortran submodule file naming causes breakage to build generation systems such as CMake Thanks to code we contributed, Meson ≥ 0.50 supports Fortran submodule across various Fortran 2008 compilers.

Examples

For the benefit of compiler vendors and build generators developers here is a brief taxonomy of file naming conventions for Fortran submodules. In all cases, we assume a two-file program as follows:

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–substiute 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
pgfortrandemo.moddemo-hi.mod
ifortdemo.moddemo@hi.smod
xlf2008demo.moddemo_hi.smod

Notes