本文介绍了推迟自定义目标,直到安装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有这样的东西:

  $ c>作为副作用。这可以通过使用命令:

  add_custom_command(OUTPUT somefile)
add_custom_target(tgt DEPENDS somefile)

install(CODEexecute_process(COMMAND \$ {CMAKE_COMMAND} \--build \$ {CMAKE_CURRENT_BINARY_DIR} \--target tgt))
install(FILES $ {CMAKE_CURRENT_BINARY_DIR} / somefile DESTINATION somedir)

execute_process 调用cmake以在之前创建目标 tgt


I have something like this in my project:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt ALL DEPENDS somefile)

install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

This works OK, but my command is being run during make because of ALL keyword in add_custom_target(). What i want is to make CMake to run this command only when make install is issued, not during build.

If i remove ALL keyword, whole target is not being built by default, so somefile is not produced and make install fails.

解决方案

A possible solution is to have the make install command invoke the make tgt as a side effect. This can be done by using the CODE signature of the install command:

add_custom_command(OUTPUT somefile)
add_custom_target(tgt DEPENDS somefile)

install(CODE "execute_process(COMMAND \"${CMAKE_COMMAND}\" --build \"${CMAKE_CURRENT_BINARY_DIR}\" --target tgt)")
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/somefile DESTINATION somedir)

The execute_process invokes cmake to build the target tgt before somefile is installed.

这篇关于推迟自定义目标,直到安装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:49