#*********************************************************************************
#  *Copyright(C): Juntuan.Lu 2021
#  *Author:  Juntuan.Lu
#  *Version: 1.0
#  *Date:  2021/04/22
#  *Phone: 15397182986
#  *Description:
#  *Others:
#  *Function List:
#  *History:
#**********************************************************************************

set(PL_SYS "UNKNOWN")
set(PL_ARCH "UNKNOWN")
set(PL_COMPILER "UNKNOWN")

set(WIN32 FALSE)
set(LINUX FALSE)
set(ANDROID FALSE)
set(DARWIN FALSE)
set(QNX FALSE)
set(INTEL FALSE)
set(FREEBSD FALSE)
set(FREERTOS FALSE)

set(X86 FALSE)
set(X64 FALSE)
set(ARM32 FALSE)
set(ARM64 FALSE)

set(GCC FALSE)
set(MINGW FALSE)
set(MSVC FALSE)
set(CLANG FALSE)
set(QCC FALSE)
set(INTEL FALSE)

macro(CHECK_GUN_X64 GUN_IS_64)
    if(CMAKE_HOST_WIN32)
        set(NULL_DEV "nul")
    else()
        set(NULL_DEV "/dev/null")
    endif()
    execute_process(COMMAND ${CMAKE_C_COMPILER} -E -dM -
        INPUT_FILE ${NULL_DEV}
        OUTPUT_VARIABLE _OUTPUT
        RESULT_VARIABLE _REVAL
        TIMEOUT 3
        )
    if(NOT ${_REVAL} EQUAL 0)
        message("COMMAND : ${CMAKE_C_COMPILER} -posix -E -dM -")
        message(WARNING "Can not check gcc arch")
    else()
        string(FIND ${_OUTPUT} "#define __x86_64" _FIND_STATUS)
        if(${_FIND_STATUS} STRLESS 0)
           set(GUN_IS_64 FALSE)
        else()
           set(GUN_IS_64 TRUE)
        endif()
    endif()
endmacro()

string(TOLOWER ${CMAKE_SYSTEM_NAME} PL_SYS)
if ((${PL_SYS} MATCHES "WIN32") OR (${PL_SYS} MATCHES "windows"))
    set(WIN32 TRUE)
    set(PL_SYS "win32")
elseif (${PL_SYS} MATCHES "linux")
    set(LINUX TRUE)
elseif (${PL_SYS} MATCHES "android")
    set(ANDROID TRUE)
elseif (${PL_SYS} MATCHES "darwin")
    set(DARWIN TRUE)
elseif (${PL_SYS} MATCHES "qnx")
    set(QNX TRUE)
elseif (${PL_SYS} MATCHES "intel")
    set(INTEL TRUE)
elseif (${PL_SYS} MATCHES "freebsd")
    set(FREEBSD TRUE)
elseif (${PL_SYS} MATCHES "freertos")
    set(FREERTOS TRUE)
else()
    message(WARNING "PL_SYS=${PL_SYS}")
endif()

string(TOLOWER ${CMAKE_SYSTEM_PROCESSOR} PL_ARCH)
if((${PL_ARCH} MATCHES "i386") OR (${PL_ARCH} MATCHES "i686"))
    set(X86 TRUE)
    set(PL_ARCH "x86")
elseif((${PL_ARCH} MATCHES "x86_64") OR (${PL_ARCH} MATCHES "amd64"))
    set(X64 TRUE)
    set(PL_ARCH "x64")
elseif((${PL_ARCH} MATCHES "armv7") OR (${PL_ARCH} MATCHES "arm"))
    set(ARM32 TRUE)
    set(PL_ARCH "arm")
elseif((${PL_ARCH} MATCHES "aarch64") OR (${PL_ARCH} MATCHES "arm64"))
    set(ARM64 TRUE)
    set(PL_ARCH "aarch64")
elseif(${PL_ARCH} MATCHES "mips")
    set(MIPS32 TRUE)
elseif(${PL_ARCH} MATCHES "mips64")
    set(MIPS64 TRUE)
else()
    message(WARNING "PL_ARCH=${PL_ARCH}")
endif()

string(TOLOWER ${CMAKE_CXX_COMPILER_ID} PL_COMPILER)
if (${PL_COMPILER} MATCHES "gnu")
    if(WIN32)
        set(MINGW TRUE)
        set(PL_COMPILER "mingw")
    else()
        set(GCC TRUE)
        set(PL_COMPILER "gcc")
    endif()
    CHECK_GUN_X64(GUN_IS_64)
    if(GUN_IS_64)
        set(X86 FALSE)
        set(X64 TRUE)
        set(PL_ARCH "x64")
    else()
        set(X86 TRUE)
        set(X64 FALSE)
        set(PL_ARCH "x86")
    endif()
elseif (${PL_COMPILER} MATCHES "msvc")
    set(MSVC TRUE)
    if(CMAKE_CL_64)
        set(X86 FALSE)
        set(X64 TRUE)
        set(PL_ARCH "x64")
    else()
        set(X86 TRUE)
        set(X64 FALSE)
        set(PL_ARCH "x86")
    endif()
elseif (${PL_COMPILER} MATCHES "clang")
    set(CLANG TRUE)
elseif (${PL_COMPILER} MATCHES "qcc")
    set(QCC TRUE)
elseif (${PL_COMPILER} MATCHES "intel")
    set(INTEL TRUE)
else()
    message(WARNING "PL_COMPILER=${PL_COMPILER}")
endif()
