Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

gfortran and -M

300 views
Skip to first unread message

Zaak

unread,
Mar 4, 2014, 12:32:56 PM3/4/14
to
Has anyone had success using gfortran with the -M flag (and its variants -MD -MMD etc.) to facilitate automatic dependency generation? It seems to me that it's not possible because any mod files on which the source file depends must be present to successfully output the dependency info of that source file.

FX

unread,
Mar 4, 2014, 3:04:03 PM3/4/14
to
> Has anyone had success using gfortran with the -M flag (and its
> variants -MD -MMD etc.) to facilitate automatic dependency generation?

-M options are not handlded by gfortran.

--
FX

Thomas Jahns

unread,
Mar 5, 2014, 5:05:22 AM3/5/14
to
On 03/04/14 18:32, Zaak wrote:
> Has anyone had success using gfortran with the -M flag (and its variants -MD -MMD etc.) to facilitate automatic dependency generation? It seems to me that it's not possible because any mod files on which the source file depends must be present to successfully output the dependency info of that source file.

This is unfortunately a much more difficult problem than might appear. Since
Fortran 90, compilation of Fortran source files not only depends on other source
files but also module description files that compilers generate as a side-effect
as either separate files (typically named with a .mod suffix, but other
conventions exist) or part of object files.

Since dependencies are therefore (partially) on something which has to come into
existence before the compiler is run, it is necessary to find dependencies
before running the compiler, i.e. running the compiler on some source file
cannot be used to emit dependencies for that source.

Several utilities to tackle this problem have been devised. I myself offer a
tool to generate dependencies at

https://www.dkrz.de/redmine/attachments/download/435/makef90depends-1.0.1.tar.gz

The contained Perl script generates a Makefile snippet when called with a bunch
of sources on the command-line. If you want it to work with pre-processor
conditionals and pre-installed .mod files some additional options need to be
specified.

Dependencies can also be prescribed manually in Makefiles if they are static.

Regards, Thomas

Erik Toussaint

unread,
Mar 5, 2014, 7:15:59 AM3/5/14
to
It works for me, although I'm a hobby programmer and almost exclusively
work on small projects with just a handful of source files. I can see
how this may not be feasible for larger projects, because of the
recursive nature, but I like how this doesn't require any extra tools
besides the compiler and make.

I'm not sure if this is particular to GNU make or works this way with
any POSIX make, but it tries to remake any makefiles that are out of
date or don't exist, and then restarts. This includes files included by
the include directive, like dependency files.

Depending on the order it tries, initially it may fail creating
dependency files for all source files that depend on a module file from
another source file, but it will work for at least one module (at the
root of the dependency tree). As long as one dependency file is remade,
make will restart and in the next run try to create the remaining
dependency files, and so on. This recursive behaviour causes all module
files to be created in the end.

Erik.

Zaak

unread,
Mar 5, 2014, 3:39:24 PM3/5/14
to
First off,
Thomas, thanks for the link to your tool. I am actually using makedepf90 at the moment, with a ton of sed hackery to examine source files one at a time. http://personal.inet.fi/private/erikedelmann/makedepf90/

On Wednesday, March 5, 2014 7:15:59 AM UTC-5, Erik Toussaint wrote:
> It works for me, although I'm a hobby programmer and almost exclusively
> work on small projects with just a handful of source files. I can see
> how this may not be feasible for larger projects, because of the
> recursive nature, but I like how this doesn't require any extra tools
> besides the compiler and make.
>
> I'm not sure if this is particular to GNU make or works this way with
> any POSIX make, but it tries to remake any makefiles that are out of
> date or don't exist, and then restarts. This includes files included by
> the include directive, like dependency files.
>
> Depending on the order it tries, initially it may fail creating
> dependency files for all source files that depend on a module file from
> another source file, but it will work for at least one module (at the
> root of the dependency tree). As long as one dependency file is remade,
> make will restart and in the next run try to create the remaining
> dependency files, and so on. This recursive behaviour causes all module
> files to be created in the end.
>
> Erik.

Erik,
I was aware that GNU make recursively updates included portions of Makefiles if a rule exists to build them, but didn't quite understand the semantics of how it keeps trying. I guess you're right, and that with MANY invocations of make it should be able to work to update all the dependency information as long as all source files are present. Perhaps if you stashed all the dependency info in an order-only prerequisite generated directory a good approach might be to use some sed/awk scripting and a tool like makedepf90 to populate that directory with the dependency includes, then use the compiler to update that info whenever an object file is built.

I think that the -M flag is handled by some back end and not gfortran itself, as FX pointed out. It's unfortunate that the compiler can't generate the dependency info as it's parsing the source even if there are missing .mod files. While it seems that they have other issues with the -gen-dep flag, Intel seems to be able to do this, although other bugs diminish its utility at the moment.
0 new messages