Use gfortran (>=4.5) to generate makefile dependencies based on .mod files.

Registered by David Ham

We currently have Fortran module dependencies hand-coded in . These dependencies are always on the .o file of the module and they only take into account the current directory. Gfortran (>=4.5) supports the generation of makefile dependencies by the compiler. With the addition of some scripting, this can be used to generate makefile dependencies which extend over multiple directories and result in fewer spurious recompilations.

Blueprint information

David Ham
Needs approval
David Ham
Series goal:
Milestone target:
Started by
David Ham
Completed by
David Ham



Important points to note:

* Even though gfortran is needed to generate the dependencies, the resulting makefiles can be used by other compilers, as long as those compilers create module files of the form modulename.mod. The last compiler of which I am aware which did not follow this convention was Intel v6 about 10 years ago. I note, for example, that scons considers .mod to be a "quasi-standard".

* Gfortran (and possibly other compilers) are smart enough not to regenerate .mod files when the interface doesn't change. This means an end to spurious compilation cascades when modules are edited. Unfortunately Intel is not as smart as this (see However this is a feature which is mainly of use to developers as compilation cascades are not an issue if source is not being edited and everyone develops using gfortran. It should be noted that the generated makefiles will still work with intel, they just don't fix the compilation cascade problem.

* As a side effect, compilation dependencies on .h files and #included .F90 files (eg reference counting) are created. This is generally a Good Thing with two exceptions: it generates dependencies on system libraries with system-specific absolute paths and it generates dependencies on confdefs.h, a file which is updated for every compilation. The script implementation therefore drops all absolute path dependencies and the dependency on confdefs.h.


The feature is implemented in the branch below.

The makefile generation capabilities of gfortran require gfortran to actually generate the .mod files in order to know which .mod to depend on. This creates something of a chicken and egg situation. The script provided (scripts/ deals with this by repeatedly trying all the .F90 files in the directory until it is able to compile them. This is a form of stupid make which is O((number of files)^2), however the regeneration of makefiles is an event which only occurs when module dependencies change and the process only takes a few minutes even with the stupid search in place.

The speed of generation is further helped if the generation is performed from a built (or partially built) tree. In this case most of the .mod files are in place so most files compile on the first pass.

The generator rewrites all entries in Makefile and below the line:

#####Automatically generated dependencies. Do not edit below this line#####

users should not enter their own module dependencies either above or below this line.


To generate updated makefiles type the following:

make # This ensures that as many .mod files as possible already exist. In particular it is necessary so that spud.mod exists.

make makefiles

Now test the new makefiles with:

make clean; make

You will now have modified files which you need to commit.


Work Items

This blueprint contains Public information 
Everyone can see this information.