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
Add a comment  | 

1 Answer 1

Reset to default 1

So 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
)

本文标签: