#    Makefile for compiling ice sheet model
#    Written by Erik Bernsen / Fons van Hees / Thomas Reerink

#    To execute the make file (in the src/ directory) e.g.:
#     make grice
#     make                          [short cut for the main program]
#     make compare_restart_program  [Notice: without the  .f90  extension]

# To include the correct Makefile.include one has to copy the one of the platforms you are using, like:
#  cp -f  Makefile.gfortran-mac-mp-4.9 Makefile.include    (default)
#  cp -f  Makefile.gfortran-ubuntu     Makefile.include
#  cp -f  Makefile.ifort-lisa          Makefile.include
include Makefile.include

# Paths where object files and modules go. In some of the include files above
# these variables are already specified, since on some platforms (my mac for
# instance) it is difficult to get the compiler to put object files and modules
# in any directory other than the current one. 
# If the OBJ_PATH and MOD_PATH is not specified in the file included above,
# then we can set it to any value we like at this point:
OBJ_PATH ?= object-files
MOD_PATH ?= module-files

# Pattern rules for creating object files from fortran 90 source files
$(OBJ_PATH)/%.o : %.f90
	mkdir -p $(OBJ_PATH) $(MOD_PATH) && $(F90) $(F90FLAGS) -c -o $@  $<

# =====================================================
#       CREATE OBJECT FILES FOR THE SUBROUTINES
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += mod_obj

mod_obj : 
	make -f Makefile.mod1

# Include Makefile for the sources and objects
include Makefile.mod1

# Update list of all sources. This is used later on to create a dependency file automatically. See the "deps" target.
#ALL_SOURCES += $(INVERSE_COUPLING_SUBROUTINES_SOURCES)

# Update list of all objects files. Used to create the final executable
ALL_OBJECTS += $(INVERSE_COUPLING_SUBROUTINES_OBJECTS)
SCH_OBJECTS += $(INVERSE_COUPLING_SUBROUTINES_OBJECTS)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE SCHEMATIC for SH
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sch_obj

sch_obj : 
	cd ../src_schematic ; make -f Makefile.sch

# Include Makefile for the sources and objects
include ../src_schematic/Makefile.sch

# Update list of all objects files.
SCH_OBJECTS += $(SCH_GRICE_PROGRAM_OBJECTS)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE SCHEMATIC for NH
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sch2_obj

sch2_obj : 
	cd ../src_schematic_nh ; make -f Makefile.sch2

# Include Makefile for the sources and objects
include ../src_schematic_nh/Makefile.sch2

# Update list of all objects files.
SCH_OBJECTS += $(SCH2_GRICE_PROGRAM_OBJECTS)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE ANTARCTICA
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += ant_obj

ant_obj : 
	cd ../src_ant4 ; make -f Makefile.ant

# Include Makefile for the sources and objects
include ../src_ant4/Makefile.ant

# Update list of all objects files.
ALL_OBJECTS += $(ANT_GRICE_PROGRAM_OBJECTS)

# Update list of all sources.
#ALL_SOURCES += $(ANT_GRICE_PROGRAM_SOURCES)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE GREENLAND
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += grl_obj

grl_obj : 
	cd ../src_grl4 ; make -f Makefile.grl

# Include Makefile for the sources and objects
include ../src_grl4/Makefile.grl

# Update list of all objects files.
ALL_OBJECTS += $(GRL_GRICE_PROGRAM_OBJECTS)

# Update list of all sources.
#ALL_SOURCES += $(GRL_GRICE_PROGRAM_SOURCES)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE EURASIA
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += eas_obj

eas_obj : 
	cd ../src_eas4 ; make -f Makefile.eas

# Include Makefile for the sources and objects
include ../src_eas4/Makefile.eas

# Update list of all objects files.
ALL_OBJECTS += $(EAS_GRICE_PROGRAM_OBJECTS)

# Update list of all sources.
#ALL_SOURCES += $(EAS_GRICE_PROGRAM_SOURCES)

# =====================================================
#	CREATE OBJECT FILES FOR GRICE NORTH AMERICA
# =====================================================

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += nam_obj

nam_obj : 
	cd ../src_nam4 ; make -f Makefile.nam

# Include Makefile for the sources and objects
include ../src_nam4/Makefile.nam

