# Makefile for the programs that compute homology gradient vector fields
# and related data structures.
# Prepared by Pawel Pilarczyk. All rights reserved.

# NOTE: Before using this makefile, please, make sure that you
# either provide the correct paths for the CHomP library,
# or have the necessary chomp header files available in the default path
# (e.g. copied locally).

# The following variable automatically detects the Windows environment.
# If its value is empty (i.e., the file CONFIG.SYS doesn't exist)
# then it is assumed that the compilation takes place in Unix/Linux,
# using the GNU C++ compiler.
windows_detect = $(wildcard C:/CONFIG.SYS)

ifeq ($(windows_detect),)
# Configuration for a local installation in Unix/Linux.
# Please, adjust the path names if necessary.
	chomp_dir = $(HOME)/chomp/
	exe =
	extralibs =
	ccflags =
	addincl =
else
# Configuration for an installation in Windows.
# Please, adjust the path names if necessary.
	chomp_dir = /ars/chomp/
	exe = .exe
	extralibs =
	ccflags =
	addincl =
endif

# ---------------------------------------------------

# subdirectories
bin_dir = bin/
obj_dir = obj/

# include paths
incpaths = -I. -I$(chomp_dir)include $(addincl)

# paths to libraries
libpaths = -L$(chomp_dir)lib

# library files
libs = -lchomp $(extralibs)

# options for the C++ compiler (note: no code optimization!)
ccopt = -Wall -ansi -pedantic -frounding-math

# the compilation command
compile = g++ $(ccflags) $(ccopt) -c $(incpaths)

# the linking command
link = g++ $(ccopt) -s $(libpaths)

# the list of programs to compile and link
progcpp = $(wildcard *.cpp)
programs = $(progcpp:%.cpp=%)

# the list of header files whose modification must cause the re-linkage
headers = $(wildcard *.h) $(wildcard chaincon/*.h)

# the default target: all the programs
.phony: all
all: $(programs:%=$(bin_dir)%$(exe))

# linking the programs
$(programs:%=$(bin_dir)%$(exe)): $(bin_dir)%$(exe): $(obj_dir)%.o
	$(link) $< -o $@ $(libs)

# the list of all the object files
objfiles = $(programs)

# compilation of all the object files
$(objfiles:%=$(obj_dir)%.o): $(obj_dir)%.o: %.cpp $(headers) makefile
	$(compile) $< -o $@

