include(cubemx.cmake/cubemx.cmake)
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
project(firmware LANGUAGES C CXX ASM)
add_executable(firmware app_version.c)
cubemx_target(
TARGET firmware
IOC "${CMAKE_CURRENT_LIST_DIR}/app_stub/app_stub.ioc"
FLASH_TARGET_NAME flash_app
IMG_ADDR 0x08008000
)
target_compile_options(firmware PRIVATE -Og -Wall -g -gdwarf-2)
target_compile_definitions(firmware PRIVATE USE_FULL_LL_DRIVER USE_HAL_DRIVER)
target_include_directories(firmware PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/app"
"${CMAKE_CURRENT_LIST_DIR}/common"
)
file(GLOB_RECURSE FIRMWARE_SOURCES
"${CMAKE_CURRENT_LIST_DIR}/app/*.c"
"${CMAKE_CURRENT_LIST_DIR}/app/*.cpp"
"${CMAKE_CURRENT_LIST_DIR}/common/*.c"
"${CMAKE_CURRENT_LIST_DIR}/common/*.cpp"
)
target_sources(firmware PRIVATE ${FIRMWARE_SOURCES})
add_executable(bootloader boot_version.c)
cubemx_target(
TARGET bootloader
IOC "${CMAKE_CURRENT_LIST_DIR}/boot_stub/boot_stub.ioc"
FLASH_TARGET_NAME flash_boot
IMG_ADDR 0x08000000
)
target_compile_options(bootloader PRIVATE -Og -Wall -g -gdwarf-2)
target_compile_definitions(bootloader PRIVATE USE_FULL_LL_DRIVER USE_HAL_DRIVER)
target_include_directories(bootloader PRIVATE
"${CMAKE_CURRENT_LIST_DIR}/boot"
"${CMAKE_CURRENT_LIST_DIR}/common"
)
file(GLOB_RECURSE BOOTLOADER_SOURCES
"${CMAKE_CURRENT_LIST_DIR}/boot/*.c"
"${CMAKE_CURRENT_LIST_DIR}/boot/*.cpp"
"${CMAKE_CURRENT_LIST_DIR}/common/*.c"
"${CMAKE_CURRENT_LIST_DIR}/common/*.cpp"
)
target_sources(bootloader PRIVATE ${BOOTLOADER_SOURCES})
add_custom_command(
OUTPUT app_version.c _ # "_" is dummy output, so this command runs on every build
COMMAND ${CMAKE_COMMAND}
-D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/app/version.c.in
-D OUTPUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/app_version.c
-P ${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen_version.cmake
)
add_custom_command(
OUTPUT boot_version.c _ # "_" is dummy output, so this command runs on every build
COMMAND ${CMAKE_COMMAND}
-D INPUT_FILE=${CMAKE_CURRENT_SOURCE_DIR}/boot/version.c.in
-D OUTPUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/boot_version.c
-P ${CMAKE_CURRENT_SOURCE_DIR}/scripts/gen_version.cmake
)
Result looks like this:
$ cmake .. && make
Using startup file: /home/user/project/app_stub/startup_stm32l476xx.s;/home/user/project/boot_stub/startup_stm32l476xx.s
Using linkerscript: /home/user/project/app_stub/STM32L476RGTx_FLASH.ld;/home/user/project/boot_stub/STM32L476RGTx_FLASH.ld
Using startup file: /home/user/project/app_stub/startup_stm32l476xx.s;/home/user/project/boot_stub/startup_stm32l476xx.s
Using linkerscript: /home/user/project/app_stub/STM32L476RGTx_FLASH.ld;/home/user/project/boot_stub/STM32L476RGTx_FLASH.ld
CMake Error at cubemx.cmake/stlink/flash-target.cmake:4 (add_custom_target):
add_custom_target cannot create target "reset" because another target with
the same name already exists. The existing target is a custom target
created in source directory "/home/user/project". See
documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
cubemx.cmake/cubemx.cmake:147 (include)
CMakeLists.txt:42 (cubemx_target)
CMake Error at cubemx.cmake/stlink/flash-target.cmake:12 (add_custom_target):
add_custom_target cannot create target "erase" because another target with
the same name already exists. The existing target is a custom target
created in source directory "/home/user/project". See
documentation for policy CMP0002 for more details.
Call Stack (most recent call first):
cubemx.cmake/cubemx.cmake:147 (include)
CMakeLists.txt:42 (cubemx_target)
Given the following rough project setup:
Result looks like this: