# Cross-Compilation Toolchain for the TM4C123GH6PM microcontroller. This
# toolchain was made following the guidelines of "Professional CMake: A 
# Practical Guide" by Craig Scott. A good toolchain file is as minimal as
# possible.

# Set the system information
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR ARM)
set(CMAKE_SYSTEM_VERSION 1)

# Without that flag CMake is not able to pass test compilation check
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)

# Set the GNU ARM toolchain
set(CMAKE_AR            arm-none-eabi-ar)
set(CMAKE_C_COMPILER    arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER  arm-none-eabi-g++)
set(CMAKE_ASM_COMPILER  arm-none-eabi-as)
set(CMAKE_OBJCOPY       arm-none-eabi-objcopy)
set(CMAKE_OBJDUMP       arm-none-eabi-objdump)
set(CMAKE_C_GDB         gdb-multiarch)

# Setup your compiler flags. The XXX_INIT initializes the cache entry the first 
# time a build tree is configured. That way the user can add or remove their own
# flags without overriding the flags set by this toolchain.
set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m4 -march=armv7e-m -mfpu=fpv4-sp-d16 -mfloat-abi=hard -ffunction-sections -fdata-sections -specs=\"nosys.specs\"")
set(CMAKE_CXX_FLAGS_INIT "${CMAKE_C_FLAGS_INIT} -fno-exceptions -fno-rtti")
set(CMAKE_EXE_LINKER_FLAGS_INIT "-e ResetISR -Wl,-gc-sections,-T\"${CMAKE_CURRENT_LIST_DIR}/tm4c123gh6pm.ld\"")

# Force the toolchain to generate ELF files
set(CMAKE_EXECUTABLE_SUFFIX_C   .elf)
set(CMAKE_EXECUTABLE_SUFFIX_CXX .elf)