# in subdirectory "build", run
# cmake ../src 
#
# for debuging (of the code:)
# cmake ../src -DCMAKE_BUILD_TYPE=Debug 
#
# for verbosity regarding commands executed, use
# cmake ../src -DCMAKE_VERBOSE_MAKEFILE=ON
#
# to enable UMFPACK from specific directory (e.g. if not installed
# system wide )
# cmake ../src -DSUITESPARSE=${HOME}/local/downloads/src/SuiteSparse/ 
#
# 


string(COMPARE EQUAL "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" bin_is_src)
if( ${bin_is_src} )
  message(FATAL_ERROR 
  "Attempting in source build. \n"
  "This is strongly discouraged, because it mixes the build output "
  "and temporary data with the source code. \n"
  "Go to the build directory, and start the build there:\n"
  "  cd [feins_root]/build \n"
  "  cmake ../src \n"
  "  make \n")
endif( ${bin_is_src} )

# Project name is not mandatory, but you should use it
project(feins C Fortran)

# States that CMake required version must be >= 2.6
cmake_minimum_required(VERSION 2.6)

# Appends the cmake/modules path inside the MAKE_MODULE_PATH variable which stores the
# directories of additional CMake modules (eg MacroOutOfSourceBuild.cmake):
set(CMAKE_MODULE_PATH ${CMAKE_HOME_DIRECTORY}/../CMakeUMFPACKModules ${CMAKE_MODULE_PATH})



#
# Find mandatory BLAS and LAPACK
#
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
SET (LIBS ${LIBS} ${LAPACK_LIBRARIES})

#
# Find optional SuiteSparse
#
IF ( NOT SUITESPARSE STREQUAL OFF)
  FIND_PACKAGE(UFCONFIG)
  FIND_PACKAGE(AMD)
  FIND_PACKAGE(UMFPACK)
ENDIF ( NOT SUITESPARSE STREQUAL OFF)

IF (UMFPACK_FOUND)
	# UFCONFIG and AMD are dependencies
	MESSAGE (STATUS " -> UMFPACK support enabled ")
	SET (INCLUDE_DIR ${INCLUDE_DIR} ${UMPFACK_INCLUDE_DIR} )
	SET (LIBS ${LIBS} ${UMFPACK_LIBRARIES})
	SET (HAVE_UMFPACK TRUE)
ENDIF (UMFPACK_FOUND)



##set(CMAKE_CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR})
##set(PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
##set(feins_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})


#
# Put information on optional libraries and configuration in config.h
#
SET ( INCLUDE_DIR ${INCLUDE_DIR} ${CMAKE_BINARY_DIR}/include )
CONFIGURE_FILE(${CMAKE_HOME_DIRECTORY}/config.h.in 
               ${CMAKE_BINARY_DIR}/include/config.h)


INCLUDE_DIRECTORIES(${INCLUDE_DIR})
INCLUDE_DIRECTORIES(${CMAKE_BINARY_DIR})



#
# Specify sources
#



# Create a variable containing all non-problem specific .c files:
set(FeinsLib_SOURCES assembly.c cubature.c elements.c gen_aux.c
                     lin_solver.c linsolve_umfpack.c mesh.c
                     mesh_deform.c sparse.c) 



# Create variables containing problem specific .c files:
set(FeinsNavsto_SOURCES   navsto_adj.c navsto_aux.c navsto_solver.c
                          navsto_solver_bcontrol.c navstoassem.c
                          stokes_aux.c stokesassem.c) 
set(FeinsLame_SOURCES     lame_adj.c)
set(FeinsConvDiff_SOURCES assem_conv_diff.c)



# triangle/triangle.c needs special treatment, i.e. special FLAGS
set_property(
   SOURCE triangle/triangle.c
   APPEND PROPERTY COMPILE_DEFINITIONS TRILIBRARY 
   )


#
# Create the executable files from sources:
#

# test_assem
add_executable(test_assem test_assem.c
               ${FeinsLib_SOURCES} triangle/triangle.c)
target_link_libraries(test_assem ${LIBS})


# test_convection_diffusion
add_executable(test_convection_diffusion test_convection_diffusion.c 
               ${FeinsLib_SOURCES} ${FeinsConvDiff_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_convection_diffusion ${LIBS})

# test_lame
add_executable(test_lame test_lame.c 
               ${FeinsLib_SOURCES} ${FeinsLame_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_lame ${LIBS})

# test_lame_adj
add_executable(test_lame_adj test_lame_adj.c 
               ${FeinsLib_SOURCES} ${FeinsLame_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_lame_adj ${LIBS})


# test_mesh_deform
add_executable(test_mesh_deform test_mesh_deform.c 
               ${FeinsLib_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_mesh_deform ${LIBS})

# test_navsto_solver
add_executable(test_navsto_solver test_navsto_solver.c 
               ${FeinsLib_SOURCES} ${FeinsNavsto_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_navsto_solver ${LIBS})

# test_stokes2
add_executable(test_stokes2 test_stokes2.c 
               ${FeinsLib_SOURCES} ${FeinsNavsto_SOURCES} 
               triangle/triangle.c )
target_link_libraries(test_stokes2 ${LIBS})


