#
# Copyright by The HDF Group.
# Copyright by the Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of HDF5.  The full HDF5 copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
##
## Makefile.am
## Run automake to generate a Makefile.in from this file.
#
# HDF5 Fortran Library Makefile(.in)
#

include $(top_srcdir)/config/commence.am
include $(top_srcdir)/config/lt_vers.am

# Include src directory in both Fortran and C flags (C compiler is used
# for linking).
AM_CPPFLAGS+=-I$(top_srcdir)/src
AM_FCFLAGS+=-I$(top_srcdir)/src -I$(top_srcdir)/fortran/src

AM_FCLIBS=$(LIBHDF5)

# This is our main target, the fortran library
lib_LTLIBRARIES=libhdf5_fortran.la

# Add libtool numbers to the HDF5 Fortran library (from config/lt_vers.am)
libhdf5_fortran_la_LDFLAGS= -version-info $(LT_F_VERS_INTERFACE):$(LT_F_VERS_REVISION):$(LT_F_VERS_AGE) $(AM_LDFLAGS)

# Some Fortran compilers can't build shared libraries, so sometimes we
# want to build a shared C library and a static Fortran library.  If so,
# pass the -static flag to the library linker.
if FORTRAN_SHARED_CONDITIONAL
else
   AM_LDFLAGS+=-static
endif

# Source files for the library.
libhdf5_fortran_la_SOURCES=H5f90global.F90 \
          H5fortran_types.F90 H5_ff.F90 H5Aff.F90 H5Dff.F90 H5Eff.F90    \
          H5Fff.F90 H5Gff.F90 H5Iff.F90 H5Lff.F90 H5Off.F90 H5Pff.F90 H5Rff.F90 H5Sff.F90    \
          H5Tff.F90 H5Zff.F90 H5_gen.F90 H5fortkit.F90 \
          H5f90kit.c H5_f.c H5Af.c H5Df.c H5Ef.c H5Ff.c H5Gf.c           \
          H5If.c H5Lf.c H5Of.c H5Pf.c H5Rf.c H5Sf.c H5Tf.c H5Zf.c HDF5.F90

# HDF5 Fortran library depends on HDF5 Library. 
libhdf5_fortran_la_LIBADD=$(LIBHDF5)

# h5fc is generated during configure.
# Remove it only when distclean.
DISTCLEANFILES=h5fc

# H5fortran_types.F90 and H5f90i.h are automatically generated by
# H5match_types, and must be cleaned explicitly.
MOSTLYCLEANFILES=H5fortran_types.F90 H5f90i_gen.h H5_gen.F90

# Fortran module files can have different extensions and different names
# (e.g., different capitalizations) on different platforms.  Write rules
# for them explicitly rather than trying to teach automake about them.
# They should be installed as headers and removed during clean.
maintainer-clean-local: clean-local
distclean-local: clean-local
clean-local:
	@if test -n "$(F9XMODEXT)" && test "X$(F9XMODEXT)" != "Xo"; then \
	  $(RM) *.$(F9XMODEXT);                                         \
	fi

