# This file sets up cross-compiling C code for arm-none-eabi (e.g. microcontrollers)

include(buildc)

set(TOOLCHAIN_PREFIX "arm-none-eabi-")
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc)
set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER})
set(CMAKE_OBJCOPY ${TOOLCHAIN_PREFIX}objcopy)
set(CMAKE_SIZE_UTIL ${TOOLCHAIN_PREFIX}size)
set(GDB ${TOOLCHAIN_PREFIX}gdb)

add_compile_options(-Wfloat-conversion)

add_compile_options(-ffunction-sections)
add_compile_options(-fdata-sections)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nostartfiles -Wl,--gc-sections")

# Clear the shared library flags, otherwise cmake appends -rdynamic
set(CMAKE_SHARED_LIBRARY_LINK_C_FLAGS)

set(GDB_INIT ${CMAKE_SOURCE_DIR}/cmake/init.gdb CACHE STRING "GDB script to connect to the target")

find_path(CMAKE_SYSROOT
    NAMES include/string.h include/math.h
    PATHS /usr/arm-none-eabi /usr/lib/arm-none-eabi
    NO_DEFAULT_PATH
    REQUIRED)

if(CMAKE_C_CLANG_TIDY)
    # clang uses the --target argument instead of using a command prefix like "arm-none-eabi-gcc"
    # clang also requires an explicit path to the sysroot
    set(CMAKE_C_CLANG_TIDY "${CMAKE_C_CLANG_TIDY};-extra-arg=--target=arm-none-eabi;-extra-arg=--sysroot=${CMAKE_SYSROOT}")
endif()