Skip to content

Commit 6802f30

Browse files
57300nordicjm
authored andcommitted
cmake: Fix and cleanup CDDL dependencies
The main problem was an extra, unnecessary CMake re-run that happened at the start of any build with CONFIG_SUIT_PROCESSOR=y. This had to do with tracking CDDL file changes to trigger regeneration of zcbor files, which was set up incorrectly. On the initial CMake run, this module would do the following: 1. Execute zcbor commands to generate some *.c, *.h, *.cmake files. 2. Include the generated *.cmake files. 3. Add custom commands and targets to regenerate the *.cmake files at build time, using the same commands from step 1. Then, Ninja would start with generating the files again. This is because whenever Ninja decides whether to run a command, it doesn't check if the output files already exist; it only checks if (and when) it has executed the command before. Here's the abridged output from `ninja -d explain`: ninja explain: command line not found in log for cose.cmake ninja explain: cose.cmake is dirty ninja explain: command line not found in log for manifest.cmake ninja explain: manifest.cmake is dirty [1/3] Generating cose.cmake [2/3] Generating manifest.cmake [2/3] Re-running CMake... In principle, we cannot set up these files to be generated at both configure time and build time. It only really makes sense to do it at configure time, because rewriting CMake sources forces a re-run anyway. Therefore, instead of using custom commands to track CDDL dependencies, use the CMAKE_CONFIGURE_DEPENDS directory property. While at it, refactor the module to move the zcbor generation code into a common function for the `cose` and `manifest` libraries. This reduces duplication and fixes another issue, where not all CDDL files were able to trigger regeneration (such as `cose_encrypt.cddl`). Signed-off-by: Grzegorz Swiderski <[email protected]>
1 parent c97fbb3 commit 6802f30

File tree

1 file changed

+43
-74
lines changed

1 file changed

+43
-74
lines changed

CMakeLists.txt

Lines changed: 43 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,57 @@ else()
1212
set(ZCBOR_COMMAND ${PYTHON_EXECUTABLE} ${ZEPHYR_ZCBOR_MODULE_DIR}/zcbor/zcbor.py)
1313
endif()
1414

15-
set(ZCBOR_DIR "${CMAKE_CURRENT_BINARY_DIR}")
15+
function(zcbor_generate_library name)
16+
cmake_parse_arguments(arg "DECODE;ENCODE" "" "CDDL_FILES;ENTRY_TYPES" ${ARGN})
1617

