# 设定源文件文件夹
aux_source_directory(lib TETGENLIB_SRC)
# 设置编译选项
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

# 以下部分为库的编译
# 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库
# 注意此处不必为目标名称添加lib前缀和相应后缀，cmake会自行添加
add_library(tetlib SHARED ${TETGENLIB_SRC})
# 首先添加静态库的生成命令
add_library(tetlib_static STATIC ${TETGENLIB_SRC})
# 设置编译选项 将源代码编译为动态与静态库
# 设置静态库的输出名称从而获得与动态库名称相同的静态库
set_target_properties(tetlib PROPERTIES OUTPUT_NAME "tetgen")
set_target_properties(tetlib_static PROPERTIES OUTPUT_NAME "tetgen")
# 设置输出目标属性以同时输出动态库与静态库
set_target_properties(tetlib PROPERTIES CLEAN_DIRECT_OUTPUT 1)
set_target_properties(tetlib_static PROPERTIES CLEAN_DIRECT_OUTPUT 1)
# 设置动态库的版本号
set_target_properties(tetlib PROPERTIES VERSION 1.6 SOVERSION 1.6)
# 设置库文件的输出地址
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

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 tetlib DESTINATION lib)
	install(TARGETS tetlib_static DESTINATION lib)
else()
	install(TARGETS tetlib tetlib_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()
# 头文件安装命令
install(FILES lib/tetgen.h DESTINATION include)


# 设置可执行文件的输出地址
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

# 以下为可执行命令行工具的编译
# 设定源文件文件夹
aux_source_directory(tool TETGEN_SRC)
# 添加可执行文件
add_executable(tetgen ${TETGEN_SRC})
# 命令行工具安装命令
install(TARGETS tetgen RUNTIME DESTINATION sbin)

# 以下部分为例子程序的编译
# 添加可执行文件
add_executable(tetcall sample/tetcall.cxx)
# 为安装文件添加动态库的搜索地址 在Windows下并没有什么用 直接忽略
set_target_properties(tetcall PROPERTIES INSTALL_RPATH ${CMAKE_INSTALL_PREFIX}/lib)
# 链接动态库
target_link_libraries(tetcall PUBLIC tetlib)
