CMAKE:在运行自定义命令后执行文件复制

发布于 2024-11-24 00:02:42 字数 882 浏览 2 评论 0原文

我有一个如下所示的构建脚本片段:

foreach(...)
...
  add_custom_command( OUTPUT ${fn_c} ${fn_s} ${fn_p_c} {fn_p_h}
                      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_SOURCE_DIR}  ${CMAKE_CURRENT_SOURCE_DIR}/${proto_var} --cpp_out=. --plugin=protoc-gen-RBLRPC=${CMAKE_BINARY_DIR}/tools/protoc-gen-RBLRPC --RBLRPC_out=. 
                      DEPENDS ${proto_var}
  )

  if(${M_S_} OR ${M_C_})
    set(MARSHALL_RPC_FILES ${MARSHALL_RPC_FILES} ${fn_p_c})
    message(status "copy marshall -------------------")
    file(COPY ${CMAKE_CURRENT_BINARY_DIR}/${fn_c} 
              ${CMAKE_CURRENT_BINARY_DIR}/${fn_s} 
              ${CMAKE_CURRENT_BINARY_DIR}/${fn_p_h} DESTINATION ${CMAKE_SOURCE_DIR}/include/rpc/marshall)
  endif()
...
endforeach(...)

在执行自定义命令之前不会生成复制的文件,但是 cmake 会在第一次传递脚本时尝试复制文件。我欢迎任何解决这个问题的建议,而不需要彻底改变我的脚本。

I have a build script fragment that looks as follows:

foreach(...)
...
  add_custom_command( OUTPUT ${fn_c} ${fn_s} ${fn_p_c} {fn_p_h}
                      COMMAND ${PROTOBUF_PROTOC_EXECUTABLE} --proto_path=${CMAKE_CURRENT_SOURCE_DIR}  ${CMAKE_CURRENT_SOURCE_DIR}/${proto_var} --cpp_out=. --plugin=protoc-gen-RBLRPC=${CMAKE_BINARY_DIR}/tools/protoc-gen-RBLRPC --RBLRPC_out=. 
                      DEPENDS ${proto_var}
  )

  if(${M_S_} OR ${M_C_})
    set(MARSHALL_RPC_FILES ${MARSHALL_RPC_FILES} ${fn_p_c})
    message(status "copy marshall -------------------")
    file(COPY ${CMAKE_CURRENT_BINARY_DIR}/${fn_c} 
              ${CMAKE_CURRENT_BINARY_DIR}/${fn_s} 
              ${CMAKE_CURRENT_BINARY_DIR}/${fn_p_h} DESTINATION ${CMAKE_SOURCE_DIR}/include/rpc/marshall)
  endif()
...
endforeach(...)

The copied files are not generated untill the custom comand is executed, however cmake attempts to copy the files upon first pass over the script. I'd welcome any suggestions to solve this problem , without drastically changing my scrips.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-12-01 00:02:42

不要使用 file(COPY...) 函数,而是将以下命令添加到您的 add_custom_command 中:

COMMAND ${CMAKE_COMMAND} copy ${CMAKE_CURRENT_BINARY_DIR}/${fn_c}
        ${CMAKE_SOURCE_DIR}/include/rpc/marshall

但是对于您要执行的操作,我建议您保持源代码树干净,直接添加使用构建目录中生成的文件。例如,如果您想从单个源树创建两个不同的构建树,那么这就会中断。

编辑:

CMAKE_COMMAND 记录在在线手册页文档的变量部分中(搜索 CMAKE_COMMAND 而不是 ${CMAKE_COMMAND}。

在命令行上 CMAKE -E 将显示可用的可移植命令列表。

Don't use file(COPY...) function, but add the following command to your add_custom_command:

COMMAND ${CMAKE_COMMAND} copy ${CMAKE_CURRENT_BINARY_DIR}/${fn_c}
        ${CMAKE_SOURCE_DIR}/include/rpc/marshall

But for what you're going to do, I would suggest you keep your source tree clean, add directly use the generated files from the build directory. That would break, for instance, if you want to make two different build tree from a single source tree.

edit :

CMAKE_COMMAND is documented in the variable section of the online man-page documentation, (search for CMAKE_COMMAND and not ${CMAKE_COMMAND}.

On the command line CMAKE -E will show you a list of portable commands useable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文