0

I have custom target to run my test after test target is built

add_executable( my_unit_test
    ${SRC}
) 

Before running the test I patch elf

add_custom_command(
    TARGET my_unit_test POST_BUILD
    COMMENT "=================== PATCH UNIT TEST ELF  ==================="
    COMMAND patchelf --set-interpreter /usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 my_unit_test
    VERBATIM
)

And this is the target to run test

add_test( 
    NAME UT
    COMMAND ${CMAKE_CURRENT_BINARY_DIR}/my_unit_test
)

add_custom_target( run_ut_tests ALL
    DEPENDS my_unit_test
)

add_custom_command( TARGET run_ut_tests POST_BUILD
    COMMENT "=================== RUN UNIT TESTS ==================="
    COMMAND ctest ARGS --output-on-failure
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
)

The problem is the test runs twice. What is the mistake here ?

chuong vo
  • 71
  • 6
  • Putting `${CMAKE_CURRENT_BINARY_DIR}/` before `my_unit_test` in `add_test` is actually wrong for multi-config generators. Just write `my_unit_test`, CMake knows it's a target and will substitute in the correct location. – Alex Reinking Aug 11 '21 at 13:33
  • I tried your code and could not reproduce the behavior you describe. The test runs once for me. You need to provide a **complete**, minimal, reproducible example if you expect anyone to be able to help you. – Alex Reinking Aug 11 '21 at 13:37
  • In `add_custom_target`, the `DEPENDS` clause is for _files_, not _targets_. You should use `add_dependencies` instead. Also, there's no reason to attach a `POST_BUILD` command to the target... `add_custom_target` has a `COMMAND` argument already! Please _read the documentation_: https://cmake.org/cmake/help/latest/command/add_custom_target.html – Alex Reinking Aug 11 '21 at 13:38
  • @AlexReinking : the POST_BUILD is for build event : `POST_BUILD Run after all other rules within the target have been executed.` . The intention here is to run the test after `my_unit_test` is built. https://cmake.org/cmake/help/latest/command/add_custom_command.html#command:add_custom_command – chuong vo Aug 11 '21 at 14:07
  • There are no rules within that target to begin with. The `POST_BUILD` is pointless. – Alex Reinking Aug 11 '21 at 14:09
  • 1
    @AlexReinking : yes, you're correct for run_unit_test, I should not add POST_BUILD. For patch_elf, the POST_BUILD should be there. – chuong vo Aug 11 '21 at 14:48
  • I've just observed that the problem only happens when using cross-build (Linux Yocto). When trying with native build the problem does not happen. – chuong vo Aug 11 '21 at 16:34
  • might be patchelf causes the problem ? – chuong vo Aug 11 '21 at 16:39

0 Answers0