cmake_minimum_required(VERSION 3.0)

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

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

MESSAGE("Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
if(CMAKE_COMPILER_IS_GNUCXX MATCHES 1)
    set(CXX_COMPILER_IS_GNU TRUE)
else()
    set(CXX_COMPILER_IS_GNU FALSE)
endif()

MESSAGE("Current system name is ${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 ()

    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 ()

    ELSEIF (${CMAKE_SYSTEM_NAME} MATCHES "Darwin")

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

        MESSAGE("Current system processor is ${CMAKE_SYSTEM_PROCESSOR}.")
        IF (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm64" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64")
            MESSAGE("Current system arch is arm64")
            SET(TD_DARWIN_64 TRUE)
            ADD_DEFINITIONS("-D_TD_DARWIN_64")
        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 "")
  MESSAGE(STATUS "The current platform " ${CMAKE_SYSTEM_PROCESSOR} " is detected")

  IF (CMAKE_SYSTEM_PROCESSOR MATCHES "(amd64)|(AMD64)")
    MESSAGE(STATUS "The current platform is amd64")
    SET(PLATFORM_ARCH_STR "amd64")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "(x86)|(X86)")
    MESSAGE(STATUS "The current platform is x86")
    SET(PLATFORM_ARCH_STR "i386")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "armv7l")
    MESSAGE(STATUS "The current platform is aarch32")
    SET(PLATFORM_ARCH_STR "arm")
  ELSEIF (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    MESSAGE(STATUS "The current platform is aarch64")
    SET(PLATFORM_ARCH_STR "arm64")
  ENDIF ()
ELSE ()
  # if generate ARM version:
  #   cmake -DCPUTYPE=aarch32 .. or cmake -DCPUTYPE=aarch64
  IF (${CPUTYPE} MATCHES "aarch32")
    SET(PLATFORM_ARCH_STR "arm")
    MESSAGE(STATUS "input cpuType: aarch32")
  ELSEIF (${CPUTYPE} MATCHES "aarch64")
    SET(PLATFORM_ARCH_STR "arm64")
    MESSAGE(STATUS "input cpuType: aarch64")
  ELSEIF (${CPUTYPE} MATCHES "mips64")
    SET(PLATFORM_ARCH_STR "mips")
    MESSAGE(STATUS "input cpuType: mips64")
  ELSEIF (${CPUTYPE} MATCHES "x64")
    SET(PLATFORM_ARCH_STR "amd64")
    MESSAGE(STATUS "input cpuType: x64")
  ELSEIF (${CPUTYPE} MATCHES "x86")
    SET(PLATFORM_ARCH_STR "i386")
    MESSAGE(STATUS "input cpuType: x86")
  ELSE ()
    MESSAGE(STATUS "input cpuType unknown " ${CPUTYPE})
  ENDIF ()
ENDIF ()

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

MESSAGE("C Compiler ID: ${CMAKE_C_COMPILER_ID}")
MESSAGE("CXX Compiler ID: ${CMAKE_CXX_COMPILER_ID}")
