不安装如果运行目标返回错误。

huangapple go评论60阅读模式
英文:

cmake: Do not install if running a target returns error

问题

我构建了一个可执行文件Myexe,想在决定安装前测试运行它。决定将基于CUSTOM_COMMAND_*输出。我该如何做呢?以下是我采取的步骤:

添加可执行文件(Myexe)和源文件。

添加自定义命令:

  • 目标为Myexe
  • 后期构建
  • 命令为Myexe
  • 依赖于MyexeLib
  • 以RESULT_VARIABLE保存结果到CUSTOM_COMMAND_RESULT
  • 以OUTPUT_VARIABLE保存输出到CUSTOM_COMMAND_OUTPUT
  • 以ERROR_VARIABLE保存错误到CUSTOM_COMMAND_ERROR

安装目标Myexe到"${CMAKE_BINARY_DIR}/install/"。

英文:

I build an executable Myexe and would like to test run it before deciding to install it. The decision would be based on the CUSTOM_COMMAND_* outputs.
How may I do that please?
Here are the steps I take:

add_executable(Myexe ${SOURCES}) 
add_custom_command(
        TARGET Myexe
        POST_BUILD
        COMMAND Myexe
        DEPENDS MyexeLib
        RESULT_VARIABLE CUSTOM_COMMAND_RESULT
        OUTPUT_VARIABLE CUSTOM_COMMAND_OUTPUT
        ERROR_VARIABLE CUSTOM_COMMAND_ERROR )

install(TARGETS Myexe DESTINATION "${CMAKE_BINARY_DIR}/install/")

答案1

得分: 2

If a POST_BUILD step fails, CMake will delete the executable that has been built. If you use POST_BUILD steps to run the project's tests, any test failure will cause the executable to be deleted. You won't be able to debug the test failure with a debugger, so this is a bad idea.

The way this is usually done instead is to use CTest to run the tests. If all you want to test is that the executable runs (as your current POST_BUILD step suggests), then it is sufficient to add a call to enable_testing() in your top-level CMakeLists.txt file and then add:

add_test(
  NAME MyExeTests
  COMMAND Myexe
)

in the CMakeLists.txt where the Myexe target is defined.

To run the tests, you can then call:

ctest --test-dir <build-dir> --output-on-failure

before running your regular cmake --build <build-dir> --target install or cmake --install <build-dir> command you currently use to install your project.

英文:

If a POST_BUILD step fails CMake will delete the executable that has been build. If you use POST_BUILD steps to run the project's tests then any test failure will cause the executable to be deleted. You thus won't be able to debug the test failure with a debbuger, so this is a bad idea.

The way this is usually done instead is to use CTest to run the tests. If all you want to test is that the executable runs (as your current POST_BUILD step suggests) then it is sufficient to add a call to enable_testing() in your top-level CMakeLists.txt file and then add

add_test(
  NAME MyExeTests
  COMMAND Myexe
)

in the CMakeLists.txt where the Myexe target is defined.

To run the tests you then can call

ctest --test-dir &lt;build-dir&gt; --output-on-failure

before running your regular cmake --build &lt;build-dir&gt; --target install or cmake --install &lt;build-dir&gt; command you currently use to install your project.

huangapple
  • 本文由 发表于 2023年5月22日 00:10:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300805.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定