#---------------------------------------------------------------------------
# client makefile
#---------------------------------------------------------------------------                     
#                                 ___   __
#                  __    __      / _ \ / /           _/\_
#    ___  _ _     / /__ / /     / / \// /__ ___ _ _ /  _/ 
#   | _ \/ ` \ _ / // // _ \ *  \ \/\/ // // -_/ ' \/ /
#   | ,_/_,_,_\  \_/\_/\___/ *   \__/\_/\_/\__//_/_/\_/
#   |_|                        
#                            Mads M. Hejlesen* and Jens H. Walther*^       
#                            * Technical University of Denmark             
#                            ^ ETH-Zurich                                  
#
#---------------------------------------------------------------------------
# Copyright (c) 2014 Technical University of Denmark, 
#                    Department of Mechanical Engineering
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
# Contact: mmhej@mek.dtu.dk or jhwa@mek.dtu.dk
#---------------------------------------------------------------------------
PROGRAM   = client

SRC_DIR  := ./sources
OBJ_DIR  := ./objects

DEFAULT: $(PROGRAM)

#---------------------------------------------------------------------------
# Path to libraries
#---------------------------------------------------------------------------
PMLIB_DIR    = ./../pmlib

#---------------------------------------------------------------------------
# Flags and libraries for compiling
#---------------------------------------------------------------------------
FC      = gfortran
FFLAGS  = -O5 -ffree-form -ffree-line-length-none

LDFLAGS = -L$(PMLIB_DIR) -lpmlib

INCL    = -I./objects -I$(PMLIB_DIR)/include

#---------------------------------------------------------------------------
# write scripts
#---------------------------------------------------------------------------
SHELL := /bin/sh
LOG   := compile.log
RUN   := $(SHELL) ./make_output.sh

#---------------------------------------------------------------------------
# Setup build directories
#---------------------------------------------------------------------------
NAMES   := $(notdir $(wildcard $(SRC_DIR)/*.f))
SOURCES := $(NAMES:%.f=$(SRC_DIR)/%.f)
OBJECTS := $(NAMES:%.f=$(OBJ_DIR)/%.o)

$(warning Setup directories...)
$(shell test -d $(OBJ_DIR) || mkdir $(OBJ_DIR))
$(shell test -e $(LOG) && rm $(LOG))

# Dont delete the given intermediate files
.SECONDARY:

#---------------------------------------------------------------------------
# Preprocess
#---------------------------------------------------------------------------
CPCMD = cpp $< $@
$(OBJ_DIR)/%.f : $(SRC_DIR)/%.f
	@printf "  CPP  %-42s" $<; \
	$(RUN) "$(CPCMD)" $(LOG) "Preprocessing Error"

#---------------------------------------------------------------------------
# Compile
#---------------------------------------------------------------------------
COMPILECMD = $(FC) $(FFLAGS) $(LDFLAGS) $(INCL) -c -o $@ $<
$(OBJ_DIR)/%.o : $(OBJ_DIR)/%.f
	@printf "  FC   %-42s" $<; \
	$(RUN) "$(COMPILECMD)" $(LOG) "Compile Error"

#---------------------------------------------------------------------------
# Link
#---------------------------------------------------------------------------
#LINKCMD = $(FC) $(OBJECTS) $(LDFLAGS) $(INCL) -o $@
#$(PROGRAM): $(OBJECTS)
#	@printf " LINK  %-42s" $@; \
#	$(RUN) "$(LINKCMD)" $(LOG) "Linking Error"; \
#	mv *.mod $(OBJ_DIR) 2> /dev/null

LINKCMD = $(FC) $(OBJECTS) $(LDFLAGS) $(INCL) -o $@
$(PROGRAM): $(OBJECTS)
	@printf " LINK  %-42s" $@; \
	$(RUN) "$(LINKCMD)" $(LOG) "Linking Error"; \
	$(shell mv *.mod $(OBJ_DIR) 2> $(LOG))

#---------------------------------------------------------------------------
# Explicit dependencies
#---------------------------------------------------------------------------
$(OBJ_DIR)/client.f:\
  $(PMLIB_DIR)/*.a
$(OBJ_DIR)/client.o:\
  $(OBJ_DIR)/client_parameters.o\
  $(OBJ_DIR)/client_input.o\
  $(OBJ_DIR)/client_output.o\
  $(OBJ_DIR)/client_particle_derivatives.o\
  $(OBJ_DIR)/client_penalisation.o\
  $(OBJ_DIR)/client_diagnostics.o
#A
#B
#C
#D
$(OBJ_DIR)/client_diagnostics.o:\
  $(OBJ_DIR)/client_parameters.o
#E
#F
#G
#H
#I
$(OBJ_DIR)/client_input.o:\
  $(OBJ_DIR)/client_parameters.o
#J
#K
#L
#M
#N
#O
$(OBJ_DIR)/client_output.o:\
  $(OBJ_DIR)/client_parameters.o\
  $(OBJ_DIR)/client_penalisation.o
#P
$(OBJ_DIR)/client_parameters.o:
$(OBJ_DIR)/client_penalisation.o:\
  $(OBJ_DIR)/client_parameters.o
#Q
#R
$(OBJ_DIR)/client_particle_derivatives.o:\
  $(OBJ_DIR)/client_parameters.o
#S
#T
#U
#V
#W
#X
#Y
#Z


#---------------------------------------------------------------------------
# Clean
#---------------------------------------------------------------------------
clean:
	rm -fr $(OBJ_DIR)
	rm -f  *.mod 2> $(LOG)
	rm -f  $(PROGRAM)
	rm -f  $(LOG)

