cmake_minimum_required(VERSION 3.16)

project(transm VERSION 1.4.2 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(config/MSystem.cmake)
set(PROJECT_URL "https://www.sinxmiao.cn/taynpg/transm")
# 将 CMAKE_SYSTEM_PROCESSOR 转换为小写
# string(TOLOWER "${CMAKE_SYSTEM_PROCESSOR}" CMAKE_SYSTEM_PROCESSOR_LOWER)
set(COMPILER_ID ${CMAKE_CXX_COMPILER_ID})
if(MSVC)
add_compile_options(/source-charset:utf-8)
endif()

if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_SYSTEM_NAME MATCHES "Windows")
message(STATUS "add mingw param.")
add_compile_options(-finput-charset=utf-8)
add_compile_options(-fexec-charset=gbk)
add_compile_options(-Wa,-mbig-obj)
set(COMPILER_ID "mingw")
endif()

if(DEFINED USE_BOOST)
message(STATUS "use boost library ${USE_BOOST}")
add_definitions(-DUSE_BOOST)
include(config/MBoost.cmake)
endif()

string(TOLOWER ${COMPILER_ID} COMPILER_ID)
if(DEFINED IOS_ISH)
message(STATUS "INPUT IOS_ISH ${IOS_ISH}")
endif()

if(DEFINED XP_SYSTEM)
message(STATUS "transm use xp defined ${XP_SYSTEM}")
add_definitions(-D_WIN32_WINNT=0x0501)
else()
add_definitions(-D_WIN32_WINNT=0x0601)
endif()

set(CMAKE_DEBUG_POSTFIX "d")
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib/${CMAKE_BUILD_TYPE})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
    ${PROJECT_BINARY_DIR}/bin/${CMAKE_BUILD_TYPE}/
)

add_definitions(-DFMT_HEADER_ONLY)
include_directories(3rd)
include_directories(.)
include_directories(${PROJECT_BINARY_DIR})
add_subdirectory(ofen)
add_subdirectory(net)
add_subdirectory(util)
add_subdirectory(server)
add_subdirectory(client)
add_subdirectory(filecomplete)
add_subdirectory(tinyaes)

if (DEFINED USE_TRANSM_TEST)
message(STATUS "USE USE_TRANSM_TEST ${USE_TRANSM_TEST}")
add_subdirectory(test)
endif()

string(TIMESTAMP VERSION_BUILD_DATE "%Y-%m-%d %H:%M")
execute_process(
    COMMAND git rev-parse --short HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE VERSION_GIT_HASH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
execute_process(
    COMMAND git rev-parse --abbrev-ref HEAD
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
    OUTPUT_VARIABLE VERSION_GIT_BRANCH
    OUTPUT_STRIP_TRAILING_WHITESPACE
)
configure_file(version.h.in version.h)
message(STATUS "${CMAKE_SYSTEM_NAME} build dir:${PROJECT_BINARY_DIR}")
message(STATUS "VERSION_GIT_BRANCH: ${VERSION_GIT_BRANCH}")
message(STATUS "VERSION_GIT_HASH: ${VERSION_GIT_HASH}")

install(TARGETS tsc DESTINATION bin)
install(TARGETS tss DESTINATION bin)

if (DEFINED USE_BOOST)
install(FILES ${MINGW32_DLLS} DESTINATION bin)
endif()
if (DEFINED USE_GUI)
message(STATUS "USE GUI ${USE_GUI}")
#add_subdirectory(gui)
endif()

# ********************************************************** pack infomation
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(ARCH_BIT "64Bit")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(ARCH_BIT "32Bit")
else()
    message(FATAL_ERROR "Unknown target architecture")
endif()
set(CPACK_PACKAGE_NAME "transm")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_PACKAGE_CONTACT "taynpg <taynpg@163.com>")
set(CPACK_PACKAGE_DESCRIPTION "A simple tool that uses a server (tss) as a relay center to indirectly transfer files between two (or more) clients (tsc).")
set(CPACK_PACKAGE_VENDOR "taynpg")
if(WIN32)
set(CPACK_NSIS_MODIFY_PATH ON)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(CPACK_GENERATOR "NSIS64;ZIP")
elseif(CMAKE_SIZEOF_VOID_P EQUAL 4)
    set(CPACK_GENERATOR "NSIS;ZIP")
else()
    message(FATAL_ERROR "Unknown target architecture")
endif()
set(CPACK_NSIS_INSTALL_ROOT "$PROFILE")
set(CPACK_PACKAGE_INSTALL_DIRECTORY "transm")
elseif(UNIX AND NOT APPLE)
    set(CPACK_GENERATOR "DEB;TGZ")
    set(CPACK_DEBIAN_PACKAGE_SECTION "utils")
    set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
    set(CPACK_DEBIAN_PACKAGE_MAINTAINER "taynpg <taynpg@163.com>")
elseif(APPLE)
    set(CPACK_GENERATOR "DragNDrop")
endif()
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-v${PROJECT_VERSION}-${COMPILER_ID}.${SYSTEM_ARCH}.${ARCH_BIT}")
include(CPack)
