fortran_dependencies.mk 3.31 KB
Newer Older
Lorenz Huedepohl's avatar
Lorenz Huedepohl committed
1
2
3
4
5
6
# Copyright 2015 Lorenz Hdepohl
#
# This file is part of fdep and licensed under the MIT license
# see the file LICENSE for more information
#

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
7
8
9
_f90_verbose = $(_f90_verbose_$(V))
_f90_verbose_ = $(_f90_verbose_$(AM_DEFAULT_VERBOSITY))
_f90_verbose_0 = @echo "  $1";
10
_f90_targets = $(subst -,_,$(subst .,_,$(PROGRAMS) $(LTLIBRARIES)))
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
11
12
13
14
15
16
17
18
19

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
20
21

# $1 program
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
22
23
24
25
26
27
#
# 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
28
define is_per_target
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$(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
43
$(if $(filter $1,$(PROGRAMS)),o,lo)
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
44
45
46
47
48
49
endef

# $1 source_file
# $2 stem
# $3 program
define module_targets
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
50
51
$(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)
52
	$(call _f90_verbose,F90 USE  [$3] $$<)$(FORTRAN_CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $($p_CPPFLAGS) $(CPPFLAGS) -o /dev/stdout $$< | grep -i -o '^ *use [^ ,!:]*' | tr '[:upper:]' '[:lower:]' | sort -u > $$@
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
53
54
55

$(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)
56
	$(call _f90_verbose,F90 MOD  [$3] $$<)$(FORTRAN_CPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $($p_CPPFLAGS) $(CPPFLAGS) -o /dev/stdout $$< | grep -i -o '^ *module [^!]*' | tr '[:upper:]' '[:lower:]' | grep -v "\<procedure\>" > $$@ || true
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
57
58

endef
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
59
$(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
60

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
61
62
_f90_depdir=$(abs_builddir)/.fortran_dependencies
_f90_depfile = $(_f90_depdir)/dependencies.mk
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
63
64
65
66
67
68
69
70

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
71
72
$(_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
73

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
74
$(_f90_depdir):
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
75
76
	@mkdir $@

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
77
$(foreach p,$(_f90_targets),$(_f90_depdir)/$p): | $(_f90_depdir)
Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
78
79
	@mkdir $@

Lorenz Hüdepohl's avatar
Lorenz Hüdepohl committed
80
81
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
82
CLEANFILES += $(_f90_depfile)