install-data-local:
	@if test -n "$(F9XMODEXT)" && test "X$(F9XMODEXT)" != "Xo"; then \
	  $(CP) $(top_builddir)/$(subdir)/*.$(F9XMODEXT) $(DESTDIR)$(includedir)/. ; \
	fi
	$(CP) $(top_builddir)/$(subdir)/H5f90i_gen.h $(DESTDIR)$(includedir)/.
	$(CP) $(top_srcdir)/fortran/src/H5f90i.h $(DESTDIR)$(includedir)/.

uninstall-local:
	@if test -n "$(F9XMODEXT)" -a "X$(F9XMODEXT)" != "Xo"; then      \
	  if test -f "$(includedir)/hdf5.$(F9XMODEXT)" -o -f "$(includedir)/HDF5.$(F9XMODEXT)"; then \
	    set -x; $(RM) $(DESTDIR)$(includedir)/*.$(F9XMODEXT);                 \
	  fi;                                                           \
	fi;								\
	$(RM) $(DESTDIR)$(bindir)/$(H5FC_NAME)
	$(RM) $(DESTDIR)$(includedir)/H5f90i*.h

# Also install and uninstall (uninstall-local above) h5fc script
install-exec-local:
	@$(INSTALL) h5fc $(DESTDIR)$(bindir)/$(H5FC_NAME)

# Custom rule for installing h5fc, since it will be named h5pfc if hdf5
# is configured for parallel mode
if BUILD_PARALLEL_CONDITIONAL
  H5FC_NAME = h5pfc
else
  H5FC_NAME = h5fc
endif

# Rules for automatically detecting which fortran types are present
# and matching them to C types.  This is accomplished using helper
# programs.
# It's a bit tricky to make sure that Automake builds things in the right
# order (especially when using 'gmake -j ...')
# H5_buildiface is compiled and run to produce H5_gen.F90.
# H5match_types.c then includes H5fort_type_defines.h and can be compiled into
# H5match_types.  When H5match_types is run, it creates H5f90i_gen.h
# and H5fortran_types.F90, which are included in the Fortran library.

# These are the helper programs we need to build.
noinst_PROGRAMS = H5match_types H5_buildiface

# Tell Automake to create H5f90i_gen.h before it builds the library
# sources.  When it creates H5f90i_gen.h, it will create
# H5fortran_types.F90 as a side effect.
BUILT_SOURCES = H5f90i_gen.h

#Specify what Automake needs to create: first the H5fort_type_defines.h
# header, then H5match_types which includes that header, then
# it needs to run H5match_types.
H5fortran_types.F90 H5f90i_gen.h: H5match_types$(EXEEXT)
	$(RUNSERIAL) ./H5match_types$(EXEEXT)

# H5_buildiface.F90 generates all the APIs that have a KIND type associated
# with them.

H5_gen.F90: H5_buildiface$(EXEEXT)
	$(RUNSERIAL) ./H5_buildiface$(EXEEXT)

# H5_buildiface.F90 is included in the distribution, and Automake knows
# how to compile a fortran program given its sources.

H5_buildiface_SOURCES = H5_buildiface.F90

# Mark this directory as part of the Fortran API
FORTRAN_API=yes

# Hardcode the dependencies of these files. There isn't a known way of
# determining this automagically (like we do with the C files). So, when
# doing a parallel make, some modules could be made way before the
# modules they depend upon are actually made. *sigh*
H5f90global.lo:      $(srcdir)/H5f90global.F90 H5fortran_types.lo
H5_buildiface.lo:    $(srcdir)/H5_buildiface.F90
H5_ff.lo:            $(srcdir)/H5_ff.F90 H5f90global.lo
H5Aff.lo:            $(srcdir)/H5Aff.F90 H5f90global.lo
H5Dff.lo:            $(srcdir)/H5Dff.F90 H5f90global.lo
H5Eff.lo:            $(srcdir)/H5Eff.F90 H5f90global.lo
H5Fff.lo:            $(srcdir)/H5Fff.F90 H5f90global.lo
H5Gff.lo:            $(srcdir)/H5Gff.F90 H5f90global.lo
H5Iff.lo:            $(srcdir)/H5Iff.F90 H5f90global.lo
H5Lff.lo:            $(srcdir)/H5Lff.F90 H5f90global.lo
H5Off.lo:            $(srcdir)/H5Off.F90 H5f90global.lo
H5Pff.lo:            $(srcdir)/H5Pff.F90 H5f90global.lo H5fortkit.lo
H5Rff.lo:            $(srcdir)/H5Rff.F90 H5f90global.lo
H5Sff.lo:            $(srcdir)/H5Sff.F90 H5f90global.lo
H5Tff.lo:            $(srcdir)/H5Tff.F90 H5f90global.lo
H5Zff.lo:            $(srcdir)/H5Zff.F90 H5f90global.lo
H5_gen.lo:           H5_gen.F90 H5f90global.lo H5Aff.lo H5Dff.lo H5Pff.lo
HDF5.lo:             $(srcdir)/HDF5.F90 H5f90global.lo H5_ff.lo H5Aff.lo  \
                     H5Dff.lo \
                     H5Eff.lo \
		     H5Fff.lo H5Gff.lo H5Iff.lo H5Lff.lo \
	             H5Off.lo H5Pff.lo H5Rff.lo \
                     H5Sff.lo H5Tff.lo H5Zff.lo H5_gen.lo

include $(top_srcdir)/config/conclude_fc.am