################################################################################
#
# Makefile to compile and link C programs with MPI subroutines
#
# Version valid for Linux machines with MPICH
#
# "make" compiles and links the specified main programs and modules,
# using the specified libraries (if any), and produces the executables
#
# "make clean" removes all files generated by "make"
#
# Dependencies on included files are automatically taken care of
#
################################################################################

all: rmxeq mkdep mkxeq
.PHONY: all


# main programs and modules to be compiled

MAIN = laplaceeq
MEASURE=   functions myIO
MODULES =   $(MEASURE)

# search path for modules
MDIRMEAS = ../modules
#directory of your mpi-installation, check this if you try to compile the program.
MPIR_HOME= /usr 
VPATH =  .: $(MDIRMEAS)  
# Logging option (-mpilog or -mpitrace or -mpianim)
LOGOPTION =

# additional include directories
INCPATH =  ../include  $(MPIR_HOME)/include/mpi/ #this may also vary depending on your installation.
LIBS = m
LIBPATH = $(MPIR_HOME)/lib

# scheduling and optimization options
CFLAGS = -Wall -std=c89 -pedantic -O3


############################## do not change ###################################
SHELL=/bin/bash
CC=$(MPIR_HOME)/bin/mpicc #you might have to adapt your mpi-compiler here if you do not use mpicc.
CLINKER=$(CC)

PGMS= $(MAIN) $(MODULES)

-include $(addsuffix .d,$(PGMS)) # The .d files contain all dependencies
		# and are herewith defined for the remainder of the 
		# makefile


# rule to make dependencies

$(addsuffix .d,$(PGMS)): %.d: %.c Makefile
	@ $(GCC) -ansi $< -MM $(addprefix -I,$(INCPATH)) -o $@


# rule to compile source programs

$(addsuffix .o,$(PGMS)): %.o: %.c Makefile
	$(CC) $< -c $(CFLAGS) $(LOGOPTION) $(addprefix -I,$(INCPATH))


# rule to link object files

$(MAIN): %: %.o $(addsuffix .o,$(MODULES)) Makefile
	$(CLINKER)  $< $(addsuffix .o,$(MODULES)) $(CFLAGS) $(LOGOPTION) \
        $(addprefix -L,$(LIBPATH)) $(addprefix -l,$(LIBS)) -o $@


# produce executables

mkxeq: $(MAIN)


# remove old executables

rmxeq:
	@ -rm -f $(MAIN); \
        echo "delete old executables"


# make dependencies

mkdep:  $(addsuffix .d,$(PGMS))
	@ echo "generate tables of dependencies"


# clean directory

clean:
	@ -rm -rf *.d *.o *.alog *.clog *.slog $(MAIN)
.PHONY: clean

################################################################################
