cmake_cpputest_template/stm32-toolchain.cmake

278 lines
8.5 KiB
CMake

# ###############################
# STM32 toolchain file
# ###############################
# ----------------------------------------
# File: stm32-toolchain.cmake
# Description: Cmake file for the stm32 controllers. It has two differnt
# types of kinds things it can output. It can output or build release binaries
# or it output debug builds.
# ----------------------------------------
##################################################
# Configurable Section
##################################################
# Specify the cross-compiler
set(CROSS_TOOLCHAIN arm-none-eabi-)
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(DEBUG_BINARY 1)
set(CMAKE_SYSTEM_VERSION 1)
#-------------------
# MCU Setup
#-------------------
# Specify the exact Chip
#set(MCU STM32F103xx)
# set(MCU STM32F441xx)
# set(MCU STM32F446xx)
set(MCU STM32G0B1CBTx)
set(MCU_DIR inc/${MCU}/)
#set(MCU_SPEC cortex-m3)
#set(MCU_SPEC cortex-m4)
set(MCU_SPEC cortex-m0plus) # Might need to change?
set(FLOAT_SPEC "") # Use when you have no FPU.
# set(FLOAT_SPEC "-mfloat-abi=hard -mfpu=fpv4-sp-d16")
# The Startup(ASM) files
string(TOLOWER startup_${MCU}.s STARTUP_FILE_NAME)
set(STARTUP_FILE ${MCU_DIR}/Source/Templates/gcc/${STARTUP_FILE_NAME})
set(SYSTEM_FILE ${MCU_DIR}/Source/Templates/system_stm32f4xx.c)
# Set the clock speed settings, needed when an external high speed
# oscilator is used.
set(HSE_VAL 8000000)
# Define the linker script location
set(LINKER_SCRIPT linker.ld)
# Programmer and debugging
#set(PROGRAMMER serialupdi)
#set(PORT /dev/ttyU0)
##################################################
# Advanced Section
##################################################
#-------------------
# General Flags
#-------------------
# "-fno-builtin"::
# "-Wall":: Enables all warnings man page line:4385
# "-ffunction-sections":: Puts each function into it's own section
# "-fdata-sections":: Puts each data chunk into it's own section.
# "-fomit-frame-pointer"::
set(OBJECT_GEN_FLAGS " \
-fno-builtin \
-Wall \
-ffunction-sections -fdata-sections \
-fomit-frame-pointer \
" CACHE INTERNAL "Common flags for C/CXX/ASM/Linker")
#-------------------
# CFLAGS
#-------------------
set(CMAKE_C_FLAGS " \
" CACHE INTERNAL "C Compiler options")
#-------------------
# ASMFLAGS for cross
#-------------------
set(CMAKE_ASM_FLAGS " \
-x assembler-with-cpp \
" CACHE INTERNAL "ASM Compiler options")
#-------------------
# LFLAGS for cross
#-------------------
# "-Wl":: Pass option as option to the linker.
# "-Map":: Map file passed to linker(an output?)
# "--gc-sections":: linkage garbage collector, part of shrinking size.
set(CMAKE_EXE_LINKER_FLAGS " \
-Wl,-Map=${PROJECT_NAME}.map \
-Wl,--print-memory-usage \
-Wl,--gc-sections \
" CACHE INTERNAL "Linker options")
#------------------
# Debug Flags
#------------------
# "-Og":: Optimizes for debugging expirence.
# "-g":: Debugging options
# "-gdwarf-3":: Selection of the format version 3
# "-gstrict-dwarf":: Disallow using extensions of later dwarf standards.
set(CMAKE_C_FLAGS_DEBUG "-Og -g -gdwarf-3 -gstrict-dwarf " CACHE INTERNAL "C Compiler options for debug build type")
set(CMAKE_CXX_FLAGS_DEBUG "-Og -g -gdwarf-3 -gstrict-dwarf " CACHE INTERNAL "C++ Compiler options for debug build type")
set(CMAKE_ASM_FLAGS_DEBUG "-Og -g -gdwarf-3 -gstrict-dwarf " CACHE INTERNAL "ASM Compiler options for debug build type")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "" CACHE INTERNAL "Linker options for debug build type")
#------------------
# Release Flags
#-----------------
# "-Os":: Optimizes for space.
# "-flto":: Link Time Optimization
set(CMAKE_C_FLAGS_RELEASE "-Os -flto " CACHE INTERNAL "C Compiler options for release build type")
set(CMAKE_CXX_FLAGS_RELEASE "-Os -flto " CACHE INTERNAL "C++ Compiler options for release build type")
set(CMAKE_ASM_FLAGS_RELEASE "" CACHE INTERNAL "ASM Compiler options for release build type")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "-flto " CACHE INTERNAL "Linker options for release build type")
if(DEBUG_BINARY STREQUAL "1") # -- START: Debug Binary
# Control specific options if crosscompiling
message(STATUS "[INFO] Cross compiling for ${MCU}")
message(STATUS "[INFO] Startup file used is ${STARTUP_FILE}")
# Control ARM Semihosting support
if(NOT SEMIHOSTING)
set(SEMIHOSTING 1)
message(WARNING "[WARN] Semihosting support not specified: Enabling by default")
endif()
#-------------------
# General Flags for cross
#-------------------
# "thumb":: Compressed instructions, 16bits. Using only ARM is an option
# as well.
# "-mcpu":: The selected and in use CPU.
# "-mthumb":: Enable the thumb(16bit) instructions.
# "-mthumb-interwork":: Support calling between ARM and Thumb instructions
# "-mabi":: Generate code for the given application binary interface.
string(APPEND OBJECT_GEN_FLAGS " \
-mcpu=${MCU_SPEC} \
-mthumb \
-mthumb-interwork \
-mabi=aapcs \
${FLOAT_SPEC} \
")
# "aapcs":: Procedure Call Standard for Arm Architecture
#-------------------
# CFLAGS for cross
#-------------------
string(APPEND CMAKE_C_FLAGS " \
${OBJECT_GEN_FLAGS} \
")
#-------------------
# ASMFLAGS for cross
#-------------------
string(APPEND CMAKE_ASM_FLAGS " \
${OBJECT_GEN_FLAGS} \
")
#-------------------
# LFLAGS for cross
#-------------------
string(APPEND CMAKE_EXE_LINKER_FLAGS " \
${OBJECT_GEN_FLAGS} \
")
find_file(LINKER_SCRIPT_PATH
NAMES "${LINKER_SCRIPT}"
PATHS
${CMAKE_CURRENT_LIST_DIR}
)
if(DEFINED LINKER_SCRIPT_PATH)
message(STATUS "[INFO] Using linker file at ${LINKER_SCRIPT_PATH}")
string(APPEND CMAKE_EXE_LINKER_FLAGS "-T${LINKER_SCRIPT_PATH} ")
string(APPEND CMAKE_EXE_LINKER_FLAGS "-L${CMAKE_CURRENT_SOURCE_DIR} ")
else()
message(FATAL_ERROR "[ERRR] Could not find linker script ${LINKER_SCRIPT}")
endif()
# semihosting:: is a mechanism that enables code running on an ARM target
# to communicate and use the Input/Output facilities on a host computer
# that is running a debugger.
if("${SEMIHOSTING}" STREQUAL "1")
#"rdimon":: remote dispatch interface monitor
string(APPEND CMAKE_EXE_LINKER_FLAGS "--specs=rdimon.specs -lc -lrdimon ")
else()
#"nosys":: systems call are stubbed out.
string(APPEND CMAKE_EXE_LINKER_FLAGS "--specs=nosys.specs ")
endif()
else()
# Actual build which will go onto the board
# AKA production build.
######################################################################
# @Target: ${PROJECT_NAME}.elf
# @Brief: Build the actual elf file of the project
######################################################################
#-------------------
# Main elf
#-------------------
# file(GLOB MAIN_SRC
# "src/*.c"
# )
# set(SOURCES
# ${MAIN_SRC}
# ${STARTUP_FILE}
# ${SYSTEM_FILE}
# )
add_executable(${PROJECT_NAME}.elf
${SOURCES}
)
target_include_directories(${PROJECT_NAME}.elf
PUBLIC
${PROJECT_SOURCE_DIR}/include
)
target_link_libraries(${PROJECT_NAME}.elf
PUBLIC
rtos
hal
${MODULES_USED}
)
target_compile_definitions(${PROJECT_NAME}.elf
PUBLIC
${MCU}
HSE_VALUE=${HSE_VAL}
SEMIHOSTING=${SEMIHOSTING}
)
######################################################################
# @Target: flash
# @Brief: flash the release/debug elf using gdb
######################################################################
add_custom_target(flash DEPENDS ${PROJECT_NAME}.elf)
add_custom_command(TARGET flash
#COMMAND bash "-c" "pgrep -x \"openocd\" || (echo \"Please start openocd\" && exit -1)"
COMMAND echo "Starting GDB client and loading ${PROJECT_NAME}.elf dashboard to $(GDB_TTY)"
COMMAND ${CMAKE_C_GDB} ${PROJECT_NAME}.elf
-ex "target extended :3333"
-ex "dashboard -output $(GDB_TTY)"
-ex "load ${PROJECT_NAME}.elf"
-ex "monitor arm semihosting enable"
)
# Add additional files to the make clean
set_property(DIRECTORY PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
"${PROJECT_NAME}.map"
"${CMAKE_CURRENT_BINARY_DIR}/html"
"${CMAKE_CURRENT_BINARY_DIR}/latex"
)
else()
message(FATAL_ERROR "Given TARGET_GROUP unknown")
endif() # -- END: DEBUG/RELEASE Binary
# Unset all cache
unset(SEMIHOSTING)
unset(CMAKE_TOOLCHAIN_FILE)
unset(CMAKE_BUILD_TYPE)