#############################################################################
# 
#  Copyright 2021 IRD
#   
#  This file is part of NCSTAT.
#   
#  NCSTAT is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Lesser General Public License as 
#  published by the Free Software Foundation, either version 3 of 
#  the License, or (at your option) any later version.
#   
#  NCSTAT is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU Lesser General Public License for more details.
#   
#  You can find a copy of the GNU Lesser General Public License
#  in the NCSTAT/doc directory.
# 
#############################################################################
#
#############################################################################
#
#  This is the makefile to create the executables in NCSTAT.
#
#  To use this makefile, the following variables
#  must be defined before (for example in ../make.inc):
#
#  EXECDIR : Absolute path to the directory for the NCSTAT executables
#  FC      : Fortran compiler
#  FFLAGS  : Fortran options for compiling the executables
#  FFLAGS2 : Fortran options for compiling the module NCSTAT_Utilities
#  LDFLAGS : Linker load options for the STATPACK, NETCDF and any other
#            external (e.g. BLAS) libraries on your machine.
#
#  To create, update or add executables, enter the command
#
#       make all
#
#  Alternatively, the command
#
#       make
#
#  , without any arguments, creates also the executables.
#
#  To create just one specific executable, enter the command
#
#       make executable_name
#
#  where executable_name is any executable available in NCSTAT.
#
#  The executables are created in the directory $(EXECDIR),
#  where EXECDIR is specified in ../make.inc or directly.
#
#  On some systems, you can force all the source files to be recompiled by
#  entering
#
#       make all FRC=FRC
#
#  To list all the commands available in this version of NCSTAT,
#  enter the command
#
#       make list
#
#  To list all the sources of the executables available in this version
#  of NCSTAT, enter the command
#
#       make list_sources
#
#  To clean the directory, enter the command
#
#       make clean
#
#  or the command
#
#       make clean_all
#
#  Version 2.1
#  February, 2021
#
#############################################################################
#
#
# Remove this line if you dont want to use a ../make.inc include file
#####################################################################
include ../make.inc
#
# Start by clearing the list of suffixes.
#########################################
.SUFFIXES:
#
# We want to specifiy default rules for .F90, .f90, .F95, .f95 and .o
#####################################################################
.SUFFIXES: .F90 .f90 .F95 .f95 .o
#
# Define useful macro abbreviations.
####################################
#
# Macro for the sources directory.
# --------------------------------
DIRSRC = ../sources
#
# Macros for the list of sources files to build the executables.
# --------------------------------------------------------------
SRC =  \
$(shell find $(DIRSRC) -maxdepth 1 -name "*.F90" | sort)
#
SOURCES =  \
$(shell echo "$(SRC)" | \
	sed -e "s+$(DIRSRC)/++g")
#
# Macros for the list of executable files.
# ----------------------------------------
BINARIES =  \
$(shell echo "$(SRC)" | \
	sed -e "s+$(DIRSRC)+$(EXECDIR)+g" \
	    -e "s+\.F90++g")
#
# Macro for the list of object files to build the executables.
# ------------------------------------------------------------
OBJS = NCSTAT_Utilities.o
#
### TARGETS ###
###############
#
all: $(BINARIES)
#
# A trick to force the source files to be recompiled.
# ---------------------------------------------------
$(BINARIES): $(FRC)
#
FRC:
	-rm  -f $(OBJS) 2>/dev/null
	@FRC=$(FRC)
#
# Target line to list the commands and the sources.
# -------------------------------------------------
list:
	@echo $(SOURCES:.F90=  )
#
list_sources:
	@ls $(SOURCES)
#
# Target lines to clean directory.
# --------------------------------
.PHONY: clean
clean:
	-rm  -f  $(SOURCES:.F90=.o  ) *~ core* 2>/dev/null
#
clean_all:
	-rm  -f  *.o  *.mod    *~ core* 2>/dev/null
#
### MAKEFILE COMPILING RULES ###
################################
#
# Dependency lines to make object files for a version of the archive library.
# ---------------------------------------------------------------------------
$(BINARIES): $(OBJS)
#
# Default rules to generate executables from .F90 and .F95 files.
# ---------------------------------------------------------------
%: $(EXECDIR)/%
	$(FC) -o $(EXECDIR)/$@ $(FFLAGS)  $@.F90 $(OBJS) $(LDFLAGS)
#
$(EXECDIR)/%: %.F90
	$(FC) -o $@ $(FFLAGS)  $<  $(OBJS) $(LDFLAGS)
#
$(EXECDIR)/%: %.F95
	$(FC) -o $@ $(FFLAGS)  $<  $(OBJS) $(LDFLAGS)
#
# Default rules to generate .o files from .F90 and .F95 files.
# ------------------------------------------------------------
.F90.o:
	$(FC) -c $(FFLAGS) $<
#
.F95.o:
	$(FC) -c $(FFLAGS) $<
# Default rules to generate .o files from .f90 and .f95 files.
# ------------------------------------------------------------
.f90.o:
	$(FC) -c $(FFLAGS2) $<
#
.f95.o:
	$(FC) -c $(FFLAGS2) $<
