Skip to content

Commit b6095a8

Browse files
tejlmandcarlescufi
authored andcommitted
cmake: add Zephyr image output files as byproducts
Export Zephyr image byproducts through `BYPRODUCT_<VAR>` cache variables. This allow external tools, such as sysbuild, to read information on products produced by a Zephyr build from the image CMake cache. For sysbuild, this means that all byproducts will be added to a phony build target, which again allow sysbuild itself to depends on target output and properly describe dependencies between byproducts and their producing targets. Signed-off-by: Torsten Rasmussen <[email protected]>
1 parent 2c1f079 commit b6095a8

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,7 @@ target_link_libraries_ifdef(CONFIG_NATIVE_LIBRARY ${ZEPHYR_LINK_STAGE_EXECUTABLE
14361436
target_byproducts(TARGET ${ZEPHYR_LINK_STAGE_EXECUTABLE}
14371437
BYPRODUCTS ${PROJECT_BINARY_DIR}/${ZEPHYR_LINK_STAGE_EXECUTABLE}.map
14381438
)
1439+
set(BYPRODUCT_KERNEL_ELF_NAME "${PROJECT_BINARY_DIR}/${KERNEL_ELF_NAME}" CACHE FILEPATH "Kernel elf file" FORCE)
14391440
set_property(TARGET
14401441
${ZEPHYR_LINK_STAGE_EXECUTABLE}
14411442
PROPERTY LINK_DEPENDS ${PROJECT_BINARY_DIR}/${ZEPHYR_CURRENT_LINKER_CMD}
@@ -1582,6 +1583,7 @@ if(CONFIG_BUILD_OUTPUT_HEX OR BOARD_FLASH_RUNNER STREQUAL openocd)
15821583
post_build_byproducts
15831584
${KERNEL_HEX_NAME}
15841585
)
1586+
set(BYPRODUCT_KERNEL_HEX_NAME "${PROJECT_BINARY_DIR}/${KERNEL_HEX_NAME}" CACHE FILEPATH "Kernel hex file" FORCE)
15851587
endif()
15861588
endif()
15871589

@@ -1603,6 +1605,7 @@ if(CONFIG_BUILD_OUTPUT_BIN)
16031605
post_build_byproducts
16041606
${KERNEL_BIN_NAME}
16051607
)
1608+
set(BYPRODUCT_KERNEL_BIN_NAME "${PROJECT_BINARY_DIR}/${KERNEL_BIN_NAME}" CACHE FILEPATH "Kernel binary file" FORCE)
16061609
endif()
16071610
endif()
16081611

@@ -1635,6 +1638,7 @@ if(CONFIG_BUILD_OUTPUT_BIN AND CONFIG_BUILD_OUTPUT_UF2)
16351638
post_build_byproducts
16361639
${KERNEL_UF2_NAME}
16371640
)
1641+
set(BYPRODUCT_KERNEL_UF2_NAME "${PROJECT_BINARY_DIR}/${KERNEL_UF2_NAME}" CACHE FILEPATH "Kernel uf2 file" FORCE)
16381642
endif()
16391643

16401644
if(CONFIG_BUILD_OUTPUT_META)
@@ -1684,6 +1688,7 @@ if(CONFIG_BUILD_OUTPUT_S19)
16841688
post_build_byproducts
16851689
${KERNEL_S19_NAME}
16861690
)
1691+
set(BYPRODUCT_KERNEL_S19_NAME "${PROJECT_BINARY_DIR}/${KERNEL_S19_NAME}" CACHE FILEPATH "Kernel s19 file" FORCE)
16871692
endif()
16881693
endif()
16891694

@@ -1766,6 +1771,7 @@ if(CONFIG_BUILD_OUTPUT_EXE)
17661771
post_build_byproducts
17671772
${KERNEL_EXE_NAME}
17681773
)
1774+
set(BYPRODUCT_KERNEL_EXE_NAME "${PROJECT_BINARY_DIR}/${KERNEL_EXE_NAME}" CACHE FILEPATH "Kernel exe file" FORCE)
17691775
else()
17701776
if(CMAKE_GENERATOR STREQUAL "Unix Makefiles")
17711777
set(MAKE "${CMAKE_MAKE_PROGRAM}" CACHE FILEPATH "cmake defined make")

cmake/mcuboot.cmake

+18
Original file line numberDiff line numberDiff line change
@@ -114,15 +114,24 @@ function(zephyr_mcuboot_tasks)
114114
list(APPEND unconfirmed_args --bin --sbin ${output}.signed.bin)
115115
list(APPEND byproducts ${output}.signed.bin)
116116
zephyr_runner_file(bin ${output}.signed.bin)
117+
set(BYPRODUCT_KERNEL_SIGNED_BIN_NAME "${output}.signed.bin"
118+
CACHE FILEPATH "Signed kernel bin file" FORCE
119+
)
117120

118121
if(CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE)
119122
list(APPEND confirmed_args --bin --sbin ${output}.signed.confirmed.bin)
120123
list(APPEND byproducts ${output}.signed.confirmed.bin)
124+
set(BYPRODUCT_KERNEL_SIGNED_CONFIRMED_BIN_NAME "${output}.signed.confirmed.bin"
125+
CACHE FILEPATH "Signed and confirmed kernel bin file" FORCE
126+
)
121127
endif()
122128

