# Makefile to build the executable `green-function'

############ FILES

SRC_HPP = global.hpp tensor.hpp state.hpp tier.hpp \
			lib_vector.hpp lib_algorithms.hpp lib_symmat.hpp lib_sparsemat.hpp 

SRC_CPP = tensor.cpp state.cpp tier.cpp eval-Green-func.cpp

# Customized files from Templated Numerical Toolkit (TNT) library, for linear algebra
CUSTOM_TNT = tnt_cmat_modified.h tnt_094_fortran_modified.h

# Files with test configuration, input parameters and expected output data, to test the program
TEST_FILES = test_config.txt test_phi2.txt test_phi3.txt test_output.txt
TEST_FILES := $(addprefix test/, $(TEST_FILES))
#  With `$(addprefix PREFIX,NAMES...)'  the value of PREFIX is prepended in front of each individual name.
#  "test" is the name of the sub-directory with the test filess

SPHINX_CFG = Sphinx.mk conf.py index.rst
# Configuration files to convert the README.rst file to PDF or other formats with Sphinx.

PKG_FILES = README.rst Makefile ${SRC_HPP} ${SRC_CPP} ${CUSTOM_TNT} ${TEST_FILES}
# The source, configuration and test files that are to be added to the zip/gzip archive with the software.

########## BUILD

### Compiling command:

# Includes of directories
CPP_INC = -Itnt_126
# Set this to the path where the TNT files have been deployed

CPP_COMP_BASE = g++ -c -MMD -std=c++11 -pedantic -Wall -Winline ${CPP_INC}
# -MMD: generate dependencies automatically
# -std=c++11: use the 2011 ISO C++ standard instead of default 1998 ISO
# -pedantic: issue all the warnings demanded by strict ISO C and ISO C++; reject all programs that use forbidden extensions
# -Winline: warn if a function that is declared as inline cannot be inlined

# Release mode:
CPP_COMP= ${CPP_COMP_BASE} -O3 -DNDEBUG
# NDEBUG defined in order to ignore "assert" statements

# Debug mode:
# CPP_COMP = ${CPP_COMP_BASE} -g2 -O0 -DGREEN_CHECK -DGREEN_DBG -DCHECK_LIMITS -DSPARSE_CHECK
# Uncomment this definition in order to compile in debug mode, generating an executable with additional 
# checks and debugging information.

# Description of some macros present in source files:
# GREEN_CHECK: time expensive checks
# GREEN_DBG: some more debugging message
# CHECK_LIMITS: TBV
# SPARSE_CHECK: time expensive checks on sparse matrices library
# GREEN_AVOID_OVERT_MAX_CHECK: see state.cpp
# ONLY_BANAL_INVERSION: program is compiled without the use of lapack library (thus only banal inversion is avaible)

### Linking command:

CPP_LINK_BASE = g++

# Release:
 CPP_LINK = ${CPP_LINK_BASE} -O3

# Debug:
#CPP_LINK = ${CPP_LINK_BASE}
# Uncomment this definition in order to compile in debug mode.

### Shared libraries:

#LAPACK_PATH = ${HOME}/lapack/lapack-3.5.0 
# Path to the Lapack library. Change this to the appropriate path, depending on where Lapack library was installed.
# It shouldn't be necessary to define, if the library was installed using the official package of the distribution.
 
#LIBS = -L${LAPACK_PATH} -llapack -lrefblas -lgfortran
# Libraries definition, if Lapack libraries were compiled from sources (tested on Cygwin).

LIBS = -llapack
# Libraries definition, if the official Lapack package provided by the distribution was installed (tested on Ubuntu).

### Building the target:

TARGET = eval-Green-func
# Name of the target executable

OBJS = ${SRC_CPP:.cpp=.o} 

DEPS = ${OBJS:.o=.d}
# Automatically generated dependencies

# Executable:
${TARGET}: ${OBJS} 
	${CPP_LINK} -o $@ ${OBJS} ${LIBS}
# Since this appears as the first dependency in the Makefile, it will be implicitly used as default 
# when invoking  `make' without parameters.

### Compiling the object files:

.SUFFIXES : .o .cpp

.cpp.o:
	${CPP_COMP} $<
# Compilation rule to produce .o from .cpp	

############ UTILS

.PHONY: zippack gzippack clean

README.pdf: README.rst ${SPHINX_CFG}
	make -f Sphinx.mk latexpdf > sphinx.log
	cp _build/latex/$@ .
# Command to generate the PDF README file from the .rst source; the Sphinx 
# tool is required to run this command. To produce the HTML version of the
# README use the command: make -f Sphinx.mk html

VERSION = 1_2_0

ZIPPACK = eval-Green-func-${VERSION}.zip
GZIPPACK = eval-Green-func-${VERSION}.tar.gz

PKG_FILES_WITH_DIR := $(addprefix eval-Green-func/, $(PKG_FILES))
# Prepending the directory name "eval-Green-func", where the source files and this Makefile are expected to be.
SPHINX_CFG_WITH_DIR :=  $(addprefix eval-Green-func/, $(SPHINX_CFG))

zippack: README.pdf
	cd .. && rm -f ${ZIPPACK} && \
	zip ${ZIPPACK} ${PKG_FILES_WITH_DIR} ${SPHINX_CFG_WITH_DIR} eval-Green-func/README.pdf && \
	cd -
# Create a zip archive with the source and test files, the updated version of the README.pdf 
# and the Sphinx configuration files used to produce it. This command is used to make the package 
# to release the software. The source files and this Makefile are expected to be in a directory
# named "eval-Green-func".

gzippack: README.pdf
	cd .. && rm -f ${GZIPPACK} && \
	tar -cf - ${PKG_FILES_WITH_DIR} ${SPHINX_CFG_WITH_DIR} eval-Green-func/README.pdf | gzip -c > ${GZIPPACK} && \
	cd -
# Similar to the previous command, but using gzip instead of zip. You can extract it with the command
# "tar -xvzf FILENAME"

clean:
	rm -f ${TARGET} ${TARGET}.exe ${OBJS} ${DEPS} ${GZIPPACK} ${ZIPPACK} README.pdf sphinx.log 
# Note that ${TARGET}.exe is the name of the executable produced with Cygwin on Windows


############ DEPENDENCIES

-include ${DEPS}
# Including dependency files
