Commit 38fe3a85 authored by Lorenz Huedepohl's avatar Lorenz Huedepohl

Remove fdep

Will be added in the subsequent commit as a 'subtree' from upstream
parent bb046d1c
Copyright (c) 2013 Lorenz Hüdepohl
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
fdep
----
fdep is a small set of scripts to teach autoconf/automake (using GNU make)
about the additional dependencies in Fortran 90 files due to modules.
With this, Fortran files can be listed in any order in Makefile.am and parallel
builds work.
Usage
-----
Put this project as a directory "fdep" in your source code, place the two
lines
m4_include([fdep/fortran_dependencies.m4])
FDEP_F90_GNU_MAKE_DEPS
in your configure.ac, and add a single line
@FORTRAN_MODULE_DEPS@
in your Makefile.am. All .F90 files of all programs in bin_PROGRAMS and all
libraries in lib_LTLIBRARIES will now be scanned for modules and the
resulting dependencies will be honoured.
What is the problem with Fortran 90 modules and make dependencies?
------------------------------------------------------------------
In Fortran 90 source files one can define any number of "modules", containing
variable and function definitions. The names of the modules defined in a file
can be arbitrary.
In another source file these modules can be used, informing the Fortran
compiler about the definitions in these modules (e.g. to do type-checking).
This creates a problem, as the compiler has to know somehow where the module
is defined.
The usual solution employed by almost every Fortran compiler is to create
special "module" files for each module contained in a source file during
compilation. Their file name is derived by a compiler-specific recipe of the
modules identifier (usually the lower-cased module's identifier plus ".mod",
so "foo_module.mod" and "some_other_module.mod"). When the compiler
encounters a "use" statement during the compilation of another file, it
confers to this file to import the definitions of the module.
That means, you cannot compile files using modules defined in yet un-compiled
files, one has to tell make about this dependency.
(A primitive solution to this problem is listing the file in a pre-sorted
order, so that files defining modules are compiled first.
However, that way the dependency-graph make knows about is incomplete and
parallel builds will fail with a high probability)
How does fdep solve this problem technically?
---------------------------------------------
As the name of the module files can be an arbitrary (and some compilers might
even save the module definitions in some completely different way), fdep
tells make about the module dependencies as a relation directly between
object files, e.g. when a file 'b.f90' is using any module of file 'a.f90',
fdep adds a dependency of
b.o: a.o
More specifically, the perl-script fortran_dependencies.pl is run by make to
create a file .fortran_dependencies/dependencies.mk, which is then included.
To do this, first every source file (for every defined program and library)
is scanned for lines with "module" or "use" statements. These are saved in
two additional files (.use_mods and .def_mods) per source file and contain
lists of defined and required modules. The perl script then reads these in
and produces the appropriate rules.
Drawbacks
---------
GNU make is required. The detailed dependency graph due to "module" and "use"
statements is only available after pre-processing, when autoconf and even
configure is long over. To still get proper dependencies, fdep uses GNU
make's feature to include generated sub-Makefiles during a running make
invocation.
License
-------
fdep is released under the MIT License. See the LICENSE file for details.
Contributing
------------
Send your patches or pull-request to dev@stellardeath.org
AC_DEFUN([FDEP_F90_GNU_MAKE_DEPS],[
AC_MSG_CHECKING([for GNU make])
for a in "$MAKE" make gmake gnumake ; do
if test -z "$a" ; then continue ; fi ;
if ( sh -c "$a --version" 2> /dev/null | grep GNU 2>&1 > /dev/null ) ; then
_fdep_gnu_make_command=$a ;
break;
fi
done ;
AC_MSG_RESULT([$_fdep_gnu_make_command])
if test x$_fdep_gnu_make_command = x ; then
AC_MSG_ERROR([Need GNU Make])
fi
AC_SUBST([FORTRAN_MODULE_DEPS], ["
CLEANFILES +=
include ${srcdir}/fdep/fortran_dependencies.mk
"])
AM_SUBST_NOTMAKE([FORTRAN_MODULE_DEPS])
])
_f90_verbose = $(_f90_verbose_$(V))
_f90_verbose_ = $(_f90_verbose_$(AM_DEFAULT_VERBOSITY))
_f90_verbose_0 = @echo " $1";
_f90_targets = $(subst -,_,$(patsubst %.la,%_la,$(patsubst %.a,%_a,$(patsubst %.so,%_so,$(PROGRAMS) $(LTLIBRARIES)))))
FORTRAN_CPP ?= cpp -P -traditional -Wall -Werror
# $1 source files
#
# returns: file without any .F90 .f90 .F .f extension
define strip_fortran_ext
$(patsubst %.F90,%,$(patsubst %.f90,%,$(patsubst %.F,%,$(patsubst %.f,%,$1))))
endef
# $1 program
#
# returns:
# '1' if object files for target $1 are prefixed due to 'per-target' flags,
# '' (the empty string) otherwise. See the automake manual for 'per-target'
# compilation
#
define is_per_target
$(if $(filter $(call strip_fortran_ext,$(firstword $(call fortran_sources,$1))),$(patsubst %.o,%,$(patsubst %.lo,%,$($1_OBJECTS)))),,1)
endef
# $1 top-level target name (i.e. an entry of _f90_targets)
#
# returns: all target source files matching *.F90 *.f90 *.F *.f
define fortran_sources
$(filter %.F90 %.f90 %.F %.f,$($1_SOURCES))
endef
# $1 top-level target name
#
# returns: the appropriate extension (i.e. 'o' for normal programs, '.lo' for libraries)
define object_extension
$(if $(filter $1,$(PROGRAMS)),o,lo)
endef
# $1 source_file
# $2 stem
# $3 program
define module_targets
$(eval _$3_use_mods += $(dir $1)$2$(call strip_fortran_ext,$(notdir $1)).use_mods.$3.$(call object_extension,$3))
$(dir $1)$2$(call strip_fortran_ext,$(notdir $1)).use_mods.$3.$(call object_extension,$3): $1 $(dir $1)$(am__dirstamp)
$(call _f90_verbose,F90 USE [$3] $$<)$(FORTRAN_CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $($p_CPPFLAGS) $(CPPFLAGS) -o /dev/stdout $$< | grep -i -o '^ *use [^ ,!:]*' | sort -u > $$@
$(eval _$3_def_mods += $(dir $1)$2$(call strip_fortran_ext,$(notdir $1)).def_mods.$3.$(call object_extension,$3))
$(dir $1)$2$(call strip_fortran_ext,$(notdir $1)).def_mods.$3.$(call object_extension,$3): $1 $(dir $1)$(am__dirstamp)
$(call _f90_verbose,F90 MOD [$3] $$<)$(FORTRAN_CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $($p_CPPFLAGS) $(CPPFLAGS) -o /dev/stdout $$< | grep -i -o '^ *module [^!]*' | grep -v "\<procedure\>" > $$@ || true
endef
$(foreach p,$(_f90_targets),$(if $(call is_per_target,$p),$(foreach s,$(call fortran_sources,$p),$(eval $(call module_targets,$s,$p-,$p))),$(foreach s,$(call fortran_sources,$p),$(eval $(call module_targets,$s,,$p)))))
_f90_depdir=$(abs_builddir)/.fortran_dependencies
_f90_depfile = $(_f90_depdir)/dependencies.mk
define is_clean
$(if $(filter-out mostlyclean clean distclean maintainer-clean,$(MAKECMDGOALS)),0,1)
endef
define _fdep_newline
endef
ifneq ($(call is_clean),1)
include $(_f90_depfile)
endif
$(_f90_depfile): $(top_srcdir)/fdep/fortran_dependencies.pl $(foreach p,$(_f90_targets),$(_$p_use_mods) $(_$p_def_mods)) | $(foreach p,$(_f90_targets),$(_f90_depdir)/$p)
$(call _f90_verbose,F90 DEPS $@)echo > $@; $(foreach p,$(_f90_targets),$(top_srcdir)/fdep/fortran_dependencies.pl $p $(_$p_use_mods) $(_$p_def_mods) >> $@ || { rm $@; exit 1; } ;$(_fdep_newline))
$(_f90_depdir):
@mkdir $@
$(foreach p,$(_f90_targets),$(_f90_depdir)/$p): | $(_f90_depdir)
@mkdir $@
CLEANFILES += $(foreach p,$(_f90_targets),$(_$p_def_mods) $(_$p_use_mods))
CLEANFILES += $(foreach p,$(_f90_targets),$(_f90_depdir)/$p/*)
CLEANFILES += $(_f90_depfile)
#!/usr/bin/perl -w
use strict;
my %defs = ();
my %uses = ();
my $use_re = qr/^\s*use\s+(\S+)\s*$/;
my $def_re = qr/^\s*module\s+(\S+)\s*$/;
sub add_use {
my ($file, $module) = @_;
if (defined($defs{$module}) && $defs{$module} eq $file) {
# do not add self-dependencies
return;
}
if (!defined($uses{$file})) {
$uses{$file} = { $module => 1 };
} else {
$uses{$file}{$module} = 1;
}
}
sub add_def {
my ($file, $module) = @_;
if (!defined($defs{$module})) {
$defs{$module} = $file;
if (defined($uses{$file}) && defined($uses{$file}{$module})) {
delete $uses{$file}{$module};
}
} else {
die "Module $module both defined in $file, $defs{$module}";
}
}
my $p = shift;
foreach my $file (@ARGV) {
my $re;
my $add;
my $object;
if (defined($ENV{V}) && $ENV{V} ge "2") {
print STDERR "fdep: Considering file $file\n";
}
if ($file =~ /^(.*)\.def_mods.$p(\..*)$/) {
$re = $def_re;
$add = \&add_def;
$object = $1 . $2;
} elsif ($file =~ /^(.*)\.use_mods.$p(\..*)$/) {
$re = $use_re;
$add = \&add_use;
$object = $1 . $2;
} else {
die "Unrecognized file extension for '$file'\nExpected (.*)\.def_mods.$p(\..*) or (.*)\.use_mods.$p(\..*)";
}
open(FILE,"<",$file) || die "\nCan't open $file: $!\n\n";
while(<FILE>) {
chomp;
$_ = lc($_);
if ($_ =~ $re) {
&$add($object, $1);
} else {
die "Cannot parse module statement '$_', was expecting $re";
}
}
close(FILE)
}
foreach my $object (sort keys %uses) {
for my $m (keys %{$uses{$object}}) {
if (defined $defs{$m}) {
print "$object: ", $defs{$m}, "\n";
} elsif (defined($ENV{V}) && $ENV{V} ge "1") {
print STDERR "fdep: Warning: Cannot find definition of module $m in files for program $p, might be external\n";
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment