Created
December 26, 2021 07:38
-
-
Save elmot/40f18ef467d5c6d4f544eacc83e29e88 to your computer and use it in GitHub Desktop.
CMake project for STM32L5 with TrustZone enabled
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CMakeLists.txt for dual-core STM32L5xx MCUs and CLion IDE | |
# | |
# DISCLAIMER: Experimental version, based on undocumented assumptions how STM32CubeMX works | |
# DISCLAIMER: THIS FILE IS PROVIDED UNDER "The Unlicense" LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND | |
# | |
# Requirements: | |
# Toolchain binaries have to be in system path | |
# STM32CubeMX field "Project Manager | Code Generator | Target IDE" must be set to "STM32CubeIDE" | |
# | |
# Tested under environment: | |
# Windows 10 | |
# GCC toolchain for ARM Cortex-M V2019q4 | |
# STM32CubeMX V6.4.0 | |
# STM32CubeL5 Firmware Package V1.4.0 | |
# CLion 2021.3.1 | |
# | |
# How To Use: | |
# 1. Set up CLion according to https://www.jetbrains.com/help/clion/embedded-overview.html#build-system | |
# 2. Create a project using STM32CubeMX STM32L5xx MCU with TrustZone enabled, set "Target IDE" as STM32CubeIDE, and click GENERATE CODE | |
# 3. Place this file into the project root folder | |
# 4. Set relevant project name as 'project' clause 1st argument | |
# 5. Open the project root folder as CLion project "File | Open..." | |
# | |
# Written by ilia.motornyi[%]jetbrains.com | |
# | |
cmake_minimum_required(VERSION 3.21) | |
# Cross compilers and tools | |
set(CMAKE_SYSTEM_NAME Generic) | |
set(CMAKE_SYSTEM_VERSION 1) | |
set(CMAKE_C_COMPILER arm-none-eabi-gcc) | |
set(CMAKE_CXX_COMPILER arm-none-eabi-g++) | |
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) | |
set(CMAKE_AR arm-none-eabi-ar) | |
set(CMAKE_OBJCOPY arm-none-eabi-objcopy) | |
set(CMAKE_OBJDUMP arm-none-eabi-objdump) | |
set(SIZE arm-none-eabi-size) | |
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) | |
# Project settings. | |
project(stm32l5-tz-cmake #[[TODO set reasonable project name]] C CXX ASM) | |
# Common compile settings | |
# Language standard | |
set(CMAKE_CXX_STANDARD 17) | |
set(CMAKE_C_STANDARD 11) | |
# Build types | |
if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") | |
message(STATUS "Maximum optimization for speed") | |
add_compile_options(-Ofast) | |
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") | |
message(STATUS "Maximum optimization for speed, debug info included") | |
add_compile_options(-Ofast -g) | |
elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") | |
message(STATUS "Maximum optimization for size") | |
add_compile_options(-Os) | |
else () | |
message(STATUS "Minimal optimization, debug info included") | |
add_compile_definitions(DEBUG) | |
add_compile_options(-Og -g3) | |
endif () | |
# Generic compiler settings | |
add_compile_options(-mcpu=cortex-m33) | |
add_compile_options(-mthumb -mthumb-interwork) | |
add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) | |
add_link_options(-mcpu=cortex-m33) | |
add_link_options(-mthumb -mthumb-interwork) | |
add_link_options(-Wl,-gc-sections,--print-memory-usage) | |
# Enable hardware FPU | |
add_compile_options(-mfloat-abi=hard -mfpu=fpv5-sp-d16) | |
add_link_options(-mfloat-abi=hard -mfpu=fpv5-sp-d16) | |
add_compile_definitions(ARM_MATH_CM35;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) | |
# Shared include | |
include_directories(Secure_nsclib) | |
# Enable assembler files preprocessing | |
add_compile_options($<$<COMPILE_LANGUAGE:ASM>:-x$<SEMICOLON>assembler-with-cpp>) | |
# Uncomment to mitigate c++17 register variable warnings | |
#add_compile_options($<$<COMPILE_LANGUAGE:CXX>:-Wno-register>) | |
# End of common part | |
function(ZoneProject PART_NAME KERNEL_NAME) | |
# Detect linker scripts. Priorities: *_USER.ld, if not found *_FLASH.ld, if not found first *.ld | |
file(GLOB LINKER_SCRIPTS "${PART_NAME}/*_USER.ld") | |
if (NOT LINKER_SCRIPTS) | |
file(GLOB LINKER_SCRIPTS "${PART_NAME}/*_FLASH.ld") | |
if (NOT LINKER_SCRIPTS) | |
file(GLOB LINKER_SCRIPTS "${PART_NAME}/*.ld") | |
endif () | |
endif () | |
list(GET LINKER_SCRIPTS 0 LINKER_SCRIPT_${PART_NAME}) | |
message(STATUS "Detected Linker Script for ${PART_NAME}: ${LINKER_SCRIPT_${PART_NAME}}") | |
# Part-specific sources | |
file(GLOB_RECURSE SRC "${PART_NAME}/*.*" "Drivers/*.*") | |
add_executable(${PROJECT_NAME}_${PART_NAME}.elf ${LINKER_SCRIPT_${PART_NAME}} ${SRC}) | |
target_link_options(${PROJECT_NAME}_${PART_NAME}.elf PRIVATE -Wl,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}_${PART_NAME}.map -T ${LINKER_SCRIPT_${PART_NAME}}) | |
set(HEX_NAME ${PROJECT_NAME}_${PART_NAME}.hex) | |
set(HEX_FILE ${PROJECT_BINARY_DIR}/${HEX_NAME}) | |
# Custom command to build .hex files | |
add_custom_command(TARGET ${PROJECT_NAME}_${PART_NAME}.elf POST_BUILD | |
COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}_${PART_NAME}.elf> ${HEX_FILE} | |
BYPRODUCTS ${HEX_FILE} | |
COMMENT "Building ${HEX_NAME}") | |
# Read kernel-specific header paths, defines, and sources from ".mxproject" | |
file(STRINGS .mxproject LINES) | |
foreach (LINE ${LINES}) | |
if (LINE MATCHES "\\[Cortex${KERNEL_NAME}:(PreviousUsedCubeIDEFiles|PreviousLibFiles)\\]") #Detect relevant group | |
set(CUBE_PRJ_GROUP "${PART_NAME}") | |
elseif (LINE MATCHES "^\\[.*\\]$") #Detect non-relevant groups | |
unset(CUBE_PRJ_GROUP) | |
elseif (CUBE_PRJ_GROUP) | |
if (LINE MATCHES "^\\s*CDefines=\\s*(.*)") #Detect defines | |
target_compile_definitions(${PROJECT_NAME}_${CUBE_PRJ_GROUP}.elf PRIVATE ${CMAKE_MATCH_1}) | |
elseif (LINE MATCHES "^\\s*HeaderPath=\\s*(.*)\\s*") #Detect header paths | |
string(REGEX MATCHALL "[^;]+" INCL_LIST "${CMAKE_MATCH_1}") | |
target_include_directories(${PROJECT_NAME}_${CUBE_PRJ_GROUP}.elf PRIVATE ${INCL_LIST}) | |
elseif (LINE MATCHES "^\\s*LibFiles=\\s*(.*)\\s*") #Detect library sources | |
string(REGEX MATCHALL "[^;]+" SRC_LIST "${CMAKE_MATCH_1}") | |
foreach (SRC_FILE ${SRC_LIST}) | |
if (EXISTS "${CMAKE_SOURCE_DIR}/${SRC_FILE}") | |
target_sources(${PROJECT_NAME}_${CUBE_PRJ_GROUP}.elf PRIVATE ${SRC_FILE}) | |
endif () | |
endforeach () | |
endif () | |
endif () | |
endforeach () | |
endfunction() | |
ZoneProject(Secure M33S) | |
ZoneProject(NonSecure M33NS) | |
add_dependencies(${PROJECT_NAME}_NonSecure.elf ${PROJECT_NAME}_Secure.elf) | |
# Additional setup for Secure part and a shared library | |
target_compile_options(${PROJECT_NAME}_Secure.elf PRIVATE -mcmse) | |
target_link_options(${PROJECT_NAME}_Secure.elf PRIVATE -static -Wl,--cmse-implib -Wl,--out-implib=./secure_nsclib.o -Wl,--start-group -lc -lm -Wl,--end-group) | |
set_source_files_properties(secure_nsclib.o | |
PROPERTIES GENERATED 1 | |
EXTERNAL_OBJECT 1) | |
set_target_properties(${PROJECT_NAME}_Secure.elf PROPERTIES ADDITIONAL_CLEAN_FILES secure_nsclib.o) | |
target_sources(${PROJECT_NAME}_NonSecure.elf PRIVATE $<TARGET_FILE_DIR:${PROJECT_NAME}_Secure.elf>/secure_nsclib.o) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment