cmake_minimum_required(VERSION 3.15.2)
# 设置项目名称与语言
project(GCTL VERSION 1.0)
# 添加配置配件编写的函数
include(CMakePackageConfigHelpers)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 添加编译选项
option(GCTL_OPENMP "Use the OpenMP library" ON)
option(GCTL_NETCDF "Use the NetCDF library" ON)
option(GCTL_FFTW3 "Use the FFTW3 library" ON)
option(GCTL_EEMD "Use the EEMD library" ON)
option(GCTL_OPENBLAS "Use the Openblas library" OFF)
option(GCTL_EXPRTK "Use the ExprTK library" ON)
option(GCTL_GMT "Use the GMT library" ON)
option(GCTL_CHECK_BOUNDER "Check array's index" OFF)
option(GCTL_CHECK_SIZE "Check array's size" OFF)
# 传递安装地址给编译期宏变量
option(GCTL_INSTALL_PREFIX "Pass the install directory." ON)
set(DIR_VAR ${CMAKE_INSTALL_PREFIX})

message(STATUS "Platform: " ${CMAKE_HOST_SYSTEM_NAME})
message(STATUS "Install prefix: " ${CMAKE_INSTALL_PREFIX})
message(STATUS "Processor: " ${CMAKE_HOST_SYSTEM_PROCESSOR})
message(STATUS "[GCTL] Use the OpenMP library: " ${GCTL_OPENMP})
message(STATUS "[GCTL] Use the NetCDF library: " ${GCTL_NETCDF})
message(STATUS "[GCTL] Use the FFTW3 library: " ${GCTL_FFTW3})
message(STATUS "[GCTL] Use the EEMD library: " ${GCTL_EEMD})
message(STATUS "[GCTL] Use the Openblas library: " ${GCTL_OPENBLAS})
message(STATUS "[GCTL] Use the ExprTK library: " ${GCTL_EXPRTK})
message(STATUS "[GCTL] Use the GMT library: " ${GCTL_GMT})
message(STATUS "[GCTL] Check Bounder: " ${GCTL_CHECK_BOUNDER})
message(STATUS "[GCTL] Check Size: " ${GCTL_CHECK_SIZE})

if(GCTL_FFTW3)
	if(NOT FFTW3_FOUND)
		find_package(FFTW3 REQUIRED)
		message(STATUS "Found FFTW3")
		include_directories(${FFTW3_INCLUDE_DIRS})
	endif()
endif()

if(GCTL_GMT)
	find_package(GMT REQUIRED)
	message(STATUS "Found GMT")
	include_directories(${GMT_INC_DIR})
endif()

if(GCTL_EEMD)
	if(NOT EEMD_FOUND)
		find_package(EEMD REQUIRED)
		message(STATUS "Found EEMD")
		include_directories(${EEMD_INC_DIR})
	endif()
endif()

if(GCTL_OPENBLAS)
	if(NOT OpenBLAS_FOUND)
		find_package(OpenBLAS REQUIRED)
		message(STATUS "Found OpenBLAS")
		include_directories(${OpenBLAS_INCLUDE_DIRS})
	endif()
endif()

if(GCTL_NETCDF)
	find_package(HDF5)

	if(NOT netCDF_FOUND)
		find_package(netCDF REQUIRED)
		include_directories(${netCDF_INCLUDE_DIR})
	endif()

	if(NOT netCDF_CXX_LEGACY_FOUND)
		find_package(netCDF_CXX_LEGACY REQUIRED)
		include_directories(${netCDF_CXX_LEGACY_INC_DIR})
	endif()

	message(STATUS "Found NetCDF")
endif()

if(GCTL_OPENMP)
	if(NOT OpenMP_CXX_FOUND)
		find_package(OpenMP REQUIRED)
		set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
		set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
		include_directories(${OpenMP_CXX_INCLUDE_DIRS})
	endif()
endif()

# 加入一个头文件配置，让cmake对源码进行操作
configure_file(
	"${PROJECT_SOURCE_DIR}/config.h.in"
	"${PROJECT_SOURCE_DIR}/lib/gctl_config.h"
	)

# 添加库源文件地址
add_subdirectory(lib)
add_subdirectory(example)
add_subdirectory(tool)