17-
# Generate and add COSE parser code
18-
set(ZCBOR_COMMAND_COSE
19-
${ZCBOR_COMMAND}
20-
code
21-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/cose_sign.cddl"
22-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/cose_encrypt.cddl"
23-
-d -e
24-
-t COSE_Sign1_Tagged Sig_structure1
25-
COSE_Encrypt_Tagged Enc_structure
26-
--output-cmake cose.cmake
18+
# Setup zcbor command arguments
19+
list(TRANSFORM arg_CDDL_FILES PREPEND "--cddl;" OUTPUT_VARIABLE zcbor_code_args)
20+
if(arg_DECODE)
21+
list(APPEND zcbor_code_args --decode)
22+
endif()
23+
if(arg_ENCODE)
24+
list(APPEND zcbor_code_args --encode)
25+
endif()
26+
list(APPEND zcbor_code_args
27+
--entry-types ${arg_ENTRY_TYPES}
28+
--output-cmake ${name}.cmake
2729
--copy-sources
28-
)
29-
30-
if(NOT EXISTS "${ZCBOR_DIR}/cose.cmake")
31-
execute_process(
32-
COMMAND ${ZCBOR_COMMAND_COSE}
33-
WORKING_DIRECTORY ${ZCBOR_DIR}
34-
COMMAND_ERROR_IS_FATAL ANY
35-
)
36-
endif()
37-
38-
# Create cmake target to track changes in the input cddl file
39-
add_custom_command(
40-
OUTPUT "${ZCBOR_DIR}/cose.cmake"
41-
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/cddl/cose_sign.cddl"
42-
COMMAND ${ZCBOR_COMMAND_COSE}
43-
WORKING_DIRECTORY "${ZCBOR_DIR}"
4430
)
45-
add_custom_target(zcbor_cose ALL
46-
DEPENDS "${ZCBOR_DIR}/cose.cmake"
47-
COMMENT "Generate cose encode/decode sources for parsing CBOR"
31+
execute_process(
32+
COMMAND ${ZCBOR_COMMAND} code ${zcbor_code_args}
33+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
34+
COMMAND_ERROR_IS_FATAL ANY
4835
)
36+
include(${CMAKE_CURRENT_BINARY_DIR}/${name}.cmake)
4937

50-
include("${ZCBOR_DIR}/cose.cmake")
51-
# Specify the absolute path for ZCBOR-generated code include directory
52-
target_include_directories(cose PUBLIC
53-
${ZCBOR_DIR}/include
54-
)
38+
# Track changes to input cddl files
39+
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS ${arg_CDDL_FILES})
40+
endfunction()
5541

56-
# Generate and add SUIT envelope parser code
57-
set(ZCBOR_COMMAND_MANIFEST
58-
${ZCBOR_COMMAND}
59-
code
60-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/manifest.cddl"
61-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/trust_domains.cddl"
62-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/update_management.cddl"
63-
-c "${CMAKE_CURRENT_LIST_DIR}/cddl/firmware_encryption.cddl"
64-
-d
65-
-t SUIT_Envelope_Tagged SUIT_Manifest SUIT_Shared_Sequence SUIT_Command_Sequence
66-
SUIT_Condition SUIT_Directive SUIT_Shared_Commands SUIT_Text_Map SUIT_Digest
67-
SUIT_Condition_Version_Comparison_Value SUIT_Parameter_Version_Match
68-
--output-cmake manifest.cmake
69-
--copy-sources
70-
)
71-
if(NOT EXISTS "${ZCBOR_DIR}/manifest.cmake")
72-
execute_process(
73-
COMMAND ${ZCBOR_COMMAND_MANIFEST}
74-
WORKING_DIRECTORY ${ZCBOR_DIR}
75-
COMMAND_ERROR_IS_FATAL ANY
42+
# Generate and add COSE parser code
43+
zcbor_generate_library(cose
44+
CDDL_FILES
45+
${CMAKE_CURRENT_LIST_DIR}/cddl/cose_sign.cddl
46+
${CMAKE_CURRENT_LIST_DIR}/cddl/cose_encrypt.cddl
47+
DECODE ENCODE
48+
ENTRY_TYPES
49+
COSE_Sign1_Tagged Sig_structure1
50+
COSE_Encrypt_Tagged Enc_structure
7651
)
77-
endif()
78-
79-
# Create cmake target to track changes in the input cddl file
80-
add_custom_command(
81-
OUTPUT "${ZCBOR_DIR}/manifest.cmake"
82-
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/cddl/manifest.cddl"
83-
DEPENDS "${CMAKE_CURRENT_LIST_DIR}/cddl/trust_domains.cddl"
84-
COMMAND ${ZCBOR_COMMAND_MANIFEST}
85-
WORKING_DIRECTORY "${ZCBOR_DIR}"
86-
)
87-
add_custom_target(zcbor_manifest ALL
88-
DEPENDS "${ZCBOR_DIR}/manifest.cmake"
89-
COMMENT "Generate manifest encode/decode sources for parsing CBOR"
90-
)
9152

92-
include("${ZCBOR_DIR}/manifest.cmake")
93-
# Specify the absolute path for ZCBOR-generated code include directory
94-
target_include_directories(manifest PUBLIC
95-
${ZCBOR_DIR}/include
96-
)
53+
# Generate and add SUIT envelope parser code
54+
zcbor_generate_library(manifest
55+
CDDL_FILES
56+
${CMAKE_CURRENT_LIST_DIR}/cddl/manifest.cddl
57+
${CMAKE_CURRENT_LIST_DIR}/cddl/trust_domains.cddl
58+
${CMAKE_CURRENT_LIST_DIR}/cddl/update_management.cddl
59+
${CMAKE_CURRENT_LIST_DIR}/cddl/firmware_encryption.cddl
60+
DECODE
61+
ENTRY_TYPES
62+
SUIT_Envelope_Tagged SUIT_Manifest SUIT_Shared_Sequence SUIT_Command_Sequence
63+
SUIT_Condition SUIT_Directive SUIT_Shared_Commands SUIT_Text_Map SUIT_Digest
64+
SUIT_Condition_Version_Comparison_Value SUIT_Parameter_Version_Match
65+
)
9766

9867
# Define SUIT library
9968
add_library(suit)

0 commit comments

Comments
 (0)