#############################
# STM32H7A3ZI (Cortex-M3) #
#############################

# CMake includes the toolchain file multiple times when configuring the build,
# which causes errors for some flags (e.g., --specs=nano.specs).
# We prevent this with an include guard.
if(STM32H7A3ZI_TOOLCHAIN_INCLUDED)
	return()
endif()

set(STM32H7A3ZI_TOOLCHAIN_INCLUDED true)

#################
# System Config #
#################

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CPU_NAME STM32H7A3ZI)

####################
# Toolchain Config #
####################

set(CMAKE_C_COMPILER    arm-none-eabi-gcc)
set(CMAKE_CXX_COMPILER  arm-none-eabi-g++)
set(AS                  arm-none-eabi-as)
set(CMAKE_AR            arm-none-eabi-gcc-ar)
set(OBJCOPY             arm-none-eabi-objcopy)
set(OBJDUMP             arm-none-eabi-objdump)
set(SIZE                arm-none-eabi-size)

# If set to ONLY, then only the roots in CMAKE_FIND_ROOT_PATH (i.e., the host machine)
# will be searched. If set to NEVER, then the roots in CMAKE_FIND_ROOT_PATH will
# be ignored and only the build machine root will be used.
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_FIND_ROOT_PATH_MODE_PACKAGE ONLY)

#####################
# Flags and Options #
#####################

set(LINKER_SCRIPT_DIR ${CMAKE_SOURCE_DIR}/cmake/linker-scripts/stm)
set(LINKER_SCRIPT_PRIMARY_FILE STM32H7A3ZI_FLASH.ld)

# These dependencies are applied to add_executable targets by the
# AddExecutableWithLinkerScriptDep module
list(APPEND LINKER_SCRIPT_DEPENDENCIES "${LINKER_SCRIPT_DIR}/${LINKER_SCRIPT_PRIMARY_FILE}")

set(CPU_FLAGS "-DSTM32H7A3xxQ -mcpu=cortex-m7 -mthumb -Dflash_layout -DARM_MATH_CM7 -DCORE_CM7")
set(VFP_FLAGS "-mfloat-abi=hard -mfpu=fpv5-d16 ${VFP_FLAGS}")
set(LD_FLAGS "-T${LINKER_SCRIPT_PRIMARY_FILE} -L${LINKER_SCRIPT_DIR} --specs=nano.specs -u _printf_float -u _scanf_float --specs=nosys.specs")

###########################
# CMake Variable Settings #
###########################
# See the CMake Manual for CMAKE_<LANG>_FLAGS_INIT:
#	https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_FLAGS_INIT.html

set(CMAKE_C_FLAGS_INIT
	"${CPU_FLAGS} ${VFP_FLAGS} -fdata-sections -ffunction-sections"
	CACHE
	INTERNAL "Default C compiler flags.")
set(CMAKE_CXX_FLAGS_INIT
	"${CPU_FLAGS} ${VFP_FLAGS} -fdata-sections -ffunction-sections"
	CACHE
	INTERNAL "Default C++ compiler flags.")
set(CMAKE_ASM_FLAGS_INIT
	"${CPU_FLAGS} -x assembler-with-cpp"
	CACHE
	INTERNAL "Default ASM compiler flags.")
set(CMAKE_EXE_LINKER_FLAGS_INIT
	"${LD_FLAGS} -Wl,--gc-sections"
	CACHE
	INTERNAL "Default linker flags.")

######################
# Clang-Tidy Support #
######################

find_program(TOOLCHAIN arm-none-eabi-gcc)
cmake_path(REMOVE_FILENAME TOOLCHAIN)
get_filename_component(TOOLCHAIN_ROOT ${TOOLCHAIN}/.. ABSOLUTE)
set(CLANG_TIDY_C_INCLUDES "${TOOLCHAIN_ROOT}/arm-none-eabi/include/")
file(GLOB_RECURSE CLANG_TIDY_CXX_INCLUDES ${CLANG_TIDY_C_INCLUDES}*/cstring)
cmake_path(GET CLANG_TIDY_CXX_INCLUDES PARENT_PATH CLANG_TIDY_CXX_INCLUDES)
set(CLANG_TIDY_OPTIONAL_ARGS --extra-arg=-isystem${CLANG_TIDY_CXX_INCLUDES} --extra-arg=-isystem${CLANG_TIDY_C_INCLUDES} --extra-arg=-isystem${CLANG_TIDY_CXX_INCLUDES}/arm-none-eabi/thumb/v7+fp/hard/)

###########################
# Override Build Settings #
###########################
set(CMAKE_USER_MAKE_RULES_OVERRIDE "${CMAKE_CURRENT_LIST_DIR}/../OverrideBuildTypeSettings.cmake")
