cmake_minimum_required(VERSION 3.0)

#define here my_add_subdir() which is used by the sub-projects
function(my_add_subdir subdir)
	set(full_subdir "${PROJECT_SOURCE_DIR}/${subdir}")
	add_subdirectory(${full_subdir})

	#various ways of organizing these files are possible, by default split them in different sub-groups cmk, hdr, imp, src
	source_group(${subdir}/cmk FILES ${subdir}/CMakeLists.txt)
	source_group(${subdir}/hdr FILES ${my_headers})
	source_group(${subdir}/imp FILES ${my_t_impls})
	source_group(${subdir}/src FILES ${my_sources})

	# but each developer can customize this locally as they see fit, e.g., put all in a single subfolder (below)
	# source_group(${subdir} FILES ${subdir}/CMakeLists.txt ${my_headers} ${my_t_impls} ${my_sources})

	# exclude template implementaiton (T.cc) files from the build
	set_source_files_properties(${my_t_impls} PROPERTIES HEADER_FILE_ONLY TRUE)

	set(HEADERS ${HEADERS} ${subdir}/CMakeLists.txt ${my_headers} ${my_t_impls} PARENT_SCOPE)
	set(SOURCES ${SOURCES} ${my_sources} PARENT_SCOPE)	
endfunction(my_add_subdir)

# this is a verbose version of set, useful to see what is going on when setting compiler & linker flags
macro(my_set_flags flags value)
	message(STATUS "${flags} before: " ${${flags}})
	set(${flags} ${value})
	message(STATUS "${flags} after: " ${${flags}})
	message(STATUS "-------------------------------")
endmacro(my_set_flags)

# we use these settings to manage the structure of the VS solution
# the folders we use for now are "apps", "CMake", "samples"
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_property(GLOBAL PROPERTY PREDEFINED_TARGETS_FOLDER "CMake")

# When it is set to true, then the full compile and link commands are shown
# set(CMAKE_VERBOSE_MAKEFILE true)

# Project configuration
set(CMAKE_CONFIGURATION_TYPES "Debug;Release" CACHE STRING "Configurations" FORCE)

#define the ReForm release version target 
set(VERSION_MAJOR 5)
set(VERSION_MINOR 1)
set(VERSION_PATCH 1)
set(DLL_SUFFIX $ENV{DLL_SUFFIX})
set(VERSION "${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}${DLL_SUFFIX}")
message(STATUS "VERSION: " ${VERSION})

project(MUMPSF Fortran)

  include(compiler_settings.cmake)
  IF(WIN32)
	#### We want to use static libraries on windows.
    string(REPLACE "/libs:dll" "/libs:static"  CMAKE_Fortran_FLAGS  ${CMAKE_Fortran_FLAGS})
    my_set_flags(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} /MP")
  endif(WIN32)

set(TARGET mumpsf)
set(TARGET_OUTPUT_NAME "${TARGET}_${VERSION_MAJOR}${DLL_SUFFIX}")

my_add_subdir(libseq)
my_add_subdir(src)

add_library(${TARGET} STATIC ${SOURCES} ${HEADERS})

project(MUMPS)

include(compiler_settings.cmake)

unset(SOURCES)
unset(HEADERS)

#define the main ReForm library target
set(TARGET "mumps")
set(TARGET_OUTPUT_NAME "${TARGET}_${VERSION_MAJOR}${DLL_SUFFIX}")

my_add_subdir(include)
my_add_subdir(PORD)
my_add_subdir(surcc)

#add_library(${TARGET} STATIC ${SOURCES} ${HEADERS})
add_library(${TARGET} SHARED ${SOURCES} ${HEADERS})

target_link_libraries(${TARGET}
  PRIVATE mumpsf 
  PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/LIB/140/$<CONFIGURATION>/blas.lib"
  PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/ThirdParty/LIB/140/$<CONFIGURATION>/lapack.lib"
  PRIVATE "$ENV{METIS_INSTALL}/lib/Win64/$<CONFIGURATION>/metis.lib"
  )
