set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_C_COMPILER_ID GNU)
set(CMAKE_CXX_COMPILER_ID GNU)
set(CMAKE_C_COMPILER_FORCED TRUE)
set(CMAKE_CXX_COMPILER_FORCED TRUE)

#Стандарт языка C
if(NOT CMAKE_C_STANDARD)
    message(STATUS "CMAKE_C_STANDARD not specified: Use gnu11 by default")
    set(CMAKE_C_STANDARD 11)
endif()

#Стандарт языка С++
if(NOT CMAKE_CXX_STANDARD)
    message(STATUS "CMAKE_CXX_STANDARD not specified: Use gnu++17 by default")
    set(CMAKE_CXX_STANDARD 17)
endif()

set(C_STANDARD_REQUIRED YES)
set(CXX_STANDARD_REQUIRED YES)

#Используем сборку Debug по умолчанию
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Debug")
  message(STATUS "CMAKE_BUILD_TYPE not specified: Use Debug by default")
endif()

if(NOT LINKER_SCRIPT)
  message(FATAL_ERROR "LINKER_SCRIPT not specified. Abort")
endif()

#Используем папку Debug для сборки Debug по умолчанию
if(NOT DEBUG_DIR)
  #set(DEBUG_DIR "Debug")
  message(STATUS "Debug output dir (DEBUG_DIR) not specified: Use default location")
endif()

#Используем папку Release для сборки Release по умолчанию
if(NOT RELEASE_DIR)
  #set(RELEASE_DIR "Release")
  message(STATUS "Release output dir (RELEASE_DIR) not specified: Use default location")
endif()

if (${CMAKE_BUILD_TYPE} MATCHES "Release")
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${RELEASE_DIR})
ELSE ()
    set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/${DEBUG_DIR})
ENDIF()

message(STATUS "Directory for output files: ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")

#
if(NOT TOOLCHAIN_PREFIX)
  set(TOOLCHAIN_PREFIX "C:/gnuARM/ArmGCC/")
  message(STATUS "TOOLCHAIN_PREFIX not specified: Use Debug by default")
endif()

#
if(NOT TARGET_TRIPLET)
  set(TARGET_TRIPLET "arm-none-eabi")
  message(STATUS "TARGET_TRIPLET not specified: Use Debug by default")
endif()

SET(TOOLCHAIN_BIN_DIR "${TOOLCHAIN_PREFIX}bin")
SET(TOOLCHAIN_INC_DIR "${TOOLCHAIN_PREFIX}${TARGET_TRIPLET}/include")
SET(TOOLCHAIN_LIB_DIR "${TOOLCHAIN_PREFIX}${TARGET_TRIPLET}/lib")

IF (WIN32)
    SET(TOOL_EXECUTABLE_SUFFIX ".exe")
ELSE()
    SET(TOOL_EXECUTABLE_SUFFIX "")
ENDIF()

SET(CMAKE_C_COMPILER "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-gcc${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "c compiler")
SET(CMAKE_CXX_COMPILER "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-g++${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "c++ compiler")
SET(CMAKE_ASM_COMPILER "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-gcc${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "asm compiler")
SET(CMAKE_MAKE_PROGRAM "${MAKE_PREFIX}make${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "make tool")

SET(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

SET(CMAKE_OBJCOPY "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-objcopy${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "objcopy tool")
SET(CMAKE_OBJDUMP "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-objdump${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "objdump tool")
SET(CMAKE_SIZE "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-size${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "size tool")
#SET(CMAKE_DEBUGER "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-gdb${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "debuger")
SET(CMAKE_CPPFILT "${TOOLCHAIN_BIN_DIR}/${TARGET_TRIPLET}-c++filt${TOOL_EXECUTABLE_SUFFIX}" CACHE INTERNAL "C++filt")
