#############################################################################
# @file  Makefile
# @brief Makefile for GNU Make.
#
# Copyright (c) 2012 University of Pennsylvania. All rights reserved.
# See http://www.rad.upenn.edu/sbia/software/license.html or COPYING file.
#
# Contact: SBIA Group <sbia-software at uphs.upenn.edu>
#############################################################################

# ===========================================================================
# settings
# ===========================================================================

#----------------------------------------------------------------------------
# build settings
PROGRAMS := LmarkObjective ForwardSolverDiffusion
LOCDIR   := src/ksp/examples/tutorials/
MANSEC   := KSP

# by default use optimization if not BOPT=g or else specified
BOPT ?= O
# disable all warnings, in particular, "maybe not initialized before use"
# use optimization level 3 if BOPT=O even if PETSc is build with level 1
CFLAGS := -w
ifeq ($(BOPT),O)
CFLAGS += -O3
endif

#----------------------------------------------------------------------------
# installation settings
PREFIX     ?= /usr/local
PREFIX_BIN := $(PREFIX)/bin
# install only already built programs and by default, if no program has been
# built yet, build and install all programs
INSTALLS   := $(filter $(PROGRAMS),$(wildcard *))
ifeq ($(INSTALLS),)
INSTALLS   := $(PROGRAMS)
endif

# ===========================================================================
# default target
# ===========================================================================

#----------------------------------------------------------------------------
# the all target must be specified before the include of bmake/common/base
all: LmarkObjective ForwardSolverDiffusion

# ===========================================================================
# PETSc settings
# ===========================================================================

#----------------------------------------------------------------------------
# include PETSC_DIR/bmake/common/base
ifeq ($(strip $(PETSC_DIR)),)
$(error Environment variable PETSC_DIR not set!)
endif

include $(PETSC_DIR)/bmake/common/base

# ===========================================================================
# targets
# ===========================================================================

# common object files shared among all programs
OBJS := ConservLaw.o quad.o pointer.o rhs.o Jacobian.o
OBJS += matrixfree.o utility.o stsdamg.o CumulateDisplacement.o
OBJS += OriginalMatProp.o OriginalLandmarks.o DeformLandmarks.o
OBJS += penalized_neumann.o penalized_neumann_mat.o
OBJS += Advection.o Diffusion.o Reaction.o Elasticity.o ComputeQuant.o
OBJS += initialize.o InitializeAll.o Auxiliary.o ReadFiles.o

#----------------------------------------------------------------------------
LmarkObjective: main_optimization.o $(OBJS)
	$(CLINKER) -o LmarkObjective main_optimization.o $(OBJS) $(PETSC_LIB) -lm

$(PREFIX_BIN)/LmarkObjective: LmarkObjective

#----------------------------------------------------------------------------
ForwardSolverDiffusion: main_forward_solver.o $(OBJS)
	$(CLINKER) -o ForwardSolverDiffusion main_forward_solver.o $(OBJS) $(PETSC_LIB) -lm

$(PREFIX_BIN)/ForwardSolverDiffusion: ForwardSolverDiffusion

#----------------------------------------------------------------------------
cleanall: clean cleanbin

cleanbin:
	$(RM) $(PROGRAMS)

#----------------------------------------------------------------------------
install: $(addprefix $(PREFIX_BIN)/,$(INSTALLS))
	mkdir -p $(PREFIX_BIN)
	install -s -t $(PREFIX_BIN) $(INSTALLS)

uninstall:
	$(RM) $(foreach FILE,$(PROGRAMS),$(addprefix $(PREFIX_BIN)/, $(FILE)))

#----------------------------------------------------------------------------
# phony's
.PHONY: all clean install