123129
if(NOT "${keyfile_enc}" STREQUAL "")
124130
list(APPEND encrypted_args --bin --sbin ${output}.signed.encrypted.bin)
125131
list(APPEND byproducts ${output}.signed.encrypted.bin)
132+
set(BYPRODUCT_KERNEL_SIGNED_ENCRYPTED_BIN_NAME "${output}.signed.encrypted.bin"
133+
CACHE FILEPATH "Signed and encrypted kernel bin file" FORCE
134+
)
126135
endif()
127136
endif()
128137

@@ -131,15 +140,24 @@ function(zephyr_mcuboot_tasks)
131140
list(APPEND unconfirmed_args --hex --shex ${output}.signed.hex)
132141
list(APPEND byproducts ${output}.signed.hex)
133142
zephyr_runner_file(hex ${output}.signed.hex)
143+
set(BYPRODUCT_KERNEL_SIGNED_HEX_NAME "${output}.signed.hex"
144+
CACHE FILEPATH "Signed kernel hex file" FORCE
145+
)
134146

135147
if(CONFIG_MCUBOOT_GENERATE_CONFIRMED_IMAGE)
136148
list(APPEND confirmed_args --hex --shex ${output}.signed.confirmed.hex)
137149
list(APPEND byproducts ${output}.signed.confirmed.hex)
150+
set(BYPRODUCT_KERNEL_SIGNED_CONFIRMED_HEX_NAME "${output}.signed.confirmed.hex"
151+
CACHE FILEPATH "Signed and confirmed kernel hex file" FORCE
152+
)
138153
endif()
139154

140155
if(NOT "${keyfile_enc}" STREQUAL "")
141156
list(APPEND encrypted_args --hex --shex ${output}.signed.encrypted.hex)
142157
list(APPEND byproducts ${output}.signed.encrypted.hex)
158+
set(BYPRODUCT_KERNEL_SIGNED_ENCRYPTED_HEX_NAME "${output}.signed.encrypted.hex"
159+
CACHE FILEPATH "Signed and encrypted kernel hex file" FORCE
160+
)
143161
endif()
144162
endif()
145163

share/sysbuild/cmake/modules/sysbuild_extensions.cmake

+25
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ function(load_cache)
3434
set_property(TARGET ${LOAD_CACHE_IMAGE}_cache APPEND PROPERTY "CACHE:VARIABLES" "${CMAKE_MATCH_1}")
3535
set_property(TARGET ${LOAD_CACHE_IMAGE}_cache PROPERTY "${CMAKE_MATCH_1}:TYPE" "${CMAKE_MATCH_2}")
3636
set_property(TARGET ${LOAD_CACHE_IMAGE}_cache PROPERTY "${CMAKE_MATCH_1}" "${variable_value}")
37+
if("${CMAKE_MATCH_1}" MATCHES "^BYPRODUCT_.*")
38+
set_property(TARGET ${LOAD_CACHE_IMAGE}_cache APPEND
39+
PROPERTY "EXTRA_BYPRODUCTS" "${variable_value}"
40+
)
41+
endif()
3742
endif()
3843
endforeach()
3944
endfunction()
@@ -297,6 +302,17 @@ endfunction()
297302
# If the application is not due to ExternalZephyrProject_Add() being called,
298303
# then an error is raised.
299304
#
305+
# The image output files are added as target properties on the image target as:
306+
# ELF_OUT: property specifying the generated elf file.
307+
# BIN_OUT: property specifying the generated bin file.
308+
# HEX_OUT: property specifying the generated hex file.
309+
# S19_OUT: property specifying the generated s19 file.
310+
# UF2_OUT: property specifying the generated uf2 file.
311+
# EXE_OUT: property specifying the generated exe file.
312+
#
313+
# the property is only set if the image is configured to generate the output
314+
# format. Elf files are always created.
315+
#
300316
# APPLICATION: <name>: Name of the application.
301317
#
302318
function(ExternalZephyrProject_Cmake)
@@ -408,6 +424,15 @@ function(ExternalZephyrProject_Cmake)
408424
endif()
409425
load_cache(IMAGE ${ZCMAKE_APPLICATION} BINARY_DIR ${BINARY_DIR})
410426
import_kconfig(CONFIG_ ${BINARY_DIR}/zephyr/.config TARGET ${ZCMAKE_APPLICATION})
427+
428+
# This custom target informs CMake how the BYPRODUCTS are generated if a target
429+
# depends directly on the BYPRODUCT instead of depending on the image target.
430+
get_target_property(${ZCMAKE_APPLICATION}_byproducts ${ZCMAKE_APPLICATION}_cache EXTRA_BYPRODUCTS)
431+
add_custom_target(${ZCMAKE_APPLICATION}_extra_byproducts
432+
COMMAND ${CMAKE_COMMAND} -E true
433+
BYPRODUCTS ${${ZCMAKE_APPLICATION}_byproducts}
434+
DEPENDS ${ZCMAKE_APPLICATION}
435+
)
411436
endfunction()
412437

413438
# Usage:

0 commit comments

Comments
 (0)