# HDF5 HL_REGION version 1.1.0 currently under development
# 
# Copyright by The HDF Group.
# All rights reserved.
#
# This file is part of the hl_region High-Level HDF5 APIs.  The full copyright 
# notice, including terms governing use, modification, and redistribution, 
# is contained in the file COPYING, which can be found at the root of the
# source code distribution tree and in the documentation directory (doc/html/).  
# If you do not have access to this file, you may request a copy of 
# "the hl_region High-Level HDF5 APIs copyright and license statement" from 
# help@hdfgroup.org.
# 
############################################################################
#
#  Program:         HL REGION
#
#  Module:          Makefile
#
#  Purpose:         Top-level Makefile to build both C and Fortran interfaces.
#
#  Creation date:   July 24, 2009
#  Modification date:  December 20, 2011 
#
#  Notes: User must set the enviroment variable HDF5_INSTALL_DIR to 
#         the location hdf5 is installed. If --enable-fortran option
#         was used in compiling hdf5 then fortran routines will be 
#         compiled. Fortran compiler should support ISO_C_BINDINGS module.
#
############################################################################

ifdef prefix
 PREFIX=$(prefix)
else
 PREFIX=$(HDF5_INSTALL_DIR)
endif

# check for HDF5 C compiler
ifeq (,$(wildcard $(HDF5_INSTALL_DIR)/bin/h5cc))
   $(error CONFIGURE ERROR: hdf5 C compiler wrapper h5cc not found in $(HDF5_INSTALL_DIR)/bin )
endif
CC = $(HDF5_INSTALL_DIR)/bin/h5cc

ifdef enable-fortran

# check to make sure the fortran hdf5 was built with fortran support
 ifeq (,$(wildcard $(HDF5_INSTALL_DIR)/bin/h5fc))
   $(error "CONFIGURE ERROR: HDF5 library must be built with the --enable-fortran option")
 endif
 FC = $(HDF5_INSTALL_DIR)/bin/h5fc

# check to make sure hdf5 was built with --enable-fortran2003
 CHCK_H5FC2003:= $(strip $(shell $(FC) -showconfig | grep -i 2003))
 ifneq ("$(CHCK_H5FC2003)","Fortran 2003 Compiler: yes")
   $(error "CONFIGURE ERROR: HDF5 library must be built with the --enable-fortran2003 option")
 endif
 FORTRAN=true
endif

all: compile

compile:
	(cd src; $(MAKE))
	(cd examples; $(MAKE))
	(cd test; $(MAKE))
# if fortran enabled then compile FORTRAN source
	if test "$(FORTRAN)" = "true"; then \
	(cd fortran/src; $(MAKE); \
	 cd ../examples; $(MAKE); \
	 cd ../test; $(MAKE));fi

install:
	(mkdir -p $(PREFIX)/lib)
	(mkdir -p $(PREFIX)/include)
	(cp src/libhdf5_hl_region.a $(PREFIX)/lib )
	(cp src/hl_region_H5LRpublic.h $(PREFIX)/include )
	(cp src/hl_region_H5LTpublic.h $(PREFIX)/include )
	(cp src/h5hl_region.h $(PREFIX)/include )
	(cp src/h5hl_api.h $(PREFIX)/include )
# if fortran enabled then compile FORTRAN source
	if test -e fortran/src/h5hl_region.o ; then \
	(cp fortran/src/libhdf5_hl_region_fortran.a $(PREFIX)/lib; \
	 cp fortran/src/*.mod $(PREFIX)/include );fi

tests:
	(cd test; $(MAKE) tests )
	(cd examples; $(MAKE) tests )
# if fortran enabled then compile FORTRAN source
	if test -e fortran/src/h5hl_region.o; then \
	(cd fortran/test; $(MAKE) tests; \
	 cd fortran/examples; $(MAKE) tests );fi

clean:
	(cd src; $(MAKE) clean )
	(cd examples; $(MAKE)  clean )
	(cd test; $(MAKE) clean )
# if fortran enabled then compile FORTRAN source
	if test -e fortran/src/h5hl_region.o; then \
	(cd fortran/src; $(MAKE) clean; \
	 cd ../examples; $(MAKE) clean; \
	 cd ../test; $(MAKE) clean );fi

uninstall:
	(\rm -f $(PREFIX)/lib/libhdf5_hl_region.a)
	(\rm -f $(PREFIX)/include/hl_region_H5LRpublic.h)
	(\rm -f $(PREFIX)/include/hl_region_H5LTpublic.h)
	(\rm -f $(PREFIX)/include/h5hl_region.h)
	(\rm -f $(PREFIX)/include/h5hl_api.h)
	(\rm -f $(PREFIX)/lib/libhdf5_hl_region_fortran.a)
	(\rm -f $(PREFIX)/include/h5hl_region.mod)