# Update list of all objects files.
ALL_OBJECTS += $(NAM_GRICE_PROGRAM_OBJECTS)

# Update list of all sources.
#ALL_SOURCES += $(NAM_GRICE_PROGRAM_SOURCES)

# =====================================================
#	CREATE OBJECT FILES FOR SELEN
# =====================================================

#- Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sle_obj

sle_obj : 
	cd ../src_selen ; make -f Makefile.sle

#- Include Makefile for the sources and objects
include ../src_selen/Makefile.sle

#- Update list of all objects files.
ALL_OBJECTS += $(SELEN_PROGRAM_OBJECTS)

#- Update list of all sources.
#ALL_SOURCES += $(SELEN_PROGRAM_SOURCES)

# =====================================================
#	CREATE OBJECT FILES FOR SELEN FOR THE SCHEMATIC EXPERIMENT
# =====================================================

#- Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sle_sch

sle_sch : 
	cd ../src_selen ; make -f Makefile.slesch

#- Include Makefile for the sources and objects
include ../src_selen/Makefile.slesch

#- Update list of all objects files.
SCH_OBJECTS += $(SELEN_SCH_PROGRAM_OBJECTS)

# ===========================================================
#	CREATE OBJECT FILE FOR THE INVERSE_COUPLING_PROGRAM
# ===========================================================

# Fortran 90 source files used for the inverse_coupling_program:
INVERSE_COUPLING_PROGRAM_F90_SOURCES = \
	inverse_coupling_program.f90 

# All object files used for the inverse_coupling_program:
INVERSE_COUPLING_PROGRAM_OBJECTS = $(INVERSE_COUPLING_PROGRAM_F90_SOURCES:%.f90=$(OBJ_PATH)/%.o) 

# Update lists of all sources. This is used later on to create a dependency file automatically. See the "deps" target.
#ALL_SOURCES += $(INVERSE_COUPLING_PROGRAM_F90_SOURCES)

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += inv_obj

inv_obj : $(INVERSE_COUPLING_PROGRAM_OBJECTS)

# ===============================================
#	BUILDING THE INVERSE_COUPLING_PROGRAM
# ===============================================

# Update list of all objects files.
ALL_OBJECTS += $(INVERSE_COUPLING_PROGRAM_OBJECTS)

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += inv_coupl

# The following line tells which object file are needed to build the final executable inv_coupl: 
inv_coupl : $(ALL_OBJECTS)
	$(F90) $(F90FLAGS) -o $@ $^ $(LDFLAGS)	

# ===========================================================
#	CREATE OBJECT FILE FOR THE SCHEMATIC_COUPLING_PROGRAM
# ===========================================================

# Fortran 90 source files used for the inverse_coupling_program:
SCHEMATIC_COUPLING_PROGRAM_F90_SOURCES = \
	schematic_coupling_program.f90 

# All object files used for the inverse_coupling_program:
SCHEMATIC_COUPLING_PROGRAM_OBJECTS = $(SCHEMATIC_COUPLING_PROGRAM_F90_SOURCES:%.f90=$(OBJ_PATH)/%.o) 

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sch_main

sch_main : $(SCHEMATIC_COUPLING_PROGRAM_OBJECTS)

# ===============================================
#	BUILDING THE SCHEMATIC_COUPLING_PROGRAM
# ===============================================

# Update list of all objects files.
SCH_OBJECTS += $(SCHEMATIC_COUPLING_PROGRAM_OBJECTS)

# Update a list of all programs, which is used by the "all" target:
ALL_PROGRAMS += sch_coupl

# The following line tells which object file are needed to build the final executable inv_coupl: 
sch_coupl : $(SCH_OBJECTS)
	$(F90) $(F90FLAGS) -o $@ $^ $(LDFLAGS)	

# =======================================
#       CLEANING UP EVERYTHING
# =======================================

.PHONY : clean

clean : 
	rm -fv $(ALL_PROGRAMS) $(OBJ_PATH)/*.o $(MOD_PATH)/*.mod $(MOD_PATH)/*.MOD *.mod *.lst
        

# ==================================
# 	BUILD EVERYTHING
# ==================================

.PHONY : all

all : $(ALL_PROGRAMS)
