# detect the OS
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    set(IME_OS_WINDOWS 1)

    # detect the architecture (note: this test won't work for cross-compilation)
    include(CheckTypeSize)
    check_type_size(void* SIZEOF_VOID_PTR)
    if(${SIZEOF_VOID_PTR} STREQUAL "4")
        set(ARCH_32BITS 1)
    elseif(${SIZEOF_VOID_PTR} STREQUAL "8")
        set(ARCH_64BITS 1)
    else()
        message(FATAL_ERROR "Unsupported architecture")
        return()
    endif()
else()
    message(FATAL_ERROR "Unsupported operating system or environment")
    return()
endif()

# detect the compiler and its version
# Note: on some platforms (OS X), CMAKE_COMPILER_IS_GNUCXX is true
# even when CLANG is used, therefore the Clang test is done first
if(CMAKE_CXX_COMPILER MATCHES ".*clang[+][+]" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
   # CMAKE_CXX_COMPILER_ID is an internal CMake variable subject to change,
   # but there is no other way to detect CLang at the moment
   set(IME_COMPILER_CLANG 1)
   execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE CLANG_VERSION_OUTPUT)
   string(REGEX REPLACE ".*clang version ([0-9]+\\.[0-9]+).*" "\\1" IME_CLANG_VERSION "${CLANG_VERSION_OUTPUT}")
elseif(CMAKE_COMPILER_IS_GNUCXX)
    set(IME_COMPILER_GCC 1)
    execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpversion" OUTPUT_VARIABLE GCC_VERSION_OUTPUT)
    string(REGEX REPLACE "([0-9]+\\.[0-9]+).*" "\\1" IME_GCC_VERSION "${GCC_VERSION_OUTPUT}")
    execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "--version" OUTPUT_VARIABLE GCC_COMPILER_VERSION)
    string(REGEX MATCHALL ".*(tdm[64]*-[1-9]).*" IME_COMPILER_GCC_TDM "${GCC_COMPILER_VERSION}")
    execute_process(COMMAND "${CMAKE_CXX_COMPILER}" "-dumpmachine" OUTPUT_VARIABLE GCC_MACHINE)
    string(STRIP "${GCC_MACHINE}" GCC_MACHINE)
    if(GCC_MACHINE MATCHES ".*w64.*")
        set(IME_COMPILER_GCC_W64 1)
    endif()
elseif(MSVC)
    set(IME_COMPILER_MSVC 1)
    if(MSVC_VERSION EQUAL 1400)
        set(IME_MSVC_VERSION 8)
    elseif(MSVC_VERSION EQUAL 1500)
        set(IME_MSVC_VERSION 9)
    elseif(MSVC_VERSION EQUAL 1600)
        set(IME_MSVC_VERSION 10)
    elseif(MSVC_VERSION EQUAL 1700)
        set(IME_MSVC_VERSION 11)
    elseif(MSVC_VERSION EQUAL 1800)
        set(IME_MSVC_VERSION 12)
    elseif(MSVC_VERSION EQUAL 1900)
        set(IME_MSVC_VERSION 14)
    elseif(MSVC_VERSION GREATER_EQUAL 1910 AND MSVC_VERSION LESS_EQUAL 1916)
        set(IME_MSVC_VERSION 17)
    elseif(MSVC_VERSION GREATER_EQUAL 1920 AND MSVC_VERSION LESS_EQUAL 1928)
        set(IME_MSVC_VERSION 19)
    endif()
else()
    message(FATAL_ERROR "Unsupported compiler")
    return()
endif()

# Set pkgconfig install directory
set(IME_PKGCONFIG_DIR "/${CMAKE_INSTALL_LIBDIR}/pkgconfig")
