set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm)

include(CMakeForceCompiler)

find_program(C_COMPILER arm-none-eabi-gcc)
if(NOT C_COMPILER)
	message(FATAL_ERROR "could not find arm-none-eabi-gcc compiler")
endif()
CMAKE_FORCE_C_COMPILER(${C_COMPILER} GNU)

find_program(CXX_COMPILER arm-none-eabi-g++)
if(NOT CXX_COMPILER)
	message(FATAL_ERROR "could not find arm-none-eabi-g++ compiler")
endif()
CMAKE_FORCE_CXX_COMPILER(${CXX_COMPILER} GNU)

set(CMAKE_OBJCOPY arm-none-eabi-objcopy CACHE INTERNAL "objcopy")
set(CMAKE_OBJDUMP arm-none-eabi-objdump CACHE INTERNAL "objdump")
set(CMAKE_AR arm-none-eabi-gcc-ar CACHE INTERNAL "gcc-ar")
set(CMAKE_NM arm-none-eabi-gcc-nm CACHE INTERNAL "gcc-nm")
set(CMAKE_RANLIB arm-none-eabi-gcc-ranlib CACHE INTERNAL "gcc-ranlib")

set(cpu_flags)
if (CHIP_TYPE STREQUAL "cortex-m7")
	set(cpu_flags "${cpu_flags} -mcpu=cortex-m7 -mthumb -mfpu=fpv5-sp-d16 -mfloat-abi=hard")
elseif (CHIP_TYPE STREQUAL "cortex-m4")
	set(cpu_flags "${cpu_flags} -mcpu=cortex-m4 -mthumb -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard")
elseif (CHIP_TYPE STREQUAL "cortex-m3")
	set(cpu_flags "${cpu_flags} -mcpu=cortex-m3 -mthumb -march=armv7-m")
else ()
	message(FATAL_ERROR "CHIP_TYPE: ${CHIP_TYPE} not recognised")
endif()

set(CMAKE_C_FLAGS "${cpu_flags}" CACHE INTERNAL "" FORCE)
set(CMAKE_CXX_FLAGS "${cpu_flags}" CACHE INTERNAL "" FORCE)
set(CMAKE_ASM_FLAGS "${cpu_flags} -D__ASSEMBLY__ " CACHE INTERNAL "" FORCE)
set(CMAKE_EXE_LINKER_FLAGS "${cpu_flags} -flto -nodefaultlibs -nostdlib -Wl,--warn-common,--gc-sections" CACHE INTERNAL "" FORCE)

# where is the target environment 
set(CMAKE_FIND_ROOT_PATH get_file_component(${C_COMPILER} PATH))
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
