Replies: 6 comments
-
# Let's say you have a source file `the_generated_file.c` that should be generated at build time
# Firstly, make it being depended by the build system
zephyr_library_sources(${CMAKE_CURRENT_BINARY_DIR}/the_generated_file.c)
# Secondly, declare how to generate this file
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/the_generated_file.c
COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/your_script_to_run.sh
)
set_source_files_properties(
${CMAKE_CURRENT_BINARY_DIR}/the_generated_file.c
PROPERTIES GENERATED TRUE
) |
Beta Was this translation helpful? Give feedback.
-
@xingrz this looks like it makes sense, let me try it. Is this documented anywhere in the zephyr docs? I can't be the only one generating files as a part of the build. |
Beta Was this translation helpful? Give feedback.
-
It's just some CMake tricks, not Zephyr-specific. ( |
Beta Was this translation helpful? Give feedback.
-
For anyone else reading it should be unnecessary to set_source_file_properties() to set GENERATED, add_custom_command() From https://cmake.org/cmake/help/latest/command/add_custom_command.html#command:add_custom_command:
|
Beta Was this translation helpful? Give feedback.
-
Just a hint from my experience with generated files, since zephyr also heavily uses this with python scripts, and I got used to adding quite an amount of those "meta" sources that are generated in compile time (trust me they save you A LOT of time if done right). I got two options that depend on when you want them to be generated, in this case let's assume you have a python script ready: If you want it to run when CMakeLists loads or reloads, add this to your CMakeLists.txt:
Otherwise if you'd like it to run before each build:
I have used those for quite a while now, e.g. generating lookup tables for ADC values and converting images to byte arrays for OLED displays. Hope this helps and solves your needs! |
Beta Was this translation helpful? Give feedback.
-
I have been trying unsuccessfully to get binary files (images, fonts, ...) objcopied to object files and then linked. Here, I thought to have the solution: prepending my cmake code by zephyr_library_sources(${CMAKE_CURRENT_BINARY_DIR}/the_generated_file.c) as notified in the first comment posting. But this is only partially true... Let me explore my code snippet in the project's CmakeLists.txt (where add_custom_command(OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/iconfont.bin.o
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} # this ensures object symbols without prepending path
COMMAND ${CMAKE_OBJCOPY}
ARGS -I binary -O elf32-littlearm iconfont.bin ${CMAKE_CURRENT_BINARY_DIR}/iconfont.bin.o)
target_link_libraries(app PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/iconfont.bin.o) No object file is created, apparently because no dependency is given that invokes the custom command. But when I prepend the code snippet by the above Thats one issue. The other one is: the object file is associated with the .data-section, not .text, which actually doubles the size of the binary data into both flash and RAM-memory. FYI: ideas to get it as far as above are found e.g. here: https://stackoverflow.com/questions/23000564/cmake-embed-elf-into-executable. The difficulty in the context of zephyr is the fact that mostly, an BOTTOM LINE: |
Beta Was this translation helpful? Give feedback.
-
I'd like to use add_custom_command() to generate some files that my project would depend on at compile time.
I can't find any guides on the recommended process for integrating generated files in the build process, nothing is mentioned in any of the zephyr build related docs, google doesn't turn up anything for 'zephyr add_custom_command' or 'zephyr add_custom_target'.
Beta Was this translation helpful? Give feedback.
All reactions