include(CheckTypeSize)
CHECK_TYPE_SIZE("void*" TYPE_SIZE_VOID_PTR BUILTIN_TYPES_ONLY LANGUAGE C)

if ("${TYPE_SIZE_VOID_PTR}" STREQUAL "4")
    set(IS_32BIT_SYSTEM TRUE)
    set(IS_64BIT_SYSTEM FALSE)
    set(BUILD_TARGET_ARCH i386)
elseif ("${TYPE_SIZE_VOID_PTR}" STREQUAL "8")
    set(IS_32BIT_SYSTEM FALSE)
    set(IS_64BIT_SYSTEM TRUE)
    set(BUILD_TARGET_ARCH x86_64)
endif()
set(BUILD_OBJCOPY_BINARY_ARCH i386)

string(REPLACE "_" "-" BUILD_TARGET_ARCH_BAR "${BUILD_TARGET_ARCH}")

string(TOLOWER "${CMAKE_SYSTEM_NAME}" TEMP_CMAKE_SYSTEM_NAME_LC)
if ("${TEMP_CMAKE_SYSTEM_NAME_LC}" STREQUAL "windows")
    set(TARGET_IS_WINDOWS TRUE)
    set(TARGET_IS_LINUX FALSE)

    set(BUILD_OBJCOPY_SYMBOL_PREFIX "_")
    set(BUILD_OBJCOPY_TARGET_TYPE pe-${BUILD_TARGET_ARCH_BAR})
elseif("${TEMP_CMAKE_SYSTEM_NAME_LC}" STREQUAL "linux")
    set(TARGET_IS_WINDOWS FALSE)
    set(TARGET_IS_LINUX TRUE)

    set(BUILD_OBJCOPY_SYMBOL_PREFIX "")
    if (IS_32BIT_SYSTEM)
        set(BUILD_OBJCOPY_TARGET_TYPE elf32-${BUILD_TARGET_ARCH_BAR})
    elseif(IS_64BIT_SYSTEM)
        set(BUILD_OBJCOPY_TARGET_TYPE elf64-${BUILD_TARGET_ARCH_BAR})
    endif()
else()
    message(FATAL_ERROR "Not supported target system: [${CMAKE_SYSTEM_NAME}] [${TEMP_CMAKE_SYSTEM_NAME_LC}]")
endif()

if (NOT CMAKE_OBJCOPY_EXECUTABLE)
    set(CMAKE_OBJCOPY_EXECUTABLE objcopy)
endif()
if (NOT CMAKE_STRIP_EXECUTABLE)
    set(CMAKE_STRIP_EXECUTABLE strip)
endif()
