fortran_dependencies.mk 3.13 KB
Newer Older
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
1
2
3
_f90_verbose = $(_f90_verbose_$(V))
_f90_verbose_ = $(_f90_verbose_$(AM_DEFAULT_VERBOSITY))
_f90_verbose_0 = @echo "  $1";
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
4
5
6
7
8
9
10
11
12
13
_f90_targets = $(subst .,_,$(bin_PROGRAMS) $(lib_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
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
14
15

# $1 program
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
16
17
18
19
20
21
#
# 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
#
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
22
define is_per_target
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
$(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 $2,$(bin_PROGRAMS)),o,lo)
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
38
39
40
41
42
43
endef

# $1 source_file
# $2 stem
# $3 program
define module_targets
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
44
45
46
47
48
49
50
51
$(info called module_targets($1,$2,$3))
$(eval _$(3)_use_mods += $(dir $1)$(2)$(call strip_fortran_ext,$(notdir $1)).use_mods.$(call object_extension,$3))
$(dir $1)$(2)$(call strip_fortran_ext,$(notdir $1)).use_mods.$(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.$(call object_extension,$3))
$(dir $1)$(2)$(call strip_fortran_ext,$(notdir $1)).def_mods.$(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 [^!]*' > $$@ || true
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
52
53

endef
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
54
$(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)))))
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
55

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
56
57
_f90_depdir=$(abs_builddir)/.fortran_dependencies
_f90_depfile = $(_f90_depdir)/dependencies.mk
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
58
59
60
61
62
63
64
65

define is_clean
$(if $(filter-out mostlyclean clean distclean maintainer-clean,$(MAKECMDGOALS)),0,1)
endef

ifneq ($(call is_clean),1)
include $(_f90_depfile)
endif
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
66
67
$(_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_use_mods) $(_$p_def_mods) >> $@; )
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
68

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
69
$(_f90_depdir):
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
70
71
	@mkdir $@

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
72
$(foreach p,$(_f90_targets),$(_f90_depdir)/$p): | $(_f90_depdir)
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
73
74
	@mkdir $@

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
75
76
CLEANFILES += $(foreach p,$(_f90_targets),$(_$p_def_mods) $(_$p_use_mods))
CLEANFILES += $(foreach p,$(_f90_targets),$(_f90_depdir)/$p/*)
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
77
CLEANFILES += $(_f90_depfile)