跳轉到主要內容

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
可以搭配 CMake 使用其他編譯器、建置系統 (例如 Make) 及/或偵錯器,但本指南假設使用 STM32CubeCLT 進行設定。

在 STM32CubeMX 中變更工具鏈

首先,將 STM32CubeMX 中「專案管理器」分頁中的工具鏈變更為 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 特定的 GUI 檔案。 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 CMake 列表之後納入,因為 STM32CubeMX CMake 列表中定義的 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 開發板設定為基礎,可以使用已經為 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 Extension 進行專案建置和偵錯。 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 擴充功能或指令列,直接使用 CMake 設定和建置專案。