跳转到主要内容

CMake作为编译系统

简介

From TouchGFX V4.25.0 and STM32CubeMX V6.14.0 onwards, CMake is a supported build system for TouchGFX projects. 此应用场景将演示如何使用CMake编译系统配置、构建和调试TouchGFX项目。

欲进一步了解CMake,请参见官方CMake文档

先决条件

建议下载STM32Cube命令行工具集 (STM32CubeCLT)。 该工具集包含配置、构建和调试STM32CubeMX生成的CMake项目所需的所有工具(编译器、调试器、CMake、Ninja等)。

Note
虽然可使用其他编译器、编译系统(如Make)和/或调试器与CMake配合使用,但本指南默认采用STM32CubeCLT的配置方案。

在STM32CubeMX中切换工具链

首先,在STM32CubeMX的“Project Manager”选项卡中将工具链更改为CMake。 随后生成代码。

选定CMake工具链

在TouchGFX Designer中生成代码

Generate code from TouchGFX Designer by pressing the button or the F4 key on your keyboard, to add the TouchGFX specific code to the project.

生成代码

项目结构

在STM32CubeMX和TouchGFX Designer中生成代码后,将创建一个类似于以下结构的文件目录:

├───<project-name>.ioc
├───CMakeLists.txt # Root CMake list
├───CMakePresets.json # Predefined CMake configurations
├───<mcu-part-number>.ld # Generated linker scripts, e.g., STM32U5g9xx_FLASH.ld
├───cmake
| ├───gcc-arm-none-eabi.cmake # CMake toolchain file
| ├───starm-clang.cmake # CMake toolchain file
│ ├───stm32cubemx
│ │ └───CMakeLists.txt # STM32CubeMX CMake list
│ └───touchgfx
│ └───CMakeLists.txt # TouchGFX CMake list
├───Core
├───Drivers
├───Middlewares
└───TouchGFX

项目根目录下的CMakeLists.txt文件允许用户自定义修改,用于CMake配置入口文件。 CMakePresets.json文件定义项目构建预设配置(如调试、发布等)。

stm32cubemx/CMakeLists.txt列表文件管理STM32CubeMX生成的文件,而touchgfx/CMakeLists.txt文件管理TouchGFX 专用图形用户界面文件。 Therefore, it is important to to re-generate code in TouchGFX Designer when adding/deleting GUI elements and assets to the project to reflect the changes to the build system.

首次通过TouchGFX Designer生成代码时,系统将自动在根目录CMakeLists.txt中添加以下内容,将TouchGFX纳入编译系统:

CMakeLists.txt
...
#添加STM32CubeMX生成的源文件
add_subdirectory(cmake/stm32cubemx)

#添加touchgfx生成的源文件和库
add_subdirectory(cmake/touchgfx)
...

必须确保TouchGFX的CMake列表文件在STM32CubeMX之后引入,因为前者依赖后者定义的MCU专用HAL和驱动配置。

Caution
If TouchGFX Designer fails to correctly include the touchgfx CMake list, a warning message is generated in TouchGFX Designer log. 此时需按照警告提示,手动添加指定的CMake列表文件。

更新链接器脚本

使用STM32CubeMX生成代码后,STM32CubeMX会生成一个或多个兼容arm GCC的链接器脚本。 这些链接器脚本仅生成一次,用户可手动修改以满足项目需求。

如果项目基于TouchGFX Board Setup创建,可参考已为STM32CubeIDE工具链定义的链接器脚本来更新默认脚本。

Alternatively, users can update one of the generated CMake toolchain files gcc-arm-none-eabi.cmake/starm-clang.cmake to point out another linker script to be used by the linker instead of the default one. 例如,使用已定义的STM32CubeIDE链接器脚本:

gcc-arm-none-eabi.cmake
...
set(CMAKE_C_LINK_FLAGS "${TARGET_FLAGS}")
set(CMAKE_C_LINK_FLAGS "${CMAKE_C_LINK_FLAGS} -T \"${CMAKE_SOURCE_DIR}/STM32CubeIDE/STM32U5G9ZJTXQ_FLASH.ld\"")
...

构建项目

生成代码并确保TouchGFX已包含在编译系统中后,即可使用STM32 VS Code扩展来构建和调试项目。 The STM32 VS Code Extension is a useful tool that allow developers to build CMake projects and debug them on target using STM32CubeCLT.

First, the project must be set-up using the STM32 VS Code Extension by selecting Setup STM32Cube project(s):

导入CMake项目

The extension will scan the workspace and determine the selected toolchain and target platform. Various VSCode tasks and launch configurations can be generated by the extension in the VSCode meta data folder .vscode/. These are used to build, clean, flash, and debug the project. 此外,还可直接使用CMake VS Code扩展或命令行配置和构建项目。