cmake_minimum_required(VERSION 3.0)

# init
SET(TD_LINUX FALSE)
SET(TD_WINDOWS FALSE)
SET(TD_DARWIN FALSE)

if(CMAKE_COMPILER_IS_GNUCXX MATCHES 1)
    set(CXX_COMPILER_IS_GNU TRUE)
else()
    set(CXX_COMPILER_IS_GNU FALSE)
endif()

MESSAGE("Current system: ${CMAKE_SYSTEM_NAME}")

IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux" OR ${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

    IF (${CXX_COMPILER_IS_GNU})
        SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=gnu99")
    ELSE ()
        ADD_DEFINITIONS("-Wno-tautological-constant-out-of-range-compare -Wno-pointer-sign -Wno-unknown-warning-option")
        set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS} -undefined dynamic_lookup")
        set(CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_CREATE_CXX_FLAGS} -undefined dynamic_lookup")
    ENDIF ()

    MESSAGE("Current system processor: ${CMAKE_SYSTEM_PROCESSOR}")

    IF (${CMAKE_SYSTEM_NAME} MATCHES "Linux")

        SET(TD_LINUX TRUE)
        SET(OSTYPE "Linux")
        ADD_DEFINITIONS("-DLINUX")

        IF (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
            SET(TD_LINUX_64 TRUE)
        ELSE ()
            SET(TD_LINUX_32 TRUE)
        ENDIF ()

        EXECUTE_PROCESS(COMMAND chmod 777  ${CMAKE_CURRENT_LIST_DIR}/../packaging/tools/get_os.sh)
	EXECUTE_PROCESS(COMMAND readlink /bin/sh OUTPUT_VARIABLE SHELL_LINK)
	MESSAGE(STATUS "The shell is: " ${SHELL_LINK})
	
        IF (${SHELL_LINK} MATCHES "dash")
	    EXECUTE_PROCESS(COMMAND  ${CMAKE_CURRENT_LIST_DIR}/../packaging/tools/get_os.sh "" OUTPUT_VARIABLE TD_OS_INFO)
	ELSE ()
	    EXECUTE_PROCESS(COMMAND sh  ${CMAKE_CURRENT_LIST_DIR}/../packaging/tools/get_os.sh "" OUTPUT_VARIABLE TD_OS_INFO)
	ENDIF ()    
	MESSAGE(STATUS "The current OS is " ${TD_OS_INFO})
	IF (${TD_OS_INFO} MATCHES "Alpine")
	    SET(TD_ALPINE TRUE)
            ADD_DEFINITIONS("-D_ALPINE")
	ENDIF ()

    ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

        SET(TD_DARWIN TRUE)
        SET(OSTYPE "macOS")
        ADD_DEFINITIONS("-DDARWIN -Wno-tautological-pointer-compare")

        IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64")
            MESSAGE("Current system arch is arm64")
            SET(TD_DARWIN_64 TRUE)
            SET(TD_DARWIN_ARM64 TRUE)
            ADD_DEFINITIONS("-D_TD_DARWIN_64")
            ADD_DEFINITIONS("-D_TD_DARWIN_ARM64")
        ENDIF ()
        IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
            MESSAGE("Current system arch is x86_64")
            SET(TD_DARWIN_64 TRUE)
            SET(TD_DARWIN_X64 TRUE)
            ADD_DEFINITIONS("-D_TD_DARWIN_64")
            ADD_DEFINITIONS("-D_TD_DARWIN_X64")
        ENDIF ()

        ADD_DEFINITIONS("-DHAVE_UNISTD_H")
    ENDIF ()

ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Windows")

    SET(TD_WINDOWS TRUE)
    SET(OSTYPE "Windows")
    ADD_DEFINITIONS("-DWINDOWS")

    IF (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
        SET(TD_WINDOWS_64 TRUE)
    	ADD_DEFINITIONS("-D_TD_WINDOWS_64")
    ELSE ()
        SET(TD_WINDOWS_32 TRUE)
    	ADD_DEFINITIONS("-D_TD_WINDOWS_32")
    ENDIF ()

ENDIF()

IF ("${CPUTYPE}" STREQUAL "")
  IF (CMAKE_SYSTEM_PROCESSOR MATCHES "(amd64)|(AMD64)")
    MESSAGE(STATUS "Current platform is amd64")
    SET(PLATFORM_ARCH_STR "amd64")
    SET(TD_INTEL_64 TRUE)
    ADD_DEFINITIONS("-D_TD_X86_")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)")
    MESSAGE(STATUS "Current platform is x86")
    SET(PLATFORM_ARCH_STR "i386")
    SET(TD_INTEL_32 TRUE)
    ADD_DEFINITIONS("-D_TD_X86_")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "armv7l")
    MESSAGE(STATUS "Current platform is aarch32")
    SET(PLATFORM_ARCH_STR "arm")
    SET(TD_ARM_32 TRUE)
    ADD_DEFINITIONS("-D_TD_ARM_")
    ADD_DEFINITIONS("-D_TD_ARM_32")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "(aarch64)|(arm64)")
    MESSAGE(STATUS "Current platform is aarch64")
    SET(PLATFORM_ARCH_STR "arm64")
    SET(TD_ARM_64 TRUE)
    ADD_DEFINITIONS("-D_TD_ARM_")
    ADD_DEFINITIONS("-D_TD_ARM_64")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "loongarch64")
    MESSAGE(STATUS "The current platform is loongarch64")
    SET(PLATFORM_ARCH_STR "loongarch64")
    SET(TD_LOONGARCH_64 TRUE)
    ADD_DEFINITIONS("-D_TD_LOONGARCH_")
    ADD_DEFINITIONS("-D_TD_LOONGARCH_64")
  ENDIF ()
ELSE ()
  # if generate ARM version:
  #   cmake -DCPUTYPE=aarch32 .. or cmake -DCPUTYPE=aarch64
  IF (${CPUTYPE} MATCHES "aarch32" OR ${CPUTYPE} MATCHES "arm32")
    SET(PLATFORM_ARCH_STR "arm")
    MESSAGE(STATUS "input cpuType: aarch32")
    ADD_DEFINITIONS("-D_TD_ARM_")
    ADD_DEFINITIONS("-D_TD_ARM_32")
    SET(TD_ARM_32 TRUE)
  ELSEIF (${CPUTYPE} MATCHES "aarch64" OR ${CPUTYPE} MATCHES "arm64")
    SET(PLATFORM_ARCH_STR "arm64")
    MESSAGE(STATUS "input cpuType: aarch64")
    ADD_DEFINITIONS("-D_TD_ARM_")
    ADD_DEFINITIONS("-D_TD_ARM_64")
    SET(TD_ARM_64 TRUE)
  ELSEIF (${CPUTYPE} MATCHES "loongarch64")
    SET(PLATFORM_ARCH_STR "loongarch64")
    MESSAGE(STATUS "input cpuType: loongarch64")
    SET(TD_LOONGARCH_64 TRUE)
    ADD_DEFINITIONS("-D_TD_LOONGARCH_")
    ADD_DEFINITIONS("-D_TD_LOONGARCH_64")
  ELSEIF (${CPUTYPE} MATCHES "mips64")
    SET(PLATFORM_ARCH_STR "mips")
    MESSAGE(STATUS "input cpuType: mips64")
    SET(TD_MIPS_64 TRUE)
    ADD_DEFINITIONS("-D_TD_MIPS_")
    ADD_DEFINITIONS("-D_TD_MIPS_64")
  ELSEIF (${CPUTYPE} MATCHES "x64")
    SET(PLATFORM_ARCH_STR "amd64")
    MESSAGE(STATUS "input cpuType: x64")
    SET(TD_INTEL_64 TRUE)
  ELSEIF (${CPUTYPE} MATCHES "x86")
    SET(PLATFORM_ARCH_STR "i386")
    MESSAGE(STATUS "input cpuType: x86")
    SET(TD_INTEL_32 TRUE)
  ELSE ()
    MESSAGE(STATUS "input cpuType unknown " ${CPUTYPE})
  ENDIF ()
ENDIF ()

MESSAGE(STATUS "Platform arch:" ${PLATFORM_ARCH_STR})

MESSAGE("C Compiler: ${CMAKE_C_COMPILER} (${CMAKE_C_COMPILER_ID}, ${CMAKE_C_COMPILER_VERSION})")
MESSAGE("CXX Compiler: ${CMAKE_CXX_COMPILER} (${CMAKE_C_COMPILER_ID}, ${CMAKE_CXX_COMPILER_VERSION})")
