set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Windows")
	# set OpenBLAS directory manually
	set(OpenBLAS_DIR D:/Library/lib/cmake/openblas)
	find_library(OpenBLAS REQUIRED)
	include_directories(${OpenBLAS_INCLUDE_DIRS})
elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Darwin")
	# set OpenBLAS directory manually
	set(OpenBLAS_DIR /opt/homebrew/Cellar/openblas/0.3.24/lib/cmake/openblas)
	find_package(OpenBLAS REQUIRED)
	include_directories(${OpenBLAS_INCLUDE_DIRS})
elseif(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
	set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -lm")
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lm")
endif()

# 设置库文件的输出地址
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# 设定库源文件
aux_source_directory(. LIBMAGTESS_SRC)

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

target_link_libraries(magtess PUBLIC ${OpenBLAS_LIBRARIES})
target_link_libraries(magtess_static ${OpenBLAS_LIBRARIES})
if(${CMAKE_HOST_SYSTEM_NAME} STREQUAL "Linux")
	target_link_libraries(magtess PUBLIC m)
	target_link_libraries(magtess PUBLIC openblas)
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}
	NO_CHECK_REQUIRED_COMPONENTS_MACRO)

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

# 库的安装命令
if(WIN32)
	install(TARGETS magtess DESTINATION lib)
	install(TARGETS magtess_static DESTINATION lib)
else()
	install(TARGETS magtess magtess_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 LIBMEGTESS_HEAD *.h)
install(FILES ${LIBMEGTESS_HEAD} DESTINATION include/magtess)
