set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

# Without that flag CMake is not able to pass test compilation check
if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
    set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
else()
    set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")
endif()

if(WIN32)
    set(ARM_BIN_EXT ".exe")
elseif(LINUX)
    set(ARM_BIN_EXT "")
else()
    get_filename_component(TOOLCHAIN_DIR "cmake"  ABSOLUTE)
    message(FATAL_ERROR "OS not supported. TOOLCHAIN_DIR: ${TOOLCHAIN_DIR}")
endif()

# path to C and CXX
set(ENV{ARM_PREFIX} "arm-none-eabi-" )
get_filename_component(ARMCC_DIR "../../../../tools/gcc"  ABSOLUTE)
message(STATUS "ARMCC_DIR: ${ARMCC_DIR}")
set(TOOLCHAIN_BIN_DIR "${ARMCC_DIR}/bin")


set(CMAKE_C_COMPILER      "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}gcc${ARM_BIN_EXT}")
set(CMAKE_CXX_COMPILER    "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}g++${ARM_BIN_EXT}")
set(CMAKE_ASM_COMPILER    "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}g++${ARM_BIN_EXT}")

set(CMAKE_OBJCOPY         "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}objcopy${ARM_BIN_EXT}")
set(CMAKE_SIZE_UTIL       "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}size$ARM_BIN_EXT}")
set(CMAKE_OBJDUMP         "${TOOLCHAIN_BIN_DIR}/$ENV{ARM_PREFIX}objdump${ARM_BIN_EXT}")

set(CMAKE_SYSROOT "${ARMCC_DIR}/arm-none-eabi")
set(CMAKE_FIND_ROOT_PATH "${ARMCC_DIR}/arm-none-eabi")
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

set(CMAKE_EXECUTABLE_SUFFIX ".elf")

set(CMAKE_CXX_FLAGS_DEBUG_INIT " -g3 -gdwarf-2" CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_DEBUG " -g3 -gdwarf-2" CACHE STRING "")
set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT " -Os -DNDEBUG" CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_MINSIZEREL " -Os -DNDEBUG" CACHE STRING "")
set(CMAKE_CXX_FLAGS_RELEASE_INIT " -O3 -DNDEBUG" CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_RELEASE " -O3 -DNDEBUG" CACHE STRING "")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT " -O2 -g" CACHE INTERNAL "")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO " -O2 -g" CACHE STRING "")