admin管理员组文章数量:1122846
I'm using add_custom_command to make some modifications of generated binary files. During the build process of my project, some non-binary files are generated. I want to embed checksums of these generated files into the target binary file.
So I use add_custom_command as a post-build command. In the target binary file I reserve some space for checksums. I have temporary files which contains checksums, with grep -abo I find the reserved places in the target binary file and then I want to place checksums in them. I'm trying to do it with dd if=${CHECKSUM_FILE} of=${TARGET_FILE} bs=1 seek=? conv=notrunc. I've tried to save offsets in the target binary file in a variable, but sh doesn't provide OFFSET=$(cat offset.tmp) syntax. Also I've tried to place value in variable by means of read OFFSET < offset.tmp and have no success.
So my question is how can I provide offset in the target binary file (this value stored in temporary file) as seek parameter for dd command?
UPD my code looks like
add_custom_target(calc_extra_file_hash ALL
DEPENDS target_binary extra_file
COMMAND ${CMAKE_COMMAND} -E sha256sum ${EXTRA_FILE_PATH} | cut -f1 -d' ' | xxd -r -p > hash.tmp
)
add_custom_command(
TARGET calc_extra_file_hash POST_BUILD
DEPENDS calc_extra_file_hash
COMMAND grep -abo 'EXTRA_FILE_HASH' ${TARGET_BINARY} | cut -f1 -d':' > offset.tmp &&
read OFFSET < offset.tmp | tr -d '\r' &&
dd if=hash.tmp of=${TARGET_BINARY} bs=1 seek=${OFFSET} conv=notrunc
)
But it doesn't work. ${OFFSET} is empty, but offset.tmp contains valid value
I'm using add_custom_command to make some modifications of generated binary files. During the build process of my project, some non-binary files are generated. I want to embed checksums of these generated files into the target binary file.
So I use add_custom_command as a post-build command. In the target binary file I reserve some space for checksums. I have temporary files which contains checksums, with grep -abo I find the reserved places in the target binary file and then I want to place checksums in them. I'm trying to do it with dd if=${CHECKSUM_FILE} of=${TARGET_FILE} bs=1 seek=? conv=notrunc. I've tried to save offsets in the target binary file in a variable, but sh doesn't provide OFFSET=$(cat offset.tmp) syntax. Also I've tried to place value in variable by means of read OFFSET < offset.tmp and have no success.
So my question is how can I provide offset in the target binary file (this value stored in temporary file) as seek parameter for dd command?
UPD my code looks like
add_custom_target(calc_extra_file_hash ALL
DEPENDS target_binary extra_file
COMMAND ${CMAKE_COMMAND} -E sha256sum ${EXTRA_FILE_PATH} | cut -f1 -d' ' | xxd -r -p > hash.tmp
)
add_custom_command(
TARGET calc_extra_file_hash POST_BUILD
DEPENDS calc_extra_file_hash
COMMAND grep -abo 'EXTRA_FILE_HASH' ${TARGET_BINARY} | cut -f1 -d':' > offset.tmp &&
read OFFSET < offset.tmp | tr -d '\r' &&
dd if=hash.tmp of=${TARGET_BINARY} bs=1 seek=${OFFSET} conv=notrunc
)
But it doesn't work. ${OFFSET} is empty, but offset.tmp contains valid value
Share Improve this question edited Nov 22, 2024 at 14:10 tserge asked Nov 22, 2024 at 13:17 tsergetserge 231 gold badge1 silver badge7 bronze badges 2- 1 Instead of describing your code, can you please edit and share a minimal reproducible example including expected and actual behavior? – Friedrich Commented Nov 22, 2024 at 13:21
- Please, make title and question sentence to be corresponding one to another. Currently they are not: "How to use temporary file content in cmake add_custom_command?" and "how can I provide offset in the target binary file". – Tsyvarev Commented Nov 22, 2024 at 13:25
1 Answer
Reset to default 1So tell cmake to generate that custom file content and properly model dendencies. One file depends on the other, which depends on the other and so on. Do not overwrite existing files, it's impossible to track dependencies then. Keep build artefacts in CMAKE_CURRENT_BINARY_DIR . I would recommend against POST_BUILD generating something.
add_custom_command(
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/hash.tmp
DEPENDS ${EXTRA_FILE_PATH}
COMMAND
${CMAKE_COMMAND} -E sha256sum ${EXTRA_FILE_PATH} |
cut -f1 -d' ' |
xxd -r -p >${CMAKE_CURRENT_BINARY_DIR}/hash.tmp
)
add_custom_command(
OUTPUT ${TARGET_BINARY}.output
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/hash.tmp ${TARGET_BINARY}
BYPRODUCTS ${CMAKE_CURRENT_BINARY_DIR}/offset.tmp
COMMAND
grep -abo 'EXTRA_FILE_HASH' ${TARGET_BINARY} | cut -f1 -d':' > offset.tmp &&
read OFFSET < offset.tmp | tr -d '\r' &&
cp ${TARGET_BINARY} ${TARGET_BINARY}.output &&
dd if=${CMAKE_CURRENT_BINARY_DIR}/hash.tmp of=${TARGET_BINARY}.output bs=1 seek=${OFFSET} conv=notrunc
)
本文标签:
版权声明:本文标题:How to provide seek parameter value in dd command from temporary file in the cmake add_custom_command? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736303568a1931932.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论