SDK CMake Quick Start
This guide teaches you how to write CMakeLists.txt for HPM SDK applications in 5 minutes. For detailed explanations, see SDK CMake User Guide. For the complete API reference, see SDK CMake API Reference.
Minimal Template
Below is the minimal CMakeLists.txt for an HPM SDK application. Copy and use it directly:
# Copyright (c) 2026 HPMicro
# SPDX-License-Identifier: BSD-3-Clause
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(my_app)
sdk_app_src(src/main.c)
generate_ide_projects()
Step by Step
cmake_minimum_required(VERSION 3.13)
Specifies the minimum CMake version. HPM SDK requires 3.13 or higher.
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
This is the most critical line. It loads the entire SDK build system, including:
Toolchain configuration (RISC-V GCC / Andes NDS / ZCC)
Board selection (specified via
-DBOARD=<name>on the command line)Build type configuration (
-DHPM_BUILD_TYPE=<type>, see SDK CMake User Guide)SDK core library (drivers, components, middleware)
Set the HPM_SDK_BASE environment variable before running cmake:
source hpm_sdk/env.sh # Automatically sets HPM_SDK_BASE
Important
All SDK configuration variables must be set before find_package(hpm-sdk), including:
CONFIG_*— feature switches (e.g.CONFIG_FREERTOS,CONFIG_LWIP)
HEAP_SIZE/STACK_SIZE— heap and stack sizes
HPM_BUILD_TYPE— build type
CUSTOM_GCC_LINKER_FILEand similar custom variables
Setting these variables after find_package will have no effect.
project(my_app)
Defines the project name. Output files will be named accordingly (e.g., my_app.elf, my_app.bin).
sdk_app_src(src/main.c)
Adds source files to the application. Multiple files can be added at once:
sdk_app_src(src/main.c src/utils.c src/config.c)
API details: SDK CMake API Reference.
generate_ide_projects()
Generates IAR EWARM and SEGGER Embedded Studio project files. Must be the last line of every CMakeLists.txt. See SDK CMake User Guide for details.
CMake Extension Cheatsheet
HPM SDK provides sdk_* CMake functions in two categories: application-level and SDK core-level.
Full API list: SDK CMake API Reference.
Application-Level Functions
Function |
Description |
|---|---|
|
Add source files to the application. Relative paths are resolved against the current CMakeLists.txt directory. |
|
Add include directories to the application. |
|
Add source files via glob patterns, e.g. |
SDK Core-Level Functions
Compile Options & Definitions
Function |
Description |
|---|---|
|
Add compiler options, e.g. |
|
Add preprocessor definitions, e.g. |
Source Files & Include Directories
Function |
Description |
|---|---|
|
Add source files to the SDK core library (use for middleware/component development). |
|
Add include directories to the SDK core library (INTERFACE visibility). |
|
Add system include directories (SYSTEM visibility, suppresses third-party warnings). |
Linker
Function |
Description |
|---|---|
|
Link libraries, e.g. |
|
Set linker options, e.g. |
|
Define global linker symbols ( |
Feature-Gated Functions (CONFIG_*)
These functions take effect only when the corresponding CONFIG_* variable is enabled:
Function |
Description |
|---|---|
|
Add source files only if |
|
Add source files only if |
|
Add include dir only if |
|
Add include dir only if |
|
Add preprocessor definitions only if |
|
Link libraries only if |
|
Set linker options only if |
|
Glob source files only if |
Common Patterns
Pattern 1: Single-File Driver Sample
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(gpio_example)
sdk_app_src(src/gpio.c)
generate_ide_projects()
Pattern 2: Multiple Files + Include Directory
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(uart_demo)
sdk_inc(src)
sdk_app_src(src/uart.c src/protocol.c src/ringbuffer.c)
generate_ide_projects()
Pattern 3: Enabling SDK Middleware
Set CONFIG_* variables before find_package(hpm-sdk). See SDK CMake User Guide for the full list of available CONFIG_* variables and feature modules:
cmake_minimum_required(VERSION 3.13)
set(CONFIG_FREERTOS 1) # Enable FreeRTOS
set(CONFIG_LWIP 1) # Enable LwIP
set(CONFIG_SDMMC 1) # Enable SD/eMMC driver
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(network_app)
sdk_app_src(src/main.c src/tcp_server.c)
generate_ide_projects()
- Common
CONFIG_*variables: CONFIG_FREERTOS— FreeRTOSCONFIG_LWIP— LwIP networking stackCONFIG_FATFS— FatFs filesystemCONFIG_USB_DEVICE/CONFIG_USB_HOST— USB stackCONFIG_LVGL— LVGL graphics libraryCONFIG_SDMMC— SD/eMMC driverCONFIG_LIBJPEG— libjpeg-turbo
Pattern 4: Compile Definitions
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(plic_example)
sdk_compile_definitions(-DUSE_S_MODE_IRQ=1)
sdk_app_src(src/plic.c)
generate_ide_projects()
Pattern 5: Heap/Stack Tuning
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(mem_intensive_app)
set(HEAP_SIZE 0x200000) # 2MB heap
set(STACK_SIZE 0x20000) # 128KB stack
sdk_app_src(src/main.c)
generate_ide_projects()
Pattern 6: Multicore (Core 0 + Core 1)
See SDK CMake User Guide for full multicore build instructions. Typical configuration:
Core 0 — builds normally with flash_xip, includes core 1’s firmware image:
if("${HPM_BUILD_TYPE}" STREQUAL "")
SET(HPM_BUILD_TYPE flash_xip)
endif()
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(multicore_core0)
sdk_app_src(../../common/multicore_common.c)
sdk_app_src(src/demo.c src/sec_core_img.c)
generate_ide_projects()
Core 1 — builds as sec_core_img, output is converted to a C array for core 0:
set(HPM_BUILD_TYPE "sec_core_img")
set(SEC_CORE_IMG_C_ARRAY_OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/../core0/src/sec_core_img.c)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(multicore_core1)
sdk_app_src(src/demo.c)
generate_ide_projects()
Pattern 7: Custom Linker Script
See SDK CMake User Guide for detailed custom linker script configuration:
cmake_minimum_required(VERSION 3.13)
find_package(hpm-sdk REQUIRED HINTS $ENV{HPM_SDK_BASE})
project(custom_memory_map)
set(CUSTOM_GCC_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/custom_layout.ld)
set(CUSTOM_SES_LINKER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/custom_layout.icf)
sdk_app_src(src/main.c)
generate_ide_projects()
Key Variables
Build System Variables (Command Line)
Variable |
Description |
|---|---|
|
Required. Target board name, e.g. |
|
Build type. Common values:
|
|
Path to RISC-V GCC toolchain. Can also be set as an environment variable. |
Application Variables
Variable |
Description |
|---|---|
|
Enable/disable SDK feature modules. Set before |
|
Heap size in bytes. Default depends on SDK configuration. |
|
Stack size in bytes. Default depends on SDK configuration. |
|
Path to custom GCC linker script. |
|
Path to custom SEGGER Embedded Studio linker config ( |
Build & Run
# Set up environment
source hpm_sdk/env.sh
export GNURISCV_TOOLCHAIN_PATH=/path/to/riscv32-unknown-elf
# Build
cd samples/my_app
mkdir build && cd build
cmake -GNinja -DBOARD=hpm6200evk ..
ninja
# Output files in build/output/:
# demo.elf — ELF executable
# demo.bin — Binary firmware
# demo.map — Memory map file
For IDE project generation, flash programming, and debug configuration, see SDK CMake User Guide.
Next Steps
SDK CMake User Guide — Full CMake user guide (workflows, multicore, IDE integration, troubleshooting)
SDK CMake API Reference — Complete SDK CMake function API reference
Getting Started — HPM SDK getting started guide