# 设置库文件的输出地址
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
# 设定库源文件文件夹
aux_source_directory(potential/ GCTL_POTENTIAL_SRC)
# 设置编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

if(NOT GCTL_POTENTIAL_TESS)
	list(REMOVE_ITEM GCTL_POTENTIAL_SRC "./gkernel_tesseroid.cpp")
endif()

if(NOT GCTL_POTENTIAL_MAGTESS)
	list(REMOVE_ITEM GCTL_POTENTIAL_SRC "./mkernel_tesseroid.cpp")
endif()

# 以下部分为库的编译
# 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库
# 注意此处不必为目标名称添加lib前缀和相应后缀，cmake会自行添加
add_library(gctl_potential SHARED ${GCTL_POTENTIAL_SRC})
# 首先添加静态库的生成命令
add_library(gctl_potential_static STATIC ${GCTL_POTENTIAL_SRC})
# 设置静态库的输出名称从而获得与动态库名称相同的静态库
set_target_properties(gctl_potential_static PROPERTIES OUTPUT_NAME "gctl_potential")
# 设置输出目标属性以同时输出动态库与静态库
set_target_properties(gctl_potential PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(gctl_potential_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# 设置动态库的版本号
set_target_properties(gctl_potential PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
# 设置动态库的运行搜索地址
set_target_properties(gctl_potential PROPERTIES INSTALL_RPATH /usr/local/lib)
set_target_properties(gctl_potential_static PROPERTIES INSTALL_RPATH /usr/local/lib)

set_target_properties(gctl_potential PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)
set_target_properties(gctl_potential_static PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON CXX_EXTENSIONS OFF)

#连接动态库
target_link_libraries(gctl_potential PUBLIC ${GCTL_LIB})
target_link_libraries(gctl_potential_static ${GCTL_LIB})

target_link_libraries(gctl_potential PUBLIC ${GCTL_MESH_LIB})
target_link_libraries(gctl_potential_static ${GCTL_MESH_LIB})

target_link_libraries(gctl_potential PUBLIC ${GCTL_GRAPHIC_LIB})
target_link_libraries(gctl_potential_static ${GCTL_GRAPHIC_LIB})

target_link_libraries(gctl_potential PUBLIC ${GCTL_OPTIMIZATION_LIB})
target_link_libraries(gctl_potential_static ${GCTL_OPTIMIZATION_LIB})

#if(GCTL_GRAPHIC_MATHGL)
#	target_link_libraries(gctl_potential PUBLIC ${MathGL2_LIBRARIES} ${MathGL2_FLTK_LIBRARIES})
#	target_link_libraries(gctl_potential_static ${MathGL2_LIBRARIES} ${MathGL2_FLTK_LIBRARIES})
#endif()

if(GCTL_POTENTIAL_FFTW3)
	target_link_libraries(gctl_potential PUBLIC ${FFTW3_LIBRARIES})
	target_link_libraries(gctl_potential_static ${FFTW3_LIBRARIES})
endif()

if(GCTL_POTENTIAL_TESS)
	target_link_libraries(gctl_potential PUBLIC ${LibTess_LIB})
	target_link_libraries(gctl_potential_static ${LibTess_LIB})
endif()

if(GCTL_POTENTIAL_MAGTESS)
	target_link_libraries(gctl_potential PUBLIC ${LibMagTess_LIB})
	target_link_libraries(gctl_potential_static ${LibMagTess_LIB})
endif()

if(GCTL_POTENTIAL_OPENMP)
	target_link_libraries(gctl_potential PUBLIC OpenMP::OpenMP_CXX)
	target_link_libraries(gctl_potential_static OpenMP::OpenMP_CXX)
endif()

if(GCTL_POTENTIAL_AUTODIFF)
	target_link_libraries(gctl_potential PUBLIC autodiff::autodiff Eigen3::Eigen)
	target_link_libraries(gctl_potential_static autodiff::autodiff Eigen3::Eigen)
endif()

set(CONFIG_FILE_PATH lib/cmake/${PROJECT_NAME})

configure_package_config_file(${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in 
	${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
	INSTALL_DESTINATION ${CONFIG_FILE_PATH})

write_basic_package_version_file(${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
	VERSION ${PROJECT_VERSION}
	COMPATIBILITY SameMajorVersion)

# 库的安装命令
if(WIN32)
	install(TARGETS gctl_potential DESTINATION lib)
	install(TARGETS gctl_potential_static DESTINATION lib)
else()
	install(TARGETS gctl_potential gctl_potential_static
		EXPORT ${PROJECT_NAME}Targets
		LIBRARY DESTINATION lib
		ARCHIVE DESTINATION lib)
	install(EXPORT ${PROJECT_NAME}Targets
		DESTINATION ${CONFIG_FILE_PATH})
	install(FILES
		${CMAKE_BINARY_DIR}/${PROJECT_NAME}Config.cmake
		${CMAKE_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
		DESTINATION ${CONFIG_FILE_PATH})
endif()

# 头文件安装命令
file(GLOB GCTL_HEAD *.h)
file(GLOB GCTL_POTENTIAL_HEAD potential/*.h)

install(FILES ${GCTL_HEAD} DESTINATION include/gctl)
install(FILES ${GCTL_POTENTIAL_HEAD} DESTINATION include/gctl/potential)